| 1 | import { jest } from "@jest/globals"; |
| 2 | import { execSync } from "node:child_process"; |
| 3 | import fs from "node:fs"; |
| 4 | import path from "path"; |
| 5 | |
| 6 | import { ContinueServerClient } from "../continueServer/stubs/client.js"; |
| 7 | import { testConfigHandler, testIde } from "../test/fixtures.js"; |
| 8 | import { |
| 9 | addToTestDir, |
| 10 | setUpTestDir, |
| 11 | tearDownTestDir, |
| 12 | TEST_DIR, |
| 13 | TEST_DIR_PATH, |
| 14 | } from "../test/testDir.js"; |
| 15 | import { getIndexSqlitePath } from "../util/paths.js"; |
| 16 | |
| 17 | import { localPathToUri } from "../util/pathToUri.js"; |
| 18 | import { CodebaseIndexer, PauseToken } from "./CodebaseIndexer.js"; |
| 19 | import { getComputeDeleteAddRemove } from "./refreshIndex.js"; |
| 20 | import { TestCodebaseIndex } from "./TestCodebaseIndex.js"; |
| 21 | import { CodebaseIndex } from "./types.js"; |
| 22 | import { walkDir, walkDirCache } from "./walkDir.js"; |
| 23 | |
| 24 | jest.useFakeTimers(); |
| 25 | |
| 26 | const TEST_TS = `\ |
| 27 | function main() { |
| 28 | console.log("Hello, world!"); |
| 29 | } |
| 30 | |
| 31 | class Foo { |
| 32 | constructor(public bar: string) {} |
| 33 | } |
| 34 | `; |
| 35 | |
| 36 | const TEST_PY = `\ |
| 37 | def main(): |
| 38 | print("Hello, world!") |
| 39 | |
| 40 | class Foo: |
| 41 | def __init__(self, bar: str): |
| 42 | self.bar = bar |
| 43 | `; |
| 44 | |
| 45 | const TEST_RS = `\ |
| 46 | fn main() { |
| 47 | println!("Hello, world!"); |
| 48 | } |
| 49 | |
| 50 | struct Foo { |
| 51 | bar: String, |
| 52 | } |
| 53 | `; |
| 54 | |
| 55 | // A subclass of CodebaseIndexer that adds a new CodebaseIndex |
| 56 | class TestCodebaseIndexer extends CodebaseIndexer { |
| 57 | protected async getIndexesToBuild(): Promise<CodebaseIndex[]> { |
| 58 | return [new TestCodebaseIndex()]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // These are more like integration tests, whereas we should separately test |
| 63 | // the individual CodebaseIndex classes |
| 64 | describe("CodebaseIndexer", () => { |
| 65 | const pauseToken = new PauseToken(false); |
| 66 | const continueServerClient = new ContinueServerClient(undefined, undefined); |
| 67 | const codebaseIndexer = new TestCodebaseIndexer( |
| 68 | testConfigHandler, |
| 69 | testIde, |
| 70 | pauseToken, |
| 71 | continueServerClient, |
| 72 | ); |
| 73 | const testIndex = new TestCodebaseIndex(); |
| 74 | |
| 75 | beforeAll(async () => { |
| 76 | tearDownTestDir(); |
| 77 | setUpTestDir(); |
| 78 | |
| 79 | execSync("git init", { cwd: TEST_DIR_PATH }); |
| 80 | execSync('git config user.email "test@example.com"', { |
| 81 | cwd: TEST_DIR_PATH, |
| 82 | }); |
| 83 | execSync('git config user.name "Test"', { cwd: TEST_DIR_PATH }); |
| 84 | }); |
| 85 | |
| 86 | afterAll(async () => { |
| 87 | tearDownTestDir(); |
| 88 | }); |
| 89 | |
| 90 | afterEach(() => { |
| 91 | walkDirCache.invalidate(); |
| 92 | }); |
| 93 | |
| 94 | async function refreshIndex() { |
| 95 | const abortController = new AbortController(); |
| 96 | const abortSignal = abortController.signal; |
| 97 | |
| 98 | const updates = []; |
| 99 | for await (const update of codebaseIndexer.refreshDirs( |
| 100 | [TEST_DIR], |
| 101 | abortSignal, |
| 102 | )) { |
| 103 | updates.push(update); |
| 104 | } |
| 105 | return updates; |
| 106 | } |
| 107 | |
| 108 | async function refreshIndexFiles(files: string[]) { |
| 109 | const updates = []; |
| 110 | for await (const update of codebaseIndexer.refreshFiles(files)) { |
| 111 | updates.push(update); |
| 112 | } |
| 113 | return updates; |
| 114 | } |
| 115 | |
| 116 | async function getAllIndexedFiles() { |
| 117 | const files = await testIndex.getIndexedFilesForTags( |
| 118 | await testIde.getTags(testIndex.artifactId), |
| 119 | ); |
| 120 | return files; |
| 121 | } |
| 122 | |
| 123 | async function getIndexPlan() { |
| 124 | const workspaceFiles = await walkDir(TEST_DIR, testIde); |
| 125 | const [tag] = await testIde.getTags(testIndex.artifactId); |
| 126 | const stats = await testIde.getFileStats(workspaceFiles); |
| 127 | |
| 128 | const [results, lastUpdated, markComplete] = |
| 129 | await getComputeDeleteAddRemove( |
| 130 | tag, |
| 131 | { ...stats }, |
| 132 | (filepath) => testIde.readFile(filepath), |
| 133 | undefined, |
| 134 | ); |
| 135 | return results; |
| 136 | } |
| 137 | |
| 138 | async function expectPlan( |
| 139 | compute: number, |
| 140 | addTag: number, |
| 141 | removeTag: number, |
| 142 | del: number, |
| 143 | ) { |
| 144 | const plan = await getIndexPlan(); |
| 145 | expect(plan.compute).toHaveLength(compute); |
| 146 | expect(plan.addTag).toHaveLength(addTag); |
| 147 | expect(plan.removeTag).toHaveLength(removeTag); |
| 148 | expect(plan.del).toHaveLength(del); |
| 149 | return plan; |
| 150 | } |
| 151 | |
| 152 | test("should index test folder without problem", async () => { |
| 153 | addToTestDir([ |
| 154 | ["test.ts", TEST_TS], |
| 155 | ["py/main.py", TEST_PY], |
| 156 | ]); |
| 157 | |
| 158 | await expectPlan(2, 0, 0, 0); |
| 159 | |
| 160 | const updates = await refreshIndex(); |
| 161 | expect(updates.length).toBeGreaterThan(0); |
| 162 | }); |
| 163 | |
| 164 | test("should have created index folder with all necessary files", async () => { |
| 165 | const exists = await testIde.fileExists( |
| 166 | localPathToUri(getIndexSqlitePath()), |
| 167 | ); |
| 168 | expect(exists).toBe(true); |
| 169 | }); |
| 170 | |
| 171 | test("should have indexed all of the files", async () => { |
| 172 | const indexed = await getAllIndexedFiles(); |
| 173 | expect(indexed.length).toBe(2); |
| 174 | expect(indexed.some((file) => file.endsWith("test.ts"))).toBe(true); |
| 175 | expect(indexed.some((file) => file.endsWith("main.py"))).toBe(true); |
| 176 | }); |
| 177 | |
| 178 | test("should successfuly re-index specific files", async () => { |
| 179 | // Could add more specific tests for this but uses similar logic |
| 180 | const before = await getAllIndexedFiles(); |
| 181 | await refreshIndexFiles(before); |
| 182 | |
| 183 | const after = await getAllIndexedFiles(); |
| 184 | expect(after.length).toBe(before.length); |
| 185 | }); |
| 186 | |
| 187 | test("should successfully re-index after adding a file", async () => { |
| 188 | addToTestDir([["main.rs", TEST_RS]]); |
| 189 | |
| 190 | await expectPlan(1, 0, 0, 0); |
| 191 | |
| 192 | const updates = await refreshIndex(); |
| 193 | expect(updates.length).toBeGreaterThan(0); |
| 194 | |
| 195 | // Check that the new file was indexed |
| 196 | const files = await getAllIndexedFiles(); |
| 197 | expect(files.length).toBe(3); |
| 198 | expect(files.some((file) => file.endsWith("main.rs"))).toBe(true); |
| 199 | }); |
| 200 | |
| 201 | test("should successfully re-index after deleting a file", async () => { |
| 202 | fs.rmSync(path.join(TEST_DIR_PATH, "main.rs")); |
| 203 | |
| 204 | await expectPlan(0, 0, 0, 1); |
| 205 | |
| 206 | const updates = await refreshIndex(); |
| 207 | expect(updates.length).toBeGreaterThan(0); |
| 208 | |
| 209 | // Check that the deleted file was removed from the index |
| 210 | const files = await getAllIndexedFiles(); |
| 211 | expect(files.length).toBe(2); |
| 212 | expect(files.every((file) => !file.endsWith("main.rs"))).toBe(true); |
| 213 | }); |
| 214 | |
| 215 | test("shouldn't index any files when nothing changed", async () => { |
| 216 | await expectPlan(0, 0, 0, 0); |
| 217 | const updates = await refreshIndex(); |
| 218 | expect(updates.length).toBeGreaterThan(0); |
| 219 | }); |
| 220 | |
| 221 | test("should create git repo for testing", async () => { |
| 222 | execSync( |
| 223 | `cd ${TEST_DIR_PATH} && git init && git checkout -b main && git add -A && git commit -m "First commit"`, |
| 224 | ); |
| 225 | }); |
| 226 | |
| 227 | test.skip("should only re-index the changed files when changing branches", async () => { |
| 228 | execSync(`cd ${TEST_DIR_PATH} && git checkout -b test2`); |
| 229 | // Rewriting the file |
| 230 | addToTestDir([["test.ts", "// This is different"]]); |
| 231 | |
| 232 | // Should re-compute test.ts, but just re-tag the .py file |
| 233 | await expectPlan(1, 1, 0, 0); |
| 234 | |
| 235 | execSync( |
| 236 | `cd ${TEST_DIR_PATH} && git add -A && git commit -m "Change .ts file"`, |
| 237 | ); |
| 238 | }); |
| 239 | |
| 240 | test.skip("shouldn't re-index anything when changing back to original branch", async () => { |
| 241 | execSync(`cd ${TEST_DIR_PATH} && git checkout main`); |
| 242 | await expectPlan(0, 0, 0, 0); |
| 243 | }); |
| 244 | }); |