|
| 1 | +// npx vitest run src/core/webview/__tests__/webviewMessageHandler.rollback.spec.ts |
| 2 | +import { describe, expect, it, vi, beforeEach } from "vitest" |
| 3 | + |
| 4 | +import type { ExtensionMessage, WebviewMessage } from "@roo-code/types" |
| 5 | + |
| 6 | +import { webviewMessageHandler } from "../webviewMessageHandler" |
| 7 | +import { rollbackFile, rollbackStep } from "../../checkpoints/rollback" |
| 8 | +import type { Task } from "../../task/Task" |
| 9 | +import type { ClineProvider } from "../ClineProvider" |
| 10 | + |
| 11 | +// The rollback cases only call these two provider methods, so the provider |
| 12 | +// double below is cast once at this boundary; the spy is shared so results |
| 13 | +// can be asserted after the handler runs. |
| 14 | +vi.mock("../../checkpoints/rollback", () => ({ |
| 15 | + rollbackFile: vi.fn(), |
| 16 | + rollbackStep: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock("vscode", () => ({ |
| 20 | + window: { |
| 21 | + showErrorMessage: vi.fn(), |
| 22 | + }, |
| 23 | + workspace: { |
| 24 | + workspaceFolders: undefined, |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +// Structural mock: the handler only needs the task identity for these cases. |
| 29 | +const mockTask = {} as Task |
| 30 | +const postMessageToWebview = vi.fn(async (_message: ExtensionMessage) => undefined) |
| 31 | + |
| 32 | +function makeProvider(task: Task | undefined): ClineProvider { |
| 33 | + const provider = { |
| 34 | + getCurrentTask: () => task, |
| 35 | + postMessageToWebview, |
| 36 | + } |
| 37 | + // Cast at the spec boundary: the rollback cases only read getCurrentTask() |
| 38 | + // and observe postMessageToWebview calls on the structural double. |
| 39 | + return provider as unknown as ClineProvider |
| 40 | +} |
| 41 | + |
| 42 | +const provider = makeProvider(mockTask) |
| 43 | + |
| 44 | +describe("webviewMessageHandler - change card rollback", () => { |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks() |
| 47 | + }) |
| 48 | + |
| 49 | + describe("checkpointRollbackFile", () => { |
| 50 | + it("restores the file and posts the success outcome back to the webview", async () => { |
| 51 | + vi.mocked(rollbackFile).mockResolvedValueOnce({ filePath: "src/a.ts", success: true }) |
| 52 | + |
| 53 | + await webviewMessageHandler(provider, { |
| 54 | + type: "checkpointRollbackFile", |
| 55 | + payload: { cardTs: 1000, checkpointId: "abc123", filePath: "src/a.ts" }, |
| 56 | + }) |
| 57 | + |
| 58 | + expect(rollbackFile).toHaveBeenCalledWith(mockTask, "abc123", "src/a.ts") |
| 59 | + expect(postMessageToWebview).toHaveBeenCalledWith({ |
| 60 | + type: "checkpointRollbackResult", |
| 61 | + checkpointRollbackResult: { cardTs: 1000, filePath: "src/a.ts", success: true }, |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + it("posts the error outcome when the restore fails", async () => { |
| 66 | + vi.mocked(rollbackFile).mockResolvedValueOnce({ |
| 67 | + filePath: "src/a.ts", |
| 68 | + success: false, |
| 69 | + error: "checkpoint not found", |
| 70 | + }) |
| 71 | + |
| 72 | + await webviewMessageHandler(provider, { |
| 73 | + type: "checkpointRollbackFile", |
| 74 | + payload: { cardTs: 1000, checkpointId: "abc123", filePath: "src/a.ts" }, |
| 75 | + }) |
| 76 | + |
| 77 | + expect(postMessageToWebview).toHaveBeenCalledWith({ |
| 78 | + type: "checkpointRollbackResult", |
| 79 | + checkpointRollbackResult: { |
| 80 | + cardTs: 1000, |
| 81 | + filePath: "src/a.ts", |
| 82 | + success: false, |
| 83 | + error: "checkpoint not found", |
| 84 | + }, |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it("posts a correlated failure result when there is no current task", async () => { |
| 89 | + const emptyProvider = makeProvider(undefined) |
| 90 | + |
| 91 | + await webviewMessageHandler(emptyProvider, { |
| 92 | + type: "checkpointRollbackFile", |
| 93 | + payload: { cardTs: 1000, checkpointId: "abc123", filePath: "src/a.ts" }, |
| 94 | + }) |
| 95 | + |
| 96 | + expect(rollbackFile).not.toHaveBeenCalled() |
| 97 | + // The requesting card must clear its pending state, so the handler |
| 98 | + // posts a correlated failure instead of nothing. |
| 99 | + expect(postMessageToWebview).toHaveBeenCalledWith({ |
| 100 | + type: "checkpointRollbackResult", |
| 101 | + checkpointRollbackResult: { |
| 102 | + cardTs: 1000, |
| 103 | + filePath: "src/a.ts", |
| 104 | + success: false, |
| 105 | + error: "No active task to roll back from.", |
| 106 | + }, |
| 107 | + }) |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + describe("checkpointRollbackStep", () => { |
| 112 | + it("restores every step file and posts the aggregated outcome", async () => { |
| 113 | + vi.mocked(rollbackStep).mockResolvedValueOnce({ |
| 114 | + checkpointId: "abc123", |
| 115 | + files: [ |
| 116 | + { filePath: "src/a.ts", success: true }, |
| 117 | + { filePath: "src/b.ts", success: true }, |
| 118 | + ], |
| 119 | + }) |
| 120 | + |
| 121 | + await webviewMessageHandler(provider, { |
| 122 | + type: "checkpointRollbackStep", |
| 123 | + payload: { cardTs: 1000, checkpointId: "abc123", filePaths: ["src/a.ts", "src/b.ts"] }, |
| 124 | + }) |
| 125 | + |
| 126 | + expect(rollbackStep).toHaveBeenCalledWith(mockTask, ["src/a.ts", "src/b.ts"], "abc123") |
| 127 | + expect(postMessageToWebview).toHaveBeenCalledWith({ |
| 128 | + type: "checkpointRollbackResult", |
| 129 | + checkpointRollbackResult: { |
| 130 | + cardTs: 1000, |
| 131 | + success: true, |
| 132 | + files: [ |
| 133 | + { filePath: "src/a.ts", success: true }, |
| 134 | + { filePath: "src/b.ts", success: true }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + it("reports success false with the first failing file's error when a step file fails", async () => { |
| 141 | + vi.mocked(rollbackStep).mockResolvedValueOnce({ |
| 142 | + checkpointId: "abc123", |
| 143 | + files: [ |
| 144 | + { filePath: "src/a.ts", success: true }, |
| 145 | + { filePath: "src/b.ts", success: false, error: "boom" }, |
| 146 | + ], |
| 147 | + }) |
| 148 | + |
| 149 | + await webviewMessageHandler(provider, { |
| 150 | + type: "checkpointRollbackStep", |
| 151 | + payload: { cardTs: 1000, filePaths: ["src/a.ts", "src/b.ts"] }, |
| 152 | + }) |
| 153 | + |
| 154 | + // Without an explicit step checkpoint id the journal lookup is used. |
| 155 | + expect(rollbackStep).toHaveBeenCalledWith(mockTask, ["src/a.ts", "src/b.ts"], undefined) |
| 156 | + expect(postMessageToWebview).toHaveBeenCalledWith({ |
| 157 | + type: "checkpointRollbackResult", |
| 158 | + checkpointRollbackResult: { |
| 159 | + cardTs: 1000, |
| 160 | + success: false, |
| 161 | + error: "boom", |
| 162 | + files: [ |
| 163 | + { filePath: "src/a.ts", success: true }, |
| 164 | + { filePath: "src/b.ts", success: false, error: "boom" }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + it("posts a correlated failure result when there is no current task", async () => { |
| 171 | + const emptyProvider = makeProvider(undefined) |
| 172 | + |
| 173 | + await webviewMessageHandler(emptyProvider, { |
| 174 | + type: "checkpointRollbackStep", |
| 175 | + payload: { cardTs: 1000, filePaths: ["src/a.ts"] }, |
| 176 | + }) |
| 177 | + |
| 178 | + expect(rollbackStep).not.toHaveBeenCalled() |
| 179 | + expect(postMessageToWebview).toHaveBeenCalledWith({ |
| 180 | + type: "checkpointRollbackResult", |
| 181 | + checkpointRollbackResult: { |
| 182 | + cardTs: 1000, |
| 183 | + success: false, |
| 184 | + error: "No active task to roll back from.", |
| 185 | + }, |
| 186 | + }) |
| 187 | + }) |
| 188 | + |
| 189 | + it("ignores payloads that do not match the schema", async () => { |
| 190 | + await webviewMessageHandler(provider, { |
| 191 | + // Malformed on purpose (only the webview produces this message): the cast |
| 192 | + // lets the spec reach the handler's safeParse rejection without `any`. |
| 193 | + type: "checkpointRollbackFile", |
| 194 | + payload: { cardTs: 1000 } as unknown as WebviewMessage["payload"], |
| 195 | + }) |
| 196 | + await webviewMessageHandler(provider, { |
| 197 | + type: "checkpointRollbackStep", |
| 198 | + payload: { cardTs: 1000, filePaths: [] }, |
| 199 | + }) |
| 200 | + |
| 201 | + expect(rollbackFile).not.toHaveBeenCalled() |
| 202 | + expect(rollbackStep).not.toHaveBeenCalled() |
| 203 | + expect(postMessageToWebview).not.toHaveBeenCalled() |
| 204 | + }) |
| 205 | + }) |
| 206 | +}) |
0 commit comments