import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { emptyArtifact, serializeArtifact, type Artifact, type Proposal, } from "../src/model"; import { CoauthorStore } from "../src/store"; import { addProposal, proposalBody, removeProposal } from "../src/proposalModel"; const agent = { kind: "agent" as const, id: "claude", agent: { sdk: "@cline/sdk", model: "sonnet", sessionId: "s1" }, }; describe("Proposal model (spec §6.3, INV-13)", () => { it("serializes proposals with stable field order and survives a round-trip", () => { const a = emptyArtifact("docs/x.md"); a.anchors["a_1"] = { fingerprint: { text: "old text", before: "", after: "", lineHint: 0 }, }; const p: Proposal = { id: "pr_1", anchorId: "a_1", replacement: "new text", author: agent, createdAt: "2026-06-10T00:00:00.000Z", turnId: "turn-1", instruction: "tighten", }; a.proposals.push(p); const reloaded = JSON.parse(serializeArtifact(a)) as Artifact; expect(reloaded.proposals).toHaveLength(1); expect(reloaded.proposals[0]).toEqual(p); // optional fields are OMITTED (not null) when absent const bare: Proposal = { id: "pr_2", anchorId: "a_1", replacement: "r", author: agent, createdAt: "2026-06-10T00:00:00.000Z", }; a.proposals.push(bare); const again = JSON.parse(serializeArtifact(a)) as Artifact; expect("turnId" in again.proposals[1]).toBe(false); expect("instruction" in again.proposals[1]).toBe(false); // serialization is byte-stable (INV-2) expect(serializeArtifact(again)).toBe(serializeArtifact(a)); }); }); describe("CoauthorStore prune with proposals (spec §6.3)", () => { it("retains anchors referenced only by proposals; prunes them after removal", () => { const dir = mkdtempSync(join(tmpdir(), "cowriting-prune-")); try { const store = new CoauthorStore(dir); store.update("docs/x.md", (a) => { a.anchors["a_p"] = { fingerprint: { text: "t", before: "", after: "", lineHint: 0 }, }; a.proposals.push({ id: "pr_1", anchorId: "a_p", replacement: "r", author: agent, createdAt: "2026-06-10T00:00:00.000Z", }); }); expect(store.load("docs/x.md")!.anchors["a_p"]).toBeDefined(); store.update("docs/x.md", (a) => { a.proposals = a.proposals.filter((p) => p.id !== "pr_1"); }); expect(store.load("docs/x.md")!.anchors["a_p"]).toBeUndefined(); } finally { rmSync(dir, { recursive: true, force: true }); } }); }); describe("proposalModel helpers (spec §6.4)", () => { const fp = { text: "old line one\nold line two", before: "", after: "", lineHint: 3 }; it("addProposal creates the anchor + pending record; removeProposal deletes by id", () => { const a = emptyArtifact("docs/x.md"); const { proposalId, anchorId } = addProposal(a, fp, "new line", agent, { turnId: "turn-9", instruction: "shorten", }); expect(a.anchors[anchorId].fingerprint).toEqual(fp); expect(a.proposals).toHaveLength(1); expect(a.proposals[0].id).toBe(proposalId); expect(a.proposals[0].replacement).toBe("new line"); expect(a.proposals[0].turnId).toBe("turn-9"); expect(a.proposals[0].instruction).toBe("shorten"); expect(removeProposal(a, proposalId)).toBe(true); expect(a.proposals).toHaveLength(0); expect(removeProposal(a, proposalId)).toBe(false); }); it("proposalBody renders instruction + a fenced unified diff of old → new", () => { const a = emptyArtifact("docs/x.md"); addProposal(a, fp, "new only line", agent, { instruction: "merge the lines" }); const body = proposalBody(fp.text, a.proposals[0]); expect(body).toContain("**Claude proposes** — _merge the lines_"); expect(body).toContain("```diff"); expect(body).toContain("- old line one"); expect(body).toContain("- old line two"); expect(body).toContain("+ new only line"); }); });