import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { attemptsOf, provenanceFrom, readFieldProvenance } from "./draft-provenance.mjs"; test("attemptsOf: the chosen attempt by value, the refused ones with their rules", () => { const u = { attempts: [ { value: { title: "A" }, hard: ["title over 60 chars"], soft: [] }, { value: { title: "B" }, hard: [], soft: ["no name grounded"] }, ], chosen: { value: { title: "B" }, hard: [], soft: ["no name grounded"] }, }; assert.deepEqual(attemptsOf(u), { attempts: 2, chosen: 2, repaired: false, failed: [{ attempt: 1, hard: ["title over 60 chars"] }], soft: ["no name grounded"], }); // No attempts: nothing to say. A clean first attempt: chosen 1, nothing failed. assert.equal(attemptsOf({ skipped: "has a value" }), null); assert.deepEqual(attemptsOf({ attempts: [{ value: "x", hard: [], soft: [] }], chosen: { value: "x", hard: [], soft: [] } }).failed, []); // A repaired value is none of the attempts; the mark's attempt is the fallback when there is no chosen. assert.equal(attemptsOf({ attempts: [{ text: "a", hard: ["q"] }], chosen: { text: "fixed" } }).repaired, true); assert.equal(attemptsOf({ attempts: [{ value: 1, hard: [] }, { value: 2, hard: [] }] }, 2).chosen, 2); }); test("provenanceFrom: release 28's units by target, release 25's narrate file by card id", () => { const manifest = { timeline: [ { id: "c01", type: "clip", onscreen: { title: "T" }, drafted: { onscreen: { task: "onscreen", model: "qwen3:8b", at: "2026-10-13T00:00:00Z", attempt: 2 } } }, { id: "k01", type: "card", narration: { text: "Words." } }, ], }; const files = [ { file: "onscreen-draft.json", doc: { task: "onscreen", model: "qwen3:8b", at: "2026-10-13T00:00:00Z", units: { c01: { target: { entry: "c01", field: "onscreen" }, attempts: [{ value: { title: "Too long" }, hard: ["title over 60"], soft: [] }, { value: { title: "T" }, hard: [], soft: [] }], chosen: { value: { title: "T" }, hard: [], soft: [] } }, c02: { target: { entry: "c02", field: "onscreen" }, skipped: "has a value" }, }, }, }, { file: "narration-draft.json", mtimeMs: Date.UTC(2026, 9, 12, 20, 54), doc: { k01: { heading: "H", attempts: [{ text: "\"q\"", hard: ["text in quotation marks: no quotes in narration"], soft: [] }, { text: "Words.", hard: [], soft: [] }], chosen: { text: "Words.", hard: [], soft: [] } } }, }, ]; const p = provenanceFrom(files, manifest); assert.deepEqual(Object.keys(p).sort(), ["narrate/k01/narration", "onscreen/c01/onscreen"]); assert.equal(p["onscreen/c01/onscreen"].chosen, 2); assert.deepEqual(p["onscreen/c01/onscreen"].failed, [{ attempt: 1, hard: ["title over 60"] }]); assert.equal(p["onscreen/c01/onscreen"].model, "qwen3:8b"); assert.equal(p["narrate/k01/narration"].file, "narration-draft.json"); assert.equal(p["narrate/k01/narration"].model, null); assert.equal(p["narrate/k01/narration"].at, "2026-10-12T20:54:00.000Z"); assert.deepEqual(p["narrate/k01/narration"].failed[0].hard, ["text in quotation marks: no quotes in narration"]); }); test("readFieldProvenance reads the draft files beside the manifest and skips one that does not parse", async () => { const dir = mkdtempSync(path.join(tmpdir(), "prov-")); try { writeFileSync(path.join(dir, "cut-draft.json"), JSON.stringify({ model: "m", at: "a", units: { cut: { target: { field: "timeline" }, attempts: [{ value: ["c01"], hard: [], soft: [] }], chosen: { value: ["c01"], hard: [], soft: [] } } } })); writeFileSync(path.join(dir, "teaser-draft.json"), "{ not json"); writeFileSync(path.join(dir, "notes.json"), "{}"); const p = await readFieldProvenance(dir, { timeline: [] }); assert.deepEqual(Object.keys(p), ["cut/*/timeline"]); assert.equal(p["cut/*/timeline"].attempts, 1); } finally { rmSync(dir, { recursive: true, force: true }); } });