import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { loadSummary, summaryFile } from "./summary"; // Run with: // pnpm --filter homepage test const PUBLIC = "/repo/homepage/public/homepage-summary.json"; test("a production build never reads E2E_HOMEPAGE_SUMMARY_FILE", () => { assert.equal( summaryFile({ NODE_ENV: "production", E2E_HOMEPAGE_SUMMARY_FILE: "/tmp/fixture.json" }, PUBLIC), PUBLIC, ); }); test("outside production (the e2e's next dev) it names the file to read", () => { assert.equal( summaryFile({ NODE_ENV: "development", E2E_HOMEPAGE_SUMMARY_FILE: "/x/.e2e-summary.json" }, PUBLIC), "/x/.e2e-summary.json", ); assert.equal(summaryFile({ NODE_ENV: "development" }, PUBLIC), PUBLIC); assert.equal(summaryFile({ NODE_ENV: "development", E2E_HOMEPAGE_SUMMARY_FILE: "" }, PUBLIC), PUBLIC); }); // A summary written before release 14 carries no `wordmarkLead` on its sites; // it must still load, and its cards show the plain title (ArchiveCards). A lead // that is not a proper prefix of the title (hand-edited, or not a string) is // dropped on read, so the card never splits a title it does not start. test("the loader keeps a wordmarkLead only when it is a proper prefix of the title", () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "hp-summary-")); const file = path.join(dir, "homepage-summary.json"); const site = { siteId: "a", siteTitle: "Jeralyzer", siteDescription: "", siteUrl: "https://a.example" }; // Next types NODE_ENV as read-only; the test writes through a plain view. const env = process.env as Record; const prev = { NODE_ENV: env.NODE_ENV, E2E: env.E2E_HOMEPAGE_SUMMARY_FILE }; const write = (sites: unknown[]) => fs.writeFileSync(file, JSON.stringify({ version: 5, totals: { transcripts: 1 }, sites })); try { delete env.NODE_ENV; env.E2E_HOMEPAGE_SUMMARY_FILE = file; write([site]); assert.ok(!("wordmarkLead" in loadSummary()!.sites[0]), "an older summary: no lead"); write([{ ...site, wordmarkLead: "Jer" }]); assert.equal(loadSummary()?.sites[0].wordmarkLead, "Jer"); for (const bad of ["Jeralyzer", "jer", "Xyz", "", 7, null]) { write([{ ...site, wordmarkLead: bad }]); assert.ok(!("wordmarkLead" in loadSummary()!.sites[0]), JSON.stringify(bad)); } } finally { if (prev.NODE_ENV === undefined) delete env.NODE_ENV; else env.NODE_ENV = prev.NODE_ENV; if (prev.E2E === undefined) delete env.E2E_HOMEPAGE_SUMMARY_FILE; else env.E2E_HOMEPAGE_SUMMARY_FILE = prev.E2E; fs.rmSync(dir, { recursive: true, force: true }); } });