|
| 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 mockSaveChanges: MockedFunction<(...args: unknown[]) => Promise<unknown>> |
| 55 | + let mockAskApproval: MockedFunction<(...args: unknown[]) => Promise<boolean>> |
| 56 | + let mockHandleError: MockedFunction<(...args: unknown[]) => Promise<void>> |
| 57 | + let mockPushToolResult: MockedFunction<(...args: unknown[]) => void> |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + vi.clearAllMocks() |
| 61 | + |
| 62 | + mockedFileExistsAtPath.mockResolvedValue(true) |
| 63 | + |
| 64 | + mockSaveDirectly = vi.fn().mockResolvedValue({ |
| 65 | + newProblemsMessage: "", |
| 66 | + userEdits: undefined, |
| 67 | + finalContent: "new content", |
| 68 | + }) |
| 69 | + mockSaveChanges = vi.fn().mockResolvedValue({ |
| 70 | + newProblemsMessage: "", |
| 71 | + userEdits: undefined, |
| 72 | + finalContent: "new content", |
| 73 | + }) |
| 74 | + |
| 75 | + // Structural stubs for the guarded-write path: the real DiffViewProvider is |
| 76 | + // out of scope here, so vi.fn() doubles stand in for the members the tool |
| 77 | + // touches (the saveDirectly double also records the writeKind plumbing). |
| 78 | + const diffViewProviderStub = { |
| 79 | + editType: undefined as "create" | "modify" | undefined, |
| 80 | + originalContent: undefined as string | undefined, |
| 81 | + saveDirectly: mockSaveDirectly, |
| 82 | + saveChanges: mockSaveChanges, |
| 83 | + open: vi.fn().mockResolvedValue(undefined), |
| 84 | + update: vi.fn().mockResolvedValue(undefined), |
| 85 | + scrollToFirstDiff: vi.fn(), |
| 86 | + pushToolWriteResult: vi.fn().mockResolvedValue("Saved file"), |
| 87 | + reset: vi.fn().mockResolvedValue(undefined), |
| 88 | + } |
| 89 | + mockTask = { |
| 90 | + cwd: "/workspace/project", |
| 91 | + consecutiveMistakeCount: 0, |
| 92 | + consecutiveMistakeCountForApplyDiff: new Map(), |
| 93 | + recordToolError: vi.fn(), |
| 94 | + rooIgnoreController: { |
| 95 | + validateAccess: vi.fn().mockReturnValue(true), |
| 96 | + } as unknown as Task["rooIgnoreController"], |
| 97 | + rooProtectedController: { |
| 98 | + isWriteProtected: vi.fn().mockReturnValue(false), |
| 99 | + } as unknown as Task["rooProtectedController"], |
| 100 | + say: vi.fn().mockResolvedValue(undefined), |
| 101 | + processQueuedMessages: vi.fn(), |
| 102 | + didEditFile: false, |
| 103 | + api: { |
| 104 | + getModel: () => ({ id: "claude-sonnet-4-5" }), |
| 105 | + } as unknown as Task["api"], |
| 106 | + diffStrategy: { |
| 107 | + applyDiff: vi.fn().mockResolvedValue({ success: true, content: "modified file content\n" }), |
| 108 | + } as unknown as Task["diffStrategy"], |
| 109 | + diffViewProvider: diffViewProviderStub as unknown as Task["diffViewProvider"], |
| 110 | + providerRef: { |
| 111 | + deref: vi.fn().mockReturnValue({ |
| 112 | + getState: vi.fn().mockResolvedValue({ |
| 113 | + diagnosticsEnabled: true, |
| 114 | + writeDelayMs: 1000, |
| 115 | + // Exercise the focus-disruption (saveDirectly) save path. |
| 116 | + experiments: { preventFocusDisruption: true }, |
| 117 | + }), |
| 118 | + }), |
| 119 | + } as unknown as Task["providerRef"], |
| 120 | + fileContextTracker: { |
| 121 | + trackFileContext: vi.fn().mockResolvedValue(undefined), |
| 122 | + } as unknown as Task["fileContextTracker"], |
| 123 | + } |
| 124 | + |
| 125 | + mockAskApproval = vi.fn().mockResolvedValue(true) |
| 126 | + mockHandleError = vi.fn().mockResolvedValue(undefined) |
| 127 | + mockPushToolResult = vi.fn() |
| 128 | + |
| 129 | + tool = new ApplyDiffTool() |
| 130 | + }) |
| 131 | + |
| 132 | + it("publishes through the guarded saveDirectly with edit kind", async () => { |
| 133 | + await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, { |
| 134 | + askApproval: mockAskApproval, |
| 135 | + handleError: mockHandleError, |
| 136 | + pushToolResult: mockPushToolResult, |
| 137 | + }) |
| 138 | + |
| 139 | + expect(mockSaveDirectly).toHaveBeenCalledWith( |
| 140 | + "src/thing.ts", |
| 141 | + "modified file content\n", |
| 142 | + false, |
| 143 | + true, |
| 144 | + 1000, |
| 145 | + "edit", |
| 146 | + ) |
| 147 | + expect(mockPushToolResult).toHaveBeenCalledWith("Saved file") |
| 148 | + expect(mockTask.didEditFile).toBe(true) |
| 149 | + expect(mockHandleError).not.toHaveBeenCalled() |
| 150 | + }) |
| 151 | + |
| 152 | + it("surfaces the unobserved edit remediation as a tool error", async () => { |
| 153 | + const guardError = new Error("File not read yet -- read the file, then retry.") |
| 154 | + mockSaveDirectly.mockRejectedValue(guardError) |
| 155 | + |
| 156 | + await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, { |
| 157 | + askApproval: mockAskApproval, |
| 158 | + handleError: mockHandleError, |
| 159 | + pushToolResult: mockPushToolResult, |
| 160 | + }) |
| 161 | + |
| 162 | + expect(mockHandleError).toHaveBeenCalledWith("applying diff", guardError) |
| 163 | + expect(vi.mocked(mockTask.diffViewProvider.reset)).toHaveBeenCalled() |
| 164 | + expect(mockTask.didEditFile).toBe(false) |
| 165 | + expect(mockPushToolResult).not.toHaveBeenCalledWith("Saved file") |
| 166 | + }) |
| 167 | + |
| 168 | + it("passes the edit kind to the diff-view save path", async () => { |
| 169 | + // Without focus disruption the tool saves through the diff view, and the |
| 170 | + // kind it passes must carry its intent: a targeted edit, not the default |
| 171 | + // full-file replacement. |
| 172 | + // The settings double is rebuilt for this test so the focus-disruption |
| 173 | + // experiment is off and the tool takes the diff-view save path. |
| 174 | + mockTask.providerRef = { |
| 175 | + deref: () => ({ |
| 176 | + getState: vi.fn().mockResolvedValue({ |
| 177 | + diagnosticsEnabled: true, |
| 178 | + writeDelayMs: 1000, |
| 179 | + experiments: {}, |
| 180 | + }), |
| 181 | + }), |
| 182 | + } as unknown as Task["providerRef"] |
| 183 | + |
| 184 | + await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, { |
| 185 | + askApproval: mockAskApproval, |
| 186 | + handleError: mockHandleError, |
| 187 | + pushToolResult: mockPushToolResult, |
| 188 | + }) |
| 189 | + |
| 190 | + expect(mockSaveChanges).toHaveBeenCalledWith(true, 1000, "edit") |
| 191 | + expect(mockSaveDirectly).not.toHaveBeenCalled() |
| 192 | + expect(mockPushToolResult).toHaveBeenCalledWith("Saved file") |
| 193 | + expect(mockHandleError).not.toHaveBeenCalled() |
| 194 | + }) |
| 195 | +}) |
0 commit comments