browser audio detection phase 3
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { BrowserSubmissionGate } from "../src/scripts/browser-submission-gate.js";
|
||||
import { BrowserSubmissionProcessor } from "../src/scripts/browser-submission-processor.js";
|
||||
|
||||
const policy = { whitelist: ["Fireworks"], privacy_sensitive_speech: ["Speech"], thresholds: { relevance: .5, speech: .5 } };
|
||||
|
||||
test("whitelist match allows normal submission even when speech is present", () => {
|
||||
const decision = BrowserSubmissionGate.evaluate([{ class: "Fireworks", score: .8 }, { class: "Speech", score: .9 }], policy);
|
||||
assert.equal(decision.result, "whitelist_match");
|
||||
assert.equal(decision.speechDetected, true);
|
||||
});
|
||||
|
||||
test("speech without relevance and irrelevant audio require explicit override", () => {
|
||||
assert.equal(BrowserSubmissionGate.evaluate([{ class: "Speech", score: .9 }], policy).result, "speech_only");
|
||||
assert.equal(BrowserSubmissionGate.evaluate([{ class: "Music", score: .9 }], policy).result, "no_match");
|
||||
});
|
||||
|
||||
test("audio and video-derived WAV use the same gate; audio never calls FFmpeg", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ ...policy, browser_processing_beta_enabled: true, version: 1 }) });
|
||||
let extractionCalls = 0;
|
||||
let classificationCalls = 0;
|
||||
const processor = new BrowserSubmissionProcessor(
|
||||
{ browserProcessingBeta: true, policyUrl: "/policy", policyVersion: 1 },
|
||||
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 }]; } },
|
||||
);
|
||||
try {
|
||||
assert.equal((await processor.prepare(new File(["audio"], "audio.wav", { type: "audio/wav" }))).gate, "speech_only");
|
||||
assert.equal(extractionCalls, 0);
|
||||
assert.equal((await processor.prepare(new File(["video"], "clip.mp4", { type: "video/mp4" }))).gate, "speech_only");
|
||||
assert.equal(extractionCalls, 1);
|
||||
assert.equal(classificationCalls, 2);
|
||||
} finally { globalThis.fetch = originalFetch; }
|
||||
});
|
||||
|
||||
test("local classifier failure restores ordinary server upload", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ ...policy, browser_processing_beta_enabled: true, version: 1 }) });
|
||||
const processor = new BrowserSubmissionProcessor(
|
||||
{ browserProcessingBeta: true, policyUrl: "/policy", policyVersion: 1 }, null,
|
||||
{ prepareAudio: async () => new Float32Array(16000), classify: async () => { throw new Error("offline"); } },
|
||||
);
|
||||
const audio = new File(["audio"], "audio.wav", { type: "audio/wav" });
|
||||
try {
|
||||
const result = await processor.prepare(audio);
|
||||
assert.equal(result.file, audio);
|
||||
assert.equal(result.gate, "unavailable");
|
||||
} finally { globalThis.fetch = originalFetch; }
|
||||
});
|
||||
|
||||
test("aborted classification never yields a gate decision", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ ...policy, browser_processing_beta_enabled: true, version: 1 }) });
|
||||
const controller = new AbortController();
|
||||
const processor = new BrowserSubmissionProcessor(
|
||||
{ browserProcessingBeta: true, policyUrl: "/policy", policyVersion: 1 }, null,
|
||||
{ prepareAudio: async () => new Float32Array(16000), classify: async () => { controller.abort(); return [{ class: "Fireworks", score: .9 }]; } },
|
||||
);
|
||||
try {
|
||||
await assert.rejects(processor.prepare(new File(["audio"], "audio.wav", { type: "audio/wav" }), { signal: controller.signal }));
|
||||
} finally { globalThis.fetch = originalFetch; }
|
||||
});
|
||||
@@ -12,7 +12,7 @@ test("disabled beta returns the original file without loading policy", async ()
|
||||
const processor = new BrowserSubmissionProcessor({ browserProcessingBeta: false });
|
||||
assert.equal(processor.isEnabled(), false);
|
||||
assert.equal(processor.canProcess(file), false);
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null, gate: "unavailable" });
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
@@ -37,9 +37,9 @@ test("admin-disabled policy and fetch failure both fall back to the original fil
|
||||
const processor = new BrowserSubmissionProcessor({ browserProcessingBeta: true, policyUrl: "/browser-policy/", policyVersion: 1 });
|
||||
try {
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ browser_processing_beta_enabled: false, version: 1 }) });
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null, gate: "unavailable" });
|
||||
globalThis.fetch = async () => { throw new Error("offline"); };
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null, gate: "unavailable" });
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
@@ -53,6 +53,7 @@ test("video routing is gated and audio never invokes FFmpeg", async () => {
|
||||
const processor = new BrowserSubmissionProcessor(
|
||||
{ browserProcessingBeta: true, policyUrl: "/browser-policy/", policyVersion: 1 },
|
||||
async () => { calls += 1; return new File(["wav"], "clip.wav", { type: "audio/wav" }); },
|
||||
{ prepareAudio: async () => new Float32Array(16000), classify: async () => [{ class: "Fireworks", score: .9 }] },
|
||||
);
|
||||
await processor.prepare(file);
|
||||
assert.equal(calls, 0);
|
||||
@@ -62,7 +63,7 @@ test("video routing is gated and audio never invokes FFmpeg", async () => {
|
||||
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 });
|
||||
assert.deepEqual(await disabled.prepare(video), { file: video, metadata: null, gate: "unavailable" });
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { BrowserYamnetClassifier } from "../src/scripts/browser-yamnet-classifier.js";
|
||||
|
||||
test("YAMNet peak scores are mapped and all owned resources are disposed", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const disposed = [];
|
||||
const values = new Float32Array(2 * 521);
|
||||
values[0] = .4;
|
||||
values[521] = .9;
|
||||
const output = { shape: [2, 521], data: async () => values, dispose: () => disposed.push("output") };
|
||||
const model = { executeAsync: async () => [output], dispose: () => disposed.push("model") };
|
||||
const tf = {
|
||||
ready: async () => {}, getBackend: () => "cpu", memory: () => ({ numTensors: 0 }),
|
||||
loadGraphModel: async () => model,
|
||||
tensor1d: () => ({ dispose: () => disposed.push("input") }),
|
||||
};
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ["Fireworks", ...Array(520).fill("Other")] });
|
||||
try {
|
||||
const predictions = await BrowserYamnetClassifier.classify(new Float32Array(16000), {
|
||||
modelUrl: "/static/browser-yamnet/model.json", classMapUrl: "/static/browser-yamnet/yamnet_class_map.json",
|
||||
loadTensorFlow: async () => tf,
|
||||
});
|
||||
assert.equal(predictions[0].class, "Fireworks");
|
||||
assert.equal(predictions[0].score, values[521]);
|
||||
assert.equal(predictions[0].timeSeconds, .48);
|
||||
assert.deepEqual(disposed, ["input", "output", "model"]);
|
||||
} finally { globalThis.fetch = originalFetch; }
|
||||
});
|
||||
|
||||
test("model failure still disposes the loaded model", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let disposed = false;
|
||||
const tf = {
|
||||
ready: async () => {}, getBackend: () => "cpu", memory: () => ({ numTensors: 0 }),
|
||||
loadGraphModel: async () => ({ dispose: () => { disposed = true; } }),
|
||||
};
|
||||
globalThis.fetch = async () => ({ ok: false });
|
||||
try {
|
||||
await assert.rejects(BrowserYamnetClassifier.classify(new Float32Array(16000), {
|
||||
modelUrl: "/model.json", classMapUrl: "/map.json", loadTensorFlow: async () => tf,
|
||||
}));
|
||||
assert.equal(disposed, true);
|
||||
} finally { globalThis.fetch = originalFetch; }
|
||||
});
|
||||
Reference in New Issue
Block a user