import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync, mkdirSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { WHOLE_RECORDING_REFUSED, archiveRecordOf, archiveRecordPageHref, ensureRemoteOnlyChannel, estimateRecordingBytes, wholeRecordingRefusal, } from "./remoteOnlyChannel"; import type { GetJson } from "../lib/remoteArchives-server"; import { detectPlatform } from "../lib/detectPlatform.mjs"; import type { Paths } from "../lib/paths"; import { isRemoteOnlyChannel, remoteOnlyRefusal } from "../lib/channelConfig"; // demo epic D1 item 5: a channel cited from a connected archive is registered // remote-only on first need — config only — and its records come from the archive. const ORIGIN = "https://stub.test"; const DOCS: Record = { [`${ORIGIN}/corpus.json`]: { spec: 6, site: { id: "stub", title: "Stub" }, channels: [ { slug: "alpha", name: "Alpha", videoCount: 2, platform: "youtube", url: "https://www.youtube.com/@alpha", manifests: { transcripts: `${ORIGIN}/transcripts/alpha/manifest.json` } }, { slug: "rum", name: "Rum", videoCount: 1, platform: "rumble", manifests: { transcripts: `${ORIGIN}/transcripts/rum/manifest.json` } }, ], }, [`${ORIGIN}/transcripts/alpha/manifest.json`]: { pageCount: 1, slugToPage: { a1: 0, a2: 0 } }, [`${ORIGIN}/transcripts/alpha/page-0000.json`]: [ { id: "a1", title: "One", duration: 600, webpageUrl: "https://www.youtube.com/watch?v=a1" }, { id: "a2", title: "Two", duration: 7980, webpageUrl: "https://www.youtube.com/watch?v=a2" }, ], }; function fake(): GetJson & { asked: string[] } { const asked: string[] = []; const f = (async (url: string) => { asked.push(url); if (!(url in DOCS)) throw new Error(`${url} answered HTTP 404`); return DOCS[url]; }) as GetJson & { asked: string[] }; f.asked = asked; return f; } function pool(): { paths: Paths; root: string } { const root = mkdtempSync(path.join(tmpdir(), "remote-only-")); return { root, paths: { channelsDir: path.join(root, "channels") } as unknown as Paths }; } test("first need registers config only: remote-only, manual, the platform, no url", async () => { const { paths, root } = pool(); try { const r = await ensureRemoteOnlyChannel(paths, "alpha", { origin: ORIGIN }, { getJson: fake() }); assert.equal(r.registered, true); const onDisk = JSON.parse(readFileSync(path.join(paths.channelsDir, "alpha", "config.json"), "utf8")); assert.deepEqual(onDisk, { name: "Alpha", handling: "youtube", platform: "youtube", remoteOnly: true, remoteArchive: ORIGIN, remoteUrl: "https://www.youtube.com/@alpha", downloadMode: "manual", }); assert.equal("url" in onDisk, false, "nothing that syncs by url can sync it"); assert.equal(isRemoteOnlyChannel(r.config), true); assert.match(remoteOnlyRefusal("alpha", r.config)!, /alpha is a remote-only channel of https:\/\/stub\.test/); // Second need: the one on disk, nothing read. const f = fake(); const again = await ensureRemoteOnlyChannel(paths, "alpha", { origin: ORIGIN }, { getJson: f }); assert.equal(again.registered, false); assert.deepEqual(f.asked, []); } finally { rmSync(root, { recursive: true, force: true }); } }); test("a channel held here is never touched; one the archive does not list is refused, nothing written", async () => { const { paths, root } = pool(); try { mkdirSync(path.join(paths.channelsDir, "alpha"), { recursive: true }); writeFileSync(path.join(paths.channelsDir, "alpha", "config.json"), JSON.stringify({ name: "Mine", handling: "youtube", url: "https://www.youtube.com/@alpha" })); const r = await ensureRemoteOnlyChannel(paths, "alpha", { origin: ORIGIN }, { getJson: fake() }); assert.equal(r.registered, false); assert.equal(r.config.name, "Mine"); assert.equal(isRemoteOnlyChannel(r.config), false); await assert.rejects(ensureRemoteOnlyChannel(paths, "nope", { origin: ORIGIN }, { getJson: fake() }), /lists no channel "nope"/); assert.throws(() => readFileSync(path.join(paths.channelsDir, "nope", "config.json"))); } finally { rmSync(root, { recursive: true, force: true }); } }); test("a cited record's page URL and length come from the archive", async () => { const f = fake(); assert.deepEqual(await archiveRecordOf(ORIGIN, "alpha", "a2", { getJson: f }), { webpageUrl: "https://www.youtube.com/watch?v=a2", duration: 7980, title: "Two", }); assert.deepEqual(f.asked, [`${ORIGIN}/corpus.json`, `${ORIGIN}/transcripts/alpha/manifest.json`, `${ORIGIN}/transcripts/alpha/page-0000.json`]); assert.equal(await archiveRecordOf(ORIGIN, "alpha", "zz", { getJson: fake() }), null); assert.equal(await archiveRecordOf(ORIGIN, "rum", "r1", { getJson: fake() }), null); }); test("a whole recording is refused with its length and estimated size, and the key that allows it", () => { assert.equal(estimateRecordingBytes(3600, 720), 1_125_000_000); assert.equal(estimateRecordingBytes(3600, 360), 360_000_000); const msg = wholeRecordingRefusal({ platform: "rumble", slug: "rum", id: "r1", duration: 7980 }); assert.ok(msg.startsWith(WHOLE_RECORDING_REFUSED)); assert.match(msg, /Rumble windows are cut from the WHOLE recording/); assert.match(msg, /rum\/r1 is 2:13:00 long, about 2\.5 GB \(estimated\)/); assert.match(msg, /"allowWholeRecording": true/); assert.match(wholeRecordingRefusal({ platform: "rumble", slug: "rum", id: "r1" }), /length is not known to its archive/); }); // demo epic D1 review, blocker 2b: an archive is read on its own origin only. test("a manifest the corpus names on another origin is not followed: the archive's own is read", async () => { const asked: string[] = []; const docs: Record = { "https://stub.test/corpus.json": { channels: [{ slug: "a", manifests: { transcripts: "http://192.168.1.1/admin.json" } }] }, "https://stub.test/transcripts/a/manifest.json": { slugToPage: { v1: 0 } }, "https://stub.test/transcripts/a/page-0000.json": [{ id: "v1", webpageUrl: "https://www.youtube.com/watch?v=v1" }], }; const getJson = async (url: string) => { asked.push(url); if (!(url in docs)) throw new Error("404"); return docs[url]; }; assert.equal((await archiveRecordOf("https://stub.test", "a", "v1", { getJson }))?.webpageUrl, "https://www.youtube.com/watch?v=v1"); assert.equal(asked.some((u) => u.includes("192.168")), false); }); // demo epic D1 re-check: what umtool's chain sends the editor for a record is the URL the check parsed. test("a record's page for an ask is the normalised href; one two parsers read differently, or off its platform, is no page", async () => { const docs: Record = { "https://stub.test/corpus.json": { channels: [{ slug: "a" }] }, "https://stub.test/transcripts/a/manifest.json": { slugToPage: { ok: 0, loud: 0, slash: 0, tab: 0, dot: 0, off: 0, none: 0 } }, "https://stub.test/transcripts/a/page-0000.json": [ { id: "ok", webpageUrl: "https://www.youtube.com/watch?v=ok" }, { id: "loud", webpageUrl: "HTTPS://WWW.YouTube.com:443/watch?v=loud" }, { id: "slash", webpageUrl: "https://youtube.com\\@evil.example/watch?v=x" }, { id: "tab", webpageUrl: "https://you\ttube.com/watch?v=x" }, { id: "dot", webpageUrl: "https://rumble.com\\.evil.example/v1" }, { id: "off", webpageUrl: "http://169.254.169.254/latest/meta-data/" }, { id: "none" }, ], }; const getJson = async (url: string) => { if (!(url in docs)) throw new Error("404"); return docs[url]; }; const href = (id: string) => archiveRecordPageHref("https://stub.test", "a", id, detectPlatform, { getJson }); assert.equal(await href("ok"), "https://www.youtube.com/watch?v=ok"); assert.equal(await href("loud"), "https://www.youtube.com/watch?v=loud"); for (const id of ["slash", "tab", "dot", "off", "none", "not-held"]) assert.equal(await href(id), null, id); });