browser audio detection phase 4

This commit is contained in:
Joren de Graaf
2026-10-02 21:45:24 +02:00
parent 1de4b8a0b3
commit 46ad514a70
20 changed files with 903 additions and 31 deletions
@@ -26,6 +26,7 @@ test("audio and video-derived WAV use the same gate; audio never calls FFmpeg",
async () => { extractionCalls += 1; return new File(["wav"], "clip.wav", { type: "audio/wav" }); },
{ prepareAudio: async () => new Float32Array(16000), classify: async () => { classificationCalls += 1; return [{ class: "Speech", score: .9 }]; } },
);
processor.setEnabled(true);
try {
assert.equal((await processor.prepare(new File(["audio"], "audio.wav", { type: "audio/wav" }))).gate, "speech_only");
assert.equal(extractionCalls, 0);
@@ -43,6 +44,7 @@ test("local classifier failure restores ordinary server upload", async () => {
{ prepareAudio: async () => new Float32Array(16000), classify: async () => { throw new Error("offline"); } },
);
const audio = new File(["audio"], "audio.wav", { type: "audio/wav" });
processor.setEnabled(true);
try {
const result = await processor.prepare(audio);
assert.equal(result.file, audio);
@@ -58,7 +60,40 @@ test("aborted classification never yields a gate decision", async () => {
{ browserProcessingBeta: true, policyUrl: "/policy", policyVersion: 1 }, null,
{ prepareAudio: async () => new Float32Array(16000), classify: async () => { controller.abort(); return [{ class: "Fireworks", score: .9 }]; } },
);
processor.setEnabled(true);
try {
await assert.rejects(processor.prepare(new File(["audio"], "audio.wav", { type: "audio/wav" }), { signal: controller.signal }));
} finally { globalThis.fetch = originalFetch; }
});
test("replacing a classification cancels the old result and keeps the new file", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => ({ ok: true, json: async () => ({ ...policy, browser_processing_beta_enabled: true, version: 1 }) });
let firstStarted;
const started = new Promise((resolve) => { firstStarted = resolve; });
let finishFirst;
let calls = 0;
const processor = new BrowserSubmissionProcessor(
{ browserProcessingBeta: true, policyUrl: "/policy", policyVersion: 1 }, null,
{ prepareAudio: async () => new Float32Array(16000), classify: async () => {
calls += 1;
if (calls === 1) {
firstStarted();
return new Promise((resolve) => { finishFirst = resolve; });
}
return [{ class: "Fireworks", score: .9 }];
} },
);
processor.setEnabled(true);
const firstFile = new File(["a"], "first.wav", { type: "audio/wav" });
const secondFile = new File(["b"], "second.wav", { type: "audio/wav" });
try {
const first = processor.process(firstFile);
await started;
const second = await processor.process(secondFile);
assert.equal(second.file, secondFile);
assert.equal(second.gate, "whitelist_match");
finishFirst([{ class: "Speech", score: .9 }]);
await assert.rejects(first);
} finally { globalThis.fetch = originalFetch; }
});