pi-mono/packages/coding-agent/test/edit-tool-diff-before-write.test.ts
Petr Houška a69ff894e4 fix(coding-agent): edit keeps line endings of untouched lines, diffs before writing
Edits are spliced into the original text, so lines the edit did not touch
keep their CRLF, LF or lone CR endings. The diff and patch are computed
before the file is written, so a failure there, such as running out of
memory on a large file, leaves the file unchanged.

fixes #8544
2026-11-06 00:34:41 +01:00

30 lines
952 B
TypeScript

import { describe, expect, it, vi } from "vitest";
import { createEditTool } from "../src/core/tools/edit.ts";
vi.mock("../src/core/tools/edit-diff.ts", async (importOriginal) => {
const original = await importOriginal<typeof import("../src/core/tools/edit-diff.ts")>();
return {
...original,
generateUnifiedPatch: () => {
throw new Error("diff failed");
},
};
});
describe("edit tool diff before write", () => {
it("should compute the diff before writing, so a diff failure leaves the file unchanged", async () => {
const writeFile = vi.fn(async () => {});
const tool = createEditTool(process.cwd(), {
operations: {
access: async () => {},
readFile: async () => Buffer.from("a\nb\n"),
writeFile,
},
});
await expect(
tool.execute("test-diff-before-write", { path: "f.txt", edits: [{ oldText: "b", newText: "B" }] }),
).rejects.toThrow("diff failed");
expect(writeFile).not.toHaveBeenCalled();
});
});