// The claim's own knobs (factcheck.mjs): `at` (a source second the stamp lands // and the count steps), `hold`, `stamp: false`, `count`, `label`; and the // fact-check stamp's `scale`, `motion`, `opacity` and a verdict's `stampLabel`. // Absent, every one leaves the schedule and the pages as they were. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { CLAIM_LIMITS, drawnStamps, normalizeClaim, resolveFactcheck, STAMP_MOTION, stampSchedule, stampsDrawn, tallyCues, tallyFinals, tallySteps, validateClaims, validateFactcheck, } from "./factcheck.mjs"; import { cueTime, deckSchedule, stampGeometry } from "./deck.mjs"; import { deckHtml } from "./chrome-deck.mjs"; import { stampCues, stampHtml } 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}`); const withFc = (factcheck) => ({ ...RENDER, chrome: { ...CHROME, factcheck } }); // ---- the claim ---------------------------------------------------------------- test("normalizeClaim: the knobs ride along when set, and only then; out-of-range refused in sentences", () => { assert.deepEqual(normalizeClaim({ id: "k1", verdict: "PARTLY" }), { id: "k1", verdict: "PARTLY" }); assert.deepEqual( normalizeClaim({ id: "k1", verdict: "PARTLY", at: 12.5, hold: 2, stamp: false, count: 2, label: " 2 SEIZURES " }), { id: "k1", verdict: "PARTLY", at: 12.5, hold: 2, stamp: false, count: 2, label: "2 SEIZURES" }, ); // The defaults written out are the defaults: dropped. assert.deepEqual(normalizeClaim({ id: "k1", verdict: "PARTLY", stamp: true, count: 1 }), { id: "k1", verdict: "PARTLY" }); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", at: -1 }), /claim\.at must be a source second/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", hold: 0.2 }), /claim\.hold must be from 0\.5 to 10/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", hold: 11 }), /claim\.hold/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", stamp: "no" }), /claim\.stamp must be true or false/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", count: 0 }), /claim\.count must be a whole number from 1 to 99/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", count: 1.5 }), /claim\.count/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", count: 100 }), /claim\.count/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", label: " " }), /claim\.label must be words/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", label: "x".repeat(CLAIM_LIMITS.label + 1) }), /at most 20/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", label: "a\nb" }), /one line/); assert.throws(() => normalizeClaim({ id: "k1", verdict: "PARTLY", when: 3 }), /claim\.when is not a claim field/); }); test("validateClaims: `at` must lie in its clip's window (source seconds)", () => { const at = (v) => ({ timeline: [{ type: "clip", id: "c1", start: 100, end: 130, claim: { id: "k", verdict: "PARTLY", at: v } }] }); assert.deepEqual(validateClaims(at(100)), []); assert.deepEqual(validateClaims(at(130)), []); assert.match(validateClaims(at(99.5))[0], /timeline\[0\] \(c1\)\.claim\.at 99\.5 is outside the clip's window 100–130/); assert.equal(validateClaims(at(131)).length, 1); }); // ---- the stamp's timing ------------------------------------------------------- // Three 10 s segments, D 0.5: starts 0, 9.5, 19; c2's source starts at 200. const SEGS = (claim) => [ { id: "c1", start: 0, duration: 10, claim: null }, { id: "c2", start: 9.5, duration: 10, claim, sourceFrom: 200 }, { id: "c3", start: 19, duration: 10, claim: null }, ]; const TOTAL = 29; test("stampSchedule: without knobs, the stamp it always placed", () => { const [s] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY" }), D: 0.5, total: TOTAL, render: RENDER }); assert.deepEqual(Object.keys(s), ["claim", "verdict", "segment", "at", "landed", "out"]); near(s.at, 16, "3 s before the out"); near(s.landed, 16 + STAMP_MOTION.slam, "landed"); assert.deepEqual(s.out, [19, 19.5]); }); test("stampSchedule: `at` lands the stamp at the source second's place in the cut; it stays `hold`, or stamp.seconds", () => { // Source 203 is 3 s into c2, which starts at 9.5: lands at 12.5. const [s] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", at: 203 }), D: 0.5, total: TOTAL, render: RENDER }); near(s.landed, 12.5, "landed"); near(s.at, 12.5 - STAMP_MOTION.slam, "slams in before"); near(s.out[0], s.at + 3, "stays stamp.seconds from its start"); near(s.out[1] - s.out[0], 0.5, "leaves over the transition's length"); // hold: 1.5 const [h] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", at: 203, hold: 1.5 }), D: 0.5, total: TOTAL, render: RENDER }); near(h.out[0], h.at + 1.5, "hold"); // A hold past the segment's out leaves with the transition, as ever. const [late] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", at: 208, hold: 10 }), D: 0.5, total: TOTAL, render: RENDER }); assert.deepEqual(late.out, [19, 19.5]); // Kept inside the segment: before its start lands at its start; past its out, at the out. const [early] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", at: 150 }), D: 0.5, total: TOTAL, render: RENDER }); near(early.landed, 9.5, "clamped to the segment's start"); near(early.at, 9.5, "never before it"); const [past] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", at: 260 }), D: 0.5, total: TOTAL, render: RENDER }); near(past.landed, 19, "clamped to its out"); // hold without `at`: comes in `hold` before the out. const [ho] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", hold: 5 }), D: 0.5, total: TOTAL, render: RENDER }); near(ho.at, 14, "hold before the out"); // No source clock: as if there were no `at`. const segs = SEGS({ id: "k", verdict: "PARTLY", at: 203 }).map(({ sourceFrom: _s, ...s }) => s); const [noClock] = stampSchedule({ segments: segs, D: 0.5, total: TOTAL, render: RENDER }); near(noClock.at, 16, "no clock, default timing"); }); test("stampSchedule: count, stamp:false and label ride on the stamp, and only when set", () => { const [s] = stampSchedule({ segments: SEGS({ id: "k", verdict: "PARTLY", count: 2, stamp: false, label: "2 SEIZURES" }), D: 0.5, total: TOTAL, render: RENDER, }); assert.equal(s.count, 2); assert.equal(s.stamp, false); assert.equal(s.label, "2 SEIZURES"); }); test("deckSchedule: a claim's `at` through the cut record's clock; the segment keeps its sourceFrom; none without `at`", () => { const entries = [ { type: "clip", id: "c1", video: "v1", start: 10, end: 20 }, { type: "clip", id: "c2", video: "v2", start: 200, end: 210, claim: { id: "k", verdict: "PARTLY", at: 203.4 } }, ]; const s = deckSchedule({ entries, durs: [10, 10], D: 0.5, render: RENDER, provenance: PROV, clipClock: new Map([["c2", 200.4]]) }); // 203.4 is 3.0 s past where c2's segment really starts (200.4): 9.5 + 3. near(s.factcheck.stamps[0].landed, 12.5, "landed"); assert.equal(s.segments[1].sourceFrom, 200.4); assert.deepEqual(s.segments[1].claim, { id: "k", verdict: "PARTLY", at: 203.4 }); // Without the clock: the play window's start (200) stands in. const est = deckSchedule({ entries, durs: [10, 10], D: 0.5, render: RENDER, provenance: PROV }); near(est.factcheck.stamps[0].landed, 12.9, "estimated"); // Without `at`, no sourceFrom key at all. const plain = deckSchedule({ entries: entries.map((e) => (e.claim ? { ...e, claim: { id: "k", verdict: "PARTLY" } } : e)), durs: [10, 10], D: 0.5, render: RENDER, provenance: PROV }); assert.equal("sourceFrom" in plain.segments[1], false); }); // ---- the tally ------------------------------------------------------------------ const ST = (claim, verdict, landed, extra = {}) => ({ claim, verdict, segment: claim, at: landed - 0.28, landed, out: [landed + 2, landed + 2.5], ...extra }); test("tallySteps / tallyFinals: a claim's count steps its verdict by n", () => { const stamps = [ST("a", "PARTLY", 5), ST("b", "PARTLY", 10, { count: 2 }), ST("c", "CONTRADICTED", 12, { count: 3 })]; const steps = tallySteps(stamps); assert.deepEqual(steps.map((s) => [s.verdict, s.count, s.step]), [["PARTLY", 1, 1], ["PARTLY", 3, 2], ["CONTRADICTED", 3, 3]]); assert.equal(tallyFinals(stamps).PARTLY, 3); assert.equal(tallyFinals(stamps).CONTRADICTED, 3); }); test("tallyCues: a step of n rolls the number it was on out and the number it reaches in", () => { const stamps = [ST("a", "PARTLY", 5), ST("b", "PARTLY", 10, { count: 2 })]; const { init, events } = tallyCues(stamps, ["PARTLY"]); // Every count up to the final has its element (n2 is passed over, never shown). assert.deepEqual(Object.keys(init).filter((k) => /\.n\d+$/.test(k)), ["ty.PARTLY.n0", "ty.PARTLY.n1", "ty.PARTLY.n2", "ty.PARTLY.n3"]); const at10 = events.filter((e) => e.at === 10 && /\.n\d+$/.test(e.k)).map((e) => [e.k, e.to.autoAlpha]); assert.deepEqual(at10, [["ty.PARTLY.n1", 0], ["ty.PARTLY.n3", 1]]); // The cell lights at its first step whatever that step's size. const first = tallyCues([ST("x", "PARTLY", 4, { count: 2 })], ["PARTLY"]).events.filter((e) => e.k === "ty.PARTLY" && e.to.opacity === 1); assert.equal(first.length, 1); }); test("the deck's tally page carries every number the tally reaches", () => { const entries = [ { type: "clip", id: "c1", video: "v1", start: 10, end: 20, claim: { id: "k1", verdict: "PARTLY", count: 2 } }, { type: "clip", id: "c2", video: "v2", start: 30, end: 40, claim: { id: "k2", verdict: "PARTLY" } }, ]; const s = deckSchedule({ entries, durs: [10, 10], D: 0.5, render: RENDER, provenance: PROV }); const html = deckHtml(s, RENDER, {}); assert.match(html, /data-k="ty\.PARTLY\.n3">3); assert.doesNotMatch(html, /data-k="ty\.PARTLY\.n4"/); }); // ---- drawing ------------------------------------------------------------------ test("stamp:false: counted in the tally, not drawn; a cut whose every claim is hidden draws no stamps", () => { const sched = { total: 30, fps: 30, factcheck: { stamps: [ST("a", "PARTLY", 5, { stamp: false }), ST("b", "PARTLY", 10)] } }; assert.equal(drawnStamps(sched.factcheck.stamps).length, 1); const { cues } = stampCues(sched); assert.ok(cues.every((c) => /st0|fl0/.test(c.k))); assert.ok(cues.some((c) => c.at === cueTime(Math.round(9.72 * 30) / 30, 30) && c.k === "st0"), "the drawn one is stamp 0"); assert.equal(tallySteps(sched.factcheck.stamps).at(-1).count, 2); assert.equal(stampsDrawn(sched, RENDER), true); const none = { ...sched, factcheck: { stamps: [ST("a", "PARTLY", 5, { stamp: false })] } }; assert.equal(stampsDrawn(none, RENDER), false); }); test("the stamp's words: the claim's label, else the verdict's stampLabel, else its label", () => { const sched = { total: 30, fps: 30, factcheck: { stamps: [ST("a", "PARTLY", 5, { label: "2 Seizures" }), ST("b", "PARTLY", 10), ST("c", "CONTRADICTED", 15)] } }; const r = withFc({ verdicts: { PARTLY: { label: "Seizures", stampLabel: "Seizure" } } }); const html = stampHtml(sched, r, {}); const words = [...html.matchAll(/