Files
llmx/codex-cli/tests/parse-apply-patch.test.ts
2025-04-16 10:21:48 -07:00

46 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { parseApplyPatch } from "../src/lib/parse-apply-patch";
import { expect, test, describe } from "vitest";
// Helper function to unwrap a nonnull result in tests that expect success.
function mustParse(patch: string) {
const parsed = parseApplyPatch(patch);
if (parsed == null) {
throw new Error(
"Expected patch to be valid, but parseApplyPatch returned null",
);
}
return parsed;
}
describe("parseApplyPatch", () => {
test("parses create, update and delete operations in a single patch", () => {
const patch = `*** Begin Patch\n*** Add File: created.txt\n+hello\n+world\n*** Update File: updated.txt\n@@\n-old\n+new\n*** Delete File: removed.txt\n*** End Patch`;
const ops = mustParse(patch);
expect(ops).toEqual([
{
type: "create",
path: "created.txt",
content: "hello\nworld",
},
{
type: "update",
path: "updated.txt",
update: "@@\n-old\n+new",
added: 1,
deleted: 1,
},
{
type: "delete",
path: "removed.txt",
},
]);
});
test("returns null for an invalid patch (missing prefix)", () => {
const invalid = `*** Add File: foo.txt\n+bar\n*** End Patch`;
expect(parseApplyPatch(invalid)).toBeNull();
});
});