towerdef/e2e/scenarios/04-coop.js
Claude 28502984ec Co-op robustness, audio/a11y, new towers and enemies, Uplink rebalance
Co-op (QA'd with two to three real browsers, TCP-proxy chaos scenarios):
- partner's tower changes are never dropped after a render stall or snapshot burst
- half-open connections show "reconnecting" in ~6 s instead of 45-65 s
- upgrade and next-wave are idempotent (stale commands refused), restart message,
  promoted host gets Play again, failed joins never kill a live game, listener leaks

Audio / keyboard / a11y:
- one Audio element per track, fade/stinger supersede tokens, play() retry on the
  next gesture, 5 s fallback to the synth, mute works everywhere, dialogs with
  role/aria-modal/focus handling, accessible names for sound/speed/pause

New content:
- enemies: Daemon (healer), Proxy (regenerating shield), Botnet (flyer swarm);
  waves weave them in from wave 9 (waves 1-8 unchanged)
- towers: Sandbox (stun pulse), Load Balancer (fire-rate aura); hotkeys 1-9
- glyphs/shapes, stun/heal effects, gallery e2e that pixel-checks every kind

Orbital Uplink rebalanced after playtesting (was affordable by wave ~6 and buyable
in bulk from boss rewards): 1500 credits, upgrades 1200/2000/3200, and every Uplink
already owned makes the next one 25% dearer to buy AND to upgrade (each tower
remembers its multiplier, carried in the tower list as a 9th field). Shared
TD.priceFor/upgradePrice so the UI shows exactly what the sim charges.

Also: tablet/foldable viewports in the layout matrix (tolerant colour check),
docs (README tower/enemy tables, PROTOCOL shield/stun/pm encodings, NEXT roadmap).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
2026-10-10 21:18:43 +00:00

100 lines
6.8 KiB
JavaScript

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);
// a first-time player gets the help whether they start solo or join co-op (code-26); it is not shown again once seen
assert(await A.page.evaluate(() => !document.getElementById('help').hidden), 'the first-run help is shown to a new co-op host');
await A.page.click('#btnHelpClose');
assert(await A.page.evaluate(() => document.getElementById('help').hidden && NW.app.inGame && !NW.app.ctrl.view.paused), 'closing it returns to the game');
// 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');
assert(await B.page.evaluate(() => !document.getElementById('help').hidden), 'and to an invited guest');
await B.page.tap('#btnHelpClose');
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');
};