// check-availability's local cue read (release 23 F1b): a Twitch record's // `v` reads its transcript.cues.json from `data//`. CHANNELS_DIR is read // when the module loads, so it is set before the import. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "check-availability-")); process.env.CHANNELS_DIR = ROOT; // The real pacing is under test here, not the e2e stub gap. delete process.env.E2E_AVAILABILITY_GAP_MS; const { cueMeta } = await import("./check-availability.mjs"); test.after(() => rm(ROOT, { recursive: true, force: true })); test("cueMeta reads a Twitch v record from data//", async () => { const n = "2700000001"; await mkdir(path.join(ROOT, "example-twitch", "data", n), { recursive: true }); await writeFile(path.join(ROOT, "example-twitch", "config.json"), JSON.stringify({ handling: "transcribe", url: "https://www.twitch.tv/example" })); await writeFile( path.join(ROOT, "example-twitch", "data", n, "transcript.cues.json"), JSON.stringify({ id: `v${n}`, title: "Example stream", webpageUrl: `https://www.twitch.tv/videos/${n}`, duration: 120, cues: [] }), ); assert.deepEqual(await cueMeta(`v${n}`, "example-twitch"), { title: "Example stream", webpageUrl: `https://www.twitch.tv/videos/${n}`, duration: 120, }); // Another platform's v-id is not mapped: no file. await writeFile(path.join(ROOT, "example-twitch", "config.json"), JSON.stringify({ handling: "youtube", platform: "youtube" })); await assert.rejects(() => cueMeta(`v${n}`, "example-twitch"), /ENOENT/); }); // ---- the preflight never bursts a platform, and never asks for what is held ---- // // A fake yt-dlp records each invocation; the pacing runs on a fake clock (the // injected `now`/`sleep`), so a 120 s Rumble gap costs the test nothing. const { checkAvailability, classify } = await import("./check-availability.mjs"); const { rawWindowName } = await import("./sources.mjs"); const { chmod, readFile } = await import("node:fs/promises"); const { CLIP_WINDOW_MIN_GAP_SECONDS, CLIP_WINDOW_PLATFORM_MIN_GAP_SECONDS } = await import( "yt-dlp-transcript-common/ytdlp/platformArgs.mjs" ); /** A fake yt-dlp: appends its last arg (the URL) to `log`; a URL with "blocked" draws a 403. */ async function fakeYtdlp(dir) { const bin = path.join(dir, "fake-yt-dlp.sh"); const log = path.join(dir, "invocations.log"); await writeFile( bin, `#!/bin/sh\nfor a; do last="$a"; done\necho "$last" >> "${log}"\n` + `case "$last" in *blocked*) echo "ERROR: [Rumble] x: Unable to download webpage: HTTP Error 403: Forbidden" >&2; exit 1;; esac\nexit 0\n`, ); await chmod(bin, 0o755); const calls = async () => (await readFile(log, "utf8").catch(() => "")).split("\n").filter(Boolean); return { bin, calls }; } /** A fake clock: `sleep` advances it and records what was asked. */ function fakeClock(start = Date.parse("2026-10-25T00:00:00Z")) { let t = start; const sleeps = []; return { now: () => t, sleep: async (ms) => { sleeps.push(ms); t += ms; }, random: () => 0, sleeps, }; } /** A channel with one video's cue file, its page at `url`. */ async function cueFile(slug, id, url) { await mkdir(path.join(ROOT, slug, "data", id), { recursive: true }); await writeFile(path.join(ROOT, slug, "config.json"), JSON.stringify({ handling: "youtube" })); await writeFile( path.join(ROOT, slug, "data", id, "transcript.cues.json"), JSON.stringify({ id, title: `video ${id}`, webpageUrl: url, duration: 600, cues: [] }), ); } async function project(name, timeline, ledger = []) { const dir = path.join(ROOT, "projects", name); await mkdir(dir, { recursive: true }); const manifest = path.join(dir, "video.manifest.json"); await writeFile( manifest, JSON.stringify({ slug: name, provenance: { channelSlug: "chan" }, render: { fetchPad: 3 }, timeline, ledger }), ); return { dir, manifest, out: path.join(dir, "out") }; } test("classify: a refusal is unchecked, never an age restriction or a death", () => { assert.equal(classify("ERROR: HTTP Error 403: Forbidden"), "unchecked"); assert.equal(classify("ERROR: HTTP Error 429: Too Many Requests"), "unchecked"); assert.equal(classify("Sign in to confirm you're not a bot"), "unchecked"); assert.equal(classify("Just a moment..."), "unchecked"); assert.equal(classify("Sign in to confirm your age"), "restricted"); assert.equal(classify("Video unavailable"), "deleted"); }); test("every clip held on disk: held-locally, and yt-dlp is never run", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "ca-held-")); const fake = await fakeYtdlp(tmp); await cueFile("chan", "held1", "https://rumble.com/v111-held.html"); await cueFile("chan", "held2", "https://www.youtube.com/watch?v=held2xxxxxx"); const p = await project("all-local", [ { id: "c1", type: "clip", video: "held1", start: 10, end: 20 }, { id: "c2", type: "clip", video: "held2", start: 30, end: 40 }, ]); await mkdir(path.join(p.out, "clips-raw"), { recursive: true }); // c1 in the build's raw cache (its exact padded span), c2 in the editor's window cache. await writeFile(path.join(p.out, "clips-raw", rawWindowName("held1", 7, 23)), "x"); await mkdir(path.join(ROOT, "chan", "data", "held2", "clips"), { recursive: true }); await writeFile(path.join(ROOT, "chan", "data", "held2", "clips", "20-50.mp4"), "x"); const clock = fakeClock(); const report = await checkAvailability(p.manifest, { outDir: p.out, ytdlp: fake.bin, ...clock }); assert.deepEqual(report.sources.map((s) => [s.key, s.state, s.ok]), [ ["chan/held1", "held-locally", true], ["chan/held2", "held-locally", true], ]); assert.deepEqual(await fake.calls(), [], "no probe at all"); assert.deepEqual(clock.sleeps, []); }); test("a gap-joined clip fetched per piece is held when every piece is; one missing is a probe", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "ca-pieced-")); const fake = await fakeYtdlp(tmp); await cueFile("chan", "pc1", "https://www.youtube.com/watch?v=pc1xxxxxxxx"); await cueFile("chan", "pc2", "https://www.youtube.com/watch?v=pc2xxxxxxxx"); // 10–200 less a 100 s gap: kept 10–50 and 150–200, padded 3 s: 7–53 and 147–203, 94 s apart (not joined). const gaps = [{ from: 50, to: 150 }]; const p = await project("pieced", [ { id: "p1", type: "clip", video: "pc1", start: 10, end: 200, gaps }, { id: "p2", type: "clip", video: "pc2", start: 10, end: 200, gaps }, ]); await mkdir(path.join(p.out, "clips-raw"), { recursive: true }); // pc1: both pieces (the build's raw names, one per piece). pc2: the first piece only. await writeFile(path.join(p.out, "clips-raw", rawWindowName("pc1", 7, 53)), "x"); await writeFile(path.join(p.out, "clips-raw", rawWindowName("pc1", 147, 203)), "x"); await writeFile(path.join(p.out, "clips-raw", rawWindowName("pc2", 7, 53)), "x"); const clock = fakeClock(); const report = await checkAvailability(p.manifest, { outDir: p.out, ytdlp: fake.bin, ...clock }); const byKey = Object.fromEntries(report.sources.map((s) => [s.key, s.state])); assert.equal(byKey["chan/pc1"], "held-locally", "every piece held"); assert.notEqual(byKey["chan/pc2"], "held-locally", "the second piece is not on disk"); assert.deepEqual(await fake.calls(), ["https://www.youtube.com/watch?v=pc2xxxxxxxx"], "only the half-held source is probed"); }); test("two Rumble sources: the second probe starts at least 120 s after the first ends", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "ca-paced-")); const fake = await fakeYtdlp(tmp); await cueFile("chan", "r1", "https://rumble.com/v201-one.html"); await cueFile("chan", "r2", "https://rumble.com/v202-two.html"); await cueFile("chan", "y1", "https://www.youtube.com/watch?v=y1xxxxxxxxx"); const p = await project("paced", [ { id: "c1", type: "clip", video: "r1", start: 10, end: 20 }, { id: "c2", type: "clip", video: "r2", start: 10, end: 20 }, { id: "c3", type: "clip", video: "y1", start: 10, end: 20 }, ]); const clock = fakeClock(); const report = await checkAvailability(p.manifest, { outDir: p.out, ytdlp: fake.bin, ...clock }); assert.equal(CLIP_WINDOW_PLATFORM_MIN_GAP_SECONDS.rumble, 120); const at = Object.fromEntries(report.sources.map((s) => [s.video, Date.parse(s.checkedAt)])); assert.ok(at.r2 - at.r1 >= 120_000, `rumble probes ${at.r2 - at.r1} ms apart`); // The YouTube probe does not wait behind Rumble's gap: it interleaves. assert.ok(at.y1 < at.r2, "youtube probed inside rumble's gap"); assert.deepEqual(clock.sleeps, [120_000]); assert.equal((await fake.calls()).length, 3); assert.ok(report.sources.every((s) => s.state === "ok")); assert.ok(CLIP_WINDOW_MIN_GAP_SECONDS >= 30); }); test("a 403 stops that platform: its remaining sources are unchecked, never unavailable", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "ca-refused-")); const fake = await fakeYtdlp(tmp); await cueFile("chan", "b1", "https://rumble.com/v301-blocked.html"); await cueFile("chan", "b2", "https://rumble.com/v302-next.html"); await cueFile("chan", "b3", "https://rumble.com/v303-later.html"); await cueFile("chan", "y2", "https://www.youtube.com/watch?v=y2xxxxxxxxx"); const p = await project( "refused", [ { id: "c1", type: "clip", video: "b1", start: 10, end: 20 }, { id: "c2", type: "clip", video: "b2", start: 10, end: 20 }, { id: "c4", type: "clip", video: "y2", start: 10, end: 20 }, ], [{ id: "k1", video: "b3" }], ); const clock = fakeClock(); const report = await checkAvailability(p.manifest, { outDir: p.out, ytdlp: fake.bin, ...clock }); const by = Object.fromEntries(report.sources.map((s) => [s.video, s])); assert.equal(by.b1.state, "unchecked"); assert.equal(by.b1.ok, null); assert.equal(by.b2.state, "unchecked"); assert.equal(by.b3.state, "unchecked"); assert.match(by.b2.error, /rumble refused an earlier probe/); assert.equal(by.y2.state, "ok", "another platform is still probed"); const calls = await fake.calls(); assert.equal(calls.filter((u) => u.includes("rumble.com")).length, 1, "one Rumble request, then none"); assert.equal(calls.length, 2); // On disk too: what the inbox reads. const disk = JSON.parse(await readFile(path.join(p.out, "availability.json"), "utf8")); assert.ok(disk.sources.every((s) => s.ok !== false), JSON.stringify(disk.sources.map((s) => s.state))); });