#!/usr/bin/env node // E2E fake ffmpeg. Recognises two invocation shapes: // // 1. Transcode: ffmpeg -y -i -vn -c:a // -> writes a small placeholder to . If contains the // CORRUPT_MARKER sentinel, exits 1 with a libavcodec-style stderr // line so the audio-check classifier flags it as malformed. // -> If is /dev/null (the audio-checked probe), behaves the // same w.r.t. corruption detection but doesn't actually write // output. import { readFile, writeFile } from "node:fs/promises"; import { installFixtureWatchdog } from "./_watchdog.mjs"; // Never outlive the run that spawned us — see _watchdog.mjs. installFixtureWatchdog(); const CORRUPT_MARKER = "__CORRUPT__"; // When the probed file contains this marker, the probe (and transcode) sleeps // SLOW_PROBE_MS before finishing. The audio-check pause test embeds it so the // integrity probe takes meaningfully longer than the download's chunk cadence, // making the "download is frozen during the probe" behaviour observable. const SLOW_PROBE_MARKER = "__SLOWPROBE__"; const SLOW_PROBE_MS = 600; const argv = process.argv.slice(2); function arg(flag) { const i = argv.indexOf(flag); if (i < 0) return undefined; return argv[i + 1]; } function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } async function readSrc(file) { try { return await readFile(file); } catch { return null; } } const src = arg("-i") ?? ""; const out = argv[argv.length - 1]; if (!out || out.startsWith("-")) { process.stderr.write(`[fake-ffmpeg] missing output path\n`); process.exit(2); } const srcBuf = await readSrc(src); if (srcBuf?.includes(SLOW_PROBE_MARKER)) { await sleep(SLOW_PROBE_MS); } if (srcBuf?.includes(CORRUPT_MARKER)) { process.stderr.write( `[aac @ 0x1234] Sample rate index in program config element does not match the sample rate index configured by the container.\n`, ); process.exit(1); } if (out === "/dev/null") { // Probe invocation — no output written. Real ffmpeg would also print a // "partial file" warning here if the input was truncated; we don't need // to simulate that for the exit-code-driven classifier. process.exit(0); } await writeFile(out, `fake-ffmpeg transcoded from ${src}\n`); process.stdout.write(`[fake-ffmpeg] wrote ${out}\n`);