// The deck's marker labels laid against crowding: a marker's `align`, and // `deck.markerLayout` -- "stagger" (the default: two rows when one would not // fit) or "truncate" (one row, ellipsized). A row that fits is laid as ever. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { DECK_DEFAULTS, deckLayout, deckMarkers, deckSchedule, MARKER_BAND, MARKER_ROW, markerLabelWidth, markerRowsOf, pipSegments, pipXs, validateChrome, } from "./deck.mjs"; import { deckHtml } from "./chrome-deck.mjs"; const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const CHROME = { engine: "hyperframes", layout: "deck" }; const BASE = { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE }; const renderWith = (deck = {}) => ({ ...BASE, chrome: { ...CHROME, deck } }); const PROV = { siteOrigin: "https://example.test", channelSlug: "chan" }; // 26 clips, evenly pipped, as the Huck story cut. const N = 26; const ENTRIES = Array.from({ length: N }, (_, i) => ({ id: `c${i}`, type: "clip", video: `v${i}`, start: 0, end: 30 })); const DURS = ENTRIES.map(() => 30); // The crowded case: nine grey chapter flags along the cut and two red promise // flags, the last three flags within ~200 px of the track's right end. const CROWD = [ ...[1, 4, 7, 10, 13, 16, 19, 22].map((i, n) => ({ after: `c${i}`, label: `Chapter ${n + 1}: the dog`, color: "muted" })), { after: "c23", label: "“3–5 weeks”: 3 wks on", color: "#e5534b" }, { after: "c24", label: "Chapter 9: the posts", color: "muted" }, { after: "c25", label: "“I'll post”: 8 wks on", color: "#e5534b" }, ]; function lay(render) { const s = deckSchedule({ entries: ENTRIES, durs: DURS, D: 0.5, render, provenance: PROV }); const l = deckLayout(render, { schedule: s }); const pipped = pipSegments(s); const xs = pipXs(pipped.length, l.pipTrack.x0, l.pipTrack.x1, "even"); return { s, l, ms: deckMarkers(s, render, xs, pipped, l) }; } test("align and markerLayout: refused in sentences; the layout defaults to stagger", () => { assert.equal(DECK_DEFAULTS.markerLayout, "stagger"); const errs = validateChrome({ ...CHROME, deck: { markerLayout: "wrap", markers: [{ after: "c1", label: "x", align: "middle" }] } }); assert.ok(errs.includes('render.chrome.deck.markers[0].align must be "auto", "left", "right", "center"'), errs.join("\n")); assert.ok(errs.includes('render.chrome.deck.markerLayout must be "stagger" or "truncate"'), errs.join("\n")); assert.deepEqual(validateChrome({ ...CHROME, deck: { markerLayout: "truncate", markers: [{ after: "c1", label: "x", align: "left" }] } }), []); }); test("markerLabelWidth: at least Fira Sans Bold's measured widths at 18 px, upper-cased", () => { // Measured with fontTools (advances + 0.04 em spacing): 207.5 and 195.7 px. assert.ok(markerLabelWidth("“3–5 weeks”: 3 wks on") >= 208); assert.ok(markerLabelWidth("“I'll post”: 8 wks on") >= 196); assert.ok(markerLabelWidth("“3–5 weeks”: 3 wks on") < 260, "and not wildly over"); assert.equal(markerLabelWidth("ab"), markerLabelWidth("AB")); }); test("a row that fits is laid as it always was: one row, no row keys, the band unchanged", () => { const r = renderWith({ markers: [{ after: "c5", label: "Short" }, { after: "c20", label: "Also short" }] }); const { l, ms } = lay(r); assert.equal(l.markerBand.height, MARKER_BAND + 6); assert.equal(l.markerBand.rows, undefined); for (const m of ms) assert.equal("row" in m, false); assert.equal(markerRowsOf(lay(r).s, r), 1); }); test("crowded: the labels stagger into two rows; none meets another in its row, a stick or the deck's edge", () => { const r = renderWith({ markers: CROWD }); const { s, l, ms } = lay(r); const W = l.width; // The last three flags sit within ~200 px of the track's end. const last3 = [...ms].sort((a, b) => a.x - b.x).slice(-3); assert.ok(last3.every((m) => l.pipTrack.x1 - m.x <= 200), last3.map((m) => m.x).join(", ")); // In one row they would not all fit. const trunc = lay(renderWith({ markers: CROWD, markerLayout: "truncate" })).ms; const fitting = (list) => list.filter((m) => markerLabelWidth(m.label) <= m.box[1] - m.box[0]).length; assert.ok(fitting(trunc) < trunc.length, "one row truncates some"); for (const m of trunc) assert.equal("row" in m, false); // Staggered: two rows. assert.equal(markerRowsOf(s, r), 2); const sorted = [...ms].sort((a, b) => a.x - b.x); assert.ok(sorted.every((m) => m.row === 0 || m.row === 1)); assert.ok(sorted.some((m) => m.row === 1)); for (const row of [0, 1]) { const inRow = sorted.filter((m) => m.row === row); for (let j = 1; j < inRow.length; j += 1) { assert.ok(inRow[j - 1].box[1] < inRow[j].box[0], `row ${row}: ${inRow[j - 1].after} and ${inRow[j].after} meet`); } } // Row 0 keeps clear of the sticks reaching up to row 1. const sticks = sorted.filter((m) => m.row === 1).map((m) => m.x); for (const m of sorted.filter((x) => x.row === 0)) { for (const x of sticks) assert.ok(x < m.box[0] || x > m.box[1], `${m.after}'s label crosses a stick at ${x}`); } for (const m of ms) assert.ok(m.box[0] >= 16 && m.box[1] <= W - 16, `${m.after} ${m.box}`); // More of them fit than in one row -- the last three among them. (Three // ~200 px labels on 140 px of track cannot all fit two rows: the rest are // ellipsized, as in one row.) assert.ok(fitting(ms) > fitting(trunc), `${fitting(ms)} vs ${fitting(trunc)}`); const lastTrunc = [...trunc].sort((a, b) => a.x - b.x).slice(-3); assert.ok(fitting(last3) > fitting(lastTrunc), `last three: ${fitting(last3)} vs ${fitting(lastTrunc)}`); // The band grows by a row; the track and everything under it move down by it. assert.equal(l.markerBand.height, MARKER_BAND + MARKER_ROW + 6); assert.equal(l.markerBand.rows, 2); assert.equal(l.pipTrack.y, 16 + MARKER_BAND + MARKER_ROW); assert.ok(l.qr.y + l.qr.size <= l.height, `qr ${l.qr.y}+${l.qr.size}`); assert.ok(l.qr.y >= l.pipTrack.y + 12); assert.ok(l.text.titleY >= l.pipTrack.y + 14); // The page: row 1's flags are `up`; their labels a row above; neither row reaches the flags' heads. const html = deckHtml(s, r, {}); assert.equal((html.match(/class="mflag up"/g) ?? []).length, ms.filter((m) => m.row === 1).length); const tr = l.pipTrack.y; const lowTop = Number(html.match(/\.mlabel \{ position: absolute; top: (-?\d+)px;/)[1]); const upTop = Number(html.match(/\.mflag\.up \.mlabel \{ top: (-?\d+)px; \}/)[1]); assert.equal(upTop, l.markerBand.y - tr); assert.equal(lowTop, l.markerBand.y + MARKER_ROW - tr); assert.ok(upTop + 20 <= lowTop, "the rows do not overlap"); assert.ok(lowTop + 20 <= -14, "row 0 ends above the flags' heads (14 px over the track)"); assert.ok(upTop >= -tr, "row 1 starts inside the deck"); const stick = html.match(/\.mflag\.up \.mstick \{ top: (-?\d+)px; height: (\d+)px; \}/); assert.equal(Number(stick[1]), upTop + 20, "the tall stick starts under its label"); assert.equal(Number(stick[1]) + Number(stick[2]), 6, "and ends where a flag's stick does"); }); test("truncate keeps one row in the crowd: the band as ever", () => { const r = renderWith({ markers: CROWD, markerLayout: "truncate" }); const { s, l } = lay(r); assert.equal(markerRowsOf(s, r), 1); assert.equal(l.markerBand.height, MARKER_BAND + 6); assert.doesNotMatch(deckHtml(s, r, {}), /mflag up|\.mflag\.up/); }); test("align: left starts the label at its flag, right ends it there, center centres it; text set to that side", () => { const markers = [ { after: "c5", label: "left one", align: "left" }, { after: "c12", label: "right one", align: "right" }, { after: "c18", label: "centred", align: "center" }, ]; const r = renderWith({ markers }); const { s, ms } = lay(r); const [a, b, c] = ms; assert.equal(a.align, "left"); assert.equal(a.box[0], Math.round(a.x)); assert.ok(a.box[1] > a.x + 100); assert.equal(b.box[1], Math.round(b.x)); assert.ok(b.box[0] < b.x - 100); assert.ok(Math.abs((c.box[0] + c.box[1]) / 2 - c.x) <= 1); const html = deckHtml(s, r, {}); assert.match(html, /data-after="c5"[^>]*>.*?