import { test } from "node:test"; import assert from "node:assert/strict"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/metadataScanBatch.test.ts // // THE LANE'S SCAN IS A BATCH (`maxTargets`). On 2026-10-25 an unbatched scan of // h3-podcast's 1,163 unread titles held platform:youtube for 3 h 16 m and no // download ran. A batch reads the first N targets in listing order, reports how // many it left, and the next batch picks up after them. A fake yt-dlp prints // one record per batch-file URL; getPaths() memoizes, so the env is set before // anything imports it. const ROOT = mkdtempSync(path.join(os.tmpdir(), "scan-batch-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); writeFileSync(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0 }) + "\n"); const CALLS = path.join(ROOT, "calls.log"); const BIN = path.join(ROOT, "fake-ytdlp.sh"); writeFileSync( BIN, `#!/bin/sh batch="" while [ $# -gt 0 ]; do if [ "$1" = "-a" ]; then batch="$2"; fi shift done echo run >> "${CALLS}" while read -r url; do [ -z "$url" ] && continue id=\${url##*v=} printf '{"id":"%s","title":"Episode %s","description":"","upload_date":"20261025","webpage_url":"%s"}\\n' "$id" "$id" "$url" done < "$batch" exit 0 `, ); chmodSync(BIN, 0o755); process.env.YTDLP_BIN = BIN; const IDS = ["aaaaaaaaaa1", "bbbbbbbbbb2", "ccccccccccc", "ddddddddddd", "eeeeeeeeeee"]; mkdirSync(path.join(ROOT, "channels", "c", "data"), { recursive: true }); writeFileSync( path.join(ROOT, "channels", "c", "playlist"), IDS.map((id) => `https://www.youtube.com/watch?v=${id}`).join("\n") + "\n", ); const { runMetadataScan } = await import("./metadataScan"); const { loadMetadataScan } = await import("../controller/metadataScanStore"); const { getPaths } = await import("../lib/paths"); const config = { handling: "youtube", url: "https://www.youtube.com/@c/videos" } as never; const scan = (maxTargets?: number) => runMetadataScan({ paths: getPaths(), channelSlug: "c", channelConfig: config, onLog: () => {}, signal: new AbortController().signal, ...(maxTargets ? { maxTargets } : {}), }); test("a batch reads the first N targets in listing order and reports the rest", async () => { const first = await scan(2); assert.equal(first.targets, 2); assert.equal(first.remaining, 3); assert.equal(first.stopped, undefined); assert.deepEqual( Object.keys((await loadMetadataScan(getPaths(), "c")).entries).sort(), IDS.slice(0, 2), ); const second = await scan(2); assert.equal(second.targets, 2); assert.equal(second.remaining, 1); assert.deepEqual( Object.keys((await loadMetadataScan(getPaths(), "c")).entries).sort(), IDS.slice(0, 4), ); }); test("no maxTargets takes the whole backlog (the operator's Run)", async () => { const last = await scan(); assert.equal(last.targets, 1); assert.equal(last.remaining, 0); assert.equal(Object.keys((await loadMetadataScan(getPaths(), "c")).entries).length, 5); // One yt-dlp process per batch: 2 + 2 + 1 targets in three runs. assert.equal(readFileSync(CALLS, "utf8").trim().split("\n").length, 3); const none = await scan(2); assert.deepEqual(none, { scanned: 0, errors: 0, targets: 0, remaining: 0 }); });