Files
vscode-cowriting-plugin/test/pendingEdits.test.ts
T
2026-06-10 10:50:07 -07:00

52 lines
2.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { minimizeReplace, PendingEditRegistry } from "../src/pendingEdits";
import type { Provenance } from "../src/model";
const AGENT: Provenance = {
kind: "agent", id: "claude",
agent: { sdk: "@cline/sdk", model: "sonnet", sessionId: "run-1" },
};
describe("PendingEditRegistry (INV-9)", () => {
it("matches and consumes an exact registration", () => {
const reg = new PendingEditRegistry();
reg.register({ docPath: "d.md", start: 3, end: 7, newText: "new", provenance: AGENT, turnId: "t1" });
const hit = reg.match("d.md", { start: 3, end: 7, text: "new" });
expect(hit?.turnId).toBe("t1");
expect(reg.match("d.md", { start: 3, end: 7, text: "new" })).toBeNull();
});
it("does not match a different doc, range, or text (human typing stays human)", () => {
const reg = new PendingEditRegistry();
reg.register({ docPath: "d.md", start: 3, end: 7, newText: "new", provenance: AGENT });
expect(reg.match("other.md", { start: 3, end: 7, text: "new" })).toBeNull();
expect(reg.match("d.md", { start: 3, end: 8, text: "new" })).toBeNull();
expect(reg.match("d.md", { start: 3, end: 7, text: "neww" })).toBeNull();
});
it("unregister removes a failed application", () => {
const reg = new PendingEditRegistry();
const p = { docPath: "d.md", start: 0, end: 0, newText: "x", provenance: AGENT };
reg.register(p);
reg.unregister(p);
expect(reg.match("d.md", { start: 0, end: 0, text: "x" })).toBeNull();
});
});
describe("minimizeReplace (host diff-minimization mirror)", () => {
it("trims a common suffix (the E2E-observed case)", () => {
expect(minimizeReplace("target sentence", "REWRITTEN-BY-CLAUDE sentence")).toEqual({ prefix: 0, suffix: 9 });
});
it("trims a common prefix", () => {
expect(minimizeReplace("Hello world", "Hello there")).toEqual({ prefix: 6, suffix: 0 });
});
it("trims both, never overlapping", () => {
expect(minimizeReplace("aba", "aa")).toEqual({ prefix: 1, suffix: 1 });
expect(minimizeReplace("aaaa", "aa")).toEqual({ prefix: 2, suffix: 0 });
});
it("identical texts minimize to nothing", () => {
expect(minimizeReplace("same", "same")).toEqual({ prefix: 4, suffix: 0 });
});
it("disjoint texts trim nothing", () => {
expect(minimizeReplace("abc", "xyz")).toEqual({ prefix: 0, suffix: 0 });
});
});