aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/browser_controls.mjs (69585B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs';
      3 import vm from 'node:vm';
      4 import test from 'node:test';
      5 
      6 function page({navigator = {userAgent: 'Mozilla/5.0 (Android) Gecko Firefox/149.0'}, saved = null, ready = true} = {}) {
      7   const elements = new Map();
      8   const keys = [], characters = [], mouse = [], orbits = [], sizes = [], chatViewports = [], cursorHidden = [], hudInsets = [];
      9   const scrolls = [], zooms = [], wheels = [], cameraWheels = [], creationNames = [];
     10   const repeats = [];
     11   const modifiers = [];
     12   const timers = new Map();
     13   const storage = new Map(saved === null ? [] : [['ao-touch-controls', saved]]);
     14   const reloads = [], requests = [];
     15   let response = new Response(null, {status: 204, headers: {'X-AO-Creation-Name': 'Phonehero'}});
     16   const document = new EventTarget();
     17   class Element extends EventTarget {
     18     hidden = false;
     19     open = false;
     20     style = {};
     21     dataset = {};
     22     clientWidth = 844;
     23     clientHeight = 390;
     24     value = '';
     25     validity = '';
     26     dispatchEvent(event) {
     27       const result = super.dispatchEvent(event);
     28       this['on' + event.type]?.(event);
     29       return result;
     30     }
     31     focus() { document.activeElement = this; this.dispatchEvent(new Event('focus')); }
     32     blur() { document.activeElement = null; this.dispatchEvent(new Event('blur')); }
     33     setPointerCapture() {}
     34     getBoundingClientRect() { return {left: 0, top: 0, width: this.clientWidth, height: this.clientHeight}; }
     35     showModal() { this.open = true; }
     36     close() { this.open = false; this.dispatchEvent(new Event('close')); }
     37     setCustomValidity(message) { this.validity = message; }
     38     reportValidity() {}
     39   }
     40   document.querySelector = selector => {
     41     if (!elements.has(selector)) elements.set(selector, new Element());
     42     return elements.get(selector);
     43   };
     44   const actions = [32, 113, 101].map(key => {
     45     const button = new Element();
     46     button.dataset.key = key;
     47     return button;
     48   });
     49   document.querySelectorAll = () => actions;
     50   document.querySelector('#chat-compose').hidden = true;
     51   document.querySelector('#touch-controls').hidden = true;
     52   document.querySelector('#reconnect').hidden = true;
     53   const canvas = document.querySelector('#canvas');
     54 
     55   const window = new EventTarget();
     56   window.visualViewport = new EventTarget();
     57   window.visualViewport.height = 390;
     58   window.visualViewport.width = 844;
     59   window.visualViewport.offsetLeft = 0;
     60   window.visualViewport.offsetTop = 0;
     61   const context = vm.createContext({
     62     document, window, navigator, Event, TextDecoder, Uint8Array, URLSearchParams,
     63     fetch: async (url, options) => { requests.push({url, ...options}); return response; },
     64     location: {reload: () => reloads.push(true)},
     65     console: {log() {}, error: console.error},
     66     localStorage: {getItem: key => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, String(value))},
     67     devicePixelRatio: 2, innerHeight: 390, crossOriginIsolated: false,
     68     ResizeObserver: class { observe() {} },
     69     setTimeout: callback => { const id = Symbol(); timers.set(id, callback); return id; },
     70     clearTimeout: id => timers.delete(id)
     71   });
     72   const html = fs.readFileSync(new URL('../browser/index.html', import.meta.url), 'utf8');
     73   for (const script of html.matchAll(/<script(?: id="player-controls")?>([\s\S]*?)<\/script>/g)) {
     74     vm.runInContext(script[1], context);
     75     // Viewport events can arrive while the HTML parser waits for the next script.
     76     window.visualViewport.dispatchEvent(new Event('resize'));
     77     window.visualViewport.dispatchEvent(new Event('scroll'));
     78   }
     79   context.Module._browser_input_key = (key, down, repeat, mods) => {
     80     keys.push([key, down]); repeats.push(!!repeat); modifiers.push(mods || 0);
     81   };
     82   context.Module._browser_input_character = character => characters.push(character);
     83   context.Module.finishCreation = name => creationNames.push(name);
     84   context.Module.loadAccount = () => {};
     85   context.Module._browser_input_motion = (x, y) => mouse.push(['mousemove', x, y, 0]);
     86   context.Module._browser_input_button = (button, down, x, y) => mouse.push([down ? 'mousedown' : 'mouseup', x, y, button === 3 ? 2 : 0]);
     87   context.Module._browser_input_wheel = (x, y, vertical) => wheels.push([x, y, vertical]);
     88   context.Module._browser_camera_wheel = (x, y) => cameraWheels.push([x, y]);
     89   context.Module._browser_orbit = (x, y, active) => orbits.push([x, y, active]);
     90   context.Module._browser_zoom = amount => zooms.push(amount);
     91   context.Module._browser_hide_cursor = hidden => cursorHidden.push(hidden);
     92   context.Module._browser_hud_insets = (right, top) => hudInsets.push([right, top]);
     93   context.Module._browser_request_resize = (width, height) => sizes.push([width, height]);
     94   context.Module._browser_chat_viewport = bottom => chatViewports.push(bottom);
     95   context.Module._browser_chat_scroll = pixels => scrolls.push(pixels);
     96   let touchFrame;
     97   context.Module._browser_touch_frame = request => { touchFrame = request; };
     98   const audio = {state: 'running', resumes: 0, resume() {
     99     ++this.resumes;
    100     this.state = 'running';
    101     return Promise.resolve();
    102   }};
    103   context.Module.audioContext = audio;
    104   context.Module.onRuntimeInitialized();
    105   if (ready) context.Module.onFirstFrame();
    106   context.Module.onChatBounds({x: 1, y: 355, width: 760, height: 14, screenWidth: 844, screenHeight: 390});
    107   context.Module.onTouchRegions({screenWidth: 844, screenHeight: 390,
    108     regions: [{left: 0, top: 0, right: 844, bottom: 390, world: true}]});
    109   function fire(selector, type, values = {}) {
    110     const event = new Event(type, {cancelable: true});
    111     Object.assign(event, {pointerId: 1, pointerType: 'touch', clientX: 100, clientY: 120}, values);
    112     if (type.startsWith('pointer')) document.dispatchEvent(event);
    113     document.querySelector(selector).dispatchEvent(event);
    114     return event;
    115   }
    116   return {document, window, navigator, canvas, audio, keys, repeats, modifiers, characters, mouse, wheels, cameraWheels, orbits, sizes, chatViewports, cursorHidden, hudInsets, scrolls, zooms, storage, actions, fire, reloads,
    117     firstFrame: () => context.Module.onFirstFrame(),
    118     closed: replaced => context.Module.onGameClosed(replaced),
    119     graphics: lost => context.Module.onGraphicsStatus(lost),
    120     chatBounds: bounds => context.Module.onChatBounds(bounds),
    121     touchRegions: bounds => context.Module.onTouchRegions(bounds),
    122     touchFrame: () => context.Module.onTouchFrame(touchFrame),
    123     requests, respond: value => { response = value; },
    124     creationNames, creationState: state => context.Module.onCreationState(state),
    125     element: selector => document.querySelector(selector),
    126     hold: () => { for (const callback of timers.values()) callback(); timers.clear(); }};
    127 }
    128 
    129 test('creator keeps its original render dimensions while the phone rotates', () => {
    130   const game = page();
    131   game.creationState({active: true, name: null});
    132   assert.deepEqual(game.sizes.at(-1), [1024, 768]);
    133   assert.equal(game.canvas.style.width, '520px');
    134   assert.equal(game.canvas.style.height, '390px');
    135 
    136   game.element('#game-surface').clientWidth = 390;
    137   game.element('#game-surface').clientHeight = 844;
    138   game.window.dispatchEvent(new Event('resize'));
    139   assert.equal(game.canvas.style.width, '390px');
    140   assert.equal(game.canvas.style.height, '292.5px');
    141   assert.deepEqual(game.sizes.at(-1), [1024, 768]);
    142 
    143   game.creationState({active: false, name: null});
    144   assert.equal(game.canvas.style.width, '');
    145   assert.equal(game.canvas.style.height, '');
    146   assert.deepEqual(game.sizes.at(-1), [844, 390]);
    147 });
    148 
    149 test('naming credentials block game keys until native Finish advances', async () => {
    150   const game = page();
    151   game.creationState({active: true, name: null});
    152   assert.equal(game.element('#creation-credentials').open, false);
    153   game.creationState({active: true, name: {value: ''}});
    154   game.element('#creation-player-name').value = 'phonehero';
    155   game.element('#creation-password').value = 'phone password';
    156   game.element('#creation-password-confirm').value = 'phone password';
    157   const nativeKeys = [];
    158   game.window.addEventListener('keydown', event => nativeKeys.push(event.key));
    159 
    160   const key = new Event('keydown', {cancelable: true});
    161   Object.assign(key, {key: 'a', code: 'KeyA'});
    162   game.window.dispatchEvent(key);
    163   await game.element('#creation-form').onsubmit({preventDefault() {}});
    164   await game.element('#creation-form').onsubmit({preventDefault() {}});
    165   game.creationState({active: true, name: {value: ''}});
    166 
    167   assert.equal(key.defaultPrevented, false);
    168   assert.deepEqual(nativeKeys, []);
    169   assert.equal(game.requests.length, 1);
    170   assert.equal(game.requests[0].url, 'browser-create');
    171   assert.equal(game.requests[0].method, 'POST');
    172   assert.equal(game.requests[0].body.toString(), 'name=phonehero&password=phone+password');
    173   assert.deepEqual(game.creationNames, ['Phonehero']);
    174   assert.equal(game.element('#creation-password').value, '');
    175   assert.equal(game.element('#creation-password-confirm').value, '');
    176   assert.equal(game.element('#creation-credentials').open, true);
    177   assert.equal(game.element('#creation-fields').disabled, true);
    178 
    179   game.creationState({active: true, name: null});
    180   assert.equal(game.element('#creation-credentials').open, false);
    181   assert.equal(game.document.activeElement, game.canvas);
    182   assert.deepEqual(game.sizes.at(-1), [1024, 768]);
    183 });
    184 
    185 test('rejected naming credentials leave the modal editable without advancing', async () => {
    186   const game = page();
    187   game.respond(new Response('Name already taken', {status: 409}));
    188   game.creationState({active: true, name: {value: ''}});
    189   game.element('#creation-player-name').value = 'Taken';
    190   game.element('#creation-password').value = 'valid';
    191   game.element('#creation-password-confirm').value = 'valid';
    192 
    193   await game.element('#creation-form').onsubmit({preventDefault() {}});
    194 
    195   assert.equal(game.element('#creation-credentials').open, true);
    196   assert.equal(game.element('#creation-fields').disabled, false);
    197   assert.equal(game.element('#creation-error').textContent, 'Name already taken');
    198   assert.deepEqual(game.creationNames, []);
    199 });
    200 
    201 test('mismatched passwords keep naming editable without submitting credentials', async () => {
    202   const game = page();
    203   game.creationState({active: true, name: {value: ''}});
    204   game.element('#creation-password').value = 'secret';
    205   game.element('#creation-password-confirm').value = 'different';
    206 
    207   await game.element('#creation-form').onsubmit({preventDefault() {}});
    208 
    209   assert.equal(game.requests.length, 0);
    210   assert.deepEqual(game.creationNames, []);
    211   assert.equal(game.element('#creation-credentials').open, true);
    212   assert.equal(!!game.element('#creation-fields').disabled, false);
    213   assert.equal(game.element('#creation-error').textContent, 'Passwords do not match.');
    214   assert.equal(game.document.activeElement, game.element('#creation-password-confirm'));
    215 });
    216 
    217 test('Escape keeps the naming modal open without abandoning creation', () => {
    218   const game = page();
    219   game.creationState({active: true, name: {value: ''}});
    220 
    221   const cancel = game.fire('#creation-credentials', 'cancel');
    222 
    223   assert.equal(cancel.defaultPrevented, true);
    224   assert.equal(game.element('#creation-credentials').open, true);
    225   assert.deepEqual(game.reloads, []);
    226 });
    227 
    228 test('Firefox Android leaves fullscreen for the naming keyboard', async () => {
    229   const game = page();
    230   game.document.fullscreenElement = game.canvas;
    231   game.document.exitFullscreen = async () => { game.document.fullscreenElement = null; };
    232 
    233   game.creationState({active: true, name: {value: ''}});
    234   await Promise.resolve();
    235 
    236   assert.equal(game.document.fullscreenElement, null);
    237   assert.equal(game.document.activeElement, game.element('#creation-player-name'));
    238 });
    239 
    240 test('wheel over native UI bypasses the camera gesture and preserves wheel units', () => {
    241   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64) Chrome/156.0.0.0'}});
    242   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    243     {left: 50, top: 100, right: 200, bottom: 180, world: false},
    244     {left: 0, top: 0, right: 844, bottom: 390, world: true}
    245   ]});
    246   const cameraEvents = [];
    247   game.canvas.addEventListener('wheel', event => cameraEvents.push(event));
    248 
    249   const pixel = game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 100, deltaMode: 0});
    250   const line = game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: -3, deltaMode: 1});
    251   const pageEvent = game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 1, deltaMode: 2});
    252   const fractional = game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 0.5, deltaMode: 0});
    253 
    254   assert.deepEqual(game.wheels, [[100, 120, -1], [100, 120, 1],
    255                                 [100, 120, -80], [100, 120, -0.005]]);
    256   assert.equal(cameraEvents.length, 0);
    257   assert([pixel, line, pageEvent, fractional].every(event => event.defaultPrevented));
    258   assert.deepEqual(game.mouse, []);
    259 });
    260 
    261 test('world wheel zooms in both directions without moving the orbit', () => {
    262   const game = page();
    263 
    264   game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 100, deltaMode: 0});
    265   game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: -3, deltaMode: 1});
    266 
    267   assert.deepEqual(game.zooms, [9830, -9830]);
    268   assert.deepEqual(game.cameraWheels, []);
    269   assert.deepEqual(game.wheels, []);
    270   assert.deepEqual(game.mouse, []);
    271 });
    272 
    273 test('world wheel captures its camera target and UI hit testing follows Retina coordinates', () => {
    274   const game = page();
    275   game.element('#pixel-mode').value = 'retina';
    276   game.fire('#pixel-mode', 'change');
    277   game.touchRegions({screenWidth: 1688, screenHeight: 780, regions: [
    278     {left: 180, top: 220, right: 240, bottom: 280, world: false},
    279     {left: 0, top: 0, right: 1688, bottom: 780, world: true}
    280   ]});
    281   const cameraEvents = [];
    282   game.canvas.addEventListener('wheel', event => cameraEvents.push(event));
    283 
    284   game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 100, deltaMode: 0});
    285   const world = game.fire('#canvas', 'wheel', {clientX: 300, deltaX: 12, deltaY: -50, deltaMode: 0, shiftKey: true});
    286 
    287   assert.deepEqual(game.wheels, [[200, 240, -1]]);
    288   assert.deepEqual(game.cameraWheels, [[932, -8402]]);
    289   assert.deepEqual(game.mouse, []);
    290   assert.deepEqual(cameraEvents, []);
    291   assert.equal(world.defaultPrevented, true);
    292 });
    293 
    294 test('wheel cannot start a camera gesture before native world bounds are available', () => {
    295   const game = page();
    296   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: []});
    297   const cameraEvents = [];
    298   game.canvas.addEventListener('wheel', event => cameraEvents.push(event));
    299 
    300   game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 100, deltaMode: 0});
    301 
    302   assert.deepEqual(game.wheels, [[100, 120, -1]]);
    303   assert.deepEqual(cameraEvents, []);
    304 });
    305 
    306 test('world to UI wheels never synthesize buttons and focus loss stops pending camera input', () => {
    307   const game = page();
    308   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    309     {left: 50, top: 100, right: 200, bottom: 180, world: false},
    310     {left: 0, top: 0, right: 844, bottom: 390, world: true}
    311   ]});
    312 
    313   game.fire('#canvas', 'wheel', {clientX: 300, deltaX: 0.5, deltaY: -0.25, deltaMode: 0, shiftKey: true});
    314   game.fire('#canvas', 'wheel', {deltaX: 0, deltaY: 100, deltaMode: 0});
    315   game.window.dispatchEvent(new Event('blur'));
    316 
    317   assert.deepEqual(game.cameraWheels, [[39, -42]]);
    318   assert.deepEqual(game.wheels, [[100, 120, -1]]);
    319   assert.deepEqual(game.mouse, []);
    320   assert.deepEqual(game.orbits, [[0, 0, 0]]);
    321 });
    322 
    323 test('Tab stays in the focused game and still reaches native key handlers', () => {
    324   const game = page();
    325   const delivered = [];
    326   game.window.addEventListener('keydown', event => delivered.push([event.key, !!event.shiftKey, !!event.repeat]));
    327 
    328   for (const values of [{}, {shiftKey: true}, {repeat: true}]) {
    329     for (const type of ['keydown', 'keyup']) {
    330       const event = new Event(type, {cancelable: true});
    331       Object.assign(event, {key: 'Tab', code: 'Tab'}, values);
    332       game.window.dispatchEvent(event);
    333       assert.equal(event.defaultPrevented, true);
    334     }
    335   }
    336 
    337   assert.deepEqual(delivered, [['Tab', false, false], ['Tab', true, false], ['Tab', false, true]]);
    338   assert.deepEqual(game.keys, []);
    339 });
    340 
    341 test('F1 reaches the focused game and cancels browser Help', () => {
    342   const game = page();
    343   const event = new Event('keydown', {cancelable: true});
    344   event.key = 'F1';
    345   const delivered = [];
    346   game.window.addEventListener('keydown', event => delivered.push(event.key));
    347 
    348   game.window.dispatchEvent(event);
    349 
    350   assert.equal(event.defaultPrevented, true);
    351   assert.deepEqual(delivered, ['F1']);
    352 });
    353 
    354 test('Firefox Quick Find keys reach native chat without opening browser search', () => {
    355   const game = page();
    356   const delivered = [];
    357   game.window.addEventListener('keypress', event => delivered.push(event.key));
    358 
    359   for (const key of ['/', "'"]) {
    360     for (const type of ['keydown', 'keypress', 'keyup']) {
    361       const event = new Event(type, {cancelable: true});
    362       Object.assign(event, {key});
    363       game.window.dispatchEvent(event);
    364       assert.equal(event.defaultPrevented, type === 'keypress');
    365     }
    366   }
    367 
    368   assert.deepEqual(delivered, ['/', "'"]);
    369   assert.deepEqual(game.characters, []);
    370 });
    371 
    372 test('Tab, F1 and Quick Find retain normal behavior outside the ready game canvas', () => {
    373   for (const scenario of ['loading', 'options', 'chat', 'button']) {
    374     const game = page({ready: scenario !== 'loading'});
    375     if (scenario === 'options') game.fire('#menu-button', 'click');
    376     if (scenario === 'chat') game.fire('#chat-button', 'click');
    377     if (scenario === 'button') game.element('#menu-button').focus();
    378     game.keys.length = 0;
    379     for (const [type, key] of [['keydown', 'Tab'], ['keydown', 'F1'], ['keypress', '/'], ['keypress', "'"]]) {
    380       const event = new Event(type, {cancelable: true});
    381       Object.assign(event, {key});
    382       game.window.dispatchEvent(event);
    383       assert.equal(event.defaultPrevented, false, scenario);
    384     }
    385     assert.deepEqual(game.keys, [], scenario);
    386   }
    387 });
    388 
    389 test('physical keypad keys survive NumLock aliases and keypad Enter remains distinct', () => {
    390   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}});
    391   const events = [
    392     {code: 'Numpad2', key: 'ArrowDown', keyCode: 40},
    393     {code: 'Numpad2', key: '2', keyCode: 98},
    394     {code: 'NumpadEnter', key: 'Enter', keyCode: 13},
    395     {code: 'NumpadAdd', key: '+', keyCode: 107},
    396   ];
    397 
    398   for (const values of events) {
    399     for (const type of ['keydown', 'keypress', 'keyup']) {
    400       const event = new Event(type, {cancelable: true});
    401       Object.assign(event, values, {location: type === 'keyup' ? 1 : 3, getModifierState: () => false});
    402       game.window.dispatchEvent(event);
    403       assert.equal(event.defaultPrevented, true);
    404     }
    405   }
    406 
    407   assert.deepEqual(game.keys, [
    408     [1073741914, true], [1073741914, false],
    409     [1073741914, true], [1073741914, false],
    410     [1073741912, true], [1073741912, false],
    411     [1073741911, true], [1073741911, false],
    412   ]);
    413   assert.deepEqual(game.characters, []);
    414 });
    415 
    416 test('held keypad camera input preserves repeat and releases on blur', () => {
    417   const game = page();
    418   const down = new Event('keydown', {cancelable: true});
    419   Object.assign(down, {code: 'Numpad8', key: 'ArrowUp', keyCode: 38, repeat: false, getModifierState: () => false});
    420   const repeat = new Event('keydown', {cancelable: true});
    421   Object.assign(repeat, {code: 'Numpad8', key: 'ArrowUp', keyCode: 38, repeat: true, getModifierState: () => false});
    422 
    423   game.window.dispatchEvent(down);
    424   game.window.dispatchEvent(repeat);
    425   game.window.dispatchEvent(new Event('blur'));
    426 
    427   assert.deepEqual(game.keys, [[1073741920, true], [1073741920, true], [1073741920, false]]);
    428   assert.deepEqual(game.repeats, [false, true, false]);
    429 });
    430 
    431 test('physical keypad input preserves Ctrl Shift Alt and lock modifiers', () => {
    432   const game = page();
    433   const event = new Event('keydown', {cancelable: true});
    434   Object.assign(event, {code: 'Numpad2', key: '2', keyCode: 98,
    435     ctrlKey: true, shiftKey: true, altKey: true,
    436     getModifierState: key => key === 'CapsLock' || key === 'NumLock'});
    437 
    438   game.window.dispatchEvent(event);
    439 
    440   assert.deepEqual(game.keys, [[1073741914, true]]);
    441   assert.deepEqual(game.modifiers, [0x33c3]);
    442 });
    443 
    444 test('dragging chat scrolls history without clicks, camera movement or a held right click', () => {
    445   const game = page();
    446   game.chatBounds({x: 13, y: 201, width: 336, height: 14, screenWidth: 844, screenHeight: 390,
    447     history: {x: 12, y: 100, width: 338, height: 100}});
    448   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    449     {left: 12, top: 100, right: 350, bottom: 200, chat: true}
    450   ]});
    451 
    452   game.fire('#canvas', 'pointerdown');
    453   game.fire('#canvas', 'pointermove', {clientY: 150});
    454   game.hold();
    455   game.fire('#canvas', 'pointerdown', {pointerId: 2});
    456   game.fire('#canvas', 'pointermove', {clientY: 135});
    457   game.fire('#canvas', 'pointerup');
    458 
    459   assert.deepEqual(game.scrolls, [30, -15]);
    460   assert.deepEqual(game.mouse, []);
    461   assert.deepEqual(game.orbits, []);
    462 });
    463 
    464 test('chat history scrolls while composing without taking focus from the keyboard', () => {
    465   const game = page();
    466   game.chatBounds({x: 21, y: 201, width: 328, height: 14, screenWidth: 844, screenHeight: 390,
    467     history: {x: 20, y: 20, width: 330, height: 180}});
    468   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    469     {left: 20, top: 20, right: 350, bottom: 200, chat: true}
    470   ]});
    471   game.fire('#chat-button', 'click');
    472 
    473   const drag = game.fire('#canvas', 'pointerdown');
    474   game.fire('#canvas', 'pointermove', {clientY: 150});
    475   game.fire('#canvas', 'pointercancel');
    476 
    477   assert.deepEqual(game.scrolls, [30]);
    478   assert.equal(drag.defaultPrevented, true);
    479   assert.equal(game.document.activeElement, game.element('#chat-text'));
    480   assert.deepEqual(game.mouse, []);
    481 });
    482 
    483 test('chat scrolling converts CSS movement into Retina rendering pixels', () => {
    484   const game = page();
    485   game.canvas.clientWidth = 422;
    486   game.canvas.clientHeight = 195;
    487   game.chatBounds({x: 25, y: 381, width: 674, height: 14, screenWidth: 844, screenHeight: 390,
    488     history: {x: 24, y: 100, width: 676, height: 280}});
    489   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    490     {left: 24, top: 100, right: 700, bottom: 380, chat: true}
    491   ]});
    492 
    493   game.fire('#canvas', 'pointerdown');
    494   game.fire('#canvas', 'pointermove', {clientY: 150});
    495   game.fire('#canvas', 'pointerup');
    496 
    497   assert.deepEqual(game.scrolls, [60]);
    498 });
    499 
    500 test('a native window covering history keeps its own touch interaction', () => {
    501   const game = page();
    502   game.chatBounds({x: 13, y: 201, width: 336, height: 14, screenWidth: 844, screenHeight: 390,
    503     history: {x: 12, y: 100, width: 338, height: 100}});
    504   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    505     {left: 50, top: 110, right: 200, bottom: 180, world: false},
    506     {left: 12, top: 100, right: 350, bottom: 200, chat: true}
    507   ]});
    508 
    509   game.fire('#canvas', 'pointerdown');
    510   game.fire('#canvas', 'pointermove', {clientY: 150});
    511   game.fire('#canvas', 'pointerup');
    512 
    513   assert.deepEqual(game.scrolls, []);
    514   assert.ok(game.mouse.some(event => event[0] === 'mousedown'));
    515 });
    516 
    517 test('desktop touch cannot activate chat scrolling', () => {
    518   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}});
    519   game.chatBounds({x: 13, y: 201, width: 336, height: 14, screenWidth: 844, screenHeight: 390,
    520     history: {x: 12, y: 100, width: 338, height: 100}});
    521   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    522     {left: 12, top: 100, right: 350, bottom: 200, chat: true}
    523   ]});
    524 
    525   game.fire('#canvas', 'pointerdown');
    526   game.fire('#canvas', 'pointermove', {clientY: 150});
    527   game.fire('#canvas', 'pointerup');
    528 
    529   assert.deepEqual(game.scrolls, []);
    530 });
    531 
    532 test('overlays wait for a rendered game world after loading', () => {
    533   const game = page({ready: false});
    534   assert.equal(game.element('#game-toolbar').hidden, true);
    535   assert.equal(game.element('#touch-controls').hidden, true);
    536   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: []});
    537 
    538   game.firstFrame();
    539   assert.equal(game.element('#game-toolbar').hidden, true);
    540   assert.equal(game.element('#touch-controls').hidden, true);
    541   game.touchRegions({screenWidth: 844, screenHeight: 390,
    542     regions: [{left: 0, top: 0, right: 844, bottom: 370, world: true}]});
    543 
    544   assert.equal(game.element('#game-toolbar').hidden, false);
    545   assert.equal(game.element('#touch-controls').hidden, false);
    546 });
    547 
    548 test('overlays stay revealed until the last touch ends and reset on blur', () => {
    549   const game = page();
    550   const surface = game.element('#game-surface');
    551 
    552   game.fire('#canvas', 'pointerdown');
    553   game.fire('#move-pad', 'pointerdown', {pointerId: 2});
    554   game.fire('#canvas', 'pointerup');
    555   assert.equal(surface.dataset.overlaysActive, '');
    556   game.fire('#move-pad', 'pointercancel', {pointerId: 2});
    557   assert.equal(surface.dataset.overlaysActive, undefined);
    558   game.fire('#canvas', 'pointerdown');
    559   assert.equal(surface.dataset.overlaysActive, '');
    560   game.window.dispatchEvent(new Event('blur'));
    561 
    562   assert.equal(surface.dataset.overlaysActive, undefined);
    563 });
    564 
    565 test('chat actions stay visible without touching and fade after chat closes', () => {
    566   const game = page();
    567 
    568   game.fire('#chat-button', 'click');
    569   assert.equal(game.element('#game-surface').dataset.overlaysActive, '');
    570   game.fire('#chat-cancel', 'click');
    571 
    572   assert.equal(game.element('#game-surface').dataset.overlaysActive, undefined);
    573 });
    574 
    575 test('touch taps move the cursor and send one complete left click', () => {
    576   const game = page();
    577 
    578   game.fire('#canvas', 'pointerdown');
    579   game.fire('#canvas', 'pointerup');
    580   game.hold();
    581 
    582   assert.deepEqual(game.cursorHidden, [true]);
    583   assert.deepEqual(game.mouse, [
    584     ['mousemove', 100, 120, 0], ['mousemove', 100, 120, 0],
    585     ['mousedown', 100, 120, 0], ['mouseup', 100, 120, 0]
    586   ]);
    587 });
    588 
    589 test('touch compatibility mouse events cannot reach SDL during or after orbit', () => {
    590   const game = page();
    591   const nativeMouseEvents = [];
    592   for (const type of ['mousemove', 'mousedown', 'mouseup'])
    593     game.canvas.addEventListener(type, event => nativeMouseEvents.push(event.type));
    594   game.fire('#canvas', 'pointerdown');
    595   game.fire('#canvas', 'pointerdown', {pointerId: 2});
    596 
    597   game.fire('#canvas', 'mousemove');
    598   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 102});
    599   game.fire('#canvas', 'mousemove');
    600   game.fire('#canvas', 'pointerup');
    601   game.fire('#canvas', 'pointerup', {pointerId: 2});
    602   game.fire('#canvas', 'mousemove');
    603   game.fire('#canvas', 'mousedown');
    604   game.fire('#canvas', 'mouseup');
    605 
    606   assert.deepEqual(nativeMouseEvents, []);
    607   game.fire('#canvas', 'pointermove', {pointerType: 'mouse'});
    608   game.fire('#canvas', 'mousemove');
    609   assert.deepEqual(nativeMouseEvents, ['mousemove']);
    610 });
    611 
    612 test('desktop mouse events pass through after touch input', () => {
    613   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}});
    614   let mouseEvents = 0;
    615   game.canvas.addEventListener('mousemove', () => ++mouseEvents);
    616 
    617   game.fire('#canvas', 'pointerdown');
    618   game.fire('#canvas', 'mousemove');
    619 
    620   assert.deepEqual(game.cursorHidden, [false]);
    621   assert.equal(mouseEvents, 1);
    622 });
    623 
    624 test('a native UI drag keeps the left button when it crosses into the world', () => {
    625   const game = page();
    626   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    627     {left: 90, top: 110, right: 120, bottom: 140, world: false},
    628     {left: 0, top: 0, right: 844, bottom: 390, world: true}
    629   ]});
    630   game.fire('#canvas', 'pointerdown');
    631 
    632   game.fire('#canvas', 'pointermove', {clientX: 140});
    633   game.fire('#canvas', 'pointercancel');
    634   game.hold();
    635 
    636   assert.deepEqual(game.mouse, [
    637     ['mousemove', 100, 120, 0], ['mousedown', 100, 120, 0],
    638     ['mousemove', 140, 120, 0], ['mouseup', 140, 120, 0]
    639   ]);
    640 });
    641 
    642 test('a world swipe rotates without selecting an object on release', () => {
    643   const game = page();
    644   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    645     {left: 400, top: 160, right: 500, bottom: 250, world: false},
    646     {left: 0, top: 0, right: 844, bottom: 390, world: true}
    647   ]});
    648 
    649   game.fire('#canvas', 'pointerdown', {clientX: 300, clientY: 200});
    650   game.fire('#canvas', 'pointermove', {clientX: 420, clientY: 180});
    651   game.fire('#canvas', 'pointerup', {clientX: 420, clientY: 180});
    652   game.hold();
    653 
    654   assert.deepEqual(game.mouse, [
    655     ['mousemove', 300, 200, 0], ['mousemove', 422, 98, 0], ['mousedown', 422, 98, 2],
    656     ['mousemove', 542, 78, 0], ['mousemove', 542, 78, 0],
    657     ['mouseup', 542, 78, 2]
    658   ]);
    659 });
    660 
    661 test('native UI hit testing scales correctly in Retina mode', () => {
    662   const game = page();
    663   game.element('#pixel-mode').value = 'retina';
    664   game.fire('#pixel-mode', 'change');
    665   game.touchRegions({screenWidth: 1688, screenHeight: 780, regions: [
    666     {left: 190, top: 230, right: 220, bottom: 260, world: false},
    667     {left: 0, top: 0, right: 1688, bottom: 780, world: true}
    668   ]});
    669 
    670   game.fire('#canvas', 'pointerdown');
    671   game.fire('#canvas', 'pointermove', {clientX: 140});
    672   game.fire('#canvas', 'pointercancel');
    673 
    674   assert.deepEqual(game.hudInsets, [[24, 0], [48, 0]]);
    675   assert.deepEqual(game.mouse, [
    676     ['mousemove', 200, 240, 0], ['mousedown', 200, 240, 0],
    677     ['mousemove', 280, 240, 0], ['mouseup', 280, 240, 0]
    678   ]);
    679 });
    680 
    681 test('mobile fullscreen adds top clearance and restores it on exit', () => {
    682   const game = page();
    683 
    684   game.document.fullscreenElement = game.element('#game-surface');
    685   game.document.dispatchEvent(new Event('fullscreenchange'));
    686   game.element('#pixel-mode').value = 'retina';
    687   game.fire('#pixel-mode', 'change');
    688   game.document.fullscreenElement = null;
    689   game.document.dispatchEvent(new Event('fullscreenchange'));
    690 
    691   assert.deepEqual(game.hudInsets, [[24, 0], [24, 24], [48, 48], [48, 0]]);
    692 });
    693 
    694 test('a world swipe anchors beside a window covering the camera origin', () => {
    695   const game = page();
    696   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    697     {left: 350, top: 80, right: 500, bottom: 130, world: false},
    698     {left: 0, top: 0, right: 844, bottom: 390, world: true}
    699   ]});
    700 
    701   game.fire('#canvas', 'pointerdown', {clientX: 300, clientY: 200});
    702   game.fire('#canvas', 'pointermove', {clientX: 420, clientY: 180});
    703   game.fire('#canvas', 'pointercancel');
    704 
    705   assert.deepEqual(game.mouse, [
    706     ['mousemove', 300, 200, 0], ['mousemove', 300, 98, 0], ['mousedown', 300, 98, 2],
    707     ['mousemove', 420, 78, 0], ['mouseup', 420, 78, 2]
    708   ]);
    709 });
    710 
    711 test('touch coordinates follow the selected Retina resolution', () => {
    712   const game = page();
    713   game.element('#pixel-mode').value = 'retina';
    714   game.fire('#pixel-mode', 'change');
    715 
    716   game.fire('#canvas', 'pointerdown');
    717   game.fire('#canvas', 'pointerup');
    718 
    719   assert.deepEqual(game.mouse, [
    720     ['mousemove', 200, 240, 0], ['mousemove', 200, 240, 0],
    721     ['mousedown', 200, 240, 0], ['mouseup', 200, 240, 0]
    722   ]);
    723 });
    724 
    725 test('holding a touch makes a right click and another finger cannot release it', () => {
    726   const game = page();
    727   game.fire('#canvas', 'pointerdown');
    728 
    729   game.hold();
    730   game.fire('#canvas', 'pointercancel', {pointerId: 2});
    731   game.fire('#canvas', 'pointerup');
    732 
    733   assert.deepEqual(game.mouse, [
    734     ['mousemove', 100, 120, 0], ['mousedown', 100, 120, 2],
    735     ['mousemove', 100, 120, 0], ['mouseup', 100, 120, 2]
    736   ]);
    737 });
    738 
    739 test('inventory taps hold the native menu open until the next tap', () => {
    740   const game = page();
    741   game.touchRegions({screenWidth: 844, screenHeight: 390,
    742     regions: [{left: 60, top: 80, right: 200, bottom: 200, detector: true},
    743       {left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    744 
    745   game.fire('#canvas', 'pointerdown');
    746   game.fire('#canvas', 'pointerup');
    747   game.hold();
    748 
    749   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), []);
    750   game.touchFrame();
    751   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), [['mousedown', 100, 120, 2]]);
    752 
    753   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [], itemMenu: true});
    754   game.fire('#canvas', 'pointerdown', {clientX: 60, clientY: 150});
    755   game.fire('#canvas', 'pointerup', {clientX: 60, clientY: 150});
    756   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), [
    757     ['mousedown', 100, 120, 2], ['mouseup', 60, 150, 2]
    758   ]);
    759 });
    760 
    761 test('an inventory hold picks up an item and an occupied destination keeps left-click placement', () => {
    762   const game = page();
    763   game.touchRegions({screenWidth: 844, screenHeight: 390,
    764     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    765 
    766   game.fire('#canvas', 'pointerdown');
    767   game.hold();
    768   game.fire('#canvas', 'pointermove', {clientX: 160, clientY: 120});
    769   game.fire('#canvas', 'pointerup', {clientX: 160, clientY: 120});
    770   game.touchRegions({screenWidth: 844, screenHeight: 390,
    771     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: false}]});
    772   game.fire('#canvas', 'pointerdown');
    773   game.fire('#canvas', 'pointerup');
    774 
    775   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), [
    776     ['mousedown', 100, 120, 0], ['mouseup', 100, 120, 0],
    777     ['mousedown', 160, 120, 0], ['mouseup', 160, 120, 0],
    778     ['mousedown', 100, 120, 0], ['mouseup', 100, 120, 0]
    779   ]);
    780 });
    781 
    782 test('double tapping an inventory item sends one native right click without opening a menu', () => {
    783   const game = page();
    784   game.touchRegions({screenWidth: 844, screenHeight: 390,
    785     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    786 
    787   game.fire('#canvas', 'pointerdown');
    788   game.fire('#canvas', 'pointerup');
    789   game.fire('#canvas', 'pointerdown');
    790   game.fire('#canvas', 'pointerup');
    791   game.touchFrame();
    792   game.hold();
    793 
    794   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), [
    795     ['mousedown', 100, 120, 2], ['mouseup', 100, 120, 2]
    796   ]);
    797 });
    798 
    799 test('tapping another item during the double-tap window does not use either item', () => {
    800   const game = page();
    801   game.touchRegions({screenWidth: 844, screenHeight: 390,
    802     regions: [{left: 80, top: 100, right: 200, bottom: 140, item: true}]});
    803 
    804   game.fire('#canvas', 'pointerdown');
    805   game.fire('#canvas', 'pointerup');
    806   game.fire('#canvas', 'pointerdown', {clientX: 160});
    807   game.fire('#canvas', 'pointerup', {clientX: 160});
    808   game.hold();
    809   game.touchFrame();
    810 
    811   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), []);
    812 });
    813 
    814 test('a window covering an inventory item retains its normal tap', () => {
    815   const game = page();
    816   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
    817     {left: 60, top: 80, right: 200, bottom: 200, detector: true},
    818     {left: 60, top: 80, right: 200, bottom: 200},
    819     {left: 80, top: 100, right: 120, bottom: 140, item: true}
    820   ]});
    821 
    822   game.fire('#canvas', 'pointerdown');
    823   game.fire('#canvas', 'pointerup');
    824 
    825   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), [
    826     ['mousedown', 100, 120, 0], ['mouseup', 100, 120, 0]
    827   ]);
    828 });
    829 
    830 test('lifting a stationary inventory hold leaves the item picked up', () => {
    831   const game = page();
    832   game.touchRegions({screenWidth: 844, screenHeight: 390,
    833     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    834 
    835   game.fire('#canvas', 'pointerdown');
    836   game.hold();
    837   game.fire('#canvas', 'pointerup');
    838 
    839   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), [
    840     ['mousedown', 100, 120, 0], ['mouseup', 100, 120, 0]
    841   ]);
    842 });
    843 
    844 test('moving an inventory touch before the hold threshold cancels pickup', () => {
    845   const game = page();
    846   game.touchRegions({screenWidth: 844, screenHeight: 390,
    847     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    848 
    849   game.fire('#canvas', 'pointerdown');
    850   game.fire('#canvas', 'pointermove', {clientX: 160});
    851   game.hold();
    852   game.fire('#canvas', 'pointerup', {clientX: 160});
    853 
    854   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), []);
    855 });
    856 
    857 test('blur cancels a delayed inventory menu before any right click', () => {
    858   const game = page();
    859   game.touchRegions({screenWidth: 844, screenHeight: 390,
    860     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    861   game.fire('#canvas', 'pointerdown');
    862   game.fire('#canvas', 'pointerup');
    863 
    864   game.window.dispatchEvent(new Event('blur'));
    865   game.hold();
    866   game.touchFrame();
    867   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [], itemMenu: true});
    868 
    869   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'), []);
    870 });
    871 
    872 test('blur releases an opened inventory menu away from the item', () => {
    873   const game = page();
    874   game.touchRegions({screenWidth: 844, screenHeight: 390,
    875     regions: [{left: 80, top: 100, right: 120, bottom: 140, item: true}]});
    876   game.fire('#canvas', 'pointerdown');
    877   game.fire('#canvas', 'pointerup');
    878   game.hold();
    879   game.touchFrame();
    880 
    881   game.window.dispatchEvent(new Event('blur'));
    882 
    883   assert.deepEqual(game.mouse.slice(-2), [['mousemove', -1, -1, 0], ['mouseup', -1, -1, 2]]);
    884 });
    885 
    886 test('movement and camera work together and opening settings releases both', () => {
    887   const game = page();
    888   game.element('#move-pad').clientWidth = game.element('#move-pad').clientHeight = 112;
    889 
    890   game.fire('#move-pad', 'pointerdown', {clientX: 90, clientY: 10});
    891   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 400, clientY: 250});
    892   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 600, clientY: 240});
    893   game.fire('#menu-button', 'click');
    894 
    895   assert.deepEqual(game.keys, [[99, true], [119, true], [99, false], [119, false]]);
    896   assert.deepEqual(game.mouse, [
    897     ['mousemove', 400, 250, 0], ['mousemove', 422, 98, 0], ['mousedown', 422, 98, 2],
    898     ['mousemove', 622, 88, 0], ['mouseup', 622, 88, 2]
    899   ]);
    900   assert.equal(game.element('#touch-controls').hidden, true);
    901   assert.equal(game.element('#player-menu').open, true);
    902 });
    903 
    904 test('two world fingers orbit while a third finger keeps moving', () => {
    905   const game = page();
    906   game.element('#move-pad').clientWidth = game.element('#move-pad').clientHeight = 112;
    907   game.fire('#move-pad', 'pointerdown', {pointerId: 3, clientX: 56, clientY: 10});
    908   game.fire('#canvas', 'pointerdown', {clientX: 400, clientY: 200});
    909   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 500, clientY: 200});
    910 
    911   game.fire('#canvas', 'pointermove', {clientX: 420, clientY: 180});
    912   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 520, clientY: 180});
    913   game.fire('#canvas', 'pointerup', {pointerId: 2});
    914   game.fire('#canvas', 'pointermove', {clientX: 600});
    915   game.fire('#canvas', 'pointerup');
    916   game.hold();
    917 
    918   assert.deepEqual(game.keys, [[119, true]]);
    919   assert.deepEqual(game.mouse, [['mousemove', 400, 200, 0]]);
    920   assert.deepEqual(game.orbits, [[0, 0, 1], [272, -588, 1], [272, -588, 1], [0, 0, 0]]);
    921   game.fire('#move-pad', 'pointerup', {pointerId: 3});
    922   assert.deepEqual(game.keys.at(-1), [119, false]);
    923 });
    924 
    925 test('two world fingers pinch in and out while orbiting, then stop on release', () => {
    926   const game = page();
    927   game.fire('#canvas', 'pointerdown', {clientX: 300, clientY: 200});
    928   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 400, clientY: 200});
    929 
    930   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 500, clientY: 200});
    931   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 400, clientY: 200});
    932   game.fire('#canvas', 'pointerup', {pointerId: 2});
    933   game.fire('#canvas', 'pointermove', {clientX: 200, clientY: 200});
    934   game.fire('#canvas', 'pointerup');
    935   game.hold();
    936 
    937   assert.deepEqual(game.zooms, [-45426, 45426]);
    938   assert.deepEqual(game.orbits, [[0, 0, 1], [1359, 0, 1], [-1359, 0, 1], [0, 0, 0]]);
    939   assert.deepEqual(game.mouse, [['mousemove', 300, 200, 0]]);
    940 });
    941 
    942 test('pinching after cancellation cannot zoom until a new two-finger gesture', () => {
    943   const game = page();
    944   game.fire('#canvas', 'pointerdown', {clientX: 300, clientY: 200});
    945   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 400, clientY: 200});
    946 
    947   game.fire('#canvas', 'pointercancel');
    948   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 500, clientY: 200});
    949 
    950   assert.deepEqual(game.zooms, []);
    951   assert.deepEqual(game.orbits, [[0, 0, 1], [0, 0, 0]]);
    952 });
    953 
    954 test('a two-finger rotation with unchanged separation does not zoom', () => {
    955   const game = page();
    956   game.fire('#canvas', 'pointerdown', {clientX: 300, clientY: 200});
    957   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 400, clientY: 200});
    958 
    959   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 300, clientY: 300});
    960 
    961   assert.deepEqual(game.zooms, []);
    962   assert.deepEqual(game.orbits, [[0, 0, 1], [-1359, 2941, 1]]);
    963 });
    964 
    965 test('adding a second world finger releases steering before starting orbit', () => {
    966   const game = page();
    967   const first = game.fire('#canvas', 'pointerdown');
    968   game.fire('#canvas', 'pointermove', {clientX: 140});
    969 
    970   game.fire('#canvas', 'pointerdown', {pointerId: 2});
    971   game.window.dispatchEvent(new Event('blur'));
    972   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 200});
    973 
    974   assert.equal(first.defaultPrevented, false);
    975   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove').map(event => [event[0], event[3]]),
    976     [['mousedown', 2], ['mouseup', 2]]);
    977   assert.deepEqual(game.keys, []);
    978   assert.deepEqual(game.orbits, [[0, 0, 1], [0, 0, 0]]);
    979 });
    980 
    981 test('mobile buttons leave pointer defaults enabled for Firefox multitouch', () => {
    982   const game = page();
    983   const cancelled = [];
    984 
    985   cancelled.push(game.fire('#move-pad', 'pointerdown').defaultPrevented);
    986   game.fire('#move-pad', 'pointerup');
    987   cancelled.push(game.fire('#autorun-button', 'pointerdown').defaultPrevented);
    988   for (const button of game.actions) {
    989     const event = new Event('pointerdown', {cancelable: true});
    990     button.dispatchEvent(event);
    991     cancelled.push(event.defaultPrevented);
    992   }
    993 
    994   assert.deepEqual(cancelled, [false, false, false, false, false]);
    995 });
    996 
    997 test('UI touches cannot join or start a camera orbit', () => {
    998   const game = page();
    999   game.touchRegions({screenWidth: 844, screenHeight: 390, regions: [
   1000     {left: 90, top: 110, right: 120, bottom: 140, world: false},
   1001     {left: 0, top: 0, right: 844, bottom: 390, world: true}
   1002   ]});
   1003 
   1004   game.fire('#canvas', 'pointerdown', {clientX: 300});
   1005   game.fire('#canvas', 'pointerdown', {pointerId: 2});
   1006   game.fire('#canvas', 'pointercancel');
   1007   game.fire('#canvas', 'pointerdown');
   1008   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 300});
   1009   game.fire('#canvas', 'pointermove', {clientX: 140});
   1010   game.fire('#canvas', 'pointercancel');
   1011 
   1012   assert.deepEqual(game.keys, []);
   1013   assert.deepEqual(game.orbits, []);
   1014   assert.deepEqual(game.mouse.filter(event => event[0] !== 'mousemove'),
   1015     [['mousedown', 100, 120, 0], ['mouseup', 140, 120, 0]]);
   1016 });
   1017 
   1018 test('releasing movement leaves orbit active until a camera finger is cancelled', () => {
   1019   const game = page();
   1020   game.element('#move-pad').clientWidth = game.element('#move-pad').clientHeight = 112;
   1021   game.fire('#move-pad', 'pointerdown', {pointerId: 3, clientX: 56, clientY: 10});
   1022   game.fire('#canvas', 'pointerdown');
   1023   game.fire('#canvas', 'pointerdown', {pointerId: 2});
   1024 
   1025   game.fire('#move-pad', 'pointerup', {pointerId: 3});
   1026   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 140});
   1027   game.fire('#canvas', 'lostpointercapture');
   1028   game.fire('#canvas', 'pointerup');
   1029   game.fire('#canvas', 'pointerup', {pointerId: 2});
   1030 
   1031   assert.deepEqual(game.keys, [[119, true], [119, false]]);
   1032   assert.deepEqual(game.orbits, [[0, 0, 1], [544, 0, 1], [0, 0, 0]]);
   1033   assert.deepEqual(game.mouse, [['mousemove', 100, 120, 0]]);
   1034 });
   1035 
   1036 test('autorun keeps moving after releasing the joystick and stops on a second press', () => {
   1037   const game = page();
   1038   game.element('#move-pad').clientWidth = game.element('#move-pad').clientHeight = 112;
   1039   game.fire('#move-pad', 'pointerdown', {clientX: 56, clientY: 10});
   1040 
   1041   game.fire('#autorun-button', 'pointerdown', {pointerId: 2});
   1042   game.fire('#autorun-button', 'click', {detail: 1});
   1043   game.fire('#move-pad', 'pointerup');
   1044 
   1045   assert.deepEqual(game.keys, [[119, true]]);
   1046   assert.equal(game.element('#autorun-button').textContent, 'Stop');
   1047   assert.equal(game.element('#autorun-button').ariaPressed, 'true');
   1048   game.fire('#autorun-button', 'pointerdown');
   1049   assert.deepEqual(game.keys, [[119, true], [119, false]]);
   1050   assert.equal(game.element('#autorun-button').textContent, 'Autorun');
   1051 });
   1052 
   1053 test('pulling the joystick backward cancels autorun', () => {
   1054   const game = page();
   1055   game.element('#move-pad').clientWidth = game.element('#move-pad').clientHeight = 112;
   1056   game.fire('#autorun-button', 'pointerdown');
   1057 
   1058   game.fire('#move-pad', 'pointerdown', {pointerId: 2, clientX: 56, clientY: 100});
   1059   game.fire('#move-pad', 'pointerup', {pointerId: 2});
   1060 
   1061   assert.deepEqual(game.keys, [[119, true], [119, false], [115, true], [115, false]]);
   1062   assert.equal(game.element('#autorun-button').ariaPressed, 'false');
   1063 });
   1064 
   1065 test('opening chat or options and losing focus stop autorun', () => {
   1066   for (const target of ['#chat-button', '#menu-button', null]) {
   1067     const game = page();
   1068     game.fire('#autorun-button', 'pointerdown');
   1069 
   1070     if (target) game.fire(target, 'click');
   1071     else game.window.dispatchEvent(new Event('blur'));
   1072 
   1073     assert.deepEqual(game.keys.slice(0, 2), [[119, true], [119, false]]);
   1074     assert.equal(game.element('#autorun-button').ariaPressed, 'false');
   1075   }
   1076 });
   1077 
   1078 test('desktop browsers cannot activate orbit, pinch zoom or autorun', () => {
   1079   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)', maxTouchPoints: 5}});
   1080 
   1081   game.fire('#canvas', 'pointerdown');
   1082   game.fire('#canvas', 'pointerdown', {pointerId: 2, clientX: 300});
   1083   game.fire('#canvas', 'pointermove', {clientX: 200});
   1084   game.fire('#autorun-button', 'pointerdown');
   1085   game.fire('#autorun-button', 'click', {detail: 0});
   1086 
   1087   assert.deepEqual(game.keys, []);
   1088   assert.deepEqual(game.mouse, []);
   1089   assert.deepEqual(game.orbits, []);
   1090   assert.deepEqual(game.zooms, []);
   1091 });
   1092 
   1093 test('blur releases movement and subsequent finger motion cannot restart it', () => {
   1094   const game = page();
   1095   game.element('#move-pad').clientWidth = game.element('#move-pad').clientHeight = 112;
   1096   game.fire('#move-pad', 'pointerdown', {clientX: 56, clientY: 10});
   1097 
   1098   game.window.dispatchEvent(new Event('blur'));
   1099   game.fire('#move-pad', 'pointermove', {clientX: 56, clientY: 0});
   1100 
   1101   assert.deepEqual(game.keys, [[119, true], [119, false]]);
   1102 });
   1103 
   1104 test('native chat sends composed text once and keeps typing out of game controls', () => {
   1105   const game = page();
   1106   let gameKeyEvents = 0;
   1107   game.window.addEventListener('keydown', () => ++gameKeyEvents);
   1108 
   1109   game.fire('#chat-button', 'click');
   1110   game.element('#chat-text').value = 'H\u00e9llo!';
   1111   game.window.dispatchEvent(new Event('keydown'));
   1112   game.fire('#chat-compose', 'submit');
   1113 
   1114   assert.deepEqual(game.characters, [72, 233, 108, 108, 111, 33]);
   1115   assert.deepEqual(game.keys, [[27, true], [27, false], [13, true], [13, false], [13, true], [13, false]]);
   1116   assert.equal(gameKeyEvents, 0);
   1117   assert.equal(game.element('#chat-compose').hidden, true);
   1118   assert.equal(game.document.activeElement, game.canvas);
   1119 });
   1120 
   1121 test('action buttons respond on press without repeating on the resulting click', () => {
   1122   const game = page();
   1123 
   1124   for (const button of game.actions) {
   1125     button.dispatchEvent(new Event('pointerdown', {cancelable: true}));
   1126     const click = new Event('click');
   1127     Object.assign(click, {detail: 1});
   1128     button.dispatchEvent(click);
   1129   }
   1130 
   1131   assert.deepEqual(game.keys, [
   1132     [32, true], [32, false], [113, true], [113, false],
   1133     [101, true], [101, false]
   1134   ]);
   1135 });
   1136 
   1137 test('the keyboard moves chat while the game viewport stays unchanged', () => {
   1138   const game = page();
   1139   game.window.visualViewport.height = 180;
   1140 
   1141   game.fire('#chat-button', 'click');
   1142   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1143   game.chatBounds({x: 1, y: 165, width: 320, height: 14, screenWidth: 844, screenHeight: 390});
   1144 
   1145   assert.equal(game.document.activeElement, game.element('#chat-text'));
   1146   assert.equal(game.element('#touch-controls').hidden, true);
   1147   assert.deepEqual(game.sizes, [[844, 390]]);
   1148   assert.equal(game.chatViewports.at(-1), 180);
   1149   assert.equal(game.canvas.clientHeight, 390);
   1150   assert.equal(game.element('#chat-compose').style.left, '1px');
   1151   assert.equal(game.element('#chat-compose').style.top, '165px');
   1152   assert.equal(game.element('#chat-compose').style.width, '320px');
   1153   assert.equal(game.element('#chat-compose').style.height, '14px');
   1154 });
   1155 
   1156 test('closing chat restores its native position without resizing the game', () => {
   1157   const game = page();
   1158   game.fire('#chat-button', 'touchend');
   1159   game.window.visualViewport.height = 180;
   1160   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1161 
   1162   game.fire('#chat-cancel', 'click');
   1163   game.window.visualViewport.height = 390;
   1164   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1165 
   1166   assert.deepEqual(game.sizes, [[844, 390]]);
   1167   assert.equal(game.chatViewports.at(-1), 0);
   1168   assert.equal(game.element('#chat-actions').hidden, true);
   1169   assert.equal(game.element('#chat-button').hidden, false);
   1170   assert.equal(game.document.activeElement, game.canvas);
   1171 });
   1172 
   1173 test('chat geometry scales Retina pixels into CSS pixels', () => {
   1174   const game = page();
   1175   game.element('#pixel-mode').value = 'retina';
   1176   game.fire('#pixel-mode', 'change');
   1177   game.fire('#chat-button', 'click');
   1178   game.window.visualViewport.height = 180;
   1179   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1180 
   1181   game.chatBounds({x: 2, y: 740, width: 640, height: 28, screenWidth: 1688, screenHeight: 780});
   1182 
   1183   assert.equal(game.element('#chat-compose').style.left, '1px');
   1184   assert.equal(game.element('#chat-compose').style.top, '165.5px');
   1185   assert.equal(game.element('#chat-compose').style.width, '320px');
   1186   assert.equal(game.element('#chat-compose').style.height, '14px');
   1187   assert.equal(game.element('#chat-text').style.fontSize, '6px');
   1188   assert.equal(game.chatViewports.at(-1), 360);
   1189 });
   1190 
   1191 test('a short landscape viewport keeps the editor above the keyboard with stale native bounds', () => {
   1192   const game = page();
   1193   game.fire('#chat-button', 'touchend');
   1194   game.element('#chat-text').value = 'Draft';
   1195   game.window.visualViewport.height = 80;
   1196   game.window.visualViewport.offsetTop = 25;
   1197 
   1198   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1199   game.chatBounds({x: 11, y: 200, width: 174, height: 14, screenWidth: 844, screenHeight: 390});
   1200 
   1201   assert.equal(game.element('#chat-compose').style.top, '65px');
   1202   assert.equal(game.element('#chat-compose').style.height, '14px');
   1203   assert.equal(game.element('#game-surface').style.top, '25px');
   1204   assert.equal(game.element('#chat-text').value, 'Draft');
   1205   assert.equal(game.chatViewports.at(-1), 80);
   1206   assert.deepEqual(game.sizes, [[844, 390]]);
   1207 });
   1208 
   1209 test('a clipped native field keeps the editor caret within the phone width', () => {
   1210   const game = page();
   1211   game.canvas.clientWidth = 390;
   1212   game.canvas.clientHeight = 844;
   1213   game.window.visualViewport.width = 390;
   1214   game.window.visualViewport.height = 844;
   1215   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1216 
   1217   game.chatBounds({x: 11, y: 826, width: 760, height: 14, screenWidth: 390, screenHeight: 844});
   1218 
   1219   assert.equal(game.element('#chat-compose').style.left, '11px');
   1220   assert.equal(game.element('#chat-compose').style.width, '379px');
   1221 });
   1222 
   1223 test('a released movement pad stays disengaged while another touch reveals the controls', () => {
   1224   const game = page();
   1225 
   1226   game.fire('#move-pad', 'pointerdown');
   1227   game.fire('#canvas', 'pointerdown', {pointerId: 2});
   1228   game.fire('#canvas', 'pointermove', {pointerId: 2, clientX: 140});
   1229   game.fire('#move-pad', 'pointerup');
   1230 
   1231   assert.equal('active' in game.element('#move-pad').dataset, false);
   1232   assert.equal(game.element('#game-surface').dataset.overlaysActive, '');
   1233 
   1234   game.window.dispatchEvent(new Event('blur'));
   1235 
   1236   assert.deepEqual(game.mouse.at(-1), ['mouseup', 462, 98, 2]);
   1237 });
   1238 
   1239 test('a chat touch focuses the field during the gesture and suppresses the compatibility click', () => {
   1240   const game = page();
   1241 
   1242   const touch = game.fire('#chat-button', 'touchend');
   1243   if (!touch.defaultPrevented) game.fire('#chat-button', 'click');
   1244 
   1245   assert.equal(touch.defaultPrevented, true);
   1246   assert.equal(game.document.activeElement, game.element('#chat-text'));
   1247   assert.equal(game.element('#chat-compose').hidden, false);
   1248   assert.deepEqual(game.keys, [[27, true], [27, false], [13, true], [13, false]]);
   1249 });
   1250 
   1251 test('IME composition cannot cancel or implicitly submit the draft', () => {
   1252   const game = page();
   1253   game.fire('#chat-button', 'touchend');
   1254   game.element('#chat-text').value = 'Hello';
   1255   const escape = new Event('keydown', {cancelable: true});
   1256   Object.assign(escape, {key: 'Escape', isComposing: true, keyCode: 229});
   1257 
   1258   game.fire('#chat-text', 'compositionstart');
   1259   game.window.dispatchEvent(escape);
   1260   game.fire('#chat-compose', 'submit', {submitter: game.element('[form="chat-compose"]')});
   1261   game.fire('#chat-text', 'compositionend');
   1262   const released = new Event('keyup');
   1263   Object.assign(released, {key: 'Escape', keyCode: 27});
   1264   game.window.dispatchEvent(released);
   1265 
   1266   assert.equal(game.element('#chat-compose').hidden, false);
   1267   assert.equal(game.element('#chat-text').value, 'Hello');
   1268   assert.deepEqual(game.characters, []);
   1269   assert.equal(game.document.activeElement, game.element('#chat-text'));
   1270 });
   1271 
   1272 test('the keyboard button refocuses the existing draft without submitting it', () => {
   1273   const game = page();
   1274   game.fire('#chat-button', 'touchend');
   1275   game.element('#chat-text').value = 'Autocompleted draft';
   1276   const focusEvents = [];
   1277   for (const type of ['blur', 'focus'])
   1278     game.element('#chat-text').addEventListener(type, () => focusEvents.push(type));
   1279 
   1280   game.fire('#chat-button', 'touchend');
   1281 
   1282   assert.deepEqual(focusEvents, ['blur', 'focus']);
   1283   assert.equal(game.element('#chat-button').textContent, 'Keyboard');
   1284   assert.equal(game.element('#chat-compose').hidden, false);
   1285   assert.equal(game.element('#chat-text').value, 'Autocompleted draft');
   1286   assert.deepEqual(game.characters, []);
   1287 });
   1288 
   1289 test('Firefox Android chat leaves fullscreen with fixed game dimensions and restores it on cancel', async () => {
   1290   const game = page();
   1291   const surface = game.element('#game-surface');
   1292   const fullscreenChanges = [];
   1293   game.document.fullscreenElement = surface;
   1294   game.document.exitFullscreen = async () => {
   1295     fullscreenChanges.push('exit');
   1296     game.document.fullscreenElement = null;
   1297     game.window.visualViewport.height = 330;
   1298     game.document.dispatchEvent(new Event('fullscreenchange'));
   1299   };
   1300   surface.requestFullscreen = async () => {
   1301     fullscreenChanges.push('enter');
   1302     game.document.fullscreenElement = surface;
   1303     game.window.visualViewport.height = 390;
   1304     game.document.dispatchEvent(new Event('fullscreenchange'));
   1305   };
   1306 
   1307   game.fire('#chat-button', 'touchend');
   1308   await Promise.resolve();
   1309 
   1310   assert.deepEqual(fullscreenChanges, ['exit']);
   1311   assert.equal(game.document.activeElement, game.element('#chat-text'));
   1312   assert.equal(surface.style.width, '844px');
   1313   assert.equal(surface.style.height, '390px');
   1314   assert.equal(game.element('#chat-compose').style.top, '315px');
   1315 
   1316   game.fire('#chat-cancel', 'click');
   1317   await Promise.resolve();
   1318   await Promise.resolve();
   1319 
   1320   assert.deepEqual(fullscreenChanges, ['exit', 'enter']);
   1321   assert.equal(game.document.fullscreenElement, surface);
   1322   assert.equal(surface.style.width, '');
   1323   assert.equal(surface.style.height, '');
   1324   assert.equal(game.element('#chat-button').textContent, 'Chat');
   1325   assert.equal(game.sizes.every(size => size[0] === 844 && size[1] === 390), true);
   1326 });
   1327 
   1328 test('unsupported chat characters stay editable and are not sent', () => {
   1329   const game = page();
   1330   game.fire('#chat-button', 'click');
   1331   game.element('#chat-text').value = '\ud83d\ude00';
   1332 
   1333   game.fire('#chat-compose', 'submit');
   1334 
   1335   assert.deepEqual(game.characters, []);
   1336   assert.equal(game.element('#chat-compose').hidden, false);
   1337   assert.match(game.element('#chat-text').validity, /cannot display/);
   1338 });
   1339 
   1340 test('mobile options toggle the native view and return focus to the game', () => {
   1341   const game = page();
   1342   game.fire('#menu-button', 'click');
   1343   assert.equal(game.element('#desktop-controls').hidden, true);
   1344   assert.equal(game.element('#mobile-options').hidden, false);
   1345   assert.equal(game.element('#menu-button').title, 'Options');
   1346 
   1347   game.fire('#view-button', 'click');
   1348 
   1349   assert.deepEqual(game.keys, [[1073741889, true], [1073741889, false]]);
   1350   assert.equal(game.element('#player-menu').open, false);
   1351   assert.equal(game.document.activeElement, game.canvas);
   1352 });
   1353 
   1354 test('desktop resolution settings remain available without enabling mobile overlays', () => {
   1355   const game = page({navigator: {userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', maxTouchPoints: 10}, saved: 'true'});
   1356   assert.equal(game.element('#touch-controls').hidden, true);
   1357 
   1358   const shortcut = new Event('keydown', {cancelable: true});
   1359   Object.assign(shortcut, {ctrlKey: true, altKey: true, code: 'KeyM'});
   1360   game.window.dispatchEvent(shortcut);
   1361   game.element('#pixel-mode').value = 'retina';
   1362   game.fire('#pixel-mode', 'change');
   1363   game.fire('#view-button', 'click');
   1364   game.element('#player-menu').close();
   1365 
   1366   assert.equal(game.element('#touch-controls').hidden, true);
   1367   assert.equal(game.element('#game-toolbar').hidden, false);
   1368   assert.equal(game.element('#mobile-options').hidden, true);
   1369   assert.equal(game.element('#desktop-controls').hidden, false);
   1370   assert.deepEqual(game.keys, []);
   1371   assert.equal(game.element('#touch-enabled').checked, false);
   1372   assert.equal(game.element('#touch-enabled').disabled, true);
   1373   assert.equal(game.storage.get('ao-touch-controls'), 'true');
   1374   assert.equal(game.storage.get('ao-pixel-mode'), 'retina');
   1375   assert.deepEqual(game.sizes.at(-1), [1688, 780]);
   1376   assert.deepEqual(game.hudInsets, [[0, 0], [0, 0], [0, 0]]);
   1377   assert.equal(page({saved: 'false'}).element('#touch-controls').hidden, true);
   1378 });
   1379 
   1380 test('desktop touch input and saved preferences cannot activate mobile controls', () => {
   1381   const game = page({navigator: {userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', maxTouchPoints: 10}, saved: 'true'});
   1382   game.element('#touch-enabled').checked = true;
   1383 
   1384   game.fire('#touch-enabled', 'change');
   1385   const touch = game.fire('#canvas', 'touchstart');
   1386   const pointer = game.fire('#canvas', 'pointerdown');
   1387   game.fire('#canvas', 'pointermove', {clientX: 200});
   1388   game.fire('#canvas', 'pointerup');
   1389   game.fire('#chat-button', 'click');
   1390 
   1391   assert.equal(game.element('#game-toolbar').hidden, false);
   1392   assert.equal(game.element('#touch-controls').hidden, true);
   1393   assert.equal(game.element('#chat-compose').hidden, true);
   1394   assert.equal(touch.defaultPrevented, false);
   1395   assert.equal(pointer.defaultPrevented, false);
   1396   assert.deepEqual(game.keys, []);
   1397   assert.deepEqual(game.mouse, []);
   1398 });
   1399 
   1400 test('desktop viewport changes cannot request chat relocation or shortcut hiding', () => {
   1401   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64) Gecko Firefox/149.0'}});
   1402   game.element('#chat-compose').hidden = false;
   1403   game.window.visualViewport.height = 80;
   1404   game.window.visualViewport.offsetTop = 25;
   1405 
   1406   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1407   game.window.visualViewport.dispatchEvent(new Event('scroll'));
   1408   game.document.dispatchEvent(new Event('fullscreenchange'));
   1409 
   1410   assert.deepEqual(game.chatViewports, [0, 0, 0]);
   1411   assert.equal(game.element('#game-surface').style.top, undefined);
   1412 });
   1413 
   1414 test('an ended session shows loading and starts one new session', () => {
   1415   const game = page();
   1416 
   1417   game.closed();
   1418   game.closed();
   1419   game.firstFrame();
   1420 
   1421   assert.equal(game.element('#loading').hidden, false);
   1422   assert.equal(game.element('#loading').textContent, 'Starting a new session...');
   1423   assert.equal(game.element('#touch-controls').hidden, true);
   1424   game.hold();
   1425   assert.deepEqual(game.reloads, [true]);
   1426 });
   1427 
   1428 test('a session that ends in the background starts anew when the tab returns', () => {
   1429   const game = page();
   1430   game.document.hidden = true;
   1431 
   1432   game.closed();
   1433   game.hold();
   1434   assert.deepEqual(game.reloads, []);
   1435   game.document.hidden = false;
   1436   game.document.dispatchEvent(new Event('visibilitychange'));
   1437 
   1438   assert.equal(game.element('#loading').hidden, false);
   1439   assert.deepEqual(game.reloads, [true]);
   1440 });
   1441 
   1442 for (const hidden of [false, true]) {
   1443   test(`a takeover waits for the reconnect button with a ${hidden ? 'hidden' : 'visible'} tab`, () => {
   1444     const game = page();
   1445     game.document.hidden = hidden;
   1446 
   1447     game.closed(true);
   1448     game.closed();
   1449     game.firstFrame();
   1450     game.graphics(false);
   1451     game.hold();
   1452     game.document.hidden = false;
   1453     game.document.dispatchEvent(new Event('visibilitychange'));
   1454     game.window.dispatchEvent(new Event('pageshow'));
   1455 
   1456     assert.equal(game.element('#loading').textContent, 'This character connected elsewhere.');
   1457     assert.equal(game.element('#loading').hidden, false);
   1458     assert.equal(game.element('#reconnect').hidden, false);
   1459     assert.equal(game.element('#touch-controls').hidden, true);
   1460     assert.deepEqual(game.reloads, []);
   1461     game.fire('#reconnect', 'click');
   1462     assert.deepEqual(game.reloads, [true]);
   1463   });
   1464 }
   1465 
   1466 test('a moderator disconnect leaves a reconnect button and explains who ended the session', () => {
   1467   const game = page();
   1468 
   1469   game.closed('moderated');
   1470   game.closed();
   1471   game.hold();
   1472 
   1473   assert.equal(game.element('#loading').textContent, 'An administrator ended this session.');
   1474   assert.equal(game.element('#reconnect').hidden, false);
   1475   assert.deepEqual(game.reloads, []);
   1476 });
   1477 
   1478 test('a takeover prevents a restart already scheduled by the other connection', () => {
   1479   const game = page();
   1480   game.closed();
   1481 
   1482   game.closed(true);
   1483   game.hold();
   1484 
   1485   assert.equal(game.element('#loading').textContent, 'This character connected elsewhere.');
   1486   assert.equal(game.element('#reconnect').hidden, false);
   1487   assert.deepEqual(game.reloads, []);
   1488 });
   1489 
   1490 test('graphics recovery shows progress and preserves the current session', () => {
   1491   const game = page();
   1492 
   1493   game.graphics(true);
   1494   assert.equal(game.element('#loading').hidden, false);
   1495   assert.equal(game.element('#loading').textContent, 'Restoring graphics...');
   1496   game.graphics(false);
   1497 
   1498   assert.equal(game.element('#loading').hidden, true);
   1499   assert.deepEqual(game.reloads, []);
   1500 });
   1501 
   1502 test('graphics recovery during the logo retains the loading screen', () => {
   1503   const game = page({ready: false});
   1504 
   1505   game.graphics(true);
   1506   game.graphics(false);
   1507 
   1508   assert.equal(game.element('#loading').hidden, false);
   1509   assert.equal(game.element('#loading').textContent, 'Loading...');
   1510   assert.deepEqual(game.reloads, []);
   1511 });
   1512 
   1513 for (const event of ['visibilitychange', 'pageshow', 'resume']) {
   1514   test(`${event} restores layout and interrupted audio in the existing session`, () => {
   1515     const game = page();
   1516     game.audio.state = 'interrupted';
   1517     game.canvas.clientWidth = 900;
   1518     game.canvas.clientHeight = 500;
   1519 
   1520     (event === 'pageshow' ? game.window : game.document).dispatchEvent(new Event(event));
   1521 
   1522     assert.deepEqual(game.sizes, [[844, 390], [900, 500]]);
   1523     assert.equal(game.audio.resumes, 1);
   1524     assert.equal(game.audio.state, 'running');
   1525     assert.equal(game.element('#loading').hidden, true);
   1526   });
   1527 }
   1528 
   1529 test('returning during the loading logo resumes without declaring the world ready', () => {
   1530   const game = page({ready: false});
   1531   game.audio.state = 'suspended';
   1532   game.document.hidden = true;
   1533 
   1534   game.document.dispatchEvent(new Event('visibilitychange'));
   1535   game.document.hidden = false;
   1536   game.document.dispatchEvent(new Event('visibilitychange'));
   1537 
   1538   assert.equal(game.audio.resumes, 1);
   1539   assert.equal(game.element('#loading').hidden, false);
   1540   assert.equal(game.element('#touch-controls').hidden, true);
   1541   assert.equal(game.element('#game-toolbar').hidden, true);
   1542 });
   1543 
   1544 test('a hidden zero-sized canvas cannot replace the game resolution', () => {
   1545   const game = page();
   1546   game.canvas.clientWidth = 0;
   1547   game.canvas.clientHeight = 0;
   1548   game.document.hidden = true;
   1549   game.audio.state = 'interrupted';
   1550 
   1551   game.window.dispatchEvent(new Event('resize'));
   1552   game.window.visualViewport.dispatchEvent(new Event('resize'));
   1553   game.window.dispatchEvent(new Event('pageshow'));
   1554 
   1555   assert.deepEqual(game.sizes, [[844, 390]]);
   1556   assert.equal(game.audio.resumes, 0);
   1557   assert.ok(game.chatViewports.every(Number.isFinite));
   1558 });
   1559 
   1560 for (const event of ['pagehide', 'freeze']) {
   1561   test(`${event} releases held controls before the page is suspended`, () => {
   1562     const game = page();
   1563     game.fire('#move-pad', 'pointerdown');
   1564     game.fire('#move-pad', 'pointermove', {clientX: 100, clientY: 50});
   1565 
   1566     (event === 'pagehide' ? game.window : game.document).dispatchEvent(new Event(event));
   1567 
   1568     assert.equal(game.element('#move-pad').dataset.active, undefined);
   1569     assert.deepEqual(game.keys.at(-1), [119, false]);
   1570   });
   1571 }
   1572 
   1573 test('iPhone and iPadOS browsers enable the mobile controls', () => {
   1574   const phone = page({navigator: {userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)'}});
   1575   const tablet = page({navigator: {userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15)', platform: 'MacIntel', maxTouchPoints: 5}});
   1576   const desktop = page({navigator: {userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15)', platform: 'MacIntel', maxTouchPoints: 0}});
   1577 
   1578   phone.fire('#canvas', 'pointerdown');
   1579   tablet.fire('#canvas', 'pointerdown');
   1580   desktop.fire('#canvas', 'pointerdown');
   1581 
   1582   assert.equal(phone.element('#game-toolbar').hidden, false);
   1583   assert.deepEqual(phone.cursorHidden, [true]);
   1584   assert.equal(phone.element('#touch-controls').hidden, false);
   1585   assert.equal(tablet.element('#game-toolbar').hidden, false);
   1586   assert.deepEqual(tablet.cursorHidden, [true]);
   1587   assert.equal(tablet.element('#touch-controls').hidden, false);
   1588   assert.equal(desktop.element('#game-toolbar').hidden, false);
   1589   assert.deepEqual(desktop.cursorHidden, [false]);
   1590   assert.equal(desktop.element('#touch-controls').hidden, true);
   1591 });
   1592 
   1593 test('desktop Options follows native toolbar coordinates and the 150 CSS pixel reveal distance', () => {
   1594   const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}});
   1595   game.element('#menu-button').getBoundingClientRect = () => ({left: 608, top: 0, right: 644, bottom: 24});
   1596   game.touchRegions({screenWidth: 1688, screenHeight: 780, regions: [
   1597     {left: 1300, top: 0, right: 1688, bottom: 32},
   1598     {left: 0, top: 0, right: 1688, bottom: 780, world: true}
   1599   ]});
   1600   const toolbar = game.element('#game-toolbar');
   1601 
   1602   game.fire('#canvas', 'pointermove', {pointerType: 'mouse', clientX: 457, clientY: 12});
   1603 
   1604   assert.equal(toolbar.style.right, '200px');
   1605   assert.equal(toolbar.dataset.desktop, '');
   1606   assert.equal(toolbar.dataset.menuVisible, undefined);
   1607   assert.equal(game.element('#chat-button').hidden, true);
   1608   assert.equal(game.element('#touch-controls').hidden, true);
   1609 
   1610   game.fire('#canvas', 'pointermove', {pointerType: 'mouse', clientX: 458, clientY: 12});
   1611 
   1612   assert.equal(toolbar.dataset.menuVisible, '');
   1613 
   1614   game.fire('#canvas', 'pointermove', {pointerType: 'mouse', clientX: 499, clientY: 133});
   1615 
   1616   assert.equal(toolbar.dataset.menuVisible, undefined);
   1617 
   1618   game.fire('#menu-button', 'click');
   1619   game.fire('#canvas', 'pointermove', {pointerType: 'mouse', clientX: 0, clientY: 390});
   1620 
   1621   assert.equal(game.element('#player-menu').open, true);
   1622   assert.equal(toolbar.dataset.menuVisible, '');
   1623 
   1624   game.element('#player-menu').close();
   1625 
   1626   assert.equal(toolbar.dataset.menuVisible, undefined);
   1627   assert.equal(game.document.activeElement, game.canvas);
   1628 });
   1629 
   1630 for (const width of [1600, 320]) {
   1631   test(`desktop Options stays beside the current toolbar after resizing to ${width}`, () => {
   1632     const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}});
   1633     const toolbar = game.element('#game-toolbar');
   1634     game.element('#menu-button').getBoundingClientRect = () => ({
   1635       left: width - parseFloat(toolbar.style.right) - 34, right: width - parseFloat(toolbar.style.right), top: 0, bottom: 24});
   1636     game.touchRegions({screenWidth: 1280, screenHeight: 900, regions: [
   1637       {left: 1088, top: 0, right: 1280, bottom: 20},
   1638       {left: 0, top: 0, right: 1280, bottom: 880, world: true}
   1639     ]});
   1640 
   1641     game.canvas.clientWidth = width;
   1642     game.window.dispatchEvent(new Event('resize'));
   1643     game.touchRegions({screenWidth: width, screenHeight: 480, regions: [
   1644       {left: 1088, top: 0, right: 1280, bottom: 20},
   1645       {left: width - 192, top: 0, right: width, bottom: 20},
   1646       {left: 0, top: 0, right: width, bottom: 460, world: true}
   1647     ]});
   1648     game.fire('#canvas', 'pointermove', {pointerType: 'mouse', clientX: width - 210, clientY: 12});
   1649 
   1650     assert.equal(toolbar.style.right, '198px');
   1651     assert.equal(toolbar.hidden, false);
   1652     assert.equal(toolbar.dataset.menuVisible, '');
   1653     game.fire('#menu-button', 'click');
   1654     assert.equal(game.element('#player-menu').open, true);
   1655   });
   1656 }
   1657 
   1658 for (const password of ['', 'abcd', '\u00e9\u00e9\u00e9\u00e9']) {
   1659   test(`short creation password is refused: ${JSON.stringify(password)}`, async () => {
   1660     const game = page();
   1661     game.creationState({active: true, name: {value: ''}});
   1662     game.element('#creation-password').value = password;
   1663     game.element('#creation-password-confirm').value = password;
   1664 
   1665     await game.element('#creation-form').onsubmit({preventDefault() {}});
   1666 
   1667     assert.equal(game.requests.length, 0);
   1668     assert.deepEqual(game.creationNames, []);
   1669     assert.equal(game.element('#creation-credentials').open, true);
   1670     assert.equal(game.element('#creation-error').textContent, 'Use a password of at least 5 characters.');
   1671   });
   1672 }