// Integration: a cited PAGE RECORD (release 37 S4) through the REAL site // compose (bin/compose-site.ts main) over a temp corpus: a Wayback pages // channel holding two captures of one page, a report citing the later one as // a post, and the prepared shot. The moment page carries the capture — its // kind, both links, the version list — and the record's links are the // original named as possibly gone and the Wayback copy. Every id is invented. // // Run with: node_modules/.bin/tsx --test publish/composeReportsPages.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "compose-pages-")); const PINNED: Record = { TRANSCRIPTS_DIR: path.join(ROOT, "transcripts"), SAVED_VIDEOS_DIR: path.join(ROOT, "saved-videos"), SITES_DIR: path.join(ROOT, "transcripts", "sites"), SETTINGS_FILE: path.join(ROOT, "settings.json"), EXPORT_PUBLIC_DIR: path.join(ROOT, "public"), EXPORT_INDEX_DIR: path.join(ROOT, ".export-index"), EXPORT_BUILDS_DIR: path.join(ROOT, ".export-builds"), EDITOR_CHANGELOG_FILE: path.join(ROOT, "editor-CHANGELOG.md"), EXPORT_CHANGELOG_FILE: path.join(ROOT, "export-CHANGELOG.md"), CHARTS_CONFIG_FILE: path.join(ROOT, "chart-templates.json"), SEARCH_ALIASES_FILE: path.join(ROOT, "transcripts", "search-aliases.json"), CURATED_TAGS_FILE: path.join(ROOT, "transcripts", "tags.json"), ARCHILYZER_CONFIG_DIR: path.join(ROOT, "config"), ARCHILYZER_SOURCE_SCRATCH: path.join(ROOT, "source-scratch"), }; Object.assign(process.env, PINNED); delete process.env.REPORTS_ALLOW_MISSING_MEDIA; after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { buildIndex } = await import("../controller/buildIndex"); const { writePosts } = await import("../lib/posts-server"); const { main: composeSite } = await import("../bin/compose-site"); const { REPORT_MEDIA_FORMAT, REPORT_MEDIA_VERSION, reportMediaDir, reportMediaIndexFile } = await import("./reportMedia"); const { pageRecordId } = await import("../lib/waybackPages/types"); const paths = getPaths(); const PAGES = "demo-pages"; const REPORT = "page-report"; const URL_A = "https://blog.example/2014/03/a-post.html"; const TS1 = "20140305120000"; const TS2 = "20150611090000"; const ID1 = pageRecordId(TS1, URL_A); const ID2 = pageRecordId(TS2, URL_A); const writeJson = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value, null, 2)); }; const readJson = >(file: string): T => JSON.parse(readFileSync(file, "utf8")) as T; const pub = (...p: string[]) => path.join(paths.exportPublicDir, ...p); const record = (id: string, ts: string, text: string) => ({ id, slug: `${PAGES}/${id}`, channelSlug: PAGES, author: "Pat Example", createdAt: "2014-03-04T18:30:00.000Z", uploadDate: "20140304", text, url: URL_A, platform: "webpage" as const, isReply: false, isRepost: false, links: [], threadId: "page-0123456789ab", page: { originalUrl: URL_A, captureTs: ts, waybackUrl: `https://web.archive.org/web/${ts}/${URL_A}`, rawUrl: `https://web.archive.org/web/${ts}id_/${URL_A}`, kind: "generic" as const, title: "A Post About Bread", extractor: "generic", extractorVersion: 1, }, }); test("a cited page record: its moment page carries the capture, the versions and both links", async () => { writeJson(paths.settingsFile, {}); writeJson(path.join(paths.channelsDir, PAGES, "config.json"), { handling: "transcribe", name: "Demo Pages", url: "https://web.archive.org/web/*/blog.example/*", sourceKind: "social", platform: "webpage", socialHandle: "blog.example", wayback: { targets: [{ url: "blog.example/", match: "prefix" }] }, }); await writePosts(path.join(paths.channelsDir, PAGES), [ record(ID1, TS1, "The first loaf came out flat."), record(ID2, TS2, "The first loaf came out flat. Update: the fourth loaf rose."), ]); writeJson(path.join(paths.sitesDir, "cited", "site.json"), { siteId: "cited", siteTitle: "Site cited", siteDescription: "fixture", headerTitle: "cited", homeTagline: "", socialLinks: [], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", channels: [{ slug: PAGES, groupId: "default" }], siteUrl: "https://cited.example.test", archives: false, search: false, pages: [REPORT], }); writeJson(path.join(paths.sitesDir, "cited", "reports", REPORT, "report.json"), { format: "archilyzer-report", version: 1, id: REPORT, kind: "sweep", title: "The bread blog", citations: { p1: { kind: "post", channel: PAGES, id: ID2, quote: "the fourth loaf rose" } }, sections: [{ id: "s1", title: "The blog", claims: [{ id: "c1", text: "The blog said the fourth loaf rose.", citations: ["p1"] }] }], }); const dir = reportMediaDir(paths, "cited"); mkdirSync(path.join(dir, "posts", PAGES, ID2), { recursive: true }); writeFileSync(path.join(dir, "posts", PAGES, ID2, "shot.png"), "shot"); writeJson(reportMediaIndexFile(paths, "cited"), { format: REPORT_MEDIA_FORMAT, version: REPORT_MEDIA_VERSION, siteId: "cited", preparedAt: "2026-10-20T00:00:00.000Z", moments: { [`${PAGES}/${ID2}`]: { kind: "post", file: `posts/${PAGES}/${ID2}/shot.png`, bytes: 4, sha256: "e".repeat(64), width: null, height: null, durationSec: null, media: [] }, }, problems: [], }); await buildIndex({ paths, onLog: () => {} }); const log = console.log; console.log = () => {}; try { await composeSite({ siteId: "cited", paths }); } finally { console.log = log; } const m = readJson<{ kind: string; record: { title?: string; originalUrl?: string; originalLabel?: string; downloads?: { label: string; url: string }[] }; post: { shot?: string; page?: { kindLabel: string; captureDate: string; versions?: { count: number; changedOn: string[] } } }; }>(pub("m", PAGES, ID2, "moment.json")); assert.equal(m.kind, "post"); assert.equal(m.record.title, "A Post About Bread"); assert.equal(m.record.originalUrl, URL_A); assert.equal(m.record.originalLabel, "Original (may be gone)"); assert.deepEqual(m.record.downloads, [{ label: "Wayback Machine copy, 2015-06-11", url: `https://web.archive.org/web/${TS2}/${URL_A}` }]); assert.equal(m.post.shot, `/media/posts/${PAGES}/${ID2}/shot.png`); assert.equal(m.post.page?.kindLabel, "Web page"); assert.equal(m.post.page?.captureDate, "2015-06-11"); assert.deepEqual(m.post.page?.versions && { count: m.post.page.versions.count, changedOn: m.post.page.versions.changedOn }, { count: 2, changedOn: ["2015-06-11"], }); });