// The source status never draws cut (lane source-status, review 1 finding 6): the // page's fit takes the whole words, else the short form, else lets the channel // give way, else draws nothing -- driven here over a stand-in for the DOM that lays // a row out as the deck's CSS does (the title and, given way, the channel shrink in // proportion to their widths, to nothing; the channel holds at most 40 %; every // other part stays whole), and once in chromium when it is there. // // Run with: node --test umtool/report-to-video/status-fit.test.mjs 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 { TITLE_KEEP_EM, fitStatusLine, statusShortForm } from "./status-fit.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const PX = 12; // a character's advance in the stand-in const SEP = 22; const EM = 26; class ClassList { constructor(names = []) { this.set = new Set(names); } contains(n) { return this.set.has(n); } add(n) { this.set.add(n); } remove(n) { this.set.delete(n); } } /** A row of parts `[token, text]` joined by separators, `width` px wide. */ function row(width, parts) { const nodes = []; const line = { classList: new ClassList(["deck-sub"]), clientWidth: width }; for (const [k, text, short] of parts) { if (nodes.length) nodes.push({ classList: new ClassList(["sep"]), style: { display: "" }, textContent: "ยท" }); const attrs = new Map([["data-p", k], ...(short ? [["data-short", short]] : [])]); nodes.push({ classList: new ClassList(["part"]), style: { display: "" }, textContent: text, hasAttribute: (a) => attrs.has(a), getAttribute: (a) => attrs.get(a) ?? null, setAttribute: (a, v) => attrs.set(a, v), }); } nodes.forEach((n, i) => { n.previousElementSibling = nodes[i - 1] ?? null; n.nextElementSibling = nodes[i + 1] ?? null; }); const shown = () => nodes.filter((n) => n.style.display !== "none"); const natural = (n) => (n.classList.contains("sep") ? SEP : n.textContent.length * PX); const keyOf = (n) => (n.getAttribute ? n.getAttribute("data-p") : null); // The CSS's layout: flex-basis is the natural width (a channel's capped at 40 %); // the shrinkable parts give up the overflow in proportion to their basis, to 0. function layout() { const give = line.classList.contains("st-give"); const width = line.clientWidth; const basis = new Map(shown().map((n) => [n, keyOf(n) === "channel" ? Math.min(natural(n), 0.4 * width) : natural(n)])); const shrinks = (n) => keyOf(n) === "title" || (give && keyOf(n) === "channel"); let over = [...basis.values()].reduce((a, b) => a + b, 0) - width; const size = new Map(basis); for (let pass = 0; over > 1e-9 && pass < 10; pass++) { const live = [...size].filter(([n, w]) => shrinks(n) && w > 0); const total = live.reduce((a, [, w]) => a + w, 0); if (!total) break; for (const [n, w] of live) size.set(n, Math.max(0, w - over * (w / total))); over = [...size.values()].reduce((a, b) => a + b, 0) - width; } return size; } Object.defineProperty(line, "scrollWidth", { get: () => Math.max(line.clientWidth, [...layout().values()].reduce((a, b) => a + b, 0)) }); for (const n of nodes) { Object.defineProperty(n, "clientWidth", { get: () => layout().get(n) ?? 0 }); Object.defineProperty(n, "scrollWidth", { get: () => natural(n) }); } line.querySelector = (sel) => { const m = /\[data-p="([^"]+)"\]/.exec(sel); return nodes.find((n) => keyOf(n) === m?.[1]) ?? null; }; const status = nodes.find((n) => keyOf(n) === "status"); return { line, status, sepBefore: status?.previousElementSibling, layout }; } const LONG = "now members-only (public when streamed)"; // 39 characters: 468 px const SHORT = "now members-only"; // 16: 192 px const PARTS = (title = "A synthetic stream title") => [ ["channel", "Synth Channel"], // 156 px ["title", title], ["date", "Mar 1, 2031"], // 132 px ["clock", "35:00"], // 60 px ["status", LONG, SHORT], ]; test("the short form drops the parenthesis, and only that", () => { assert.equal(statusShortForm(LONG), SHORT); assert.equal(statusShortForm("now deleted (was public)"), "now deleted"); assert.equal(statusShortForm("now private"), null); assert.equal(statusShortForm(""), null); }); test("a wide column draws the whole words; the title keeps its room", () => { const r = row(1500, PARTS()); assert.equal(fitStatusLine(r.line, EM, "row", TITLE_KEEP_EM), "long"); assert.equal(r.status.textContent, LONG); }); test("THE NARROWEST COLUMN: the tail is never cut -- the short form, then the channel gives way, then nothing", () => { // Fixed parts: channel 156 + date 132 + clock 60 + 4 separators 88 = 436 px, then the status. // 700 px: the whole status (468) would leave the title nothing; the short (192) fits. const narrow = row(700, PARTS()); assert.equal(fitStatusLine(narrow.line, EM, "row", TITLE_KEEP_EM), "short"); assert.equal(narrow.status.textContent, SHORT); assert.ok(narrow.line.scrollWidth <= 700 + 0.5, "nothing past the column"); // 600 px: even the short one overflows with the channel whole (436 + 192 = 628); the channel gives way. const tighter = row(600, PARTS()); assert.equal(fitStatusLine(tighter.line, EM, "row", TITLE_KEEP_EM), "short-channel"); assert.ok(tighter.line.classList.contains("st-give")); assert.ok(tighter.line.scrollWidth <= 600 + 0.5); // 400 px: the date, the clock and the separators alone take 280 with the short form's 192 past it: nothing. const tightest = row(400, PARTS()); assert.equal(fitStatusLine(tightest.line, EM, "row", TITLE_KEEP_EM), "none"); assert.equal(tightest.status.style.display, "none"); assert.equal(tightest.sepBefore.style.display, "none", "its separator goes with it"); assert.equal(tightest.line.classList.contains("st-give"), false, "the channel is not squeezed for nothing"); }); test("the whole words may squeeze the title, but not below TITLE_KEEP_EM (or all of itself)", () => { // 1000 px: 436 + 468 = 904 leaves the title 96 px of its 288, under 6 em (156): the short form. const squeezed = row(1000, PARTS()); assert.equal(fitStatusLine(squeezed.line, EM, "row", TITLE_KEEP_EM), "short"); // A short title (60 px) kept whole: the whole words. const shortTitle = row(1000, PARTS("Live")); assert.equal(fitStatusLine(shortTitle.line, EM, "row", TITLE_KEEP_EM), "long"); }); test("a second fit starts from the line as drawn (the live preview's)", () => { const r = row(400, PARTS()); assert.equal(fitStatusLine(r.line, EM), "none"); r.line.clientWidth = 1500; // the same line, its column widened assert.equal(fitStatusLine(r.line, EM), "long"); assert.equal(fitStatusLine(r.line, EM), "long", "idempotent"); assert.equal(r.status.style.display, ""); assert.equal(r.status.textContent, LONG); r.line.clientWidth = 700; assert.equal(fitStatusLine(r.line, EM), "short"); }); test("a line with no status is left alone", () => { const r = row(300, PARTS().slice(0, 4)); assert.equal(fitStatusLine(r.line, EM), "absent"); }); test("the finale's caption (a block of two lines): wholly inside, else the short form, else nothing", () => { const make = (fitsLong, fitsShort) => { const attrs = new Map([["data-p", "status"], ["data-short", SHORT]]); const st = { classList: new ClassList(["part"]), style: { display: "" }, textContent: LONG, hasAttribute: (a) => attrs.has(a), getAttribute: (a) => attrs.get(a) ?? null, setAttribute: (a, v) => attrs.set(a, v), previousElementSibling: { classList: new ClassList(["sep"]), style: { display: "" } }, nextElementSibling: null, get offsetWidth() { return this.textContent.length * PX; }, get offsetTop() { return (this.textContent === LONG ? fitsLong : fitsShort) ? 31 : 62; }, offsetHeight: 31, }; const cs = { classList: new ClassList(["cs"]), clientWidth: 640, clientHeight: 62, offsetTop: 0, querySelector: (sel) => (/status/.test(sel) ? st : null) }; return { cs, st }; }; const a = make(true, true); assert.equal(fitStatusLine(a.cs, EM, "block"), "long"); const b = make(false, true); assert.equal(fitStatusLine(b.cs, EM, "block"), "short"); const c = make(false, false); assert.equal(fitStatusLine(c.cs, EM, "block"), "none"); assert.equal(c.st.style.display, "none"); }); // THE REAL PAGE, when chromium is on the machine: a deck whose column a tally and a // long channel narrow, its DOM after the fit. const CHROME = process.env.CHROME ?? "/usr/bin/chromium"; const haveChromium = spawnSync(CHROME, ["--version"], { stdio: "ignore" }).status === 0; test("in chromium: the narrowest column draws a whole status or none, never a cut one", { skip: !haveChromium }, async () => { const { composeChrome } = await import("./compose-chrome.mjs"); const { estimateSchedule } = await import("./deck.mjs"); const dir = mkdtempSync(path.join(tmpdir(), "status-fit-")); try { const face = path.join(HERE, "fonts", "IBMPlexSans[wdth,wght].ttf"); const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const stamp = (now, firstWhen) => ({ now, since: null, asOf: "2031-03-01T00:00:00Z", first: firstWhen ? "public" : null, firstAt: null, firstWhen, from: "corpus", evidence: [] }); const manifest = { slug: "t", title: "Synthetic cut", provenance: { channelSlug: "synth-channel", channel: "A Synthetic Channel With A Long Display Name" }, render: { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE, fontRegular: face, fontBold: face, chrome: { engine: "hyperframes", layout: "deck", deck: { subtitle: { parts: ["channel", "title", "date", "clock"], status: "end" } }, factcheck: { verdicts: { CONTRADICTED: { label: "Synthetic" }, CORROBORATED: { label: "Synthetic too" } } }, }, }, timeline: [ { id: "c01", type: "clip", video: "synthvid001", start: 2090, end: 2110, cite: 2100, onscreen: { title: "The first synthetic moment" }, claim: { id: "k1", verdict: "CONTRADICTED" } }, { id: "c02", type: "clip", video: "synthvid002", start: 2090, end: 2110, cite: 2100, onscreen: { title: "The second" }, claim: { id: "k2", verdict: "CORROBORATED" } }, ], sourceStatus: { refreshedAt: "2031-03-02T00:00:00.000Z", sources: { "synth-channel/synthvid001": stamp("members_only", "live"), "synth-channel/synthvid002": stamp("private", "other") } }, }; const meta = { title: "A synthetic stream title that runs long enough to have to give way on the line", uploadDate: "20310301", channel: manifest.provenance.channel }; const schedule = estimateSchedule(manifest, { metas: [meta, meta] }); const manifestPath = path.join(dir, "video.manifest.json"); writeFileSync(manifestPath, JSON.stringify(manifest)); const { projDir } = await composeChrome({ manifestPath, region: "deck", schedule }); // The test's own probe, after the page's fit: does each line hold what it draws, and // would it have held the whole words (the column is as narrow as the test says)? const page = path.join(projDir, "index.html"); const { readFileSync } = await import("node:fs"); writeFileSync(page, readFileSync(page, "utf8").replace("", ``)); const out = spawnSync(CHROME, [ "--headless", "--disable-gpu", "--no-sandbox", "--hide-scrollbars", "--window-size=1920,1080", "--virtual-time-budget=6000", "--dump-dom", `file://${path.join(projDir, "index.html")}?still=3`, ], { encoding: "utf8", maxBuffer: 1 << 26 }); assert.equal(out.status, 0, out.stderr); assert.match(out.stdout, /data-probed="1"/, "the probe ran after the fit"); const lines = [...out.stdout.matchAll(/
]*)>/g)].map((x) => x[1]).filter((a) => /data-holds/.test(a)); assert.equal(lines.length, 2); for (const a of lines) assert.match(a, /data-holds="true"/, "the line holds what it draws"); assert.ok(lines.some((a) => /data-whole-held="false"/.test(a)), "the column is too narrow for the whole words somewhere"); const spans = [...out.stdout.matchAll(/]*)>([^<]*)<\/span>/g)]; assert.equal(spans.length, 2, "both lines carry a status part"); for (const [, attrs, text] of spans) { const full = /data-full="([^"]*)"/.exec(attrs)?.[1]; assert.ok(full, "the fit ran"); const hidden = /display: none/.test(attrs); // Whole words, its short form, or not drawn: never a prefix of either. assert.ok(hidden || text === full || text === statusShortForm(full), `drawn "${text}" of "${full}"`); } } finally { rmSync(dir, { recursive: true, force: true }); } });