// downloadOneManaged with the browser lookup (release 32 R9): a record that // stores no media URL is read from its page (a fake page reader — no browser) // and downloaded from the file the page named, from a fake CDN on loopback; // the channel's title match filter still decides; off, nothing changes. Real // ffmpeg for the audio check (skipped without it). No real network. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/browserLookupDownload.test.ts 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"; import { hasFfmpeg, makeToneFile } from "../testing/audio/corruptAudio.mjs"; import { startFakeRangedCdn, type FakeRangedCdn } from "../testing/web/fakeRangedCdn.mjs"; import { fakePageReader, syntheticChannel, syntheticClaim, videoPageRead } from "../testing/web/fakeOdyseePage.mjs"; import type { ChannelConfig } from "../lib/channelConfig"; import type { Paths } from "../lib/paths"; import { storedUrlAttemptOf } from "../lib/downloadOutcome"; import { clearPlatformGaps } from "../jobs/platformGap"; import { downloadOneManaged, type ManagedDownloadOpts } from "./downloadOneManaged"; const FF = hasFfmpeg(); const CLAIM = "FAKE-TEST-VALUEFAKE-TEST-VALUEfedcba98"; const FILE_PATH = `/v6/streams/${CLAIM}/d4e5f6.mp4`; const PAGE = `https://odysee.com/synthetic-stream:${CLAIM}`; const FAST = { intervalMs: 0, sizeGateBytes: 0, maxRollbacks: 3 }; const ch = syntheticChannel({ name: "@synth", title: "Synth" }); type Fixture = { root: string; videoDir: string; ytdlpLog: string; paths: Paths; cdn: FakeRangedCdn }; async function fixture(opts: { prefetch?: "ok" | "429" } = {}): Promise { const root = await mkdtemp(path.join(os.tmpdir(), "browser-download-")); const channelsDir = path.join(root, "channels"); const videoDir = path.join(channelsDir, "c", "data", CLAIM); await mkdir(videoDir, { recursive: true }); const clean = FF ? await readFile(makeToneFile(path.join(root, "src.mp4"), { duration: 30, faststart: true })) : Buffer.alloc(1); const cdn = await startFakeRangedCdn({ files: { [FILE_PATH]: clean } }); const ytdlpLog = path.join(root, "ytdlp.log"); const bin = path.join(root, "fake-ytdlp.sh"); const line429 = `echo "ERROR: [lbry] ${CLAIM}: Unable to download webpage: HTTP Error 429: Too Many Requests" >&2; exit 1`; await writeFile( bin, `#!/bin/sh\necho "$@" >> ${JSON.stringify(ytdlpLog)}\n` + `case "$*" in *--skip-download*) ${opts.prefetch === "429" ? line429 : "exit 0"} ;; esac\nexit 0\n`, ); await chmod(bin, 0o755); const paths = { channelsDir, ytdlpBin: bin, ffmpegBin: "ffmpeg", ffprobeBin: "ffprobe" } as Paths; return { root, videoDir, ytdlpLog, paths, cdn }; } function claim(title = "SynthShow - 01/02/2024") { return syntheticClaim({ name: "synthetic-stream", title, channel: ch, claimId: CLAIM, duration: 30 }); } function run(f: Fixture, reader: ReturnType, over: Partial = {}, log = { s: "" }) { return downloadOneManaged({ channelSlug: "c", channelConfig: { handling: "transcribe", url: "https://odysee.com/@synth", audioFormat: "mp3", browserLookup: true } as ChannelConfig, paths: f.paths, videoUrl: PAGE, onLog: (s) => (log.s += s), signal: new AbortController().signal, storedUrlDeps: { fetch: f.cdn.fetch, chunkBytes: 16 * 1024, knobs: FAST, gapMs: 0, openPage: null }, browserLookupDeps: { reader, gapMs: 0 }, ...over, }); } async function done(f: Fixture) { clearPlatformGaps(); await f.cdn.close(); await rm(f.root, { recursive: true, force: true }); } test("only, no record: the page is read, the record written, the file downloaded from the URL it named", { skip: !FF }, async () => { const f = await fixture(); try { const reader = fakePageReader({ [PAGE]: videoPageRead({ claim: claim(), streamingUrl: f.cdn.url(FILE_PATH) }) }); const log = { s: "" }; const record = await run(f, reader, { storedUrl: "only" }, log); assert.equal(record.status, "ok-audio-checked", log.s); assert.equal(reader.calls.length, 1); assert.equal(storedUrlAttemptOf(record)?.why, "only"); const info = JSON.parse(await readFile(path.join(f.videoDir, "metadata.info.json"), "utf8")); assert.equal(info._source, "browser"); assert.equal(info.formats[0].url, f.cdn.url(FILE_PATH)); assert.match(log.s, /stores no media URL; reading its record from the page in a browser/); } finally { await done(f); } }); test("fallback, the prefetch rate-limited and no record: the browser lookup, then the stored-URL download", { skip: !FF }, async () => { const f = await fixture({ prefetch: "429" }); try { const reader = fakePageReader({ "*": videoPageRead({ claim: claim(), streamingUrl: f.cdn.url(FILE_PATH) }) }); const log = { s: "" }; const record = await run(f, reader, { storedUrl: "fallback" }, log); assert.equal(record.status, "ok-audio-checked", log.s); assert.equal(storedUrlAttemptOf(record)?.why, "rate-limited", "the platform still backs off"); assert.equal(reader.calls.length, 1); } finally { await done(f); } }); test("the channel's title match filter declines what the page found: skipped, no CDN read", async () => { const f = await fixture(); try { const reader = fakePageReader({ "*": videoPageRead({ claim: claim("A vlog"), streamingUrl: f.cdn.url(FILE_PATH) }) }); const record = await run(f, reader, { storedUrl: "only", channelConfig: { handling: "transcribe", url: "https://odysee.com/@synth", browserLookup: true, ytdlpExtraArgs: ["--match-filters=title~='SynthShow'"], } as ChannelConfig, }); assert.equal(record.status, "skipped-filtered"); assert.equal(record.filter?.name, "ytdlpMatchFilter"); assert.equal(f.cdn.requests.length, 0); } finally { await done(f); } }); test("browserLookup off: no page is read, and `only` with no record fails asking nothing (as before)", async () => { const f = await fixture(); try { const reader = fakePageReader({ "*": videoPageRead({ claim: claim(), streamingUrl: f.cdn.url(FILE_PATH) }) }); const record = await run(f, reader, { storedUrl: "only", channelConfig: { handling: "transcribe", url: "https://odysee.com/@synth" } as ChannelConfig, }); assert.equal(record.status, "failed"); assert.equal(record.failureClass, "per_video"); assert.equal(reader.calls.length, 0); } finally { await done(f); } });