Skip to content

Commit aab1cb9

Browse files
author
easonLiangWorldedtech
committed
test(tools): cover the diff-view branch of the observation rule
The registration for this row (comment 6107451284) lists three non-publishing outcomes that must leave the task-wide registry as they found it: a failed match, a rejected approval, and the diff-view branch. The previous commit shipped tests for the first two; this adds the third for each tool: run the tool with the focus-disruption experiment off, let the edit succeed through saveChanges(), and assert the registry holds no entry for the path. Before the fix the read-time observe left the token behind on this branch too, so all five new tests run red against the pre-fix production file (5/5) and green after it; the five touched specs are 127 passed. eslint --max-warnings=0 and prettier --check are clean on all five spec files.
1 parent e975f07 commit aab1cb9

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎src/core/tools/__tests__/applyDiffTool.guardedWrite.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,39 @@ describe("ApplyDiffTool.execute - guarded write (S4b, epic #1375)", () => {
304304
expect(mockSaveDirectly).not.toHaveBeenCalled()
305305
expect(mockTask.observationRegistry.has(path.resolve("/workspace/project", "src/thing.ts"))).toBe(false)
306306
})
307+
308+
it("leaves the registry empty after the diff-view save", async () => {
309+
// The diff-view branch saves through saveChanges(), which never consults the
310+
// registry, so the read must not leave a token behind for a later write_to_file
311+
// to spend on an unrelated overwrite.
312+
const diffViewStub = mockTask.diffViewProvider as unknown as Record<string, unknown>
313+
diffViewStub.open = vi.fn().mockResolvedValue(undefined)
314+
diffViewStub.update = vi.fn().mockResolvedValue(undefined)
315+
diffViewStub.scrollToFirstDiff = vi.fn()
316+
diffViewStub.saveChanges = vi.fn().mockResolvedValue({
317+
newProblemsMessage: "",
318+
userEdits: undefined,
319+
finalContent: "new content",
320+
})
321+
mockTask.providerRef = {
322+
deref: vi.fn().mockReturnValue({
323+
getState: vi.fn().mockResolvedValue({
324+
diagnosticsEnabled: true,
325+
writeDelayMs: 1000,
326+
// Diff-view save path: no focus-disruption experiment.
327+
experiments: {},
328+
}),
329+
}),
330+
} as unknown as Task["providerRef"]
331+
332+
await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, {
333+
askApproval: mockAskApproval,
334+
handleError: mockHandleError,
335+
pushToolResult: mockPushToolResult,
336+
})
337+
338+
expect(diffViewStub.saveChanges).toHaveBeenCalled()
339+
expect(mockSaveDirectly).not.toHaveBeenCalled()
340+
expect(mockTask.observationRegistry.has(path.resolve("/workspace/project", "src/thing.ts"))).toBe(false)
341+
})
307342
})

‎src/core/tools/__tests__/applyPatchTool.execute.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,41 @@ describe("ApplyPatchTool.execute - guarded write (S4b, epic #1375)", () => {
359359
expect(mockTask.observationRegistry.has(path.resolve("/workspace/project", "src/thing.ts"))).toBe(false)
360360
})
361361

