const test = require('node:test'); const assert = require('node:assert/strict'); const { load } = require('./harness'); const start = "ACT.start({});S.nextFail=1e9;S.nextEvent=1e9;S.cash=1e9"; const keyer = dom => (k, t, o) => (t || dom.window.document.body).dispatchEvent(new dom.window.KeyboardEvent('keydown', { key: k, bubbles: true, ...o })); test('[ and ] step through your POPs in name order, wrap around, and move the map there', () => { const { ev, dom, errors } = load(); const key = keyer(dom); ev(start + ";for(const c of ['par','lon','ams'])ACT.buildPop({city:c})"); assert.equal(ev("popOrder().join()"), 'ams,fra,lon,par', 'Amsterdam, Frankfurt, London, Paris'); const sel = () => ev('UI.sel&&UI.sel.type==="city"?UI.sel.id:null'); assert.equal(sel(), 'fra', 'the game starts on the home POP'); key(']'); assert.equal(sel(), 'lon'); assert.equal(ev('cam.x'), ev('CITY.lon.x'), 'with the map on it'); key(']'); assert.equal(sel(), 'par'); key(']'); assert.equal(sel(), 'ams', '] wraps to the first'); key('['); assert.equal(sel(), 'par', '[ wraps to the last'); key('['); assert.equal(sel(), 'lon'); // Nothing selected, or a city without a POP: ] starts at the first POP, [ at the last. ev('UI.sel=null'); key(']'); assert.equal(sel(), 'ams'); ev('UI.sel=null'); key('['); assert.equal(sel(), 'par'); ev("selectCity('tok')"); key(']'); assert.equal(sel(), 'ams'); // The NOC dashboard steps aside so the POP can be seen. ev("ACT.dash()"); assert.equal(ev("$('dash').hidden"), false); key(']'); assert.equal(sel(), 'fra'); assert.equal(ev("$('dash').hidden"), true); // Not while typing, not with Ctrl or Cmd, not behind an open window. key(']', null, { ctrlKey: true }); key(']', null, { metaKey: true }); assert.equal(sel(), 'fra'); key(']', null, { altKey: true }); assert.equal(sel(), 'lon', 'Option or AltGr types [ ] on many layouts'); ev("ACT.rename()"); key(']', dom.window.document.getElementById('rnIn')); key(']'); assert.equal(sel(), 'lon'); assert.equal(ev("$('modal').hidden"), false); assert.deepEqual(errors, []); }); test('- and = zoom the map out and in around its middle, within the zoom limits', () => { const { ev, dom, errors } = load(); const key = keyer(dom); ev(start + ';cam.z=1'); const x = ev('cam.x'), z = () => ev('cam.z'); key('='); assert.ok(Math.abs(z() - 1.25) < 1e-9); key('+'); assert.ok(Math.abs(z() - 1.5625) < 1e-9, '+ zooms in too'); key('-'); key('_'); assert.ok(Math.abs(z() - 1) < 1e-9, '- and _ zoom out'); assert.ok(Math.abs(ev('cam.x') - x) < 1e-6, 'the middle of the map stays put'); ev('cam.z=ZMAX'); key('='); assert.equal(z(), ev('ZMAX')); ev('cam.z=ZMIN'); key('-'); assert.equal(z(), ev('ZMIN')); ev("cam.z=1;ACT.rename()"); key('=', dom.window.document.getElementById('rnIn')); key('='); assert.equal(z(), 1, 'not while a window is open'); assert.deepEqual(errors, []); }); test('n opens the NOC dashboard, and n followed by a letter opens one of its tabs', () => { const { ev, dom, errors } = load(); const key = keyer(dom), open = () => !ev("$('dash').hidden"), tab = () => ev("UI.dtab||'overview'"); ev(start); key('n'); assert.ok(open(), 'n opens it'); assert.equal(tab(), 'overview'); key('c'); assert.equal(tab(), 'conn', 'n c: Connections'); key('n'); assert.ok(open(), 'n does not close it, so n c can switch tabs without a flash'); key('u'); assert.equal(tab(), 'cust'); for (const [l, t] of [['o', 'overview'], ['t', 'traffic'], ['r', 'infra'], ['l', 'lease'], ['c', 'conn']]) { key('n'); key(l); assert.equal(tab(), t, 'n ' + l); } key('o'); assert.equal(tab(), 'conn', 'a letter on its own does nothing'); key('n'); key('Shift'); key('T'); assert.equal(tab(), 'traffic', 'Shift on the way does not cancel it'); key('n'); ev('keyLead=Date.now()-KEY_LEAD-1'); key('c'); assert.equal(tab(), 'traffic', 'too late: the letter stands alone'); key('n'); key('x'); key('c'); assert.equal(tab(), 'traffic', 'another key in between ends the wait'); key('Escape'); assert.ok(!open(), 'Escape closes it'); key('n'); key('c', null, { ctrlKey: true }); assert.equal(tab(), 'traffic', 'not with Ctrl'); ev("$('dash').hidden=true;ACT.rename()"); key('n', dom.window.document.getElementById('rnIn')); key('n'); assert.ok(!open(), 'not while typing or behind a window'); assert.deepEqual(errors, []); }); test('? shows every keyboard shortcut and closes again; Help links to it on a computer, not on a phone', () => { const { ev, dom, errors } = load(); const doc = dom.window.document, key = keyer(dom); ev(start); key('?'); assert.equal(ev("$('modal').className"), 'keys', '? opens the list'); const txt = doc.getElementById('modalCard').textContent; for (const s of ['Keyboard shortcuts', 'Space', 'Pause or play', 'Find a city', 'Previous or next of your POPs', 'Zoom the map out or in', 'NOC dashboard', 'Connections', 'Leased lines', 'Esc', 'Show this list']) assert.ok(txt.includes(s), s); for (const k of ['[', ']', '-', '=', '/', '?', '0', '4', 'n', 'o', 'u', 'c', 't', 'r', 'l']) assert.ok([...doc.querySelectorAll('#modalCard .kbd')].some(e => e.textContent === k), k); key('?', null, { repeat: true }); assert.equal(ev("$('modal').hidden"), false, 'holding ? does not flicker it'); key('?'); assert.equal(ev("$('modal').hidden"), true, '? again closes it'); key('?'); key('Escape'); assert.equal(ev("$('modal').hidden"), true, 'so does Escape'); // Another window stays as it is, and ? in a text field is just a question mark. ev("ACT.findOpen()"); key('?'); assert.equal(ev("$('modal').className"), 'find'); key('?', doc.getElementById('findIn')); assert.equal(ev("$('modal').className"), 'find'); ev('closeModal();ACT.help()'); const link = doc.querySelector('#modalCard [data-act="keys"]'); assert.ok(link, 'Help has a Keyboard shortcuts button'); link.click(); assert.equal(ev("$('modal').className"), 'keys'); ev("closeModal();window.innerWidth=360;ACT.help()"); assert.equal(doc.querySelector('#modalCard [data-act="keys"]'), null, 'not on a phone, which has no keys'); assert.deepEqual(errors, []); }); test('the shortcut list in German', () => { const { ev, dom, errors } = load({ lang: 'de' }); ev(start + ';if(ACT.pitchSkip)ACT.pitchSkip()'); keyer(dom)('?'); const txt = dom.window.document.getElementById('modalCard').textContent; assert.match(txt, /Tastenkürzel/); assert.match(txt, /Leertaste/); assert.deepEqual(errors, []); });