| 1 | import { jest } from "@jest/globals"; |
| 2 | |
| 3 | import { testIde } from "../test/fixtures"; |
| 4 | import { |
| 5 | insertMockChunks, |
| 6 | mockPathAndCacheKey, |
| 7 | updateIndexAndAwaitGenerator, |
| 8 | } from "./test/indexing"; |
| 9 | |
| 10 | import { CodeSnippetsCodebaseIndex } from "./CodeSnippetsIndex"; |
| 11 | import { DatabaseConnection, SqliteDb } from "./refreshIndex"; |
| 12 | import { IndexResultType } from "./types"; |
| 13 | |
| 14 | describe.skip("CodeSnippetsCodebaseIndex", () => { |
| 15 | let index: CodeSnippetsCodebaseIndex; |
| 16 | let db: DatabaseConnection; |
| 17 | |
| 18 | async function getAllSnippets() { |
| 19 | return await db.all("SELECT * FROM code_snippets"); |
| 20 | } |
| 21 | |
| 22 | async function getAllSnippetTags() { |
| 23 | return await db.all("SELECT * FROM code_snippets_tags"); |
| 24 | } |
| 25 | |
| 26 | beforeAll(async () => { |
| 27 | db = await SqliteDb.get(); |
| 28 | index = new CodeSnippetsCodebaseIndex(testIde); |
| 29 | }); |
| 30 | |
| 31 | it("should update the index and maintain expected database state", async () => { |
| 32 | const mockMarkComplete = jest |
| 33 | .fn() |
| 34 | .mockImplementation(() => Promise.resolve()) as any; |
| 35 | |
| 36 | const mockSnippet = { |
| 37 | title: "", |
| 38 | content: "", |
| 39 | signature: "", |
| 40 | startLine: 0, |
| 41 | endLine: 1, |
| 42 | }; |
| 43 | |
| 44 | // We mock this fn since currently in testing the directory structure to access the tree-sitter |
| 45 | // binaries does not match what is in the release environment. |
| 46 | jest |
| 47 | .spyOn(CodeSnippetsCodebaseIndex.prototype, "getSnippetsInFile") |
| 48 | .mockResolvedValue([mockSnippet]); |
| 49 | |
| 50 | await insertMockChunks(); |
| 51 | |
| 52 | // Compute test |
| 53 | await updateIndexAndAwaitGenerator(index, "compute", mockMarkComplete); |
| 54 | expect((await getAllSnippetTags()).length).toBe(1); |
| 55 | expect((await getAllSnippets()).length).toBe(1); |
| 56 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 57 | [mockPathAndCacheKey], |
| 58 | IndexResultType.Compute, |
| 59 | ); |
| 60 | |
| 61 | // RemoveTag test |
| 62 | await updateIndexAndAwaitGenerator(index, "removeTag", mockMarkComplete); |
| 63 | expect((await getAllSnippetTags()).length).toBe(0); |
| 64 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 65 | [mockPathAndCacheKey], |
| 66 | IndexResultType.RemoveTag, |
| 67 | ); |
| 68 | |
| 69 | // AddTag test |
| 70 | await updateIndexAndAwaitGenerator(index, "addTag", mockMarkComplete); |
| 71 | expect((await getAllSnippetTags()).length).toBe(1); |
| 72 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 73 | [mockPathAndCacheKey], |
| 74 | IndexResultType.AddTag, |
| 75 | ); |
| 76 | |
| 77 | // Delete test |
| 78 | await updateIndexAndAwaitGenerator(index, "del", mockMarkComplete); |
| 79 | expect((await getAllSnippetTags()).length).toBe(0); |
| 80 | expect((await getAllSnippets()).length).toBe(0); |
| 81 | expect(mockMarkComplete).toHaveBeenCalledWith( |
| 82 | [mockPathAndCacheKey], |
| 83 | IndexResultType.Delete, |
| 84 | ); |
| 85 | }); |
| 86 | }); |