import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { cdnUrlOf, channelPageRead, channelUrlOf, pageUrlOf, syntheticChannel, syntheticClaim, videoPageRead, } from "yt-dlp-transcript-common/testing/web/fakeOdyseePage.mjs"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/browser-lookup/route.test.ts" // // `/api/ops/browser-lookup` (release 32 R9) end to end: the body's checks, the // door, and the job over recorded page reads (E2E_ODYSEE_PAGE_FIXTURE — no // browser, no network): a record completed with the file its page named, and // a listing added to the top of the playlist. const ROOT = await mkdtemp(path.join(os.tmpdir(), "browser-lookup-route-")); const SLUG = "demo-odysee"; const FIXTURE = path.join(ROOT, "pages.json"); const ch = syntheticChannel({ name: "@demo", title: "Demo" }); const v1 = syntheticClaim({ name: "stream-one", title: "Stream One", channel: ch, releaseTime: 1700000000 }); const v2 = syntheticClaim({ name: "stream-two", title: "Stream Two", channel: ch, releaseTime: 1700300000 }); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); process.env.E2E_ODYSEE_PAGE_FIXTURE = FIXTURE; // No queue gap: the listing job is the second on the odysee queue, and the // default 20 s gap (stretched by pacing.jitter) outlasts settle()'s 20 s. await writeFile( process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0, pacing: { jobGapSeconds: { odysee: 0, default: 0 } } }), ); await writeFile( FIXTURE, JSON.stringify({ [pageUrlOf(v1)]: videoPageRead({ claim: v1 }), [channelUrlOf(ch)]: channelPageRead({ channel: ch, pages: [[v2, v1]] }), }), ); const CHANNEL = path.join(ROOT, "channels", SLUG); await mkdir(path.join(CHANNEL, "data", v1.claim_id), { recursive: true }); await writeFile( path.join(CHANNEL, "config.json"), JSON.stringify({ name: "Demo", handling: "transcribe", url: channelUrlOf(ch), platform: "odysee" }), ); await writeFile(path.join(CHANNEL, "playlist"), `${pageUrlOf(v1)}\n`); await writeFile( path.join(CHANNEL, "data", v1.claim_id, "metadata.info.json"), JSON.stringify({ id: v1.claim_id, title: "Stream One", webpage_url: pageUrlOf(v1), extractor: "lbry" }), ); const { POST } = await import("./route"); const { getRegistry } = await import("yt-dlp-transcript-common/jobs/registry"); // A job's end still writes after its status reads done (its log and sidecar, // and the report regeneration it arms in the channel dir): waited for before // the corpus is removed (testing/jobs/quiesce.ts). const { quiesceJobs, settleJob } = await import("yt-dlp-transcript-common/testing/jobs/quiesce.js"); test.after(async () => { await quiesceJobs(); await rm(ROOT, { recursive: true, force: true }); }); function post(body: Record): Promise { return POST( new Request("http://localhost/api/ops/browser-lookup", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); } async function settle(jobId: string): Promise { const reg = getRegistry(); await settleJob(jobId); return reg.get(jobId)?.status; } test("the body: ids or listing, scrolls bounded", async () => { assert.equal((await post({ slug: SLUG })).status, 400); assert.equal((await post({ slug: SLUG, listing: true, scrolls: 41 })).status, 400); assert.equal((await post({ slug: SLUG, ids: [] })).status, 400); }); test("ids: the record is completed with the file its page named", async () => { const res = await post({ slug: SLUG, ids: [v1.claim_id] }); assert.equal(res.status, 200, await res.clone().text()); const { jobId } = (await res.json()) as { jobId: string }; assert.equal(await settle(jobId), "done"); assert.deepEqual(getRegistry().get(jobId)?.spec?.params?.videoIds, [v1.claim_id], "a Retry keeps the ids"); const info = JSON.parse(await readFile(path.join(CHANNEL, "data", v1.claim_id, "metadata.info.json"), "utf8")); assert.equal(info.formats[0].url, cdnUrlOf(v1)); assert.equal(info.title, "Stream One"); const history = JSON.parse(await readFile(path.join(CHANNEL, "data", v1.claim_id, "metadata.history.json"), "utf8")); assert.equal(history.entries.at(-1).by, "browser-lookup"); }); test("listing: the page's new uploads go to the top of the playlist", async () => { const res = await post({ slug: SLUG, listing: true, scrolls: 2 }); assert.equal(res.status, 200, await res.clone().text()); const { jobId } = (await res.json()) as { jobId: string }; assert.equal(await settle(jobId), "done"); const playlist = (await readFile(path.join(CHANNEL, "playlist"), "utf8")).trim().split("\n"); assert.deepEqual(playlist, [pageUrlOf(v2), pageUrlOf(v1)]); });