// code-2: the selected-tower panel used to be rebuilt (innerHTML) on every view.rev change. In co-op the partner's actions // bump rev constantly, so the button under a finger was replaced between press and release and the tap was lost. // Here a page-side timer hammers rev (a wall is built and sold every 30 ms, standing in for a busy partner) while real // presses (down, hold 150 ms, up) land on Upgrade and Target. Every one of them must arrive, and the buttons must be the // very same DOM nodes at the end. On the old code only a small fraction of the presses produced a click. module.exports = async function ({ browser, url: URL }) { const assert = (c, m) => { if (!c) throw new Error('ASSERT: ' + m); console.log('ok -', m); }; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const errors = []; for (const [name, opts, touch] of [ ['desktop mouse', { viewport: { width: 1100, height: 700 } }, false], ['phone touch', { viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true }, true], ]) { console.log('=== ' + name); 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()}`); }); const cdp = touch ? await ctx.newCDPSession(page) : null; await page.goto(URL); await sleep(300); await page.evaluate(() => document.getElementById('btnSolo').click()); await sleep(200); await page.evaluate(() => document.getElementById('btnHelpClose').click()); await sleep(100); await page.evaluate(() => { const c = NW.app.ctrl; c.sim.gold = 100000; c.send({ c: 'place', type: 'filter', x: 10, y: 8 }); }); await sleep(200); const board = await page.locator('#board').boundingBox(); const cp = await page.evaluate(() => NW.app.renderer.cellToScreen(10, 8)); if (touch) await page.touchscreen.tap(board.x + cp.x, board.y + cp.y); else await page.mouse.click(board.x + cp.x, board.y + cp.y); await sleep(250); assert(await page.evaluate(() => NW.app.selTower > 0), 'tower selected'); // watch: clicks that reach an action button, and any action button being removed from the DOM await page.evaluate(() => { window.__clicks = { up: 0, mode: 0 }; window.__removed = 0; document.addEventListener('click', (e) => { const b = e.target.closest && e.target.closest('[data-act]'); if (b && window.__clicks[b.dataset.act] !== undefined) window.__clicks[b.dataset.act]++; }, true); new MutationObserver((muts) => { for (const m of muts) for (const n of m.removedNodes) if (n.nodeType === 1 && (n.matches('[data-act]') || n.querySelector('[data-act]'))) window.__removed++; }).observe(document.getElementById('info'), { childList: true, subtree: true }); window.__els = { up: document.querySelector('[data-act="up"]'), mode: document.querySelector('[data-act="mode"]'), sell: document.querySelector('[data-act="sell"]') }; // the busy partner: build / sell a wall every 30 ms (each one bumps view.rev) let i = 0; window.__rev0 = NW.app.ctrl.view.rev; window.__hammer = setInterval(() => { const c = NW.app.ctrl, x = 2 + (i % 6), y = 2 + ((i >> 3) % 2), ex = c.view.towers.find((t) => t.x === x && t.y === y); if (ex) c.send({ c: 'sell', id: ex.id }); else c.send({ c: 'place', type: 'wall', x, y }); i++; }, 30); }); const press = async (act) => { const r = await page.evaluate((a) => { const e = document.querySelector(`[data-act="${a}"]`).getBoundingClientRect(); return { x: e.left + e.width / 2, y: e.top + e.height / 2 }; }, act); if (touch) { await cdp.send('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: [{ x: r.x, y: r.y, id: 1 }] }); await sleep(150); await cdp.send('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] }); } else { await page.mouse.move(r.x, r.y); await page.mouse.down(); await sleep(150); await page.mouse.up(); } await sleep(120); }; for (let i = 0; i < 2; i++) await press('up'); for (let i = 0; i < 10; i++) await press('mode'); const res = await page.evaluate(() => { clearInterval(window.__hammer); const t = NW.app.ctrl.view.towers.find((x) => x.id === NW.app.selTower); return { clicks: window.__clicks, removed: window.__removed, revBumps: NW.app.ctrl.view.rev - window.__rev0, level: t && t.level, mode: t && t.mode, same: ['up', 'mode', 'sell'].every((k) => document.querySelector(`[data-act="${k}"]`) === window.__els[k]) }; }); console.log(JSON.stringify(res)); assert(res.revBumps > 40, `rev changed ${res.revBumps} times during the presses`); assert(res.clicks.up === 2 && res.level === 3, `both Upgrade presses arrived (clicks ${res.clicks.up}, level ${res.level})`); assert(res.clicks.mode === 10, `all 10 Target presses arrived (${res.clicks.mode})`); assert(res.mode === 'strong', `10 Target taps cycled the mode to ${res.mode}`); assert(res.removed === 0 && res.same, `no action button was replaced in the DOM (${res.removed} removals)`); await ctx.close(); } if (errors.length) throw new Error('console errors:\n' + errors.join('\n')); console.log('no console errors'); };