// The deck's finale (finale.mjs, chrome-finale.mjs): validation of // `render.chrome.deck.finale` and an entry's `layout`, the run rule, the // schedule's `finale` (the clip's move, the deck's hide, stamps landed before // it), the legibility floors, the page -- and a cut without a finale written // byte for byte as before. // // Run with: node --test finale.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { cueTime, deckChoreography, deckSchedule, estimateSchedule, validateChrome } from "./deck.mjs"; import { deckHtml } from "./chrome-deck.mjs"; import { finaleCues, finaleHtml, finaleLayout } from "./chrome-finale.mjs"; import { FINALE_DEFAULTS, FINALE_MIN, FINALE_MOTION, finaleGeometry, finaleRunProblems, railLabelLayout, validateFinaleEntries, validateFinaleSettings, } from "./finale.mjs"; import { validatorErrors } from "./lint.mjs"; import { finaleRegions } from "./build-video.mjs"; const PALETTE = { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" }; const CHROME = { engine: "hyperframes", layout: "deck" }; const BASE = { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE }; const PROV = { siteOrigin: "https://example.test", channelSlug: "chan" }; const STATS = [ { label: "2025 (from 26 Feb)", value: "5+", note: "4 incidents" }, { label: "2026 (to 21 Oct)", value: "23+", note: "8 incidents" }, { label: "His “seizure free” 18 months", value: "8+", note: "7 incidents, before the Costco refill" }, ]; const renderWith = (deck = {}, extra = {}) => ({ ...BASE, chrome: { ...CHROME, deck: { pip: { color: "verdict" }, ...deck }, factcheck: { verdicts: { CORROBORATED: { label: "Seizure episodes", color: "#6fa8dc" }, CONTRADICTED: { label: "Grift", color: "#e3b23c" } } }, ...extra, }, }); // 26 clips, every other one claimed, the last marked for the finale. const N = 26; const clips = (finale = [N - 1]) => Array.from({ length: N }, (_, i) => ({ id: `c${i}`, type: "clip", video: `v${i}`, start: 100, end: 130, onscreen: { title: `${(i % 28) + 1} Oct 2026` }, ...(i % 2 ? { claim: { id: `k${i}`, verdict: i % 4 === 1 ? "CORROBORATED" : "CONTRADICTED" } } : {}), ...(finale.includes(i) ? { layout: "finale" } : {}), })); const DURS = Array.from({ length: N }, () => 30); const sched = (entries, render, D = 0.5) => deckSchedule({ entries, durs: entries.map(() => 30), D, render, provenance: PROV }); test("deck.finale: every key validated, unknown keys refused, the defaults", () => { assert.deepEqual(validateFinaleSettings(undefined), []); assert.deepEqual(validateFinaleSettings({ video: "corner", motion: "cut", totals: false, railLabels: "nodes", statsTitle: "Seizures he reported", stats: STATS }), []); const errs = validateFinaleSettings({ video: "left", motion: "slide", totals: "yes", railLabels: "some", statsTitle: "x".repeat(41), colour: 1, stats: [{ label: "", value: "1" }, { label: "ok", value: "12345678901234567" }, { label: "ok", value: "1", note: "n".repeat(61), color: "red" }, { label: "ok", value: "1", size: 2 }], }); for (const want of [ 'render.chrome.deck.finale.video must be "side", "corner", "hidden"', 'render.chrome.deck.finale.motion must be "grow", "cut"', 'render.chrome.deck.finale.railLabels must be "all", "nodes", "sections", "none"', "render.chrome.deck.finale.totals must be true or false", "render.chrome.deck.finale.statsTitle is 41 characters (at most 40)", "render.chrome.deck.finale.stats[0].label must not be empty", "render.chrome.deck.finale.stats[1].value is 17 characters (at most 16)", "render.chrome.deck.finale.stats[2].note is 61 characters (at most 60)", "render.chrome.deck.finale.stats[2].color must be a #hex colour or one of fg, muted, accent, amber", "render.chrome.deck.finale.stats[3].size is not a stat field (label, value, note, color)", ]) assert.ok(errs.includes(want), `missing: ${want}\n${errs.join("\n")}`); assert.ok(errs.some((e) => e.startsWith("render.chrome.deck.finale.colour is not a finale setting"))); assert.ok(validateFinaleSettings({ stats: Array.from({ length: 9 }, () => ({ label: "a", value: "1" })) }).includes("render.chrome.deck.finale.stats has 9 (at most 8)")); assert.deepEqual(FINALE_DEFAULTS.video, "side"); assert.deepEqual(FINALE_DEFAULTS.motion, "grow"); assert.deepEqual(FINALE_DEFAULTS.railLabels, "all"); // Through validateChrome (the writer's and the build's door). assert.deepEqual(validateChrome({ ...CHROME, deck: { finale: { stats: STATS, statsTitle: "Seizures he reported" } } }), []); assert.ok(validateChrome({ ...CHROME, deck: { finale: { video: "pip" } } }).includes('render.chrome.deck.finale.video must be "side", "corner", "hidden"')); }); test("layout: clip or finale, finale on a clip; one run, a clip before it, none after", () => { assert.deepEqual(validateFinaleEntries({ timeline: clips() }), []); assert.deepEqual(validateFinaleEntries({ timeline: clips([24, 25]) }), []); assert.deepEqual(validateFinaleEntries({ timeline: [...clips([25]), { id: "end", type: "card" }] }), [], "a card may follow"); const bad = validateFinaleEntries({ timeline: [{ id: "a", type: "clip", layout: "wide" }, { id: "b", type: "card", layout: "finale" }] }); assert.ok(bad.includes('timeline[0] (a).layout must be "clip" or "finale"'), bad.join("\n")); assert.ok(bad.includes('timeline[1] (b).layout: "finale" is a clip\'s layout, not a card\'s'), bad.join("\n")); assert.ok(finaleRunProblems(clips([23, 25])).some((e) => e.includes("consecutive") && e.includes("c24"))); assert.ok(finaleRunProblems(clips([24])).some((e) => e.startsWith("no clip may follow the finale") && e.includes("c25"))); assert.ok(finaleRunProblems(clips([0])).some((e) => e.includes("needs a clip before it"))); // The lint names them, as the build refuses them. assert.ok(validatorErrors({ render: renderWith(), timeline: clips([24]) }).some((e) => e.includes("no clip may follow"))); assert.ok(validatorErrors({ render: renderWith({ finale: { video: "pip" } }), timeline: clips() }) .includes('render.chrome.deck.finale.video must be "side", "corner", "hidden"'), "the lint validates deck.finale"); assert.deepEqual(validatorErrors({ render: renderWith({ finale: { stats: STATS } }), timeline: clips() }), []); }); test("legibility at 1080p: rail labels ≥ 24 px, totals ≥ 96 px, pips ≥ 2× the footer's", () => { for (const video of ["side", "corner", "hidden"]) { const g = finaleGeometry(renderWith({ finale: { video, stats: STATS } }), { verdicts: 2, stats: 3, statsTitle: true }); assert.ok(g.rail.labelSize >= FINALE_MIN.railLabel && g.rail.dateSize >= FINALE_MIN.railLabel && g.rail.markerSize >= FINALE_MIN.railLabel, video); assert.ok(g.totals.numberSize >= FINALE_MIN.totals, `${video}: ${g.totals.numberSize}`); assert.ok(g.rail.x1 - g.rail.x0 >= 1600, "the rail runs the frame's width"); // Everything inside the frame and below the rail. for (const box of [g.totals, g.stats, g.video, { ...g.qr, width: g.qr.size, height: g.qr.size }].filter(Boolean)) { assert.ok(box.x >= 0 && box.y >= g.rail.bottom && box.x + box.width <= 1920 && box.y + box.height <= 1080, `${video}: ${JSON.stringify(box)}`); } } const side = finaleGeometry(renderWith({ finale: { video: "side" } })); assert.deepEqual(side.video, { x: 1104, y: 448, width: 768, height: 432 }, "40 % of the width"); assert.equal(finaleGeometry(renderWith({ finale: { video: "corner" } })).video.width, 538, "28 %"); assert.equal(finaleGeometry(renderWith({ finale: { video: "hidden" } })).video, null); // Five verdicts still get ≥ 96 px numbers. assert.ok(finaleGeometry(renderWith({ finale: {} }), { verdicts: 5, stats: 8 }).totals.numberSize >= 96); const r = renderWith({ finale: { stats: STATS } }); const s = sched(clips(), r); const L = finaleLayout(s, r); for (const p of L.pips) assert.ok(p.size >= FINALE_MIN.pipScale * p.foot.size, `${p.id}: ${p.size} vs ${p.foot.size}`); assert.equal(L.pips.length, N, "every clip has its pip, the finale's too"); assert.equal(L.pips.filter((p) => p.here).length, 1); }); test("rail labels: shared words merge, crowded labels turn, every label stays on the frame", () => { const g = finaleGeometry(renderWith({ finale: {} })); const few = railLabelLayout([{ x: 300, label: "Arrives" }, { x: 900, label: "First seizure" }, { x: 1500, label: "ER" }], g); assert.equal(few.mode, "row"); const crowd = railLabelLayout(Array.from({ length: 26 }, (_, i) => ({ x: 160 + i * 68, label: `${i} Oct 2026` })), g); assert.equal(crowd.mode, "turned"); assert.ok(crowd.angle >= 35 && crowd.angle <= 65); const merged = railLabelLayout([{ x: 200, label: "24 Aug" }, { x: 260, label: "24 Aug" }, { x: 900, label: "25 Aug" }], g); assert.equal(merged.groups.length, 2); assert.equal(merged.groups[0].x, 230); // A date the label already says is not said twice. assert.equal(railLabelLayout([{ x: 500, label: "Oct 21, 2026", date: "Oct 21, 2026" }], g).groups[0].date, ""); assert.equal(railLabelLayout([{ x: 500, label: "26 Aug 2026", date: "Aug 26, 2026" }], g).groups[0].date, "", "in another spelling"); assert.equal(railLabelLayout([{ x: 500, label: "26 Aug 2026", date: "2026-08-26" }], g).groups[0].date, ""); assert.equal(railLabelLayout([{ x: 500, label: "The ER", date: "Aug 24, 2026" }], g).groups[0].date, "Aug 24, 2026"); assert.equal(railLabelLayout([{ x: 500, label: "24 Aug 2026", date: "Aug 26, 2026" }], g).groups[0].date, "Aug 26, 2026", "a different day is said"); }); test("the schedule: the run, the clip's move, the deck's hide, stamps landed before it", () => { const r = renderWith({ finale: { stats: STATS } }); const s = sched(clips([24, 25]), r); const f = s.finale; const first = s.segments[24]; assert.deepEqual(f.segments, ["c24", "c25"]); assert.equal(f.start, first.start); assert.ok(Math.abs(f.at - (first.start + Math.max(0.5, 0.25 + 0.08 + 0.45))) < 1e-3, "grow waits for the dissolve and the words"); assert.equal(f.hide, f.at); assert.ok(Math.abs(f.cover - (f.at + FINALE_MOTION.cover)) < 1e-3); assert.equal(f.to, s.total); assert.ok(s.segments[24].finale && s.segments[25].finale && !s.segments[23].finale); // Each finale clip's footage moves into the box: the first grows, the next is there. const moves = s.moves.filter((m) => m.segment === "c24" || m.segment === "c25"); assert.equal(moves[0].seconds, FINALE_MOTION.grow); assert.ok(Math.abs(moves[0].segmentAt - (f.at - first.start)) < 1e-3); assert.deepEqual([moves[1].seconds, moves[1].segmentAt], [0, 0]); assert.deepEqual(moves[0].to, f.box); // c25's claim would stamp inside the finale: counted at its start, not drawn. const k25 = s.factcheck.stamps.find((x) => x.claim === "k25"); assert.deepEqual([k25.landed, k25.stamp, k25.finale], [f.start, false, true]); assert.ok(s.factcheck.stamps.every((x) => x.landed <= f.start)); // The deck hands over into the run, and goes at `hide`. const ch = deckChoreography(s, r); assert.ok(ch.handovers.some((h) => h.to === "c24")); assert.ok(!ch.handovers.some((h) => h.to === "c25")); assert.deepEqual(ch.visibility.find((v) => v.i === 24), { i: 24, hide: true, at: [f.hide, f.hide] }); // "cut": the page dissolves in over the run's first dissolve; the deck goes once covered. const c = sched(clips(), renderWith({ finale: { motion: "cut", video: "hidden" } })).finale; assert.deepEqual([c.at, c.grow, c.hide, c.box], [c.start, 0, c.cover, null]); // A card after the run: the page leaves through its dissolve. const withCard = deckSchedule({ entries: [...clips(), { id: "end", type: "card", seconds: 5 }], durs: [...DURS, 5], D: 0.5, render: r, provenance: PROV }); assert.deepEqual(withCard.finale.out, [withCard.segments[N].start, withCard.segments[N].start + 0.5].map((v) => Math.round(v * 1000) / 1000)); }); test("the page: the large rail, the totals at their final counts, the stats, the clip's caption", () => { const r = renderWith({ finale: { statsTitle: "Seizures he reported", stats: STATS } }); const s = sched(clips(), r); const win = finaleRegions(r, "/tmp/x", s)[0].window; const html = finaleHtml(s, r, win, {}); assert.equal((html.match(/class="pip"/g) ?? []).length, N); assert.equal((html.match(/class="pl[ "]/g) ?? []).length, N, "a label per pip (none shared here)"); assert.equal((html.match(/class="sc"/g) ?? []).length, 3); assert.match(html, /Seizures he reported/); assert.match(html, /
6<\/div>
Grift<\/div>/); assert.match(html, /
7<\/div>
Seizure episodes<\/div>/); assert.match(html, /His “seizure free” 18 months/); assert.match(html, /class="ct">26 Oct 2026 Object.keys(c.to).every((p) => p in c.from))); const hole = cues.find((c) => c.k === "hole"); assert.equal(hole.ease, "smoothstep"); assert.equal(hole.at, cueTime(s.finale.at, 30), "on the cue grid (deck.mjs cueTime)"); assert.equal(hole.dur, FINALE_MOTION.grow); // The region: the whole frame, a window from `at` to the end. const reg = finaleRegions(r, "/tmp/x", s)[0]; assert.deepEqual([reg.x, reg.y, reg.width, reg.height], [0, 0, 1920, 1080]); assert.ok(reg.offset <= s.finale.from && reg.offset > s.finale.from - 1 / 30); }); test("byte identity: no finale entry -- the schedule and the deck page are as they were, finale settings or not", () => { const entries = clips([]); const plain = renderWith(); const withSettings = renderWith({ finale: { stats: STATS, video: "corner" } }); const a = sched(entries, plain); const b = sched(entries, withSettings); assert.equal(JSON.stringify(a), JSON.stringify(b)); assert.equal(a.finale, undefined); assert.ok(a.segments.every((x) => !("finale" in x))); assert.equal(deckHtml(a, plain), deckHtml(b, withSettings)); assert.deepEqual(finaleRegions(plain, "/tmp/x", a), []); // A finale that breaks the run is not drawn (the validators refuse it). assert.equal(sched(clips([24]), withSettings).finale, undefined); // An estimate carries the finale as the build's schedule does. assert.ok(estimateSchedule({ render: withSettings, timeline: clips(), provenance: PROV }).finale); }); test("tallyLabel: the deck's tally cell shows it, the finale's totals keep the full label", () => { const r = renderWith({ finale: { stats: STATS } }); r.chrome.factcheck.verdicts.CONTRADICTED = { label: "Huck's Blend pitches", tallyLabel: "Pitches", color: "#e3b23c" }; assert.deepEqual(validateChrome(r.chrome, r), []); const s = sched(clips(), r); const deck = deckHtml(s, r, {}); assert.match(deck, /
Pitches<\/div>/); assert.doesNotMatch(deck, /class="tlabel">Huck/); const totals = finaleLayout(s, r).totals; assert.deepEqual(totals.map((t) => t.label).sort(), ["Huck's Blend pitches", "Seizure episodes"]); }); test("finale.pips: false -- the finale's clips leave the rail, footer and large; the rest is untouched", () => { assert.equal(FINALE_DEFAULTS.pips, true); assert.ok(validateFinaleSettings({ pips: "no" }).includes("render.chrome.deck.finale.pips must be true or false")); const fin = [22, 23, 24, 25]; const on = sched(clips(fin), renderWith({ finale: {} })); const r = renderWith({ finale: { pips: false } }); const s = sched(clips(fin), r); assert.deepEqual(s.segments.filter((x) => x.noPip).map((x) => x.id), ["c22", "c23", "c24", "c25"]); assert.ok(!on.segments.some((x) => x.noPip), "default: every clip pipped, as before"); // The deck's cues are drawn with 22 pips and no lookup of a missing one. const deck = deckHtml(s, r, {}); assert.equal((deck.match(/data-k="pip\d+"/g) ?? []).length, N - fin.length); // The finale's large rail: the record's pips only; its clips keep their captions. const win = finaleRegions(r, "/tmp/x", s)[0].window; const html = finaleHtml(s, r, win, {}); assert.equal((html.match(/class="pip"/g) ?? []).length, N - fin.length); assert.match(html, /class="ct">26 Oct 2026