import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import vm from "node:vm"; import ts from "typescript"; // Reload the real hook module with an iOS-style permission API and the same // device storage. The hook snapshot is the value bound to the Settings switch. function launch(storage, permission = async () => "granted") { const exports = {}; const effects = []; const document = new EventTarget(); const context = { exports, require: () => ({ useSyncExternalStore: (_subscribe, snapshot) => snapshot(), useEffect: (effect) => effects.push(effect), }), DeviceOrientationEvent: class { static requestPermission = permission; }, localStorage: { getItem: (key) => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value), }, document, }; vm.runInNewContext( ts.transpileModule( readFileSync(new URL("../src/useTilt.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, }, }, ).outputText, context, ); return { api: exports, document, effects, snapshot: () => exports.useTilt() }; } test("saved tilt stays switched on when an iOS PWA is relaunched", async () => { const storage = new Map(); const first = launch(storage); await first.snapshot().toggleTilt(); assert.equal(first.snapshot().enabled, true); assert.equal(storage.get("calio-tilt"), "true"); const reopened = launch(storage); assert.equal( reopened.snapshot().enabled, true, "A session permission check must not reset the saved switch", ); }); test("explicitly switching tilt off stays off after reopening", async () => { const storage = new Map(); const first = launch(storage); await first.snapshot().toggleTilt(); await first.snapshot().toggleTilt(); assert.equal(launch(storage).snapshot().enabled, false); }); test("the first app interaction resumes iOS motion without toggling the preference", async () => { let calls = 0; const reopened = launch(new Map([["calio-tilt", "true"]]), async () => { calls++; return "granted"; }); assert.equal(reopened.snapshot().status, "permission"); reopened.api.useTiltResume(true); const cleanup = reopened.effects[0](); reopened.document.dispatchEvent(new Event("pointerup")); reopened.document.dispatchEvent(new Event("touchend")); await reopened.snapshot().requestTiltPermission(); assert.equal( calls, 1, "Overlapping pointer/touch events must share one request", ); assert.equal(reopened.snapshot().enabled, true); assert.equal(reopened.snapshot().status, "enabled"); cleanup(); }); test("denial preserves the preference and a late permission grant cannot undo switching off", async () => { const denied = launch( new Map([["calio-tilt", "true"]]), async () => "denied", ); await denied.snapshot().requestTiltPermission(); assert.equal(denied.snapshot().enabled, true); assert.equal(denied.snapshot().status, "denied"); let grant; const storage = new Map(); const pending = launch( storage, () => new Promise((resolve) => { grant = resolve; }), ); const enabling = pending.snapshot().toggleTilt(); await pending.snapshot().toggleTilt(); grant("granted"); await enabling; assert.equal(pending.snapshot().enabled, false); assert.equal(pending.snapshot().status, "off"); assert.equal(storage.get("calio-tilt"), "false"); });