// A POSTS CHANNEL WITH NO SOURCE — posts published from a private library // (lane x-trending-publish) — MAKES NO REQUEST ANYWHERE: every path that // would fetch, capture or check it refuses it by name with one sentence, and // every list of channels to run leaves it out. No fetcher is resolved, no // binary spawned, no browser opened (the suite also runs with the network // refused: lib/noNetwork.mjs). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/sourcelessPosts.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { chmod, mkdtemp, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "sourceless-posts-")); const TOUCHED = path.join(ROOT, "a-binary-ran"); const BIN = path.join(ROOT, "fake-bin.mjs"); await writeFile(BIN, `#!/usr/bin/env node\nimport { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(TOUCHED)}, "ran");\n`); await chmod(BIN, 0o755); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); process.env.GALLERY_DL_BIN = BIN; process.env.YT_DLP_BIN = BIN; const { getPaths } = await import("../lib/paths"); const { writePosts } = await import("../lib/posts-server"); const { isSourcelessPostsChannel, SOURCELESS_POSTS_CHANNEL, syncSkipForShape } = await import("../lib/sourcelessPosts"); const { registerSocialFetcher } = await import("../social/fetchers"); const { createChannel } = await import("./channels"); const { capturePosts } = await import("./capturePosts"); const { checkPostAvailability } = await import("./checkPostAvailability"); const { fetchPosts } = await import("./fetchPosts"); const { isXPostsChannel, listXRunCandidates, planXRun } = await import("./xRun"); const { findVideoSourceUrl } = await import("./undownloadedVideos"); const { recordWebpageUrl } = await import("../publish/findCopy"); const { planImportPost } = await import("../social/importPost"); const { defaultWebpageUrl } = await import("../lib/platform"); const { readChannelConfig } = await import("./channels"); const paths = getPaths(); const settings = {} as never; // Every fetcher the registry could hand out, replaced by one that counts any // call made on it. let fetcherCalls = 0; const spy = (id: string) => new Proxy({ id, label: id, platform: "twitter", fields: {} } as Record, { get(target, prop) { if (prop in target || typeof prop === "symbol") return target[prop as string]; return () => { fetcherCalls++; throw new Error(`fetcher ${id}.${String(prop)} was called`); }; }, has: () => true, }); for (const id of ["x-gallery-dl", "x-playwright", "bluesky", "x"]) registerSocialFetcher(spy(id) as never); const PUBLISHED = { name: "Kettle Festival", handling: "transcribe", sourceKind: "social", platform: "twitter" } as const; await createChannel(paths, "kettle-festival", { ...PUBLISHED }); await createChannel(paths, "teapot-x", { name: "Teapot (X)", handling: "transcribe", sourceKind: "social", platform: "twitter", url: "https://x.com/teapot_news", socialHandle: "teapot_news", postFetcher: "x-gallery-dl" }); await writePosts(path.join(paths.channelsDir, "kettle-festival"), [ { id: "801", slug: "kettle-festival/801", channelSlug: "kettle-festival", author: "teapot_news", createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text: "words", url: "https://x.com/teapot_news/status/801", platform: "twitter", isReply: false, isRepost: false, links: [] }, ]); // The same channel after some hand gave it a fetcher key. await createChannel(paths, "kettle-keyed", { ...PUBLISHED, postFetcher: "x-gallery-dl" }); await writePosts(path.join(paths.channelsDir, "kettle-keyed"), [ { id: "801", slug: "kettle-keyed/801", channelSlug: "kettle-keyed", author: "teapot_news", createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text: "words", url: "https://x.com/teapot_news/status/801", platform: "twitter", isReply: false, isRepost: false, links: [] }, ]); const SENTENCE = SOURCELESS_POSTS_CHANNEL("kettle-festival"); test("the shape: an X posts channel with no account, search, page or marker — and no other", () => { assert.equal(isSourcelessPostsChannel(PUBLISHED), true); for (const other of [ { ...PUBLISHED, url: "https://x.com/teapot_news" }, { ...PUBLISHED, socialHandle: "teapot_news" }, { ...PUBLISHED, xSearch: { query: "kettles" } }, { ...PUBLISHED, xTrending: {} }, { ...PUBLISHED, platform: "bluesky" }, { ...PUBLISHED, sourceKind: "video" }, { ...PUBLISHED, remoteOnly: true }, { ...PUBLISHED, platform: "webpage", wayback: { targets: [{ url: "https://example.org", match: "exact" }] } }, null, ]) { assert.equal(isSourcelessPostsChannel(other as never), false, JSON.stringify(other)); } // A FETCHER KEY IS NOT A SOURCE (review 1 M2): writing one cannot switch the guards off. assert.equal(isSourcelessPostsChannel({ ...PUBLISHED, postFetcher: "x-gallery-dl" } as never), true); assert.match(SENTENCE, /^kettle-festival holds posts published from a private library: it has no account to fetch from/); }); test("a direct ask is refused with the sentence: fetch-posts, capture-posts, the availability check, an X run", async () => { const fetched = await fetchPosts({ paths, slug: "kettle-festival", settings } as never); assert.deepEqual([fetched.ok, fetched.error, fetched.written], [false, SENTENCE, 0]); for (const extra of [{}, { full: true }, { limit: 5 }]) { const r = await fetchPosts({ paths, slug: "kettle-festival", settings, ...extra } as never); assert.equal(r.error, SENTENCE); } for (const extra of [{}, { avatarsOnly: true }, { contextOnly: true }, { force: true, thread: true }]) { const captured = await capturePosts({ paths, slug: "kettle-festival", settings, ids: ["801"], ...extra } as never); assert.deepEqual([captured.ok, captured.error, captured.outcomes], [false, SENTENCE, []], JSON.stringify(extra)); } for (const mode of ["stale", "all"]) { const checked = await checkPostAvailability({ paths, slug: "kettle-festival", mode } as never); assert.deepEqual([checked.ok, checked.error, checked.checked], [false, SENTENCE, 0]); } const run = await planXRun(paths, { channels: ["teapot-x", "kettle-festival"] }); assert.deepEqual(run, { ok: false, error: SENTENCE }); assert.equal(fetcherCalls, 0, "no fetcher was called"); assert.equal(existsSync(TOUCHED), false, "no binary was run"); }); test("where channels are listed it is left out, with its reason: an X run's candidates, Sync all", async () => { assert.equal(isXPostsChannel(PUBLISHED), false); assert.deepEqual((await listXRunCandidates(paths)).map((c) => c.slug), ["teapot-x"]); assert.equal(syncSkipForShape(PUBLISHED), "published posts: no account to fetch from"); assert.equal(syncSkipForShape({ handling: "transcribe" } as never), "no url"); assert.equal(syncSkipForShape({ url: "https://example.org/c", remoteOnly: true } as never), "remote-only"); assert.equal(syncSkipForShape({ url: "https://x.com/teapot_news", sourceKind: "social", platform: "twitter" }), null); assert.equal(fetcherCalls, 0); assert.equal(existsSync(TOUCHED), false); }); // Review 1 M2. test("a fetcher key written onto it changes nothing: every door still refuses, and no fetcher is called", async () => { const says = SOURCELESS_POSTS_CHANNEL("kettle-keyed"); assert.equal((await fetchPosts({ paths, slug: "kettle-keyed", settings } as never)).error, says); assert.equal((await capturePosts({ paths, slug: "kettle-keyed", settings, ids: ["801"] } as never)).error, says); assert.equal((await checkPostAvailability({ paths, slug: "kettle-keyed" } as never)).error, says); assert.deepEqual(await planXRun(paths, { channels: ["kettle-keyed"] }), { ok: false, error: says }); assert.deepEqual((await listXRunCandidates(paths)).map((c) => c.slug), ["teapot-x"]); assert.equal(syncSkipForShape({ ...PUBLISHED, postFetcher: "x-gallery-dl" } as never), "published posts: no account to fetch from"); assert.equal(fetcherCalls, 0); assert.equal(existsSync(TOUCHED), false); }); // Review 1 S1. test("no source URL is ever built for one of its posts, and import-post is refused; an account channel answers as before", async () => { for (const slug of ["kettle-festival", "kettle-keyed"]) { assert.equal(await findVideoSourceUrl(paths, slug, "801"), null, `${slug}, config read`); assert.equal(await findVideoSourceUrl(paths, slug, "801", (await readChannelConfig(paths, slug))!), null, `${slug}, config handed in`); assert.equal(await recordWebpageUrl(paths, slug, "801"), null, `${slug}: nothing for a Wayback lookup`); const imported = await planImportPost(paths, slug, "https://x.com/teapot_news/status/1900000000000001001"); assert.deepEqual(imported, { ok: false, error: SOURCELESS_POSTS_CHANNEL(slug) }); } // CONTROL: an X account channel still gets the address built from its platform and the id. const built = defaultWebpageUrl("twitter", "801"); assert.ok(built, "the platform builds an address from an id"); assert.equal(await findVideoSourceUrl(paths, "teapot-x", "801", (await readChannelConfig(paths, "teapot-x"))!), built); assert.equal(fetcherCalls, 0); });