import test from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdir, 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/channels/*/videos/fetchWindowsStoredUrl.test.ts" // // The batch's door with `storedUrl` (release 32 R5): Odysee is in a rate-limit // cooldown throughout. "never" refuses its group whole, as before; "only" starts // a job with the window whose record stores a CDN URL and refuses the one // without; the job cuts it from that URL. yt-dlp and ffmpeg are fakes (YTDLP_BIN, // FFMPEG_BIN): an odysee.com target answers 429, anything else is written. const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetch-windows-stored-")); const SLUG = "demo-odysee"; const ARGS_LOG = path.join(ROOT, "argv.jsonl"); const YTDLP = path.join(ROOT, "fake-ytdlp.mjs"); const FFMPEG = path.join(ROOT, "fake-ffmpeg.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 url = args[args.length - 1]; if (url.startsWith("https://odysee.com/")) { process.stderr.write("ERROR: [lbry] x: Unable to download webpage: HTTP Error 429: Too Many Requests\\n"); process.exit(1); } writeFileSync(args[args.indexOf("-o") + 1], "mp4"); `, ); // ffmpeg: a cut from a stored URL (an http input) writes the window; a probe (a local input) passes. await writeFile( FFMPEG, `#!/usr/bin/env node import { appendFileSync, writeFileSync } from "node:fs"; const args = process.argv.slice(2); const input = args[args.indexOf("-i") + 1] ?? ""; if (input.startsWith("http")) { appendFileSync(${JSON.stringify(ARGS_LOG)}, JSON.stringify([...args, input]) + "\\n"); writeFileSync(args[args.length - 1], "mp4"); } process.exit(0); `, ); await chmod(YTDLP, 0o755); await chmod(FFMPEG, 0o755); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); process.env.YTDLP_BIN = YTDLP; process.env.FFMPEG_BIN = FFMPEG; // NO BROWSER, HOWEVER THIS FILE IS RUN. The real player-page opener stays off // under `node --test` (NODE_TEST_CONTEXT), but a bare `tsx ` sets no such // thing, and this file's stored-URL cuts then opened odysee.com in headless // Chromium. With a (empty) page fixture named, the opener opens nothing. process.env.E2E_ODYSEE_PAGE_FIXTURE = path.join(ROOT, "odysee-pages.json"); await writeFile(process.env.E2E_ODYSEE_PAGE_FIXTURE, "{}"); await writeFile(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0 })); await mkdir(path.join(ROOT, "channels", SLUG), { recursive: true }); await writeFile( path.join(ROOT, "channels", SLUG, "config.json"), JSON.stringify({ name: "Demo", handling: "youtube", url: "https://odysee.com/@demo", platform: "odysee" }), ); const page = (id: string) => `https://odysee.com/@demo:1/stream-${id}:${id}`; const cdn = (id: string) => `https://player.odycdn.com/v6/streams/${id}/f0.mp4`; async function video(id: string, withStored: boolean): Promise { const dir = path.join(ROOT, "channels", SLUG, "data", id); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "metadata.info.json"), JSON.stringify({ id, webpage_url: page(id), ...(withStored ? { formats: [{ format_id: "original", url: cdn(id), protocol: "https", ext: "mp4" }] } : {}), }), ); return dir; } const STORED = await video("s1", true); await video("n1", false); const { fetchWindowsAction } = await import("./fetchWindowsAction"); const { getRegistry } = await import("yt-dlp-transcript-common/jobs/registry"); const { recordDownloadBackoff } = await import("yt-dlp-transcript-common/jobs/downloadBackoff"); const { getPaths } = await import("yt-dlp-transcript-common/lib/paths"); await recordDownloadBackoff("odysee", getPaths()); // 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 items = [ { slug: SLUG, id: "s1", from: 1, to: 3, clipId: "r#1" }, { slug: SLUG, id: "n1", from: 1, to: 3, clipId: "r#2" }, ]; const targets = async () => (await readFile(ARGS_LOG, "utf8").catch(() => "")) .trim() .split("\n") .filter(Boolean) .map((l) => (JSON.parse(l) as string[]).at(-1)); test("a dry run says how many of a group's windows have a stored media URL", async () => { const r = await fetchWindowsAction({ items, requestedBy: "test", dryRun: true, storedUrl: "only" }); assert.ok(r.ok); if (!r.ok) return; assert.deepEqual( r.groups.map((g) => [g.platform, g.queueKey, g.items.length, g.storedUrlItems]), [["odysee", "clips:odysee", 2, 1]], ); const plain = await fetchWindowsAction({ items, requestedBy: "test", dryRun: true }); assert.ok(plain.ok); if (!plain.ok) return; assert.equal(plain.groups[0].storedUrlItems, undefined, "never: not even asked"); }); test("never (the default): the cooling platform's group is refused whole", async () => { const r = await fetchWindowsAction({ items, requestedBy: "test" }); assert.ok(!r.ok); if (r.ok) return; assert.match(r.error, /^odysee: odysee is in a rate-limit cooldown/); assert.deepEqual(await targets(), []); }); test("only: the window with a stored URL runs on clips:odysee and is cut from it; the other is refused", async () => { const r = await fetchWindowsAction({ items, requestedBy: "test", manifest: "m", storedUrl: "only" }); assert.ok(r.ok, JSON.stringify(r)); if (!r.ok) return; assert.deepEqual(r.jobs.map((j) => [j.platform, j.queueKey, j.items]), [["odysee", "clips:odysee", 1]]); assert.equal(r.refused.length, 1); assert.equal(r.refused[0].items, 1); assert.match(r.refused[0].error, /cooldown.*\(1 window\(s\) with no stored media URL; 1 cut from the stored URL\)/); const jobId = r.jobs[0].jobId; const reg = getRegistry(); // The job's end, its sidecar written: bounded by the job, not by 200 polls a loaded machine outlasts. await settleJob(jobId); assert.equal(reg.get(jobId)?.status, "done", `job ${jobId}: ${reg.get(jobId)?.status}`); assert.equal(reg.get(jobId)?.spec?.params?.storedUrl, "only", "a Retry keeps the mode"); assert.deepEqual(await targets(), [cdn("s1")], "no page request"); const sidecar = JSON.parse(await readFile(path.join(STORED, "clips", "1.00-3.00.json"), "utf8")); assert.equal(sidecar.source, "stored-media-url"); assert.equal(sidecar.sourceHost, "player.odycdn.com"); assert.equal(sidecar.manifest, "m"); });