import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, writeFile, chmod, rm, symlink } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { execFileSync } from "node:child_process"; import { probeLocalRecording } from "./localRecordingProbe-server"; import { prepareLocalRecordingMetadata } from "./localRecordingMetadata"; test("real ffprobe inspects synthetic MP4 without changing it or requiring an online source", async () => { const root = await mkdtemp(path.join(tmpdir(), "local-probe-")); try { const file = path.join(root, "Lesson one.mp4"); execFileSync("ffmpeg", ["-v", "error", "-f", "lavfi", "-i", "color=c=black:s=32x32:r=5", "-t", "1", "-c:v", "mpeg4", file], {timeout: 15000}); const before = await readFile(file); const probe = await probeLocalRecording({file, ffprobeBin: "ffprobe"}); assert.ok(probe.format.duration > 0); assert.deepEqual(await readFile(file), before); const metadata = prepareLocalRecordingMetadata({id: "local-FAKE-TEST-VALUEFAKE-TEST-VALUE", filename: "Lesson one.mp4", probe}); assert.equal(metadata.title, "Lesson one"); assert.equal("upload_date" in metadata, false); assert.equal("webpage_url" in metadata, false); assert.equal(JSON.stringify(probe).includes(root), false); await assert.rejects(probeLocalRecording({file, ffprobeBin: path.join(root, "missing-tool")}), /ffprobe is unavailable.*editor server/); const abort = new AbortController(); abort.abort(); await assert.rejects(probeLocalRecording({file, ffprobeBin: "ffprobe", signal: abort.signal})); const linked = path.join(root, "linked.mp4"); await symlink(file, linked); await assert.rejects(probeLocalRecording({file: linked, ffprobeBin: "ffprobe"}), /regular MP4/); } finally { await rm(root, {recursive: true, force: true}); } }); test("corrupt MP4 and remote inputs fail without exposing private diagnostic paths", async () => { const root = await mkdtemp(path.join(tmpdir(), "private-probe-")); try { const file = path.join(root, "bad.mp4"); await writeFile(file, "not a video"); await assert.rejects(probeLocalRecording({file, ffprobeBin: "ffprobe"}), error => { assert.ok(error instanceof Error); assert.equal(error.message.includes(root), false); return /Media inspection failed/.test(error.message); }); await assert.rejects(probeLocalRecording({file: "https://example.com/movie.mp4", ffprobeBin: "ffprobe"}), /absolute local/); } finally { await rm(root, {recursive: true, force: true}); } }); test("inspection bounds timeout, output, and invalid JSON from the executable", async () => { const root = await mkdtemp(path.join(tmpdir(), "bounded-probe-")); try { const file = path.join(root, "sample.mp4"); await writeFile(file, "fixture"); const bin = path.join(root, "probe-fixture"); const script = async (body: string) => { await writeFile(bin, "#!/usr/bin/env node\n" + body + "\n"); await chmod(bin, 0o755); }; await script("setInterval(() => {}, 1000)"); await assert.rejects(probeLocalRecording({file, ffprobeBin: bin, timeoutMs: 200}), /time limit/); await script("process.stdout.write('x'.repeat(1024 * 1024))"); await assert.rejects(probeLocalRecording({file, ffprobeBin: bin}), /output limit/); await script("process.stdout.write('not JSON')"); await assert.rejects(probeLocalRecording({file, ffprobeBin: bin}), /invalid data/); } finally { await rm(root, {recursive: true, force: true}); } });