import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import type { MetadataScan } from "../controller/metadataScanStore"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/partitionDownloadBatch.test.ts // // THE BATCH PREFILTER HONOURS THE SCAN'S VERDICTS. On 2026-10-25 a // download-missing on h3-podcast excluded 2 members-only videos (the two with an // availability.json) and walked into the 277 the metadata scan had read as // members-only that night: four yt-dlp runs each, nothing landed. The snapshot // (and so the download lane) already skipped them; the batch did not. const ROOT = mkdtempSync(path.join(os.tmpdir(), "partition-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 DATA = path.join(ROOT, "channels", "c", "data"); mkdirSync(DATA, { recursive: true }); const { partitionDownloadBatch } = await import("./runYtdlp"); const AT = "2026-10-25T07:00:00.000Z"; const url = (id: string) => `https://www.youtube.com/watch?v=${id}`; function onDisk(id: string, availability: string): void { mkdirSync(path.join(DATA, id), { recursive: true }); writeFileSync( path.join(DATA, id, "availability.json"), JSON.stringify({ checkedAt: AT, availability }) + "\n", ); } function scanWith( errors: Record, entries: Record = {}, ): MetadataScan { return { version: 1, updatedAt: AT, lastRun: null, entries: Object.fromEntries( Object.entries(entries).map(([id, title]) => [ id, { title, description: "", uploadDate: "", scannedAt: AT }, ]), ), errors: Object.fromEntries( Object.entries(errors).map(([id, cls]) => [ id, { class: cls, message: "Join this channel …", at: AT }, ]), ), }; } test("the scan's members-only and private verdicts exclude a video with no directory", async () => { const r = await partitionDownloadBatch({ urls: ["memberAAAAA", "privateBBBB", "plainCCCCCC"].map(url), dataDir: DATA, scan: scanWith({ memberAAAAA: "members_only", privateBBBB: "private" }), config: null, deferNeedsAuth: false, }); assert.deepEqual(r.fetch, [url("plainCCCCCC")]); assert.deepEqual(r.excludedCounts, { members_only: 1, deleted: 0, private: 1 }); assert.equal(r.excludedByScan, 2); }); test("the scan's `deleted` is not evidence enough (a soft block reads the same)", async () => { const r = await partitionDownloadBatch({ urls: [url("deletedDDDD")], dataDir: DATA, scan: scanWith({ deletedDDDD: "deleted" }), config: null, deferNeedsAuth: false, }); assert.deepEqual(r.fetch, [url("deletedDDDD")]); assert.equal(r.excludedByScan, 0); }); test("a conclusive on-disk availability wins over the scan; an on-disk `error` does not", async () => { onDisk("publicEEEEE", "public"); onDisk("errorFFFFFF", "error"); const r = await partitionDownloadBatch({ urls: [url("publicEEEEE"), url("errorFFFFFF")], dataDir: DATA, scan: scanWith({ publicEEEEE: "members_only", errorFFFFFF: "members_only" }), config: null, deferNeedsAuth: false, }); assert.deepEqual(r.fetch, [url("publicEEEEE")]); assert.equal(r.excludedCounts.members_only, 1); assert.equal(r.excludedByScan, 1); }); test("an on-disk exclusion is counted, but not as the scan's", async () => { onDisk("diskMemberG", "members_only"); const r = await partitionDownloadBatch({ urls: [url("diskMemberG")], dataDir: DATA, scan: scanWith({}), config: null, deferNeedsAuth: false, }); assert.deepEqual(r.fetch, []); assert.equal(r.excludedCounts.members_only, 1); assert.equal(r.excludedByScan, 0); }); test("the download filter's settled set is checked first; needs_auth defers only under defer", async () => { onDisk("needsAuthHH", "needs_auth"); const scan = scanWith( { leftoverIII: "members_only" }, { leftoverIII: "Leftovers #12", episodeJJJJ: "H3 Podcast #300" }, ); const config = { downloadFilter: { exclude: "Leftovers (Live )?#\\d+" } }; const urls = ["leftoverIII", "episodeJJJJ", "needsAuthHH"].map(url); const deferred = await partitionDownloadBatch({ urls, dataDir: DATA, scan, config: config as never, deferNeedsAuth: true, }); // The scan read leftoverIII's title before it ever errored — an entry wins // over an error in the store, but either way the filter settles it first. assert.equal(deferred.settledByFilterCount, 1); assert.equal(deferred.deferredAuthCount, 1); assert.deepEqual(deferred.fetch, [url("episodeJJJJ")]); const always = await partitionDownloadBatch({ urls, dataDir: DATA, scan, config: config as never, deferNeedsAuth: false, }); assert.deepEqual(always.fetch, [url("episodeJJJJ"), url("needsAuthHH")]); }); // SKIP SHORTS (lib/skipShorts.ts): a `/shorts/` listing line is out of every // batch on a skipShorts channel — counted, never fetched — and only there. test("a skipShorts channel's batch leaves its /shorts/ entries out; any other channel fetches them", async () => { const short = (id: string) => `https://www.youtube.com/shorts/${id}`; const urls = [url("uploadKKKKK"), short("shortLLLLLL"), short("shortMMMMMM")]; const skipping = await partitionDownloadBatch({ urls, dataDir: DATA, scan: scanWith({}), config: { skipShorts: true }, deferNeedsAuth: false, }); assert.deepEqual(skipping.fetch, [url("uploadKKKKK")]); assert.equal(skipping.shortsSkippedCount, 2); for (const config of [null, {}, { skipShorts: false }] as const) { const keeping = await partitionDownloadBatch({ urls, dataDir: DATA, scan: scanWith({}), config: config as never, deferNeedsAuth: false, }); assert.deepEqual(keeping.fetch, urls); assert.equal(keeping.shortsSkippedCount, 0); } });