// `wayback.auto` in the download lane (release 37 S2): which channel's row a // unit takes (opted in only, the lane's order, the focus hold, the platform // slot), each row once per runner, the minute-long cache; and a unit's // import-page job waited on to its end. Synthetic rows; the starter is the // seam. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/laneWaybackAuto.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "lane-wayback-")); process.env.TRANSCRIPTS_DIR = path.join(ROOT, "transcripts"); const { WaybackAutoPicker, waybackAutoOn, awaitJobEnd, launchWaybackAutoImport, WAYBACK_AUTO_PLATFORM } = await import("./laneWaybackAuto"); const { setImportPageStarterForTest } = await import("./waybackImport"); const { runManagedFunction } = await import("../jobs/streamCommand"); const { getPaths } = await import("../lib/paths"); const row = (key: string) => ({ key, originalUrl: key, path: key, digest: "D", firstTs: "20160101000000", lastTs: "20160101000000", count: 1, version: 1, versions: 1, isNew: true, imported: null, replayUrl: `https://web.archive.org/web/20160101000000/${key}` }) as const; const on = (targets = [{ url: "a.example", match: "host" }]) => ({ handling: "transcribe", wayback: { targets, auto: true } }); test("only opted-in channels, in the lane's order; each row once; none while the platform is skipped or outside a holding focus", async () => { let reads = 0; let clock = 0; const picker = new WaybackAutoPicker({ pending: async (slug) => { reads++; return slug === "b" ? [row("https://b.example/1"), row("https://b.example/2")] : slug === "c" ? [row("https://c.example/1")] : []; }, now: () => clock, ttlMs: 1000, }); assert.equal(waybackAutoOn(on()), true); assert.equal(waybackAutoOn({ wayback: { targets: [{ url: "x" }] } }), false); assert.equal(waybackAutoOn({}), false); const channels = [ { slug: "a", config: { handling: "transcribe", wayback: { targets: [{ url: "a.example" }] } } }, { slug: "b", config: on() }, { slug: "c", config: on() }, ]; const none = new Set(); assert.equal(await picker.pick(channels, { platformSkip: new Set([WAYBACK_AUTO_PLATFORM]) }), null); const p1 = await picker.pick(channels, { platformSkip: none }); assert.equal(p1?.slug, "b"); assert.equal(p1?.row.key, "https://b.example/1"); assert.equal(p1?.unitId, "wayback-import b https://b.example/1"); assert.equal((await picker.pick(channels, { platformSkip: none }))?.row.key, "https://b.example/2"); assert.equal((await picker.pick(channels, { platformSkip: none }))?.slug, "c"); assert.equal(await picker.pick(channels, { platformSkip: none }), null, "each row once in a runner's life"); const readsBefore = reads; await picker.pick(channels, { platformSkip: none }); assert.equal(reads, readsBefore, "cached inside the TTL"); clock = 5000; await picker.pick(channels, { platformSkip: none }); assert.ok(reads > readsBefore, "re-read after the TTL"); const focused = new WaybackAutoPicker({ pending: async (slug) => [row(`https://${slug}.example/1`)] }); assert.equal((await focused.pick(channels, { platformSkip: none, focus: { slugs: new Set(["c"]), holding: true } }))?.slug, "c"); assert.equal((await focused.pick(channels, { platformSkip: none, focus: { slugs: new Set(["c"]), holding: false } }))?.slug, "b"); }); test("a unit: the row's import-page job, waited on to its end; a refusal is a skip", async () => { const paths = getPaths(); const pick = { slug: "b", row: row("https://b.example/1"), unitId: "u" }; for (const [fail, outcome] of [[false, "transcribed"], [true, "failed"]] as const) { setImportPageStarterForTest(async () => { const r = await runManagedFunction({ kind: "import-page-test", queueKey: "", paths, fn: async () => { await new Promise((res) => setTimeout(res, 20)); if (fail) throw new Error("no"); }, }); if (!r.ok) throw new Error(r.error); void r.stream.cancel(); return { jobId: r.jobId }; }); let child = ""; const lines: string[] = []; const out = await launchWaybackAutoImport({ pick, onLog: (l) => lines.push(l), onChildJob: (j) => (child = j) }); assert.equal(out.outcome, outcome); assert.ok(child); assert.ok(["done", "failed"].includes(await awaitJobEnd(child)), "an ended job answers at once"); } setImportPageStarterForTest(async () => { throw new Error("held"); }); const lines: string[] = []; assert.deepEqual(await launchWaybackAutoImport({ pick, onLog: (l) => lines.push(l) }), { outcome: "skipped" }); assert.match(lines[0], /could not queue the Wayback import .*: held/); setImportPageStarterForTest(async () => ({ jobId: "seam-1" })); assert.deepEqual(await launchWaybackAutoImport({ pick, onLog: () => {} }), { outcome: "skipped" }, "an id the registry does not hold"); setImportPageStarterForTest(null); });