| 1 | import { jest } from "@jest/globals"; |
| 2 | import lance from "vectordb"; |
| 3 | |
| 4 | import { testConfigHandler, testIde } from "../test/fixtures"; |
| 5 | import { getLanceDbPath } from "../util/paths"; |
| 6 | |
| 7 | import { LanceDbIndex } from "./LanceDbIndex"; |
| 8 | import { DatabaseConnection, SqliteDb } from "./refreshIndex"; |
| 9 | import { |
| 10 | mockPathAndCacheKey, |
| 11 | mockTag, |
| 12 | updateIndexAndAwaitGenerator, |
| 13 | } from "./test/indexing"; |
| 14 | import { IndexResultType } from "./types"; |
| 15 | |
| 16 | jest.useFakeTimers(); |
| 17 | |
| 18 | describe.skip("ChunkCodebaseIndex", () => { |
| 19 | let index: LanceDbIndex; |
| 20 | let sqliteDb: DatabaseConnection; |
| 21 | let lanceDb: lance.Connection; |
| 22 | |
| 23 | async function getAllSqliteLanceDbCache() { |
| 24 | return await sqliteDb.all("SELECT * FROM lance_db_cache"); |
| 25 | } |
| 26 | |
| 27 | beforeAll(async () => { |
| 28 | const { config: mockConfig } = await testConfigHandler.loadConfig(); |
| 29 | if (!mockConfig) { |
| 30 | throw new Error("Failed to load config"); |
| 31 | } |
| 32 | if (!mockConfig.selectedModelByRole.embed) { |
| 33 | throw new Error("No embeddings model selected"); |
| 34 | } |
| 35 | |
| 36 | index = (await LanceDbIndex.create( |
| 37 | mockConfig.selectedModelByRole.embed, |
| 38 | testIde.readFile.bind(testIde), |
| 39 | ))!; |
| 40 | |
| 41 | sqliteDb = await SqliteDb.get(); |
| 42 | lanceDb = await lance.connect(getLanceDbPath()); |
| 43 | }); |
| 44 | |
| 45 | /** |
| 46 | * Some notes on quirks of this test: |
| 47 | * - Currently, it appears that there is a race condition in LanceDB that |
| 48 | * requires us to re-open the table connection each time we want to read |
| 49 | * from it. Attempting to follow these docs did not resolve the issue: |
| 50 | * https://lancedb.github.io/lancedb/guides/tables/#consistency |
| 51 | * |
| 52 | * - We cannot open either the 'dev' or 'main' tag tables ubtil after we |
| 53 | * can assume they've been created through `updateIndexAndAwaitGenerator` |
| 54 | */ |
| 55 | it("should update the index and maintain expected database state", async () => { |
| 56 | const mainTag = mockTag; |
| 57 | const mainTagTableName = index.tableNameForTag(mainTag); |
| 58 | |
| 59 | const devTag = { ...mockTag, branch: "dev" }; |
| 60 | const devTagTableName = index.tableNameForTag(devTag); |
| 61 | |
| 62 | const mockMarkComplete = jest |
| 63 | .fn() |
| 64 | .mockImplementation(() => Promise.resolve()) as any; |
| 65 | |
| 66 | // Compute test - inserts into both LanceDB and Sqlite |
| 67 | await updateIndexAndAwaitGenerator( |
| 68 | index, |
| 69 | "compute", |
| 70 | mockMarkComplete, |
| 71 | mainTag, |
| 72 | ); |
| 73 | let mainTagTable = await lanceDb.openTable(mainTagTableName); |
| 74 | expect(await mainTagTable.countRows()).toBe(1); |
| 75 | expect((await getAllSqliteLanceDbCache()).length).toBe(1); |
| 76 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 77 | [mockPathAndCacheKey], |
| 78 | IndexResultType.Compute, |
| 79 | ); |
| 80 | |
| 81 | // AddTag test - only inserts into Lance DB |
| 82 | await updateIndexAndAwaitGenerator( |
| 83 | index, |
| 84 | "addTag", |
| 85 | mockMarkComplete, |
| 86 | devTag, |
| 87 | ); |
| 88 | let devTagTable = await lanceDb.openTable(devTagTableName); |
| 89 | expect(await devTagTable.countRows()).toBe(1); |
| 90 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 91 | [mockPathAndCacheKey], |
| 92 | IndexResultType.AddTag, |
| 93 | ); |
| 94 | |
| 95 | // RemoveTag - only removes from Lance DB |
| 96 | await updateIndexAndAwaitGenerator( |
| 97 | index, |
| 98 | "removeTag", |
| 99 | mockMarkComplete, |
| 100 | devTag, |
| 101 | ); |
| 102 | devTagTable = await lanceDb.openTable(devTagTableName); |
| 103 | expect(await devTagTable.countRows()).toBe(0); |
| 104 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 105 | [mockPathAndCacheKey], |
| 106 | IndexResultType.RemoveTag, |
| 107 | ); |
| 108 | |
| 109 | // Delete test - removes from LanceDB and Sqlite |
| 110 | await updateIndexAndAwaitGenerator(index, "del", mockMarkComplete, mainTag); |
| 111 | mainTagTable = await lanceDb.openTable(mainTagTableName); |
| 112 | expect(await mainTagTable.countRows()).toBe(0); |
| 113 | expect((await getAllSqliteLanceDbCache()).length).toBe(0); |
| 114 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 115 | [mockPathAndCacheKey], |
| 116 | IndexResultType.Delete, |
| 117 | ); |
| 118 | }); |
| 119 | }); |