// The stats region (release 31, lane P3): stats.mjs (settings, which clips // declare it, the schedule in the cut's clock), deck.mjs (validation, the // schedule, the geometry), chrome-stats.mjs (the page), the overlay chain, // and a tiny real build -- two lavfi clips beside the manifest, a stub // renderer -- that verify-build passes, and fails loudly once the frames or // a declared clip are missing. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { overlayPointsOf, resolveStats, statsClips, statsSchedule, statsWanted, validateStats, validateStatsEntries, } from "./stats.mjs"; import { deckSchedule, estimateSchedule, statsGeometry, validateChrome } from "./deck.mjs"; import { panelSpans, statsCues, statsHtml, statsValue } from "./chrome-stats.mjs"; import { buildVideo, chromeOverlayChain, setProgressMode, statsRegion } from "./build-video.mjs"; import { verifyBuild } from "./verify-build.mjs"; import { lintManifest } from "./lint.mjs"; import { MONO_BOLD_FONT_FILE, MONO_FONT_FILE } from "./brand.mjs"; const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const DECK = { engine: "hyperframes", layout: "deck", deck: {} }; const render = (chrome = { ...DECK, stats: {} }, more = {}) => ({ width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE, chrome, ...more }); // A frame scan's overlay, frames-cut's shape: source seconds. const OVERLAY = { overlay: [ { clip: "c1", points: [{ at: 100, name: "SYNTH MAIN", stack: 50000, stackBB: 50, playersLeft: 120, level: 1 }, { at: 106, name: "SYNTH MAIN", stack: 80000, stackBB: 66.7, playersLeft: 120, level: 1 }] }, { clip: "c2", points: [{ at: 400, name: "SYNTH MAIN", stack: 170000, stackBB: 106.3, playersLeft: 45, level: 4 }] }, ], }; const ENTRIES = [ { type: "clip", id: "c1", channel: "synth", video: "v1", start: 100, end: 110 }, { type: "card", id: "k1", seconds: 3, heading: "Between" }, { type: "clip", id: "c2", channel: "synth", video: "v1", start: 400, end: 408 }, ]; test("settings: defaults, validation (unknown keys and bad values refused), the deck accepts `stats`", () => { assert.deepEqual(resolveStats(render()), { position: "bottom-left", fields: ["stack", "stackBB", "playersLeft", "level"], name: true }); assert.deepEqual(resolveStats(render({ ...DECK, stats: { fields: ["level", "stack"], name: false } })).fields, ["stack", "level"]); assert.deepEqual(validateStats({}), []); assert.deepEqual(validateStats({ position: "middle", fields: ["stack", "chips"], name: "yes", colour: 1 }), [ "render.chrome.stats.colour is not a stats setting", 'render.chrome.stats.position must be "top-left", "top-right", "bottom-left", "bottom-right"', "render.chrome.stats.fields must be a list of stack, stackBB, playersLeft, level, each at most once", "render.chrome.stats.name must be true or false", ]); assert.deepEqual(validateChrome({ ...DECK, stats: {} }, render()), []); assert.deepEqual(validateChrome({ ...DECK, stats: { position: "top-right" } }, render()), []); assert.ok(validateChrome({ ...DECK, stats: [] }, render()).includes("render.chrome.stats must be an object")); }); test("which clips declare it: the cut's setting, a clip's own true or false, and points to draw", () => { const r = render(); assert.equal(statsWanted(ENTRIES[0], r), true); assert.equal(statsWanted(ENTRIES[1], r), false); assert.equal(statsWanted({ ...ENTRIES[0], stats: false }, r), false); assert.equal(statsWanted({ ...ENTRIES[0], stats: true }, render(DECK)), true); assert.equal(statsWanted(ENTRIES[0], render(DECK)), false); const m = { render: r, timeline: [...ENTRIES, { type: "clip", id: "c3", video: "v1", start: 0, end: 2 }], frameScan: OVERLAY }; assert.deepEqual(statsClips(m), ["c1", "c2"]); assert.deepEqual(overlayPointsOf(m, "c2").map((p) => p.at), [400]); assert.deepEqual(validateStatsEntries(m), []); // A stats key: a boolean, on a clip, true only under the deck and with points. const bad = { render: render({ engine: "hyperframes", layout: "deck" }), frameScan: OVERLAY, timeline: [{ ...ENTRIES[0], stats: "on" }, { ...ENTRIES[1], stats: true }, { type: "clip", id: "c9", video: "v1", start: 0, end: 1, stats: true }], }; assert.deepEqual(validateStatsEntries(bad), [ "timeline[0] (c1).stats must be true or false", "timeline[1] (k1).stats: only a clip carries the stats", "timeline[2] (c9).stats: frameScan.overlay has no points for c9", ]); assert.deepEqual(validateStatsEntries({ render: { chrome: undefined }, timeline: [{ ...ENTRIES[0], stats: true }], frameScan: OVERLAY }), [ "timeline[0] (c1).stats: the stats are drawn by the deck, and render.chrome is not the deck", ]); }); test("the lint says the stats keys a build would refuse", () => { const m = { slug: "s", title: "S", render: render({ ...DECK }), frameScan: OVERLAY, timeline: [{ ...ENTRIES[0], stats: "on" }] }; const lint = lintManifest(m, { exists: () => true }); assert.ok(lint.errors.some((e) => /c1\)\.stats must be true or false/.test(e)), JSON.stringify(lint.errors)); }); test("statsSchedule: source seconds into the cut's clock, held between points, up from mid-dissolve to mid-dissolve", () => { const segs = [ { id: "c1", start: 0, duration: 10, sourceFrom: 100, points: OVERLAY.overlay[0].points }, { id: "k1", start: 9.5, duration: 3 }, { id: "c2", start: 12, duration: 8, sourceFrom: 400, points: OVERLAY.overlay[1].points }, ]; const s = statsSchedule({ segments: segs, D: 0.5, total: 20, render: render() }); assert.equal(s.position, "bottom-left"); assert.deepEqual(s.clips.map((c) => [c.segment, c.in, c.out, c.name]), [["c1", 0, 9.75, "SYNTH MAIN"], ["c2", 12.25, 20, "SYNTH MAIN"]]); assert.deepEqual(s.clips[0].states.map((x) => [x.at, x.values.stack, x.values.stackBB]), [[0, 50000, 50], [6, 80000, 66.7]]); assert.deepEqual(s.clips[1].states.map((x) => [x.at, x.values.playersLeft]), [[12.25, 45]]); // A clip whose picture starts after a point (a cut inside the extent) opens on the value held there. const late = statsSchedule({ segments: [{ id: "c1", start: 0, duration: 4, sourceFrom: 107, points: OVERLAY.overlay[0].points }], D: 0, total: 4, render: render() }); assert.deepEqual(late.clips[0].states, [{ at: 0, values: { stack: 80000, stackBB: 66.7, playersLeft: 120, level: 1 } }]); // Nothing declared: no stats in the schedule. assert.equal(statsSchedule({ segments: [{ id: "c1", start: 0, duration: 4 }], D: 0, total: 4, render: render() }), null); }); test("deckSchedule / estimateSchedule carry `stats` only when a clip declares it", () => { const est = estimateSchedule({ render: render(), timeline: ENTRIES, frameScan: OVERLAY }); assert.deepEqual(est.stats.clips.map((c) => c.segment), ["c1", "c2"]); // The c1 point at 106 lands 6 s into the cut (no lead-in: no cutStart). assert.deepEqual(est.stats.clips[0].states.map((x) => x.at), [0, 6]); const off = estimateSchedule({ render: render(DECK), timeline: ENTRIES, frameScan: OVERLAY }); assert.equal("stats" in off, false); const one = deckSchedule({ entries: [ENTRIES[0], { ...ENTRIES[2], stats: false }], durs: [10, 8], D: 0, render: render(), frameScan: OVERLAY }); assert.deepEqual(one.stats.clips.map((c) => c.segment), ["c1"]); }); test("the geometry: a panel in the footage box's corner, scaled with the frame", () => { assert.deepEqual(statsGeometry(render()), { x: 201, y: 710, width: 648, height: 150 }); assert.deepEqual(statsGeometry(render({ ...DECK, stats: { position: "top-right", name: false, fields: ["stack", "playersLeft"] } })), { x: 1371, y: 30, width: 348, height: 112 }); const small = statsGeometry(render(undefined, { width: 1280, height: 720 })); assert.equal(small.width, 432); assert.equal(small.height, 100); }); test("the page: one block per state, the numbers as the panel shows them, every cue stating its from", () => { assert.equal(statsValue("stack", 41000), "41,000"); assert.equal(statsValue("stack", 323170), "323.2K"); assert.equal(statsValue("stack", 1250000), "1.25M"); assert.equal(statsValue("stackBB", 66.66), "66.7"); assert.equal(statsValue("stackBB", 202.2), "202"); assert.equal(statsValue("playersLeft", null), "—"); assert.deepEqual(panelSpans([{ in: 0, out: 5 }, { in: 5, out: 9 }, { in: 12, out: 14 }]), [{ in: 0, out: 9 }, { in: 12, out: 14 }]); const schedule = estimateSchedule({ render: render(), timeline: ENTRIES, frameScan: OVERLAY }); const { init, cues } = statsCues(schedule); assert.deepEqual(Object.keys(init).sort(), ["pn", "s0_0", "s0_1", "s1_0"]); // The first state shows at the clip's in -- here the cut's first instant, so it is up in init, on frame 0 (cue-start.mjs // settleFirstFrame) -- and hides as the second shows. const s00 = cues.filter((c) => c.k === "s0_0"); assert.equal(init.s0_0.autoAlpha, 1); assert.deepEqual(s00.map((c) => [c.at, c.from.autoAlpha, c.to.autoAlpha]), [[6, 1, 0]]); for (const c of cues) assert.ok(Object.keys(c.to).every((p) => p in c.from), JSON.stringify(c)); const html = statsHtml(schedule, render(), { fonts: { regular: "assets/DeckSans.ttf", bold: "assets/DeckSansBold.ttf" } }); assert.match(html, /data-composition-id="stats"/); assert.match(html, /data-width="648" data-height="150"/); assert.match(html, /