Skip to content

Commit 9d75b10

Browse files
committed
test(write_to_file): prove revert-then-reset ordering when handleError rejects
Address the remaining CodeRabbit finding: The "runs diff cleanup when handleError rejects" regression test asserted only invocation counts. It now uses the same deferred-promise sequencing pattern as the missing-parameter cleanup tests: the revertChanges mock awaits a deferred promise, so the test asserts reset() has NOT run while the revert is still pending, then resolves it and asserts the calls occur exactly in revertChanges-then-reset order (diffViewCallOrder).
1 parent 7b6835f commit 9d75b10

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,22 +1381,42 @@ describe("writeToFileTool", () => {
13811381
// aborted. A rejected handleError must not skip the diff cleanup: the unapproved
13821382
// streamed content has to be reverted and the diff view reset, or a user save
13831383
// could persist the failed write. The handleError rejection itself propagates
1384-
// (it is not swallowed by the cleanup).
1384+
// (it is not swallowed by the cleanup). As in the missing-parameter tests
1385+
// above, the revert mock awaits a deferred so the test proves the cleanup
1386+
// AWAITs revertChanges() before reset(): with the revert still pending,
1387+
// reset() must not have run yet.
13851388
mockHandleError.mockRejectedValue(new Error("handleError rejected (aborted task)"))
13861389
mockedCreateDirectoriesForFile.mockRejectedValue(
13871390
Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }),
13881391
)
1392+
const diffViewCallOrder: string[] = []
1393+
let resolveRevert: () => void = () => {}
1394+
const revertDeferred = new Promise<void>((resolve) => {
1395+
resolveRevert = resolve
1396+
})
1397+
mockCline.diffViewProvider.revertChanges.mockImplementation(async () => {
1398+
diffViewCallOrder.push("revert")
1399+
await revertDeferred
1400+
})
1401+
mockCline.diffViewProvider.reset.mockImplementation(async () => {
1402+
diffViewCallOrder.push("reset")
1403+
})
13891404

1390-
await expect(executeWriteFileTool({}, { fileExists: false })).rejects.toThrow(
1391-
"handleError rejected (aborted task)",
1392-
)
1405+
const executePromise = executeWriteFileTool({}, { fileExists: false })
1406+
await new Promise<void>((resolve) => setTimeout(resolve, 0))
13931407

1394-
// handleError was attempted with the write context...
1408+
// handleError was attempted with the write context and the cleanup has reached
1409+
// the deferred revert...
13951410
expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error))
1396-
// ...and the diff cleanup still ran despite its rejection: the unapproved
1397-
// content is reverted before the diff view reset.
13981411
expect(mockCline.diffViewProvider.revertChanges).toHaveBeenCalledTimes(1)
1399-
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1)
1412+
// ...and while the revert is still pending, reset() must not have run yet.
1413+
expect(mockCline.diffViewProvider.reset).not.toHaveBeenCalled()
1414+
1415+
resolveRevert()
1416+
await expect(executePromise).rejects.toThrow("handleError rejected (aborted task)")
1417+
1418+
// The unapproved content is reverted before the diff view reset.
1419+
expect(diffViewCallOrder).toEqual(["revert", "reset"])
14001420
expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled()
14011421
expect(mockCline.diffViewProvider.saveChanges).not.toHaveBeenCalled()
14021422
})

0 commit comments

Comments
 (0)