browser audio detection phase 2

This commit is contained in:
Joren de Graaf
2026-10-02 16:57:12 +02:00
parent df837c240f
commit ff0d78ccba
17 changed files with 334 additions and 10 deletions
@@ -44,3 +44,40 @@ test("admin-disabled policy and fetch failure both fall back to the original fil
globalThis.fetch = originalFetch;
}
});
test("video routing is gated and audio never invokes FFmpeg", async () => {
const originalFetch = globalThis.fetch;
let calls = 0;
globalThis.fetch = async () => ({ ok: true, json: async () => ({ browser_processing_beta_enabled: true, version: 1 }) });
try {
const processor = new BrowserSubmissionProcessor(
{ browserProcessingBeta: true, policyUrl: "/browser-policy/", policyVersion: 1 },
async () => { calls += 1; return new File(["wav"], "clip.wav", { type: "audio/wav" }); },
);
await processor.prepare(file);
assert.equal(calls, 0);
const video = { type: "video/mp4", size: 123, name: "clip.mp4" };
const result = await processor.prepare(video);
assert.equal(calls, 1);
assert.equal(result.file.type, "audio/wav");
assert.equal(result.metadata.browser_processing_used, true);
const disabled = new BrowserSubmissionProcessor({ browserProcessingBeta: false }, async () => { throw new Error("must not run"); });
assert.deepEqual(await disabled.prepare(video), { file: video, metadata: null });
} finally {
globalThis.fetch = originalFetch;
}
});
test("video conversion failure rejects instead of falling back to original video", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => ({ ok: true, json: async () => ({ browser_processing_beta_enabled: true, version: 1 }) });
try {
const processor = new BrowserSubmissionProcessor(
{ browserProcessingBeta: true, policyUrl: "/browser-policy/", policyVersion: 1 },
async () => { throw new Error("decode failed"); },
);
await assert.rejects(processor.prepare({ type: "video/mp4", name: "bad.mp4" }), /decode failed/);
} finally {
globalThis.fetch = originalFetch;
}
});
@@ -0,0 +1,38 @@
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");
});