|
| 1 | +import fs from "fs/promises" |
| 2 | +import os from "os" |
| 3 | +import path from "path" |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 6 | + |
| 7 | +import type { Task } from "../../task/Task" |
| 8 | +import { getCheckpointService } from "../index" |
| 9 | +import { appendChange } from "../changeJournal" |
| 10 | +import { rollbackFile, rollbackStep } from "../rollback" |
| 11 | + |
| 12 | +vi.mock("../index", () => ({ |
| 13 | + getCheckpointService: vi.fn(), |
| 14 | + checkpointSave: vi.fn(), |
| 15 | + checkpointRestore: vi.fn(), |
| 16 | + checkpointDiff: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +const mockedGetCheckpointService = getCheckpointService as unknown as ReturnType<typeof vi.fn> |
| 20 | + |
| 21 | +function makeTask(): Task { |
| 22 | + return { |
| 23 | + taskId: "task-rollback", |
| 24 | + providerRef: { |
| 25 | + deref: vi.fn().mockReturnValue({ context: { globalStorageUri: { fsPath: globalStorageDir } } }), |
| 26 | + }, |
| 27 | + } as unknown as Task |
| 28 | +} |
| 29 | + |
| 30 | +let globalStorageDir: string |
| 31 | + |
| 32 | +beforeEach(async () => { |
| 33 | + globalStorageDir = await fs.mkdtemp(path.join(os.tmpdir(), "b3a-rollback-")) |
| 34 | + mockedGetCheckpointService.mockReset() |
| 35 | +}) |
| 36 | + |
| 37 | +afterEach(async () => { |
| 38 | + await fs.rm(globalStorageDir, { recursive: true, force: true }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe("rollbackFile (B3a)", () => { |
| 42 | + it("restores the file through the task's checkpoint service", async () => { |
| 43 | + const restoreFile = vi.fn().mockResolvedValue(undefined) |
| 44 | + mockedGetCheckpointService.mockResolvedValue({ restoreFile }) |
| 45 | + |
| 46 | + const outcome = await rollbackFile(makeTask(), "sha-1", "src/a.ts") |
| 47 | + |
| 48 | + expect(outcome).toEqual({ filePath: "src/a.ts", success: true }) |
| 49 | + expect(restoreFile).toHaveBeenCalledWith("sha-1", "src/a.ts") |
| 50 | + }) |
| 51 | + |
| 52 | + it("fails cleanly when checkpoints are not enabled", async () => { |
| 53 | + mockedGetCheckpointService.mockResolvedValue(undefined) |
| 54 | + |
| 55 | + const outcome = await rollbackFile(makeTask(), "sha-1", "src/a.ts") |
| 56 | + |
| 57 | + expect(outcome).toEqual({ |
| 58 | + filePath: "src/a.ts", |
| 59 | + success: false, |
| 60 | + error: "Checkpoints are not enabled for this task", |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it("reports the service error without throwing", async () => { |
| 65 | + mockedGetCheckpointService.mockResolvedValue({ |
| 66 | + restoreFile: vi.fn().mockRejectedValue(new Error("pathspec did not match")), |
| 67 | + }) |
| 68 | + |
| 69 | + const outcome = await rollbackFile(makeTask(), "sha-bad", "src/a.ts") |
| 70 | + |
| 71 | + expect(outcome.success).toBe(false) |
| 72 | + expect(outcome.error).toContain("pathspec did not match") |
| 73 | + }) |
| 74 | + |
| 75 | + it("stringifies non-Error rejections into the outcome", async () => { |
| 76 | + mockedGetCheckpointService.mockResolvedValue({ |
| 77 | + restoreFile: vi.fn().mockRejectedValue("raw failure"), |
| 78 | + }) |
| 79 | + |
| 80 | + const outcome = await rollbackFile(makeTask(), "sha-bad", "src/a.ts") |
| 81 | + |
| 82 | + expect(outcome).toEqual({ filePath: "src/a.ts", success: false, error: "raw failure" }) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +describe("rollbackStep (B3a)", () => { |
| 87 | + it("restores every step file from the step's checkpoint via the journal", async () => { |
| 88 | + await appendChange(globalStorageDir, "task-rollback", { |
| 89 | + path: "src/a.ts", |
| 90 | + operation: "create", |
| 91 | + checkpointId: "sha-step", |
| 92 | + }) |
| 93 | + await appendChange(globalStorageDir, "task-rollback", { |
| 94 | + path: "src/b.ts", |
| 95 | + operation: "update", |
| 96 | + checkpointId: "sha-step", |
| 97 | + }) |
| 98 | + |
| 99 | + const restoreFile = vi.fn().mockResolvedValue(undefined) |
| 100 | + mockedGetCheckpointService.mockResolvedValue({ restoreFile }) |
| 101 | + |
| 102 | + const outcome = await rollbackStep(makeTask(), ["src/a.ts", "src/b.ts"], "sha-step") |
| 103 | + |
| 104 | + expect(outcome.checkpointId).toBe("sha-step") |
| 105 | + expect(outcome.files).toEqual([ |
| 106 | + { filePath: "src/a.ts", success: true }, |
| 107 | + { filePath: "src/b.ts", success: true }, |
| 108 | + ]) |
| 109 | + expect(restoreFile).toHaveBeenCalledTimes(2) |
| 110 | + expect(restoreFile).toHaveBeenNthCalledWith(1, "sha-step", "src/a.ts") |
| 111 | + expect(restoreFile).toHaveBeenNthCalledWith(2, "sha-step", "src/b.ts") |
| 112 | + }) |
| 113 | + |
| 114 | + it("rejects a file that is not part of the given step checkpoint", async () => { |
| 115 | + await appendChange(globalStorageDir, "task-rollback", { |
| 116 | + path: "src/a.ts", |
| 117 | + operation: "create", |
| 118 | + checkpointId: "sha-step", |
| 119 | + }) |
| 120 | + |
| 121 | + const restoreFile = vi.fn().mockResolvedValue(undefined) |
| 122 | + mockedGetCheckpointService.mockResolvedValue({ restoreFile }) |
| 123 | + |
| 124 | + const outcome = await rollbackStep(makeTask(), ["src/a.ts", "src/other.ts"], "sha-step") |
| 125 | + |
| 126 | + expect(outcome.files[0]).toEqual({ filePath: "src/a.ts", success: true }) |
| 127 | + expect(outcome.files[1].success).toBe(false) |
| 128 | + expect(outcome.files[1].error).toBe("File is not part of this step's checkpoint") |
| 129 | + expect(restoreFile).toHaveBeenCalledTimes(1) |
| 130 | + }) |
| 131 | + |
| 132 | + it("falls back to the latest journal entry per file without a step checkpoint id", async () => { |
| 133 | + await appendChange(globalStorageDir, "task-rollback", { |
| 134 | + path: "src/a.ts", |
| 135 | + operation: "create", |
| 136 | + checkpointId: "sha-1", |
| 137 | + }) |
| 138 | + await appendChange(globalStorageDir, "task-rollback", { |
| 139 | + path: "src/a.ts", |
| 140 | + operation: "update", |
| 141 | + checkpointId: "sha-2", |
| 142 | + }) |
| 143 | + |
| 144 | + const restoreFile = vi.fn().mockResolvedValue(undefined) |
| 145 | + mockedGetCheckpointService.mockResolvedValue({ restoreFile }) |
| 146 | + |
| 147 | + const outcome = await rollbackStep(makeTask(), ["src/a.ts"]) |
| 148 | + |
| 149 | + expect(outcome.checkpointId).toBe("sha-2") |
| 150 | + expect(restoreFile).toHaveBeenCalledWith("sha-2", "src/a.ts") |
| 151 | + }) |
| 152 | + |
| 153 | + it("fails listed files without journal entries and keeps the checkpoint when resolvable", async () => { |
| 154 | + await appendChange(globalStorageDir, "task-rollback", { |
| 155 | + path: "src/a.ts", |
| 156 | + operation: "create", |
| 157 | + checkpointId: "sha-1", |
| 158 | + }) |
| 159 | + |
| 160 | + const restoreFile = vi.fn().mockResolvedValue(undefined) |
| 161 | + mockedGetCheckpointService.mockResolvedValue({ restoreFile }) |
| 162 | + |
| 163 | + const outcome = await rollbackStep(makeTask(), ["src/a.ts", "src/missing.ts"]) |
| 164 | + |
| 165 | + expect(outcome.checkpointId).toBe("sha-1") |
| 166 | + expect(outcome.files[0]).toEqual({ filePath: "src/a.ts", success: true }) |
| 167 | + expect(outcome.files[1].success).toBe(false) |
| 168 | + expect(outcome.files[1].error).toBe("No change journal entry for this file") |
| 169 | + }) |
| 170 | + |
| 171 | + it("treats a missing global storage directory as an empty journal", async () => { |
| 172 | + // No context on the provider double → no journal location to read. |
| 173 | + const task = { |
| 174 | + taskId: "task-rollback", |
| 175 | + providerRef: { deref: vi.fn().mockReturnValue(undefined) }, |
| 176 | + } as unknown as Task |
| 177 | + |
| 178 | + const restoreFile = vi.fn().mockResolvedValue(undefined) |
| 179 | + mockedGetCheckpointService.mockResolvedValue({ restoreFile }) |
| 180 | + |
| 181 | + const outcome = await rollbackStep(task, ["src/a.ts"], "sha-step") |
| 182 | + |
| 183 | + expect(outcome.checkpointId).toBe("sha-step") |
| 184 | + expect(outcome.files[0].success).toBe(false) |
| 185 | + expect(outcome.files[0].error).toBe("File is not part of this step's checkpoint") |
| 186 | + expect(restoreFile).not.toHaveBeenCalled() |
| 187 | + }) |
| 188 | + it("fails every file when checkpoints are not enabled", async () => { |
| 189 | + mockedGetCheckpointService.mockResolvedValue(undefined) |
| 190 | + |
| 191 | + const outcome = await rollbackStep(makeTask(), ["src/a.ts"], "sha-step") |
| 192 | + |
| 193 | + expect(outcome.checkpointId).toBe("sha-step") |
| 194 | + expect(outcome.files).toEqual([ |
| 195 | + { filePath: "src/a.ts", success: false, error: "Checkpoints are not enabled for this task" }, |
| 196 | + ]) |
| 197 | + }) |
| 198 | +}) |
0 commit comments