/** * Schema-conformance helper (F5 SLICE-1, PUC-5): every serialized artifact in * the unit suite must validate against the published cross-rung schema — * contract drift fails CI (spec §7.4). */ import { readFileSync } from "node:fs"; import Ajv from "ajv/dist/2020"; import { serializeArtifact, type Artifact } from "../../src/model"; const schemaUrl = new URL("../../schemas/coauthoring-sidecar.schema.json", import.meta.url); const schema = JSON.parse(readFileSync(schemaUrl, "utf8")); const ajv = new Ajv({ allErrors: true }); const validate = ajv.compile(schema); export function validateSidecar(data: unknown): { valid: boolean; errors: string[] } { const valid = validate(data) as boolean; const errors = (validate.errors ?? []).map((e) => `${e.instancePath || "/"} ${e.message}`); return { valid, errors }; } export function expectValidSidecar(artifact: Artifact): void { const { valid, errors } = validateSidecar(JSON.parse(serializeArtifact(artifact))); if (!valid) throw new Error(`sidecar failed schema validation:\n${errors.join("\n")}`); }