seriesoftubes/tests/harness.js
Frederic Jung e082d6fc60
Persist games across reloads and announce new releases
- Autosave every five seconds while playing, plus on pagehide and when
  the tab is hidden. On load, a saved game resumes immediately (same
  speed and camera) instead of waiting behind the title screen.
- The build turns this version's CHANGELOG section into release notes:
  baked into the page for a one-time "What's new" card for returning
  players, and published as /version.json.
- Open games poll /version.json every 10 minutes and when the tab comes
  back into view; if a newer release is out, a banner offers to save
  and reload.
- "Start a new network" moves into the help screen, behind a confirm.
2026-10-23 11:42:28 +02:00

36 lines
1.4 KiB
JavaScript

// Loads site/index.html in jsdom with the animation loop disabled, so tests can
// drive the simulation directly through the game's globals (ACT, S, R, dayTick…).
const fs = require('node:fs');
const path = require('node:path');
const { JSDOM, VirtualConsole } = require('jsdom');
function load(opts = {}) {
const file = opts.file || path.join(__dirname, '..', 'site', 'index.html');
const html = fs.readFileSync(file, 'utf8');
const errors = [];
const vc = new VirtualConsole();
vc.on('jsdomError', (e) => {
// jsdom has no canvas; drawing calls are expected to be unimplemented.
if (!/getContext|Not implemented/.test(e.message)) errors.push(e.message);
});
const dom = new JSDOM(html, {
url: 'https://tests.invalid/',
runScripts: 'dangerously',
pretendToBeVisual: true,
virtualConsole: vc,
beforeParse(window) {
window.__TUBES_TEST__ = true;
for (const [k, v] of Object.entries(opts.storage || {})) window.localStorage.setItem(k, v);
window.Path2D = class { moveTo() {} quadraticCurveTo() {} closePath() {} };
},
});
const ev = (src) => dom.window.eval(src);
return { dom, ev, errors };
}
// Deterministic Math.random so failures are reproducible.
function seed(ev, n) {
ev(`Math.random=(()=>{let s=${n};return()=>{s=(s*16807)%2147483647;return s/2147483647;};})();`);
}
module.exports = { load, seed };