import { describe, it, expect } from "vitest"; import { SCHEMA_VERSION, emptyArtifact, serializeArtifact, type Artifact, } from "../src/model"; function sampleArtifact(): Artifact { return { schemaVersion: SCHEMA_VERSION, document: { path: "docs/spec.md" }, anchors: { a2: { fingerprint: { text: "beta", before: "x", after: "y", lineHint: 5 } }, a1: { fingerprint: { text: "alpha", before: "", after: "", lineHint: 1 } }, }, threads: [ { id: "t1", anchorId: "a1", status: "open", messages: [ { id: "m1", author: { kind: "human", id: "ben" }, body: "hi", createdAt: "2026-06-10T00:00:00.000Z" }, ], }, ], attributions: [], proposals: [], }; } describe("serializeArtifact", () => { it("round-trips through JSON.parse", () => { const a = sampleArtifact(); const parsed = JSON.parse(serializeArtifact(a)) as Artifact; expect(parsed).toEqual(a); }); it("emits stable, sorted anchor keys and a trailing newline regardless of insertion order", () => { const a = sampleArtifact(); const out = serializeArtifact(a); expect(out.endsWith("\n")).toBe(true); // a1 must serialize before a2 even though a2 was inserted first expect(out.indexOf('"a1"')).toBeLessThan(out.indexOf('"a2"')); // byte-identical on re-serialize expect(serializeArtifact(JSON.parse(out))).toBe(out); }); it("emptyArtifact has the shared F3/F4 extension points present and empty", () => { const e = emptyArtifact("docs/x.md"); expect(e.schemaVersion).toBe(SCHEMA_VERSION); expect(e.document.path).toBe("docs/x.md"); expect(e.anchors).toEqual({}); expect(e.threads).toEqual([]); expect(e.attributions).toEqual([]); expect(e.proposals).toEqual([]); }); });