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) {} if (Date.now() - t0 > ms) throw new Error('TIMEOUT: ' + what); await sleep(50); } } module.exports = async function ({ browser, url: URL, out: OUT }) { const errors = []; const mk = async (name, opts) => { const ctx = await browser.newContext(opts); const page = await ctx.newPage(); page.on('pageerror', e => errors.push(`[${name}] pageerror: ${e.message}`)); page.on('console', m => { if (m.type() === 'error') errors.push(`[${name}] console.error: ${m.text()}`); }); return { ctx, page, name }; }; const A = await mk('host-desktop', { viewport: { width: 1280, height: 800 } }); const B = await mk('guest-phone', { viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true }); // host creates a room await A.page.goto(URL); await sleep(300); 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, 'host joined'); const room = await A.page.evaluate(() => NW.app.ctrl.room); assert(/^[A-Z0-9]{4}$/.test(room), 'room code ' + room); // guest opens the invite link await B.page.goto(URL + '?room=' + room); await sleep(300); assert(await B.page.inputValue('#inCode') === room, 'invite link prefilled the code'); await B.page.fill('#inName', 'Bob'); await B.page.tap('#btnJoin'); await until(() => B.page.evaluate(() => NW.app.ctrl.kind === 'remote' && NW.app.ctrl.status === 'joined'), 5000, 'guest joined'); await until(() => A.page.evaluate(() => NW.app.ctrl.players.length === 2), 3000, 'host sees 2 players'); const slots = await Promise.all([A, B].map(p => p.page.evaluate(() => NW.app.ctrl.you.slot))); assert(slots[0] === 0 && slots[1] === 1, 'slots 0 and 1: ' + slots); assert((await B.page.evaluate(() => NW.app.renderer.rot)) > 0, 'guest phone portrait board rotated'); const boxA = await A.page.locator('#board').boundingBox(), boxB = await B.page.locator('#board').boundingBox(); const cellPt = async (p, box, cx, cy) => { const r = await p.page.evaluate(([a, b]) => NW.app.renderer.cellToScreen(a, b), [cx, cy]); return { x: box.x + r.x, y: box.y + r.y }; }; // host builds walls, guest sees them await A.page.click('.tbtn[data-type="wall"]'); for (let y = 3; y <= 9; y++) { const p = await cellPt(A, boxA, 12, y); await A.page.mouse.click(p.x, p.y); } await until(() => B.page.evaluate(() => NW.app.ctrl.view.towers.filter(t => t.type === 'wall').length === 7), 3000, 'guest sees 7 walls'); // both mirrored boards must converge on the same maze (each applies server changes with its own display delay) await until(async () => { const [ra, rb] = await Promise.all([A, B].map((p) => p.page.evaluate(() => NW.app.ctrl.board.routeLen))); return Math.abs(ra - rb) < 1e-6 && ra > 26; }, 3000, 'mirrored boards agree on the route length'); console.log('ok - mirrored boards agree on the route length'); // guest builds a turret (tap, tap), host sees it owned by slot 1 await B.page.tap('.tbtn[data-type="filter"]'); const gp = await cellPt(B, boxB, 11, 6); await B.page.touchscreen.tap(gp.x, gp.y); await sleep(150); await B.page.touchscreen.tap(gp.x, gp.y); await until(() => A.page.evaluate(() => NW.app.ctrl.view.towers.some(t => t.type === 'filter' && t.owner === 1)), 3000, 'host sees guest turret with owner 1'); await until(async () => (await A.page.evaluate(() => NW.app.ctrl.view.gold)) === 80 && (await B.page.evaluate(() => NW.app.ctrl.view.gold)) === 80, 3000, 'shared credits 140-35-25=80 on both screens'); console.log('ok - shared credit pool'); // cursors and pings const mp = await cellPt(A, boxA, 6, 6); await A.page.mouse.move(mp.x, mp.y); await A.page.mouse.move(mp.x + 5, mp.y + 5); await until(() => B.page.evaluate(() => NW.app.ctrl.view.cursors.some(c => c.slot === 0)), 3000, 'guest sees host cursor'); await A.page.keyboard.down('Alt'); await A.page.mouse.click(mp.x, mp.y); await A.page.keyboard.up('Alt'); await until(() => B.page.evaluate(() => NW.app.renderer.marks.length > 0), 3000, 'guest sees host ping'); // either player starts the wave; both see monsters; kills are shared await B.page.tap('#btnWave'); await until(() => A.page.evaluate(() => NW.app.ctrl.view.monsters.length > 0), 4000, 'host sees monsters'); await sleep(2500); await A.page.screenshot({ path: OUT + 'coop-host.png' }); await B.page.screenshot({ path: OUT + 'coop-guest.png' }); const mA = await A.page.evaluate(() => NW.app.ctrl.view.monsters.length), mB = await B.page.evaluate(() => NW.app.ctrl.view.monsters.length); assert(Math.abs(mA - mB) <= 2 && mA > 0, `both see the same monsters (${mA} vs ${mB})`); // pause is shared; speed is shared await A.page.click('#btnPause'); await until(() => B.page.evaluate(() => NW.app.ctrl.view.paused === true), 3000, 'guest sees pause'); await B.page.tap('#btnPause'); await until(() => A.page.evaluate(() => NW.app.ctrl.view.paused === false), 3000, 'host sees resume'); const rtt = await A.page.evaluate(() => NW.app.ctrl.rtt); console.log('rtt ms', rtt); // guest's connection drops: auto reconnect resumes the same seat await B.page.evaluate(() => NW.app.ctrl.ws.close()); await until(() => B.page.evaluate(() => NW.app.ctrl.status === 'reconnecting'), 2000, 'guest shows reconnecting').catch(() => console.log('(reconnect was instant)')); await until(() => B.page.evaluate(() => NW.app.ctrl.status === 'joined' && NW.app.ctrl.you.slot === 1), 8000, 'guest resumed seat'); const towersB = await B.page.evaluate(() => NW.app.ctrl.view.towers.length); assert(towersB === 8, 'state restored after reconnect (' + towersB + ' towers)'); // host leaves -> guest becomes host await A.page.evaluate(() => NW.app.ctrl.close()); await until(() => B.page.evaluate(() => NW.app.ctrl.players.length === 1 && NW.app.ctrl.you.host), 4000, 'guest promoted to host'); if (errors.length) throw new Error('console errors:\n' + errors.join('\n')); console.log('no console errors'); };