/** * Contract-identity ACCEPTANCE §5 (core half) — the shared stamp record: * header form, frontmatter-safe serialization, and the tolerant parser that * gates read the stamp back with. */ import { describe, expect, it } from "vitest"; import type { ContractDomain } from "./preimage.js"; import { type ContractStampRecord, formatStampHeader, parseContractStamp, serializeContractStamp, } from "./stamp.js"; const FCID = "a3f9e21b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b785"; const DOMAINS: Record = { components: "1".repeat(64), tokens: "2".repeat(64), canonicalMap: "3".repeat(64), policy: "4".repeat(64), }; const RECORD: ContractStampRecord = { contractVersion: 12, fcid: FCID, domains: DOMAINS }; describe("formatStampHeader", () => { it("prefixes the shared pin formatter's output — never a second pin format", () => { expect(formatStampHeader(RECORD)).toBe("fragments contract v12 · a3f9e21"); }); }); describe("serializeContractStamp", () => { it("renders plain header lines with the literal header form on the first line", () => { expect(serializeContractStamp(RECORD)).toBe( [ "contract: fragments contract v12 · a3f9e21", `contract-fcid: ${FCID}`, `contract-domains: components=${DOMAINS.components} tokens=${DOMAINS.tokens} canonicalMap=${DOMAINS.canonicalMap} policy=${DOMAINS.policy}`, ].join("\n") ); }); it("omits the domains line when sub-hashes are unknown", () => { const serialized = serializeContractStamp({ contractVersion: 12, fcid: FCID }); expect(serialized).not.toContain("contract-domains:"); expect(serialized.split("\n")).toHaveLength(2); }); }); describe("parseContractStamp", () => { it("round-trips serialize → parse, with and without domains", () => { expect(parseContractStamp(serializeContractStamp(RECORD))).toEqual(RECORD); expect(parseContractStamp(serializeContractStamp({ contractVersion: 12, fcid: FCID }))).toEqual( { contractVersion: 12, fcid: FCID } ); }); it("finds the stamp inside a rendered agent-context-shaped file", () => { const rendered = `--- generated_by: fragments-sdk@0.0.0 preset: universal last_init: 2026-06-09T00:00:00.000Z ${serializeContractStamp(RECORD)} --- # Agent context for this project Prefer canonical components over raw HTML elements. `; expect(parseContractStamp(rendered)).toEqual(RECORD); }); it("returns null when the stamp is absent or malformed — never throws", () => { expect(parseContractStamp("")).toBeNull(); expect(parseContractStamp("# Just markdown\n\ncontract talk, no stamp\n")).toBeNull(); // Header without the fcid line (and vice versa) is not a stamp. expect(parseContractStamp("contract: fragments contract v12 · a3f9e21\n")).toBeNull(); expect(parseContractStamp(`contract-fcid: ${FCID}\n`)).toBeNull(); // Short fcid in the header must prefix the full fcid. expect( parseContractStamp(`contract: fragments contract v12 · 0000000\ncontract-fcid: ${FCID}\n`) ).toBeNull(); // Truncated full fcid. expect( parseContractStamp( `contract: fragments contract v12 · a3f9e21\ncontract-fcid: ${FCID.slice(0, 40)}\n` ) ).toBeNull(); // Malformed domains line poisons the whole stamp. expect( parseContractStamp( `${serializeContractStamp({ contractVersion: 12, fcid: FCID })}\ncontract-domains: components=zzz\n` ) ).toBeNull(); expect( parseContractStamp( `${serializeContractStamp({ contractVersion: 12, fcid: FCID })}\ncontract-domains: components=${DOMAINS.components}\n` ) ).toBeNull(); }); });