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(`
`, { 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;
let submissions = 0;
window.htmx = { ajax: () => { submissions++; } };
const previous = Object.fromEntries(["window", "document", "navigator", "DeviceOrientationEvent", "htmx", "DOMParser"].map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]));
for (const [key, value] of Object.entries({ window, document: window.document, navigator: window.navigator, DeviceOrientationEvent: MockOrientation, htmx: window.htmx, DOMParser: window.DOMParser })) {
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);
window.document.querySelector("#manual-increase").click();
assert.equal(window.document.querySelector("#manual-occurrences").value, "2");
window.document.querySelector("#manual-decrease").click();
assert.equal(window.document.querySelector("#manual-occurrences").value, "1");
window.document.querySelector("#manual-direction-panel").outerHTML = '';
const opened = window.document.querySelector("#manual-direction-panel");
window.document.body.dispatchEvent(new window.CustomEvent("htmx:afterSwap", { detail: { target: opened } }));
assert.equal(compass.parentElement.id, "manual-direction-panel");
window.document.body.dispatchEvent(new window.CustomEvent("htmx:beforeSwap", { detail: { target: opened } }));
assert.equal(compass.parentElement.id, "manual-compass-home");
opened.outerHTML = '';
const closed = window.document.querySelector("#manual-direction-panel");
window.document.body.dispatchEvent(new window.CustomEvent("htmx:afterSwap", { detail: { target: closed } }));
assert.equal(window.document.querySelector("#manual-direction-summary").textContent, "North · 1°");
const submit = window.document.querySelector("#manual-submit");
const form = window.document.querySelector("#manual-report-form");
submit.click();
submit.click();
assert.equal(submissions, 1);
assert.equal(submit.disabled, true);
window.document.querySelector("#manual-submission").outerHTML = 'Received
';
window.document.body.dispatchEvent(new window.CustomEvent("htmx:afterRequest", { detail: { elt: form, xhr: { status: 200 } } }));
assert.equal(submit.disabled, false);
submit.click();
assert.equal(submissions, 2);
window.document.body.dispatchEvent(new window.CustomEvent("htmx:afterRequest", { detail: { elt: form, xhr: { status: 400, responseText: 'Try again
' } } }));
assert.equal(window.document.querySelector("#manual-submission").dataset.result, "error");
assert.equal(submit.disabled, false);
} finally {
for (const [key, descriptor] of Object.entries(previous)) {
if (descriptor === undefined) delete globalThis[key];
else Object.defineProperty(globalThis, key, descriptor);
}
dom.window.close();
}
});