// THE REPORT DOOR IS SHUT ON AN X TRENDING LIBRARY (lane x-trending, review 1 // MUST 1). An article may cite any channel of the corpus, and a draft resolves // an X URL to whichever channel holds the post — so, before this, a library's // post could be composed into the homepage's public dir, even unasked. // // Pinned here over a temp corpus, with a marker in the library's post, and the // post ALSO planted where an ordinary posts channel keeps its posts (so the // refusals are tested, not only the fact that a library keeps its posts under // trending/, where no ordinary reader looks): // - both report hosts refuse a library as citable; // - composing an article that cites a library post is refused, and nothing // of the post is written; // - no site may carry a library's posts by the visibility rule, private // sites included; // - a draft's X URL never resolves to a library; // - a research run is never shown a library. // // Run with: node_modules/.bin/tsx --test publish/xTrendingReportDoor.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(), "x-trending-report-door-")); 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, readAllPosts } = await import("../lib/posts-server"); const { setPublished, articleDir } = await import("../lib/report/registry"); const { articlesReportHost, siteReportHost } = await import("./reportHost"); const { composeHostReports, ComposeReportsError } = await import("./composeReports"); const { postsVisibleTo } = await import("../lib/postsVisibility"); const { diskPostChannelOf, diskPostOf } = await import("../lib/report/convert-server"); const { editorChannelsOf } = await import("../controller/reportDraft"); const { writeXTrendingPosts, readXTrendingPosts } = await import("../lib/xTrending/store"); const { X_TRENDING_NOT_CITABLE } = await import("../lib/xTrending/config"); const paths = getPaths(); const OUT = path.join(ROOT, "homepage-public"); const LIB = "trending-lib"; const ACCOUNT = "kettle-org-x"; const LIB_ID = "1900000000000006001"; const SHARED_ID = "1900000000000006002"; const MARK = "trenddoormarkerq7x"; const HANDLE = "trenddoorhandlez4k"; const writeJson = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value, null, 2)); }; const post = (channelSlug: string, id: string, author: string, text: string) => ({ id, slug: `${channelSlug}/${id}`, channelSlug, author, createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text, url: `https://x.com/${author}/status/${id}`, platform: "twitter" as const, isReply: false, isRepost: false, links: [], }); const LIBRARY_CONFIG = { handling: "transcribe", sourceKind: "social", platform: "twitter", name: "Trending", xTrending: {} }; writeJson(paths.settingsFile, {}); const libRoot = path.join(paths.channelsDir, LIB); writeJson(path.join(libRoot, "config.json"), LIBRARY_CONFIG); const libPosts = [post(LIB, LIB_ID, HANDLE, `a trending post ${MARK}`), post(LIB, SHARED_ID, "kettle_org", `shared ${MARK}`)]; await writeXTrendingPosts(libRoot, libPosts); // And where an ordinary posts channel keeps them (a hand copy; an older library). await writePosts(libRoot, libPosts); const accountRoot = path.join(paths.channelsDir, ACCOUNT); writeJson(path.join(accountRoot, "config.json"), { handling: "transcribe", sourceKind: "social", platform: "twitter", name: "Kettle Org (X)", url: "https://x.com/kettle_org", socialHandle: "kettle_org", }); await writePosts(accountRoot, [post(ACCOUNT, SHARED_ID, "kettle_org", "the account's own copy")]); // Lane x-trending-2: the library's first post is KEPT and CAPTURED (a note, // a shot, a media file in the store, its context) and its topic watched. No // report door may give out any of it either. const { recordXTrendingRead, readXTrendingTopics } = await import("../lib/xTrending/store"); const { plantSlice2Library, SLICE2_CAPTURES_DIRNAME, SLICE2_FILE_NAMES, SLICE2_MARK_LIST } = await import("../lib/xTrending/__fixtures__/slice2Library"); await recordXTrendingRead(libRoot, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: { trendId: "1900000000000000001", url: "https://x.com/i/trending/1900000000000000001" }, topic: "Kettles", postIds: [LIB_ID, SHARED_ID], added: 2, }); await plantSlice2Library(libRoot, { postId: LIB_ID, topicId: (await readXTrendingTopics(libRoot))[0].id }); // Nothing lane x-trending-2 leaves in a library is in what a report door wrote. function assertNoSlice2(files: { rel: string; text: string }[], where: string): void { for (const f of files) { for (const mark of SLICE2_MARK_LIST) assert.ok(!f.text.includes(mark), `${where}: ${f.rel} holds ${mark}`); for (const name of SLICE2_FILE_NAMES) assert.ok(!f.rel.split(path.sep).includes(name), `${where}: wrote ${f.rel}`); } } function filesUnder(dir: string): { rel: string; text: string }[] { const out: { rel: string; text: string }[] = []; const walk = (d: string) => { for (const e of readdirSync(d, { withFileTypes: true })) { const p = path.join(d, e.name); if (e.isDirectory()) walk(p); else out.push({ rel: path.relative(dir, p), text: readFileSync(p).toString("latin1") }); } }; if (existsSync(dir)) walk(dir); return out; } test("a library keeps its posts under trending/: where a posts channel's readers look, it has none of its own making", async () => { const fresh = path.join(paths.channelsDir, "fresh-lib"); writeJson(path.join(fresh, "config.json"), LIBRARY_CONFIG); await writeXTrendingPosts(fresh, [post("fresh-lib", "1900000000000006009", HANDLE, MARK)]); assert.equal((await readXTrendingPosts(fresh)).length, 1); assert.equal((await readAllPosts(fresh)).length, 0); assert.equal(existsSync(path.join(fresh, "posts")), false); assert.equal(existsSync(path.join(fresh, "posts-archive")), false); assert.equal(existsSync(path.join(fresh, "trending", "posts")), true); rmSync(fresh, { recursive: true, force: true }); }); test("both report hosts refuse a library as citable", async () => { const articles = await articlesReportHost(paths, { audience: "private" }); assert.equal(articles.citableProblem(LIB), X_TRENDING_NOT_CITABLE(LIB)); assert.equal(articles.citableProblem(ACCOUNT), null); // A site whose site.json, written by hand, names the library. const site = { siteId: "priv", siteTitle: "priv", audience: "private", pages: [], groups: [{ id: "default", name: "All", selectedByDefault: true }], defaultGroupId: "default", channels: [{ slug: ACCOUNT, groupId: "default" }, { slug: LIB, groupId: "default" }], }; const host = siteReportHost(paths, site as never); assert.equal(host.citableProblem(LIB), X_TRENDING_NOT_CITABLE(LIB)); assert.equal(host.citableProblem(ACCOUNT), null); assert.match(X_TRENDING_NOT_CITABLE(LIB), /is an X trending library: it is private/); }); test("no site may carry a library's posts, a private one included, whatever social.x.visibility says", () => { const priv = { audience: "private" as const }; assert.equal(postsVisibleTo(priv, LIBRARY_CONFIG as never, {}), false); assert.equal(postsVisibleTo(priv, LIBRARY_CONFIG as never, { social: { x: { visibility: "public" } } }), false); // The marker alone decides: a library whose kind was edited by hand is still one. assert.equal(postsVisibleTo(priv, { ...LIBRARY_CONFIG, sourceKind: "video" } as never, {}), false); assert.equal(postsVisibleTo(priv, { sourceKind: "social", platform: "twitter" } as never, {}), true); }); test("composing an article that cites a library post is refused, and nothing of the post is published", async () => { writeJson(path.join(articleDir(paths, "art-lib"), "report.json"), { format: "archilyzer-report", version: 1, id: "art-lib", kind: "sweep", title: "Article citing a library", sections: [{ id: "s1", title: "What was said", body: "It was said: [p01](cite:p01)." }], citations: { p01: { kind: "post", channel: LIB, id: LIB_ID, quote: "a trending post" } }, }); await setPublished("art-lib", true, {}, paths); for (const audience of ["private", "public"] as const) { const host = await articlesReportHost(paths, { audience, published: ["art-lib"] }); await assert.rejects(composeHostReports({ paths, host, publicDir: OUT, settings: {}, log: () => {} }), (e: unknown) => { assert.ok(e instanceof ComposeReportsError, String(e)); assert.deepEqual( e.problems.map((p) => [p.kind, p.citation, p.message]), [["not-in-site", "art-lib#p01", X_TRENDING_NOT_CITABLE(LIB)]], ); return true; }); } for (const dir of [OUT, paths.exportPublicDir]) { for (const f of filesUnder(dir)) { assert.ok(!f.text.includes(MARK) && !f.text.includes(HANDLE), `${f.rel} holds the library's post`); } } }); test("a draft's X URL never resolves to a library, and a library's post is not read as a record", async () => { const channelOf = diskPostChannelOf(paths.channelsDir); // Only the library holds it: unresolved. assert.equal(await channelOf({ platform: "x", id: LIB_ID, handle: HANDLE }), null); // The library and an account channel hold it: the account's, never the library's. assert.equal(await channelOf({ platform: "x", id: SHARED_ID, handle: "kettle_org" }), ACCOUNT); assert.equal(await channelOf({ platform: "x", id: SHARED_ID, handle: "trending" }), ACCOUNT); const postOf = diskPostOf(paths.channelsDir); assert.equal(await postOf(LIB, LIB_ID), null); assert.ok(await postOf(ACCOUNT, SHARED_ID)); }); test("a research run is never shown a library among the editor's channels", async () => { const channels = await editorChannelsOf(paths); assert.deepEqual(channels.map((c) => c.slug), [ACCOUNT]); }); // --- review 2 N8a: the refusals BEHIND the host's --------------------------- // // A host that lets the library through (a stub: the real hosts refuse it // first) reaches the second line in composeReports and the two in // reportMedia. Each must refuse by the library's own sentence. const { prepareReportMedia, missingEvidenceWindows } = await import("./reportMedia"); async function lenientHost(published: string[]) { const host = await articlesReportHost(paths, { audience: "private", published }); return { ...host, citableProblem: () => null }; } test("N8a: behind a host that lets it through, compose still refuses a library's post by name", async () => { const host = await lenientHost(["art-lib"]); await assert.rejects(composeHostReports({ paths, host, publicDir: OUT, settings: {}, log: () => {} }), (e: unknown) => { assert.ok(e instanceof ComposeReportsError, String(e)); assert.deepEqual( e.problems.map((p) => [p.kind, p.citation, p.message]), [["not-visible", "art-lib#p01", X_TRENDING_NOT_CITABLE(LIB)]], ); return true; }); for (const f of filesUnder(OUT)) assert.ok(!f.text.includes(MARK) && !f.text.includes(HANDLE), `${f.rel} holds the library's post`); assertNoSlice2(filesUnder(OUT), "compose"); }); test("N8a: behind such a host, preparing media refuses a library's post, and no window of a library is planned", async () => { const host = await lenientHost(["art-lib"]); const index = await prepareReportMedia({ paths, host, settings: {} }); assert.deepEqual( index.problems.map((p) => [p.kind, p.moment, p.message]), [["not-visible", `${LIB}/${LIB_ID}`, X_TRENDING_NOT_CITABLE(LIB)]], ); assert.deepEqual(Object.keys(index.moments), []); // The post is CAPTURED now (a shot and a media file under trending/): no // report media is prepared from it all the same, and nothing of it is written. assert.ok(!JSON.stringify(index).includes(SLICE2_CAPTURES_DIRNAME), "the media index names a capture"); for (const mark of SLICE2_MARK_LIST) assert.ok(!JSON.stringify(index).includes(mark), `the media index holds ${mark}`); assertNoSlice2(filesUnder(OUT), "report media"); // A span cited on a library (a config edited by hand could carry videos). writeJson(path.join(articleDir(paths, "art-lib-span"), "report.json"), { format: "archilyzer-report", version: 1, id: "art-lib-span", kind: "sweep", title: "Article citing a span of a library", sections: [{ id: "s1", title: "What was said", body: "It was said: [c01](cite:c01)." }], citations: { c01: { kind: "video", channel: LIB, id: "vid00000001", start: 10, end: 20, quote: "said on a recording" } }, }); await setPublished("art-lib-span", true, {}, paths); const windows = await missingEvidenceWindows(paths, await lenientHost(["art-lib-span"]), { settings: {} as never }); assert.deepEqual(windows.missing, []); assert.deepEqual( windows.unfetchable.map((w) => [w.slug, w.id, w.why, w.message]), [[LIB, "vid00000001", "not-in-site", X_TRENDING_NOT_CITABLE(LIB)]], ); }); // Every call of `fn` in a source, with its WHOLE argument text (parentheses balanced, so a call whose // first argument is itself a call — `listXTrending(path.join(a, b), { detail: true })` — is read to its end). function callsOf(text: string, fn: string): string[] { const out: string[] = []; for (let at = text.indexOf(`${fn}(`); at >= 0; at = text.indexOf(`${fn}(`, at + 1)) { let depth = 0; let end = -1; for (let i = at + fn.length; i < text.length; i++) { if (text[i] === "(") depth++; else if (text[i] === ")" && --depth === 0) { end = i; break; } } out.push(text.slice(at + fn.length + 1, end < 0 ? text.length : end)); } return out; } // The judge view's asks of the listing: no publishing source may make one. const JUDGE_VIEW_ASKS = /\b(detail|ids|filter|offset|seen)\b\s*[:,}]/; function judgeViewAsk(text: string): string | null { for (const args of callsOf(text, "listXTrending")) { // The options are everything after the first argument (the library's root). if (JUDGE_VIEW_ASKS.test(args)) return `listXTrending(${args.replace(/\s+/g, " ").slice(0, 120)})`; } return null; } // Lane posts-library: the judge view's reads (the detail listing) are the one // place a kept post's note and its capture's context are given out. No report // door wrote them, and no report module can ask for them or for the purge. test("posts-library: the detail listing holds the note and the context; what the report doors wrote holds neither, and no report source names the judge view's reads or the purge", async () => { const { listXTrending } = await import("../lib/xTrending/store"); const { SLICE2_MARKS } = await import("../lib/xTrending/__fixtures__/slice2Library"); const detail = JSON.stringify(await listXTrending(libRoot, { detail: true })); assert.ok(detail.includes(SLICE2_MARKS.note) && detail.includes(SLICE2_MARKS.context), "the judge view's listing carries them"); assert.ok(!detail.includes(SLICE2_MARKS.address), "and never a media address"); assertNoSlice2(filesUnder(paths.exportPublicDir), "the public dir, after the judge view's read"); const here = path.dirname(new URL(import.meta.url).pathname); const sources = filesUnder(here).filter((f) => /\.(ts|mjs)$/.test(f.rel) && !f.rel.includes(".test.")); assert.ok(sources.length > 5, "the publish sources were read"); for (const f of sources) { for (const word of ["xTrending/purge", "purgeXTrendingDropped", "removeXTrendingPostCapture", "summarizeXTrending", "xTrendingCaptureFiles"]) { assert.ok(!f.text.includes(word), `publish/${f.rel} names ${word}`); } assert.equal(judgeViewAsk(f.text), null, `publish/${f.rel} asks the listing for the judge view's fields`); } // The sweep catches the shape a real caller has (the root built by a call, the options after it). assert.ok(judgeViewAsk("await listXTrending(path.join(paths.channelsDir, slug), {\n detail: true,\n});")); assert.ok(judgeViewAsk("listXTrending(root(x), { topic, ids: true })")); assert.equal(judgeViewAsk("listXTrending(path.join(a, b), { topic, limit })"), null); });