// import-post (release 27 S3b): one post by its URL into the posts archive of // a channel of that platform, through a fake fetcher. Every name is invented. // // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test social/importPost.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import type { Post } from "../lib/posts"; const ROOT = mkdtempSync(path.join(os.tmpdir(), "import-post-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); writeFileSync(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0 }) + "\n"); after(() => rmSync(ROOT, { recursive: true, force: true })); // A fake gallery-dl: records its argv, prints the status it is asked for and // the post that status quotes, as gallery-dl's jsonl tuples (ids unquoted). const FAKE_BIN = path.join(ROOT, "fake-gallery-dl.mjs"); const ARGV_FILE = path.join(ROOT, "fake-gallery-dl.argv.json"); writeFileSync( FAKE_BIN, `#!/usr/bin/env node import { writeFileSync } from "node:fs"; writeFileSync(${JSON.stringify(ARGV_FILE)}, JSON.stringify(process.argv.slice(2))); const tweet = (id, name, content) => JSON.stringify([2, { tweet_id: id, conversation_id: id, date: "2026-02-11 12:00:00", content, author: { name, nick: name }, user: { name, nick: name } }]).replace(/"tweet_id":"(\\d+)"/, '"tweet_id":$1'); console.log(tweet("1700000000000000077", "synthbaker", "The second shop opens in the spring.")); console.log(tweet("1700000000000000066", "someoneelse", "a quoted post")); `, { mode: 0o755 }, ); process.env.GALLERY_DL_BIN = FAKE_BIN; const channel = (slug: string, config: Record) => { mkdirSync(path.join(ROOT, "channels", slug), { recursive: true }); writeFileSync(path.join(ROOT, "channels", slug, "config.json"), JSON.stringify(config)); }; channel("synth-social", { name: "Synth (X)", handling: "transcribe", sourceKind: "social", url: "https://x.com/synthbaker", postFetcher: "fake-x" }); channel("synth-sky", { name: "Synth (Bluesky)", handling: "transcribe", sourceKind: "social", url: "https://bsky.app/profile/synthbaker.bsky.social", postFetcher: "fake-bsky" }); channel("synth-kitchen", { name: "Synth Kitchen", handling: "youtube", url: "https://video.example/@synth-kitchen" }); const { getPaths } = await import("../lib/paths"); const { readAllPosts } = await import("../lib/posts-server"); const { parsePostUrl } = await import("./fetchers"); // X is asked only inside a job of the editor's, on X's queue (lib/xJobRule.ts): these calls stand where one does. const { importPost: importPostDirect, planImportPost, NOT_A_POST_URL } = await import("./importPost"); const { insideEditorJob } = await import("../testing/jobs/asEditorJob"); const importPost = insideEditorJob(importPostDirect); const { xGalleryDlFetcher } = await import("./xGalleryDlFetcher"); const { readFileSync } = await import("node:fs"); type Fetcher = import("./fetchers").SocialFetcher; const paths = getPaths(); const post = (id: string, author: string, platform: Post["platform"] = "twitter"): Post => ({ id, slug: `synth-social/${id}`, channelSlug: "synth-social", author, createdAt: "2026-02-11T12:00:00.000Z", uploadDate: "20260211", text: "No, I did not enter the pie contest.", url: `https://x.com/${author}/status/${id}`, platform, isReply: false, isRepost: false, links: [], }); function fakeFetcher(platform: "twitter" | "bluesky", answer: (id: string) => Post | null, one = true) { const asked: string[] = []; const f: Fetcher = { id: `fake-${platform}`, label: `Fake ${platform}`, platform, detect: () => false, fields: {}, probe: async () => ({ ok: true }), fetch: async () => ({ posts: [], complete: true }), ...(one ? { fetchOne: async ({ post: ref }) => { asked.push(ref.url); return { post: answer(ref.id) }; }, } : {}), }; return { f, asked }; } test("post URLs: X statuses and Bluesky posts, nothing else", () => { assert.deepEqual(parsePostUrl("https://twitter.com/SynthBaker/status/1700000000000000001?s=20"), { platform: "twitter", id: "1700000000000000001", handle: "SynthBaker", url: "https://x.com/SynthBaker/status/1700000000000000001", }); assert.deepEqual(parsePostUrl("https://x.com/i/web/status/17"), { platform: "twitter", id: "17", url: "https://x.com/i/web/status/17" }); assert.deepEqual(parsePostUrl("https://bsky.app/profile/synthbaker.bsky.social/post/3kabc"), { platform: "bluesky", id: "3kabc", actor: "synthbaker.bsky.social", url: "https://bsky.app/profile/synthbaker.bsky.social/post/3kabc", }); assert.equal(parsePostUrl("at://did:plc:abc/app.bsky.feed.post/3kxyz")?.platform, "bluesky"); for (const bad of ["https://x.com/synthbaker", "https://x.com/a/status/notanid", "https://video.example/v/1", "nonsense"]) { assert.equal(parsePostUrl(bad), null, bad); } }); test("refused before any read: not a post URL, not a posts channel, another platform's channel, another account's post, a fetcher that cannot", async () => { const { f: x, asked } = fakeFetcher("twitter", () => null); const { f: noOne } = fakeFetcher("twitter", () => null, false); const plan = (slug: string, url: string, fetcher = x) => planImportPost(paths, slug, url, { fetcherFor: () => fetcher }); assert.deepEqual(await plan("synth-social", "https://video.example/v/1"), { ok: false, error: NOT_A_POST_URL }); assert.match((await plan("synth-kitchen", "https://x.com/synthbaker/status/1") as { error: string }).error, /synth-kitchen is not a posts channel/); assert.match((await plan("nope", "https://x.com/synthbaker/status/1") as { error: string }).error, /No such channel: nope/); assert.match( (await plan("synth-social", "https://bsky.app/profile/synthbaker.bsky.social/post/3kabc") as { error: string }).error, /synth-social is an X posts channel, not a Bluesky one: import a Bluesky post into a Bluesky channel/, ); assert.match((await plan("synth-social", "https://x.com/someoneelse/status/1") as { error: string }).error, /the post is @someoneelse's; synth-social archives @synthbaker's posts/); assert.match((await plan("synth-social", "https://x.com/synthbaker/status/1", noOne) as { error: string }).error, /cannot import one post/); assert.equal((await plan("synth-social", "https://x.com/SYNTHBAKER/status/1")).ok, true, "handles compare without case"); assert.deepEqual(asked, []); }); test("an X post: read through the channel's fetcher and written to its archive; a second import reads nothing", async () => { const { f, asked } = fakeFetcher("twitter", (id) => post(id, "synthbaker")); const logs: string[] = []; const r = await importPost({ paths, slug: "synth-social", url: "https://x.com/synthbaker/status/1700000000000000001", settings: {}, onLog: (l) => logs.push(l), fetcherFor: () => f, }); assert.equal(r.ok, true, r.error); assert.equal(r.written, 1); assert.deepEqual(r.post, { id: "1700000000000000001", url: "https://x.com/synthbaker/status/1700000000000000001", author: "synthbaker", createdAt: "2026-02-11T12:00:00.000Z" }); assert.deepEqual(asked, ["https://x.com/synthbaker/status/1700000000000000001"]); const archived = await readAllPosts(path.join(ROOT, "channels", "synth-social")); assert.deepEqual(archived.map((p) => p.id), ["1700000000000000001"]); const again = await importPost({ paths, slug: "synth-social", url: "https://x.com/synthbaker/status/1700000000000000001", settings: {}, fetcherFor: () => f }); assert.equal(again.already, true); assert.equal(asked.length, 1, "not read again"); }); test("an X status URL with no handle: the post's author is checked after the read, and another account's is not written", async () => { const { f } = fakeFetcher("twitter", (id) => post(id, "someoneelse")); const r = await importPost({ paths, slug: "synth-social", url: "https://x.com/i/web/status/1700000000000000009", settings: {}, fetcherFor: () => f }); assert.equal(r.ok, false); assert.match(r.error!, /the post is @someoneelse's; synth-social archives @synthbaker's posts — nothing written/); const archived = await readAllPosts(path.join(ROOT, "channels", "synth-social")); assert.ok(!archived.some((p) => p.id === "1700000000000000009")); }); test("a post the platform does not have is a failure, in words", async () => { const { f } = fakeFetcher("bluesky", () => null); const r = await importPost({ paths, slug: "synth-sky", url: "https://bsky.app/profile/synthbaker.bsky.social/post/3kgone", settings: {}, fetcherFor: () => f }); assert.equal(r.ok, false); assert.match(r.error!, /no post 3kgone/); }); test("X through gallery-dl: the status URL is what it reads, and the one post it names is picked out of what it printed", async () => { const ref = parsePostUrl("https://x.com/synthbaker/status/1700000000000000077")!; const r = await xGalleryDlFetcher.fetchOne!({ post: ref, channelSlug: "synth-social", signal: new AbortController().signal }); assert.equal(r.error, undefined); assert.equal(r.post?.id, "1700000000000000077"); assert.equal(r.post?.author, "synthbaker"); assert.equal(r.post?.text, "The second shop opens in the spring."); const argv = JSON.parse(readFileSync(ARGV_FILE, "utf8")) as string[]; assert.equal(argv[argv.length - 1], "https://x.com/synthbaker/status/1700000000000000077"); assert.ok(argv.includes("--no-download"), "metadata only"); // And through the import, on a channel whose fetcher is gallery-dl's. channel("synth-gdl", { name: "Synth (X)", handling: "transcribe", sourceKind: "social", url: "https://x.com/synthbaker" }); const imp = await importPost({ paths, slug: "synth-gdl", url: "https://x.com/synthbaker/status/1700000000000000077", settings: {} }); assert.equal(imp.ok, true, imp.error); const archived = await readAllPosts(path.join(ROOT, "channels", "synth-gdl")); assert.deepEqual(archived.map((p) => p.id), ["1700000000000000077"], "the quoted post is not written"); }); // Added scope R2 (lane x-followups): where X is about to be asked, only a job on X's queue stands. test("R2a: importing an X post outside the editor's job, or in a job on another queue, asks the fetcher nothing; a Bluesky post is not X's", async () => { const { asEditorJob } = await import("../testing/jobs/asEditorJob"); const { f, asked } = fakeFetcher("twitter", (id) => post(id, "synthbaker")); const ask = { paths, slug: "synth-social", url: "https://x.com/synthbaker/status/1700000000000000555", settings: {}, fetcherFor: () => f }; const outside = await importPostDirect(ask); assert.equal(outside.ok, false); assert.match(outside.error ?? "", /^Importing https:\/\/x\.com\/synthbaker\/status\/1700000000000000555 asks X, and X is asked only by the editor's own jobs .*Queue it instead: pnpm ops import-post/); const elsewhere = await asEditorJob(() => importPostDirect(ask), { queueKey: "platform:youtube" }); assert.match(elsewhere.error ?? "", /X jobs run on X's queue only \(platform:twitter\): the queue "platform:youtube" is refused/); assert.deepEqual(asked, []); // Bluesky has no such rule: read with no job at all. const sky = fakeFetcher("bluesky", () => null); const r = await importPostDirect({ paths, slug: "synth-sky", url: "https://bsky.app/profile/synthbaker.bsky.social/post/3kother", settings: {}, fetcherFor: () => sky.f }); assert.doesNotMatch(r.error ?? "", /asks X/); });