Something went wrong. Try again.
Web frontend and supporting services for lance.blue
Something went wrong. Try again.
2.3 kB · 63 lines
JavaScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364/** * The waiting screen reads trivia.csv through parseTrivia(), and nothing else * checks that file. A malformed row does not error: parseTrivia skips it with * a console warning nobody watches, and the fact quietly leaves the rotation. * A typo'd era is worse — it renders straight into the badge. * * So this pins both halves: the parser against the CSV shapes the file uses, * and the file against the shape the screen renders. * * Run with `npm test`. */import { test } from "node:test";import assert from "node:assert/strict";import { access, readFile } from "node:fs/promises";
const { ERA_ICONS, parseCsv, parseTrivia } = await import("../src/content/trivia.ts");
const csv = await readFile( new URL("../src/content/trivia.csv", import.meta.url), "utf8",);
/** The eras the file may use; the badge renders these strings verbatim. */const ERAS = new Set(Object.keys(ERA_ICONS));
test("every era icon points at a file that ships", async () => { for (const [era, icon] of Object.entries(ERA_ICONS)) { if (icon === null) continue; await access(new URL(`../public${icon}`, import.meta.url)).catch(() => { assert.fail(`era "${era}": ${icon} is not in web/public`); }); }});
test("quoted fields, embedded commas and doubled quotes parse", () => { assert.deepEqual(parseCsv('a,"b ""c"", d",e\n'), [["a", 'b "c", d', "e"]]);});
test("every row of trivia.csv survives as a complete fact", () => { const facts = parseTrivia(csv);
// Row count against the raw parse: if these differ, a row was dropped as // malformed, and the assert message should name which id is missing. assert.equal(facts.length, parseCsv(csv).length - 1, "a row was dropped"); assert.ok(facts.length >= 100, `only ${facts.length} facts`);
const ids = new Set(); for (const fact of facts) { assert.ok(!ids.has(fact.id), `duplicate id ${fact.id}`); ids.add(fact.id); assert.ok(ERAS.has(fact.era), `fact ${fact.id}: unknown era "${fact.era}"`); assert.ok(fact.category.length > 0, `fact ${fact.id}: empty category`); assert.ok( fact.text.length > 0 && fact.text.length <= 280, `fact ${fact.id}: text is ${fact.text.length} chars`, ); assert.ok( fact.source.startsWith("https://www.sarna.net/"), `fact ${fact.id}: source "${fact.source}"`, ); }});