| 1 | /* eslint-disable max-lines-per-function */ |
| 2 | /* lint is not useful for test classes */ |
| 3 | import { jest } from "@jest/globals"; |
| 4 | import { execSync } from "node:child_process"; |
| 5 | import fs from "node:fs"; |
| 6 | import path from "path"; |
| 7 | |
| 8 | import { LLMError } from "../llm/index.js"; |
| 9 | import { testConfigHandler, testIde } from "../test/fixtures.js"; |
| 10 | import { |
| 11 | addToTestDir, |
| 12 | setUpTestDir, |
| 13 | tearDownTestDir, |
| 14 | TEST_DIR, |
| 15 | TEST_DIR_PATH, |
| 16 | } from "../test/testDir.js"; |
| 17 | import { getIndexSqlitePath } from "../util/paths.js"; |
| 18 | |
| 19 | import { ConfigResult } from "@continuedev/config-yaml"; |
| 20 | import CodebaseContextProvider from "../context/providers/CodebaseContextProvider.js"; |
| 21 | import { ContinueConfig } from "../index.js"; |
| 22 | import { localPathToUri } from "../util/pathToUri.js"; |
| 23 | import { CodebaseIndexer } from "./CodebaseIndexer.js"; |
| 24 | import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex.js"; |
| 25 | import { getComputeDeleteAddRemove } from "./refreshIndex.js"; |
| 26 | import { TestCodebaseIndex } from "./TestCodebaseIndex.js"; |
| 27 | import { CodebaseIndex } from "./types.js"; |
| 28 | import { walkDir, walkDirCache } from "./walkDir.js"; |
| 29 | |
| 30 | jest.useFakeTimers(); |
| 31 | |
| 32 | const TEST_TS = `\ |
| 33 | function main() { |
| 34 | console.log("Hello, world!"); |
| 35 | } |
| 36 | |
| 37 | class Foo { |
| 38 | constructor(public bar: string) {} |
| 39 | } |
| 40 | `; |
| 41 | |
| 42 | const TEST_PY = `\ |
| 43 | def main(): |
| 44 | print("Hello, world!") |
| 45 | |
| 46 | class Foo: |
| 47 | def __init__(self, bar: str): |
| 48 | self.bar = bar |
| 49 | `; |
| 50 | |
| 51 | const TEST_RS = `\ |
| 52 | fn main() { |
| 53 | println!("Hello, world!"); |
| 54 | } |
| 55 | |
| 56 | struct Foo { |
| 57 | bar: String, |
| 58 | } |
| 59 | `; |
| 60 | |
| 61 | // A subclass of CodebaseIndexer that adds a new CodebaseIndex |
| 62 | class TestCodebaseIndexer extends CodebaseIndexer { |
| 63 | protected async getIndexesToBuild(): Promise<CodebaseIndex[]> { |
| 64 | return [new TestCodebaseIndex()]; |
| 65 | } |
| 66 | |
| 67 | // Add public methods to test private methods |
| 68 | public testHasIndexingProvider() { |
| 69 | return (this as any).hasIndexingContextProvider(); |
| 70 | } |
| 71 | |
| 72 | public async testHandleConfigUpdate( |
| 73 | configResult: ConfigResult<ContinueConfig>, |
| 74 | ) { |
| 75 | return (this as any).handleConfigUpdate({ config: configResult.config }); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Create a mock messenger type that doesn't require actual protocol imports |
| 80 | type MockMessengerType = { |
| 81 | send: jest.Mock; |
| 82 | request: jest.Mock; |
| 83 | invoke: jest.Mock; |
| 84 | on: jest.Mock; |
| 85 | onError: jest.Mock; |
| 86 | }; |
| 87 | |
| 88 | // These are more like integration tests, whereas we should separately test |
| 89 | // the individual CodebaseIndex classes |
| 90 | describe("CodebaseIndexer", () => { |
| 91 | // Replace mockProgressReporter with mockMessenger |
| 92 | const mockMessenger: MockMessengerType = { |
| 93 | send: jest.fn(), |
| 94 | request: jest.fn(async () => {}), |
| 95 | invoke: jest.fn(), |
| 96 | on: jest.fn(), |
| 97 | onError: jest.fn(), |
| 98 | }; |
| 99 | |
| 100 | let codebaseIndexer: TestCodebaseIndexer; |
| 101 | let testIndex: TestCodebaseIndex; |
| 102 | |
| 103 | beforeAll(async () => { |
| 104 | tearDownTestDir(); |
| 105 | setUpTestDir(); |
| 106 | |
| 107 | execSync("git init", { cwd: TEST_DIR_PATH }); |
| 108 | execSync('git config user.email "test@example.com"', { |
| 109 | cwd: TEST_DIR_PATH, |
| 110 | }); |
| 111 | execSync('git config user.name "Test"', { cwd: TEST_DIR_PATH }); |
| 112 | |
| 113 | codebaseIndexer = new TestCodebaseIndexer( |
| 114 | testConfigHandler, |
| 115 | testIde, |
| 116 | mockMessenger as any, |
| 117 | false, |
| 118 | ); |
| 119 | await codebaseIndexer.initPromise; |
| 120 | testIndex = new TestCodebaseIndex(); |
| 121 | }); |
| 122 | |
| 123 | afterAll(async () => { |
| 124 | tearDownTestDir(); |
| 125 | }); |
| 126 | |
| 127 | afterEach(() => { |
| 128 | walkDirCache.invalidate(); |
| 129 | jest.clearAllMocks(); |
| 130 | }); |
| 131 | |
| 132 | async function refreshIndex() { |
| 133 | const abortController = new AbortController(); |
| 134 | const abortSignal = abortController.signal; |
| 135 | |
| 136 | const updates = []; |
| 137 | for await (const update of codebaseIndexer.refreshDirs( |
| 138 | [TEST_DIR], |
| 139 | abortSignal, |
| 140 | )) { |
| 141 | updates.push(update); |
| 142 | } |
| 143 | return updates; |
| 144 | } |
| 145 | |
| 146 | async function getAllIndexedFiles() { |
| 147 | const files = await testIndex.getIndexedFilesForTags( |
| 148 | await testIde.getTags(testIndex.artifactId), |
| 149 | ); |
| 150 | return files; |
| 151 | } |
| 152 | |
| 153 | async function getIndexPlan() { |
| 154 | const workspaceFiles = await walkDir(TEST_DIR, testIde); |
| 155 | const [tag] = await testIde.getTags(testIndex.artifactId); |
| 156 | const stats = await testIde.getFileStats(workspaceFiles); |
| 157 | |
| 158 | const [results, lastUpdated, markComplete] = |
| 159 | await getComputeDeleteAddRemove( |
| 160 | tag, |
| 161 | { ...stats }, |
| 162 | (filepath) => testIde.readFile(filepath), |
| 163 | undefined, |
| 164 | ); |
| 165 | return results; |
| 166 | } |
| 167 | |
| 168 | async function expectPlan( |
| 169 | compute: number, |
| 170 | addTag: number, |
| 171 | removeTag: number, |
| 172 | del: number, |
| 173 | ) { |
| 174 | const plan = await getIndexPlan(); |
| 175 | expect(plan.compute).toHaveLength(compute); |
| 176 | expect(plan.addTag).toHaveLength(addTag); |
| 177 | expect(plan.removeTag).toHaveLength(removeTag); |
| 178 | expect(plan.del).toHaveLength(del); |
| 179 | return plan; |
| 180 | } |
| 181 | |
| 182 | describe("Sequential Integration Tests", () => { |
| 183 | // These tests must run in sequence as they depend on each other |
| 184 | test("should index test folder without problem", async () => { |
| 185 | walkDirCache.invalidate(); |
| 186 | addToTestDir([ |
| 187 | ["test.ts", TEST_TS], |
| 188 | ["py/main.py", TEST_PY], |
| 189 | ]); |
| 190 | |
| 191 | await expectPlan(2, 0, 0, 0); |
| 192 | |
| 193 | const updates = await refreshIndex(); |
| 194 | expect(updates.length).toBeGreaterThan(0); |
| 195 | }); |
| 196 | |
| 197 | test("should have created index folder with all necessary files", async () => { |
| 198 | const exists = await testIde.fileExists( |
| 199 | localPathToUri(getIndexSqlitePath()), |
| 200 | ); |
| 201 | expect(exists).toBe(true); |
| 202 | }); |
| 203 | |
| 204 | test("should have indexed all of the files", async () => { |
| 205 | const indexed = await getAllIndexedFiles(); |
| 206 | expect(indexed.length).toBe(2); |
| 207 | expect(indexed.some((file) => file.endsWith("test.ts"))).toBe(true); |
| 208 | expect(indexed.some((file) => file.endsWith("main.py"))).toBe(true); |
| 209 | }); |
| 210 | |
| 211 | test("should successfuly re-index specific files", async () => { |
| 212 | // Could add more specific tests for this but uses similar logic |
| 213 | const before = await getAllIndexedFiles(); |
| 214 | await codebaseIndexer.refreshCodebaseIndexFiles(before); |
| 215 | |
| 216 | const after = await getAllIndexedFiles(); |
| 217 | expect(after.length).toBe(before.length); |
| 218 | }); |
| 219 | |
| 220 | test("should successfully re-index after adding a file", async () => { |
| 221 | addToTestDir([["main.rs", TEST_RS]]); |
| 222 | |
| 223 | await expectPlan(1, 0, 0, 0); |
| 224 | |
| 225 | const updates = await refreshIndex(); |
| 226 | expect(updates.length).toBeGreaterThan(0); |
| 227 | |
| 228 | // Check that the new file was indexed |
| 229 | const files = await getAllIndexedFiles(); |
| 230 | expect(files.length).toBe(3); |
| 231 | expect(files.some((file) => file.endsWith("main.rs"))).toBe(true); |
| 232 | }); |
| 233 | |
| 234 | test("should successfully re-index after deleting a file", async () => { |
| 235 | fs.rmSync(path.join(TEST_DIR_PATH, "main.rs")); |
| 236 | |
| 237 | await expectPlan(0, 0, 0, 1); |
| 238 | |
| 239 | const updates = await refreshIndex(); |
| 240 | expect(updates.length).toBeGreaterThan(0); |
| 241 | |
| 242 | // Check that the deleted file was removed from the index |
| 243 | const files = await getAllIndexedFiles(); |
| 244 | expect(files.length).toBe(2); |
| 245 | expect(files.every((file) => !file.endsWith("main.rs"))).toBe(true); |
| 246 | }); |
| 247 | |
| 248 | test("shouldn't index any files when nothing changed", async () => { |
| 249 | await expectPlan(0, 0, 0, 0); |
| 250 | const updates = await refreshIndex(); |
| 251 | expect(updates.length).toBeGreaterThan(0); |
| 252 | }); |
| 253 | |
| 254 | test("should create git repo for testing", async () => { |
| 255 | execSync( |
| 256 | `cd ${TEST_DIR_PATH} && git init && git checkout -b main && git add -A && git commit -m "First commit"`, |
| 257 | ); |
| 258 | }); |
| 259 | |
| 260 | test.skip("should only re-index the changed files when changing branches", async () => { |
| 261 | execSync(`cd ${TEST_DIR_PATH} && git checkout -b test2`); |
| 262 | // Rewriting the file |
| 263 | addToTestDir([["test.ts", "// This is different"]]); |
| 264 | |
| 265 | // Should re-compute test.ts, but just re-tag the .py file |
| 266 | await expectPlan(1, 1, 0, 0); |
| 267 | |
| 268 | execSync( |
| 269 | `cd ${TEST_DIR_PATH} && git add -A && git commit -m "Change .ts file"`, |
| 270 | ); |
| 271 | }); |
| 272 | |
| 273 | test.skip("shouldn't re-index anything when changing back to original branch", async () => { |
| 274 | execSync(`cd ${TEST_DIR_PATH} && git checkout main`); |
| 275 | await expectPlan(0, 0, 0, 0); |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | // Isolated unit tests for Core.ts methods |
| 280 | describe("Core.ts Methods (Isolated Unit Tests)", () => { |
| 281 | beforeAll(async () => { |
| 282 | // Clear database to ensure these tests don't interfere with sequential integration tests |
| 283 | await testIndex.clearDatabase(); |
| 284 | }); |
| 285 | |
| 286 | afterEach(async () => { |
| 287 | // Clear after each test to ensure full isolation within this group |
| 288 | await testIndex.clearDatabase(); |
| 289 | }); |
| 290 | |
| 291 | // Simplified tests to focus on behavior, not implementation details |
| 292 | test("should call messenger with progress updates when refreshing codebase index", async () => { |
| 293 | jest |
| 294 | .spyOn(codebaseIndexer as any, "refreshDirs") |
| 295 | .mockImplementation(async function* () { |
| 296 | yield { status: "done", progress: 1, desc: "Completed" }; |
| 297 | }); |
| 298 | |
| 299 | await codebaseIndexer.refreshCodebaseIndex([TEST_DIR]); |
| 300 | |
| 301 | expect(mockMessenger.request).toHaveBeenCalledWith( |
| 302 | "indexProgress", |
| 303 | expect.anything(), |
| 304 | ); |
| 305 | expect(mockMessenger.send).toHaveBeenCalledWith("refreshSubmenuItems", { |
| 306 | providers: "all", |
| 307 | }); |
| 308 | }); |
| 309 | |
| 310 | test("should call messenger with progress updates when refreshing specific files", async () => { |
| 311 | jest |
| 312 | .spyOn(codebaseIndexer as any, "refreshFiles") |
| 313 | .mockImplementation(async function* () { |
| 314 | yield { status: "done", progress: 1, desc: "Completed" }; |
| 315 | }); |
| 316 | |
| 317 | await codebaseIndexer.refreshCodebaseIndexFiles(["/test/file.ts"]); |
| 318 | |
| 319 | expect(mockMessenger.request).toHaveBeenCalledWith( |
| 320 | "indexProgress", |
| 321 | expect.anything(), |
| 322 | ); |
| 323 | expect(mockMessenger.send).toHaveBeenCalledWith("refreshSubmenuItems", { |
| 324 | providers: "all", |
| 325 | }); |
| 326 | }); |
| 327 | |
| 328 | test("should abort previous indexing when starting a new one", async () => { |
| 329 | // Set up a situation where indexingCancellationController exists |
| 330 | const mockAbort = jest.fn(); |
| 331 | const controller = { abort: mockAbort, signal: { aborted: false } }; |
| 332 | |
| 333 | // Access the private property in a type-safe way for testing |
| 334 | (codebaseIndexer as any).indexingCancellationController = controller; |
| 335 | |
| 336 | // Mock refreshDirs to return immediately |
| 337 | jest |
| 338 | .spyOn(codebaseIndexer as any, "refreshDirs") |
| 339 | .mockImplementation(async function* () { |
| 340 | yield { status: "done", progress: 1, desc: "Completed" }; |
| 341 | }); |
| 342 | |
| 343 | // Start indexing - this should call abort on the existing controller |
| 344 | await codebaseIndexer.refreshCodebaseIndex([TEST_DIR]); |
| 345 | |
| 346 | // Verify abort was called |
| 347 | expect(mockAbort).toHaveBeenCalled(); |
| 348 | }); |
| 349 | |
| 350 | test("should handle errors properly during indexing", async () => { |
| 351 | const testError = new Error("Test indexing error"); |
| 352 | |
| 353 | // Mock console.log to avoid printing errors |
| 354 | const consoleLogSpy = jest |
| 355 | .spyOn(console, "log") |
| 356 | .mockImplementation(() => {}); |
| 357 | |
| 358 | // Mock refreshDirs to throw an error |
| 359 | jest |
| 360 | .spyOn(codebaseIndexer as any, "refreshDirs") |
| 361 | .mockImplementation(() => { |
| 362 | throw testError; |
| 363 | }); |
| 364 | |
| 365 | // We don't need to mock AbortController because we're mocking the entire refreshDirs call |
| 366 | await codebaseIndexer.refreshCodebaseIndex([TEST_DIR]); |
| 367 | |
| 368 | // Use the first argument only for the assertion since the second argument doesn't match exactly |
| 369 | expect(consoleLogSpy).toHaveBeenCalledWith( |
| 370 | `Failed refreshing codebase index directories: Error: ${testError.message}`, |
| 371 | ); |
| 372 | expect(mockMessenger.request).toHaveBeenCalledWith( |
| 373 | "indexProgress", |
| 374 | expect.objectContaining({ |
| 375 | status: "failed", |
| 376 | }), |
| 377 | ); |
| 378 | |
| 379 | consoleLogSpy.mockRestore(); |
| 380 | }); |
| 381 | |
| 382 | test("should handle LLMError specially during indexing", async () => { |
| 383 | // Create a mock LLM |
| 384 | const mockLlm: any = { |
| 385 | providerName: "test-provider", |
| 386 | model: "test-model", |
| 387 | }; |
| 388 | |
| 389 | // Create a real LLMError |
| 390 | const llmError = new LLMError("Test LLM error", mockLlm); |
| 391 | |
| 392 | // Mock console.log to avoid printing errors |
| 393 | jest.spyOn(console, "log").mockImplementation(() => {}); |
| 394 | |
| 395 | // Mock refreshDirs to throw an LLMError |
| 396 | jest |
| 397 | .spyOn(codebaseIndexer as any, "refreshDirs") |
| 398 | .mockImplementation(() => { |
| 399 | throw llmError; |
| 400 | }); |
| 401 | |
| 402 | // We don't need to mock AbortController because we're mocking the entire refreshDirs call |
| 403 | await codebaseIndexer.refreshCodebaseIndex([TEST_DIR]); |
| 404 | |
| 405 | expect(mockMessenger.request).toHaveBeenCalledWith( |
| 406 | "reportError", |
| 407 | llmError, |
| 408 | ); |
| 409 | expect(mockMessenger.request).toHaveBeenCalledWith( |
| 410 | "indexProgress", |
| 411 | expect.objectContaining({ |
| 412 | status: "failed", |
| 413 | desc: "Test LLM error", |
| 414 | }), |
| 415 | ); |
| 416 | }); |
| 417 | |
| 418 | test("should provide access to current indexing state", async () => { |
| 419 | const testState = { |
| 420 | progress: 0.5, |
| 421 | status: "indexing" as const, |
| 422 | desc: "Test state", |
| 423 | }; |
| 424 | |
| 425 | // Mock refreshDirs to set a specific state |
| 426 | jest |
| 427 | .spyOn(codebaseIndexer as any, "refreshDirs") |
| 428 | .mockImplementation(async function* () { |
| 429 | yield testState; |
| 430 | }); |
| 431 | |
| 432 | // We don't need to mock AbortController because we're mocking the entire refreshDirs call |
| 433 | await codebaseIndexer.refreshCodebaseIndex([TEST_DIR]); |
| 434 | |
| 435 | // Check that the state was updated |
| 436 | expect(codebaseIndexer.currentIndexingState).toEqual(testState); |
| 437 | }); |
| 438 | }); |
| 439 | |
| 440 | // Isolated unit tests for configuration handling |
| 441 | describe("Configuration Handling (Isolated Unit Tests)", () => { |
| 442 | beforeAll(async () => { |
| 443 | // Clear database to ensure these tests don't interfere with other tests |
| 444 | await testIndex.clearDatabase(); |
| 445 | }); |
| 446 | |
| 447 | afterEach(async () => { |
| 448 | // Clear after each test to ensure full isolation within this group |
| 449 | await testIndex.clearDatabase(); |
| 450 | }); |
| 451 | let testIndexer: TestCodebaseIndexer; |
| 452 | let mockRefreshCodebaseIndex: jest.MockedFunction<any>; |
| 453 | let mockGetWorkspaceDirs: jest.MockedFunction<any>; |
| 454 | |
| 455 | beforeEach(() => { |
| 456 | testIndexer = new TestCodebaseIndexer( |
| 457 | testConfigHandler, |
| 458 | testIde, |
| 459 | mockMessenger as any, |
| 460 | false, |
| 461 | ); |
| 462 | |
| 463 | // Mock the refreshCodebaseIndex method to avoid actual indexing |
| 464 | mockRefreshCodebaseIndex = jest |
| 465 | .spyOn(testIndexer, "refreshCodebaseIndex") |
| 466 | .mockImplementation(async () => {}); |
| 467 | |
| 468 | // Mock getWorkspaceDirs to return test directories |
| 469 | mockGetWorkspaceDirs = jest |
| 470 | .spyOn(testIde, "getWorkspaceDirs") |
| 471 | .mockResolvedValue(["/test/workspace"]); |
| 472 | }); |
| 473 | |
| 474 | afterEach(() => { |
| 475 | jest.clearAllMocks(); |
| 476 | }); |
| 477 | |
| 478 | describe("hasIndexingProvider", () => { |
| 479 | test("should return true when codebase context provider is present", () => { |
| 480 | // Set up config with codebase context provider |
| 481 | (testIndexer as any).config = { |
| 482 | contextProviders: [new CodebaseContextProvider({})], |
| 483 | }; |
| 484 | |
| 485 | const result = testIndexer.testHasIndexingProvider(); |
| 486 | expect(result).toBe(true); |
| 487 | }); |
| 488 | |
| 489 | test("should return false when no context providers are configured", () => { |
| 490 | (testIndexer as any).config = { |
| 491 | contextProviders: undefined, |
| 492 | }; |
| 493 | |
| 494 | const result = testIndexer.testHasIndexingProvider(); |
| 495 | expect(result).toBe(false); |
| 496 | }); |
| 497 | |
| 498 | test("should return false when context providers exist but no codebase provider", () => { |
| 499 | (testIndexer as any).config = { |
| 500 | contextProviders: [ |
| 501 | { |
| 502 | description: { |
| 503 | title: "SomeOtherProvider", |
| 504 | }, |
| 505 | }, |
| 506 | ], |
| 507 | }; |
| 508 | |
| 509 | const result = testIndexer.testHasIndexingProvider(); |
| 510 | expect(result).toBe(false); |
| 511 | }); |
| 512 | |
| 513 | test("should return false when context providers is empty array", () => { |
| 514 | (testIndexer as any).config = { |
| 515 | contextProviders: [], |
| 516 | }; |
| 517 | |
| 518 | const result = testIndexer.testHasIndexingProvider(); |
| 519 | expect(result).toBe(false); |
| 520 | }); |
| 521 | }); |
| 522 | |
| 523 | describe("handleConfigUpdate", () => { |
| 524 | test("should return early when newConfig is null", async () => { |
| 525 | const configResult: ConfigResult<ContinueConfig> = { |
| 526 | config: null as any, |
| 527 | errors: [], |
| 528 | configLoadInterrupted: false, |
| 529 | }; |
| 530 | |
| 531 | await testIndexer.testHandleConfigUpdate(configResult); |
| 532 | |
| 533 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(0); |
| 534 | expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(0); |
| 535 | }); |
| 536 | |
| 537 | test("should return early when newConfig is undefined", async () => { |
| 538 | const configResult: ConfigResult<ContinueConfig> = { |
| 539 | config: undefined as any, |
| 540 | errors: [], |
| 541 | configLoadInterrupted: false, |
| 542 | }; |
| 543 | |
| 544 | await testIndexer.testHandleConfigUpdate(configResult); |
| 545 | |
| 546 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(0); |
| 547 | expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(0); |
| 548 | }); |
| 549 | |
| 550 | test("should return early when no codebase context provider is present", async () => { |
| 551 | const configResult: ConfigResult<ContinueConfig> = { |
| 552 | config: { |
| 553 | contextProviders: [ |
| 554 | { |
| 555 | description: { |
| 556 | title: "SomeOtherProvider", |
| 557 | }, |
| 558 | }, |
| 559 | ], |
| 560 | selectedModelByRole: { |
| 561 | embed: { |
| 562 | model: "test-model", |
| 563 | provider: "test-provider", |
| 564 | }, |
| 565 | }, |
| 566 | } as unknown as ContinueConfig, |
| 567 | errors: [], |
| 568 | configLoadInterrupted: false, |
| 569 | }; |
| 570 | |
| 571 | await testIndexer.testHandleConfigUpdate(configResult); |
| 572 | |
| 573 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(0); |
| 574 | expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(0); |
| 575 | }); |
| 576 | |
| 577 | test("should return early when no embed model is configured", async () => { |
| 578 | const configResult: ConfigResult<ContinueConfig> = { |
| 579 | config: { |
| 580 | contextProviders: [new CodebaseContextProvider({})], |
| 581 | selectedModelByRole: { |
| 582 | embed: undefined, |
| 583 | }, |
| 584 | } as unknown as ContinueConfig, |
| 585 | errors: [], |
| 586 | configLoadInterrupted: false, |
| 587 | }; |
| 588 | |
| 589 | await testIndexer.testHandleConfigUpdate(configResult); |
| 590 | |
| 591 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(0); |
| 592 | expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(0); |
| 593 | }); |
| 594 | |
| 595 | test("should call refreshCodebaseIndex when all conditions are met", async () => { |
| 596 | const configResult: ConfigResult<ContinueConfig> = { |
| 597 | config: { |
| 598 | contextProviders: [new CodebaseContextProvider({})], |
| 599 | selectedModelByRole: { |
| 600 | embed: { |
| 601 | model: "test-model", |
| 602 | provider: "test-provider", |
| 603 | }, |
| 604 | }, |
| 605 | } as unknown as ContinueConfig, |
| 606 | errors: [], |
| 607 | configLoadInterrupted: false, |
| 608 | }; |
| 609 | |
| 610 | await testIndexer.testHandleConfigUpdate(configResult); |
| 611 | |
| 612 | expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(1); |
| 613 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1); |
| 614 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledWith([ |
| 615 | "/test/workspace", |
| 616 | ]); |
| 617 | }); |
| 618 | |
| 619 | test("should set config property before checking conditions", async () => { |
| 620 | const testConfig = { |
| 621 | contextProviders: [new CodebaseContextProvider({})], |
| 622 | selectedModelByRole: { |
| 623 | embed: { |
| 624 | model: "test-model", |
| 625 | provider: "test-provider", |
| 626 | }, |
| 627 | }, |
| 628 | } as unknown as ContinueConfig; |
| 629 | |
| 630 | const configResult: ConfigResult<ContinueConfig> = { |
| 631 | config: testConfig, |
| 632 | errors: [], |
| 633 | configLoadInterrupted: false, |
| 634 | }; |
| 635 | |
| 636 | await testIndexer.testHandleConfigUpdate(configResult); |
| 637 | |
| 638 | // Verify that the config was set |
| 639 | expect((testIndexer as any).config).toBe(testConfig); |
| 640 | // These get called once on init, and we want them to get called again |
| 641 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1); |
| 642 | }); |
| 643 | |
| 644 | test("should handle multiple context providers correctly", async () => { |
| 645 | const configResult: ConfigResult<ContinueConfig> = { |
| 646 | config: { |
| 647 | contextProviders: [ |
| 648 | { |
| 649 | description: { |
| 650 | title: "SomeOtherProvider", |
| 651 | }, |
| 652 | }, |
| 653 | new CodebaseContextProvider({}), |
| 654 | { |
| 655 | description: { |
| 656 | title: "AnotherProvider", |
| 657 | }, |
| 658 | }, |
| 659 | ], |
| 660 | selectedModelByRole: { |
| 661 | embed: { |
| 662 | model: "test-model", |
| 663 | provider: "test-provider", |
| 664 | }, |
| 665 | }, |
| 666 | } as unknown as ContinueConfig, |
| 667 | errors: [], |
| 668 | configLoadInterrupted: false, |
| 669 | }; |
| 670 | |
| 671 | await testIndexer.testHandleConfigUpdate(configResult); |
| 672 | |
| 673 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1); |
| 674 | expect(mockRefreshCodebaseIndex).toHaveBeenCalledWith([ |
| 675 | "/test/workspace", |
| 676 | ]); |
| 677 | }); |
| 678 | }); |
| 679 | |
| 680 | describe("getIndexesToBuild", () => { |
| 681 | let indexer: CodebaseIndexer; |
| 682 | let mockConfig: any; |
| 683 | let mockEmbeddingsModel: any; |
| 684 | let mockIdeSettings: any; |
| 685 | |
| 686 | beforeEach(async () => { |
| 687 | indexer = new CodebaseIndexer( |
| 688 | testConfigHandler, |
| 689 | testIde, |
| 690 | mockMessenger as any, |
| 691 | false, |
| 692 | ); |
| 693 | |
| 694 | mockEmbeddingsModel = { |
| 695 | maxEmbeddingChunkSize: 1000, |
| 696 | model: "test-model", |
| 697 | provider: "test-provider", |
| 698 | }; |
| 699 | |
| 700 | mockIdeSettings = { |
| 701 | remoteConfigServerUrl: "http://test.com", |
| 702 | userToken: "test-token", |
| 703 | }; |
| 704 | |
| 705 | mockConfig = { |
| 706 | selectedModelByRole: { |
| 707 | embed: mockEmbeddingsModel, |
| 708 | }, |
| 709 | contextProviders: [], |
| 710 | }; |
| 711 | |
| 712 | jest |
| 713 | .spyOn(testIde, "getIdeSettings") |
| 714 | .mockResolvedValue(mockIdeSettings); |
| 715 | }); |
| 716 | |
| 717 | afterEach(() => { |
| 718 | jest.clearAllMocks(); |
| 719 | }); |
| 720 | |
| 721 | test("should create specific indexTypes for the requested context providers", async () => { |
| 722 | mockConfig.contextProviders = [ |
| 723 | { |
| 724 | description: { |
| 725 | dependsOnIndexing: ["chunk", "codeSnippets"], |
| 726 | }, |
| 727 | }, |
| 728 | ]; |
| 729 | |
| 730 | jest.spyOn(testConfigHandler, "loadConfig").mockResolvedValue({ |
| 731 | config: mockConfig, |
| 732 | errors: [], |
| 733 | configLoadInterrupted: false, |
| 734 | }); |
| 735 | |
| 736 | const indexes = (await ( |
| 737 | indexer as any |
| 738 | ).getIndexesToBuild()) as CodebaseIndex[]; |
| 739 | |
| 740 | expect(indexes).toHaveLength(2); |
| 741 | expect(indexes[0].artifactId).toBe("chunks"); |
| 742 | expect(indexes[1].artifactId).toBe("codeSnippets"); |
| 743 | }); |
| 744 | |
| 745 | test("should handle duplicate contexts", async () => { |
| 746 | mockConfig.contextProviders = [ |
| 747 | { |
| 748 | description: { |
| 749 | dependsOnIndexing: ["codeSnippets"], |
| 750 | }, |
| 751 | }, |
| 752 | { |
| 753 | description: { |
| 754 | dependsOnIndexing: ["codeSnippets"], |
| 755 | }, |
| 756 | }, |
| 757 | ]; |
| 758 | |
| 759 | jest.spyOn(testConfigHandler, "loadConfig").mockResolvedValue({ |
| 760 | config: mockConfig, |
| 761 | errors: [], |
| 762 | configLoadInterrupted: false, |
| 763 | }); |
| 764 | |
| 765 | const indexes = await (indexer as any).getIndexesToBuild(); |
| 766 | |
| 767 | expect(indexes).toHaveLength(1); |
| 768 | expect(indexes[0].artifactId).toBe("codeSnippets"); |
| 769 | }); |
| 770 | }); |
| 771 | }); |
| 772 | |
| 773 | describe("wasAnyOneIndexAdded", () => { |
| 774 | let testIndexer: TestCodebaseIndexer; |
| 775 | let mockGetIndexesToBuild: jest.MockedFunction<any>; |
| 776 | |
| 777 | beforeEach(() => { |
| 778 | testIndexer = new TestCodebaseIndexer( |
| 779 | testConfigHandler, |
| 780 | testIde, |
| 781 | mockMessenger as any, |
| 782 | false, |
| 783 | ); |
| 784 | |
| 785 | // Mock getIndexesToBuild to control what indexes should be built |
| 786 | mockGetIndexesToBuild = jest |
| 787 | .spyOn(testIndexer as any, "getIndexesToBuild") |
| 788 | .mockResolvedValue([]); |
| 789 | |
| 790 | jest |
| 791 | .spyOn(testIndexer, "refreshCodebaseIndex") |
| 792 | .mockImplementation(async () => {}); |
| 793 | }); |
| 794 | |
| 795 | afterEach(() => { |
| 796 | jest.clearAllMocks(); |
| 797 | }); |
| 798 | |
| 799 | test("should return true when indexes to build differ from built indexes", async () => { |
| 800 | const mockIndex1 = new TestCodebaseIndex(); |
| 801 | const mockIndex2 = new FullTextSearchCodebaseIndex(); |
| 802 | mockGetIndexesToBuild.mockResolvedValue([mockIndex1]); |
| 803 | (testIndexer as any).builtIndexes = [mockIndex2]; |
| 804 | |
| 805 | const result = await testIndexer.wasAnyOneIndexAdded(); |
| 806 | expect(result).toBe(true); |
| 807 | }); |
| 808 | |
| 809 | test("should return true when there are more indexes to build than built indexes", async () => { |
| 810 | const mockIndex1 = new TestCodebaseIndex(); |
| 811 | const mockIndex2 = new FullTextSearchCodebaseIndex(); |
| 812 | mockGetIndexesToBuild.mockResolvedValue([mockIndex1, mockIndex2]); |
| 813 | (testIndexer as any).builtIndexes = [mockIndex1]; |
| 814 | |
| 815 | const result = await testIndexer.wasAnyOneIndexAdded(); |
| 816 | expect(result).toBe(true); |
| 817 | }); |
| 818 | |
| 819 | test("should return false when there are fewer indexes to build than built indexes", async () => { |
| 820 | const mockIndex1 = new TestCodebaseIndex(); |
| 821 | const mockIndex2 = new FullTextSearchCodebaseIndex(); |
| 822 | mockGetIndexesToBuild.mockResolvedValue([mockIndex1]); |
| 823 | (testIndexer as any).builtIndexes = [mockIndex1, mockIndex2]; |
| 824 | |
| 825 | const result = await testIndexer.wasAnyOneIndexAdded(); |
| 826 | expect(result).toBe(false); |
| 827 | }); |
| 828 | }); |
| 829 | }); |