// Tests for the deck-counts lane (plans/deck-counts.md): the stamps off with
// the tally still counting (`factcheck.stamp.show`), no QR on the post cards
// (`deck.posts.qr`), pips in their claim's verdict colour and shape
// (`deck.pip.color: "verdict"`), and flags on the pip track
// (`deck.markers`). Each is optional, and a deck without them is as it was.
//
// Run with: pnpm test:scripts
import assert from "node:assert/strict";
import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import test from "node:test";
import {
DECK_DEFAULTS, deckLayout, deckMarkers, deckSchedule, MARKER_BAND, MARKER_LIMITS, pipSegments, pipXs, postWindows,
resolveDeck, snapWindow, validateChrome, validateDeckMarkers,
} from "./deck.mjs";
import { deckCues, deckHtml, MARKER_POP } from "./chrome-deck.mjs";
import { postsHtml, windowPosts } from "./chrome-posts.mjs";
import { feedHtml, feedLayout } from "./chrome-feed.mjs";
import {
FACTCHECK_DEFAULTS, resolveFactcheck, stampsDrawn, tallyCues, validateFactcheck, VERDICT_SHAPES, VERDICTS,
} from "./factcheck.mjs";
import { validatorErrors } from "./lint.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 = {}, factcheck = undefined) =>
({ ...BASE, chrome: { ...CHROME, deck, ...(factcheck ? { factcheck } : {}) } });
const PROV = { siteOrigin: "https://example.test", channelSlug: "chan" };
// Four clips and a card: c1 PARTLY, c2 CONTRADICTED, c4 CORROBORATED, c3 none.
const ENTRIES = [
{ id: "c1", type: "clip", video: "a", start: 0, end: 10, date: "2024-09-05", claim: { id: "k1", verdict: "PARTLY" } },
{ id: "c2", type: "clip", video: "b", start: 0, end: 20, date: "2024-10-01", claim: { id: "k2", verdict: "CONTRADICTED" } },
{ id: "t1", type: "card", seconds: 4, heading: "Three weeks later" },
{ id: "c3", type: "clip", video: "c", start: 0, end: 6, date: "2025-12-08" },
{ id: "c4", type: "clip", video: "d", start: 0, end: 9, date: "2025-12-09", claim: { id: "k4", verdict: "CORROBORATED" } },
];
const DURS = [10, 20, 4, 6, 9];
const POST = (id, date, extra = {}) => ({
id, platform: "x", author: "Someone", handle: "someone", date, text: `words of ${id}`,
url: `https://x.com/someone/status/${id}`, ...extra,
});
const POSTS = [POST("p1", "2024-10-02", { attachTo: "c2" }), POST("p2", "2024-10-03", { attachTo: "c2" })];
const sched = (render, posts = []) => deckSchedule({ entries: ENTRIES, durs: DURS, D: 0.5, render, provenance: PROV, posts });
const FONTS = { regular: "assets/DeckSans.ttf", bold: "assets/DeckSansBold.ttf" };
// ---- F1: the stamp off, the tally still counting ------------------------------
test("stamp.show: a setting, default on; refused when not a boolean", () => {
assert.equal(FACTCHECK_DEFAULTS.stamp.show, true);
assert.equal(resolveFactcheck(renderWith()).stamp.show, true);
assert.equal(resolveFactcheck(renderWith({}, { stamp: { show: false } })).stamp.show, false);
assert.deepEqual(validateFactcheck({ stamp: { show: false, seconds: 2 } }), []);
assert.match(validateFactcheck({ stamp: { show: "no" } }).join(" "), /stamp\.show must be true or false/);
assert.deepEqual(validateChrome({ ...CHROME, factcheck: { stamp: { show: false } } }, BASE), []);
});
test("stamp.show false: the schedule keeps its stamps, nothing is drawn, and the tally counts at the same seconds", () => {
const on = renderWith();
const off = renderWith({}, { stamp: { show: false } });
const a = sched(on), b = sched(off);
// The schedule is the same: the stamps are when the tally steps.
assert.deepEqual(b, a);
assert.equal(a.factcheck.stamps.length, 3);
assert.equal(stampsDrawn(a, on), true);
assert.equal(stampsDrawn(b, off), false);
assert.equal(stampsDrawn({ segments: [] }, on), false);
// The tally's cues -- each count and its flash -- are exactly the stamps-on ones.
const tally = (r, s) => deckCues(s, r).cues.filter((c) => c.k.startsWith("ty."));
assert.deepEqual(tally(off, b), tally(on, a));
assert.ok(tally(off, b).some((c) => c.k === "ty.CONTRADICTED.flash"), "the flash fires");
const steps = tallyCues(b.factcheck.stamps).events.filter((e) => e.k.endsWith(".n1"));
assert.deepEqual(steps.map((e) => e.at).sort(), b.factcheck.stamps.map((s) => s.landed).sort());
// The deck page itself is identical: the stamp is another region.
assert.equal(deckHtml(b, off, { fonts: FONTS }), deckHtml(a, on, { fonts: FONTS }));
});
test("compose-chrome: no stamp region when the stamps are off", async () => {
const dir = mkdtempSync(path.join(tmpdir(), "deck-counts-stamp-"));
const env = process.env.HYPERFRAMES_BIN;
try {
const stub = path.join(dir, "hf-stub.mjs");
writeFileSync(stub, "#!/usr/bin/env node\nprocess.exit(1);\n");
chmodSync(stub, 0o755);
process.env.HYPERFRAMES_BIN = stub;
const font = path.join(dir, "face.ttf");
writeFileSync(font, "face");
const render = { ...renderWith({}, { stamp: { show: false } }), fontRegular: font, fontBold: font };
const mp = path.join(dir, "video.manifest.json");
writeFileSync(mp, JSON.stringify({ slug: "f", render, timeline: ENTRIES }));
const out = path.join(dir, "out", "sourced");
mkdirSync(out, { recursive: true });
writeFileSync(path.join(out, "schedule.json"), JSON.stringify(sched(render)));
const { composeChrome } = await import("./compose-chrome.mjs");
await assert.rejects(composeChrome({ manifestPath: mp, region: "stamp" }), /the stamps are off/);
} finally {
if (env === undefined) delete process.env.HYPERFRAMES_BIN;
else process.env.HYPERFRAMES_BIN = env;
rmSync(dir, { recursive: true, force: true });
}
});
// ---- F2: no QR on the posts ---------------------------------------------------
test("posts.qr: a setting, default on; refused when not a boolean", () => {
assert.equal(DECK_DEFAULTS.posts.qr, true);
assert.equal(resolveDeck(renderWith({ posts: { qr: false } })).posts.qr, false);
assert.deepEqual(validateChrome({ ...CHROME, deck: { posts: { qr: false } } }, BASE), []);
assert.match(validateChrome({ ...CHROME, deck: { posts: { qr: 0 } } }, BASE).join(" "), /posts\.qr must be true or false/);
});
test("posts.qr false, popup: no code and no cell; the words take the width; the deck's own QR stays", () => {
const on = renderWith();
const off = renderWith({ posts: { qr: false } });
const qrSrcs = { p1: "assets/pqr0.png", p2: "assets/pqr1.png" };
const page = (r) => {
const s = sched(r, POSTS);
const win = snapWindow(postWindows(s)[0], { fps: 30, total: s.total });
assert.deepEqual(windowPosts(s, win.segment).map((p) => p.id), ["p1", "p2"]);
return postsHtml(s, r, win, { fonts: FONTS, qrSrcs, logoSrcs: { p2: "assets/logo.png" } });
};
const withQr = page(on);
assert.ok(withQr.includes('
/g) ?? []).length, 1);
assert.match(without, /\.post\.no-plate \.body \{ width: 594px; \}/, "600 less the rail");
assert.match(without, /\.post \{ min-height: 0; \}/);
// The clip's deck QR is the deck's: untouched.
const s = sched(off, POSTS);
assert.equal(deckHtml(s, off, { qrSrcs: { c2: "assets/qr1.png" } }), deckHtml(sched(on, POSTS), on, { qrSrcs: { c2: "assets/qr1.png" } }));
});
test("posts.qr false, feed: no code, no cell, the body the stack's width less the rail", () => {
const off = renderWith({ posts: { layout: "feed", qr: false } });
const on = renderWith({ posts: { layout: "feed" } });
assert.equal(feedLayout(off).card.plate, 0);
assert.equal(feedLayout(on).card.plate, 120 + 28);
const qrSrcs = { p1: "assets/pqr0.png", p2: "assets/pqr1.png" };
const html = feedHtml(sched(off, POSTS), off, { qrSrcs });
assert.doesNotMatch(html, /pqr\d\.png/);
assert.doesNotMatch(html, /
{
assert.equal(DECK_DEFAULTS.pip.color, "accent");
assert.deepEqual(validateChrome({ ...CHROME, deck: { pip: { color: "verdict" } } }, BASE), []);
assert.match(validateChrome({ ...CHROME, deck: { pip: { color: "red" } } }, BASE).join(" "), /pip\.color must be "accent" or "verdict"/);
});
test("VERDICT_SHAPES: one distinct shape per verdict, in VERDICTS order", () => {
assert.deepEqual(Object.keys(VERDICT_SHAPES), [...VERDICTS]);
assert.equal(new Set(Object.values(VERDICT_SHAPES)).size, VERDICTS.length);
assert.deepEqual(Object.values(VERDICT_SHAPES), ["circle", "diamond", "square", "triangle", "ring"]);
});
test("pip.color verdict: a claimed pip lit in its verdict's colour (relabels included) and shape; an unclaimed one as before", () => {
const r = renderWith({ pip: { color: "verdict" } }, { verdicts: { PARTLY: { color: "#ff00aa" } } });
const s = sched(r);
const { pips } = deckCues(s, r);
assert.deepEqual(pips.map((p) => [p.id, p.verdict ?? null]),
[["c1", "PARTLY"], ["c2", "CONTRADICTED"], ["c3", null], ["c4", "CORROBORATED"]]);
const html = deckHtml(s, r, {});
const lit = (k) => new RegExp(`