// A channel's listing from its page in a (fake) browser, and the browser // lookup job's door (release 32 R9). No browser: testing/web/fakeOdyseePage.mjs. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/odyseeBrowserListing.test.ts import { test, beforeEach } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { browserListChannel, browserListingRefusal } from "./odyseeBrowserListing"; import { browserLookupSummaryOf, runBrowserLookupJob } from "./browserLookupJob"; import { clearPlatformGaps } from "../jobs/platformGap"; import { channelPageRead, channelUrlOf, fakePageReader, pageUrlOf, syntheticChannel, syntheticClaim, } from "../testing/web/fakeOdyseePage.mjs"; import type { ChannelConfig } from "../lib/channelConfig"; import type { Paths } from "../lib/paths"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "browserlisting-")); const ch = syntheticChannel({ name: "@synthchan", title: "Synth Channel" }); const show1 = syntheticClaim({ name: "show-1", title: "SynthShow - 1", channel: ch, releaseTime: 1700000000 }); const show2 = syntheticClaim({ name: "show-2", title: "SynthShow - 2", channel: ch, releaseTime: 1700200000 }); const vlog = syntheticClaim({ name: "vlog", title: "A vlog", channel: ch, releaseTime: 1700100000 }); const stranger = syntheticClaim({ name: "theirs", title: "SynthShow - not ours", channel: syntheticChannel({ name: "@other" }) }); const signal = new AbortController().signal; const noLog = () => {}; let n = 0; async function channel(config: Partial, playlist?: string[]) { const channelsDir = path.join(ROOT, `case-${++n}`, "channels"); const slug = "synth"; await mkdir(path.join(channelsDir, slug), { recursive: true }); const cfg = { handling: "transcribe", platform: "odysee", url: channelUrlOf(ch), ...config } as ChannelConfig; await writeFile(path.join(channelsDir, slug, "config.json"), JSON.stringify(cfg)); if (playlist) await writeFile(path.join(channelsDir, slug, "playlist"), playlist.join("\n") + "\n"); return { paths: { channelsDir } as Paths, slug, cfg, root: path.join(channelsDir, slug) }; } beforeEach(() => clearPlatformGaps()); test("the listing adds the page's own new uploads at the top of the playlist, newest first, and to the roster", async () => { const c = await channel({}, [pageUrlOf(show1)]); const reader = fakePageReader({ [channelUrlOf(ch)]: channelPageRead({ channel: ch, pages: [[show2, stranger], [vlog, show1]] }) }); const r = await browserListChannel({ paths: c.paths, slug: c.slug, config: c.cfg, scrolls: 2, skipLiveDownloads: true, onLog: noLog, signal, deps: { reader, gapMs: 0 }, }); assert.equal(r.status, "listed"); assert.ok(r.status === "listed" && r.seen === 3 && r.added.length === 2); const playlist = (await readFile(path.join(c.root, "playlist"), "utf8")).trim().split("\n"); assert.deepEqual(playlist, [pageUrlOf(show2), pageUrlOf(vlog), pageUrlOf(show1)], "new first, nothing removed"); const roster = JSON.parse(await readFile(path.join(c.root, "roster.json"), "utf8")); assert.ok(JSON.stringify(roster).includes(show2.claim_id)); assert.equal(reader.calls[0].scrolls, 2); }); test("the channel's title match filter decides what is listed", async () => { const c = await channel({ ytdlpExtraArgs: ["--match-filters=title~='SynthShow'"] }); const reader = fakePageReader({ "*": channelPageRead({ channel: ch, pages: [[show2, vlog, show1]] }) }); const r = await browserListChannel({ paths: c.paths, slug: c.slug, config: c.cfg, skipLiveDownloads: true, onLog: noLog, signal, deps: { reader, gapMs: 0 } }); assert.ok(r.status === "listed" && r.filtered === 1); const playlist = (await readFile(path.join(c.root, "playlist"), "utf8")).trim().split("\n"); assert.deepEqual(playlist, [pageUrlOf(show2), pageUrlOf(show1)]); }); test("refused before anything opens: not Odysee, no @channel, an unreadable match filter", async () => { assert.match(browserListingRefusal({ url: "https://www.youtube.com/@x" } as ChannelConfig) ?? "", /Odysee|odysee/); assert.match(browserListingRefusal({ url: "https://odysee.com/some-stream:abc" } as ChannelConfig) ?? "", /@channel/); assert.match( browserListingRefusal({ url: channelUrlOf(ch), ytdlpExtraArgs: ["--match-filter", "duration>600"] } as ChannelConfig) ?? "", /cannot be applied/, ); assert.equal(browserListingRefusal({ url: channelUrlOf(ch) } as ChannelConfig), null); }); test("the job's door: nothing named, too many, an id with no Odysee page", async () => { const c = await channel({}, [pageUrlOf(show1)]); const base = { paths: c.paths, slug: c.slug, channelConfig: c.cfg, skipLiveDownloads: true }; assert.equal((await runBrowserLookupJob({ ...base })).ok, false); const many = await runBrowserLookupJob({ ...base, videoIds: Array.from({ length: 51 }, (_, i) => `v${i}`) }); assert.match((many as { error: string }).error, /At most 50/); const yt = await channel({ platform: "youtube", url: "https://www.youtube.com/@synth" } as Partial); const notOdysee = await runBrowserLookupJob({ paths: yt.paths, slug: yt.slug, channelConfig: yt.cfg, skipLiveDownloads: true, videoIds: ["abcdefghijk"] }); assert.match((notOdysee as { error: string }).error, /No Odysee page URL for abcdefghijk/); }); test("the summary line reads back", () => { const log = `x\nsummary: {"videos":[{"id":"a","status":"written","stored":true}]}\n`; assert.deepEqual(browserLookupSummaryOf(log), { videos: [{ id: "a", status: "written", stored: true }] }); assert.equal(browserLookupSummaryOf("nothing"), null); });