// One video's metadata from its page in a (fake) browser (release 32 R9): the // record written whole or patched, paced, and the channel's filters applied to // what it finds. No browser: testing/web/fakeOdyseePage.mjs answers. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/odyseeBrowserLookup.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 { browserLookupDecision, browserLookupGapKey, browserLookupVideo, browserLookupWanted, matchFiltersOf, titleMatchFilter, } from "./odyseeBrowserLookup"; import { readOdyseePage } from "./odyseePageReader"; import { clearPlatformGaps, platformGapRemainingMs } from "../jobs/platformGap"; import { cdnUrlOf, fakePageReader, pageUrlOf, syntheticChannel, syntheticClaim, videoPageRead, } from "../testing/web/fakeOdyseePage.mjs"; import type { ChannelConfig } from "../lib/channelConfig"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "browserlookup-")); const ch = syntheticChannel({ name: "@synthchan", title: "Synth Channel" }); const v1 = syntheticClaim({ name: "first-stream", title: "SynthShow - 01/02/2024 - Topics", channel: ch }); const other = syntheticClaim({ name: "recommended", title: "Someone Else", channel: syntheticChannel({ name: "@other" }) }); let n = 0; const dirOf = () => path.join(ROOT, `case-${++n}`, "data", v1.claim_id); const noLog = () => {}; const signal = new AbortController().signal; beforeEach(() => clearPlatformGaps()); test("no record yet: the whole record is written from the page, with its stored media URL", async () => { const reader = fakePageReader({ [pageUrlOf(v1)]: videoPageRead({ claim: v1, related: [other] }) }); const dir = dirOf(); const r = await browserLookupVideo({ videoDir: dir, pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader, gapMs: 0 } }); assert.equal(r.status, "written"); assert.ok("stored" in r && r.stored?.url === cdnUrlOf(v1)); const disk = JSON.parse(await readFile(path.join(dir, "metadata.info.json"), "utf8")); assert.equal(disk._source, "browser"); assert.equal(disk.id, v1.claim_id); assert.deepEqual(reader.calls, [{ pageUrl: pageUrlOf(v1), play: true, scrolls: 0 }], "a video page is played"); }); test("a record with no stored URL is patched (the format and what it lacks), in the history as browser-lookup", async () => { const dir = dirOf(); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "metadata.info.json"), JSON.stringify({ id: v1.claim_id, title: "Kept Title", webpage_url: pageUrlOf(v1), extractor: "lbry" }), ); const reader = fakePageReader({ "*": videoPageRead({ claim: v1 }) }); const r = await browserLookupVideo({ videoDir: dir, pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader, gapMs: 0 } }); assert.equal(r.status, "patched"); const disk = JSON.parse(await readFile(path.join(dir, "metadata.info.json"), "utf8")); assert.equal(disk.title, "Kept Title", "a key it has is never replaced"); assert.equal(disk.formats[0].url, cdnUrlOf(v1)); assert.equal(disk._source, undefined, "a patched yt-dlp record is not re-labelled"); assert.ok(disk._browserLookupAt); const history = JSON.parse(await readFile(path.join(dir, "metadata.history.json"), "utf8")); assert.equal(history.entries.at(-1).by, "browser-lookup"); const again = await browserLookupVideo({ videoDir: dir, pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader, gapMs: 0 } }); assert.equal(again.status, "unchanged"); }); test("a page that names no such claim is not-found; one that cannot be read is unreadable; nothing written", async () => { const dir = dirOf(); const wrong = fakePageReader({ "*": videoPageRead({ claim: other }) }); const r = await browserLookupVideo({ videoDir: dir, pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader: wrong, gapMs: 0 } }); assert.equal(r.status, "not-found"); const dead = fakePageReader({}); const r2 = await browserLookupVideo({ videoDir: dir, pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader: dead, gapMs: 0 } }); assert.equal(r2.status, "unreadable"); await assert.rejects(readFile(path.join(dir, "metadata.info.json"))); }); test("not an Odysee page: refused before any read", async () => { const reader = fakePageReader({ "*": videoPageRead({ claim: v1 }) }); const r = await browserLookupVideo({ videoDir: dirOf(), pageUrl: "https://www.youtube.com/watch?v=abcdefghijk", onLog: noLog, signal, deps: { reader } }); assert.equal(r.status, "refused"); assert.equal(reader.calls.length, 0); }); test("paced: the second lookup on the platform waits out the gap the first set", async () => { const reader = fakePageReader({ "*": videoPageRead({ claim: v1 }) }); const waits: number[] = []; const sleep = async (ms: number) => { waits.push(ms); clearPlatformGaps(); }; await browserLookupVideo({ videoDir: dirOf(), pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader, gapMs: 60_000, sleep } }); assert.deepEqual(waits, []); assert.ok(platformGapRemainingMs(browserLookupGapKey("odysee")) > 55_000); await browserLookupVideo({ videoDir: dirOf(), pageUrl: pageUrlOf(v1), onLog: noLog, signal, deps: { reader, gapMs: 60_000, sleep } }); assert.equal(waits.length, 1); }); test("the real reader never opens a browser under a test runner", async () => { const log = { s: "" }; const r = await readOdyseePage({ pageUrl: pageUrlOf(v1), signal, onLog: (s) => (log.s += s) }); assert.equal(r.ok, false); assert.match(r.error ?? "", /test runner/); }); test("the opt-in: the request's word, else the channel's", () => { assert.equal(browserLookupWanted(undefined, {}), false); assert.equal(browserLookupWanted(undefined, { browserLookup: true }), true); assert.equal(browserLookupWanted(false, { browserLookup: true }), false); assert.equal(browserLookupWanted(true, {}), true); }); test("the channel's yt-dlp title match filter is applied; one it cannot read refuses", () => { assert.deepEqual(matchFiltersOf({ ytdlpExtraArgs: ["--match-filters=title~='SynthShow'", "--match-filter", "title~=\"(?i)other\""] }), [ "title~='SynthShow'", 'title~="(?i)other"', ]); assert.ok(titleMatchFilter("title~='(?i)synthshow'")?.test("SYNTHSHOW live")); assert.equal(titleMatchFilter("duration>600"), null); assert.equal(titleMatchFilter("title~='a' & duration>600"), null); const cfg = (args: string[]) => ({ handling: "transcribe", ytdlpExtraArgs: args }) as ChannelConfig; const s = { skipLiveDownloads: true }; assert.deepEqual(browserLookupDecision(cfg(["--match-filters=title~='SynthShow'"]), { title: "SynthShow - x" }, s), { ok: true }); const no = browserLookupDecision(cfg(["--match-filters=title~='SynthShow'"]), { title: "A vlog" }, s); assert.equal(no.ok, false); assert.ok(!no.ok && no.skip && no.filter === "ytdlpMatchFilter"); const cannot = browserLookupDecision(cfg(["--match-filters=duration>600"]), { title: "SynthShow" }, s); assert.ok(!cannot.ok && !cannot.skip); assert.deepEqual(browserLookupDecision(cfg([]), { title: "anything" }, s), { ok: true }); });