// `compose homepage`'s articles stage (release 26 A3/A4), over a temp corpus: // the public compose carries only public articles, the private one every // published article; both stamp homepage-build.json; a moment's record opens // in the listed public site that publishes its channel. // // Run with: node_modules/.bin/tsx --test bin/compose-homepage.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-homepage-")); const PUBLIC = path.join(ROOT, "homepage-public"); 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"), HOMEPAGE_PUBLIC_DIR: PUBLIC, }); delete process.env.ARTICLES_DIR; after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { buildIndex } = await import("../controller/buildIndex"); const { articleDir, configureArticle } = await import("../lib/report/registry"); const { main, channelSiteUrlOf } = await import("./compose-homepage"); const { createIteration, iterationDir, updateIteration, writeIterationFile } = await import("../lib/report/iterations"); const { ModelLogWriter, modelLogFilesIn, recordModelCall, runWithModelLog } = await import("../lib/llm/callLog-server"); const { REPORT_MEDIA_FORMAT, REPORT_MEDIA_VERSION } = await import("../publish/reportMedia"); const paths = getPaths(); const put = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, typeof value === "string" ? value : JSON.stringify(value, null, 2)); }; const readJson = >(file: string): T => JSON.parse(readFileSync(file, "utf8")) as T; const ts = (s: number) => new Date(s * 1000).toISOString().slice(11, 23); const QUOTE = "The bridge opened in the spring, I was there for it."; put(paths.settingsFile, {}); put(path.join(paths.channelsDir, "vids", "config.json"), { handling: "youtube", name: "Vids", url: "https://www.youtube.com/@vids/videos" }); put(path.join(paths.channelsDir, "vids", "data", "abc1", "metadata.info.json"), { id: "abc1", title: "A stream", channel: "vids", upload_date: "20260110", duration: 600, webpage_url: "https://www.youtube.com/watch?v=abc1", extractor_key: "Youtube", }); put( path.join(paths.channelsDir, "vids", "data", "abc1", "transcript.en.vtt"), `WEBVTT\nKind: captions\nLanguage: en\n\n${ts(10)} --> ${ts(20)} align:start position:0%\n${QUOTE}<${ts(10)}>\n`, ); put(path.join(paths.sitesDir, "listed", "site.json"), { siteId: "listed", siteTitle: "Listed", groups: [{ id: "default", name: "All", selectedByDefault: true }], defaultGroupId: "default", channels: [{ slug: "vids", groupId: "default" }], siteUrl: "https://listed.example.test", }); const article = (id: string, over: Record = {}) => ({ format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, citations: { c1: { kind: "video", channel: "vids", id: "abc1", start: 10, end: 20, quote: QUOTE } }, sections: [{ id: "s", title: "S", body: "Said [this](cite:c1)." }], ...over, }); put(path.join(articleDir(paths, "open"), "report.json"), article("open")); put(path.join(articleDir(paths, "closed"), "report.json"), article("closed")); await configureArticle("open", { published: true, audience: "public" }, paths); await configureArticle("closed", { published: true }, paths); // The articles' prepared media, as `reports prepare --article` leaves it. const media = path.join(paths.exportIndexDir, "articles", "report-media"); const hash = "0".repeat(31) + "1"; put(path.join(media, `${hash}.mp4`), "video"); put(path.join(media, `${hash}.json`), { span: { from: 10, to: 20 } }); put(path.join(media, "index.json"), { format: REPORT_MEDIA_FORMAT, version: REPORT_MEDIA_VERSION, siteId: "articles", preparedAt: "2026-10-11T00:00:00.000Z", moments: { "vids/abc1/10.00-20.00": { kind: "video", file: `${hash}.mp4`, bytes: 5, sha256: "f".repeat(64), width: null, height: null, durationSec: 10 } }, problems: [], }); await buildIndex({ paths, onLog: () => {} }); const quietly = async (fn: () => Promise) => { const log = console.log; console.log = () => {}; try { await fn(); } finally { console.log = log; } }; test("a public compose carries the public article only, stamped public; its moment opens in the listed site", async () => { await quietly(() => main({ paths, audience: "public" })); const index = readJson<{ reports: { id: string; href: string }[] }>(path.join(PUBLIC, "articles", "index.json")); assert.deepEqual(index.reports.map((r) => [r.id, r.href]), [["open", "/articles/open/"]]); assert.deepEqual(readJson(path.join(PUBLIC, "homepage-build.json")), { format: "archilyzer-homepage-build", version: 1, audience: "public", articles: ["open"], }); const moment = readJson<{ record: { corpusUrl?: string } }>(path.join(PUBLIC, "m", "vids", "abc1", "10.00-20.00", "moment.json")); assert.equal(moment.record.corpusUrl, "https://listed.example.test/?v=vids%2Fabc1&t=10"); assert.ok(existsSync(path.join(PUBLIC, "media", "clips", "vids", "abc1", "10.00-20.00.mp4"))); assert.ok(existsSync(path.join(PUBLIC, "homepage-summary.json")), "the landing data is composed as before"); }); test("a private compose carries every published article, stamped private", async () => { await quietly(() => main({ paths, audience: "private" })); const index = readJson<{ reports: { id: string }[] }>(path.join(PUBLIC, "articles", "index.json")); assert.deepEqual(index.reports.map((r) => r.id), ["open", "closed"]); assert.equal(readJson(path.join(PUBLIC, "homepage-build.json")).audience, "private"); }); test("channelSiteUrlOf: the first listed site with a public URL; none for a channel no listed site has", () => { const sites = [ { siteId: "a", siteUrl: undefined }, { siteId: "b", siteUrl: "https://b.example.test/" }, { siteId: "p", siteUrl: "https://p.example.test", audience: "private" }, ] as never; const of = channelSiteUrlOf({ x: ["a", "b"], y: ["p"] }, sites); assert.equal(of("x"), "https://b.example.test"); assert.equal(of("y"), undefined); assert.equal(of("z"), undefined); }); // Every file under `dir`, relative. 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; } test("iterations are never published: a public article's iterations and an article in the making stay off the homepage (release 27 S4)", async () => { const MARK = "ITERATION-ONLY-WORDS-7f3a"; await createIteration("open", { kind: "sweep", model: "qwen3:8b", question: `${MARK}?` }, paths); await writeIterationFile("open", 1, "report.json", article("open", { title: `${MARK} title` }), paths); await writeIterationFile("open", 1, "sweep.md", `# ${MARK}\n`, paths); await writeIterationFile("open", 1, "turns.jsonl", `${JSON.stringify({ role: "user", content: MARK })}\n`, paths); // The model log too (demo epic D3): the prompts and answers of the iteration's runs. const modelLog = new ModelLogWriter({ files: modelLogFilesIn(iterationDir(paths, "open", 1)), task: "sweep", scrub: {} }); await runWithModelLog(modelLog, async () => recordModelCall({ phase: "terms", model: "qwen3:8b", route: "local", system: MARK, prompt: `${MARK} asked`, answer: `${MARK} answered`, seconds: 1 })); await modelLog.flush(); assert.ok(existsSync(path.join(iterationDir(paths, "open", 1), "calls.jsonl")) && existsSync(path.join(iterationDir(paths, "open", 1), "calls-text.jsonl"))); await updateIteration("open", 1, { status: "done" }, paths); await createIteration("in-the-making", { kind: "sweep", model: "qwen3:8b", question: `${MARK}?` }, paths); await writeIterationFile("in-the-making", 1, "report.json", article("in-the-making", { title: MARK }), paths); for (const audience of ["public", "private"] as const) { await quietly(() => main({ paths, audience })); const files = filesUnder(PUBLIC); assert.ok(files.length > 0); assert.deepEqual(files.filter((f) => /iteration|in-the-making|turns\.jsonl|sweep\.md|calls(-text)?\.jsonl/.test(f)), [], `${audience}: no iteration file`); const leaked = files.filter((f) => /\.(json|html|md|txt|xml)$/.test(f) && readFileSync(path.join(PUBLIC, f), "utf8").includes(MARK)); assert.deepEqual(leaked, [], `${audience}: no iteration's words`); const index = readJson<{ reports: { id: string }[] }>(path.join(PUBLIC, "articles", "index.json")); assert.ok(!index.reports.some((r) => r.id === "in-the-making")); } });