|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +import type { Task } from "../../task/Task" |
| 4 | +import { RepoPerTaskCheckpointService } from "../../../services/checkpoints" |
| 5 | +import { checkGitInstalled } from "../../../utils/git" |
| 6 | +import { getCheckpointService } from "../index" |
| 7 | + |
| 8 | +vi.mock("../../../services/checkpoints", () => ({ |
| 9 | + RepoPerTaskCheckpointService: { create: vi.fn() }, |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock("../../../utils/git", () => ({ |
| 13 | + checkGitInstalled: vi.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock("../../../utils/path", () => ({ |
| 17 | + getWorkspacePath: vi.fn(() => "/workspace"), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock("../../../i18n", () => ({ |
| 21 | + t: (key: string) => key, |
| 22 | +})) |
| 23 | + |
| 24 | +/** |
| 25 | + * Structural service double: getCheckpointService only registers listeners, |
| 26 | + * awaits initShadowGit, and reads isInitialized. The double assertion at the |
| 27 | + * create() call sites is confined to this file (AGENTS.md last-resort cast): |
| 28 | + * the real class carries a Git handle and event surface the test never |
| 29 | + * exercises. |
| 30 | + */ |
| 31 | +function serviceDouble(initShadowGitImpl: () => Promise<void>) { |
| 32 | + const listeners = new Map<string, (...args: unknown[]) => void>() |
| 33 | + let initialized = false |
| 34 | + const svc = { |
| 35 | + on: vi.fn((event: string, cb: (...args: unknown[]) => void) => { |
| 36 | + listeners.set(event, cb) |
| 37 | + }), |
| 38 | + initShadowGit: vi.fn(() => initShadowGitImpl()), |
| 39 | + get isInitialized() { |
| 40 | + return initialized |
| 41 | + }, |
| 42 | + completeInit: () => { |
| 43 | + initialized = true |
| 44 | + listeners.get("initialize")?.() |
| 45 | + }, |
| 46 | + } |
| 47 | + return svc |
| 48 | +} |
| 49 | + |
| 50 | +function makeTask(): Task { |
| 51 | + return { |
| 52 | + taskId: "task-init", |
| 53 | + cwd: "/workspace", |
| 54 | + enableCheckpoints: true, |
| 55 | + checkpointService: undefined, |
| 56 | + checkpointServiceInitializing: false, |
| 57 | + checkpointTimeout: 1, |
| 58 | + providerRef: { |
| 59 | + deref: vi.fn().mockReturnValue({ |
| 60 | + context: { globalStorageUri: { fsPath: "/tmp/b3-init-storage" } }, |
| 61 | + postMessageToWebview: vi.fn(), |
| 62 | + log: vi.fn(), |
| 63 | + }), |
| 64 | + }, |
| 65 | + say: vi.fn().mockResolvedValue(undefined), |
| 66 | + } as unknown as Task |
| 67 | +} |
| 68 | + |
| 69 | +describe("getCheckpointService initialization failures", () => { |
| 70 | + beforeEach(() => { |
| 71 | + vi.mocked(checkGitInstalled).mockReset() |
| 72 | + vi.mocked(RepoPerTaskCheckpointService.create).mockReset() |
| 73 | + }) |
| 74 | + |
| 75 | + it("does not store a service whose shadow-git initialization failed", async () => { |
| 76 | + vi.mocked(checkGitInstalled).mockResolvedValue(true) |
| 77 | + const failing = serviceDouble(async () => { |
| 78 | + throw new Error("shadow git init exploded") |
| 79 | + }) |
| 80 | + vi.mocked(RepoPerTaskCheckpointService.create).mockReturnValue( |
| 81 | + failing as unknown as RepoPerTaskCheckpointService, |
| 82 | + ) |
| 83 | + |
| 84 | + const task = makeTask() |
| 85 | + const service = await getCheckpointService(task) |
| 86 | + |
| 87 | + // The failed service must not be stored: a stored uninitialized handle |
| 88 | + // would be returned by the fast path to every later save, and the |
| 89 | + // initializing flag must not strand later lookups in a wait for an |
| 90 | + // initialization that already died. |
| 91 | + expect(service).toBeUndefined() |
| 92 | + expect(task.checkpointService).toBeUndefined() |
| 93 | + expect(task.checkpointServiceInitializing).toBe(false) |
| 94 | + expect(task.enableCheckpoints).toBe(false) |
| 95 | + }) |
| 96 | + |
| 97 | + it("does not store a service when Git is not installed", async () => { |
| 98 | + vi.mocked(checkGitInstalled).mockResolvedValue(false) |
| 99 | + const svc = serviceDouble(async () => {}) |
| 100 | + vi.mocked(RepoPerTaskCheckpointService.create).mockReturnValue(svc as unknown as RepoPerTaskCheckpointService) |
| 101 | + |
| 102 | + const task = makeTask() |
| 103 | + const service = await getCheckpointService(task) |
| 104 | + |
| 105 | + expect(service).toBeUndefined() |
| 106 | + expect(task.checkpointService).toBeUndefined() |
| 107 | + expect(task.checkpointServiceInitializing).toBe(false) |
| 108 | + expect(task.enableCheckpoints).toBe(false) |
| 109 | + }) |
| 110 | + |
| 111 | + it("stores an initialized service on the success path", async () => { |
| 112 | + vi.mocked(checkGitInstalled).mockResolvedValue(true) |
| 113 | + const svc = serviceDouble(async () => { |
| 114 | + svc.completeInit() |
| 115 | + }) |
| 116 | + vi.mocked(RepoPerTaskCheckpointService.create).mockReturnValue(svc as unknown as RepoPerTaskCheckpointService) |
| 117 | + |
| 118 | + const task = makeTask() |
| 119 | + const service = await getCheckpointService(task) |
| 120 | + |
| 121 | + expect(service).toBe(svc) |
| 122 | + expect(task.checkpointService).toBe(svc) |
| 123 | + expect(task.checkpointServiceInitializing).toBe(false) |
| 124 | + expect(task.enableCheckpoints).toBe(true) |
| 125 | + }) |
| 126 | +}) |
0 commit comments