import test from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdtemp, readFile, 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/media/fetch-window/route.remote.test.ts" // // demo epic D1: the single window (the MCP's fetch_clip over a remote source) // of a channel this editor does not hold, named with its connected archive — // registered remote-only; its Rumble whole recording refused with the size // (409, no job). The archive is a fake fetch. const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetch-window-remote-")); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); delete process.env.TRANSCRIPT_SITE_URL; // yt-dlp is a fake that writes down what it was asked for: no test here reaches a platform. const ARGS_LOG = path.join(ROOT, "argv.jsonl"); const YTDLP = path.join(ROOT, "fake-ytdlp.mjs"); await writeFile( YTDLP, `#!/usr/bin/env node import { appendFileSync, writeFileSync } from "node:fs"; const args = process.argv.slice(2); appendFileSync(${JSON.stringify(ARGS_LOG)}, JSON.stringify(args) + "\\n"); const o = args.indexOf("-o"); if (o >= 0) writeFileSync(args[o + 1], "mp4"); `, ); await chmod(YTDLP, 0o755); process.env.YTDLP_BIN = YTDLP; process.env.E2E_CLIP_WINDOW_GAP_MS = "0"; const ORIGIN = "https://stub.test"; await writeFile(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0, remoteArchives: [{ id: "stub", origin: ORIGIN, title: "Stub", addedAt: "2026-11-11T00:00:00Z" }] })); const DOCS: Record = { [`${ORIGIN}/corpus.json`]: { spec: 6, channels: [ { slug: "rb-remote", name: "RB Remote", platform: "rumble", manifests: { transcripts: `${ORIGIN}/transcripts/rb-remote/manifest.json` } }, { slug: "yt-evil", name: "Evil", platform: "youtube", manifests: { transcripts: `${ORIGIN}/transcripts/yt-evil/manifest.json` } }, ], }, [`${ORIGIN}/transcripts/yt-evil/manifest.json`]: { slugToPage: { e1: 0, d1: 0, d2: 0, n1: 0 } }, [`${ORIGIN}/transcripts/yt-evil/page-0000.json`]: [ { id: "e1", webpageUrl: "http://169.254.169.254/latest/meta-data/" }, // Read as youtube.com by Node's URL parser, as another host by Python's (re-check, the parser differential). { id: "d1", webpageUrl: "https://youtube.com\\@evil.example/watch?v=d1" }, { id: "d2", webpageUrl: "https://you\ttube.com/watch?v=d2" }, // The platform's own page, spelled loudly. { id: "n1", webpageUrl: "HTTPS://WWW.YouTube.com:443/watch?v=n1" }, ], [`${ORIGIN}/transcripts/rb-remote/manifest.json`]: { slugToPage: { rb9: 0 } }, [`${ORIGIN}/transcripts/rb-remote/page-0000.json`]: [{ id: "rb9", duration: 600, webpageUrl: "https://rumble.com/vrb9-x.html" }], }; const asked: string[] = []; globalThis.fetch = (async (input: RequestInfo | URL) => { const url = String(input); asked.push(url); return url in DOCS ? new Response(JSON.stringify(DOCS[url]), { status: 200 }) : new Response("nope", { status: 404 }); }) as typeof fetch; const { POST } = await import("./route"); // A job's end still writes after its status reads done (its sidecar, and the // report regeneration it arms — snapshot.json in the channel dir): waited for // before the corpus is removed (testing/jobs/quiesce.ts). const { quiesceJobs, settleJob } = await import("yt-dlp-transcript-common/testing/jobs/quiesce.js"); test.after(async () => { await quiesceJobs(); await rm(ROOT, { recursive: true, force: true }); }); const post = async (body: Record) => { const res = await POST( new Request("http://localhost/api/media/fetch-window", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as { error?: string } }; }; test("a single window of an archive's channel registers it remote-only, and its Rumble whole recording is refused with the size", async () => { const r = await post({ channelSlug: "rb-remote", videoId: "rb9", from: 10, to: 20, requestedBy: "test", reason: "a test", archive: ORIGIN }); assert.equal(r.status, 409, r.json.error); assert.match(r.json.error ?? "", /^whole recording refused: Rumble windows are cut from the WHOLE recording, and rb-remote is a remote-only channel: rb-remote\/rb9 is 10:00 long/); const config = JSON.parse(await readFile(path.join(ROOT, "channels", "rb-remote", "config.json"), "utf8")); assert.equal(config.remoteOnly, true); assert.equal(config.remoteArchive, ORIGIN); }); test("without its archive named, a channel not held is a 404 as before", async () => { const r = await post({ channelSlug: "nope-remote", videoId: "x1", from: 10, to: 20, requestedBy: "test", reason: "a test" }); assert.equal(r.status, 404); }); test("a single window whose archive record points at a non-platform host is refused (400), nothing fetched", async () => { const r = await post({ channelSlug: "yt-evil", videoId: "e1", from: 10, to: 20, requestedBy: "test", reason: "a test", archive: ORIGIN }); assert.equal(r.status, 400); assert.match(r.json.error ?? "", /^yt-evil\/e1: 169\.254\.169\.254 is not a video platform's host/); }); test("a page URL two parsers read differently is refused (400) before any request, from the record or from the ask", async () => { const ask = (videoId: string, webpageUrl?: string) => post({ channelSlug: "yt-evil", videoId, from: 10, to: 20, requestedBy: "test", reason: "a test", archive: ORIGIN, ...(webpageUrl ? { webpageUrl } : {}) }); for (const r of [ await ask("d1"), await ask("d2"), await ask("a1", "https://youtube.com\\@evil.example/watch?v=x"), await ask("a2", "https://youtube.com\\@127.0.0.1/x"), await ask("a3", "https://rumble.com\\.evil.example/v1"), await ask("a4", "https://you\ttube.com/watch?v=x"), ]) { assert.equal(r.status, 400, r.json.error); assert.match(r.json.error ?? "", /carries a backslash, whitespace or a control character/); } assert.equal(await readFile(ARGS_LOG, "utf8").catch(() => ""), "", "yt-dlp was never run"); }); test("what reaches yt-dlp is the URL the check parsed, written back — never the record's own string", async () => { const res = await POST( new Request("http://localhost/api/media/fetch-window", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify({ channelSlug: "yt-evil", videoId: "n1", from: 10, to: 20, requestedBy: "test", reason: "a test", archive: ORIGIN }), }), ); const body = (await res.json()) as { jobId?: string; error?: string }; assert.equal(res.status, 202, body.error); // The job's end, its sidecar written: bounded by the job, not by 200 polls a loaded machine outlasts. await settleJob(body.jobId!); const runs = (await readFile(ARGS_LOG, "utf8")).trim().split("\n").map((l) => JSON.parse(l) as string[]); assert.ok(runs.length > 0, "yt-dlp was asked"); for (const args of runs) { assert.equal(args.at(-1), "https://www.youtube.com/watch?v=n1"); assert.equal(args.some((a) => /YouTube|:443/.test(a)), false, "the record's own spelling is nowhere in the command"); } }); // LAST: it disconnects the archive for the rest of the file. test("a remote-only channel whose archive was removed from settings is refused (400), and the archive is not read", async () => { await writeFile(process.env.SETTINGS_FILE!, JSON.stringify({ remoteArchives: [] })); asked.length = 0; const r = await post({ channelSlug: "rb-remote", videoId: "rb9", from: 10, to: 20, requestedBy: "test", reason: "a test" }); assert.equal(r.status, 400, r.json.error); assert.match(r.json.error ?? "", /^rb-remote is a remote-only channel, and its archive https:\/\/stub\.test is no longer connected/); assert.deepEqual(asked, [], "nothing asked of an archive that is not connected"); });