Skip to content

Commit 3159d0a

Browse files
test(apply-patch): cover the move refusal under both preventFocusDisruption settings
The reviewer on the move-refusal thread asked for a regression covering both `preventFocusDisruption` settings and confirming that the rejection preserves the source file. The product was re-measured first, because the answer is not the one the escalation that funded this implied: the refusal (ApplyPatchTool.ts:594-:643) sits above the `isPreventFocusDisruptionEnabled` split - guarded `saveDirectly` at :646, raw `fs.mkdir` + `fs.writeFile` at :727 - so both settings already refuse, and the wording cannot diverge because there is one statement of the condition. What was missing was coverage: every row in this file ran with the flag armed on, which left the symmetry a claim about line numbers rather than something a test had checked. Three rows, all with the flag off: - a destination that names the source's file is refused, and neither the mkdir, the writeFile nor the unlink runs; - an ordinary flag-off move still publishes through mkdir + fs.writeFile + unlink with the saveDirectly double unused - that is what pins which branch ran, and it says the refusal has not become a ban on flag-off moves; - a destination that is not there reads as "absent", which is not the source, and the move proceeds. The harness arms the flag on, and the provider stub captures the getState function by value, so a row has to change what that function resolves to (`mockGetState.mockResolvedValue`) rather than re-point the `mockGetState` binding. Re-pointing it compiles, runs green and exercises the guarded branch; the two must-move rows were green-but-wrong that way until the mechanism was measured, and it is recorded here because a flag-off row that is secretly flag-on is the kind of green that survives a review. `mkdir` joins `writeFile` in the `mockedFsPromises` cast, loosely typed for the same stated reason - both carry overload sets that MockedFunction would collapse. Controls, as run counts: - gating the refusal on the flag (`moveRefusal !== undefined && isPreventFocusDisruptionEnabled`), the asymmetry the row objects to, reddens 1 of 47 and the row it reddens is the flag-off refusal row. That is a boundary control for this claim. - removing the refusal statement reddens 5 of 47 (the three consequence rows, the withheld-identity row and this file's flag-off row). That is a whole-feature revert, not a control, and it is counted as such. Shipped tree: 47 passed (47), exit 0. `tsc --noEmit -p .` reports one error, the junction artefact at activate/registerCommands.ts(88,102), which is not this change. eslint --prune-suppressions --max-warnings=0 exits 0 on the touched file; the prune also rewrote unrelated entries elsewhere in src/eslint-suppressions.json, which is reverted here rather than shipped, because cleanup with nothing to do with these rows belongs in its own commit. prettier --check clean. No it.runIf added or removed, and nothing claimed about the platform: the geometry these rows drive is the doubles' inode table, as in the rows landed in f653f86, so they run wherever the suite runs. This does not close the separate finding that the flag-off destination write bypasses `guardedWrite` altogether (thread bwM6z8Hy8): the identity refusal is not that guard, and closing that changes write semantics for every flag-off user, which is the owner's call.
1 parent 42077a2 commit 3159d0a

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const mockedFsPromises = vi.mocked(
2929
// Typed loosely because fsPromises.writeFile carries an overload set that MockedFunction
3030
// would collapse to one arbitrary member; the rows only ever ask whether it was called.
3131
writeFile: ReturnType<typeof vi.fn>
32+
// Added for the flag-off move rows: the raw branch mkdirs the destination's parent before
33+
// writing, and the rows ask whether that mkdir ran. Typed loosely for the same reason as
34+
// writeFile above - fsPromises.mkdir is overloaded and the rows only ever ask about calls.
35+
mkdir: ReturnType<typeof vi.fn>
3236
}
3337
},
3438
)
@@ -899,6 +903,109 @@ describe("ApplyPatchTool.execute - guarded write (S4b, epic #1375)", () => {
899903
expect(mockTask.didEditFile).toBe(false)
900904
})
901905

