d5beb619aa
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
76 lines
2.6 KiB
TypeScript
76 lines
2.6 KiB
TypeScript
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";
|
|
|
|
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 });
|
|
}
|
|
});
|
|
});
|