362+
it("leaves the registry empty after the diff-view save", async () => {
363+
// The diff-view branch saves through saveChanges(), which never consults the
364+
// registry, so the read must not leave a token behind for a later write_to_file
365+
// to spend on an unrelated overwrite.
366+
const diffViewStub = mockTask.diffViewProvider as unknown as Record<string, unknown>
367+
diffViewStub.open = vi.fn().mockResolvedValue(undefined)
368+
diffViewStub.update = vi.fn().mockResolvedValue(undefined)
369+
diffViewStub.scrollToFirstDiff = vi.fn()
370+
diffViewStub.saveChanges = vi.fn().mockResolvedValue({
371+
newProblemsMessage: "",
372+
userEdits: undefined,
373+
finalContent: "new content",
374+
})
375+
mockTask.providerRef = {
376+
deref: vi.fn().mockReturnValue({
377+
getState: vi.fn().mockResolvedValue({
378+
diagnosticsEnabled: true,
379+
writeDelayMs: 1000,
380+
// Diff-view save path: no focus-disruption experiment.
381+
experiments: {},
382+
}),
383+
}),
384+
} as unknown as Task["providerRef"]
385+
386+
await tool.execute({ patch: updatePatch }, mockTask as Task, {
387+
askApproval: mockAskApproval,
388+
handleError: mockHandleError,
389+
pushToolResult: mockPushToolResult,
390+
})
391+
392+
expect(diffViewStub.saveChanges).toHaveBeenCalled()
393+
expect(mockSaveDirectly).not.toHaveBeenCalled()
394+
expect(mockTask.observationRegistry.has(path.resolve("/workspace/project", "src/thing.ts"))).toBe(false)
395+
})
396+
362397
it("add: publishes the new file through the guarded saveDirectly with create kind", async () => {
363398
mockedFileExistsAtPath.mockResolvedValueOnce(false)
364399

‎src/core/tools/__tests__/editFileTool.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,20 @@ describe("editFileTool", () => {
953953
expect(result).toContain("Changes were rejected by the user.")
954954
expect(mockTask.observationRegistry.get(absoluteFilePath)).toBeUndefined()
955955
})
956+
957+
it("leaves the registry empty after the diff-view save", async () => {
958+
// The diff-view branch saves through saveChanges(), which never consults the
959+
// registry, so this tool's read must not leave a token behind for a later
960+
// write_to_file to spend on an unrelated overwrite.
961+
const result = await executeEditFileTool(
962+
{ old_string: "Line 2", new_string: "Modified Line 2" },
963+
{ fileExists: true, fileContent: "Line 1\nLine 2\nLine 3" },
964+
)
965+
966+
expect(result).toContain("Tool result message")
967+
expect(mockTask.diffViewProvider.saveChanges).toHaveBeenCalled()
968+
expect(mockTask.observationRegistry.get(absoluteFilePath)).toBeUndefined()
969+
})
956970
})
957971

958972
describe("CRLF normalization", () => {

‎src/core/tools/__tests__/editTool.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,5 +627,19 @@ describe("editTool", () => {
627627
expect(result).toContain("Changes were rejected by the user.")
628628
expect(mockTask.observationRegistry.get(absoluteFilePath)).toBeUndefined()
629629
})
630+
631+
it("leaves the registry empty after the diff-view save", async () => {
632+
// The diff-view branch saves through saveChanges(), which never consults the
633+
// registry, so this tool's read must not leave a token behind for a later
634+
// write_to_file to spend on an unrelated overwrite.
635+
const result = await executeEditTool(
636+
{ old_string: "Line 2", new_string: "Modified Line 2" },
637+
{ fileContent: "Line 1\nLine 2\nLine 3" },
638+
)
639+
640+
expect(result).toContain("Tool result message")
641+
expect(mockTask.diffViewProvider.saveChanges).toHaveBeenCalled()
642+
expect(mockTask.observationRegistry.get(absoluteFilePath)).toBeUndefined()
643+
})
630644
})
631645
})

‎src/core/tools/__tests__/searchReplaceTool.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,5 +642,19 @@ describe("searchReplaceTool", () => {
642642
expect(result).toContain("Changes were rejected by the user.")
643643
expect(mockCline.observationRegistry.get(absoluteFilePath)).toBeUndefined()
644644
})
645+
646+
it("leaves the registry empty after the diff-view save", async () => {
647+
// The diff-view branch saves through saveChanges(), which never consults the
648+
// registry, so this tool's read must not leave a token behind for a later
649+
// write_to_file to spend on an unrelated overwrite.
650+
const result = await executeSearchReplaceTool(
651+
{ old_string: "Line 2", new_string: "Modified Line 2" },
652+
{ fileContent: "Line 1\nLine 2\nLine 3" },
653+
)
654+
655+
expect(result).toContain("Tool result message")
656+
expect(mockCline.diffViewProvider.saveChanges).toHaveBeenCalled()
657+
expect(mockCline.observationRegistry.get(absoluteFilePath)).toBeUndefined()
658+
})
645659
})
646660
})

0 commit comments

Comments
 (0)