906+
it("move: refuses a same-file destination on the raw write path too, not only the guarded one", async () => {
907+
// The refusal sits above the isPreventFocusDisruptionEnabled split (:646 vs. :727), so a
908+
// flag-off run refuses for the same reason a flag-on run does. Until this row every test in
909+
// this file ran with the flag on, which made that symmetry a claim about line numbers rather
910+
// than something anyone had checked. The flag-off branch publishes with a raw mkdir plus
911+
// fs.writeFile rather than through the diff view, so those are the calls this row expects not
912+
// to see.
913+
const sourceKey = path.resolve("/workspace/project", "src/old.ts")
914+
const destKey = path.resolve("/workspace/project", "src/new.ts")
915+
// The harness arms preventFocusDisruption on for every row. The binding holds the function
916+
// object the provider stub captured, so a row has to change what that function resolves
917+
// to rather than re-point the binding.
918+
mockGetState.mockResolvedValue({
919+
diagnosticsEnabled: true,
920+
writeDelayMs: 1000,
921+
experiments: { preventFocusDisruption: false },
922+
})
923+
924+
mockedFsPromises.default.stat.mockImplementation(() => Promise.resolve(_identityOf(sourceKey)))
925+
926+
await tool.execute({ patch: movePatch }, mockTask as Task, {
927+
askApproval: mockAskApproval,
928+
handleError: mockHandleError,
929+
pushToolResult: mockPushToolResult,
930+
toolCallId: "flag-off-move-destination-is-the-source",
931+
})
932+
933+
expect(mockPushToolResult).toHaveBeenCalledWith(expect.stringContaining("the two paths name the same file"))
934+
expect(mockedFsPromises.default.writeFile).not.toHaveBeenCalled()
935+
expect(mockedFsPromises.default.mkdir).not.toHaveBeenCalled()
936+
expect(mockedFsPromises.default.unlink).not.toHaveBeenCalled()
937+
expect(mockSaveDirectly).not.toHaveBeenCalled()
938+
expect(mockTask.didEditFile).toBe(false)
939+
// Both spellings went to the filesystem before the refusal, here as on the other path.
940+
expect(mockedFsPromises.default.stat).toHaveBeenCalledWith(sourceKey, { bigint: true })
941+
expect(mockedFsPromises.default.stat).toHaveBeenCalledWith(destKey, { bigint: true })
942+
})
943+
944+
it("move: publishes through the raw write path when the flag is off and the two spellings name different files", async () => {
945+
// The other half of the coverage, and the half that says the refusal did not become a ban:
946+
// with different inodes this is an ordinary move, and the branch that answers for it is the
947+
// mkdir + fs.writeFile one. The saveDirectly double staying unused is what pins the branch.
948+
const sourceKey = path.resolve("/workspace/project", "src/old.ts")
949+
const destKey = path.resolve("/workspace/project", "src/new.ts")
950+
// The harness arms preventFocusDisruption on for every row. The binding holds the function
951+
// object the provider stub captured, so a row has to change what that function resolves
952+
// to rather than re-point the binding.
953+
mockGetState.mockResolvedValue({
954+
diagnosticsEnabled: true,
955+
writeDelayMs: 1000,
956+
experiments: { preventFocusDisruption: false },
957+
})
958+
959+
await tool.execute({ patch: movePatch }, mockTask as Task, {
960+
askApproval: mockAskApproval,
961+
handleError: mockHandleError,
962+
pushToolResult: mockPushToolResult,
963+
toolCallId: "flag-off-move-between-different-files",
964+
})
965+
966+
expect(mockedFsPromises.default.mkdir).toHaveBeenCalledWith(path.dirname(destKey), { recursive: true })
967+
expect(mockedFsPromises.default.writeFile).toHaveBeenCalledWith(destKey, "modified file content\n", "utf8")
968+
expect(mockedFsPromises.default.unlink).toHaveBeenCalledWith(sourceKey)
969+
expect(mockSaveDirectly).not.toHaveBeenCalled()
970+
expect(mockPushToolResult).toHaveBeenCalledWith(expect.stringContaining("Saved file"))
971+
expect(mockTask.didEditFile).toBe(true)
972+
})
973+
974+
it("move: treats a destination that is not there as not-the-source on the raw write path", async () => {
975+
// Absence is answered the same way on both paths: a destination that is not there is not the
976+
// source, so the identity read returns "absent" and the move proceeds. The row that arms an
977+
// EACCES destination pins the other answer the filesystem can give.
978+
const sourceKey = path.resolve("/workspace/project", "src/old.ts")
979+
const destKey = path.resolve("/workspace/project", "src/new.ts")
980+
const enoent = Object.assign(new Error("ENOENT: no such file or directory, stat '" + destKey + "'"), {
981+
code: "ENOENT",
982+
})
983+
// The harness arms preventFocusDisruption on for every row. The binding holds the function
984+
// object the provider stub captured, so a row has to change what that function resolves
985+
// to rather than re-point the binding.
986+
mockGetState.mockResolvedValue({
987+
diagnosticsEnabled: true,
988+
writeDelayMs: 1000,
989+
experiments: { preventFocusDisruption: false },
990+
})
991+
992+
mockedFsPromises.default.stat.mockImplementation((target: unknown) =>
993+
String(target) === destKey ? Promise.reject(enoent) : Promise.resolve(_identityOf(String(target))),
994+
)
995+
996+
await tool.execute({ patch: movePatch }, mockTask as Task, {
997+
askApproval: mockAskApproval,
998+
handleError: mockHandleError,
999+
pushToolResult: mockPushToolResult,
1000+
toolCallId: "flag-off-move-destination-absent",
1001+
})
1002+
1003+
expect(mockTask.recordToolError).not.toHaveBeenCalled()
1004+
expect(mockHandleError).not.toHaveBeenCalled()
1005+
expect(mockedFsPromises.default.writeFile).toHaveBeenCalledWith(destKey, "modified file content\n", "utf8")
1006+
expect(mockedFsPromises.default.unlink).toHaveBeenCalledWith(sourceKey)
1007+
})
1008+
9021009
it("move: rejects a partial source onto an observed destination before changing any state", async () => {
9031010
// The destination content is the source file plus one hunk, so it can only be
9041011
// as complete as the view the model had of the source. Rejecting before the

0 commit comments

Comments
 (0)