import test from "node:test"; import assert from "node:assert/strict"; import { 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/ops/fetch-windows/route.remote.test.ts" // // demo epic D1 item 5: a window cited from a CONNECTED archive, of a channel // this editor does not hold. The archive is a fake fetch (no request leaves // the process); no job is queued — every run here ends refused or unresolved. const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetch-windows-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; const ORIGIN = "https://stub.test"; await writeFile( process.env.SETTINGS_FILE, JSON.stringify({ remoteArchives: [{ id: "stub", origin: ORIGIN, title: "Stub", addedAt: "2026-11-11T00:00:00Z" }] }), ); const DOCS: Record = { [`${ORIGIN}/corpus.json`]: { spec: 6, site: { id: "stub", title: "Stub" }, channels: [ { slug: "yt-remote", name: "YT Remote", videoCount: 1, platform: "youtube", url: "https://www.youtube.com/@ytr", manifests: { transcripts: `${ORIGIN}/transcripts/yt-remote/manifest.json` } }, { slug: "rb-remote", name: "RB Remote", videoCount: 1, platform: "rumble", manifests: { transcripts: `${ORIGIN}/transcripts/rb-remote/manifest.json` } }, { slug: "evil-remote", name: "Evil", videoCount: 1, platform: "youtube", manifests: { transcripts: `${ORIGIN}/transcripts/evil-remote/manifest.json` } }, ], }, [`${ORIGIN}/transcripts/rb-remote/manifest.json`]: { pageCount: 1, slugToPage: { rb9: 0 } }, [`${ORIGIN}/transcripts/rb-remote/page-0000.json`]: [{ id: "rb9", duration: 5400, webpageUrl: "https://rumble.com/vrb9-x.html" }], // An archive pointing the editor at its own API (review of 9fddc3bb, blocker 2). [`${ORIGIN}/transcripts/evil-remote/manifest.json`]: { slugToPage: { e1: 0, e2: 0, e3: 0, d1: 0, d2: 0, d3: 0, n1: 0 } }, [`${ORIGIN}/transcripts/evil-remote/page-0000.json`]: [ { id: "e1", webpageUrl: "http://127.0.0.1:3001/api/ops/settings" }, { id: "e2", webpageUrl: "https://rumble.com/ve2-x.html" }, // A look-alike of the channel's platform (re-review, must-fix 1). { id: "e3", webpageUrl: "https://evilyoutube.com/watch?v=e3" }, // 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://youtube.com\\@127.0.0.1/x" }, { id: "d3", webpageUrl: "https://you\ttube.com/watch?v=d3" }, // The platform's own page, spelled loudly: handed on as the parser wrote it back. { id: "n1", webpageUrl: "HTTPS://WWW.YouTube.com:443/watch?v=n1" }, ], }; const asked: string[] = []; globalThis.fetch = (async (input: RequestInfo | URL) => { const url = String(input); asked.push(url); if (!(url in DOCS)) return new Response("not found", { status: 404 }); return new Response(JSON.stringify(DOCS[url]), { status: 200 }); }) as typeof fetch; const { POST } = await import("./route"); test.after(() => rm(ROOT, { recursive: true, force: true })); async function post(body: Record) { const res = await POST( new Request("http://localhost/api/ops/fetch-windows", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as Record & { error?: string } }; } type Unresolved = { item: { slug: string }; error: string }[]; test("a dry run names the channel it would register, and registers nothing", async () => { const r = await post({ requestedBy: "test", archive: ORIGIN, dryRun: true, items: [{ slug: "yt-remote", id: "v1", from: 1, to: 5, webpageUrl: "https://www.youtube.com/watch?v=v1" }] }); assert.equal(r.status, 200, r.json.error); assert.deepEqual(r.json.registered, ["yt-remote"]); assert.match((r.json.unresolved as Unresolved)[0].error, /would register it as a remote-only channel of https:\/\/stub\.test/); await assert.rejects(readFile(path.join(ROOT, "channels", "yt-remote", "config.json"))); assert.deepEqual(asked, [], "a dry run reads no archive either"); }); test("an origin not connected registers nothing, and the refusal names the connect", async () => { const r = await post({ requestedBy: "test", archive: "https://elsewhere.test", dryRun: true, items: [{ slug: "yt-remote", id: "v1", from: 1, to: 5 }] }); assert.equal(r.status, 200, r.json.error); assert.deepEqual(r.json.registered, []); assert.match((r.json.unresolved as Unresolved)[0].error, /not found — https:\/\/elsewhere\.test is not a connected archive: connect it/); }); test("a run registers the remote-only channel from the archive, and refuses its Rumble whole recording with the size", async () => { const r = await post({ requestedBy: "test", archive: ORIGIN, items: [{ slug: "rb-remote", id: "rb9", from: 60, to: 70 }] }); assert.equal(r.status, 200, r.json.error); assert.deepEqual(r.json.registered, ["rb-remote"]); assert.deepEqual(r.json.jobIds, [], "nothing queued"); const error = (r.json.unresolved as Unresolved)[0].error; assert.match(error, /^whole recording refused: Rumble windows are cut from the WHOLE recording/); assert.match(error, /rb-remote\/rb9 is 1:30:00 long, about 1\.7 GB \(estimated\)/); const config = JSON.parse(await readFile(path.join(ROOT, "channels", "rb-remote", "config.json"), "utf8")); assert.deepEqual(config, { name: "RB Remote", handling: "transcribe", platform: "rumble", remoteOnly: true, remoteArchive: ORIGIN, downloadMode: "manual" }); // Its page URL came from the archive's record (the item had none). assert.ok(asked.includes(`${ORIGIN}/transcripts/rb-remote/page-0000.json`)); // The allowance, as a dry run: planned on Rumble's download queue, the archive's URL. const allowed = await post({ requestedBy: "test", archive: ORIGIN, allowWholeRecording: true, dryRun: true, items: [{ slug: "rb-remote", id: "rb9", from: 60, to: 70 }] }); assert.equal(allowed.status, 200, allowed.json.error); const groups = allowed.json.groups as { platform: string; queueKey: string; items: { webpageUrl: string }[] }[]; assert.equal(groups.length, 1); assert.equal(groups[0].platform, "rumble"); assert.equal(groups[0].items[0].webpageUrl, "https://rumble.com/vrb9-x.html"); }); test("an archive's record pointing anywhere but its channel's video platform is refused before any request", async () => { const r = await post({ requestedBy: "test", archive: ORIGIN, items: [{ slug: "evil-remote", id: "e1", from: 1, to: 5 }, { slug: "evil-remote", id: "e2", from: 1, to: 5 }] }); assert.equal(r.status, 200, r.json.error); const errors = (r.json.unresolved as Unresolved).map((u) => u.error); assert.match(errors[0], /127\.0\.0\.1:3001 is not a video platform's host/); assert.match(errors[1], /rumble\.com is rumble, and the channel is youtube's/); assert.deepEqual(r.json.jobIds, []); assert.equal(asked.some((u) => u.startsWith("http://127.0.0.1")), false); }); test("a look-alike of the channel's platform host is refused before any request", async () => { const r = await post({ requestedBy: "test", archive: ORIGIN, items: [{ slug: "evil-remote", id: "e3", from: 1, to: 5 }, { slug: "evil-remote", id: "e4", from: 1, to: 5, webpageUrl: "https://www.youtube.com.evil.example/watch?v=e4" }] }); assert.equal(r.status, 200, r.json.error); const errors = (r.json.unresolved as Unresolved).map((u) => u.error); assert.match(errors[0], /^evilyoutube\.com is not a video platform's host/); assert.match(errors[1], /^www\.youtube\.com\.evil\.example is not a video platform's host/); assert.deepEqual(r.json.jobIds, []); }); test("a page URL two parsers read differently is refused before any request, from the record or from the ask", async () => { const r = await post({ requestedBy: "test", archive: ORIGIN, items: [ { slug: "evil-remote", id: "d1", from: 1, to: 5 }, { slug: "evil-remote", id: "d2", from: 1, to: 5 }, { slug: "evil-remote", id: "d3", from: 1, to: 5 }, { slug: "evil-remote", id: "a1", from: 1, to: 5, webpageUrl: "https://youtube.com\\@evil.example/watch?v=x" }, { slug: "evil-remote", id: "a2", from: 1, to: 5, webpageUrl: "https://rumble.com\\.evil.example/v1" }, { slug: "evil-remote", id: "a3", from: 1, to: 5, webpageUrl: "https://you\ttube.com/watch?v=x" }, { slug: "evil-remote", id: "a4", from: 1, to: 5, webpageUrl: "https://youtube.com/watch?v=x\nhttps://evil.example/" }, ], }); assert.equal(r.status, 200, r.json.error); const errors = (r.json.unresolved as Unresolved).map((u) => u.error); assert.equal(errors.length, 7); for (const e of errors) assert.match(e, /carries a backslash, whitespace or a control character/); assert.deepEqual(r.json.jobIds, []); }); test("what is planned for the fetcher is the URL the check parsed, written back — the record's and the ask's", async () => { const r = await post({ requestedBy: "test", archive: ORIGIN, dryRun: true, items: [ { slug: "evil-remote", id: "n1", from: 1, to: 5 }, { slug: "evil-remote", id: "n2", from: 1, to: 5, webpageUrl: " https:YOUTUBE.com:443/watch?v=n2 " }, ], }); assert.equal(r.status, 200, r.json.error); assert.deepEqual(r.json.unresolved, []); const urls = (r.json.groups as { items: { id: string; webpageUrl: string }[] }[]).flatMap((g) => g.items).map((i) => i.webpageUrl).sort(); assert.deepEqual(urls, ["https://www.youtube.com/watch?v=n1", "https://youtube.com/watch?v=n2"]); }); // LAST: it disconnects the archive for the rest of the file. test("a remote-only channel whose archive was removed from settings is refused, and the archive is not read", async () => { await writeFile(process.env.SETTINGS_FILE!, JSON.stringify({ remoteArchives: [] })); asked.length = 0; const r = await post({ requestedBy: "test", items: [{ slug: "rb-remote", id: "rb9", from: 60, to: 70 }] }); assert.equal(r.status, 200, r.json.error); assert.deepEqual(r.json.jobIds, []); assert.match((r.json.unresolved as Unresolved)[0].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"); });