|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { ConfigHandler } from "../../config/ConfigHandler"; |
| 4 | +import { ContinueServerClient } from "../../continueServer/stubs/client"; |
| 5 | +import { CodebaseIndexer, PauseToken } from "../../indexing/CodebaseIndexer"; |
| 6 | +import { LanceDbIndex } from "../../indexing/LanceDbIndex"; |
| 7 | +import TransformersJsEmbeddingsProvider from "../../indexing/embeddings/TransformersJsEmbeddingsProvider"; |
| 8 | +import FileSystemIde from "../../util/filesystem"; |
| 9 | +import { |
| 10 | + getIndexFolderPath, |
| 11 | + getIndexSqlitePath, |
| 12 | + getLanceDbPath, |
| 13 | +} from "../../util/paths"; |
| 14 | +import { |
| 15 | + addToTestDir, |
| 16 | + setUpTestDir, |
| 17 | + tearDownTestDir, |
| 18 | + TEST_DIR, |
| 19 | +} from "../testUtils/testDir"; |
| 20 | + |
| 21 | +const TEST_TS = `\ |
| 22 | +function main() { |
| 23 | + console.log("Hello, world!"); |
| 24 | +} |
| 25 | +
|
| 26 | +class Foo { |
| 27 | + constructor(public bar: string) {} |
| 28 | +} |
| 29 | +`; |
| 30 | + |
| 31 | +const TEST_PY = `\ |
| 32 | +def main(): |
| 33 | + print("Hello, world!") |
| 34 | +
|
| 35 | +class Foo: |
| 36 | + def __init__(self, bar: str): |
| 37 | + self.bar = bar |
| 38 | +`; |
| 39 | + |
| 40 | +const TEST_RS = `\ |
| 41 | +fn main() { |
| 42 | + println!("Hello, world!"); |
| 43 | +} |
| 44 | +
|
| 45 | +struct Foo { |
| 46 | + bar: String, |
| 47 | +} |
| 48 | +`; |
| 49 | + |
| 50 | +// These are more like integration tests, whereas we should separately test |
| 51 | +// the individual CodebaseIndex classes |
| 52 | +describe.skip("CodebaseIndexer", () => { |
| 53 | + const ide = new FileSystemIde(TEST_DIR); |
| 54 | + const ideSettingsPromise = ide.getIdeSettings(); |
| 55 | + const configHandler = new ConfigHandler( |
| 56 | + ide, |
| 57 | + ideSettingsPromise, |
| 58 | + async (text) => {}, |
| 59 | + ); |
| 60 | + const pauseToken = new PauseToken(false); |
| 61 | + const continueServerClient = new ContinueServerClient(undefined, undefined); |
| 62 | + const codebaseIndexer = new CodebaseIndexer( |
| 63 | + configHandler, |
| 64 | + ide, |
| 65 | + pauseToken, |
| 66 | + continueServerClient, |
| 67 | + ); |
| 68 | + const lancedbIndex = new LanceDbIndex( |
| 69 | + new TransformersJsEmbeddingsProvider(), |
| 70 | + ide.readFile.bind(ide), |
| 71 | + continueServerClient, |
| 72 | + ); |
| 73 | + |
| 74 | + beforeAll(async () => { |
| 75 | + setUpTestDir(); |
| 76 | + }); |
| 77 | + |
| 78 | + afterAll(async () => { |
| 79 | + tearDownTestDir(); |
| 80 | + }); |
| 81 | + |
| 82 | + test("should index test folder without problem", async () => { |
| 83 | + addToTestDir([ |
| 84 | + ["test.ts", TEST_TS], |
| 85 | + ["py/main.py", TEST_PY], |
| 86 | + ]); |
| 87 | + const abortController = new AbortController(); |
| 88 | + const abortSignal = abortController.signal; |
| 89 | + |
| 90 | + const updates = []; |
| 91 | + for await (const update of codebaseIndexer.refresh( |
| 92 | + [TEST_DIR], |
| 93 | + abortSignal, |
| 94 | + )) { |
| 95 | + updates.push(update); |
| 96 | + } |
| 97 | + |
| 98 | + expect(updates.length).toBeGreaterThan(0); |
| 99 | + }); |
| 100 | + |
| 101 | + test("should have created index folder with all necessary files", async () => { |
| 102 | + expect(fs.existsSync(getIndexFolderPath())).toBe(true); |
| 103 | + expect(fs.existsSync(getIndexSqlitePath())).toBe(true); |
| 104 | + expect(fs.existsSync(getLanceDbPath())).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + test("should be able to query lancedb index", async () => { |
| 108 | + const chunks = await lancedbIndex.retrieve( |
| 109 | + "What is the main function doing?", |
| 110 | + 10, |
| 111 | + await ide.getTags(lancedbIndex.artifactId), |
| 112 | + undefined, |
| 113 | + ); |
| 114 | + |
| 115 | + expect(chunks.length).toBe(2); |
| 116 | + // Check that the main function from both files is returned |
| 117 | + expect(chunks.some((chunk) => chunk.filepath.endsWith("test.ts"))).toBe( |
| 118 | + true, |
| 119 | + ); |
| 120 | + expect(chunks.some((chunk) => chunk.filepath.endsWith("main.py"))).toBe( |
| 121 | + true, |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + test("should successfully re-index after adding a file", async () => { |
| 126 | + addToTestDir([["main.rs", TEST_RS]]); |
| 127 | + const abortController = new AbortController(); |
| 128 | + const abortSignal = abortController.signal; |
| 129 | + const updates = []; |
| 130 | + for await (const update of codebaseIndexer.refresh( |
| 131 | + [TEST_DIR], |
| 132 | + abortSignal, |
| 133 | + )) { |
| 134 | + updates.push(update); |
| 135 | + } |
| 136 | + expect(updates.length).toBeGreaterThan(0); |
| 137 | + // Check that the new file was indexed |
| 138 | + const chunks = await lancedbIndex.retrieve( |
| 139 | + "What is the main function doing?", |
| 140 | + 3, |
| 141 | + await ide.getTags(lancedbIndex.artifactId), |
| 142 | + undefined, |
| 143 | + ); |
| 144 | + expect(chunks.length).toBe(3); |
| 145 | + expect(chunks.some((chunk) => chunk.filepath.endsWith("main.rs"))).toBe( |
| 146 | + true, |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + test("should successfully re-index after deleting a file", async () => { |
| 151 | + fs.rmSync(path.join(TEST_DIR, "main.rs")); |
| 152 | + const abortController = new AbortController(); |
| 153 | + const abortSignal = abortController.signal; |
| 154 | + const updates = []; |
| 155 | + for await (const update of codebaseIndexer.refresh( |
| 156 | + [TEST_DIR], |
| 157 | + abortSignal, |
| 158 | + )) { |
| 159 | + updates.push(update); |
| 160 | + } |
| 161 | + expect(updates.length).toBeGreaterThan(0); |
| 162 | + // Check that the deleted file was removed from the index |
| 163 | + const chunks = await lancedbIndex.retrieve( |
| 164 | + "What is the main function doing?", |
| 165 | + 10, |
| 166 | + await ide.getTags(lancedbIndex.artifactId), |
| 167 | + undefined, |
| 168 | + ); |
| 169 | + expect(chunks.length).toBe(2); |
| 170 | + expect(chunks.every((chunk) => !chunk.filepath.endsWith("main.rs"))).toBe( |
| 171 | + true, |
| 172 | + ); |
| 173 | + }); |
| 174 | +}); |
0 commit comments