// The download lane takes a cooling or held platform's stored-URL videos // (release 32 R9): the channel's opt-in, the filter, the per-video memo, and // what a failed stored-URL unit does to the platform (nothing). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/laneStoredUrl.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { makeStoredUrlLaneMemo, storedUrlLaneMode, storedUrlRescue } from "./laneStoredUrl"; import { applyUnitOutcome, type UnitOutcomeState } from "../jobs/unitOutcome"; import { isVideoDeferred } from "../jobs/platformBackoff"; import type { ChannelConfig } from "../lib/channelConfig"; const cfg = (over: Partial) => ({ handling: "transcribe", ...over }) as ChannelConfig; test("the opt-in: only/fallback, transcribe handling, a stored-URL platform", () => { assert.equal(storedUrlLaneMode(cfg({ storedUrlDownloads: "only" }), "odysee"), "only"); assert.equal(storedUrlLaneMode(cfg({ storedUrlDownloads: "fallback" }), "odysee"), "fallback"); assert.equal(storedUrlLaneMode(cfg({ storedUrlDownloads: "never" }), "odysee"), null); assert.equal(storedUrlLaneMode(cfg({}), "odysee"), null, "absent = never"); assert.equal(storedUrlLaneMode(cfg({ storedUrlDownloads: "only", handling: "youtube" }), "odysee"), null); assert.equal(storedUrlLaneMode(cfg({ storedUrlDownloads: "only" }), "youtube"), null); }); test("the filter keeps a skipped platform's opted-in videos that store a URL, and lists the unread", () => { const owner = new Map([ ["o1", "ody"], ["o2", "ody"], ["o3", "ody"], ["n1", "ody-plain"], ["y1", "yt"], ]); const platforms: Record = { ody: "odysee", "ody-plain": "odysee", yt: "youtube" }; const known: Record = { o1: true, o2: false }; const r = storedUrlRescue({ pending: { leaf: ["o1", "o2", "o3", "n1", "y1"], other: ["o3"] }, owner, platformSkip: new Set(["odysee"]), rescuable: new Set(["odysee"]), platformOf: (slug) => platforms[slug], optedIn: (slug) => slug === "ody", known: (_slug, id) => known[id], }); assert.deepEqual(r.pending, { leaf: ["o1", "y1"], other: [] }); assert.deepEqual([...r.storedIds], ["o1"]); assert.deepEqual(r.unknown, [{ slug: "ody", id: "o3" }], "once, though it is in two leaves"); }); test("a skipped platform that is not rescuable (busy, or its stored-URL rest not over) keeps nothing", () => { const r = storedUrlRescue({ pending: { leaf: ["o1", "y1"] }, owner: new Map([ ["o1", "ody"], ["y1", "yt"], ]), platformSkip: new Set(["odysee"]), rescuable: new Set(), platformOf: (slug) => (slug === "ody" ? "odysee" : "youtube"), optedIn: () => true, known: () => true, }); assert.deepEqual(r.pending, { leaf: ["y1"] }); assert.equal(r.storedIds.size, 0); assert.deepEqual(r.unknown, []); }); test("the memo reads a record once, at most the limit per call, and a trusted URL reads true", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "lanestored-")); const dir = (id: string) => path.join(root, "ody", "data", id); await mkdir(dir("a"), { recursive: true }); await writeFile( path.join(dir("a"), "metadata.info.json"), JSON.stringify({ id: "a", webpage_url: "https://odysee.com/@x:1/a:1", formats: [{ format_id: "original", url: "https://player.odycdn.com/v6/streams/a/sd.mp4", protocol: "https" }], }), ); await mkdir(dir("b"), { recursive: true }); let reads = 0; const memo = makeStoredUrlLaneMemo(async (d) => { reads++; const { storedMediaUrl } = await import("../lib/storedMediaUrl-server"); return storedMediaUrl(d); }); const ids = [ { slug: "ody", id: "a" }, { slug: "ody", id: "b" }, { slug: "ody", id: "c" }, ]; assert.equal(await memo.probe(root, ids, 2), 2); assert.equal(memo.known("ody", "a"), true); assert.equal(memo.known("ody", "b"), false); assert.equal(memo.known("ody", "c"), undefined, "past the limit: next tick"); assert.equal(await memo.probe(root, ids, 2), 1); assert.equal(reads, 3, "each record read once"); }); const empty = (): UnitOutcomeState => ({ platformBackoff: {}, videoDeferrals: {}, platformPace: {}, platformHolds: {}, subtitleDeferrals: {}, }); test("a failed stored-URL unit: a CDN refusal defers the video and backs nothing off", () => { const s = empty(); const now = 1_000_000; const r = applyUnitOutcome( s, { platform: "odysee", videoId: "v1", channelSlug: "ody", outcome: "failed", failureClass: "rate_limit", viaStoredUrl: true }, now, ); assert.equal(r.markCompleted, false); assert.ok(isVideoDeferred(s.videoDeferrals, "v1", now + 1)); assert.deepEqual(s.platformBackoff, {}, "the platform is not backed off"); assert.deepEqual(s.platformPace, {}); assert.match(r.line ?? "", /stored media URL refused it \(rate_limit\).*odysee is not backed off/); }); test("a failed stored-URL unit for any other reason retires the video, touching nothing", () => { const s = empty(); const r = applyUnitOutcome( s, { platform: "odysee", videoId: "v2", channelSlug: "ody", outcome: "failed", failureClass: "per_video", viaStoredUrl: true }, 1_000_000, ); assert.equal(r.markCompleted, true); assert.deepEqual(s.platformBackoff, {}); assert.deepEqual(s.videoDeferrals, {}); });