'use strict'; /* * Session / lifecycle details of the game screen that the co-op work fixed (every block fails on the old behaviour): * code-17 no co-op owner flags or join toasts left over after leaving a room for a solo game * code-18 the Reset-zoom button follows the camera however the camera was reset * code-20 the screen wake lock is released when the game ends and the player is back at the menu * code-21 Help and the Menu do not un-pause a game the player paused themselves * code-25 winning persists the best wave * code-26 first-run help once per install, and only once per page load when storage throws */ const { createServer } = require('../../server'); const assert = (c, m) => { if (!c) throw new Error('ASSERT: ' + m); console.log('ok -', m); }; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); async function until(fn, ms, what) { const t0 = Date.now(); for (;;) { try { const v = await fn(); if (v) return v; } catch (e) { /* retry */ } if (Date.now() - t0 > ms) throw new Error('TIMEOUT: ' + what); await sleep(40); } } module.exports = async function ({ browser, url: URL }) { const errors = []; const mk = async (name, init, opts) => { const ctx = await browser.newContext(opts || { viewport: { width: 1100, height: 700 } }); await ctx.addInitScript(init || (() => { try { localStorage.setItem('nw.helped', 'true'); } catch (e) { /* ignore */ } })); const page = await ctx.newPage(); page.on('pageerror', (e) => errors.push('[' + name + '] pageerror: ' + e.message)); page.on('console', (m) => { if (m.type() === 'error' && !/WebSocket|ERR_|Failed to load/.test(m.text())) errors.push('[' + name + '] console.error: ' + m.text()); }); return { ctx, page }; }; const paused = (P) => P.page.evaluate(() => NW.app.ctrl.view.paused); const flush = (P) => P.page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))); // two frames: the main loop has run // ---- code-21 pause, code-18 fit, code-25 best, code-20 wake lock (one solo session with a counting wake lock) const wl = () => { try { localStorage.setItem('nw.helped', 'true'); } catch (e) { /* ignore */ } window.__wl = { req: 0, active: 0 }; Object.defineProperty(navigator, 'wakeLock', { configurable: true, value: { request: async () => { window.__wl.req++; window.__wl.active++; let done = false; return { release: async () => { if (!done) { done = true; window.__wl.active--; } }, addEventListener() {} }; } } }); }; const S = await mk('solo', wl); try { const p = S.page; await p.goto(URL); await sleep(300); await p.click('#btnSolo'); await sleep(300); assert(await p.evaluate(() => window.__wl.active) === 1, 'code-20: the screen is held awake while playing'); await p.keyboard.press('p'); await flush(S); assert(await paused(S), 'P pauses'); await p.keyboard.press('h'); await sleep(100); await p.click('#btnHelpClose'); await flush(S); assert(await paused(S), 'code-21: closing Help leaves a game the player paused paused'); await p.click('#btnMenu'); await sleep(100); await p.click('#btnCloseMenu'); await flush(S); assert(await paused(S), 'code-21: so does opening and closing the Menu'); await p.keyboard.press('p'); await flush(S); assert(!(await paused(S)), 'P resumes'); await p.click('#btnMenu'); await flush(S); assert(await paused(S), 'the Menu pauses a running solo game'); await p.click('#btnCloseMenu'); await flush(S); assert(!(await paused(S)), 'and closing it resumes what it paused'); // code-18: the Reset-zoom button follows the camera const box = await p.locator('#board').boundingBox(); await p.mouse.move(box.x + 300, box.y + 200); await p.mouse.wheel(0, -400); await sleep(200); assert(await p.evaluate(() => !document.getElementById('btnFit').hidden && NW.app.renderer.zoom > 1.02), 'code-18: zooming in shows Reset zoom'); await p.evaluate(() => NW.app.renderer.resetCamera()); // any code path that resets the camera (a new board, a rotation, ...) await flush(S); assert(await p.evaluate(() => document.getElementById('btnFit').hidden), 'code-18: the button is gone as soon as the camera is back at 1x'); // code-25: a win is a best-wave record too assert(await p.evaluate(() => localStorage.getItem('nw.best.open')) === null, 'no record yet'); await p.evaluate(() => { const c = NW.app.ctrl; c.sim.winWave = 1; c.send({ c: 'wave' }); c.sim.spawnQ.length = 0; c.sim.monsters.length = 0; }); await until(() => p.evaluate(() => !document.getElementById('end').hidden), 5000, 'the end screen after winning'); assert(await p.evaluate(() => document.getElementById('endTitle').textContent) === 'Server secured!', 'it is a win'); assert(await p.evaluate(() => localStorage.getItem('nw.best.open')) === '1', 'code-25: the win stored the best wave'); // code-20: back at the menu the lock is released await p.click('#btnEndMenu'); await sleep(300); assert(await p.evaluate(() => !NW.app.inGame && window.__wl.active) === 0, 'code-20: the wake lock is released at the menu (' + await p.evaluate(() => JSON.stringify(window.__wl)) + ')'); } finally { await S.ctx.close(); } // ---- code-26: storage that throws (some private modes): the first-run help shows once per page load, not on every solo start const T = await mk('throwing', () => { const boom = () => { throw new Error('denied'); }; for (const k of ['getItem', 'setItem', 'removeItem']) { try { Storage.prototype[k] = boom; } catch (e) { /* ignore */ } } }); try { const p = T.page; await p.goto(URL); await sleep(300); await p.click('#btnSolo'); await sleep(300); assert(await p.evaluate(() => !document.getElementById('help').hidden), 'code-26: the first solo start shows the help'); await p.click('#btnHelpClose'); await p.click('#btnMenu'); await p.click('#btnSolo'); await sleep(300); // "New solo game" assert(await p.evaluate(() => document.getElementById('help').hidden), 'code-26: the second start does not show it again'); } finally { await T.ctx.close(); } // ---- code-17: leaving a co-op room for a solo game leaves no co-op flags behind (needs a server and a partner) const srv = await createServer({ port: 0, host: '127.0.0.1', log: () => {}, pingMs: 0 }); const base = 'http://127.0.0.1:' + srv.port + '/'; const A = await mk('A'), B = await mk('B'); try { await A.page.goto(base); await sleep(250); await A.page.click('#btnCoop'); await A.page.fill('#inName', 'Alice'); await A.page.click('#btnCreate'); await until(() => A.page.evaluate(() => NW.app.ctrl.kind === 'remote' && NW.app.ctrl.status === 'joined'), 5000, 'A joined'); const room = await A.page.evaluate(() => NW.app.ctrl.room); await A.page.evaluate(() => { const r = NW.app.renderer, f = r.frame.bind(r); r.frame = (now, view, ui) => { window.__ui = ui; return f(now, view, ui); }; }); await B.page.goto(base + '?room=' + room); await sleep(250); await B.page.fill('#inName', 'Bob'); await B.page.click('#btnJoin'); await until(() => A.page.evaluate(() => window.__ui && window.__ui.coop === true && window.__ui.names[1] === 'Bob'), 4000, 'A draws the co-op flags with two players'); await A.page.click('#btnMenu'); await sleep(150); await A.page.evaluate(() => document.querySelector('#players [data-act="leave"]').click()); await sleep(600); await A.page.click('#btnSolo'); await sleep(500); const ui = await A.page.evaluate(() => ({ coop: window.__ui.coop, names: Object.keys(window.__ui.names).length, kind: NW.app.ctrl.kind })); assert(ui.kind === 'local' && ui.coop === false && ui.names === 0, 'code-17: the solo game does not draw co-op owner flags'); } finally { await A.ctx.close(); await B.ctx.close(); await srv.close(); } if (errors.length) throw new Error('console errors:\n' + errors.join('\n')); console.log('no console errors'); };