Files
FireworksApp/frontend/tests/browser-submission-gate.test.mjs
T

100 lines
5.3 KiB
JavaScript

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 }]; } },
);
processor.setEnabled(true);
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" });
processor.setEnabled(true);
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 }]; } },
);
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; }
});