// MARKS, the writers (plans/clip-marks.md): updateMarks (add, remove, a // verdict, all), updateClaimKnobs on one mark, the On-screen table on the // first, the window rule over every mark, and `umtool set` / `umtool show`. // One mark is written `claim`, two or more `claims`; a mark added and removed // again leaves the file the bytes it was. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-marks-")); process.env.REPORTS_DIR = ROOT; const { MANIFEST_NAME, StaleToken, manifestToken, updateClaimKnobs, updateClip, updateMarks, updateOnscreen } = await import("./manifest.mjs"); const { freshMarkId, markVerdictsOf, setMarks, verdictOfInput } = await import("./knobs.mjs"); const { knobsLines, knobsOf, setCommand } = await import("./knobsCli.mjs"); const HUCK_FC = { verdicts: { CONTRADICTED: { label: "Grift", color: "#e3b23c", stampLabel: "Grift" }, CORROBORATED: { label: "Episodes", color: "#6fa8dc", stampLabel: "Seizure" }, }, stamp: { seconds: 3 }, }; const base = () => ({ schemaVersion: 1, slug: "huck", title: "Huck", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, transition: 0.5, chrome: { engine: "hyperframes", layout: "deck", factcheck: HUCK_FC } }, timeline: [ { type: "clip", id: "first", video: "v1", start: 171.33, end: 211.78, claim: { id: "first", verdict: "CORROBORATED", at: 188.41 }, quote: "q" }, { type: "clip", id: "er", video: "v2", start: 275.2, end: 304.21, claim: { id: "er", verdict: "CORROBORATED" } }, { type: "clip", id: "dozen", video: "v3", start: 336.96, end: 385.89, claim: { id: "er", verdict: "CORROBORATED", at: 349.25 } }, { type: "clip", id: "fed", video: "v4", start: 6117, end: 6155.63 }, ], }); let n = 0; async function project(manifest = base()) { const name = `p${(n += 1)}`; const dir = path.join(ROOT, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); return { dir, name }; } const readRaw = (dir) => readFile(path.join(dir, MANIFEST_NAME), "utf8"); const read = async (dir) => JSON.parse(await readRaw(dir)); const entry = (m, id) => m.timeline.find((e) => e.id === id); test.after(() => rm(ROOT, { recursive: true, force: true })); test("knobs: setMarks writes one as claim, two as claims, none as neither -- in the marks' place among the keys", () => { const e = { id: "c", claim: { id: "a", verdict: "PARTLY" }, quote: "q" }; setMarks(e, [{ id: "a", verdict: "PARTLY" }, { id: "b", verdict: "PARTLY" }]); assert.deepEqual(Object.keys(e), ["id", "claims", "quote"]); setMarks(e, [{ id: "a", verdict: "PARTLY" }]); assert.deepEqual(Object.keys(e), ["id", "claim", "quote"]); setMarks(e, []); assert.deepEqual(Object.keys(e), ["id", "quote"]); setMarks(e, [{ id: "z", verdict: "PARTLY" }]); assert.deepEqual(Object.keys(e), ["id", "quote", "claim"]); }); test("knobs: a new mark's id is the clip's when free, else .2, .3 …; a verdict by name or this cut's label", () => { const m = base(); assert.equal(freshMarkId(m, "fed"), "fed"); assert.equal(freshMarkId(m, "first"), "first.2"); m.timeline[0].claims = [{ id: "first", verdict: "CORROBORATED" }, { id: "first.2", verdict: "CORROBORATED" }]; delete m.timeline[0].claim; assert.equal(freshMarkId(m, "first"), "first.3"); assert.equal(verdictOfInput("Episodes", m.render), "CORROBORATED"); assert.equal(verdictOfInput("grift", m.render), "CONTRADICTED"); assert.equal(verdictOfInput("Seizure", m.render), "CORROBORATED", "the stamp word too"); assert.equal(verdictOfInput("partly", m.render), "PARTLY"); assert.throws(() => verdictOfInput("nonsense", m.render), /"nonsense" is not a verdict: CORROBORATED \(Episodes\)/); const v = markVerdictsOf(m); assert.deepEqual(v.slice(0, 2).map((x) => [x.verdict, x.label, x.stampLabel, x.stock]), [["CORROBORATED", "Episodes", "Seizure", true], ["CONTRADICTED", "Grift", "Grift", true]]); assert.equal(v.length, 5); }); test("updateMarks add: a second mark on the clip gets a NEW id and makes claims; add then remove is the bytes it was", async () => { const { dir } = await project(); const before = await readRaw(dir); const r = await updateMarks(dir, "first", { add: { verdict: "Episodes", at: 180.5 } }, { token: await manifestToken(dir) }); assert.equal(r.added, "first.2"); const e = entry(await read(dir), "first"); assert.equal("claim" in e, false); assert.deepEqual(e.claims, [{ id: "first", verdict: "CORROBORATED", at: 188.41 }, { id: "first.2", verdict: "CORROBORATED", at: 180.5 }]); assert.deepEqual(r.stampsOn, { first: "first", "first.2": "first" }); await updateMarks(dir, "first", { remove: "first.2" }); assert.equal(await readRaw(dir), before, "back to one mark: `claim`, byte for byte"); // A first mark on a clip with none takes the clip's id. const f = await updateMarks(dir, "fed", { add: { verdict: "CONTRADICTED", at: 6149.65, count: 2, stamp: true } }); assert.equal(f.added, "fed"); assert.deepEqual(entry(await read(dir), "fed").claim, { id: "fed", verdict: "CONTRADICTED", at: 6149.65, count: 2 }); }); test("updateMarks: refuses overlapping stamps, an at outside the window, a ninth mark, a stale token -- the file as it was", async () => { const { dir } = await project(); const before = await readRaw(dir); await assert.rejects( updateMarks(dir, "first", { add: { verdict: "CORROBORATED", at: 189.5 } }), /the marks first \(at 188\.41\) and first\.2 \(at 189\.5\) overlap -- first's stamp stays 3 s; land first\.2 at 191\.41 or later/, ); await assert.rejects(updateMarks(dir, "first", { add: { verdict: "CORROBORATED", at: 250 } }), /claims\[1\]\.at 250 is outside the clip's window 171\.33–211\.78/); await assert.rejects(updateMarks(dir, "first", { add: { verdict: "nope" } }), /is not a verdict/); await assert.rejects(updateMarks(dir, "first", { add: { verdict: "PARTLY", colour: 1 } }), /add\.colour is not a mark field/); await assert.rejects(updateMarks(dir, "first", { remove: "zz" }), /carries no mark zz/); await assert.rejects(updateMarks(dir, "first", { add: { verdict: "PARTLY" } }, { token: "stale" }), StaleToken); await assert.rejects(updateMarks(dir, "first", { add: {}, remove: "x" }), /exactly one of/); assert.equal(await readRaw(dir), before); const eight = Array.from({ length: 8 }, (_, i) => ({ id: `m${i}`, verdict: "PARTLY" })); await updateMarks(dir, "fed", { set: eight }); await assert.rejects(updateMarks(dir, "fed", { add: { verdict: "PARTLY" } }), /carries 8 marks already \(at most 8\)/); }); test("updateMarks set / mark-verdict: all at once (null: none); a verdict changes on every clip carrying the id", async () => { const { dir } = await project(); await updateMarks(dir, "first", { set: [{ id: "first", verdict: "Episodes", at: 188.41 }, { id: "first.0", verdict: "Episodes", at: "175", label: "EARLIER" }] }); assert.deepEqual(entry(await read(dir), "first").claims[1], { id: "first.0", verdict: "CORROBORATED", at: 175, label: "EARLIER" }); await assert.rejects(updateMarks(dir, "first", { set: [{ id: "a", verdict: "PARTLY", when: 1 }] }), /claims\[0\]\.when is not a mark field/); // "er" is one event on two clips: its verdict moves on both. await updateMarks(dir, "er", { mark: "er", verdict: "Grift" }); const m = await read(dir); assert.equal(entry(m, "er").claim.verdict, "CONTRADICTED"); assert.equal(entry(m, "dozen").claim.verdict, "CONTRADICTED"); await updateMarks(dir, "first", { set: null }); const e = entry(await read(dir), "first"); assert.equal("claim" in e || "claims" in e, false); }); test("updateClaimKnobs: `mark` names the mark (default the first); a lone claim edited in place", async () => { const { dir } = await project(); await updateMarks(dir, "first", { add: { verdict: "CORROBORATED", at: 195 } }); const r = await updateClaimKnobs(dir, "first", { count: 2, hold: 2 }, { mark: "first.2" }); assert.deepEqual(r.claim, { id: "first.2", verdict: "CORROBORATED", at: 195, count: 2, hold: 2 }); assert.equal(r.stamps, true); const r1 = await updateClaimKnobs(dir, "first", { label: "ONE" }); assert.equal(r1.claim.id, "first"); await assert.rejects(updateClaimKnobs(dir, "first", { count: 2 }, { mark: "zz" }), /carries no mark zz/); // Moving the second mark onto the first's stamp is refused. await assert.rejects(updateClaimKnobs(dir, "first", { at: 189 }, { mark: "first.2" }), /overlap/); }); test("updateClip: a window that leaves any mark outside is refused, naming the mark", async () => { const { dir } = await project(); await updateMarks(dir, "first", { add: { verdict: "CORROBORATED", at: 205 } }); await assert.rejects(updateClip(dir, "first", { end: 200 }), /the mark first\.2's second \(claims\[1\]\.at 205\) must lie inside the window 171\.33–200/); await updateClip(dir, "first", { end: 206 }); }); test("updateOnscreen: the table's claim is the FIRST mark; the others stay", async () => { const { dir } = await project(); await updateMarks(dir, "first", { add: { verdict: "CORROBORATED", at: 195 } }); await updateOnscreen(dir, { first: { title: "First", claim: { id: "first", verdict: "CORROBORATED" } } }); let e = entry(await read(dir), "first"); assert.deepEqual(e.claims.map((m) => [m.id, m.at]), [["first", 188.41], ["first.2", 195]], "the first's knobs kept, the second untouched"); await updateOnscreen(dir, { first: { title: "First", claim: null } }); e = entry(await read(dir), "first"); assert.deepEqual(e.claim, { id: "first.2", verdict: "CORROBORATED", at: 195 }, "the first removed: one mark left, written as claim"); }); test("previewSchedule: a clip's marks stamp in the On-screen preview; a drafted row replaces only the first", async () => { const { previewSchedule } = await import("./onscreen.mjs"); const m = base(); setMarks(m.timeline[0], [{ id: "first", verdict: "CORROBORATED", at: 188.41 }, { id: "first.2", verdict: "CONTRADICTED", at: 200 }]); const ids = (s) => s.factcheck.stamps.map((x) => x.claim); const plain = previewSchedule({ variantManifest: m, built: null, draft: new Map(), metas: [] }); assert.deepEqual(ids(plain), ["first", "first.2", "er"]); const drafted = previewSchedule({ variantManifest: m, built: null, draft: new Map(), metas: [], claimsDraft: new Map([["first", { id: "lead", verdict: "PARTLY" }]]) }); assert.ok(ids(drafted).includes("lead") && ids(drafted).includes("first.2") && !ids(drafted).includes("first")); }); test("umtool set: mark.add=@, mark.remove, mark.verdict, marks=, claim.* --mark; show prints the marks", async () => { const { dir, name } = await project(); const lines = []; const run = (...args) => setCommand(args, { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }); assert.equal(await run(name, "first", "mark.add=Episodes@176.2"), 0); assert.match(lines.at(-2), /^first: marks first CORROBORATED at=188\.41 \| first\.2 CORROBORATED at=176\.2$/); assert.match(lines.at(-1), /added first\.2/); assert.equal(await run(name, "first", "claim.count=2", "--mark", "first.2"), 0); assert.equal(entry(await read(dir), "first").claims[1].count, 2); assert.equal(await run(name, "first", "mark.verdict=Grift", "--mark", "first.2"), 0); assert.equal(entry(await read(dir), "first").claims[1].verdict, "CONTRADICTED"); const show = knobsLines(await read(dir)).find((l) => l.trim().startsWith("first ")); assert.match(show, /marks first CORROBORATED at=188\.41 \(stamps here\) \| first\.2 CONTRADICTED at=176\.2 count=2 \(stamps here\)/); assert.equal(knobsOf(await read(dir)).clips.find((c) => c.id === "first").marks.length, 2); assert.equal(await run(name, "first", "mark.add=Episodes@177"), 1); assert.match(lines.at(-1), /^ERR .*overlap/); assert.equal(await run(name, "first", "mark.add=Episodes@1@2"), 2); assert.equal(await run(name, "first", "mark.remove=first.2"), 0); assert.deepEqual(entry(await read(dir), "first").claim, { id: "first", verdict: "CORROBORATED", at: 188.41 }); assert.equal(await run(name, "fed", 'marks=[{"id":"fed","verdict":"Grift","at":6149.65},{"id":"fed.2","verdict":"Episodes","at":6130}]'), 0); assert.equal(entry(await read(dir), "fed").claims.length, 2); assert.equal(await run(name, "fed", "marks="), 0); assert.equal("claims" in entry(await read(dir), "fed"), false); assert.match(lines.at(-1), /fed: no marks/); }); test("a teaser takes marks (lane counter-zero): updateMarks and `umtool set` write them, `at` seconds from the card's start", async () => { const sides = { series: [{ id: "LEFT", label: "Left" }, { id: "CENTER", label: "Center" }] }; const m = base(); m.render.chrome = { engine: "hyperframes", layout: "deck", deck: { counter: sides } }; m.timeline = [ { type: "clip", id: "d2", video: "v1", start: 10, end: 20, claim: { id: "d2", verdict: "CENTER", at: 15 } }, { type: "teaser", id: "x-d28", seconds: 5, lines: [{ text: "@Leaflit on X · 1 Feb 2026", role: "overline" }, { text: "“… a moderate.”", role: "kicker" }] }, ]; const { dir, name } = await project(m); const r = await updateMarks(dir, "x-d28", { add: { verdict: "CENTER", at: 1.45 } }, { token: await manifestToken(dir) }); assert.equal(r.added, "x-d28"); assert.deepEqual(entry(await read(dir), "x-d28").claim, { id: "x-d28", verdict: "CENTER", at: 1.45 }); // A second past the card's end is refused, in the renderer's words, and nothing is written. await assert.rejects( updateMarks(dir, "x-d28", { add: { verdict: "LEFT", at: 6 } }, { token: await manifestToken(dir) }), /claims\[1\]\.at 6 is outside the card's 0–5 \(seconds from the card's start\) — nothing was written/, ); assert.deepEqual(entry(await read(dir), "x-d28").claim, { id: "x-d28", verdict: "CENTER", at: 1.45 }); // The CLI, as the operator runs it: remove, then add by the series' label. const lines = []; const run = (...args) => setCommand(args, { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }); assert.equal(await run(name, "x-d28", "mark.remove=x-d28"), 0); assert.equal("claim" in entry(await read(dir), "x-d28"), false); assert.equal(await run(name, "x-d28", "mark.add=CENTER@2.15"), 0, lines.join("\n")); assert.deepEqual(entry(await read(dir), "x-d28").claim, { id: "x-d28", verdict: "CENTER", at: 2.15 }); assert.equal(await run(name, "x-d28", "claim.hold=2"), 0, lines.join("\n")); assert.equal(entry(await read(dir), "x-d28").claim.hold, 2); });