|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import os from 'os'; |
| 4 | +import path from 'path'; |
| 5 | +import { IndexCorruptedError } from '../src/errors/index.js'; |
| 6 | + |
| 7 | +const lancedb = vi.hoisted(() => ({ |
| 8 | + connect: vi.fn() |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('@lancedb/lancedb', () => ({ |
| 12 | + connect: lancedb.connect |
| 13 | +})); |
| 14 | + |
| 15 | +describe('LanceDBStorageProvider corruption detection', () => { |
| 16 | + let tempDir: string; |
| 17 | + let consoleErrorSpy: ReturnType<typeof vi.spyOn>; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'lancedb-test-')); |
| 21 | + lancedb.connect.mockReset(); |
| 22 | + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(async () => { |
| 26 | + consoleErrorSpy.mockRestore(); |
| 27 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('throws IndexCorruptedError when vector column missing during initialize()', async () => { |
| 31 | + const dropTable = vi.fn(async () => {}); |
| 32 | + const db = { |
| 33 | + tableNames: vi.fn(async () => ['code_chunks']), |
| 34 | + openTable: vi.fn(async () => ({ |
| 35 | + schema: vi.fn(async () => ({ fields: [{ name: 'id' }] })) |
| 36 | + })), |
| 37 | + dropTable |
| 38 | + }; |
| 39 | + |
| 40 | + lancedb.connect.mockResolvedValue(db); |
| 41 | + |
| 42 | + const { LanceDBStorageProvider } = await import('../src/storage/lancedb.js'); |
| 43 | + const provider = new LanceDBStorageProvider(); |
| 44 | + |
| 45 | + await expect(provider.initialize(tempDir)).rejects.toBeInstanceOf(IndexCorruptedError); |
| 46 | + expect(dropTable).toHaveBeenCalledWith('code_chunks'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('throws IndexCorruptedError when schema validation fails during initialize()', async () => { |
| 50 | + const dropTable = vi.fn(async () => {}); |
| 51 | + const db = { |
| 52 | + tableNames: vi.fn(async () => ['code_chunks']), |
| 53 | + openTable: vi.fn(async () => ({ |
| 54 | + schema: vi.fn(async () => { |
| 55 | + throw new Error('schema error'); |
| 56 | + }) |
| 57 | + })), |
| 58 | + dropTable |
| 59 | + }; |
| 60 | + |
| 61 | + lancedb.connect.mockResolvedValue(db); |
| 62 | + |
| 63 | + const { LanceDBStorageProvider } = await import('../src/storage/lancedb.js'); |
| 64 | + const provider = new LanceDBStorageProvider(); |
| 65 | + |
| 66 | + await expect(provider.initialize(tempDir)).rejects.toBeInstanceOf(IndexCorruptedError); |
| 67 | + expect(dropTable).toHaveBeenCalledWith('code_chunks'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('throws IndexCorruptedError when vector search fails with "No vector column"', async () => { |
| 71 | + const { LanceDBStorageProvider } = await import('../src/storage/lancedb.js'); |
| 72 | + const provider = new LanceDBStorageProvider() as any; |
| 73 | + |
| 74 | + const query = { |
| 75 | + limit: vi.fn(() => query), |
| 76 | + where: vi.fn(() => query), |
| 77 | + toArray: vi.fn(async () => { |
| 78 | + throw new Error('Schema Error: No vector column found to create index'); |
| 79 | + }) |
| 80 | + }; |
| 81 | + |
| 82 | + provider.initialized = true; |
| 83 | + provider.table = { |
| 84 | + vectorSearch: vi.fn(() => query) |
| 85 | + }; |
| 86 | + |
| 87 | + await expect(provider.search([0.1, 0.2], 5)).rejects.toBeInstanceOf(IndexCorruptedError); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
0 commit comments