// Integration (release 24 H1): a channel that leaves a site's published pool — // removed from `channels`, or moved to `citedChannels` — ships TOMBSTONES for // every per-channel path the site's last bundle served for it, through the // REAL index build and the REAL site compose over a temp corpus. // // The record of what the site published before is its last bundle, // `//out`; each test stands one in by copying the // composed public/ there, as the build stage's install does. // // Run with: node_modules/.bin/tsx --test common/bin/compose-site.departed.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; // Every path getPaths() can resolve to a place this file's code may write is // pinned under ROOT before anything calls it (buildIndex.test.ts's list). const ROOT = mkdtempSync(path.join(tmpdir(), "departed-")); 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); after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { buildIndex } = await import("../controller/buildIndex"); const { main: composeSite } = await import("./compose-site"); const { builtCitedOnlyProblem, isTombstoneChannelDir } = await import("../lib/builtExport"); const { lastBundleDir } = await import("../publish/tombstones"); const paths = getPaths(); const KEEP = "keeper"; const GONE = "departs"; const FRESH = "fresh-critic"; 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; // YouTube's rolling-caption shape (parseVtt keeps lines with inline timing). const VTT = "WEBVTT\nKind: captions\nLanguage: en\n\n" + "00:00:00.000 --> 00:00:05.000 align:start position:0%\n" + "First<00:00:01.000> caption<00:00:02.000> line.\n"; function videoChannel(slug: string, videos: number) { writeJson(path.join(paths.channelsDir, slug, "config.json"), { handling: "youtube", name: slug, url: `https://www.youtube.com/@${slug}/videos`, }); for (let i = 1; i <= videos; i++) { const id = `${slug}-v${i}`; const dir = path.join(paths.channelsDir, slug, "data", id); writeJson(path.join(dir, "metadata.info.json"), { id, title: `${slug} video ${i}`, channel: slug, upload_date: `2026060${i}`, duration: 120, webpage_url: `https://www.youtube.com/watch?v=${id}`, extractor_key: "Youtube", }); writeFileSync(path.join(dir, "transcript.en.vtt"), VTT); } } function site(pool: string[], cited: string[] = []) { writeJson(path.join(paths.sitesDir, "pub", "site.json"), { siteId: "pub", siteTitle: "pub", siteDescription: "fixture", headerTitle: "pub", homeTagline: "", socialLinks: [], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", channels: pool.map((slug) => ({ slug, groupId: "default" })), ...(cited.length > 0 ? { citedChannels: cited.map((slug) => ({ slug })) } : {}), siteUrl: "https://pub.example.test", archives: false, }); } const quiet = () => {}; async function indexAndCompose(): Promise { const log = console.log; console.log = quiet; try { await buildIndex({ paths, onLog: quiet }); await composeSite({ siteId: "pub", paths }); } finally { console.log = log; } } // The build stage's install, as far as compose can see it: public/ becomes // the site's bundle. function install(): string { const bundle = lastBundleDir(paths, "pub"); rmSync(bundle, { recursive: true, force: true }); mkdirSync(path.dirname(bundle), { recursive: true }); cpSync(paths.exportPublicDir, bundle, { recursive: true, dereference: true }); return bundle; } const pub = (...rel: string[]) => path.join(paths.exportPublicDir, ...rel); const TREES = ["transcripts", "subs", "posts", "digests"] as const; // The trees public/ holds for `slug`. const treesOf = (slug: string) => TREES.filter((t) => existsSync(pub(t, slug))); const pageFiles = (dir: string) => readdirSync(dir).filter((f) => f.startsWith("page-")).sort(); test("a channel removed from the pool ships a tombstone for every tree its last bundle served", async () => { videoChannel(KEEP, 1); videoChannel(GONE, 2); videoChannel(FRESH, 1); writeJson(paths.settingsFile, {}); site([KEEP, GONE]); await indexAndCompose(); // This fixture's index writes the channel's transcripts; its subs and // digests trees (two pages each) stand in for what a richer build served. assert.deepEqual(treesOf(GONE), ["transcripts"]); const bundle = install(); for (const tree of ["subs", "digests"]) { writeJson(path.join(bundle, tree, GONE, "manifest.json"), { version: 1, channelSlug: GONE, pageCount: 2, maxPageBytes: 4096, generatedAt: "2026-10-01T00:00:00.000Z", slugToPage: { [`${GONE}-v1`]: 0, [`${GONE}-v2`]: 1 }, ...(tree === "subs" ? { tracks: ["en"] } : { pageHashes: ["a", "b"] }), }); for (const page of ["page-0000.json", "page-0001.json"]) { writeJson(path.join(bundle, tree, GONE, page), [{ id: `${GONE}-v1`, text: "served" }]); } } const servedTrees = ["transcripts", "subs", "digests"]; const servedPages = Object.fromEntries( servedTrees.map((t) => [t, pageFiles(path.join(bundle, t, GONE))]), ); assert.deepEqual(servedPages.subs, ["page-0000.json", "page-0001.json"]); site([KEEP]); await indexAndCompose(); // Path for path: the same trees, the same page files, each empty. assert.deepEqual(treesOf(GONE), servedTrees); for (const tree of servedTrees) { const dir = pub(tree, GONE); assert.ok(isTombstoneChannelDir(dir), `${tree}/${GONE} is a tombstone`); assert.deepEqual(pageFiles(dir), servedPages[tree], tree); for (const page of pageFiles(dir)) assert.deepEqual(readJson(path.join(dir, page)), []); const m = readJson>(path.join(dir, "manifest.json")); assert.equal(m.channelSlug, GONE); if (tree === "subs") assert.deepEqual(m.tracks, []); assert.equal(m.pageHashes, undefined); assert.equal(m.pageCount, 0); assert.equal(m.maxPageBytes, 0); assert.deepEqual(m.slugToPage, {}); assert.ok(!readFileSync(path.join(dir, "manifest.json"), "utf8").includes(`${GONE}-v1`)); } // The member's trees are real; the departed channel is listed nowhere. assert.ok(!isTombstoneChannelDir(pub("transcripts", KEEP))); for (const file of ["corpus.json", "site.json", "summaries/manifest.json", "subs/manifest.json"]) { assert.ok(!readFileSync(pub(file), "utf8").includes(GONE), `${file} names ${GONE}`); } // …served no-store, each tree by its own rule, under the trees' CORS lines. const headers = readFileSync(pub("_headers"), "utf8"); assert.ok( headers.endsWith( "# Withdrawn content (tombstones): never stored at the edge.\n" + servedTrees.map((t) => `/${t}/${GONE}/*\n Cache-Control: no-store\n`).join(""), ), headers, ); // The bundle compose read is untouched by it. assert.ok(!isTombstoneChannelDir(path.join(bundle, "transcripts", GONE))); // Carried: a build over a bundle that holds the tombstones (never deployed, // say) ships them again. install(); await indexAndCompose(); assert.deepEqual(treesOf(GONE), servedTrees); for (const tree of servedTrees) assert.ok(isTombstoneChannelDir(pub(tree, GONE))); // Back in the pool: its real transcripts replace the tombstone and leave the // no-store block. The subs and digests it has no tree for now stay // tombstones — a path served once is never left to the edge's cache. install(); site([KEEP, GONE]); await indexAndCompose(); assert.ok(!isTombstoneChannelDir(pub("transcripts", GONE))); assert.ok(isTombstoneChannelDir(pub("subs", GONE))); assert.ok(readFileSync(pub("corpus.json"), "utf8").includes(GONE)); const back = readFileSync(pub("_headers"), "utf8"); assert.ok(!back.includes(`/transcripts/${GONE}/*`), back); assert.ok(back.includes(`/subs/${GONE}/*`), back); }); test("a channel moved to the cited scope is tombstoned; one never published is not; the guard passes", async () => { site([KEEP, GONE]); await indexAndCompose(); const servedTrees = treesOf(GONE); install(); // GONE moves to the cited scope; FRESH is cited too, and was never in the // pool, so no bundle served anything for it. site([KEEP], [GONE, FRESH]); await indexAndCompose(); assert.deepEqual(treesOf(GONE), servedTrees); for (const tree of servedTrees) assert.ok(isTombstoneChannelDir(pub(tree, GONE)), tree); assert.deepEqual(treesOf(FRESH), [], "a channel the site never published gets nothing"); const headers = readFileSync(pub("_headers"), "utf8"); assert.ok(!headers.includes(FRESH)); for (const file of ["corpus.json", "site.json"]) { assert.ok(!readFileSync(pub(file), "utf8").includes(GONE), `${file} names ${GONE}`); } // A tombstone is not a leak: the cited-only guard passes over the // tombstoned bundle… const cited = { siteId: "pub", channels: [{ slug: KEEP }], citedChannels: [{ slug: GONE }, { slug: FRESH }], }; assert.equal(builtCitedOnlyProblem(cited, paths.exportPublicDir), null); // …and still refuses the corpus itself. assert.match( builtCitedOnlyProblem(cited, lastBundleDir(paths, "pub")) ?? "", /cites "departs", "fresh-critic" only \(citedChannels\), but .* publishes its corpus: transcripts\/departs\//, ); }); test("no bundle, or another site's, is no record: nothing is tombstoned", async () => { site([KEEP, GONE]); await indexAndCompose(); const bundle = install(); // A bundle that names another site is not this site's record. writeJson(path.join(bundle, "site.json"), { siteId: "elsewhere" }); site([KEEP]); await indexAndCompose(); assert.deepEqual(treesOf(GONE), []); assert.ok(!readFileSync(pub("_headers"), "utf8").includes("no-store")); rmSync(path.dirname(bundle), { recursive: true, force: true }); site([KEEP, GONE]); await indexAndCompose(); site([KEEP]); await indexAndCompose(); assert.deepEqual(treesOf(GONE), []); });