|
| 1 | +// npx vitest run core/tools/__tests__/applyDiffTool.guardedWrite.spec.ts |
| 2 | + |
| 3 | +import type { MockedFunction } from "vitest" |
| 4 | + |
| 5 | +import { fileExistsAtPath } from "../../../utils/fs" |
| 6 | +import type { Task } from "../../task/Task" |
| 7 | +import { ApplyDiffTool } from "../ApplyDiffTool" |
| 8 | + |
| 9 | +vi.mock("fs/promises", () => ({ |
| 10 | + default: { |
| 11 | + readFile: vi.fn().mockResolvedValue("original file content\n"), |
| 12 | + }, |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock("../../../utils/fs", () => ({ |
| 16 | + fileExistsAtPath: vi.fn().mockResolvedValue(true), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock("../../prompts/responses", () => ({ |
| 20 | + formatResponse: { |
| 21 | + toolError: vi.fn((msg: string) => `Error: ${msg}`), |
| 22 | + rooIgnoreError: vi.fn((filePath: string) => `Access denied: ${filePath}`), |
| 23 | + createPrettyPatch: vi.fn(() => "mock-diff"), |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock("../../diff/stats", () => ({ |
| 28 | + sanitizeUnifiedDiff: vi.fn((diff: string) => diff), |
| 29 | + computeDiffStats: vi.fn(() => ({ additions: 1, deletions: 1 })), |
| 30 | +})) |
| 31 | + |
| 32 | +describe("ApplyDiffTool.execute - guarded write (S4b, epic #1375)", () => { |
| 33 | + const mockedFileExistsAtPath = fileExistsAtPath as MockedFunction<typeof fileExistsAtPath> |
| 34 | + |
| 35 | + let tool: ApplyDiffTool |
| 36 | + let mockTask: Pick< |
| 37 | + Task, |
| 38 | + | "cwd" |
| 39 | + | "consecutiveMistakeCount" |
| 40 | + | "consecutiveMistakeCountForApplyDiff" |
| 41 | + | "recordToolError" |
| 42 | + | "rooIgnoreController" |
| 43 | + | "rooProtectedController" |
| 44 | + | "say" |
| 45 | + | "processQueuedMessages" |
| 46 | + | "didEditFile" |
| 47 | + | "api" |
| 48 | + | "diffStrategy" |
| 49 | + | "diffViewProvider" |
| 50 | + | "providerRef" |
| 51 | + | "fileContextTracker" |
| 52 | + > |
| 53 | + let mockSaveDirectly: MockedFunction<(...args: unknown[]) => Promise<unknown>> |
| 54 | + let mockAskApproval: MockedFunction<(...args: unknown[]) => Promise<boolean>> |
| 55 | + let mockHandleError: MockedFunction<(...args: unknown[]) => Promise<void>> |
| 56 | + let mockPushToolResult: MockedFunction<(...args: unknown[]) => void> |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + vi.clearAllMocks() |
| 60 | + |
| 61 | + mockedFileExistsAtPath.mockResolvedValue(true) |
| 62 | + |
| 63 | + mockSaveDirectly = vi.fn().mockResolvedValue({ |
| 64 | + newProblemsMessage: "", |
| 65 | + userEdits: undefined, |
| 66 | + finalContent: "new content", |
| 67 | + }) |
| 68 | + |
| 69 | + // Structural stubs for the guarded-write path: the real DiffViewProvider is |
| 70 | + // out of scope here, so vi.fn() doubles stand in for the members the tool |
| 71 | + // touches (the saveDirectly double also records the writeKind plumbing). |
| 72 | + const diffViewProviderStub = { |
| 73 | + editType: undefined as "create" | "modify" | undefined, |
| 74 | + originalContent: undefined as string | undefined, |
| 75 | + saveDirectly: mockSaveDirectly, |
| 76 | + pushToolWriteResult: vi.fn().mockResolvedValue("Saved file"), |
| 77 | + reset: vi.fn().mockResolvedValue(undefined), |
| 78 | + } |
| 79 | + mockTask = { |
| 80 | + cwd: "/workspace/project", |
| 81 | + consecutiveMistakeCount: 0, |
| 82 | + consecutiveMistakeCountForApplyDiff: new Map(), |
| 83 | + recordToolError: vi.fn(), |
| 84 | + rooIgnoreController: { |
| 85 | + validateAccess: vi.fn().mockReturnValue(true), |
| 86 | + } as unknown as Task["rooIgnoreController"], |
| 87 | + rooProtectedController: { |
| 88 | + isWriteProtected: vi.fn().mockReturnValue(false), |
| 89 | + } as unknown as Task["rooProtectedController"], |
| 90 | + say: vi.fn().mockResolvedValue(undefined), |
| 91 | + processQueuedMessages: vi.fn(), |
| 92 | + didEditFile: false, |
| 93 | + api: { |
| 94 | + getModel: () => ({ id: "claude-sonnet-4-5" }), |
| 95 | + } as unknown as Task["api"], |
| 96 | + diffStrategy: { |
| 97 | + applyDiff: vi.fn().mockResolvedValue({ success: true, content: "modified file content\n" }), |
| 98 | + } as unknown as Task["diffStrategy"], |
| 99 | + diffViewProvider: diffViewProviderStub as unknown as Task["diffViewProvider"], |
| 100 | + providerRef: { |
| 101 | + deref: vi.fn().mockReturnValue({ |
| 102 | + getState: vi.fn().mockResolvedValue({ |
| 103 | + diagnosticsEnabled: true, |
| 104 | + writeDelayMs: 1000, |
| 105 | + // Exercise the focus-disruption (saveDirectly) save path. |
| 106 | + experiments: { preventFocusDisruption: true }, |
| 107 | + }), |
| 108 | + }), |
| 109 | + } as unknown as Task["providerRef"], |
| 110 | + fileContextTracker: { |
| 111 | + trackFileContext: vi.fn().mockResolvedValue(undefined), |
| 112 | + } as unknown as Task["fileContextTracker"], |
| 113 | + } |
| 114 | + |
| 115 | + mockAskApproval = vi.fn().mockResolvedValue(true) |
| 116 | + mockHandleError = vi.fn().mockResolvedValue(undefined) |
| 117 | + mockPushToolResult = vi.fn() |
| 118 | + |
| 119 | + tool = new ApplyDiffTool() |
| 120 | + }) |
| 121 | + |
| 122 | + it("publishes through the guarded saveDirectly with edit kind", async () => { |
| 123 | + await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, { |
| 124 | + askApproval: mockAskApproval, |
| 125 | + handleError: mockHandleError, |
| 126 | + pushToolResult: mockPushToolResult, |
| 127 | + }) |
| 128 | + |
| 129 | + expect(mockSaveDirectly).toHaveBeenCalledWith( |
| 130 | + "src/thing.ts", |
| 131 | + "modified file content\n", |
| 132 | + false, |
| 133 | + true, |
| 134 | + 1000, |
| 135 | + "edit", |
| 136 | + ) |
| 137 | + expect(mockPushToolResult).toHaveBeenCalledWith("Saved file") |
| 138 | + expect(mockTask.didEditFile).toBe(true) |
| 139 | + expect(mockHandleError).not.toHaveBeenCalled() |
| 140 | + }) |
| 141 | + |
| 142 | + it("surfaces the unobserved edit remediation as a tool error", async () => { |
| 143 | + const guardError = new Error("File not read yet -- read the file, then retry.") |
| 144 | + mockSaveDirectly.mockRejectedValue(guardError) |
| 145 | + |
| 146 | + await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, { |
| 147 | + askApproval: mockAskApproval, |
| 148 | + handleError: mockHandleError, |
| 149 | + pushToolResult: mockPushToolResult, |
| 150 | + }) |
| 151 | + |
| 152 | + expect(mockHandleError).toHaveBeenCalledWith("applying diff", guardError) |
| 153 | + expect(vi.mocked(mockTask.diffViewProvider.reset)).toHaveBeenCalled() |
| 154 | + expect(mockTask.didEditFile).toBe(false) |
| 155 | + expect(mockPushToolResult).not.toHaveBeenCalledWith("Saved file") |
| 156 | + }) |
| 157 | +}) |
0 commit comments