|
1 | 1 | import { describe, it, expect, beforeEach, vi } from "vitest" |
2 | 2 | import { CodeIndexOrchestrator } from "../orchestrator" |
3 | 3 | import { TelemetryService } from "@roo-code/telemetry" |
| 4 | +import * as vscode from "vscode" |
4 | 5 |
|
5 | 6 | import { clearAllMocks } from "../../../test-utils/reset" |
6 | 7 |
|
@@ -308,6 +309,175 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => { |
308 | 309 | expect(disposeFinished).toHaveBeenCalledTimes(1) |
309 | 310 | }) |
310 | 311 |
|
| 312 | + it.each([undefined, []])("rejects indexing without workspace folders (%s)", async (folders) => { |
| 313 | + const original = vscode.workspace.workspaceFolders |
| 314 | + Object.defineProperty(vscode.workspace, "workspaceFolders", { value: folders, configurable: true }) |
| 315 | + try { |
| 316 | + const orchestrator = new CodeIndexOrchestrator( |
| 317 | + configManager, |
| 318 | + stateManager, |
| 319 | + workspacePath, |
| 320 | + cacheManager, |
| 321 | + vectorStore, |
| 322 | + scanner, |
| 323 | + fileWatcher, |
| 324 | + ) |
| 325 | + await orchestrator.startIndexing() |
| 326 | + expect(orchestrator.state).toBe("Error") |
| 327 | + expect(vectorStore.initialize).not.toHaveBeenCalled() |
| 328 | + } finally { |
| 329 | + Object.defineProperty(vscode.workspace, "workspaceFolders", { value: original, configurable: true }) |
| 330 | + } |
| 331 | + }) |
| 332 | + |
| 333 | + it("rejects indexing without configuration", async () => { |
| 334 | + configManager.isFeatureConfigured = false |
| 335 | + const orchestrator = new CodeIndexOrchestrator( |
| 336 | + configManager, |
| 337 | + stateManager, |
| 338 | + workspacePath, |
| 339 | + cacheManager, |
| 340 | + vectorStore, |
| 341 | + scanner, |
| 342 | + fileWatcher, |
| 343 | + ) |
| 344 | + await orchestrator.startIndexing() |
| 345 | + expect(orchestrator.state).toBe("Standby") |
| 346 | + expect(vectorStore.initialize).not.toHaveBeenCalled() |
| 347 | + }) |
| 348 | + |
| 349 | + it.each(["Indexing", "Stopping"])("rejects indexing in %s state", async (state) => { |
| 350 | + stateManager.setSystemState(state, "busy") |
| 351 | + const orchestrator = new CodeIndexOrchestrator( |
| 352 | + configManager, |
| 353 | + stateManager, |
| 354 | + workspacePath, |
| 355 | + cacheManager, |
| 356 | + vectorStore, |
| 357 | + scanner, |
| 358 | + fileWatcher, |
| 359 | + ) |
| 360 | + await orchestrator.startIndexing() |
| 361 | + expect(orchestrator.state).toBe(state) |
| 362 | + expect(vectorStore.initialize).not.toHaveBeenCalled() |
| 363 | + }) |
| 364 | + |
| 365 | + it.each(["configuration lost", "watcher failed"])("handles watcher startup failure: %s", async (failure) => { |
| 366 | + vectorStore.initialize.mockResolvedValue(false) |
| 367 | + vectorStore.hasIndexedData.mockResolvedValue(true) |
| 368 | + scanner.scanDirectory.mockImplementation(async () => { |
| 369 | + if (failure === "configuration lost") configManager.isFeatureConfigured = false |
| 370 | + return { stats: { processed: 0, skipped: 0 }, totalBlockCount: 0 } |
| 371 | + }) |
| 372 | + fileWatcher.initialize.mockRejectedValue(new Error("watcher failed")) |
| 373 | + const orchestrator = new CodeIndexOrchestrator( |
| 374 | + configManager, |
| 375 | + stateManager, |
| 376 | + workspacePath, |
| 377 | + cacheManager, |
| 378 | + vectorStore, |
| 379 | + scanner, |
| 380 | + fileWatcher, |
| 381 | + ) |
| 382 | + await orchestrator.startIndexing() |
| 383 | + expect(orchestrator.state).toBe("Error") |
| 384 | + expect(vectorStore.markIndexingComplete).not.toHaveBeenCalled() |
| 385 | + expect(fileWatcher.dispose).toHaveBeenCalled() |
| 386 | + expect(TelemetryService.instance.captureEvent).toHaveBeenCalledWith( |
| 387 | + expect.any(String), |
| 388 | + expect.objectContaining({ location: "startIndexing" }), |
| 389 | + ) |
| 390 | + if (failure === "watcher failed") { |
| 391 | + expect(TelemetryService.instance.captureEvent).toHaveBeenCalledWith( |
| 392 | + expect.any(String), |
| 393 | + expect.objectContaining({ location: "_startWatcher", error: failure }), |
| 394 | + ) |
| 395 | + } else { |
| 396 | + expect(fileWatcher.initialize).not.toHaveBeenCalled() |
| 397 | + } |
| 398 | + }) |
| 399 | + |
| 400 | + it("preserves the original error when collection cleanup rejects a non-Error value", async () => { |
| 401 | + vectorStore.initialize.mockResolvedValue(false) |
| 402 | + vectorStore.hasIndexedData.mockResolvedValue(false) |
| 403 | + scanner.scanDirectory.mockRejectedValue(new Error("original failure")) |
| 404 | + vectorStore.clearCollection.mockRejectedValue("cleanup failed") |
| 405 | + const orchestrator = new CodeIndexOrchestrator( |
| 406 | + configManager, |
| 407 | + stateManager, |
| 408 | + workspacePath, |
| 409 | + cacheManager, |
| 410 | + vectorStore, |
| 411 | + scanner, |
| 412 | + fileWatcher, |
| 413 | + ) |
| 414 | + await orchestrator.startIndexing() |
| 415 | + expect(cacheManager.clearCacheFile).toHaveBeenCalledOnce() |
| 416 | + expect(stateManager.setSystemState).toHaveBeenLastCalledWith( |
| 417 | + "Error", |
| 418 | + expect.stringContaining("original failure"), |
| 419 | + ) |
| 420 | + expect(TelemetryService.instance.captureEvent).toHaveBeenCalledWith( |
| 421 | + expect.any(String), |
| 422 | + expect.objectContaining({ location: "startIndexing.cleanup", error: "cleanup failed", stack: undefined }), |
| 423 | + ) |
| 424 | + }) |
| 425 | + |
| 426 | + it.each([null, "connection failed", { message: "" }])("handles unexpected rejection values (%s)", async (error) => { |
| 427 | + vectorStore.initialize.mockRejectedValue(error) |
| 428 | + const orchestrator = new CodeIndexOrchestrator( |
| 429 | + configManager, |
| 430 | + stateManager, |
| 431 | + workspacePath, |
| 432 | + cacheManager, |
| 433 | + vectorStore, |
| 434 | + scanner, |
| 435 | + fileWatcher, |
| 436 | + ) |
| 437 | + await expect(orchestrator.startIndexing()).resolves.toBeUndefined() |
| 438 | + expect(stateManager.setSystemState).toHaveBeenLastCalledWith("Error", expect.stringContaining("unknownError")) |
| 439 | + expect(fileWatcher.dispose).toHaveBeenCalled() |
| 440 | + }) |
| 441 | + |
| 442 | + it("clears cache without deleting storage when configuration is missing", async () => { |
| 443 | + configManager.isFeatureConfigured = false |
| 444 | + vectorStore.deleteCollection = vi.fn() |
| 445 | + const orchestrator = new CodeIndexOrchestrator( |
| 446 | + configManager, |
| 447 | + stateManager, |
| 448 | + workspacePath, |
| 449 | + cacheManager, |
| 450 | + vectorStore, |
| 451 | + scanner, |
| 452 | + fileWatcher, |
| 453 | + ) |
| 454 | + await orchestrator.clearIndexData() |
| 455 | + expect(vectorStore.deleteCollection).not.toHaveBeenCalled() |
| 456 | + expect(cacheManager.clearCacheFile).toHaveBeenCalledOnce() |
| 457 | + expect(orchestrator.state).toBe("Standby") |
| 458 | + }) |
| 459 | + |
| 460 | + it("reports non-Error deletion failures and releases the clearing guard", async () => { |
| 461 | + vectorStore.deleteCollection = vi.fn().mockRejectedValueOnce("delete failed").mockResolvedValue(undefined) |
| 462 | + const orchestrator = new CodeIndexOrchestrator( |
| 463 | + configManager, |
| 464 | + stateManager, |
| 465 | + workspacePath, |
| 466 | + cacheManager, |
| 467 | + vectorStore, |
| 468 | + scanner, |
| 469 | + fileWatcher, |
| 470 | + ) |
| 471 | + await orchestrator.clearIndexData() |
| 472 | + expect(stateManager.setSystemState).toHaveBeenLastCalledWith( |
| 473 | + "Error", |
| 474 | + "Failed to clear index data: delete failed", |
| 475 | + ) |
| 476 | + expect(cacheManager.clearCacheFile).not.toHaveBeenCalled() |
| 477 | + await orchestrator.clearIndexData() |
| 478 | + expect(orchestrator.state).toBe("Standby") |
| 479 | + }) |
| 480 | + |
311 | 481 | it("should preserve existing data when checking collection contents fails", async () => { |
312 | 482 | vectorStore.initialize.mockResolvedValue(false) |
313 | 483 | vectorStore.hasIndexedData.mockRejectedValue(new Error("query failed")) |
@@ -626,6 +796,8 @@ describe("CodeIndexOrchestrator - stopIndexing", () => { |
626 | 796 | const clearing = orchestrator.clearIndexData() |
627 | 797 | await deletionStarted |
628 | 798 | try { |
| 799 | + await orchestrator.clearIndexData() |
| 800 | + expect(vectorStore.deleteCollection).toHaveBeenCalledTimes(1) |
629 | 801 | await orchestrator.startIndexing() |
630 | 802 | expect(vectorStore.initialize).not.toHaveBeenCalled() |
631 | 803 | expect(cacheManager.clearCacheFile).not.toHaveBeenCalled() |
|
0 commit comments