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 { createReportReader, reportMetadata } from "./reader-server"; import { isReportBase, reportBaseDir, SITE_REPORTS_BASE } from "./routes"; import { reportHistoryPagePath, reportHistoryViewPath, REPORT_HISTORY_FORMAT } from "./revisions"; import { MOMENT_INDEX_FORMAT, MOMENT_PAGE_FORMAT, MOMENTS_INDEX_PATH, REPORT_INDEX_FORMAT, REPORT_PAGE_FORMAT, REPORTS_INDEX_PATH, citedInHref, momentViewPath, reportAssetPath, reportPagePath, reportsIndexPath, reportViewPath, } from "./views"; // The shared reader (reader-server.ts) under a base of its own: it reads // what compose would write under that base, and nothing under another. function publicDirWith(files: Record): string { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "report-reader-")); for (const [urlPath, value] of Object.entries(files)) { const file = path.join(dir, ...urlPath.split("/").filter(Boolean)); fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, typeof value === "string" ? value : JSON.stringify(value)); } return dir; } const page = (id: string) => ({ format: REPORT_PAGE_FORMAT, id, kind: "sweep", title: `Title ${id}`, sections: [] }); test("the site's base is /pages/, and every path helper defaults to it", () => { assert.equal(SITE_REPORTS_BASE, "/pages/"); assert.equal(REPORTS_INDEX_PATH, "/pages/index.json"); assert.equal(reportPagePath("a"), "/pages/a/"); assert.equal(reportHistoryPagePath("a"), "/pages/a/history/"); assert.equal(reportBaseDir(SITE_REPORTS_BASE), "pages"); }); test("a base names every report path", () => { assert.equal(reportsIndexPath("/articles/"), "/articles/index.json"); assert.equal(reportPagePath("a", "/articles/"), "/articles/a/"); assert.equal(reportViewPath("a", "/articles/"), "/articles/a/page.json"); assert.equal(reportAssetPath("a", "./stills/x.png", "/articles/"), "/articles/a/stills/x.png"); assert.equal(reportHistoryViewPath("a", "/articles/"), "/articles/a/history/history.json"); assert.equal(citedInHref({ reportId: "a", sectionId: "s1", claimId: "k1" }, "/articles/"), "/articles/a/#k1"); }); test("a base is lowercase segments with a slash on both ends", () => { for (const ok of ["/reports/", "/pages/", "/articles/", "/a/b/"]) assert.ok(isReportBase(ok), ok); for (const bad of ["reports/", "/reports", "/", "/Reports/", "//", "/../", 3]) assert.ok(!isReportBase(bad), String(bad)); assert.equal(reportBaseDir("/reports/"), "reports"); assert.equal(reportBaseDir("/a/b/"), "a/b"); }); test("the reader reads its own base's views, in index order, and nothing under another base", () => { const dir = publicDirWith({ [reportsIndexPath("/articles/")]: { format: REPORT_INDEX_FORMAT, reports: [{ id: "two" }, { id: "Not An Id" }, { id: "one" }], }, [reportViewPath("one", "/articles/")]: page("one"), [reportViewPath("two", "/articles/")]: page("two"), [reportViewPath("one")]: page("one"), [reportHistoryViewPath("one", "/articles/")]: { format: REPORT_HISTORY_FORMAT, reportId: "one", revisions: [{ revision: 1 }], }, [MOMENTS_INDEX_PATH]: { format: MOMENT_INDEX_FORMAT, moments: ["ch/post1", "bad"] }, [momentViewPath("ch/post1")]: { format: MOMENT_PAGE_FORMAT, key: "ch/post1" }, }); const articles = createReportReader({ publicDir: dir, base: "/articles/" }); assert.equal(articles.base, "/articles/"); assert.deepEqual(articles.reportIds(), ["two", "one"]); assert.ok(articles.hasReports()); assert.equal(articles.readReportView("one")?.title, "Title one"); assert.equal(articles.readReportView("../one"), null); assert.equal(articles.readReportHistory("one")?.reportId, "one"); assert.equal(articles.readReportHistory("two"), null); assert.equal(articles.onlyReportView(), null); assert.deepEqual(articles.momentKeys(), ["ch/post1"]); assert.equal(articles.readMomentView("ch/post1")?.key, "ch/post1"); assert.equal(articles.readMomentView("bad"), null); // The site's base (the default) has no index here: no reports. const site = createReportReader({ publicDir: () => dir }); assert.equal(site.base, SITE_REPORTS_BASE); assert.equal(site.readReportIndex(), null); assert.deepEqual(site.reportIds(), []); assert.ok(!site.hasReports()); }); test("a missing, unreadable or foreign file reads as none", () => { const dir = publicDirWith({ [REPORTS_INDEX_PATH]: { format: "something-else", reports: [{ id: "a" }] }, [reportViewPath("a")]: "{not json", }); const reader = createReportReader({ publicDir: dir }); assert.equal(reader.readReportIndex(), null); assert.equal(reader.readReportView("a"), null); assert.equal(reader.readReportView("b"), null); assert.deepEqual(reader.momentKeys(), []); }); test("one report: onlyReportView is it", () => { const dir = publicDirWith({ [REPORTS_INDEX_PATH]: { format: REPORT_INDEX_FORMAT, reports: [{ id: "solo" }] }, [reportViewPath("solo")]: page("solo"), }); assert.equal(createReportReader({ publicDir: dir }).onlyReportView()?.id, "solo"); }); test("a report's metadata: its full title, its subtitle, its feeds", () => { const md = reportMetadata({ ...page("a"), series: "Series", subtitle: "Sub", feeds: { rss: "https://x.example/reports/a/feed.xml", json: "https://x.example/reports/a/feed.json" }, } as unknown as Parameters[0]); assert.equal(md.title, "Series: Title a"); assert.equal(md.description, "Sub"); assert.deepEqual(Object.keys(md.alternates?.types ?? {}), ["application/rss+xml", "application/feed+json"]); });