// The stored media URL (release 32 R5): only an allowlisted platform's single // direct format on its CDN host; anything else is null. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/storedMediaUrl.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 { STORED_URL_PLATFORMS, isStoredUrlMode, storedMediaUrlFromInfo, storedUrlWhyText, } from "./storedMediaUrl"; import { storedMediaUrl } from "./storedMediaUrl-server"; const PAGE = "https://odysee.com/@synthetic:1/stream-one:abc123"; const CDN = "https://player.odycdn.com/v6/streams/abc123/def456.mp4"; const odysee = (formats: unknown, extra: Record = {}) => ({ webpage_url: PAGE, extractor: "lbry", ...(formats === undefined ? {} : { formats }), ...extra, }); test("an Odysee record's single CDN format is its stored media URL", () => { assert.deepEqual(STORED_URL_PLATFORMS, ["odysee"]); assert.deepEqual( storedMediaUrlFromInfo(odysee([{ format_id: "original", url: CDN, protocol: "https", ext: "mp4" }])), { url: CDN, host: "player.odycdn.com", platform: "odysee", referer: "https://odysee.com/" }, ); }); test("no formats: the top-level url, on the same terms", () => { assert.equal(storedMediaUrlFromInfo(odysee(undefined, { url: CDN }))?.url, CDN); assert.equal(storedMediaUrlFromInfo(odysee([], { url: CDN }))?.host, "player.odycdn.com"); }); test("a host off the platform's CDN is ignored", () => { for (const url of [ "https://cdn.example.net/v6/streams/abc123/def456.mp4", "https://odycdn.com.example.net/x.mp4", "https://odysee.com/$/stream/abc123", "https://player.odycdn.com.evil/x.mp4", ]) { assert.equal(storedMediaUrlFromInfo(odysee([{ url, protocol: "https" }])), null, url); } }); test("an off-allowlist platform's stored URL is ignored, CDN-shaped or not", () => { for (const page of ["https://www.youtube.com/watch?v=abcdefghijk", "https://rumble.com/v1-x.html"]) { assert.equal(storedMediaUrlFromInfo({ webpage_url: page, formats: [{ url: CDN, protocol: "https" }] }), null, page); } // No page at all: no platform, no trust. assert.equal(storedMediaUrlFromInfo({ formats: [{ url: CDN }] }), null); }); test("more than one format, a manifest protocol, credentials or a non-http scheme: null", () => { assert.equal(storedMediaUrlFromInfo(odysee([{ url: CDN }, { url: CDN.replace("def456", "ghi789") }])), null); assert.equal(storedMediaUrlFromInfo(odysee([{ url: CDN.replace(".mp4", ".m3u8"), protocol: "m3u8_native" }])), null); assert.equal(storedMediaUrlFromInfo(odysee([{ url: "https://u:p@player.odycdn.com/x.mp4" }])), null); assert.equal(storedMediaUrlFromInfo(odysee([{ url: "ftp://player.odycdn.com/x.mp4" }])), null); assert.equal(storedMediaUrlFromInfo(odysee([{ url: 42 }])), null); assert.equal(storedMediaUrlFromInfo(null), null); assert.equal(storedMediaUrlFromInfo([]), null); }); test("the reader: the file's URL, or null for no file or a malformed one", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "storedurl-")); const good = path.join(dir, "good"); const bad = path.join(dir, "bad"); await mkdir(good); await mkdir(bad); await writeFile(path.join(good, "metadata.info.json"), JSON.stringify(odysee([{ url: CDN, protocol: "https" }]))); await writeFile(path.join(bad, "metadata.info.json"), "{ not json"); assert.equal((await storedMediaUrl(good))?.url, CDN); assert.equal(await storedMediaUrl(bad), null); assert.equal(await storedMediaUrl(path.join(dir, "absent")), null); }); test("modes and the log's words", () => { assert.ok(isStoredUrlMode("never") && isStoredUrlMode("fallback") && isStoredUrlMode("only")); assert.ok(!isStoredUrlMode("always") && !isStoredUrlMode(undefined)); assert.equal(storedUrlWhyText("only", "odysee"), "(asked: only)"); assert.equal(storedUrlWhyText("rate-limited", "odysee"), "— odysee lookup rate-limited"); assert.equal(storedUrlWhyText("held", "odysee"), "— odysee lookup held"); });