import { test } from "node:test"; import assert from "node:assert/strict"; import * as React from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { REPORT_HISTORY_FORMAT, REPORT_HISTORY_VERSION, type ReportHistoryView } from "../../lib/report/revisions"; import { MOMENT_PAGE_FORMAT, REPORT_VIEWS_VERSION, type MomentPageView } from "../../lib/report/views"; import MomentArticle from "./MomentArticle"; import ReportHistory from "./ReportHistory"; import ReportIndex from "./ReportIndex"; // The shared renderer's parameters (plans/reports-on-homepage.md, A1): the // base its build serves reports under, the corpus link resolver, and an empty // index's words. Every default is what a site rendered before they existed. (globalThis as { React?: typeof React }).React = React; const h = React.createElement; const history: ReportHistoryView = { format: REPORT_HISTORY_FORMAT, version: REPORT_HISTORY_VERSION, reportId: "demo", title: "Demo", branch: "main", clone: "/pages/demo/history/repo", revisions: [{ revision: 1, date: "2026-10-01T09:00:00Z", commit: "c".repeat(40), reportSha256: "a".repeat(64), summary: [], claims: [] }], }; const moment: MomentPageView = { format: MOMENT_PAGE_FORMAT, version: REPORT_VIEWS_VERSION, key: "chan/post1", kind: "post", record: { channel: "chan", id: "post1", originalUrl: "https://example.org/p/1", corpusUrl: "/?v=chan%2Fpost1&vm=post" }, quote: "A quote.", citedIn: [], }; test("a history page links its report and its history.json under the site's base, or the one it is given", () => { const site = renderToStaticMarkup(h(ReportHistory, { view: history })); assert.match(site, /href="\/pages\/demo\/"/); assert.match(site, /href="\/pages\/demo\/history\/history.json"/); const other = renderToStaticMarkup(h(ReportHistory, { view: history, base: "/articles/" })); assert.match(other, /href="\/articles\/demo\/"/); assert.match(other, /href="\/articles\/demo\/history\/history.json"/); assert.doesNotMatch(other, /href="\/pages\//); }); test("a moment opens in the archive at the view's corpus link, or the resolver's, or nowhere", () => { const own = renderToStaticMarkup(h(MomentArticle, { view: moment })); assert.match(own, /href="\/\?v=chan%2Fpost1&vm=post"[^>]*>Open in the archive/); const elsewhere = renderToStaticMarkup( h(MomentArticle, { view: moment, corpusHref: (r) => `https://archive.example.org/?v=${r.channel}%2F${r.id}` }), ); assert.match(elsewhere, /href="https:\/\/archive.example.org\/\?v=chan%2Fpost1"[^>]*>Open in the archive/); const none = renderToStaticMarkup(h(MomentArticle, { view: moment, corpusHref: () => undefined })); assert.doesNotMatch(none, /Open in the archive/); assert.match(none, /Original post/); }); test("an empty index says the site's words, or the ones it is given", () => { assert.match(renderToStaticMarkup(h(ReportIndex, { index: null })), /This site has not published a report yet/); const other = renderToStaticMarkup( h(ReportIndex, { index: null, empty: { title: "No articles", message: "Nothing published yet." } }), ); assert.match(other, /No articles/); assert.match(other, /Nothing published yet\./); });