mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
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
30 lines
952 B
TypeScript
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();
|
|
});
|
|
});
|