65 lines
3.7 KiB
JavaScript
65 lines
3.7 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 }]; } },
|
|
);
|
|
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; }
|
|
});
|