// The counter module: named series in place of the fact-check verdicts
// (report-to-video counter.mjs, modules/counter.mjs).
import { test } from "node:test";
import assert from "node:assert/strict";
import { counterOrder, resolveCounter, validateCounter, VERDICT_SHAPES } from "../counter.mjs";
import {
claimOf, counterLabelProblem, fitTallyLabel, tallyLabelNotes, marksOf, normalizeClaim, resolveFactcheck, tallyAlways, tallyCells, tallyCellsFrom, tallyCues, tallyFinals, tallyLabelLines,
tallyOf, tallyShowAt, validateClaims, writtenLabelLines,
} from "../factcheck.mjs";
import { deckLayout, estimateSchedule, validateChrome } from "../deck.mjs";
import { counterCell } from "./design.mjs";
import { deckHtml } from "../chrome-deck.mjs";
import counter from "./counter.mjs";
import { moduleProblems } from "./index.mjs";
const PALETTE = { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" };
const render = (deck = {}, factcheck = undefined) => ({
width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE,
chrome: { engine: "hyperframes", layout: "deck", deck, ...(factcheck ? { factcheck } : {}) },
});
const SERIES = [
{ id: "seizures", label: "Seizures", color: "#e5534b", stampLabel: "Seizure" },
{ id: "pitches", label: "Pitches", color: "#e3b23c", showAt: "first" },
];
test("without a counter the series are the verdicts, as the tally always counted them", () => {
const c = resolveCounter(render({}, { verdicts: { CORROBORATED: { label: "Seizures" } }, tally: { showAt: { PARTLY: "first" } } }));
assert.equal(c.custom, false);
assert.deepEqual(c.order, ["CORROBORATED", "PARTLY", "CONTRADICTED", "NOT_FOUND", "UNTESTABLE"]);
assert.equal(c.byId.CORROBORATED.label, "Seizures");
assert.deepEqual(c.shapes, VERDICT_SHAPES);
assert.equal(c.showAt.PARTLY, "first");
assert.equal(c.showAt.CORROBORATED, "always");
assert.deepEqual(resolveFactcheck(render()).verdicts, resolveCounter(render()).byId);
});
test("named series: their order, labels, colours, shapes by place, showAt and unit", () => {
const r = render({ counter: { series: [...SERIES, { id: "NOT_FOUND" }, { id: "x", label: "X", unit: "things" }], position: "left" } });
const c = resolveCounter(r);
assert.equal(c.custom, true);
assert.deepEqual(c.order, ["seizures", "pitches", "NOT_FOUND", "x"]);
assert.deepEqual(c.byId.seizures, { label: "Seizures", color: "#e5534b", stampLabel: "Seizure" });
assert.deepEqual(c.byId.NOT_FOUND, { label: "Not found", color: "#8b93a7" });
assert.equal(c.shapes.seizures, "circle");
assert.equal(c.shapes.pitches, "diamond");
assert.equal(c.shapes.NOT_FOUND, "triangle", "a verdict keeps its own shape");
assert.equal(c.series[3].unit, "things");
assert.equal(c.showAt.pitches, "first");
assert.deepEqual(tallyShowAt(r), c.showAt);
assert.equal(resolveFactcheck(r).tally.position, "left");
assert.deepEqual(counterOrder(r), c.order);
});
test("validateCounter: every shape refused in a sentence; one home for the series", () => {
assert.deepEqual(validateCounter(undefined), []);
assert.deepEqual(validateCounter({ series: SERIES }), []);
assert.match(validateCounter({ series: [] })[0], /at least one series/);
assert.match(validateCounter({ series: [{ id: "9x", label: "A" }] })[0], /id must be a letter/);
assert.match(validateCounter({ series: [{ id: "a", label: "A" }, { id: "a", label: "B" }] })[0], /listed twice/);
assert.match(validateCounter({ series: [{ id: "custom" }] })[0], /label is needed/);
assert.match(validateCounter({ series: [{ id: "a", label: "A", color: "red" }] })[0], /color must be a hex/);
assert.match(validateCounter({ series: [{ id: "a", label: "A", shape: "star" }] })[0], /shape must be one of/);
assert.match(validateCounter({ series: SERIES, colour: 1 })[0], /colour is not a counter setting/);
assert.match(validateCounter({ series: SERIES }, { factcheck: { verdicts: {} } })[0], /replaces render.chrome.factcheck.verdicts/);
// Through the deck's validator: a label wider than its cell is fitted, never refused -- the lint says so.
const wide = render({ counter: { series: [{ id: "a", label: "Extraordinarilylong" }] } });
assert.deepEqual(validateChrome(wide.chrome, render()), []);
assert.match(tallyLabelNotes(wide).join("\n"), /^render\.chrome\.deck\.counter\.series\[0\]\.label "Extraordinarilylong" is wider than the tally's cell: (drawn at|cut to)/);
assert.deepEqual(validateChrome(render({ counter: { series: SERIES } }).chrome, render()), []);
});
test("a mark names a series by id: `verdict`, or its alias `counter`", () => {
const series = ["seizures", "pitches"];
assert.deepEqual(normalizeClaim({ id: "k1", counter: "seizures" }, { series }), { id: "k1", verdict: "seizures" });
assert.deepEqual(normalizeClaim({ id: "k1", verdict: "pitches", at: 3 }, { series }), { id: "k1", verdict: "pitches", at: 3 });
assert.throws(() => normalizeClaim({ id: "k1", counter: "seizures", verdict: "pitches" }, { series }), /one setting/);
assert.throws(() => normalizeClaim({ id: "k1", verdict: "CORROBORATED" }, { series }), /must be one of seizures, pitches/);
// Without the series named, the verdicts (as ever).
assert.throws(() => normalizeClaim({ id: "k1", verdict: "seizures" }), /must be one of CORROBORATED/);
assert.deepEqual(marksOf({ type: "clip", claim: { id: "k", counter: "pitches" } }, series), [{ id: "k", verdict: "pitches" }]);
assert.equal(claimOf({ type: "clip", claim: { id: "k", counter: "pitches" } }), null, "not a verdict, not a mark without the series");
});
test("a cut counting named series: the schedule, the tally's cells, the deck's page", () => {
const r = render({ counter: { series: SERIES }, pip: { color: "verdict" } });
const manifest = {
provenance: {}, posts: [],
render: r,
timeline: [
{ type: "clip", id: "a", video: "v1", start: 0, end: 10, claim: { id: "s1", counter: "seizures" } },
{ type: "clip", id: "b", video: "v2", start: 0, end: 10, claims: [{ id: "p1", verdict: "pitches" }, { id: "s2", verdict: "seizures", count: 2 }] },
{ type: "clip", id: "c", video: "v3", start: 0, end: 10 },
],
};
assert.deepEqual(validateClaims(manifest), []);
assert.deepEqual(moduleProblems(manifest), {});
const s = estimateSchedule(manifest);
assert.deepEqual(s.factcheck.stamps.map((x) => [x.claim, x.verdict]), [["s1", "seizures"], ["p1", "pitches"], ["s2", "seizures"]]);
assert.deepEqual(tallyOf(s, r), ["seizures", "pitches"]);
assert.deepEqual(tallyFinals(s.factcheck.stamps, counterOrder(r)), { seizures: 3, pitches: 1 });
const html = deckHtml(s, r, {});
assert.match(html, /data-verdict="seizures"/);
assert.match(html, /
Pitches<\/div>/);
assert.match(html, /data-shape="diamond"/);
// A series the counter does not have is refused by name.
const bad = { ...manifest, timeline: [{ type: "clip", id: "a", video: "v", start: 0, end: 5, claim: { id: "x", verdict: "CORROBORATED" } }] };
assert.match(validateClaims(bad)[0], /claim\.verdict must be one of seizures, pitches/);
});
test("a series that says `showAt: always` is drawn at 0 though no mark uses it (lane counter-zero); unsaid, `first` and the verdicts are not", () => {
const SIDES = [
{ id: "LEFT", label: "Left", color: "#5b9bd5" },
{ id: "CENTER", label: "Center", color: "#e8d27a" },
{ id: "RIGHT", label: "Right", color: "#e5534b", showAt: "first" },
];
const r = render({ counter: { series: SIDES, showAt: "always" } });
const manifest = {
provenance: {}, posts: [], render: r,
timeline: [
{ type: "clip", id: "a", video: "v1", start: 0, end: 10, claim: { id: "d1", verdict: "CENTER" } },
{ type: "clip", id: "b", video: "v2", start: 0, end: 10 },
],
};
const s = estimateSchedule(manifest);
// LEFT (always, the default) is a cell, in the counter's order; RIGHT (first, never counted) is not.
assert.deepEqual(tallyAlways(r), ["LEFT", "CENTER"]);
assert.deepEqual(tallyOf(s, r), ["LEFT", "CENTER"]);
assert.deepEqual(tallyCells(s.factcheck.stamps, r), ["LEFT", "CENTER"]);
// Its cell reads 0, dim as an always cell is before its first count, and never steps.
const cues = tallyCues(s.factcheck.stamps, tallyOf(s, r));
assert.deepEqual(cues.init["ty.LEFT"], { opacity: 0.42 });
assert.deepEqual(cues.init["ty.LEFT.n0"], { yPercent: 0, autoAlpha: 1 });
assert.equal(cues.events.filter((e) => e.k.startsWith("ty.LEFT")).length, 0);
const html = deckHtml(s, r, {});
assert.match(html, /data-verdict="LEFT"/);
assert.match(html, /
Left<\/div>/);
assert.doesNotMatch(html, /data-verdict="RIGHT"/);
// Said, not defaulted: a counter that never says "always" (as `umtool migrate` writes one) is a cell per series counted.
const unsaid = render({ counter: { series: SIDES } });
assert.deepEqual(tallyAlways(unsaid), []);
assert.deepEqual(tallyOf(estimateSchedule({ ...manifest, render: unsaid }), unsaid), ["CENTER"]);
// A series' own "always" says it for that series alone.
const one = render({ counter: { series: [{ ...SIDES[0], showAt: "always" }, SIDES[1], SIDES[2]] } });
assert.deepEqual(tallyAlways(one), ["LEFT"]);
// The counter's own showAt: first leaves an unused series out, as before.
const firstAll = render({ counter: { series: SIDES, showAt: "first" } });
assert.deepEqual(tallyAlways(firstAll), []);
assert.deepEqual(tallyOf(estimateSchedule({ ...manifest, render: firstAll }), firstAll), ["CENTER"]);
// No mark at all: no tally, as ever (the counter steps on stamps).
assert.equal(tallyOf(estimateSchedule({ ...manifest, timeline: [manifest.timeline[1]] }), r), null);
// The fact-check verdicts keep their tally of the verdicts the cut stamps.
const fc = render();
assert.deepEqual(tallyAlways(fc), []);
const verdicts = { ...manifest, render: fc, timeline: [{ ...manifest.timeline[0], claim: { id: "d1", verdict: "PARTLY" } }] };
assert.deepEqual(tallyOf(estimateSchedule(verdicts), fc), ["PARTLY"]);
// The pure form the pages read: order, then the always ones besides the used.
assert.deepEqual(tallyCellsFrom([{ verdict: "c" }], ["a", "b", "c"], ["a"]), ["a", "c"]);
assert.deepEqual(tallyCellsFrom([{ verdict: "c" }], ["a", "b", "c"]), ["c"]);
});
test("past 8 series the cells narrow into one row (up to 12); as written, a label keeps its case in up to two lines (lane leaflit-labels)", () => {
// The cell: 124 px up to 8, then the dense row's share of 1040 px.
assert.deepEqual(counterCell(8), { cell: 124, gap: 10, labelPad: 6 });
assert.deepEqual(counterCell(9), { cell: 108, gap: 8, labelPad: 6 });
assert.deepEqual(counterCell(10), { cell: 96, gap: 8, labelPad: 6 });
assert.deepEqual(counterCell(12), { cell: 79, gap: 8, labelPad: 6 });
const twelve = Array.from({ length: 12 }, (_, i) => ({ id: `s${i}`, label: `S${i}` }));
assert.deepEqual(validateCounter({ series: twelve }), []);
assert.match(validateCounter({ series: [...twelve, { id: "s12", label: "S12" }] }).join(), /series holds 13 \(at most 12\)/);
assert.match(validateCounter({ series: SERIES, labels: "lower" })[0], /labels must be "caps" or "as-written"/);
// Her phrases, as said: two lines in a 10-cell row's 84 px, no word cut, no last-words fallback.
assert.deepEqual(writtenLabelLines("slightly more conservative", { width: 84 }), ["slightly more", "conservative"]);
assert.deepEqual(writtenLabelLines("close to MAGA", { width: 84 }), ["close to", "MAGA"]);
assert.deepEqual(writtenLabelLines("moderate", { width: 84 }), ["moderate"]);
assert.equal(writtenLabelLines("a b c d e f g h i j k l m n o p q r s t", { width: 40 }), null, "never a third line");
assert.equal(writtenLabelLines("conservatively", { width: 60 }), null, "never inside a word");
assert.deepEqual(tallyLabelLines({ label: "moderate center right" }, { n: 10, labels: "as-written" }), ["moderate", "center right"]);
// Capitalised (the default), the one line it always was -- in a 9-cell row's narrower room.
assert.deepEqual(tallyLabelLines({ label: "Seizures" }, { n: 4 }), ["Seizures"]);
assert.deepEqual(tallyLabelLines({ label: "Moderate center right" }, { n: 10 }), ["right"]);
// Its fit check, in the row it stands in: a word wider than a 12-cell row's room is fitted (drawn smaller), never refused.
assert.equal(counterLabelProblem({ id: "a", label: "slightly more conservative" }, "w", { n: 10, labels: "as-written" }), null);
assert.equal(counterLabelProblem({ id: "a", label: "conservative" }, "w", { n: 12, labels: "as-written" }), null);
const fit = fitTallyLabel({ label: "conservative" }, { n: 12, labels: "as-written" });
assert.deepEqual([fit.lines, fit.shortened], [["conservative"], "shrunk"]);
assert.ok(fit.size < 13);
// Through the deck's validator: 10 phrases as written, a written label to 32 characters, a stamp word still 24.
const PHRASES = ["moderate", "moderate conservative", "centrist", "moderate right", "centered conservative", "slightly more conservative", "moderate center right", "socially moderate", "conservative", "close to MAGA"];
const series = PHRASES.map((label, i) => ({ id: `p${i}`, label }));
const asWritten = render({ counter: { series, labels: "as-written" } });
assert.deepEqual(validateChrome(asWritten.chrome, asWritten), []);
const capped = render({ counter: { series } });
assert.match(validateChrome(capped.chrome, capped).join("\n"), /series\[5\]\.label is 26 characters \(at most 24\)/);
const stampy = render({ counter: { series: [{ id: "a", label: "slightly more conservative", stampLabel: "slightly more conservative" }], labels: "as-written" } });
assert.match(validateChrome(stampy.chrome, stampy).join("\n"), /stampLabel is 26 characters \(at most 24\)/);
// The deck: one row of 10 narrower cells, each label as written in its lines, the count over them.
const manifest = {
provenance: {}, posts: [], render: asWritten,
timeline: series.map((x, i) => ({ type: "clip", id: `c${i}`, video: `v${i}`, start: 0, end: 5, claim: { id: `k${i}`, verdict: x.id } })),
};
assert.deepEqual(validateClaims(manifest), []);
const s = estimateSchedule(manifest);
const lay = deckLayout(asWritten, { tally: tallyOf(s, asWritten), schedule: s });
assert.equal(lay.tally?.cell, 96);
assert.equal(lay.tally?.width, 10 * 96 + 9 * 8);
assert.ok((lay.text.width ?? 0) > 500, `the title keeps a column (${lay.text.width} px)`);
const html = deckHtml(s, asWritten, {});
assert.match(html, /
slightly more
conservative<\/div>/);
assert.match(html, /
close to
MAGA<\/div>/);
assert.match(html, /
moderate<\/div>/);
assert.match(html, /letter-spacing: 0\.02em; text-transform: none;/);
// Capitalised up to 8 series: the page it always was.
const four = render({ counter: { series: SERIES } });
const s4 = estimateSchedule({ ...manifest, render: four, timeline: [{ type: "clip", id: "a", video: "v", start: 0, end: 5, claim: { id: "k", verdict: "seizures" } }] });
const h4 = deckHtml(s4, four, {});
assert.match(h4, /letter-spacing: 0\.08em; text-transform: uppercase;/);
assert.match(h4, /
Seizures<\/div>/);
});
test("the counter module: its home, its validator, its docs", () => {
assert.equal(counter.kind, "part");
assert.equal(counter.host, "shell");
assert.equal(counter.configKey, "render.chrome.deck.counter");
assert.deepEqual(counter.provides, ["counter.totals", "counter.series"]);
assert.match(counter.docs.rows[0][0], /deck\.counter\.series/);
assert.ok(counter.ui.some((f) => f.key === "series"));
});