import { test } from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { probeVideoHeight } from "./ffprobeDuration"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/ffprobeDuration.test.ts // // A stand-in ffprobe: a shell script that prints what the test says, so no // media file and no real ffprobe are needed. async function fakeFfprobe(dir: string, body: string): Promise { const bin = path.join(dir, "ffprobe"); await writeFile(bin, `#!/bin/sh\n${body}\n`); await chmod(bin, 0o755); return bin; } test("probeVideoHeight reads the first video stream's height", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "probe-height-")); try { const ok = await fakeFfprobe(dir, 'echo 720'); assert.equal(await probeVideoHeight({ ffprobeBin: ok, file: "x.mp4" }), 720); } finally { await rm(dir, { recursive: true, force: true }); } }); test("probeVideoHeight is null when ffprobe fails, prints nothing, or is missing", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "probe-height-")); try { const fails = await fakeFfprobe(dir, "exit 1"); assert.equal(await probeVideoHeight({ ffprobeBin: fails, file: "x.mp4" }), null); // An audio-only file: no v:0 stream, so ffprobe prints nothing. await rm(fails); const empty = await fakeFfprobe(dir, "exit 0"); assert.equal(await probeVideoHeight({ ffprobeBin: empty, file: "x.m4a" }), null); assert.equal( await probeVideoHeight({ ffprobeBin: path.join(dir, "no-such-bin"), file: "x.mp4" }), null, ); } finally { await rm(dir, { recursive: true, force: true }); } });