84 lines
4.0 KiB
JavaScript
84 lines
4.0 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { BrowserSubmissionProcessor } from "../src/scripts/browser-submission-processor.js";
|
|
|
|
const file = { type: "audio/wav", size: 64 };
|
|
|
|
test("disabled beta returns the original file without loading policy", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = () => { throw new Error("fetch must not run"); };
|
|
try {
|
|
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 });
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("enabled shell checks current server policy and never claims processing occurred", 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 });
|
|
const result = await processor.prepare(file);
|
|
assert.equal(result.file, file);
|
|
assert.equal(result.metadata.browser_processing_used, false);
|
|
assert.equal(result.metadata.browser_gate_result, "unavailable");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("admin-disabled policy and fetch failure both fall back to the original file", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
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 });
|
|
globalThis.fetch = async () => { throw new Error("offline"); };
|
|
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
|
} finally {
|
|
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;
|
|
}
|
|
});
|