import { readFileSync } from "node:fs"; import assert from "node:assert/strict"; import { test } from "node:test"; import ts from "typescript"; const exports = {}; new Function("exports", ts.transpileModule(readFileSync(new URL("../src/lib/operator-input.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, }).outputText)(exports); const { OperatorEscapeGate, OperatorInputSampler, LatestInputSender, neutralAxes } = exports; const sampler = () => { const value = new OperatorInputSampler(); value.reset(0); return value; }; const hookExports = {}; new Function("require", "exports", ts.transpileModule(readFileSync(new URL("../src/lib/use-operator-control.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, }).outputText)(() => ({}), hookExports); const { operatorGate } = hookExports; const snapshot = { role: "team", team: 0, config: { armorId: 42 }, armorBoards: [{ id: 42, team: 0, online: true, reportsAllPlates: true }], health: [{ team: 0, hp: 100, outcome: null, available: true }], matchClock: { phase: "idle", source: null, connected: false } }; test("the first Escape releases capture, repeats are consumed, and the next press opens the menu", () => { const gate = new OperatorEscapeGate(); assert.equal(gate.consume(true, true, 0), true); assert.equal(gate.consume(true, false, 20), true); assert.equal(gate.consume(false, false, 30), true); assert.equal(gate.consume(true, false, 40), false); }); test("pointer-lock release before Escape is consumed without swallowing a second physical key press", () => { const gate = new OperatorEscapeGate(); gate.pointerReleased(100); assert.equal(gate.consume(true, false, 110), true); gate.consume(false, false, 120); assert.equal(gate.consume(true, false, 130), false); gate.pointerReleased(200); gate.consume(false, false, 210); // Browser may deliver only keyup for the native unlock gesture. assert.equal(gate.consume(true, false, 220), false); gate.pointerReleased(300); assert.equal(gate.consume(true, false, 401), false); }); test("debug is explicit before a match and refuses stale referee sessions and later phases", () => { assert.equal(operatorGate(snapshot, true, "debug"), null); assert.notEqual(operatorGate(snapshot, true, "match"), null); for (const phase of ["idle", "preparation", "ready"]) { assert.equal(operatorGate({ ...snapshot, matchClock: { phase, source: "192.0.2.1", connected: true } }, true, "debug"), null); assert.notEqual(operatorGate({ ...snapshot, matchClock: { phase, source: "192.0.2.1", connected: false } }, true, "debug"), null); } for (const phase of ["countdown", "running", "paused", "finished"]) { assert.notEqual(operatorGate({ ...snapshot, matchClock: { phase, source: "192.0.2.1", connected: true } }, true, "debug"), null); } }); test("normal operation needs the running clock, live bound team board and backend", () => { const running = { ...snapshot, matchClock: { phase: "running", source: "192.0.2.1", connected: true } }; assert.equal(operatorGate(running, true, "match"), null); for (const changed of [{ role: "referee" }, { armorBoards: [] }, { armorBoards: [{ id: 42, team: 1, online: true }] }, { armorBoards: [{ ...snapshot.armorBoards[0], reportsAllPlates: false }] }, { health: [{ team: 0, hp: 100, outcome: null, available: false }] }, { health: [{ team: 0, hp: 0, outcome: "defeat", available: true }] }, { matchClock: { ...running.matchClock, connected: false } }]) { assert.notEqual(operatorGate({ ...running, ...changed }, true, "match"), null); } assert.notEqual(operatorGate(running, false, "match"), null); }); test("equal mouse trajectories do not depend on the DOM event rate", () => { const high = sampler(), low = sampler(); for (let i = 0; i < 20; i++) high.mouse(0.5, -0.25); low.mouse(10, -5); assert.deepEqual(high.sample(20), low.sample(20)); }); test("velocity and smoothing use elapsed time, independent of sampling rate", () => { const fast = sampler(), slow = sampler(); fast.mouse(10, -5); fast.sample(20); fast.mouse(10, -5); slow.mouse(20, -10); assert.deepEqual(fast.sample(40), slow.sample(40)); }); test("stationary mouse returns both channels to zero immediately while a held key remains active", () => { const input = sampler(); input.key("KeyW", true); input.mouse(20, -20); assert.ok(input.sample(20).axes.yaw > 0); assert.deepEqual(input.sample(40).axes, { ...neutralAxes(), forward: 1000 }); }); test("axis signs, limits, opposing keys and diagonal magnitude are defined", () => { const input = sampler(); input.key("KeyW", true); input.key("KeyD", true); input.mouse(-100000, 100000); input.trigger(true); const { axes } = input.sample(20, 4); assert.equal(axes.forward, 707); assert.equal(axes.strafe, 707); assert.ok(axes.yaw < 0 && axes.yaw >= -1000); assert.ok(axes.pitch < 0 && axes.pitch >= -1000); assert.equal(axes.fire, true); input.key("KeyS", true); input.key("KeyA", true); assert.equal(input.sample(40).axes.forward, 0); assert.equal(input.sample(60).axes.strafe, 0); }); test("a stalled timer discards all buffered movement, keys and fire instead of replaying them", () => { const input = sampler(); input.key("KeyW", true); input.mouse(9000, 9000); input.trigger(true); assert.deepEqual(input.sample(101), { axes: neutralAxes(), stalled: true }); assert.deepEqual(input.sample(121), { axes: neutralAxes(), stalled: false }); }); test("reset clears held state and invalid mouse samples do not poison output", () => { const input = sampler(); input.key("KeyA", true); input.trigger(true); input.mouse(10, 10); input.reset(10); input.mouse(NaN, 20); input.mouse(Infinity, 1); assert.deepEqual(input.sample(30).axes, neutralAxes()); }); test("the sender keeps only the latest state behind one in-flight request", async () => { const sent = []; const release = []; const sender = new LatestInputSender(value => { sent.push(value); return new Promise(resolve => release.push(resolve)); }, error => { throw error; }); sender.offer(1); sender.offer(2); sender.offer(3); assert.deepEqual(sent, [1]); release.shift()(); await new Promise(resolve => setImmediate(resolve)); assert.deepEqual(sent, [1, 3]); release.shift()(); }); test("stop replaces unsent movement and permanently rejects later offers", async () => { const sent = []; const release = []; const sender = new LatestInputSender(value => { sent.push(value); return new Promise(resolve => release.push(resolve)); }, error => { throw error; }); sender.offer("moving"); sender.offer("old movement"); sender.end("neutral"); sender.offer("late movement"); release.shift()(); await new Promise(resolve => setImmediate(resolve)); assert.deepEqual(sent, ["moving", "neutral"]); release.shift()(); });