// MARKS (plans/clip-marks.md): an entry's `claims: [mark, …]` -- each mark // its own stamp and tally step at its own `at`. A lone `claim` is a list of // one, and a cut whose entries carry one claim each schedules, stamps and // counts exactly as it did before marks. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { CLAIM_MARKS_MAX, claimOf, marksOf, normalizeMarks, rawMarksOf, resolveFactcheck, roundStamps, STAMP_MOTION, stampSchedule, tallyFinals, tallySteps, validateClaims, } from "./factcheck.mjs"; import { deckSchedule } from "./deck.mjs"; import { deckCues } from "./chrome-deck.mjs"; import { stampCues } from "./chrome-stamp.mjs"; const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const CHROME = { engine: "hyperframes", layout: "deck", deck: {} }; const RENDER = { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE, chrome: CHROME }; const PROV = { siteOrigin: "https://example.test", channelSlug: "demo-channel" }; const near = (a, b, msg) => assert.ok(Math.abs(a - b) < 1e-3, `${msg}: ${a} != ${b}`); // ---- the shape ---------------------------------------------------------------- test("normalizeMarks: each mark normalised; 1–8, one id once; errors named claims[k]", () => { assert.deepEqual( normalizeMarks([{ id: "a", verdict: "PARTLY", at: 12, count: 1 }, { id: "a.2", verdict: "CONTRADICTED", stamp: true }]), [{ id: "a", verdict: "PARTLY", at: 12 }, { id: "a.2", verdict: "CONTRADICTED" }], ); assert.throws(() => normalizeMarks([]), /at least one mark/); assert.throws(() => normalizeMarks({ id: "a" }), /claims must be a list/); const nine = Array.from({ length: CLAIM_MARKS_MAX + 1 }, (_, i) => ({ id: `m${i}`, verdict: "PARTLY" })); assert.throws(() => normalizeMarks(nine), /9 marks \(at most 8\)/); assert.throws(() => normalizeMarks([{ id: "a", verdict: "PARTLY" }, { id: "a", verdict: "PARTLY" }]), /the mark a is on this entry twice/); assert.throws(() => normalizeMarks([{ id: "a", verdict: "PARTLY" }, { id: "b", verdict: "PARTLY", hold: 0.1 }]), /^Error: claims\[1\]\.hold must be from 0\.5 to 10/); }); test("marksOf / claimOf / rawMarksOf: a lone claim is a list of one; claimOf is the first mark", () => { const one = { type: "clip", id: "c", claim: { id: "k", verdict: "PARTLY" } }; const two = { type: "clip", id: "c", claims: [{ id: "k", verdict: "PARTLY" }, { id: "k.2", verdict: "CONTRADICTED", at: 3 }] }; assert.deepEqual(marksOf(one), [{ id: "k", verdict: "PARTLY" }]); assert.deepEqual(marksOf(two).map((m) => m.id), ["k", "k.2"]); assert.deepEqual(claimOf(two), { id: "k", verdict: "PARTLY" }); // A teaser carries marks as a clip does (lane counter-zero: a post shown as a text card that counts). assert.deepEqual(marksOf({ ...two, type: "teaser" }).map((m) => m.id), ["k", "k.2"]); assert.deepEqual(marksOf({ ...two, claim: { id: "x", verdict: "PARTLY" } }), [], "both set: none (validateClaims refuses it)"); assert.equal(claimOf({ type: "clip", id: "c" }), null); assert.equal(rawMarksOf(two).length, 2); assert.equal(rawMarksOf({ claim: null }).length, 0); }); // ---- validation ----------------------------------------------------------------- const clip = (id, start, end, extra) => ({ type: "clip", id, start, end, ...extra }); test("validateClaims: claim and claims together refused; a mark's at in the window; one id, one verdict across entries", () => { const both = { timeline: [clip("c1", 0, 30, { claim: { id: "k", verdict: "PARTLY" }, claims: [{ id: "k", verdict: "PARTLY" }] })] }; assert.match(validateClaims(both)[0], /timeline\[0\] \(c1\): claim and claims are both set/); const out = { timeline: [clip("c1", 100, 130, { claims: [{ id: "a", verdict: "PARTLY", at: 101 }, { id: "b", verdict: "PARTLY", at: 131 }] })] }; assert.match(validateClaims(out)[0], /timeline\[0\] \(c1\)\.claims\[1\]\.at 131 is outside the clip's window 100–130/); const split = { timeline: [ clip("c1", 0, 30, { claims: [{ id: "a", verdict: "PARTLY" }, { id: "b", verdict: "CORROBORATED", at: 20 }] }), clip("c2", 40, 60, { claim: { id: "b", verdict: "CONTRADICTED" } }), ], }; assert.match(validateClaims(split)[0], /timeline\[1\] \(c2\)\.claim b is CONTRADICTED here and CORROBORATED at timeline\[0\] \(c1\)/); assert.match(validateClaims({ timeline: [clip("c1", 0, 30, { claims: [] })] })[0], /claims must name at least one mark/); // A teaser's marks: `at` is seconds from the card's start, 0 to its `seconds` (else to TEASER_MARK_MAX). assert.deepEqual(validateClaims({ timeline: [{ type: "teaser", id: "t", seconds: 5, claims: [{ id: "a", verdict: "PARTLY", at: 1 }, { id: "b", verdict: "PARTLY", at: 4.5 }] }] }), []); assert.match(validateClaims({ timeline: [{ type: "teaser", id: "t", seconds: 5, claim: { id: "a", verdict: "PARTLY", at: 5.5 } }] })[0], /timeline\[0\] \(t\)\.claim\.at 5\.5 is outside the card's 0–5 \(seconds from the card's start\)/); assert.match(validateClaims({ timeline: [{ type: "teaser", id: "t", claim: { id: "a", verdict: "PARTLY", at: 21 } }] })[0], /outside the card's 0–20/); }); test("validateClaims: two stamps on one entry may not overlap -- the first's hold (else stamp.seconds) before the next lands", () => { const two = (a, b, extra = {}, render = RENDER) => ({ render, timeline: [clip("c1", 100, 140, { claims: [{ id: "a", verdict: "PARTLY", at: a, ...extra }, { id: "b", verdict: "PARTLY", at: b }] })], }); assert.deepEqual(validateClaims(two(110, 113)), [], "3 s apart: the default 3 s hold has passed"); const [msg] = validateClaims(two(110, 111.5)); assert.match(msg, /timeline\[0\] \(c1\)\.claims: the marks a \(at 110\) and b \(at 111\.5\) overlap -- a's stamp stays 3 s; land b at 113 or later, or shorten a's hold/); assert.deepEqual(validateClaims(two(110, 111.5, { hold: 1.5 })), [], "a shorter hold"); assert.deepEqual(validateClaims(two(110, 111.5, { stamp: false })), [], "a mark that draws nothing overlaps nothing"); // The order written does not matter: they are taken in landing order. assert.equal(validateClaims(two(111.5, 110)).length, 1); // stamp.seconds is the cut's. const long = { ...RENDER, chrome: { ...CHROME, factcheck: { stamp: { seconds: 5 } } } }; assert.equal(validateClaims(two(110, 113, {}, long)).length, 1); // A mark that stamps on a later entry (its id carried on) is not drawn here. const carried = { render: RENDER, timeline: [ clip("c1", 100, 140, { claims: [{ id: "a", verdict: "PARTLY", at: 110 }, { id: "b", verdict: "PARTLY", at: 111 }] }), clip("c2", 200, 240, { claim: { id: "a", verdict: "PARTLY" } }), ], }; assert.deepEqual(validateClaims(carried), []); }); // ---- the schedule ----------------------------------------------------------------- // Three 10 s segments, D 0.5: starts 0, 9.5, 19; c2's source starts at 200. const SEGS = (marks) => [ { id: "c1", start: 0, duration: 10, claim: null }, { id: "c2", start: 9.5, duration: 10, claim: marks[0], ...(marks.length > 1 ? { claims: marks } : {}), sourceFrom: 200 }, { id: "c3", start: 19, duration: 10, claim: null }, ]; const TOTAL = 29; test("stampSchedule: two marks with `at` on one clip -- each lands at its own second, each steps the tally", () => { const stamps = stampSchedule({ segments: SEGS([{ id: "a", verdict: "CORROBORATED", at: 201 }, { id: "a.2", verdict: "CORROBORATED", at: 205, count: 2 }]), D: 0.5, total: TOTAL, render: RENDER, }); assert.deepEqual(stamps.map((s) => s.claim), ["a", "a.2"]); near(stamps[0].landed, 10.5, "a: 1 s into c2"); near(stamps[1].landed, 14.5, "a.2: 5 s into c2"); near(stamps[0].out[0], stamps[0].at + 3, "a stays stamp.seconds"); assert.ok(stamps[0].out[1] <= stamps[1].at + 1e-9, "a is gone before a.2 comes in"); const steps = tallySteps(stamps); assert.deepEqual(steps.map((s) => [s.verdict, s.count, s.step]), [["CORROBORATED", 1, 1], ["CORROBORATED", 3, 2]]); assert.deepEqual(tallyFinals(stamps).CORROBORATED, 3); }); test("stampSchedule: a stamp whose hold reaches the next leaves early, gone as the next comes in", () => { const [a, b] = stampSchedule({ segments: SEGS([{ id: "a", verdict: "PARTLY", at: 201, hold: 6 }, { id: "b", verdict: "PARTLY", at: 204 }]), D: 0.5, total: TOTAL, render: RENDER, }); near(a.out[1], b.at, "a's leaving ends where b starts"); near(a.out[1] - a.out[0], 0.5, "over the transition's length"); }); test("stampSchedule: two marks without `at` land in order, back to back -- the last as a lone claim would", () => { const [a, b] = stampSchedule({ segments: SEGS([{ id: "a", verdict: "PARTLY" }, { id: "b", verdict: "CONTRADICTED" }]), D: 0.5, total: TOTAL, render: RENDER, }); const [lone] = stampSchedule({ segments: SEGS([{ id: "b", verdict: "CONTRADICTED" }]), D: 0.5, total: TOTAL, render: RENDER }); assert.deepEqual(b, lone, "the last: exactly a lone claim's stamp"); near(a.out[1], b.at, "a gone as b comes in"); near(a.out[0], b.at - 0.5, "a leaves over the transition's length"); near(a.at, a.out[0] - 3, "a stays stamp.seconds"); near(a.landed, a.at + STAMP_MOTION.slam, "a lands a slam after it starts"); assert.ok(a.landed < b.landed); }); test("stampSchedule: a mark id carried on to a later clip stamps once, there", () => { const segments = [ { id: "c1", start: 0, duration: 10, claim: { id: "a", verdict: "PARTLY" }, claims: [{ id: "a", verdict: "PARTLY" }, { id: "b", verdict: "PARTLY" }] }, { id: "c2", start: 9.5, duration: 10, claim: { id: "a", verdict: "PARTLY" } }, ]; const stamps = stampSchedule({ segments, D: 0.5, total: 19.5, render: RENDER }); assert.deepEqual(stamps.map((s) => [s.claim, s.segment]), [["b", "c1"], ["a", "c2"]]); }); test("stamp cues: two stamps on one clip are two stamp elements, in landing order", () => { const stamps = roundStamps(stampSchedule({ segments: SEGS([{ id: "a", verdict: "PARTLY", at: 201 }, { id: "b", verdict: "PARTLY", at: 205 }]), D: 0.5, total: TOTAL, render: RENDER, })); const { init, cues } = stampCues({ factcheck: { stamps } }, RENDER); assert.ok("st0" in init && "st1" in init); assert.deepEqual([...new Set(cues.filter((c) => c.k.startsWith("st")).map((c) => c.why.split(" ")[1]))], ["a", "b"]); }); // ---- byte identity: one claim per clip is what it always was ------------------------- /** stampSchedule as it was before marks (main at lane/clip-marks' base), verbatim. */ function legacyStampSchedule({ segments, D, total, render }) { const seconds = resolveFactcheck(render).stamp.seconds; const last = new Map(); segments.forEach((s, i) => { if (s.claim) last.set(s.claim.id, i); }); return [...last.entries()] .sort((a, b) => a[1] - b[1]) .map(([claim, i]) => { const seg = segments[i]; const c = seg.claim; const next = segments[i + 1]; const out = !next ? [total - 0.3, total] : D > 0 ? [next.start, next.start + D] : [next.start - 0.3, next.start]; const from = seg.start + (i > 0 ? D : 0); const stay = c.hold ?? seconds; const knobs = { ...(c.count !== undefined && c.count !== 1 ? { count: c.count } : {}), ...(c.stamp === false ? { stamp: false } : {}), ...(c.label !== undefined ? { label: c.label } : {}), }; if (c.at !== undefined && Number.isFinite(seg.sourceFrom)) { const want = seg.start + (c.at - seg.sourceFrom); const landed = Math.min(Math.max(seg.start, want), out[0]); const at = Math.max(seg.start, landed - STAMP_MOTION.slam); const leave = Math.min(at + stay, out[0]); const own = leave < out[0] ? [leave, leave + (out[1] - out[0])] : out; return { claim, verdict: c.verdict, segment: seg.id, at, landed, out: own, ...knobs }; } const at = Math.min(Math.max(from, out[0] - stay), out[0]); const landed = Math.min(at + STAMP_MOTION.slam, out[0]); return { claim, verdict: c.verdict, segment: seg.id, at, landed, out, ...knobs }; }); } test("stampSchedule: one claim per clip -- the stamps it always placed, byte for byte (200 random cuts)", () => { let seed = 7; const rand = () => ((seed = (seed * 16807) % 2147483647) / 2147483647); const verdicts = ["CORROBORATED", "PARTLY", "CONTRADICTED"]; for (let n = 0; n < 200; n += 1) { const D = rand() < 0.2 ? 0 : 0.5; const count = 2 + Math.floor(rand() * 8); let t = 0; const segments = []; for (let i = 0; i < count; i += 1) { const duration = 1 + rand() * 20; const sourceFrom = Math.round(rand() * 5000); const claimed = rand() < 0.6; const id = `k${Math.floor(rand() * 5)}`; const claim = claimed ? { id, verdict: verdicts[Number(id.slice(1)) % 3], ...(rand() < 0.5 ? { at: sourceFrom + rand() * duration } : {}), ...(rand() < 0.3 ? { hold: 0.5 + rand() * 5 } : {}), ...(rand() < 0.2 ? { count: 2 } : {}), ...(rand() < 0.2 ? { stamp: false } : {}), ...(rand() < 0.2 ? { label: "X" } : {}), } : null; segments.push({ id: `c${i}`, start: t, duration, claim, sourceFrom }); t += duration - (i < count - 1 ? D : 0); } const args = { segments, D, total: t, render: RENDER }; assert.equal(JSON.stringify(roundStamps(stampSchedule(args))), JSON.stringify(roundStamps(legacyStampSchedule(args))), `cut ${n}`); } }); test("deckSchedule: `claim` and `claims: [claim]` are the same schedule; a lone claim writes no `claims`", () => { const c = { id: "k", verdict: "PARTLY", at: 203.4 }; const entries = (e2) => [{ type: "clip", id: "c1", video: "v1", start: 10, end: 20 }, { type: "clip", id: "c2", video: "v2", start: 200, end: 210, ...e2 }]; const one = deckSchedule({ entries: entries({ claim: c }), durs: [10, 10], D: 0.5, render: RENDER, provenance: PROV }); const list = deckSchedule({ entries: entries({ claims: [c] }), durs: [10, 10], D: 0.5, render: RENDER, provenance: PROV }); assert.equal(JSON.stringify(list), JSON.stringify(one)); assert.equal("claims" in one.segments[1], false); }); test("deckSchedule: two marks on a clip -- `claim` the first (the pip's verdict), `claims` both, two stamps, the finals count both", () => { const render = { ...RENDER, chrome: { ...CHROME, deck: { pip: { color: "verdict" } } } }; const marks = [{ id: "s1", verdict: "CORROBORATED", at: 201 }, { id: "g1", verdict: "CONTRADICTED", at: 206 }]; const entries = [ { type: "clip", id: "c1", video: "v1", start: 10, end: 20 }, { type: "clip", id: "c2", video: "v2", start: 200, end: 210, claims: marks }, { type: "clip", id: "c3", video: "v3", start: 300, end: 310, claim: { id: "s2", verdict: "CORROBORATED" } }, ]; const s = deckSchedule({ entries, durs: [10, 10, 10], D: 0.5, render, provenance: PROV, clipClock: new Map([["c2", 200]]) }); assert.deepEqual(s.segments[1].claim, marks[0]); assert.deepEqual(s.segments[1].claims, marks); assert.equal(s.segments[1].sourceFrom, 200); assert.deepEqual(s.factcheck.stamps.map((x) => [x.claim, x.segment]), [["s1", "c2"], ["g1", "c2"], ["s2", "c3"]]); near(s.factcheck.stamps[0].landed, 10.5, "s1"); near(s.factcheck.stamps[1].landed, 15.5, "g1"); assert.deepEqual(tallyFinals(s.factcheck.stamps), { CORROBORATED: 2, PARTLY: 0, CONTRADICTED: 1, NOT_FOUND: 0, UNTESTABLE: 0 }); const { pips } = deckCues(s, render); assert.equal(pips.find((p) => p.id === "c2").verdict, "CORROBORATED", "a multi-mark clip's pip: its first mark's verdict"); });