// Reading an Odysee record out of a page's own API answers (release 32 R9), // over synthetic answers in the LBRY SDK's shape (testing/web/fakeOdyseePage.mjs). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/odyseePageApi.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { claimForPage, claimsFromResponses, isOdyseeApiUrl, listingFromResponses, odyseeInfoFromClaim, odyseePageRef, originalFileUrl, streamingUrlFor, channelClaimFor, } from "./odyseePageApi"; import { storedMediaUrlFromInfo } from "./storedMediaUrl"; import { cdnUrlOf, channelPageRead, pageUrlOf, refusedAnswer, syntheticChannel, syntheticClaim, videoPageRead, } from "../testing/web/fakeOdyseePage.mjs"; const ch = syntheticChannel({ name: "@synthchan", title: "Synth Channel" }); const v1 = syntheticClaim({ name: "first-stream", title: "First Stream", channel: ch, releaseTime: 1700000000, duration: 4321 }); const v2 = syntheticClaim({ name: "second-stream", title: "Second Stream", channel: ch, releaseTime: 1700100000 }); const other = syntheticClaim({ name: "recommended", title: "Someone Else", channel: syntheticChannel({ name: "@other" }) }); test("the API hosts, and nothing else", () => { assert.ok(isOdyseeApiUrl("https://api.na-backend.odysee.com/api/v1/proxy?m=resolve")); assert.ok(isOdyseeApiUrl("https://api.odysee.com/file/view_count")); assert.ok(isOdyseeApiUrl("https://api.lbry.tv/api/v1/proxy")); assert.ok(!isOdyseeApiUrl("https://player.odycdn.com/v6/streams/x/y.mp4")); assert.ok(!isOdyseeApiUrl("https://odysee.com/@x")); assert.ok(!isOdyseeApiUrl("not a url")); }); test("claims come out of resolve, claim_search and bare answers; a refusal adds nothing", () => { const read = videoPageRead({ claim: v1, related: [other, v2] }); const claims = claimsFromResponses([...read.responses, refusedAnswer("resolve")]); assert.deepEqual([...claims.keys()].sort(), [v1.claim_id, v2.claim_id, other.claim_id].sort()); }); test("the page's claim: by the URL's claim id (full or a prefix), never 'the only one'", () => { const claims = claimsFromResponses(videoPageRead({ claim: v1, related: [other] }).responses); assert.equal(claimForPage(claims, pageUrlOf(v1))?.claim_id, v1.claim_id); assert.equal(claimForPage(claims, `https://odysee.com/first-stream:${v1.claim_id.slice(0, 3)}`)?.claim_id, v1.claim_id); assert.equal(claimForPage(claims, "https://odysee.com/first-stream")?.claim_id, v1.claim_id, "by name, one match"); assert.equal(claimForPage(claims, "https://odysee.com/not-here:abc")?.claim_id, undefined); assert.deepEqual(odyseePageRef(`https://odysee.com/@synthchan:5/first-stream:${v1.claim_id}`), { name: "first-stream", claimRef: v1.claim_id, channel: "@synthchan", }); }); test("the file: the get answer's streaming_url, else the player's CDN request; v3 for an /api/ path", () => { const read = videoPageRead({ claim: v1 }); assert.equal(streamingUrlFor(v1.claim_id, read.responses, read.mediaUrls), cdnUrlOf(v1)); const noGet = videoPageRead({ claim: v1, get: false }); assert.equal(streamingUrlFor(v1.claim_id, noGet.responses, noGet.mediaUrls), cdnUrlOf(v1), "the CDN request, query dropped"); assert.equal(streamingUrlFor(v1.claim_id, noGet.responses, []), null); assert.equal(originalFileUrl("https://cdn.example/api/v4/streams/free/x/y"), "https://cdn.example/api/v3/streams/free/x/y"); }); test("the record yt-dlp would write, marked _source: browser, and the stored-URL reader trusts it", () => { const info = odyseeInfoFromClaim(v1, { pageUrl: pageUrlOf(v1), streamingUrl: cdnUrlOf(v1), at: "2026-10-17T00:00:00.000Z" }); assert.equal(info.id, v1.claim_id); assert.equal(info.title, "First Stream"); assert.equal(info.duration, 4321); assert.equal(info.release_timestamp, 1700000000); assert.equal(info.upload_date, "20231114", "from the timestamp, UTC, as yt-dlp derives it"); assert.equal(info.channel, "Synth Channel"); assert.equal(info.uploader_id, "@synthchan"); assert.equal(info.extractor, "lbry"); assert.equal(info.webpage_url, pageUrlOf(v1)); assert.equal(info._source, "browser"); assert.equal((info.formats as unknown[]).length, 1); assert.equal(info.filesize, 123456789, "the SDK's string size, as a number"); const stored = storedMediaUrlFromInfo(info); assert.equal(stored?.url, cdnUrlOf(v1)); assert.equal(stored?.host, "player.odycdn.com"); const bare = odyseeInfoFromClaim(v1, { pageUrl: pageUrlOf(v1), streamingUrl: null }); assert.equal(bare.formats, undefined); assert.equal(storedMediaUrlFromInfo(bare), null); }); test("a channel page's listing: its own uploads, newest first, as listing URLs", () => { const read = channelPageRead({ channel: ch, pages: [[v1, other], [v2]] }); const claim = channelClaimFor(read.responses, "@synthchan"); assert.equal(claim?.claimId, ch.claim_id); const listing = listingFromResponses(read.responses, { claimId: claim?.claimId, name: "@synthchan" }); assert.deepEqual( listing.map((l) => l.url), [pageUrlOf(v2), pageUrlOf(v1)], ); assert.deepEqual(listingFromResponses(read.responses, { name: "@synthchan" }).length, 2, "by name when no id"); assert.deepEqual(listingFromResponses(read.responses, {}), [], "no channel, no listing"); });