// A run that is already cancelled launches nothing (lib/childKillGuard.ts says // why execa must never be handed an aborted signal): the fake yt-dlp below // leaves a file behind when it runs, and none is left. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/abortedBeforeLaunch.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdir, mkdtemp, rm, stat, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { runAudioCheckedYtdlp } from "./audioCheckedDownload"; import { runOneYtdlp } from "./runOneYtdlp"; import type { Paths } from "../lib/paths"; import type { ChannelConfig } from "../lib/channelConfig"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "aborted-launch-")); const RAN = path.join(ROOT, "ran"); const YTDLP = path.join(ROOT, "fake-ytdlp.mjs"); await writeFile(YTDLP, `#!/usr/bin/env node\nimport { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(RAN)}, "ran");\n`); await chmod(YTDLP, 0o755); const ran = () => stat(RAN).then(() => true, () => false); const aborted = () => { const ac = new AbortController(); ac.abort(); return ac.signal; }; test("runOneYtdlp: aborted before launch spawns nothing and logs nothing", async () => { const said: string[] = []; const out = await runOneYtdlp({ ytdlpBin: YTDLP, onLog: (l) => said.push(l), signal: aborted() }, ROOT, ["--version"]); assert.deepEqual(out, { exitCode: null, stderrTail: "", archiveLine: null, formatLine: null }); assert.deepEqual(said, []); await new Promise((r) => setTimeout(r, 300)); assert.equal(await ran(), false, "the fake yt-dlp never ran"); }); test("runOneYtdlp: the same launch, not cancelled, does run (the fake is a real control)", async () => { const out = await runOneYtdlp({ ytdlpBin: YTDLP, onLog: () => {}, signal: new AbortController().signal }, ROOT, ["--version"]); assert.equal(out.exitCode, 0); assert.equal(await ran(), true); await rm(RAN, { force: true }); }); test("the audio-checked download: aborted before launch is `aborted`, and yt-dlp never ran — over a channel directory that is gone, too", async () => { for (const channelDir of [path.join(ROOT, "channel"), path.join(ROOT, "removed", "channel")]) { if (channelDir.includes("/channel") && !channelDir.includes("removed")) await mkdir(path.join(channelDir, "data"), { recursive: true }); const said: string[] = []; const out = await runAudioCheckedYtdlp({ paths: { ytdlpBin: YTDLP, ffmpegBin: "ffmpeg" } as unknown as Paths, channelDir, channelConfig: {} as ChannelConfig, ytdlpArgs: ["--version"], onLog: (l) => said.push(l), signal: aborted(), archiveMarker: "DLOM_ARCHIVE", expectedVideoIdHint: "vid0000001", }); assert.equal(out.kind, "aborted"); assert.ok(!said.some((l) => l.startsWith("$ ")), "no launch line"); } await new Promise((r) => setTimeout(r, 300)); assert.equal(await ran(), false, "the fake yt-dlp never ran"); }); test("the audio-checked download: cancelled by the log sink on the launch line, yt-dlp is not spawned", async () => { const channelDir = path.join(ROOT, "channel-log"); await mkdir(path.join(channelDir, "data"), { recursive: true }); const ac = new AbortController(); const out = await runAudioCheckedYtdlp({ paths: { ytdlpBin: YTDLP, ffmpegBin: "ffmpeg" } as unknown as Paths, channelDir, channelConfig: {} as ChannelConfig, ytdlpArgs: ["--version"], onLog: (l) => { if (l.startsWith("$ ")) ac.abort(); }, signal: ac.signal, archiveMarker: "DLOM_ARCHIVE", expectedVideoIdHint: "vid0000001", }); assert.equal(out.kind, "aborted"); await new Promise((r) => setTimeout(r, 300)); assert.equal(await ran(), false, "the fake yt-dlp never ran"); });