towerdef/test/sim-commands.test.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

87 lines
4.5 KiB
JavaScript

'use strict';
/*
* Idempotent commands (coop-5): two players who press Upgrade / Next wave at the same moment send the same
* command twice. Each carries what its sender saw (`lv`: the tower level, `n`: the wave it expects to start);
* the sim applies the first and rejects the second with reason 'stale' instead of charging or launching twice.
* A command without the field is applied as before (auto waves, the balance bot, older clients).
*/
const test = require('node:test');
const assert = require('node:assert/strict');
const TD = require('../public/js/sim.js');
const { Sim, TOWERS } = TD;
function withTower(type = 'filter') {
const s = new Sim({ map: 'open', gold: 5000 });
const r = s.cmd({ c: 'place', type, x: 10, y: 8, by: 0 });
assert.ok(r.ok, 'place ' + type);
return { s, id: r.id };
}
test('upgrade carrying the level the sender saw is applied once; the same command again is stale and costs nothing', () => {
const { s, id } = withTower();
const g0 = s.gold, cost = TOWERS.filter.levels[1].cost;
const a = s.cmd({ c: 'upgrade', id, lv: 1, by: 0 });
assert.deepEqual([a.ok, a.level], [true, 2]);
assert.equal(s.gold, g0 - cost);
const rev = s.rev;
const b = s.cmd({ c: 'upgrade', id, lv: 1, by: 1 });
assert.deepEqual([b.ok, b.reason], [false, 'stale']);
assert.equal(s.towerById(id).level, 2, 'still level 2, not 3');
assert.equal(s.gold, g0 - cost, 'charged once');
assert.equal(s.rev, rev, 'a rejected command changes nothing');
assert.equal(s.cmd({ c: 'upgrade', id, lv: 2, by: 1 }).ok, true, 'a sender who saw level 2 may go on to 3');
assert.equal(s.towerById(id).level, 3);
});
test('upgrade without a level (older client, bot, console) keeps working; a stale level never matches a later level either', () => {
const { s, id } = withTower();
assert.equal(s.cmd({ c: 'upgrade', id }).ok, true);
assert.equal(s.towerById(id).level, 2);
assert.equal(s.cmd({ c: 'upgrade', id, lv: null }).ok, true);
assert.equal(s.towerById(id).level, 3);
assert.equal(s.cmd({ c: 'upgrade', id, lv: 2 }).reason, 'stale');
assert.equal(s.cmd({ c: 'upgrade', id, lv: '3' }).reason, 'stale', 'a level of the wrong type is never "the level I saw"');
assert.equal(s.towerById(id).level, 3);
});
test('stale beats max / gold: a sender who saw an older level is told it is stale, not that the tower is maxed out', () => {
const { s, id } = withTower();
const top = TOWERS.filter.levels.length;
for (let lv = 1; lv < top; lv++) assert.ok(s.cmd({ c: 'upgrade', id, lv }).ok);
assert.equal(s.towerById(id).level, top);
assert.equal(s.cmd({ c: 'upgrade', id, lv: top - 1 }).reason, 'stale');
assert.equal(s.cmd({ c: 'upgrade', id, lv: top }).reason, 'max', 'a sender who saw the top level still gets the real reason');
const poor = new Sim({ map: 'open', gold: TOWERS.filter.levels[0].cost });
const p = poor.cmd({ c: 'place', type: 'filter', x: 10, y: 8 });
assert.equal(poor.cmd({ c: 'upgrade', id: p.id, lv: 1 }).reason, 'gold');
assert.equal(poor.cmd({ c: 'upgrade', id: 999, lv: 1 }).reason, 'no_tower');
s.state = 'lost'; // and a finished game answers "over" before it says "stale"
assert.equal(s.cmd({ c: 'upgrade', id, lv: 7 }).reason, 'over');
assert.equal(s.cmd({ c: 'wave', n: 7 }).reason, 'over');
});
test('wave carrying the number it expects to start: the first starts it, a second sender who saw the same wave is stale', () => {
const s = new Sim({ map: 'open' });
const a = s.cmd({ c: 'wave', n: 1, by: 0 });
assert.deepEqual([a.ok, a.wave], [true, 1]);
const queued = s.spawnQ.length;
const b = s.cmd({ c: 'wave', n: 1, by: 1 });
assert.deepEqual([b.ok, b.reason], [false, 'stale']);
assert.equal(s.wave, 1, 'one wave started, not two');
assert.equal(s.spawnQ.length, queued, 'nothing was queued by the rejected command');
assert.equal(s.cmd({ c: 'wave', n: 2 }).ok, true, 'a sender who saw wave 1 may start wave 2');
assert.equal(s.wave, 2);
assert.equal(s.cmd({ c: 'wave', n: 9 }).reason, 'stale', 'a number from the future is stale too');
});
test('wave without a number still starts the next wave; auto waves never need one', () => {
const s = new Sim({ map: 'open' });
assert.equal(s.cmd({ c: 'wave' }).wave, 1);
assert.equal(s.cmd({ c: 'wave', n: null }).wave, 2);
s.cmd({ c: 'auto', v: true });
s.nextWaveIn = 0.01;
s.tick(TD.STEP * 2);
assert.equal(s.wave, 3, 'the timer starts a wave on its own');
assert.equal(s.cmd({ c: 'wave', n: 3 }).reason, 'stale', 'and a click that saw the old wave number is now stale');
});