Skip to content

Commit 115f873

Browse files
committed
test(code-index): cover remaining orchestration and scan branches
1 parent e1b64f8 commit 115f873

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

‎src/services/code-index/__tests__/code-index-scan-executor.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ describe("CodeIndexScanExecutor", () => {
1212
return { scanner, vectorStore, stateManager, executor }
1313
}
1414

15+
it.each(["runFullScan", "runIncrementalScan"] as const)("%s rejects a missing scanner result", async (method) => {
16+
const { executor } = setup()
17+
// An unconfigured mock returns undefined, simulating a broken scanner contract.
18+
await expect(executor[method](new AbortController().signal)).rejects.toThrow(
19+
method === "runFullScan"
20+
? "Scan failed, is scanner initialized?"
21+
: "Incremental scan failed, is scanner initialized?",
22+
)
23+
})
24+
1525
it.each(["runFullScan", "runIncrementalScan"] as const)(
1626
"%s reports progress without completing the operation",
1727
async (method) => {

‎src/services/code-index/__tests__/orchestrator.spec.ts‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest"
22
import { CodeIndexOrchestrator } from "../orchestrator"
33
import { TelemetryService } from "@roo-code/telemetry"
4+
import * as vscode from "vscode"
45

56
import { clearAllMocks } from "../../../test-utils/reset"
67

@@ -308,6 +309,175 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
308309
expect(disposeFinished).toHaveBeenCalledTimes(1)
309310
})
310311

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+
311481
it("should preserve existing data when checking collection contents fails", async () => {
312482
vectorStore.initialize.mockResolvedValue(false)
313483
vectorStore.hasIndexedData.mockRejectedValue(new Error("query failed"))
@@ -626,6 +796,8 @@ describe("CodeIndexOrchestrator - stopIndexing", () => {
626796
const clearing = orchestrator.clearIndexData()
627797
await deletionStarted
628798
try {
799+
await orchestrator.clearIndexData()
800+
expect(vectorStore.deleteCollection).toHaveBeenCalledTimes(1)
629801
await orchestrator.startIndexing()
630802
expect(vectorStore.initialize).not.toHaveBeenCalled()
631803
expect(cacheManager.clearCacheFile).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)