repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/overkill/__tests__/patches.test.ts
RawFile outline
- optimizer patch contract line 4
- parses exactly one through four exact operations line 5
- rejects malformed output, excess edits, and operation-specific fields line 25
- enforces target, content, and aggregate byte bounds line 52
- validates against the unchanged body before descending-byte application line 76
- rejects missing, ambiguous, and overlapping unchanged-body targets atomically line 96
- preserves trailing bytes for append, rejects empty candidates, and caps candidates line 144
import { describe, expect, it } from "vitest";
import { applyPatch, parsePatch } from "../lib/patches.ts";
describe("optimizer patch contract", () => {
it("parses exactly one through four exact operations", () => {
expect(
parsePatch({
edits: [
{ op: "append", content: "append" },
{ op: "insert_after", target: "a", content: " after" },
{ op: "replace", target: "b", content: "B" },
{ op: "delete", target: "c" },
],
}),
).toEqual({
edits: [
{ op: "append", content: "append" },
{ op: "insert_after", target: "a", content: " after" },
{ op: "replace", target: "b", content: "B" },
{ op: "delete", target: "c" },
],
});
});
it("rejects malformed output, excess edits, and operation-specific fields", () => {
for (const value of [
null,
{},
{ edits: [] },
{ edits: [{ op: "append", content: "x", target: "no" }] },
{ edits: [{ op: "delete", target: "x", content: "no" }] },
{ edits: [{ op: "replace", target: "", content: "x" }] },
{ edits: [{ op: "unknown", content: "x" }] },
{ edits: [{ op: "append", content: "x", extra: true }] },
{
edits: Array.from({ length: 5 }, () => ({
op: "append",
content: "x",
})),
},
{
edits: [
{ op: "append", content: "x" },
{ op: "append", content: "y" },
],
},
]) {
expect(() => parsePatch(value)).toThrow();
}
});
it("enforces target, content, and aggregate byte bounds", () => {
expect(() =>
parsePatch({
edits: [
{ op: "replace", target: "x".repeat(16 * 1024 + 1), content: "x" },
],
}),
).toThrow("16 KiB");
expect(() =>
parsePatch({
edits: [{ op: "append", content: "x".repeat(16 * 1024 + 1) }],
}),
).toThrow("16 KiB");
expect(() =>
parsePatch({
edits: Array.from({ length: 3 }, () => ({
op: "replace",
target: "x",
content: "x".repeat(12 * 1024),
})),
}),
).toThrow("32 KiB");
});
it("validates against the unchanged body before descending-byte application", () => {
const body = "é first\nsecond\nthird\n";
const parsed = parsePatch({
edits: [
{ op: "replace", target: "first", content: "one" },
{ op: "insert_after", target: "second", content: " + two" },
{ op: "delete", target: "third\n" },
],
});
expect(applyPatch(body, parsed)).toEqual({
body: "é one\nsecond + two\n",
changed: true,
reports: [
{ index: 0, status: "applied" },
{ index: 1, status: "applied" },
{ index: 2, status: "applied" },
],
});
});
it("rejects missing, ambiguous, and overlapping unchanged-body targets atomically", () => {
const missing = applyPatch(
"one two",
parsePatch({ edits: [{ op: "replace", target: "none", content: "x" }] }),
);
expect(missing.body).toBe("one two");
expect(missing.changed).toBe(false);
expect(missing.reports).toEqual([
{
index: 0,
status: "rejected",
reason: "target must occur exactly once",
},
]);
const overlappingOccurrence = applyPatch(
"aaa",
parsePatch({ edits: [{ op: "replace", target: "aa", content: "x" }] }),
);
expect(overlappingOccurrence).toMatchObject({
body: "aaa",
changed: false,
reports: [
{ status: "rejected", reason: "target must occur exactly once" },
],
});
const overlap = applyPatch(
"abc",
parsePatch({
edits: [
{ op: "replace", target: "abc", content: "x" },
{ op: "delete", target: "bc" },
],
}),
);
expect(overlap.body).toBe("abc");
expect(
overlap.reports.every((report) => report.status === "rejected"),
).toBe(true);
for (const patch of [{ edits: [{ op: "delete", target: "x" }] }]) {
const result = applyPatch("abc abc", parsePatch(patch));
expect(result.body).toBe("abc abc");
expect(result.changed).toBe(false);
expect(
result.reports.every((report) => report.status === "rejected"),
).toBe(true);
}
});
it("preserves trailing bytes for append, rejects empty candidates, and caps candidates", () => {
expect(
applyPatch(
"body",
parsePatch({ edits: [{ op: "append", content: "tail" }] }),
).body,
).toBe("body\ntail");
expect(
applyPatch(
"body\n",
parsePatch({ edits: [{ op: "append", content: "tail" }] }),
).body,
).toBe("body\ntail");
expect(
applyPatch("x", parsePatch({ edits: [{ op: "delete", target: "x" }] })),
).toMatchObject({
body: "x",
changed: false,
reports: [
{ status: "rejected", reason: "candidate body must be nonempty" },
],
});
expect(
applyPatch(
"x".repeat(256 * 1024),
parsePatch({ edits: [{ op: "append", content: "x" }] }),
),
).toMatchObject({
changed: false,
reports: [
{ status: "rejected", reason: "candidate body exceeds 256 KiB" },
],
});
});
});