import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/sync/route.test.ts" // // A SOCIAL CHANNEL'S SYNC IS ITS POST FETCH (UI notes 2026-10-30, #17): Sync on // an X channel ran yt-dlp against the profile and failed. Every case here is a // refusal answered before any job, from a temp corpus: the X channel has no // cookie jar, so fetch-posts' own guard refuses it in its own sentence — which // is how this knows the sync went to fetch-posts and not the video listing — // and nothing reaches X. const ROOT = await mkdtemp(path.join(os.tmpdir(), "sync-route-")); // Set before the route (and getPaths, which caches) is first imported. process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const channel = async (slug: string, config: Record) => { await mkdir(path.join(ROOT, "channels", slug), { recursive: true }); await writeFile(path.join(ROOT, "channels", slug, "config.json"), JSON.stringify(config)); }; await channel("demo-x", { handling: "transcribe", sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", socialHandle: "example_user", name: "Example (X)", url: "https://x.com/example_user", }); await channel("demo-pages", { handling: "transcribe", sourceKind: "social", platform: "webpage", name: "Example pages", url: "https://web.archive.org/web/2020/https://example.test/", }); const { POST } = await import("./route"); const { WAYBACK_PAGES_NOT_FETCHED } = await import("yt-dlp-transcript-common/controller/fetchPosts"); test.after(() => rm(ROOT, { recursive: true, force: true })); async function post(body: Record): Promise<{ status: number; error: string }> { const res = await POST( new Request("http://localhost/api/ops/sync", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, error: ((await res.json()) as { error?: string }).error ?? "" }; } const jobs = () => readdir(path.join(ROOT, ".jobs")).catch(() => []); test("an X channel's sync is fetch-posts: refused on no cookie jar, in the post fetch's own words, before any job", async () => { const res = await post({ slug: "demo-x" }); assert.notEqual(res.status, 200); assert.match(res.error, /^No X cookie jar has been exported: press Connect X account on \/cookies, then fetch again\.$/); assert.deepEqual(await jobs(), []); }); test("a Wayback pages channel's sync is its post fetch too, which says such a channel is never fetched", async () => { const res = await post({ slug: "demo-pages" }); assert.equal(res.error, WAYBACK_PAGES_NOT_FETCHED); assert.deepEqual(await jobs(), []); }); test("a full sweep is the video listing's: a social channel's sync says where its whole re-walk is", async () => { const res = await post({ slug: "demo-x", full: true }); assert.match(res.error, /^A social channel's Sync fetches its new posts; a full re-walk of its timeline is Fetch posts → Re-fetch full history/); assert.deepEqual(await jobs(), []); });