// 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, /
SYNTH MAIN<\/div>/); assert.match(html, /data-k="s0_1"[^>]*>.*
80,000<\/div>.*
66\.7<\/div>/s); assert.match(html, /
Left<\/div>
45<\/div>/); // The palette's roles, never a colour of its own for the ground or the text. assert.match(html, /background: rgba\(18, 16, 26, 0\.84\); border-left: 5px solid #a97bff/); assert.throws(() => statsHtml({ ...schedule, stats: undefined }, render()), /draws no clip's stats/); }); test("the overlay chain lays the stats like the stamps: reinit off, rgba, shortest", () => { const r = render(); const region = statsRegion(r, "/x/chrome/stats-frames", { layout: undefined }); assert.deepEqual({ ...region, frames: undefined }, { name: "stats", frames: undefined, x: 201, y: 710, width: 648, height: 150 }); const chain = chromeOverlayChain(r, [region], "[0:v]", 1, {}); assert.deepEqual(chain.inputs.slice(0, 2), ["-reinit_filter", "0"]); assert.match(chain.chain, /\[1:v\]format=rgba\[hfa0\];\[0:v\]\[hfa0\]overlay=x=201:y=710:format=yuv444:shortest=1/); }); const have = ["ffmpeg", "ffprobe", "magick"].every((b) => spawnSync(b, ["-version"]).status === 0); test("a deck build draws the stats region, verify-build holds it; a missing sequence or clip fails loudly", { skip: !have && "needs ffmpeg, ffprobe and magick" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-stats-build-")); const env = process.env.HYPERFRAMES_BIN; t.after(() => { if (env === undefined) delete process.env.HYPERFRAMES_BIN; else process.env.HYPERFRAMES_BIN = env; rmSync(root, { recursive: true, force: true }); }); // A renderer that writes the frames the composition declares, as real PNGs, // and logs each project it renders. const stub = path.join(root, "hf-stub.mjs"); writeFileSync(stub, `#!/usr/bin/env node import { spawnSync } from "node:child_process"; import { appendFileSync, copyFileSync, mkdirSync, readFileSync } from "node:fs"; const a = process.argv.slice(2); if (a.includes("--version")) { process.stdout.write("0.0.0-test\\n"); process.exit(0); } const out = a[a.indexOf("--output") + 1]; const fps = Number(a[a.indexOf("--fps") + 1]); const html = readFileSync(a.at(-1) + "/index.html", "utf8"); appendFileSync(${JSON.stringify(path.join(root, "renders.log"))}, a.at(-1) + "\\n"); const dur = Number(/data-composition-id="[^"]*"[^>]*data-duration="([\\d.]+)"/.exec(html)[1]); const w = Number((/data-width="(\\d+)"/.exec(html) ?? [0, 640])[1]); const h = Number((/data-height="(\\d+)"/.exec(html) ?? [0, 360])[1]); mkdirSync(out, { recursive: true }); const one = out + "/.one.png"; spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=0x112233@0.5:size=" + w + "x" + h, "-frames:v", "1", "-pix_fmt", "rgba", one]); for (let i = 1; i <= Math.round(dur * fps); i += 1) copyFileSync(one, out + "/frame_" + String(i).padStart(6, "0") + ".png"); `); chmodSync(stub, 0o755); process.env.HYPERFRAMES_BIN = stub; mkdirSync(path.join(root, "media"), { recursive: true }); for (const name of ["a", "b"]) { const r = spawnSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=10:duration=3", "-f", "lavfi", "-i", "sine=f=330:r=48000:d=3", "-shortest", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", path.join(root, "media", `${name}.mp4`), ]); assert.equal(r.status, 0, String(r.stderr)); } const manifestPath = path.join(root, "video.manifest.json"); const manifest = { slug: "stats", title: "Stats", render: { width: 1920, height: 1080, fps: 10, transition: 0, preset: "ultrafast", audioRate: 48000, audioChannels: 2, palette: PALETTE, fontRegular: MONO_FONT_FILE, fontBold: MONO_BOLD_FONT_FILE, chrome: { engine: "hyperframes", layout: "deck", deck: { qr: { show: false } }, stats: {} }, }, timeline: [ { id: "c1", type: "clip", src: "media/a.mp4", start: 0, end: 3 }, { id: "c2", type: "clip", src: "media/b.mp4", start: 0, end: 3 }, ], frameScan: { overlay: [ { clip: "c1", points: [{ at: 0, name: "SYNTH MAIN", stack: 50000, stackBB: 50, playersLeft: 120, level: 1 }, { at: 1.5, name: "SYNTH MAIN", stack: 80000, stackBB: 66.7, playersLeft: 120, level: 1 }] }, { clip: "c2", points: [{ at: 0, name: "SYNTH MAIN", stack: 170000, stackBB: 106.3, playersLeft: 45, level: 4 }] }, ], }, }; writeFileSync(manifestPath, JSON.stringify(manifest)); const v = path.join(root, "out", "sourced"); setProgressMode(() => {}); try { await buildVideo({ manifestPath, opts: { noNetwork: true } }); } finally { setProgressMode("human"); } const schedule = JSON.parse(readFileSync(path.join(v, "schedule.json"), "utf8")); assert.deepEqual(schedule.stats.clips.map((c) => c.segment), ["c1", "c2"]); assert.match(readFileSync(path.join(root, "renders.log"), "utf8"), /chrome\/(work-capture-)?stats\n/); assert.match(readFileSync(path.join(v, "chrome", "stats", "index.html"), "utf8"), /
80,000<\/div>/); const ok = await verifyBuild(manifestPath, {}); assert.deepEqual(ok.problems, []); assert.equal(ok.deck.stats.frames, ok.deck.expectedFrames); assert.equal(ok.deck.stats.declared, 2); // The frames gone (pruned with no record of them): loud. const prunedPath = path.join(v, "chrome", "pruned.json"); const pruned = JSON.parse(readFileSync(prunedPath, "utf8")); delete pruned.dirs["stats-frames"]; writeFileSync(prunedPath, JSON.stringify(pruned)); const noFrames = await verifyBuild(manifestPath, {}); assert.equal(noFrames.ok, false); assert.ok(noFrames.problems.some((p) => /stats region is declared but .*stats-frames has no frames/.test(p)), JSON.stringify(noFrames.problems)); // A clip that declares the stats but is not in the schedule's: loud. const sched2 = { ...schedule, stats: { ...schedule.stats, clips: schedule.stats.clips.filter((c) => c.segment !== "c2") } }; writeFileSync(path.join(v, "schedule.json"), JSON.stringify(sched2)); const missing = await verifyBuild(manifestPath, {}); assert.ok(missing.problems.some((p) => /^c2 declare the stats region but .* draws none for them/.test(p)), JSON.stringify(missing.problems)); });