Make the test suite fully deterministic
Seed Math.random before the game script loads, keep the game's own seeded-games wrapper in place, and seed the scenario test too. Random events during dayTick could replace the deadline modal and fail CI.
This commit is contained in:
parent
8fd79ef122
commit
56e15d10dd
2 changed files with 5 additions and 3 deletions
|
|
@ -20,6 +20,8 @@ function load(opts = {}) {
|
|||
virtualConsole: vc,
|
||||
beforeParse(window) {
|
||||
window.__TUBES_TEST__ = true;
|
||||
// Seed before the game script runs, so nothing random happens at load time either.
|
||||
if (opts.seed !== false) { let r = opts.seed || 12345; window.Math.random = () => { r = (r * 16807) % 2147483647; return r / 2147483647; }; window.__reseed = (n) => { r = n; }; }
|
||||
for (const [k, v] of Object.entries(opts.storage || {})) window.localStorage.setItem(k, v);
|
||||
window.Path2D = class { moveTo() {} lineTo() {} quadraticCurveTo() {} bezierCurveTo() {} arc() {} rect() {} closePath() {} };
|
||||
// A recording 2D context, so draw code really runs under test (jsdom has no canvas).
|
||||
|
|
@ -40,13 +42,13 @@ function load(opts = {}) {
|
|||
});
|
||||
const ev = (src) => dom.window.eval(src);
|
||||
// Deterministic by default so a test never passes or fails by luck; pass {seed:false} to opt out.
|
||||
if (opts.seed !== false) seed(ev, opts.seed || 12345);
|
||||
// (Seeded in beforeParse, so the game's own seeded-games wrapper around Math.random stays in place.)
|
||||
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;};})();`);
|
||||
ev(`if(window.__reseed)__reseed(${n});else Math.random=(()=>{let s=${n};return()=>{s=(s*16807)%2147483647;return s/2147483647;};})();`);
|
||||
}
|
||||
|
||||
module.exports = { load, seed };
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ test('sandbox: endless money, every unlock, nothing breaks, no badges', () => {
|
|||
});
|
||||
|
||||
test('scenarios: rules apply, goals complete, deadlines fail, seeds repeat', () => {
|
||||
const { ev, errors } = load({ seed: false });
|
||||
const { ev, errors } = load();
|
||||
ev("ACT.scenarios()");
|
||||
assert.match(ev("document.getElementById('modalCard').textContent"), /Weekly challenge/);
|
||||
ev("ACT.startScn({id:'stg'})");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue