first commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { JSDOM } from "jsdom";
|
||||
|
||||
function fixture() {
|
||||
const dom = new JSDOM(`<!doctype html><html><body>
|
||||
<script id="boomcast-browser-config" type="application/json">{"browserProcessingBeta":true,"policyUrl":"/policy","policyVersion":1}</script>
|
||||
<form class="upload-form" action="/classify">
|
||||
<label id="drop-zone"><input id="audio-file" name="audio" type="file" accept="audio/*,.wav" required><span id="upload-progress-circle"><span id="upload-icon-content"></span></span><span id="file-name"></span></label>
|
||||
<input id="browser-processing-toggle" type="checkbox">
|
||||
<button type="submit">Submit</button><button class="upload-cancel" type="button" hidden>Cancel</button>
|
||||
<section id="browser-gate" hidden><strong id="browser-gate-title"></strong><p id="browser-gate-message"></p><button id="browser-gate-choose" type="button"></button><button id="browser-gate-override" type="button"></button></section>
|
||||
<section id="classification-results"></section>
|
||||
</form><p id="upload-status"></p></body></html>`, { url: "https://example.test/" });
|
||||
const { window } = dom;
|
||||
window.matchMedia = () => ({ matches: false });
|
||||
window.BOOMCAST_I18N = {};
|
||||
const requests = [];
|
||||
class MockXHR {
|
||||
upload = { addEventListener() {} };
|
||||
addEventListener() {}
|
||||
open() {}
|
||||
send(data) { requests.push(data); }
|
||||
}
|
||||
const originals = Object.fromEntries(["window", "document", "FormData", "XMLHttpRequest", "DOMParser", "fetch"].map((name) => [name, globalThis[name]]));
|
||||
Object.assign(globalThis, { window, document: window.document, FormData: window.FormData, XMLHttpRequest: MockXHR, DOMParser: window.DOMParser });
|
||||
const form = window.document.querySelector("form");
|
||||
form.reportValidity = () => true;
|
||||
const input = window.document.querySelector("#audio-file");
|
||||
const audio = new window.File(["audio"], "clip.wav", { type: "audio/wav" });
|
||||
Object.defineProperty(input, "files", { configurable: true, value: [audio] });
|
||||
const toggle = window.document.querySelector("#browser-processing-toggle");
|
||||
return { window, form, input, audio, toggle, requests, restore: () => {
|
||||
for (const [name, value] of Object.entries(originals)) {
|
||||
if (value === undefined) delete globalThis[name];
|
||||
else globalThis[name] = value;
|
||||
}
|
||||
dom.window.close();
|
||||
} };
|
||||
}
|
||||
|
||||
test("admin ON with user toggle OFF uploads the original audio without policy fetch", async () => {
|
||||
const ui = fixture();
|
||||
let fetches = 0;
|
||||
globalThis.fetch = () => { fetches += 1; throw new Error("browser policy must not load"); };
|
||||
try {
|
||||
await import("../src/scripts/audio-upload.js?mode=server");
|
||||
assert.equal(ui.toggle.checked, false);
|
||||
ui.input.dispatchEvent(new ui.window.Event("change", { bubbles: true }));
|
||||
assert.equal(ui.form.querySelector('button[type="submit"]').disabled, false);
|
||||
ui.form.dispatchEvent(new ui.window.Event("submit", { bubbles: true, cancelable: true }));
|
||||
assert.equal(fetches, 0);
|
||||
assert.equal(ui.requests.length, 1);
|
||||
assert.equal(ui.requests[0].get("audio").name, "clip.wav");
|
||||
assert.equal(ui.requests[0].has("browser_processing_metadata"), false);
|
||||
} finally { ui.restore(); }
|
||||
});
|
||||
|
||||
test("switching browser mode off invalidates a policy job and restores server submission", async () => {
|
||||
const ui = fixture();
|
||||
let resolveFetch;
|
||||
let fetches = 0;
|
||||
globalThis.fetch = () => { fetches += 1; return new Promise((resolve) => { resolveFetch = resolve; }); };
|
||||
try {
|
||||
await import("../src/scripts/audio-upload.js?mode=switch");
|
||||
ui.input.dispatchEvent(new ui.window.Event("change", { bubbles: true }));
|
||||
ui.toggle.checked = true;
|
||||
ui.toggle.dispatchEvent(new ui.window.Event("change", { bubbles: true }));
|
||||
for (let i = 0; i < 30 && !resolveFetch; i += 1) await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
assert.equal(fetches, 1);
|
||||
assert.equal(ui.form.querySelector('button[type="submit"]').disabled, true);
|
||||
ui.toggle.checked = false;
|
||||
ui.toggle.dispatchEvent(new ui.window.Event("change", { bubbles: true }));
|
||||
resolveFetch({ ok: true, json: async () => ({ browser_processing_beta_enabled: true, version: 1 }) });
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
assert.equal(ui.input.accept, "audio/*,.wav");
|
||||
assert.equal(ui.form.querySelector('button[type="submit"]').disabled, false);
|
||||
assert.equal(ui.window.document.querySelector("#browser-gate").hidden, true);
|
||||
ui.form.dispatchEvent(new ui.window.Event("submit", { bubbles: true, cancelable: true }));
|
||||
assert.equal(ui.requests.length, 1);
|
||||
assert.equal(ui.requests[0].get("audio").name, "clip.wav");
|
||||
assert.equal(ui.requests[0].has("browser_processing_metadata"), false);
|
||||
} finally { ui.restore(); }
|
||||
});
|
||||
@@ -0,0 +1,99 @@
|
||||
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; }
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
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 });
|
||||
processor.setEnabled(true);
|
||||
assert.equal(processor.isEnabled(), false);
|
||||
assert.equal(processor.canProcess(file), false);
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null, gate: "unavailable" });
|
||||
} 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 });
|
||||
processor.setEnabled(true);
|
||||
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 });
|
||||
processor.setEnabled(true);
|
||||
try {
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ browser_processing_beta_enabled: false, version: 1 }) });
|
||||
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, gate: "unavailable" });
|
||||
} 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" }); },
|
||||
{ prepareAudio: async () => new Float32Array(16000), classify: async () => [{ class: "Fireworks", score: .9 }] },
|
||||
);
|
||||
processor.setEnabled(true);
|
||||
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, gate: "unavailable" });
|
||||
} 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"); },
|
||||
);
|
||||
processor.setEnabled(true);
|
||||
await assert.rejects(processor.prepare({ type: "video/mp4", name: "bad.mp4" }), /decode failed/);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
test("admin ON and user toggle OFF bypasses policy, FFmpeg and TensorFlow", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = () => { throw new Error("policy must not load"); };
|
||||
const audio = new File(["audio"], "clip.wav", { type: "audio/wav" });
|
||||
const processor = new BrowserSubmissionProcessor(
|
||||
{ browserProcessingBeta: true, policyUrl: "/policy", policyVersion: 1 },
|
||||
async () => { throw new Error("FFmpeg must not load"); },
|
||||
{ prepareAudio: async () => { throw new Error("audio preparation must not run"); },
|
||||
classify: async () => { throw new Error("TensorFlow must not load"); } },
|
||||
);
|
||||
try {
|
||||
assert.equal(processor.isAvailable(), true);
|
||||
assert.equal(processor.isEnabled(), false);
|
||||
assert.deepEqual(await processor.process(audio), { file: audio, metadata: null, gate: "unavailable" });
|
||||
processor.setEnabled(true);
|
||||
assert.equal(processor.isEnabled(), true);
|
||||
processor.setEnabled(false);
|
||||
assert.equal(processor.isEnabled(), false);
|
||||
assert.deepEqual(await processor.process(audio), { file: audio, 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; }
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { extractVideoAudio } from "../src/scripts/ffmpeg-video-extractor.js";
|
||||
|
||||
function mockRuntime(exitCode = 0) {
|
||||
const calls = [];
|
||||
const ffmpeg = {
|
||||
loaded: false,
|
||||
on: () => {},
|
||||
load: async () => { calls.push("load"); ffmpeg.loaded = true; },
|
||||
writeFile: async () => { calls.push("write"); },
|
||||
exec: async (args) => { calls.push(args); return exitCode; },
|
||||
readFile: async () => { calls.push("read"); return new Uint8Array([82, 73, 70, 70]); },
|
||||
deleteFile: async (path) => { calls.push(`delete:${path}`); },
|
||||
terminate: () => { calls.push("terminate"); ffmpeg.loaded = false; },
|
||||
};
|
||||
return { ffmpeg, calls };
|
||||
}
|
||||
|
||||
const video = new File(["video"], "clip.mp4", { type: "video/mp4" });
|
||||
|
||||
test("successful extraction emits WAV and cleans up virtual files and worker", async () => {
|
||||
const runtime = mockRuntime();
|
||||
const wav = await extractVideoAudio(video, { createFFmpeg: async () => runtime });
|
||||
assert.equal(wav.type, "audio/wav");
|
||||
assert.equal(wav.name, "clip.wav");
|
||||
assert.deepEqual(runtime.calls.find(Array.isArray), ["-i", "input", "-vn", "-ac", "1", "-ar", "16000", "-c:a", "pcm_s16le", "audio.wav"]);
|
||||
assert.ok(runtime.calls.includes("delete:input"));
|
||||
assert.ok(runtime.calls.includes("delete:audio.wav"));
|
||||
assert.equal(runtime.calls.at(-1), "terminate");
|
||||
});
|
||||
|
||||
test("failed extraction removes input and terminates worker", async () => {
|
||||
const runtime = mockRuntime(1);
|
||||
await assert.rejects(extractVideoAudio(video, { createFFmpeg: async () => runtime }), { code: "invalid_media" });
|
||||
assert.ok(runtime.calls.includes("delete:input"));
|
||||
assert.equal(runtime.calls.at(-1), "terminate");
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { JSDOM } from "jsdom";
|
||||
|
||||
test("compass stays hidden until permission and a heading, then rotates and hides when stopped", async () => {
|
||||
const dom = new JSDOM(`<!doctype html><html><body>
|
||||
<form id="manual-report-form" action="/submit">
|
||||
<input id="manual-latitude"><input id="manual-longitude">
|
||||
<input id="manual-direction-degrees"><input id="manual-direction-accuracy">
|
||||
<div id="manual-direction-panel" hidden><button id="manual-capture-direction" type="button"></button><div id="manual-direction-status"></div></div>
|
||||
<button id="manual-submit" type="button"></button>
|
||||
<button id="burst-start" type="button"></button><div id="burst-panel"></div><strong id="burst-heading"></strong>
|
||||
<div id="burst-detail"></div><canvas id="burst-waveform"></canvas><audio id="burst-preview"></audio>
|
||||
<button id="burst-resume" type="button"></button><button id="burst-discard" type="button"></button>
|
||||
<button id="burst-stop" type="button"></button><p id="burst-error"></p>
|
||||
</form>
|
||||
<div id="manual-submission"></div>
|
||||
<div id="manual-compass" hidden><div id="manual-compass-rose"></div><span id="manual-compass-heading"></span></div>
|
||||
</body></html>`, { url: "https://example.test/" });
|
||||
const { window } = dom;
|
||||
Object.defineProperty(window, "isSecureContext", { value: true });
|
||||
let resolvePermission;
|
||||
class MockOrientation {}
|
||||
MockOrientation.requestPermission = () => new Promise((resolve) => { resolvePermission = resolve; });
|
||||
window.DeviceOrientationEvent = MockOrientation;
|
||||
const previous = Object.fromEntries(["window", "document", "navigator", "DeviceOrientationEvent"].map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]));
|
||||
for (const [key, value] of Object.entries({ window, document: window.document, navigator: window.navigator, DeviceOrientationEvent: MockOrientation })) {
|
||||
Object.defineProperty(globalThis, key, { configurable: true, writable: true, value });
|
||||
}
|
||||
try {
|
||||
await import("../src/scripts/manual-report.js?compass=visual");
|
||||
const compass = window.document.querySelector("#manual-compass");
|
||||
const rose = window.document.querySelector("#manual-compass-rose");
|
||||
const button = window.document.querySelector("#manual-capture-direction");
|
||||
assert.equal(compass.hidden, true);
|
||||
button.click();
|
||||
assert.equal(compass.hidden, true);
|
||||
resolvePermission("granted");
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
const reading = (heading) => {
|
||||
const event = new window.Event("deviceorientation");
|
||||
Object.defineProperty(event, "webkitCompassHeading", { value: heading });
|
||||
window.dispatchEvent(event);
|
||||
};
|
||||
reading(359);
|
||||
assert.equal(compass.hidden, false);
|
||||
assert.equal(rose.style.transform, "rotate(-359deg)");
|
||||
reading(1);
|
||||
assert.equal(rose.style.transform, "rotate(-361deg)");
|
||||
assert.equal(window.document.querySelector("#manual-direction-degrees").value, "1");
|
||||
button.click();
|
||||
assert.equal(compass.hidden, true);
|
||||
} finally {
|
||||
for (const [key, descriptor] of Object.entries(previous)) {
|
||||
if (descriptor === undefined) delete globalThis[key];
|
||||
else Object.defineProperty(globalThis, key, descriptor);
|
||||
}
|
||||
dom.window.close();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user