Test harnesses for browser canvas games and simulations
testingheadless-chromecanvasharnessdeterminismes-modules
Every game and simulation on justinahampton.com ships with a headless harness (tests/<name>/run.sh) that prints one line per check. The pattern makes "it works" a claim with evidence and lets a later session change a game without breaking it. These are the conventions that made it reliable.
Expose a test hook from the page
- One global per product:
window.OD2,window.MEASLY,window.RECOURSE,QuasiPage,SandPage,Hyp. Typical surface:start,state(),set({...})to inject positions or stats,act/input,tick(dt)orupdate(dt),draw(),freeze()/thaw(). ?test=1(orfreeze()) stops the page's own rAF loop so the harness steps frames itself. rAF barely fires under headless virtual time and is starved inside iframes, so pumpingtick()is the only reliable clock.?seed=Nmakes the RNG deterministic;?instant=1removes waits and animations;?pad=1forces touch layout.- Keep the model DOM-free (
sand.js,fly.js,quasi.js,pebble.js) so math checks run without a page, under Node or GNOME'sgjs -m(see Shell and process gotchas for agent sessions on the desktop).
Check behavior, not just "no exceptions"
- Assert outcomes: a scripted 20 s run must commit a crime, raise heat, steal a car and visit the shop; a scripted six-move loop must produce a square with reach 5; all 40 puzzle levels must be won by replaying their stored solutions and every par re-proven by a fresh search.
- Physics regression baselines: record measured numbers (turn radius on grip vs handbrake, wall-grind speeds) as the expected values.
- Layout checks in a 390 to 412 px iframe: every overlay heading and button sits inside the stage rect,
scrollHeight <= clientHeight, no horizontal overflow (headless windows cannot be that narrow; see Headless Chrome testing gotchas). - Math checks against known results: Onsager's exact magnetization, the 192 and 100,352 recurrent sandpile boards of 2x2 and 3x3, the 104-step Langton's ant highway, secretary-problem exact values.
Pitfalls seen
- Natural spawns keep happening during long runs: assert
>=on deed counts and deltas, not exact values. Clearcars/pedseach frame in pure-physics tests; respawned traffic faked a large speed drop once. - Random power-ups made checks flaky (a shield ate the orb that should have cost a life). Add a switch (
OD2.noPups) and set it up front. - Leave one idle entity on the board in score checks, or "board empty" fires the wave-clear bonus every frame.
- An open modal blocks every other action; a scripted player must handle it first. A bot that "waits until an offer" loops forever against an opponent that never offers.
- Guards in the game (e.g.
bomb()ignored while paused) trip harnesses that freeze the loop: wrap asthaw(); bomb(); freeze()synchronously. performance.now()does not advance under--virtual-time-budget; a timing assertion there passes at 0 ms. Benchmark from wall clock (difference of a long and a short run) or the page's own onload.- A test page must be served from inside the site root; copy it in, run, delete it before deploying.
Random play as a smoke test
~400 random key presses must produce no runtime errors and real progress. Random play is high variance (17% to 71% territory claimed across runs in Encircle), so keep such thresholds loose.
See also
- Headless Chrome testing gotchas
- Measuring game balance with scripted bots
- Structuring a browser game as plain ES modules (no bundler)