// The section title (module `section-title`, `deck.sectionTitle`): the // current section's name on one quiet line under the rail. Its timeline // (which title is up when, the crossfade), its settings, its row in the // deck's layout, its frame keys, the module's registration, and that a deck // without it is the deck it always was -- then a still, when the tools are here. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { deckCues, deckHtml, sectionTitleAt, sectionTitleColor, sectionTitleTimeline } from "./chrome-deck.mjs"; import { contrast } from "./chrome-intro.mjs"; import { cueTime, deckLayout, resolveSectionTitle, validateChrome } from "./deck.mjs"; import { frameKeys } from "./frame-keys.mjs"; import { moduleConfigToManifest } from "./modules/config-loader.mjs"; import { moduleById, partsOf } from "./modules/index.mjs"; import { panelState, panelWrite } from "./modules/panel.mjs"; import sectionTitle from "./modules/section-title.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const PALETTE = { bg: "#12100c", fg: "#f6f1e6", muted: "#a2957f", accent: "#c8752a", amber: "#ffc860" }; const CHROME = { engine: "hyperframes", layout: "deck" }; const BASE = { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE }; const MARKERS = [ { after: "c0", label: "Second", color: "#e07a9a" }, { after: "c1", color: "#5fa8d3" }, // unlabelled: nothing for its section { after: "c2", label: "Fourth", color: "#7fdc9a" }, { after: "c3", label: "Fourth", color: "#ffc861" }, // the same words: one run ]; const renderWith = (deck = {}) => ({ ...BASE, chrome: { ...CHROME, deck: { markers: MARKERS, rail: { labels: false }, ...deck } } }); const ON = (st = { first: "First" }, extra = {}) => renderWith({ sectionTitle: st, ...extra }); const FONTS = { regular: "assets/DeckSans.ttf", bold: "assets/DeckSansBold.ttf" }; /** Five clips and a card between c2 and c3 (the deck slides away over it). */ function schedule() { const seg = (id, type, start, duration, extra = {}) => ({ id, type, start, duration, end: start + duration, title: `Clip ${id}`, subtitle: "Somebody · Oct 1, 2026", qrUrl: null, hideDeck: false, ...extra, }); return { version: 1, kind: "deck", estimated: true, fps: 30, transition: 0.5, total: 52.5, multiChannel: false, segments: [ seg("c0", "clip", 0, 10), seg("c1", "clip", 9.5, 10), seg("c2", "clip", 19, 10), seg("k1", "card", 28.5, 5, { hideDeck: true }), seg("c3", "clip", 33, 10), seg("c4", "clip", 42.5, 10), ], }; } test("the timeline: first, then each marker's label as the marker crosses into its section; unlabelled is nothing; one run for the same words", () => { const s = schedule(); const tl = sectionTitleTimeline(s, ON()); assert.deepEqual(tl.titles.map((t) => t.label), ["First", "Second", "Fourth"]); assert.equal(tl.init, 0); // The handover's middle (c1 starts at 9.5, the dissolve is 0.5); into c3 the deck rises on it: instant. assert.deepEqual(tl.changes, [ { at: 9.75, from: 0, to: 1, instant: false }, { at: 19.25, from: 1, to: null, instant: false }, { at: 33, from: null, to: 2, instant: true }, ]); const at = (t) => sectionTitleAt(tl, t); assert.equal(at(0), "First"); assert.equal(at(9.7), "First"); assert.equal(at(9.75), "Second"); assert.equal(at(15), "Second"); assert.equal(at(25), "", "an unlabelled marker's section shows nothing"); assert.equal(at(30), ""); assert.equal(at(35), "Fourth"); assert.equal(at(50), "Fourth", "the same words after the next marker: no change"); // No `first`: the first section shows nothing. const bare = sectionTitleTimeline(s, ON(true)); assert.equal(bare.init, null); assert.equal(sectionTitleAt(bare, 1), ""); assert.equal(sectionTitleAt(bare, 12), "Second"); // Off: no timeline. assert.equal(sectionTitleTimeline(s, renderWith()), null); assert.equal(sectionTitleTimeline(s, renderWith({ sectionTitle: false })), null); }); test("the cues: the old out over the fade's first half, the new in over its second; an instant as the deck rises", () => { const s = schedule(); const { init, cues, sectionTitle: tl } = deckCues(s, ON()); assert.deepEqual(init.st0, { opacity: 0.85 }); assert.deepEqual(init.st1, { opacity: 0 }); assert.deepEqual(init.st2, { opacity: 0 }); const st = cues.filter((c) => /^st\d+$/.test(c.k)).map(({ k, at, dur, from, to }) => ({ k, at, dur, from: from.opacity, to: to.opacity })); assert.deepEqual(st, [ { k: "st0", at: cueTime(9.55, 30), dur: 0.2, from: 0.85, to: 0 }, { k: "st1", at: cueTime(9.75, 30), dur: 0.2, from: 0, to: 0.85 }, { k: "st1", at: cueTime(19.05, 30), dur: 0.2, from: 0.85, to: 0 }, { k: "st2", at: 33, dur: 0.001, from: 0, to: 0.85 }, ]); assert.equal(tl.titles.length, 3); // fade and opacity are the settings'; a fade of 0 is a cut. const cut = deckCues(s, ON({ first: "First", fade: 0, opacity: 0.6 })).cues.filter((c) => c.k === "st0"); assert.deepEqual(cut.map((c) => [c.at, c.dur, c.to.opacity]), [[9.75, 0.001, 0]]); const slow = deckCues(s, ON({ first: "First", fade: 1 })).cues.find((c) => c.k === "st1"); assert.deepEqual([slow.at, slow.dur], [9.75, 0.5]); }); test("settings: true, false and the object validate; bad values refused in the build's words", () => { const ok = (deck) => validateChrome({ ...CHROME, deck }, BASE); assert.deepEqual(ok({ sectionTitle: true }), []); assert.deepEqual(ok({ sectionTitle: false }), []); assert.deepEqual(ok({ sectionTitle: { first: "Who she is", size: 20, opacity: 0.7, fade: 0.6, color: "section" } }), []); assert.deepEqual(ok({ sectionTitle: { color: "#9fb3c8" } }), []); assert.deepEqual(ok({ sectionTitle: { color: "fg" } }), []); const bad = (st) => ok({ sectionTitle: st }).join(" | "); assert.match(bad("yes"), /sectionTitle must be true, false or \{ first, size, opacity, fade, color \}/); assert.match(bad({ first: "x".repeat(25) }), /sectionTitle\.first is 25 characters \(at most 24\)/); assert.match(bad({ first: "two\nlines" }), /sectionTitle\.first must be one line/); assert.match(bad({ first: 3 }), /sectionTitle\.first must be a word or two/); assert.match(bad({ size: 40 }), /sectionTitle\.size must be from 12 to 32 pixels/); assert.match(bad({ opacity: 0 }), /sectionTitle\.opacity must be from 0\.2 to 1/); assert.match(bad({ fade: 3 }), /sectionTitle\.fade must be from 0 to 2 seconds/); assert.match(bad({ color: "pink" }), /sectionTitle\.color must be "auto", "section", a #hex colour or one of fg, muted, accent, amber, dim/); assert.match(bad({ label: "x" }), /sectionTitle\.label is not a section title setting \(first, size, opacity, fade, color\)/); // The resolved settings. assert.equal(resolveSectionTitle(renderWith()), null); assert.equal(resolveSectionTitle(renderWith({ sectionTitle: false })), null); assert.deepEqual(resolveSectionTitle(ON(true)), { first: "", size: 18, opacity: 0.85, fade: 0.4, color: "auto" }); assert.deepEqual(resolveSectionTitle(ON({ first: " A ", size: 22 })), { first: "A", size: 22, opacity: 0.85, fade: 0.4, color: "auto" }); }); test("the layout: one row under the track, clear of the marker's ring and the QR's cell; the title moves down only by what it needs", () => { const off = deckLayout(renderWith()); const on = deckLayout(ON()); const row = on.sectionTitle; assert.equal(off.sectionTitle, undefined); assert.equal(row.y, on.pipTrack.y + 6 + 6, "the ring (activeSize 12: 6 px) and 6 px of air"); assert.equal(row.height, Math.round(18 * 1.3)); assert.equal(row.x + row.width / 2, 960, "centred"); assert.ok(row.x + row.width <= on.qr.x - 46 - 24, "short of the QR's cell"); assert.equal(on.text.titleY, Math.max(off.text.titleY, row.y + row.height + 2)); // Nothing else moves. assert.deepEqual(on.qr, off.qr); assert.deepEqual(on.pipTrack, off.pipTrack); assert.equal(on.text.x, off.text.x); // A bigger line takes more room; the title follows it. const big = deckLayout(ON({ size: 28 })); assert.ok(big.text.titleY > on.text.titleY); }); test("the colour: auto is the quietest neutral that reads at the line's opacity; section is the section's, fitted to 4.5:1", () => { const s = schedule(); const ground = "#231e19"; const tl = sectionTitleTimeline(s, ON()); const auto = sectionTitleColor(tl.titles[0], tl, PALETTE, ground); const blend = (c, a) => { const p = (h) => [1, 3, 5].map((i) => Number.parseInt(h.slice(i, i + 2), 16)); const [x, y] = [p(ground), p(c)]; return `#${x.map((v, i) => Math.round(v + (y[i] - v) * a).toString(16).padStart(2, "0")).join("")}`; }; assert.ok(contrast(blend(auto, 0.85), ground) >= 4.5); assert.notEqual(auto, PALETTE.fg, "not the title's own white"); // On a ground the muted grey reads on as it is, it is the muted grey. assert.equal(sectionTitleColor(tl.titles[0], tl, PALETTE, "#000000"), PALETTE.muted); const sec = sectionTitleTimeline(s, ON({ first: "First", color: "section" })); assert.equal(sec.titles[1].color, "#e07a9a"); assert.ok(contrast(sectionTitleColor(sec.titles[1], sec, PALETTE, ground), ground) >= 4.5); assert.equal(sectionTitleColor(tl.titles[0], { ...tl, color: "#123456" }, PALETTE, ground), "#123456"); assert.equal(sectionTitleColor(tl.titles[0], { ...tl, color: "amber" }, PALETTE, ground), PALETTE.amber); }); test("the page: a line per title in the panel under the track, its words escaped; absent or false is the deck it always was, byte for byte", () => { const s = schedule(); const html = deckHtml(s, ON({ first: "&" }), { fonts: FONTS }); const lines = html.match(/
[^<]*<\/div>/g); assert.equal(lines.length, 3); assert.ok(html.includes("><b>&</b>
")); const panel = html.indexOf('data-k="panel"'); const track = html.indexOf('
'); const line = html.indexOf('class="stitle"'); assert.ok(panel < track && track < line && line < html.indexOf('data-seg="c0"'), "in the panel (it slides with it), after the track"); assert.match(html, /\.stitle \{ position: absolute; left: 268px; top: 28px; width: 1384px; height: 23px;/); assert.match(html, /text-overflow: ellipsis;/); // Absent, false: identical pages, cues and layout -- no trace of the line. const absent = renderWith(); const off = renderWith({ sectionTitle: false }); for (const sch of [s, { ...s, segments: s.segments.map((x) => ({ ...x, hideDeck: false })) }]) { const a = deckHtml(sch, absent, { fonts: FONTS }); assert.equal(deckHtml(sch, off, { fonts: FONTS }), a); assert.doesNotMatch(a, /stitle|data-k="st\d/); assert.deepEqual(deckCues(sch, off), deckCues(sch, absent)); assert.equal(deckCues(sch, absent).sectionTitle, undefined); } assert.deepEqual(deckLayout(off), deckLayout(absent)); // Items: each line is one, gated on its own opacity. const items = []; deckHtml(s, ON(), { fonts: FONTS, items }); const st = items.filter((i) => i.key.startsWith("st")); assert.deepEqual(st.map((i) => [i.key, i.gate]), [["st0", ["st0", "opacity"]], ["st1", ["st1", "opacity"]], ["st2", ["st2", "opacity"]]]); }); test("frame keys: the line changes keys only around its boundaries, and a renamed section only where it is up", () => { const s = schedule(); const frames = Math.round(s.total * 30); const keys = (render) => { const items = []; const html = deckHtml(s, render, { fonts: FONTS, items }); return frameKeys({ html, region: "deck", fps: 30, frames, version: "v", items }); }; const off = keys(renderWith()); const on = keys(ON()); assert.equal(on.length, frames); // Where the line's key moves from frame to frame and the deck's does not: // only inside a crossfade (or on the frame after it lands). const fades = [[9.55, 9.95], [19.05, 19.25], [33, 33.001]]; const inFade = (f) => fades.some(([a, b]) => f / 30 >= a - 1 / 30 && f / 30 <= b + 1 / 30); // Outside them a frame repeats the one before exactly when the deck's does // (the crossfades fall in the deck's own handovers, which move every frame). for (let f = 1; f < frames; f += 1) { if (inFade(f)) continue; assert.equal(on[f] === on[f - 1], off[f] === off[f - 1], `frame ${f} (${(f / 30).toFixed(3)} s) moved outside a crossfade`); } // Renaming the second section: the frames it is up on change, and no other. const renamed = keys(ON({ first: "First" }, { markers: MARKERS.map((m) => (m.label === "Second" ? { ...m, label: "Segundo" } : m)) })); const differ = []; for (let f = 0; f < frames; f += 1) if (renamed[f] !== on[f]) differ.push(f / 30); assert.ok(differ.length > 0); assert.ok(differ.every((t) => t >= 9.75 - 1e-6 && t <= 19.25 + 1e-6), `changed at ${differ[0]}..${differ.at(-1)}`); assert.ok(differ.length >= Math.floor((19.05 - 9.95) * 30), "every frame it is up on"); // Renaming the first changes only its own frames. const first = keys(ON({ first: "Opening" })); const d1 = []; for (let f = 0; f < frames; f += 1) if (first[f] !== on[f]) d1.push(f / 30); assert.ok(d1.length && d1.every((t) => t < 9.75 + 1e-6)); }); test("the module: a part of the deck, registered, its home and ui; module-config and the panel switch it on", () => { const m = moduleById("section-title"); assert.equal(m, sectionTitle); assert.equal(m.kind, "part"); assert.equal(m.host, "shell"); assert.ok(partsOf("shell").includes(m)); assert.equal(m.configKey, "render.chrome.deck.sectionTitle"); assert.deepEqual(m.deps, ["rail.sections"]); assert.ok(moduleById("rail").provides.includes("rail.sections")); assert.deepEqual(m.ui.map((u) => u.key), ["first", "size", "opacity", "fade", "color"]); assert.equal(m.resolve(renderWith()), null); assert.deepEqual(m.resolve(ON(true)), resolveSectionTitle(ON(true))); assert.deepEqual(m.validate({ size: 99 }, { where: "render.chrome.deck" }), ["render.chrome.deck.sectionTitle.size must be from 12 to 32 pixels"]); // module-config.json: `modules.section-title`. const manifest = { timeline: [{ id: "c0", type: "clip" }], render: { chrome: { ...CHROME, deck: {} } } }; const { patch, unmapped } = moduleConfigToManifest({ modules: { "section-title": { first: "A very long first section title", size: 20, glow: true } } }, manifest); assert.deepEqual(patch.render.chrome.deck.sectionTitle, { first: "A very long first secti…", size: 20 }); assert.deepEqual(unmapped, [ "modules.section-title.first: shortened to 24 characters", "modules.section-title.glow: not a section title setting (first, size, opacity, fade, color)", ]); assert.equal(moduleConfigToManifest({ modules: { "section-title": true } }, manifest).patch.render.chrome.deck.sectionTitle, true); // The generic panel: off until switched on; on writes {}; false reads as off. const state = (render) => panelState({ render }, { render }).find((x) => x.id === "section-title"); assert.equal(state(manifest.render).enabled, false); assert.equal(state(ON(false)).enabled, false); assert.equal(state(ON()).enabled, true); assert.deepEqual(panelWrite(manifest, "section-title", { enabled: true }).chrome.deck.sectionTitle, {}); assert.deepEqual(panelWrite({ render: ON(false) }, "section-title", { enabled: true, fields: { first: "Who" } }).chrome.deck.sectionTitle, { first: "Who" }); assert.equal(panelWrite({ render: ON() }, "section-title", { enabled: false }).chrome.deck.sectionTitle, undefined); }); const haveTools = ["qrencode", "magick"].every((b) => spawnSync(b, [b === "magick" ? "-version" : "-V"], { stdio: "ignore" }).status === 0); const haveChromium = haveTools && spawnSync(process.env.CHROME ?? "/usr/bin/chromium", ["--version"], { stdio: "ignore" }).status === 0; test("a still: the line's ink is centred in its row under the rail; without it the row is the panel's ground", { skip: !haveChromium }, async () => { const { composeChrome } = await import("./compose-chrome.mjs"); const dir = mkdtempSync(path.join(tmpdir(), "section-title-")); try { const fonts = { fontRegular: path.join(HERE, "fonts", "IBMPlexMono-Regular.ttf"), fontBold: path.join(HERE, "fonts", "IBMPlexMono-Bold.ttf") }; const inkOf = async (render, name) => { const manifestPath = path.join(dir, `${name}.manifest.json`); writeFileSync(manifestPath, JSON.stringify({ slug: "t", title: "t", provenance: {}, render: { ...render, ...fonts }, timeline: [] })); const png = path.join(dir, `${name}.png`); await composeChrome({ manifestPath, region: "deck", schedule: schedule(), still: 14, png, outDir: path.join(dir, `out-${name}`) }); const row = deckLayout(ON()).sectionTitle; const raw = spawnSync("magick", [png, "-crop", `${row.width}x${row.height}+${row.x}+${row.y}`, "+repage", "-colorspace", "gray", "-depth", "8", "gray:-"], { maxBuffer: 1 << 24 }).stdout; const ref = raw[0]; const cols = []; for (let x = 0; x < row.width; x += 1) { for (let y = 0; y < row.height; y += 1) if (raw[y * row.width + x] - ref > 40) { cols.push(x); break; } } return { cols, row }; }; const { cols, row } = await inkOf(ON(), "on"); assert.ok(cols.length > 20, `ink in ${cols.length} columns`); const mid = (Math.min(...cols) + Math.max(...cols)) / 2; assert.ok(Math.abs(mid - row.width / 2) <= 3, `centred: ${mid} vs ${row.width / 2}`); const none = await inkOf(renderWith(), "off"); // Off, the marker (on c1's pip, under the row's left part) may reach in; no words do. assert.ok(none.cols.filter((x) => Math.abs(x - row.width / 2) < 120).length === 0, "no words in the middle"); } finally { rmSync(dir, { recursive: true, force: true }); } });