|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import { getDbConnection, resetDbConnection, closeDbConnection } from '@/lib/db/client' |
| 3 | + |
| 4 | +/** |
| 5 | + * Unit Tests for LanceDB Auto-Initialization |
| 6 | + * |
| 7 | + * Tests the automatic schema initialization that occurs on first connection. |
| 8 | + * Validates that tables are created automatically in production environments. |
| 9 | + * |
| 10 | + * Maps to PR #186 - Fix LanceDB schema initialization in production |
| 11 | + */ |
| 12 | + |
| 13 | +describe('LanceDB Auto-Initialization', () => { |
| 14 | + beforeEach(async () => { |
| 15 | + // Reset connection state before each test |
| 16 | + resetDbConnection() |
| 17 | + }) |
| 18 | + |
| 19 | + afterEach(async () => { |
| 20 | + // Clean up after each test |
| 21 | + await closeDbConnection() |
| 22 | + }) |
| 23 | + |
| 24 | + describe('Schema Initialization on First Connection', () => { |
| 25 | + it('should initialize schema automatically on first getDbConnection call', async () => { |
| 26 | + // Get connection - this should trigger auto-initialization |
| 27 | + const db = await getDbConnection() |
| 28 | + |
| 29 | + // Verify connection is established |
| 30 | + expect(db).toBeDefined() |
| 31 | + |
| 32 | + // Verify tables were created |
| 33 | + const tableNames = await db.tableNames() |
| 34 | + expect(tableNames).toContain('messages') |
| 35 | + expect(tableNames).toContain('flashcards') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should not re-initialize schema on subsequent getDbConnection calls', async () => { |
| 39 | + // First call - initializes schema |
| 40 | + const db1 = await getDbConnection() |
| 41 | + const tablesAfterFirst = await db1.tableNames() |
| 42 | + |
| 43 | + // Second call - should return same connection without re-initializing |
| 44 | + const db2 = await getDbConnection() |
| 45 | + const tablesAfterSecond = await db2.tableNames() |
| 46 | + |
| 47 | + // Should be the same connection instance |
| 48 | + expect(db1).toBe(db2) |
| 49 | + |
| 50 | + // Tables should be the same |
| 51 | + expect(tablesAfterSecond).toEqual(tablesAfterFirst) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle schema initialization errors gracefully', async () => { |
| 55 | + // This test verifies that if schema init fails, the app continues |
| 56 | + // In a real error scenario, getDbConnection would still return a connection |
| 57 | + // but operations would fail with error logging |
| 58 | + |
| 59 | + const db = await getDbConnection() |
| 60 | + |
| 61 | + // Connection should still be established even if there were init errors |
| 62 | + expect(db).toBeDefined() |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe('Schema Initialization State Management', () => { |
| 67 | + it('should reset schema initialization flag when resetDbConnection is called', async () => { |
| 68 | + // First connection - initializes schema |
| 69 | + await getDbConnection() |
| 70 | + |
| 71 | + // Reset connection |
| 72 | + resetDbConnection() |
| 73 | + |
| 74 | + // This would normally re-initialize if tables were missing |
| 75 | + // In tests, tables persist, so we're just verifying the reset works |
| 76 | + const db = await getDbConnection() |
| 77 | + expect(db).toBeDefined() |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + describe('Table Creation', () => { |
| 82 | + it('should create messages table with correct schema', async () => { |
| 83 | + const db = await getDbConnection() |
| 84 | + const table = await db.openTable('messages') |
| 85 | + |
| 86 | + // Verify table exists and is accessible |
| 87 | + expect(table).toBeDefined() |
| 88 | + |
| 89 | + // Tables should be empty after init (init rows are cleaned up) |
| 90 | + const count = await table.countRows() |
| 91 | + expect(count).toBeGreaterThanOrEqual(0) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should create flashcards table with correct schema', async () => { |
| 95 | + const db = await getDbConnection() |
| 96 | + const table = await db.openTable('flashcards') |
| 97 | + |
| 98 | + // Verify table exists and is accessible |
| 99 | + expect(table).toBeDefined() |
| 100 | + |
| 101 | + // Tables should be empty after init (init rows are cleaned up) |
| 102 | + const count = await table.countRows() |
| 103 | + expect(count).toBeGreaterThanOrEqual(0) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + describe('Idempotency', () => { |
| 108 | + it('should handle multiple concurrent getDbConnection calls safely', async () => { |
| 109 | + // Simulate multiple concurrent calls during app startup |
| 110 | + const connections = await Promise.all([ |
| 111 | + getDbConnection(), |
| 112 | + getDbConnection(), |
| 113 | + getDbConnection(), |
| 114 | + ]) |
| 115 | + |
| 116 | + // All should return the same connection instance |
| 117 | + expect(connections[0]).toBe(connections[1]) |
| 118 | + expect(connections[1]).toBe(connections[2]) |
| 119 | + |
| 120 | + // Tables should exist |
| 121 | + const tableNames = await connections[0].tableNames() |
| 122 | + expect(tableNames).toContain('messages') |
| 123 | + expect(tableNames).toContain('flashcards') |
| 124 | + }) |
| 125 | + |
| 126 | + it('should not create duplicate tables on concurrent initialization', async () => { |
| 127 | + // Get connection multiple times concurrently |
| 128 | + await Promise.all([getDbConnection(), getDbConnection(), getDbConnection()]) |
| 129 | + |
| 130 | + const db = await getDbConnection() |
| 131 | + const tableNames = await db.tableNames() |
| 132 | + |
| 133 | + // Should have exactly these tables (no duplicates) |
| 134 | + const messagesTables = tableNames.filter((t) => t === 'messages') |
| 135 | + const flashcardsTables = tableNames.filter((t) => t === 'flashcards') |
| 136 | + |
| 137 | + expect(messagesTables).toHaveLength(1) |
| 138 | + expect(flashcardsTables).toHaveLength(1) |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + describe('Integration with Existing Schema Module', () => { |
| 143 | + it('should use the existing initializeSchema function', async () => { |
| 144 | + // Verify that auto-initialization delegates to schema.ts |
| 145 | + const db = await getDbConnection() |
| 146 | + |
| 147 | + // If schema.ts is being used, tables will have the correct structure |
| 148 | + const tableNames = await db.tableNames() |
| 149 | + expect(tableNames).toContain('messages') |
| 150 | + expect(tableNames).toContain('flashcards') |
| 151 | + |
| 152 | + // Verify we can open both tables (confirms they were created correctly) |
| 153 | + const messagesTable = await db.openTable('messages') |
| 154 | + const flashcardsTable = await db.openTable('flashcards') |
| 155 | + |
| 156 | + expect(messagesTable).toBeDefined() |
| 157 | + expect(flashcardsTable).toBeDefined() |
| 158 | + }) |
| 159 | + }) |
| 160 | +}) |
0 commit comments