Files
BoomCast/frontend/tests/ffmpeg-video-extractor.test.mjs
2026-10-04 17:09:22 +02:00

39 lines
1.7 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { extractVideoAudio } from "../src/scripts/ffmpeg-video-extractor.js";
function mockRuntime(exitCode = 0) {
const calls = [];
const ffmpeg = {
loaded: false,
on: () => {},
load: async () => { calls.push("load"); ffmpeg.loaded = true; },
writeFile: async () => { calls.push("write"); },
exec: async (args) => { calls.push(args); return exitCode; },
readFile: async () => { calls.push("read"); return new Uint8Array([82, 73, 70, 70]); },
deleteFile: async (path) => { calls.push(`delete:${path}`); },
terminate: () => { calls.push("terminate"); ffmpeg.loaded = false; },
};
return { ffmpeg, calls };
}
const video = new File(["video"], "clip.mp4", { type: "video/mp4" });
test("successful extraction emits WAV and cleans up virtual files and worker", async () => {
const runtime = mockRuntime();
const wav = await extractVideoAudio(video, { createFFmpeg: async () => runtime });
assert.equal(wav.type, "audio/wav");
assert.equal(wav.name, "clip.wav");
assert.deepEqual(runtime.calls.find(Array.isArray), ["-i", "input", "-vn", "-ac", "1", "-ar", "16000", "-c:a", "pcm_s16le", "audio.wav"]);
assert.ok(runtime.calls.includes("delete:input"));
assert.ok(runtime.calls.includes("delete:audio.wav"));
assert.equal(runtime.calls.at(-1), "terminate");
});
test("failed extraction removes input and terminates worker", async () => {
const runtime = mockRuntime(1);
await assert.rejects(extractVideoAudio(video, { createFFmpeg: async () => runtime }), { code: "invalid_media" });
assert.ok(runtime.calls.includes("delete:input"));
assert.equal(runtime.calls.at(-1), "terminate");
});