import { test } from "node:test"; import assert from "node:assert/strict"; import { chmodSync, mkdirSync, mkdtempSync, 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/platformClean.test.ts // // A CLEAN MANUAL RUN SETTLES THE PLATFORM — ONLY IF IT ASKED THE SOURCE // SOMETHING (release 17 slice RL, re-review R3). runYtdlp calls // `onPlatformClean` after a run that neither threw nor recorded a backoff; a // run that made no request (download-missing with nothing to fetch) must not, // or it would lift a due hold on no evidence. getPaths() memoizes, so the env // is set before anything imports it. const ROOT = mkdtempSync(path.join(os.tmpdir(), "platform-clean-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); writeFileSync( process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0, sleepBetweenDownloadsSeconds: 0 }) + "\n", ); const BIN = path.join(ROOT, "fake-ytdlp.sh"); writeFileSync(BIN, "#!/bin/sh\nexit 0\n"); chmodSync(BIN, 0o755); process.env.YTDLP_BIN = BIN; mkdirSync(path.join(ROOT, "channels", "c", "data"), { recursive: true }); // An empty saved playlist: download-missing has nothing to fetch. writeFileSync(path.join(ROOT, "channels", "c", "playlist"), ""); const { runYtdlp } = await import("./runYtdlp"); const { getPaths } = await import("../lib/paths"); async function cleanCalls(mode: "download-missing" | "download-one-audio"): Promise { let calls = 0; await runYtdlp({ channelSlug: "c", mode, channelConfig: { handling: "transcribe", url: "https://www.youtube.com/@c/videos" } as never, paths: getPaths(), onLog: () => {}, signal: new AbortController().signal, singleVideoUrl: "https://www.youtube.com/watch?v=abcdefghijk", onPlatformClean: async () => { calls++; return null; }, }); return calls; } test("a run that made no request is not a clean run", async () => { assert.equal(await cleanCalls("download-missing"), 0); }); test("a run that made a request and recorded no backoff is", async () => { assert.equal(await cleanCalls("download-one-audio"), 1); });