|
| 1 | +import { CodeIndexScanExecutor } from "../code-index-scan-executor" |
| 2 | +import type { IDirectoryScanner } from "../interfaces" |
| 3 | + |
| 4 | +vi.mock("../../../i18n", () => ({ t: (key: string) => key })) |
| 5 | + |
| 6 | +function setup() { |
| 7 | + const scanner = { scanDirectory: vi.fn<IDirectoryScanner["scanDirectory"]>() } |
| 8 | + const vectorStore = { markIndexingIncomplete: vi.fn().mockResolvedValue(undefined) } |
| 9 | + const stateManager = { setSystemState: vi.fn(), reportBlockIndexingProgress: vi.fn() } |
| 10 | + return { |
| 11 | + scanner, |
| 12 | + vectorStore, |
| 13 | + stateManager, |
| 14 | + executor: new CodeIndexScanExecutor("/workspace", scanner, vectorStore, stateManager), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe("CodeIndexScanExecutor", () => { |
| 19 | + it.each(["runFullScan", "runIncrementalScan"] as const)( |
| 20 | + "%s skips failure validation when cancellation occurs during the scan", |
| 21 | + async (method) => { |
| 22 | + const { executor, scanner } = setup() |
| 23 | + const controller = new AbortController() |
| 24 | + scanner.scanDirectory.mockImplementation(async (_path, onError, _onIndexed, onParsed) => { |
| 25 | + onParsed?.(3) |
| 26 | + onError?.(new Error("batch failure")) |
| 27 | + controller.abort() |
| 28 | + return { stats: { processed: 0, skipped: 0 }, totalBlockCount: 3 } |
| 29 | + }) |
| 30 | + await expect(executor[method](controller.signal)).resolves.toBe(false) |
| 31 | + }, |
| 32 | + ) |
| 33 | + |
| 34 | + it("completes an incremental scan with no changed files", async () => { |
| 35 | + const { executor, scanner } = setup() |
| 36 | + scanner.scanDirectory.mockResolvedValue({ stats: { processed: 0, skipped: 2 }, totalBlockCount: 0 }) |
| 37 | + await expect(executor.runIncrementalScan(new AbortController().signal)).resolves.toBe(true) |
| 38 | + }) |
| 39 | + |
| 40 | + it.each(["runFullScan", "runIncrementalScan"] as const)( |
| 41 | + "%s preserves progress and signal wiring", |
| 42 | + async (method) => { |
| 43 | + const { executor, scanner, vectorStore, stateManager } = setup() |
| 44 | + const signal = new AbortController().signal |
| 45 | + scanner.scanDirectory.mockImplementation(async (path, _onError, onIndexed, onParsed, receivedSignal) => { |
| 46 | + expect(path).toBe("/workspace") |
| 47 | + expect(receivedSignal).toBe(signal) |
| 48 | + expect(vectorStore.markIndexingIncomplete).toHaveBeenCalledOnce() |
| 49 | + onParsed?.(2) |
| 50 | + onIndexed?.(1) |
| 51 | + onParsed?.(1) |
| 52 | + onIndexed?.(2) |
| 53 | + return { stats: { processed: 2, skipped: 0 }, totalBlockCount: 3 } |
| 54 | + }) |
| 55 | + await expect(executor[method](signal)).resolves.toBe(true) |
| 56 | + expect(stateManager.reportBlockIndexingProgress.mock.calls).toEqual([ |
| 57 | + [0, 2], |
| 58 | + [1, 2], |
| 59 | + [1, 3], |
| 60 | + [3, 3], |
| 61 | + ]) |
| 62 | + expect(stateManager.setSystemState).toHaveBeenCalledExactlyOnceWith( |
| 63 | + "Indexing", |
| 64 | + method === "runFullScan" |
| 65 | + ? "Services ready. Starting workspace scan..." |
| 66 | + : "Checking for new or modified files...", |
| 67 | + ) |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + it.each(["runFullScan", "runIncrementalScan"] as const)("%s rejects missing results", async (method) => { |
| 72 | + const { executor } = setup() |
| 73 | + // An unconfigured mock simulates a scanner violating its return contract. |
| 74 | + await expect(executor[method](new AbortController().signal)).rejects.toThrow( |
| 75 | + method === "runFullScan" |
| 76 | + ? "Scan failed, is scanner initialized?" |
| 77 | + : "Incremental scan failed, is scanner initialized?", |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + it.each(["runFullScan", "runIncrementalScan"] as const)( |
| 82 | + "%s returns cancellation before validating results", |
| 83 | + async (method) => { |
| 84 | + const { executor, scanner } = setup() |
| 85 | + const controller = new AbortController() |
| 86 | + // Abort before the unconfigured scanner returns a missing result. |
| 87 | + controller.abort() |
| 88 | + await expect(executor[method](controller.signal)).resolves.toBe(false) |
| 89 | + expect(scanner.scanDirectory).toHaveBeenCalledOnce() |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + it.each(["runFullScan", "runIncrementalScan"] as const)( |
| 94 | + "%s propagates scanner and marker failures unchanged", |
| 95 | + async (method) => { |
| 96 | + const { executor, scanner, vectorStore } = setup() |
| 97 | + const signal = new AbortController().signal |
| 98 | + const failure = new Error("scan failed") |
| 99 | + scanner.scanDirectory.mockRejectedValueOnce(failure) |
| 100 | + await expect(executor[method](signal)).rejects.toBe(failure) |
| 101 | + const abort = new DOMException("stopped", "AbortError") |
| 102 | + scanner.scanDirectory.mockRejectedValueOnce(abort) |
| 103 | + await expect(executor[method](signal)).rejects.toBe(abort) |
| 104 | + vectorStore.markIndexingIncomplete.mockRejectedValueOnce(failure) |
| 105 | + await expect(executor[method](signal)).rejects.toBe(failure) |
| 106 | + expect(scanner.scanDirectory).toHaveBeenCalledTimes(2) |
| 107 | + }, |
| 108 | + ) |
| 109 | + |
| 110 | + it.each([ |
| 111 | + { found: 0, indexed: 0, error: false, message: undefined }, |
| 112 | + { found: 3, indexed: 0, error: false, message: "embeddings:orchestrator.indexingFailedNoBlocks" }, |
| 113 | + { found: 3, indexed: 0, error: true, message: "Indexing failed: batch failure" }, |
| 114 | + { found: 0, indexed: 0, error: true, message: "Indexing failed completely: batch failure" }, |
| 115 | + { found: 10, indexed: 9, error: true, message: undefined }, |
| 116 | + { |
| 117 | + found: 10, |
| 118 | + indexed: 8, |
| 119 | + error: true, |
| 120 | + message: "Indexing partially failed: Only 8 of 10 blocks were indexed. batch failure", |
| 121 | + }, |
| 122 | + { found: 10, indexed: 8, error: false, message: undefined }, |
| 123 | + ])( |
| 124 | + "preserves full-scan policy for $indexed/$found blocks, error=$error", |
| 125 | + async ({ found, indexed, error, message }) => { |
| 126 | + const { executor, scanner } = setup() |
| 127 | + scanner.scanDirectory.mockImplementation(async (_path, onError, onIndexed, onParsed) => { |
| 128 | + onParsed?.(found) |
| 129 | + onIndexed?.(indexed) |
| 130 | + if (error) onError?.(new Error("batch failure")) |
| 131 | + return { stats: { processed: 1, skipped: 0 }, totalBlockCount: found } |
| 132 | + }) |
| 133 | + const result = executor.runFullScan(new AbortController().signal) |
| 134 | + if (message) await expect(result).rejects.toThrow(message) |
| 135 | + else await expect(result).resolves.toBe(true) |
| 136 | + }, |
| 137 | + ) |
| 138 | + |
| 139 | + it.each([0, 2])("preserves incremental batch-error tolerance with %s indexed blocks", async (indexed) => { |
| 140 | + const { executor, scanner } = setup() |
| 141 | + scanner.scanDirectory.mockImplementation(async (_path, onError, onIndexed, onParsed) => { |
| 142 | + onParsed?.(3) |
| 143 | + onIndexed?.(indexed) |
| 144 | + onError?.(new Error("batch failure")) |
| 145 | + return { stats: { processed: 1, skipped: 0 }, totalBlockCount: 3 } |
| 146 | + }) |
| 147 | + await expect(executor.runIncrementalScan(new AbortController().signal)).resolves.toBe(true) |
| 148 | + }) |
| 149 | +}) |
0 commit comments