// The reports stage over the ARTICLES host (release 26 A2): the same resolver // and writer a site's compose runs, fed the article store // (transcripts/reports/, index.json, `audience`) and written into another // output root — the homepage's public dir. A temp corpus only. // // - the whole corpus is citable (D4): a channel no site has is cited; // - a public build carries only `audience: "public"` articles; a private one // every published article, in index.json's order; // - the post visibility rule holds by the build's audience; // - a record links into the archive the host names for its channel, or // nowhere; // - export/public is not touched. // // Run with: node_modules/.bin/tsx --test publish/composeReportsArticles.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "compose-articles-")); Object.assign(process.env, { 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"), }); after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { writePosts } = await import("../lib/posts-server"); const { setPublished, configureArticle, articleDir } = await import("../lib/report/registry"); const { articlesReportHost, articleNotInCorpusMessage } = await import("./reportHost"); const { composeHostReports, ComposeReportsError } = await import("./composeReports"); const { REPORT_MEDIA_FORMAT, REPORT_MEDIA_VERSION, storeMediaDir, storeMediaIndexFile } = await import("./reportMedia"); const paths = getPaths(); const OUT = path.join(ROOT, "homepage-public"); const LONE = "lone-channel"; const X = "x-posts"; const X_PRIVATE = { social: { x: { visibility: "private" } } }; 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; function filesUnder(dir: string): string[] { const out: string[] = []; const walk = (d: string, rel: string) => { for (const e of readdirSync(d, { withFileTypes: true })) { const r = rel ? `${rel}/${e.name}` : e.name; if (e.isDirectory()) walk(path.join(d, e.name), r); else out.push(r); } }; if (existsSync(dir)) walk(dir, ""); return out.sort(); } const ts = (s: number) => new Date(s * 1000).toISOString().slice(11, 23); const vtt = (cues: [number, number, string][]) => "WEBVTT\nKind: captions\nLanguage: en\n\n" + cues.map(([a, b, text]) => `${ts(a)} --> ${ts(b)} align:start position:0%\n${text}<${ts(a)}>\n`).join("\n"); const SPAN = { kind: "video", channel: LONE, id: "abc123", start: 10, end: 20, quote: "The bridge opened in the spring, I was there for it." }; const POST = { kind: "post", channel: X, id: "1800", quote: "Posted to settle it" }; function article(id: string, citations: Record, over: Record = {}) { const cited = Object.keys(citations).map((c) => `[${c}](cite:${c})`).join(", "); return { format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, sections: [{ id: "s1", title: "What was said", body: `It was said: ${cited}.` }], citations, ...over, }; } function seedArticle(id: string, citations: Record, over: Record = {}) { writeJson(path.join(articleDir(paths, id), "report.json"), article(id, citations, over)); } async function seed() { writeJson(paths.settingsFile, {}); // A channel no site has: an article may cite it (D4). writeJson(path.join(paths.channelsDir, LONE, "config.json"), { handling: "youtube", name: "Lone Channel", url: "https://www.youtube.com/@lone/videos", }); const abc = path.join(paths.channelsDir, LONE, "data", "abc123"); writeJson(path.join(abc, "metadata.info.json"), { id: "abc123", title: "A lone stream", channel: LONE, upload_date: "20260110", duration: 600, webpage_url: "https://www.youtube.com/watch?v=abc123", extractor_key: "Youtube", }); writeFileSync( path.join(abc, "transcript.en.vtt"), vtt([ [6, 10, "Let me be clear about this one."], [10, 15, "The bridge opened in the spring,"], [15, 20, "I was there for it."], [20, 30, "Anyway, back to the mail."], ]), ); // An X channel: its posts are private while `social.x.visibility` is. writeJson(path.join(paths.channelsDir, X, "config.json"), { handling: "youtube", name: "Some X account", url: "https://x.com/someone", sourceKind: "social", platform: "twitter", socialHandle: "someone", }); await writePosts(path.join(paths.channelsDir, X), [ { id: "1800", slug: `${X}/1800`, channelSlug: X, author: "someone", authorName: "Someone", createdAt: "2025-03-14T12:00:00.000Z", uploadDate: "20250314", text: "Posted to settle it. The bridge was open before I got there.", url: "https://x.com/someone/status/1800", platform: "twitter", isReply: false, isRepost: false, links: [], }, ]); seedArticle("art-pub", { c01: SPAN }); seedArticle("art-priv", { p01: POST }); seedArticle("art-draft", { c01: SPAN }); await setPublished("art-priv", true, {}, paths); await configureArticle("art-pub", { published: true, audience: "public" }, paths); // What `archilyzer reports prepare --article` leaves. const media = storeMediaDir({ stagingDir: path.join(paths.exportIndexDir, "articles") }); const hash = "0".repeat(31) + "1"; writeFileSync(path.join((mkdirSync(media, { recursive: true }), media), `${hash}.mp4`), "video-bytes"); writeJson(path.join(media, `${hash}.json`), { span: { from: 10, to: 20 } }); mkdirSync(path.join(media, "posts", X, "1800"), { recursive: true }); writeFileSync(path.join(media, "posts", X, "1800", "shot.png"), "shot"); writeJson(storeMediaIndexFile({ stagingDir: path.join(paths.exportIndexDir, "articles") }), { format: REPORT_MEDIA_FORMAT, version: REPORT_MEDIA_VERSION, siteId: "articles", preparedAt: "2026-10-11T00:00:00.000Z", moments: { [`${LONE}/abc123/10.00-20.00`]: { kind: "video", file: `${hash}.mp4`, bytes: 11, sha256: "f".repeat(64), width: null, height: null, durationSec: 15 }, [`${X}/1800`]: { kind: "post", file: `posts/${X}/1800/shot.png`, bytes: 4, sha256: "e".repeat(64), width: null, height: null, durationSec: null, media: [] }, }, problems: [], }); } await seed(); const quiet = { log: () => {} }; test("a private build: every published article in index.json's order, any corpus channel, posts by its audience", async () => { const host = await articlesReportHost(paths, { audience: "private" }); assert.deepEqual(host.published, ["art-priv", "art-pub"]); const composed = await composeHostReports({ paths, host, publicDir: OUT, settings: X_PRIVATE, ...quiet }); assert.deepEqual(composed.reports.map((r) => r.id), ["art-priv", "art-pub"]); assert.deepEqual(composed.moments, [`${LONE}/abc123/10.00-20.00`, `${X}/1800`]); const index = readJson<{ reports: { id: string }[] }>(path.join(OUT, "articles", "index.json")); assert.deepEqual(index.reports.map((r) => r.id), ["art-priv", "art-pub"]); // Served under the homepage's base: every href and published path is /articles/. const page = readJson<{ id: string }>(path.join(OUT, "articles", "art-priv", "page.json")); assert.equal(page.id, "art-priv"); assert.deepEqual( readJson<{ reports: { href: string }[] }>(path.join(OUT, "articles", "index.json")).reports.map((r) => r.href), ["/articles/art-priv/", "/articles/art-pub/"], ); assert.ok( readJson<{ citedIn: { href: string }[] }>(path.join(OUT, "m", LONE, "abc123", "10.00-20.00", "moment.json")).citedIn.every((e) => e.href.startsWith("/articles/"), ), ); assert.equal(existsSync(path.join(OUT, "pages")), false); assert.ok(existsSync(path.join(OUT, "media", "clips", LONE, "abc123", "10.00-20.00.mp4"))); assert.ok(existsSync(path.join(OUT, "media", "posts", X, "1800", "shot.png"))); // The draft is not published. assert.equal(existsSync(path.join(OUT, "articles", "art-draft")), false); // No archive named for the channel: the record links nowhere. const moment = readJson<{ record: { corpusUrl?: string } }>(path.join(OUT, "m", LONE, "abc123", "10.00-20.00", "moment.json")); assert.equal(moment.record.corpusUrl, undefined); // Feeds and history use the homepage's base (PROJECT_URL by default). assert.equal(host.siteUrl, "https://archilyzer.pages.dev"); // export/public is not this stage's output root here. assert.equal(existsSync(path.join(paths.exportPublicDir, "articles")), false); }); test("a public build: only public articles; a record links into the listed site that publishes its channel", async () => { const host = await articlesReportHost(paths, { audience: "public", channelSiteUrl: (slug) => (slug === LONE ? "https://lone.example.test/" : undefined), }); assert.deepEqual(host.published, ["art-pub"]); const composed = await composeHostReports({ paths, host, publicDir: OUT, settings: X_PRIVATE, ...quiet }); assert.deepEqual(composed.reports.map((r) => r.id), ["art-pub"]); // The earlier build's output is gone, the private article's post with it. assert.deepEqual( filesUnder(OUT).filter((f) => !f.startsWith("articles/art-pub/")), ["articles/index.json", "m/index.json", `m/${LONE}/abc123/10.00-20.00/moment.json`, `media/clips/${LONE}/abc123/10.00-20.00.mp4`], ); const moment = readJson<{ record: { corpusUrl?: string } }>(path.join(OUT, "m", LONE, "abc123", "10.00-20.00", "moment.json")); assert.equal(moment.record.corpusUrl, `https://lone.example.test/?v=${encodeURIComponent(`${LONE}/abc123`)}&t=10`); }); test("refused: an X post in a public build, a channel the corpus does not have", async () => { seedArticle("art-pub", { c01: SPAN, p01: POST }, { audience: "public" }); seedArticle("art-priv", { p01: POST, c09: { ...SPAN, channel: "no-such-channel" } }); try { const pub = await articlesReportHost(paths, { audience: "public" }); await assert.rejects(composeHostReports({ paths, host: pub, publicDir: OUT, settings: X_PRIVATE, ...quiet }), (e: unknown) => { assert.ok(e instanceof ComposeReportsError); assert.deepEqual( e.problems.map((p) => [p.kind, p.citation, p.message]), [["not-visible", "art-pub#p01", `the articles may not carry posts of "${X}" (the post visibility rule)`]], ); return true; }); // With X posts public, the same build carries the post. await composeHostReports({ paths, host: pub, publicDir: OUT, settings: {}, ...quiet }); assert.ok(existsSync(path.join(OUT, "m", X, "1800", "moment.json"))); const priv = await articlesReportHost(paths, { audience: "private" }); await assert.rejects(composeHostReports({ paths, host: priv, publicDir: OUT, settings: X_PRIVATE, ...quiet }), (e: unknown) => { assert.ok(e instanceof ComposeReportsError); assert.deepEqual( e.problems.map((p) => [p.kind, p.citation, p.message]), [["not-in-site", "art-priv#c09", articleNotInCorpusMessage("no-such-channel")]], ); return true; }); } finally { seedArticle("art-pub", { c01: SPAN }, { audience: "public" }); seedArticle("art-priv", { p01: POST }); } }); test("nothing published: the stage writes nothing and clears the output root's reports", async () => { const host = await articlesReportHost(paths, { audience: "public", published: [] }); const composed = await composeHostReports({ paths, host, publicDir: OUT, ...quiet }); assert.deepEqual(composed, { reports: [], moments: [], allowed: [] }); assert.deepEqual(filesUnder(OUT), []); }); // lane/local-media review fix 3: a local recording publishes only on a private // host — a public build refuses a citation of one; a private build carries it. test("refused: a local recording cited in a public build; a private build carries it", async () => { const { prepareManualLocalRecordingMetadata } = await import("../lib/localRecordingMetadata"); const LOCAL = "local-" + "f".repeat(32); const dir = path.join(paths.channelsDir, LONE, "data", LOCAL); writeJson(path.join(dir, "metadata.info.json"), prepareManualLocalRecordingMetadata({ id: LOCAL, filename: "talk.mp4", title: "A local talk" })); writeFileSync(path.join(dir, "transcript.en.vtt"), readFileSync(path.join(paths.channelsDir, LONE, "data", "abc123", "transcript.en.vtt"))); seedArticle("art-local", { c01: { ...SPAN, id: LOCAL } }); await configureArticle("art-local", { published: true, audience: "public" }, paths); try { const pub = await articlesReportHost(paths, { audience: "public" }); await assert.rejects(composeHostReports({ paths, host: pub, publicDir: OUT, settings: {}, ...quiet }), (e: unknown) => { assert.ok(e instanceof ComposeReportsError); assert.deepEqual( e.problems.filter((p) => p.citation === "art-local#c01").map((p) => [p.kind, p.message]), [["not-visible", `the articles is public and may not carry the local recording ${LONE}/${LOCAL} (local media is private until slice 4)`]], ); return true; }); const priv = await articlesReportHost(paths, { audience: "private" }); const composed = await composeHostReports({ paths, host: priv, publicDir: OUT, settings: {}, ...quiet }).catch((e: unknown) => e); const problems = composed instanceof ComposeReportsError ? composed.problems : []; assert.deepEqual(problems.filter((p) => p.citation === "art-local#c01"), []); } finally { await configureArticle("art-local", { published: false }, paths); rmSync(dir, { recursive: true, force: true }); } });