Faster tests, split everywhere: one sharded CI for pushes and releases

- The harness builds the DOM without the page's 2 MB of script text and
  runs the scripts straight in the window: a page load in tests takes
  about half the time (jsdom's parser was slow on the script text).
- That also took the game's source out of the DOM, which showed three
  tests that matched a phrase in the page source rather than the screen;
  they now check the panel, the routing window and the dictionary.
- CI plans its split once and runs the tests on 6 runners; every run on
  main records each file's time and caches it, so the parts stay even
  without anyone refreshing tests/durations.json.
- auto-release.yml and release.yml call the same sharded tests instead
  of running all 170 files on one runner each (11 and 15 min); a release
  is tested once, before it is tagged.
- Audit: five tests that duplicated a gameplay rule or checked nothing
  are gone; the two slowest checks sample a third of the cities, and a
  year of play to reach one due date became a moved due date.
This commit is contained in:
Frederic Jung 2026-11-07 07:24:09 +01:00
parent 7a6eb29013
commit 0342d90657
No known key found for this signature in database
17 changed files with 244 additions and 109 deletions

View file

@ -38,9 +38,10 @@ test('a new network runs plain IGP: no Auto-TE until the backbone is upgraded, o
assert.deepEqual(JSON.parse(ev('JSON.stringify(S.core)')), { lv: 0, run: 0 });
ev('ACT.buyTE()');
assert.equal(ev('!!S.te'), false, 'no Auto-TE on plain IGP');
ev("ACT.leftTab({t:'noc'})");
assert.match(dom.window.document.body.textContent, /Backbone/);
assert.match(dom.window.document.body.textContent, /Auto-TE needs an RSVP-TE/);
// The backbone lives in the NOC's routing window; the refusal is a toast.
ev('ACT.routingOpen()');
assert.match(dom.window.document.getElementById('modal').textContent, /Backbone/);
assert.match(dom.window.document.getElementById('toasts').textContent, /Auto-TE needs an RSVP-TE/);
const cash = ev('S.cash');
ev('ACT.coreUp()');
assert.equal(ev('S.core.lv'), 1);

View file

@ -23,14 +23,6 @@ test('#257 the calendar has real months and leap years',()=>{
assert.deepEqual(errors,[]);
}finally{dom.window.close();}
});
test('#257 a calendar month costs its monthly price, whatever its length',()=>{
const {ev,dom}=load();try{
ev("ACT.start({});S.day=31");// 1 February Y1
const before=ev('S.cash'),net=ev('R.revMo-R.costMo');
ev("for(let i=0;i<28;i++)S.cash+=(R.revMo-R.costMo)/monLen(S.day+i)");
assert.ok(Math.abs(ev('S.cash')-before-net)<1e-6,'28 days of February add up to one month');
}finally{dom.window.close();}
});
test('#257 loan statements and the intern budget follow calendar months',()=>{
const {ev,dom}=load();try{
ev("ACT.start({})");

View file

@ -15,12 +15,6 @@ test('every city sits on its landmass', () => {
assert.equal(off, '');
});
test('every cable lands at two known cities and has a length', () => {
const { ev } = load();
const bad = ev("CABLE_PAIRS.filter(c=>!CITY[c.a]||!CITY[c.b]||!(c.km>0)).map(c=>c.cid).join()");
assert.equal(bad, '');
});
test('a year of scripted play stays healthy', () => {
const { ev, errors } = load();
seed(ev, 7);

View file

@ -6,12 +6,33 @@
// it fails here rather than in production.
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { JSDOM, VirtualConsole } = require('jsdom');
const { assemble } = require('../scripts/lib/page');
const PAGE = {};
const page = (dev) => PAGE[dev] || (PAGE[dev] = assemble({ dev }).html);
// The page's inline scripts, taken out of the HTML once per process and compiled once. jsdom's HTML parser is slow
// on 2 MB of script text (it was more than half of every load()), so the DOM is built from the rest of the page and
// the scripts then run in its window, in order, the same way jsdom runs them (vm.Script.runInContext). Only plain
// <script> tags run, as in the browser here; data blocks (ld+json) stay in the page untouched.
const SPLIT = new Map();
function split(html) {
let s = SPLIT.get(html);
if (s) return s;
const code = [];
const shell = html.replace(/<script>([\s\S]*?)<\/script>/g, (m, src) => {
code.push(new vm.Script(src, { filename: `page-script-${code.length}.js` }));
return '<script></script>';
});
// With scripting on, the parser keeps a <noscript>'s content as text; with scripts run from outside it builds those
// elements (an extra <h1>, <p>, <div>). Escaped, the content stays one text node, as players' browsers have it.
const page = shell.replace(/(<noscript>)([\s\S]*?)(<\/noscript>)/g, (m, a, body, b) => a + body.replace(/&/g, '&amp;').replace(/</g, '&lt;') + b);
SPLIT.set(html, s = { shell: page, code });
return s;
}
// opts.file: a built or older page to load instead (the perf bench, the release build test).
// opts.agent: the development page, with src/agent/.
function load(opts = {}) {
@ -22,12 +43,13 @@ function load(opts = {}) {
// 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, {
const { shell, code } = split(html);
const dom = new JSDOM(shell, {
// opts.url: the origin the page is "served" from. Tests that dial the agent relay pass the relay's own host, so
// the page's WebSocket Origin matches (the relay refuses a cross-site one); everything else stays on a made-up
// https origin nothing can reach.
url: opts.url || 'https://tests.invalid/',
runScripts: 'dangerously',
runScripts: 'outside-only',
pretendToBeVisual: true,
virtualConsole: vc,
beforeParse(window) {
@ -61,6 +83,14 @@ function load(opts = {}) {
window.HTMLCanvasElement.prototype.toBlob = (cb) => cb(null);
},
});
// The language picker document.write()s a dictionary <script src> for a non-English player. jsdom fetched nothing
// for it anyway (opts.lang registers the dictionary), and after parsing a real write would wipe the page.
const doc = dom.window.document, ctx = dom.getInternalVMContext();
doc.write = () => {};
for (const sc of code) {
try { sc.runInContext(ctx); } catch (e) { vc.emit('jsdomError', Object.assign(new Error(`Uncaught [${e}]`), { detail: e })); }
}
delete doc.write;
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.
// (Seeded in beforeParse, so the game's own seeded-games wrapper around Math.random stays in place.)

View file

@ -19,11 +19,13 @@ test('diversity map: the indexed graph answers like disjointRoutes() over every
const cp = ev('CABLE_PAIRS[0].cid+":"+CABLE_PAIRS[0].a+"-"+CABLE_PAIRS[0].b');
for (const st of [{ won: false }, { won: true }, { won: false, scn: { rules: { banCities: ['jam', 'lon'], banPairs: [cp] } } }]) {
const r = JSON.parse(ev(`(()=>{const st=${lit(st)},L=plainLinks(st);diversityMap=null;diversityPossible('fra','nyc',st);
const ids=CITIES.map(c=>c.id),bad=[];let n=0;for(let i=0;i<ids.length;i+=3)for(let j=i+1;j<ids.length;j+=3){n++;const a=ids[i],b=ids[j];
const ids=CITIES.map(c=>c.id),bad=[];let n=0;for(let i=0;i<ids.length;i+=9)for(let j=i+1;j<ids.length;j+=9){n++;const a=ids[i],b=ids[j];
const want=(st.scn&&st.scn.rules.banCities.some(x=>x===a||x===b))?diversityPossible(a,b,st):disjointRoutes(a,b,L);if(diversityPossible(a,b,st)!==want)bad.push(a+'-'+b);}
return JSON.stringify({links:L.length,mine:diversityMap.links,n,bad})})()`));
assert.equal(r.mine, r.links, 'the same possible links');
assert.ok(r.n > 2000);
// Every 9th city against every 9th after it: over a thousand pairs, near and far, in all regions (each pair walks
// every possible link the plain way, so all pairs took 90 s).
assert.ok(r.n > 1000);
assert.deepEqual(r.bad, [], JSON.stringify(st));
}
assert.deepEqual(errors, []);
@ -41,7 +43,7 @@ test('link curves: skipping far-away cities gives the same bend around the ones
const cand=[1,1.5,2.1,2.9,3.8].map(k=>lim(sg*mag*k)).concat([1,1.5,2.1,2.9,3.8].map(k=>lim(-sg*Math.max(mag,base)*k)));let best=null,bs=-Infinity;
for(const off of cand){const cx=mx+nx*off,cy=my+ny*off,c=clear(cx,cy);if(c>=need){best=[cx,cy];break;}const sc=Math.min(c,need)-Math.abs(off)*0.35;if(sc>bs){bs=sc;best=[cx,cy];}}
return {ax:A.x,ay:A.y,bx,by,cx:best[0],cy:best[1],len};}`);
const bad = JSON.parse(ev(`(()=>{const ids=CITIES.map(c=>c.id),bad=[];for(let i=0;i<ids.length;i+=6)for(let j=i+1;j<ids.length;j+=5)for(const k of [0,1,4]){
const bad = JSON.parse(ev(`(()=>{const ids=CITIES.map(c=>c.id),bad=[];for(let i=0;i<ids.length;i+=12)for(let j=i+1;j<ids.length;j+=10)for(const k of [0,1,4]){
const l={id:'x'+i+'_'+j,a:ids[i],b:ids[j]};if(JSON.stringify(bezGeom(l,k))!==JSON.stringify(plainGeom(l,k)))bad.push(l.a+'-'+l.b+'#'+k);}return JSON.stringify(bad)})()`));
assert.deepEqual(bad, []);
assert.deepEqual(errors, []);

View file

@ -105,14 +105,3 @@ test('old saves with inbox, max-prefix and Large Peering Model state load and pl
assert.equal(ev('__o.pdeskRep'), null);
assert.deepEqual(errors, []);
});
test('max-prefix incidents can no longer fire', () => {
const { ev, errors } = load();
setup(ev);
assert.equal(ev("failureRun.toString().includes('maxpfx')"), false);
ev("S.day=100;const src=failureRun.toString().replace(/^function\* failureRun/,'function*').replace('let tot=opts.reduce',\"opts.splice(0,opts.length,['funny',1]);let tot=opts.reduce\");window.__ff=eval('('+src+')')");
ev("__reseed(7);for(let i=0;i<200;i++){runNow(__ff());S.inc=S.inc.filter(x=>x.type!=='maxpfx'||(window.__mp=1,0));}");
assert.equal(ev('!!window.__mp'), false);
assert.equal(ev("typeof inboxDay+typeof ibx+typeof ACT.inboxRaiseAll+typeof ACT.inboxBot"), 'undefinedundefinedundefinedundefined');
assert.deepEqual(errors, []);
});

View file

@ -127,23 +127,6 @@ test('launches fill crusts, the party is billed, heavy test flights explode', ()
assert.deepEqual(errors, []);
});
test('O5: launches only fail with chaos on, and never in the first 90 days', () => {
const run = (after) => {
const { ev } = load();
won(ev); company(ev);
ev(`S.day=S.pie.born+${after};Math.random=()=>0`); // the unluckiest dice there are
ev("ACT.pieLaunch({rk:'falcon',cr:'short'});S.pie.q[0].day=S.day;pieDay(S.day)");
return ev("pieLive('short')");
};
assert.equal(run(10), 20, 'quiet days');
assert.equal(run(120), 0, 'chaos can lose a launch');
const { ev } = load();
sandbox(ev);
company(ev);
ev("S.day=500;S.pie.pads[0].next=0;Math.random=()=>0;ACT.pieLaunch({rk:'falcon',cr:'short'});S.pie.q[0].day=S.day;pieDay(S.day)");
assert.equal(ev("pieLive('short')"), 20, 'chaos none: nothing fails');
});
test('cohorts wear out over the end of their life, and a crust carries a share from 40%, all of it from 60%', () => {
const { ev, errors } = load();
won(ev);
@ -186,7 +169,9 @@ test('promises slip a year and get broken', () => {
won(ev);
company(ev);
assert.equal(ev('S.pie.founder.promises'), 1);
ev('for(let i=0;i<361;i++)dayTick()');
assert.ok(ev('S.pie.founder.due[0]-S.day') >= 359, 'due a year from the promise');
// Then a year passes: rather than play 360 days, the due date moves to tomorrow.
ev('S.pie.founder.due[0]=S.day+1;for(let i=0;i<2;i++)dayTick()');
assert.equal(ev('S.pie.founder.broken'), 1);
assert.ok(ev("S.pie.founder.posts.some(p=>p.k==='pie.fp_slip')"));
assert.match(ev('pieFpText(S.pie.founder.posts[0])'), /\w/);

View file

@ -166,11 +166,6 @@ test('F4: the chaos monkey snips a laser link while it visits, healed within a w
assert.deepEqual(errors, []);
});
test('the booster that falls over on the drone ship is the foundation\'s', () => {
const { ev } = load();
assert.ok(ev("pieFly.toString().includes('fp_booster')"));
});
test('sanitize drops orbit incidents that make no sense, and old saves are untouched', () => {
const { ev, errors } = load();
sky(ev);

View file

@ -1021,7 +1021,8 @@ test('Down Under: routers upside down, BrisVegas, Next-Door-DC and everything tr
assert.match(ev("siteName('bne','b')"), /^Next-Door-DC B[12]$/);
assert.equal(ev("siteName('cbr','b')"), 'Next-Door-DC C1');
// everything is trying to kill you: the new outages can come up on Australian links and POPs
const src = ev("document.documentElement.innerHTML");
// (The game's English text, where the outages are written; the page once carried it in its <script>.)
const src = ev("JSON.stringify(I18N.en)");
for (const s of ['drop bear', 'eastern brown snake', 'Magpie swooping', 'Box jellyfish', 'Vegemite as thermal paste', 'brought Marmite', 'four o’clock storm']) assert.ok(src.includes(s), s);
assert.match(ev("ixName('syd~2')"), /At-The-Edge/); assert.match(ev("XIX.find(x=>x.id==='syd~2').blurb"), /Vegemite/);
ev('cam.z=2;draw(0);cam.z=0.4;draw(0)');

View file

@ -22,8 +22,10 @@ test('#157 a POP can grow past six routers, with a cage extension', () => {
assert.equal(withCage - without, ev('ROUTER_MO+RG[S.pops.fra.rgen||0].mo+CAGE_MO'), 'the cage extension is billed monthly');
ev("S.pops.fra.routers=7;compute()");
assert.ok(ev('nodeCap("fra")') === ev('RG[S.pops.fra.rgen||0].cap*7'), 'every router adds forwarding');
ev("UI.sel={k:'city',id:'fra'};renderAll()");
assert.match(dom.window.document.body.innerHTML, /Cage extension/);
// The routers live on the city's Infra tab. (Not the whole body: that once held the game's source, dictionary
// and all, so a phrase was always found there.)
ev("UI.sel={type:'city',id:'fra'};UI.tab='links';renderAll()");
assert.match(dom.window.document.getElementById('panel').innerHTML, /Cage extension/);
assert.deepEqual(errors, []);
});