|
| 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 | +describe("CodeIndexScanExecutor", () => { |
| 7 | + function setup() { |
| 8 | + const scanner = { scanDirectory: vi.fn<IDirectoryScanner["scanDirectory"]>() } |
| 9 | + const vectorStore = { markIndexingIncomplete: vi.fn().mockResolvedValue(undefined) } |
| 10 | + const stateManager = { setSystemState: vi.fn(), reportBlockIndexingProgress: vi.fn() } |
| 11 | + const executor = new CodeIndexScanExecutor("/workspace", scanner, vectorStore, stateManager) |
| 12 | + return { scanner, vectorStore, stateManager, executor } |
| 13 | + } |
| 14 | + |
| 15 | + it.each(["runFullScan", "runIncrementalScan"] as const)( |
| 16 | + "%s reports progress without completing the operation", |
| 17 | + async (method) => { |
| 18 | + const { scanner, vectorStore, stateManager, executor } = setup() |
| 19 | + const signal = new AbortController().signal |
| 20 | + scanner.scanDirectory.mockImplementation(async (_path, _onError, onIndexed, onParsed, receivedSignal) => { |
| 21 | + expect(receivedSignal).toBe(signal) |
| 22 | + expect(vectorStore.markIndexingIncomplete).toHaveBeenCalledOnce() |
| 23 | + onParsed?.(3) |
| 24 | + onIndexed?.(1) |
| 25 | + onIndexed?.(2) |
| 26 | + return { stats: { processed: 1, skipped: 0 }, totalBlockCount: 3 } |
| 27 | + }) |
| 28 | + await executor[method](signal) |
| 29 | + expect(stateManager.reportBlockIndexingProgress.mock.calls).toEqual([ |
| 30 | + [0, 3], |
| 31 | + [1, 3], |
| 32 | + [3, 3], |
| 33 | + ]) |
| 34 | + expect(stateManager.setSystemState).not.toHaveBeenCalledWith("Indexed", expect.any(String)) |
| 35 | + }, |
| 36 | + ) |
| 37 | + |
| 38 | + it.each(["runFullScan", "runIncrementalScan"] as const)( |
| 39 | + "%s propagates cancellation to its owner", |
| 40 | + async (method) => { |
| 41 | + const { scanner, executor } = setup() |
| 42 | + const controller = new AbortController() |
| 43 | + scanner.scanDirectory.mockImplementation(async () => { |
| 44 | + controller.abort() |
| 45 | + return { stats: { processed: 0, skipped: 0 }, totalBlockCount: 0 } |
| 46 | + }) |
| 47 | + await expect(executor[method](controller.signal)).rejects.toMatchObject({ name: "AbortError" }) |
| 48 | + }, |
| 49 | + ) |
| 50 | + |
| 51 | + it.each([ |
| 52 | + { method: "runFullScan", rejects: false }, |
| 53 | + { method: "runIncrementalScan", rejects: true }, |
| 54 | + ] as const)("$method preserves its partial-failure policy", async ({ method, rejects }) => { |
| 55 | + const { scanner, executor } = setup() |
| 56 | + scanner.scanDirectory.mockImplementation(async (_path, onError, onIndexed, onParsed) => { |
| 57 | + onParsed?.(10) |
| 58 | + onIndexed?.(9) |
| 59 | + onError?.(new Error("batch failed")) |
| 60 | + return { stats: { processed: 2, skipped: 0 }, totalBlockCount: 10 } |
| 61 | + }) |
| 62 | + const result = executor[method](new AbortController().signal) |
| 63 | + if (rejects) { |
| 64 | + await expect(result).rejects.toThrow("batch failed") |
| 65 | + } else { |
| 66 | + await expect(result).resolves.toBeUndefined() |
| 67 | + } |
| 68 | + }) |
| 69 | +}) |
0 commit comments