diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2858ab3..cbae642 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -146,6 +146,8 @@ built from (the live graph with its weights; every exit with what it reaches and the events that move routes), so most days, when only demand changes, they are reused, and any other change rebuilds them. Only indices are kept: exits, lists and paths are made fresh every call, because routing writes on them. `tests/route-cache.test.js` checks the keys, and the equivalence tests route with and without the kept tables. +`scripts/route-fuzz.js` plays hundreds of random seeded games on main's page and this checkout's at once, comparing all +of R and S after every move and day, with the kept tables and without (`tests/route-fuzz.test.js` plays a few). Each loop of `compute()` runs in a function of its own (the stages are closures, the busiest is `rankPass`; the money is `computeMoney()`). V8 optimises a small function quickly and keeps it; `compute()` as one 33 KB function diff --git a/scripts/lib/fuzz-page.js b/scripts/lib/fuzz-page.js new file mode 100644 index 0000000..c495355 --- /dev/null +++ b/scripts/lib/fuzz-page.js @@ -0,0 +1,167 @@ +// Runs inside a game page (scripts/route-fuzz.js loads two: main's and this checkout's), next to scripts/lib/mcp-page.js. +// The driver picks every move with its own seeded random numbers and sends the same calls to both pages, so the pages +// stay in lockstep; this file only offers what a player could do now (buttons on screen, POPs, cities) and digests what +// routing gave, as strings the driver compares. +(() => { + const DENY = new Set(['animSet', 'cont', 'exportSave', 'importGo', 'importPick', 'install', 'langSet', 'menu', 'mute', 'newgame', 'phonePlay', 'phoneSpeed', + 'prefSet', 'pzShare', 'resetAsk', 'settings', 'settingsClose', 'share', 'shareDl', 'shareGo', 'slotBack', 'slotDel', 'slotDelGo', 'slotExport', 'slotMenu', + 'slotRetry', 'slotSpace', 'slotSwitch', 'slotTake', 'soundSet', 'speed', 'start', 'toTitle', 'traceCopy', 'updateLater', 'updateNow', 'vpSet', 'wsShare', + // Leave the game or open what is not this game: the title screen, menus of other modes, AI helpers that fetch. + 'closePanel', 'puzzleMenu', 'puzzleDoor', 'scnMenu', 'startScn', 'startWeekly', 'pzStart', 'pzArStart', 'arMenu', 'teSlop', 'help', 'helpClose', 'keepPlaying', + 'logOpen', 'pitchSkip', 'pitchGo', 'feedback', 'tutSkip', 'tutNext']); + const sig = (b) => b.act + ' ' + JSON.stringify(b.args); + const btns = (root) => (root ? [...root.querySelectorAll('[data-act]')] : []).filter((b) => !b.disabled && !DENY.has(b.dataset.act) && typeof ACT[b.dataset.act] === 'function') + .map((b) => { let args = {}; try { args = JSON.parse(b.dataset.args || '{}'); } catch (e) {} return { act: b.dataset.act, args }; }); + const modalOpen = () => !$('modal').hidden; + const errs = []; + const call = (act, args) => { + try { const r = ACT[act](args || {}); if (r && typeof r.then === 'function') r.catch((e) => errs.push(act + ': ' + e.message)); return ''; } catch (e) { return act + ': ' + (e && e.message); } + }; + const fixClock = (t) => { Date.now = () => t; }; + + // ---------- starts ---------- + const homes = () => CITIES.filter((c) => homeOk(c.id)).map((c) => c.id); + // Home cities with rules of their own: China, Korea, Russia, Australia, the Philippines, islands and far places. + const special = () => CITIES.filter((c) => homeOk(c.id) && (c.cn || c.kr || c.ru || ['AU', 'PH', 'NZ', 'JP', 'ID', 'IS', 'FJ', 'PF', 'MU', 'MV', 'CL', 'ZA', 'BR', 'IN', 'SG', 'HK', 'TW', 'KR', 'CN'].includes(c.cc))).map((c) => c.id); + const lists = () => ({ homes: homes(), special: special(), scn: SCN.map((s) => s.id), pz: PZ.map((p) => p.id), ar: AR_RUNS.map((r) => r.id) }); + const settle = () => { UI.started = true; UI.pitched = true; UI.speed = 0; if (UI.pendGame) ACT.pitchSkip(); closeModal(); if (S && !pzOn()) compute(); renderAll(); }; + const start = ({ kind, id, city, preset, seed, name }) => { + if (window.__reseed) window.__reseed(seed); + fixClock(1.7e12 + seed * 1000); + UI.pitched = true; UI.pendGame = null; + if (pzOn()) pzLeave(); + if (kind === 'normal') beginGame(name, city, { mode: 'normal', preset }); + else if (kind === 'sandbox') beginGame(name, city, { mode: 'sandbox', rules: { chaos: preset } }); + else if (kind === 'scenario') { const sc = SCN.find((x) => x.id === id); beginGame(name, sc.city, { scn: sc }); } + else if (kind === 'weekly') { const wk = weekly(); beginGame(name, wk.city, { scn: wk, seed: wk.seed }); } + else if (kind === 'puzzle') pzBegin(pzDef(id)); + else if (kind === 'architect') arBegin(arRunDef(id)); + // A saved network (the big fixture): loaded and migrated as an import is, with its incidents and events still on. + else if (kind === 'save') { S = migrate(JSON.parse(window.__fzSave)); } + settle(); + return { mode: S.mode, home: S.home, puzzle: pzOn() }; + }; + + // ---------- what a player could do now ---------- + // The nearest cities, and those a cable lands from here (subsea seats, IRUs and own cables start there). + const near = (c, n) => CITIES.filter((x) => x.id !== c).map((x) => [x.id, kmBetween(c, x.id)]).sort((a, b) => a[1] - b[1]).slice(0, n).map((x) => x[0]) + .concat(CABLE_PAIRS.filter((p) => p.a === c || p.b === c).slice(0, 4).map((p) => (p.a === c ? p.b : p.a))); + const state = () => { + const pops = Object.keys(S.pops), nb = new Set(); + for (const p of pops.slice(0, 12)) for (const c of near(p, 8)) if (!S.pops[c]) nb.add(c); + return { pops, near: [...nb], cities: CITIES.length, links: S.links.map((l) => l.id), day: S.day, puzzle: pzOn(), modal: modalOpen(), lost: !!S.lost, + clock: !!rule('clock'), core: S.core ? S.core.lv : 0, nets: NET_IDS.length, transit: S.transit.map((t) => t.id), pni: S.pni.map((p) => p.id), biz: S.biz.map((b) => b.id), offers: (S.offers || []).map((o) => o.id) }; + }; + // The buttons of one place on screen, after opening it. + const screen = (w) => { + if (w.where === 'modal') return btns($('modal')); + if (w.where === 'city') { UI.sel = { type: 'city', id: w.city }; UI.tab = w.tab; renderAll(); return btns($('panel')); } + if (w.where === 'link') { if (!S.links.some((l) => l.id === w.id)) return []; UI.sel = { type: 'link', id: w.id }; renderAll(); return btns($('panel')); } + if (w.where === 'dash') { $('dash').hidden = false; UI.dtab = w.tab; renderDash(); const b = btns($('dash')); return b; } + if (w.where === 'left') { UI.left = w.tab; UI.leftOpen = true; renderLeft(); return btns($('leftBody')); } + if (w.where === 'openLink') { if (!S.pops[w.a]) return []; openLinkModal(w.a, w.b); return modalOpen() ? btns($('modal')) : []; } + return []; + }; + const press = (b) => { const e = call(b.act, b.args); return e; }; + const close = () => { if (modalOpen()) closeModal(); $('dash').hidden = true; }; + + // ---------- things a player does with no button handy here ---------- + const RS = ['spill at 80%', 'spill at 60%', 'protect customers', 'blackhole ddos']; + const script = (rules) => { + const L = []; + for (const r of rules) { + const [k, a, b] = r; + const net = NETS[a % NETS.length], t = S.transit.length ? NET[S.transit[a % S.transit.length].net] : net; + const pops = Object.keys(S.pops), l = S.links.length ? S.links[a % S.links.length] : null; + if (k === 0) L.push(RS[a % RS.length]); + else if (k === 1) L.push(`${b % 2 ? 'prefer' : 'avoid'} ${['pni', 'ix', 'transit'][b % 3]} for ${net.name.toLowerCase()}`); + else if (k === 2) L.push(`cap transit ${t.name.toLowerCase()} at ${10 + (b % 50)} gbps`); + else if (k === 3) L.push(`${b % 2 ? 'prefer' : 'avoid'} transit ${t.name.toLowerCase()}`); + else if (k === 4) L.push(`prepend transit ${t.name.toLowerCase()} ${1 + (b % 3)}`); + else if (k === 5 && pops.length) L.push(`drain ${CITY[pops[a % pops.length]].name.toLowerCase()}`); + else if (k === 6 && l) L.push(`avoid link ${CITY[l.a].name.toLowerCase()}-${CITY[l.b].name.toLowerCase()}`); + } + return L.join('\n'); + }; + const poke = (p) => { + const [k, a, b] = p; + if (k === 'te') { if (!S.te) return 'no te'; S.te.script = script(a); S.te.on = !!b; S.core = S.core || { lv: 0, run: 0 }; after(); return S.te.script; } + if (k === 'teOff') { if (S.te) { S.te.on = false; after(); } return ''; } + if (k === 'core') { if (S.core) { const v = a % (S.core.lv + 1); return call('coreSet', { v }); } return ''; } + if (k === 'fail') { S.nextFail = S.day + 1; return ''; } + if (k === 'event') { S.nextEvent = S.day + 1; return ''; } + if (k === 'cash') { S.cash += a; after(); return ''; } + if (k === 'lvl') { S.lvl = Math.min(LEVELS.length, (S.lvl || 0) + 1); after(); return ''; } + // Events that move routes, as the game's own events set them. + if (k === 'ev') { + const n = NETS[a % NETS.length].id; + if (b === 0) S.ev.leak = { net: n, until: S.day + 3 }; + else if (b === 1) S.ev.rpki = { nets: [n, NETS[(a * 7) % NETS.length].id], until: S.day + 3 }; + else if (b === 2) S.ev.depeer = S.day + 3; + else if (b === 3) { const x = Object.keys(S.ixp); if (x.length) { S.ev.ixdown = S.ev.ixdown || {}; S.ev.ixdown[x[a % x.length]] = S.day + 2; } } + else if (b === 4 && S.links.length) S.links[a % S.links.length].cut = S.day + 2; + else if (b === 5 && S.transit.length) S.transit[a % S.transit.length].down = S.day + 2; + // After an update brings networks: the courtesy route to them (sanitize() sets it on load). + else if (b === 7) S.grace = { until: S.day + 4, nets: [n, NETS[(a * 13) % NETS.length].id, NETS[(a * 31) % NETS.length].id], fresh: 0 }; + else if (b === 8) S.krExit = S.day + 2; + else if (b === 6) { const p = Object.keys(S.pops); if (p.length > 1) S.pops[p[1 + (a % (p.length - 1))]].down = S.day + 2; } + after(); return ''; + } + return 'unknown poke'; + }; + + /* Actions on what the network already has, with arguments a button somewhere would give (some sit in collapsed + lists, or on screens the walker rarely reaches): one list of calls, the driver picks one. */ + const direct = () => { + const o = [], add = (act, args) => { if (typeof ACT[act] === 'function') o.push({ act, args }); }; + const pops = Object.keys(S.pops), PR = ['high', 'low', 'normal']; + for (const t of S.transit) { add('setPref', { kind: 't', id: t.id, v: PR[(t.id.length + S.day) % 3] }); add('setCarry', { id: t.id, on: !t.carry }); add('lagT', { id: t.id, d: (S.day % 3) ? 1 : -1 }); add('cancelTransit', { id: t.id, ok: true }); add('togglePrem', { id: t.id }); add('cleanPipe', { id: t.id }); } + for (const p of S.pni) { add('setPref', { kind: 'p', id: p.id, v: PR[(p.id.length + S.day) % 3] }); add('lagP', { id: p.id, d: (S.day % 3) ? 1 : -1 }); add('cancelPni', { id: p.id, ok: true }); add('sacrif', { id: p.id }); } + for (const x of Object.keys(S.ixp)) { add('setPref', { kind: 'ix', id: x, v: PR[(x.length + S.day) % 3] }); add('lagIx', { city: x, d: (S.day % 3) ? 1 : -1 }); add('peerAll', { city: x }); add('rs', { city: x }); add('cancelIx', { city: x, sure: true, ok: true }); + for (const n of ixMembers(x).slice(0, 30)) add(ixSessions(x).has(n) ? 'unpeer' : 'peer', { city: x, net: n }); } + for (const c of pops) { + for (const x of ixIdsIn(c)) if (S.ixp[x] === undefined) add('buyIx', { city: x, tier: S.day % 3 }); + for (const n of NETS) if (n.pops.includes(c)) { if (S.day % 2 === 0 || n.cache) add('pni', { city: c, net: n.id, tier: n.id.length % 3 }); if (n.cache) add('cache', { city: c, net: n.id }); } + for (const n of NETS) if (sellsAt(n, c) && (n.id.length + S.day) % 4 === 0) add('buyTransit', { city: c, net: n.id, tier: (S.day >> 2) % 3 }); + add('addRouter', { city: c }); add('upRouter', { city: c, g: 1 + (S.day % 4) }); add('foundIx', { city: c }); add('launchBB', { city: c }); add('addSite', { city: c }); + } + for (const c of S.caches) add('rmCache', { id: c.id }); + for (const l of S.links) { add('setStandby', { id: l.id, on: !l.standby }); add('setMetric', { id: l.id, v: ['backup', 'avoid', 'prefer', 'normal'][(l.id.length + S.day) % 4] }); add('upLink', { id: l.id, tier: (S.day % 4) }); add('rmLink', { id: l.id }); add('addParallel', { id: l.id }); } + for (const b of S.offers || []) add('accept', { id: b.id }); + for (const b of S.biz) add('dropBiz', { id: b.id }); + for (const l of S.loanOffers || []) add('takeLoan', { id: l.id }); + for (const l of S.loans || []) add('repayLoan', { id: l.id }); + return o; + }; + + // ---------- time ---------- + const day = () => { if (S.lost || !rule('clock')) return 'no clock'; if (modalOpen()) closeModal(); try { dayTick(); } catch (e) { return 'dayTick: ' + e.message; } return ''; }; + + // ---------- digests ---------- + // Everything routing produced: R in full (with every link's R.ef), the flows (R.fl), and what the PNI lists read. + const routing = () => { + if (!R) return 'noR'; + const r = JSON.stringify(R), fl = R.fl ? JSON.stringify(R.fl) : ''; + let pf = ''; + if (typeof pniFlows === 'function' && S && !pzOn()) for (const c of Object.keys(S.pops).slice(0, 4)) try { pf += c + JSON.stringify(pniFlows(c)); } catch (e) { pf += c + 'E' + e.message; } + return r + '\n' + fl + '\n' + pf; + }; + const parts = () => { const o = {}; if (!R) return o; for (const k of Object.keys(R)) o[k] = JSON.stringify(R[k]); o.fl = R.fl ? JSON.stringify(R.fl) : ''; return o; }; + const sDigest = () => JSON.stringify(S); + const sParts = () => { const o = {}; for (const k of Object.keys(S)) o[k] = JSON.stringify(S[k]); return o; }; + /* After a move: what the move routed (a), routing again from the same state (b: on this checkout's page the kept + tables are used), and routing from that state with the cache off (c: RC.on=false where there is an RC). b and c + route from the same previous R; c's R is kept, as a plain compute would. */ + const check = () => { + if (!S || pzOn() && !R) return { a: routing() }; + const a = routing(), prior = R, keep = UI.rkR === prior; + compute(); const b = routing(); + R = prior; const rc = typeof RC !== 'undefined'; + if (rc) RC.on = false; + try { compute(); } finally { if (rc) RC.on = true; } + const c = routing(); + if (keep) UI.rkR = R; + return { a, b, c }; + }; + window.__fz = { direct, lists, start, state, screen, press, close, poke, day, routing, parts, sDigest, sParts, check, errs, modalOpen }; +})(); diff --git a/scripts/lib/route-fuzz.js b/scripts/lib/route-fuzz.js new file mode 100644 index 0000000..b5b5eb2 --- /dev/null +++ b/scripts/lib/route-fuzz.js @@ -0,0 +1,188 @@ +// Differential games (scripts/route-fuzz.js, tests/route-fuzz.test.js): two game pages in one process, a base page +// (main's) and this checkout's, play the same seeded game move for move. After every move and every day both must +// have routed identically (all of R, R.fl, R.ef, the PNI lists), and this checkout must route the same with the +// routing cache (RC) on and off. The whole game state S must match too, so the two games stay in lockstep. +// Every move is picked here, from what the base page offers right now, with the game's seed: the seed alone +// replays a game. +const fs = require('node:fs'); +const path = require('node:path'); +const { load } = require('../../tests/harness'); + +const FUZZ_JS = fs.readFileSync(path.join(__dirname, 'fuzz-page.js'), 'utf8'); + +function openPage(file, label) { + const g = load({ seed: 1, file }); + g.ev(FUZZ_JS); + g.label = label; + g.call = (fn, ...args) => { + g.dom.window.__fzA = JSON.parse(JSON.stringify(args)); + const out = g.ev(`JSON.stringify(__fz.${fn}(...window.__fzA))`); + return out === undefined ? undefined : JSON.parse(out); + }; + return g; +} + +// mulberry32: the driver's own numbers, apart from the pages' Math.random. +function rng(seed) { + let a = seed >>> 0; + const f = () => { a = (a + 0x6D2B79F5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; + f.int = (n) => Math.floor(f() * n); + f.pick = (a) => a[f.int(a.length)]; + // A button by its action's weight (w), then any button of that action: 38 transit sellers count as one choice. + f.wp = (a, w) => { const g = new Map(); for (const x of a) { const l = g.get(x.act); if (l) l.push(x); else g.set(x.act, [x]); } + const ks = [...g.keys()]; let s = 0; for (const k of ks) s += w(g.get(k)[0]); let t = f() * s; + for (const k of ks) { t -= w(g.get(k)[0]); if (t < 0) return f.pick(g.get(k)); } return f.pick(g.get(ks[ks.length - 1])); }; + f.w = (o) => { let s = 0; for (const k in o) s += o[k]; let x = f() * s; for (const k in o) { x -= o[k]; if (x < 0) return k; } return Object.keys(o)[0]; }; + return f; +} + +// The first key of two {key: json} maps that differs, with a short look at where. +function firstDiff(A, B) { + const keys = [...new Set([...Object.keys(A), ...Object.keys(B)])]; + const out = []; + for (const k of keys) { + if (A[k] === B[k]) continue; + const a = A[k] || '', b = B[k] || ''; + let i = 0; while (i < a.length && a[i] === b[i]) i++; + out.push({ key: k, at: i, base: a.slice(Math.max(0, i - 80), i + 120), head: b.slice(Math.max(0, i - 80), i + 120) }); + if (out.length >= 4) break; + } + return out; +} + +const DTABS = ['overview', 'cust', 'conn', 'traffic', 'infra', 'lease']; +const TABS = ['cust', 'up', 'peer', 'links']; +const LEFT = ['nets', 'noc', 'deals', 'goals']; +const PRESETS = ['freeze', 'oncall', 'bgp']; +// How often a button is pressed against the others on screen: what changes the network often, looking around rarely. +const BUILD = /^(buy\w*|pni\w*All|pni|peer|peerAll|unpeer|cache|cacheSite|rmCache|buildLink|buildPop|launchBB|accept|decline|dropBiz|takeLoan|repayLoan|lag[TPI]x?|setPref|setCarry|setStandby|setMetric|togglePrem|cleanPipe|sacrif|rs|foundIx|myIxUp|addRouter|rmRouter|upRouter|addSite|rmSite|siteSet|decomToggle|rmLink|upLink|addParallel|cancel\w*|upVxc|buyNni|rmNni|buildScrub|scrubUp|rmScrub|addGen|regen|addCh|rmCh|addPair|upSys|spares|layShip|crews|fixInc|seaPackage|subUpgrade|orderCable|orderDf|ownCab|opsHire|opsToggle|opsParam|opsCap|opsPop|opsTpl|opsNewPop|popStdSet|coreSet|teRecipe|dkOptOut|dkStay|utility\w*|listFiber|unlistFiber|leaseSwap|custGo|confGo|confSponsor|seatBuy|charter|cnPrem|applyCn|ad|ddosAuto|pz(Pop|RmPop|Link|AddPar|Tier|RmLink|RtAdd|RtRm|RtModel|Route|Via|Div)|setRoute|route|via|metricSet)$/; +const UIACT = /^(tab|dashTab|leftTab|\w*Sort|\w*Filter|pniGrp|trace\w*|focusCity|findGo|selLink|dashLink|logAll|tailTog|listMore|netFocus|netFilter|meetGo|phoneTab|dashClose|connF|connCity|connNet|connFor|custF|custCity|opsOpen|decomOpen|linkCheck)$/; +const DW = { launchBB: 4, accept: 4, buyTransit: 3, pni: 3, buyIx: 3, setPref: 4, cache: 2, peerAll: 2, setCarry: 2, takeLoan: 2 }; +const weight = (b) => (BUILD.test(b.act) ? 8 : UIACT.test(b.act) ? 0.3 : 1); + +/* One game. pages: {base, head}. Returns {seed, start, moves, days, log, div} where div is the first divergence (or null). + opts: moves (count, default 50 to 200 by seed), maxDays per day-move, onMove(i, move) */ +function playGame(pages, seed, opts = {}) { + const { base, head } = pages; + const r = rng(seed * 2654435761); + const both = (fn, ...a) => [base.call(fn, ...a), head.call(fn, ...a)]; + const L = base.call('lists'); + const log = [], acts = {}; + let div = null, days = 0; + const note = (m) => { log.push(m); }; + const fail = (where, info) => { if (!div) div = { seed, where, move: log.length, last: log.slice(-6), ...info }; }; + const same = (where, a, b) => { if (JSON.stringify(a) !== JSON.stringify(b)) fail(where, { base: a, head: b }); }; + + // Compare everything: routing on both pages (a: the move's, b: again, c: cache off) and the game state. + const compare = (where) => { + const [x, y] = both('check'); + for (const k of ['a', 'b', 'c']) if (x[k] !== y[k]) { fail(where + ': routing (' + k + ') base vs head', { diff: firstDiff(base.call('parts'), head.call('parts')) }); return false; } + if (y.b !== y.c) { fail(where + ': head cached vs uncached', { diff: 'R from the kept tables differs from a fresh route' }); return false; } + const [s1, s2] = both('sDigest'); + if (s1 !== s2) { fail(where + ': game state S', { diff: firstDiff(base.call('sParts'), head.call('sParts')) }); return false; } + return true; + }; + + // The start. + const kind = opts.save ? 'save' : r.w({ normal: 44, sandbox: 14, scenario: 20, weekly: 3, puzzle: 11, architect: 8 }); + if (opts.save) for (const p of [base, head]) p.dom.window.__fzSave = opts.save; + const home = r() < 0.4 ? r.pick(L.special) : r.pick(L.homes); + const st = { kind, seed: (seed % 2147483646) + 1, name: 'Fuzz ' + seed, city: home, + preset: kind === 'sandbox' ? r.pick(['none', 'calm', 'normal', 'spicy']) : r.pick(PRESETS), + id: kind === 'scenario' ? r.pick(L.scn) : kind === 'puzzle' ? r.pick(L.pz) : kind === 'architect' ? r.pick(L.ar) : undefined }; + const [s0, s1] = both('start', st); + same('start', s0, s1); + note('start ' + JSON.stringify(st)); + if (!div) compare('start'); + + // A screen's buttons on both pages; the same list or a divergence. + const screen = (w) => { const [a, b] = both('screen', w); same('buttons of ' + JSON.stringify(w), a, b); return a || []; }; + const press = (b, where) => { acts[b.act] = (acts[b.act] || 0) + 1; const [e1, e2] = both('press', b); same('press ' + where, e1, e2); note(where + ' ' + b.act + JSON.stringify(b.args) + (e1 ? ' !' + e1 : '')); }; + // Answer whatever window opened, a few levels deep, then close it. + const modal = (bias) => { + for (let k = 0; k < 4 && !div; k++) { + if (!base.call('modalOpen')) return; + let bs = screen({ where: 'modal' }); + if (bias) { const pref = bs.filter((b) => bias.test(b.act)); if (pref.length && r() < 0.7) bs = pref; } + const real = bs.filter((b) => !/^(closeModal|x)$/.test(b.act)); + if (!real.length || r() < 0.12) break; + press(r.wp(real, weight), ' modal'); + if (!div) compare('modal button'); + } + both('close'); + }; + + const N = opts.moves || 50 + (seed % 151); + for (let i = 0; i < N && !div; i++) { + const s = base.call('state'); + if (s.lost) { note('bankrupt'); break; } + const mv = r.w(s.puzzle ? { city: 40, openLink: 30, link: 15, modal: 5 } : { city: 22, direct: 22, openLink: 13, link: 5, dash: 5, left: 3, poke: 8, day: s.clock ? 20 : 0, build: 4 }); + if (opts.onMove) opts.onMove(i, mv); + if (mv === 'city' || mv === 'build') { + const city = mv === 'build' || !s.pops.length || r() < 0.3 ? (s.near.length ? r.pick(s.near) : r.pick(L.homes)) : r.pick(s.pops); + const tab = r.pick(TABS); + let bs = screen({ where: 'city', city, tab }); + if (mv === 'build') { const p = bs.filter((b) => /^(buildPop|launchBB|opsNewPop|popStdSet)$/.test(b.act)); if (p.length) bs = p; } + if (bs.length) press(r.wp(bs, weight), `city ${city}/${tab}`); + if (!div) compare('city button'); + modal(); + } else if (mv === 'openLink') { + if (!s.pops.length) continue; + const a = r.pick(s.pops), b = r() < 0.75 && s.near.length ? r.pick(s.near) : r() < 0.5 && s.pops.length > 1 ? r.pick(s.pops) : r.pick(L.homes); + const bs = screen({ where: 'openLink', a, b }); + note(`openLink ${a}-${b} (${bs.length})`); + if (bs.length) { + // Pick a route or a kind first sometimes (dark fiber, subsea seat, own cable...), then build. + const opt = bs.filter((x) => !/^(buildLink|closeModal)$/.test(x.act)); + if (opt.length && r() < 0.4) { press(r.pick(opt), ' link option'); if (!div) compare('link option'); } + modal(/^(buildLink|pzLink|seaPackage|layShip|buyVxc|dfSys|seaDesign)$/); + } else both('close'); + if (!div) compare('openLink'); + } else if (mv === 'link') { + if (!s.links.length) continue; + const id = r.pick(s.links); + const bs = screen({ where: 'link', id }); + if (bs.length) press(r.wp(bs, weight), 'link ' + id); + if (!div) compare('link button'); + modal(); + } else if (mv === 'dash' || mv === 'left') { + const w = mv === 'dash' ? { where: 'dash', tab: r.pick(DTABS) } : { where: 'left', tab: r.pick(LEFT) }; + const bs = screen(w); + if (bs.length) press(r.wp(bs, weight), mv + ' ' + w.tab); + both('close'); + if (!div) compare(mv + ' button'); + modal(); + } else if (mv === 'direct') { + const [a, b] = both('direct'); + same('direct calls', a, b); + if (a.length) press(r.wp(a, (x) => DW[x.act] || (/^(cancel|rm|drop|unpeer)/.test(x.act) ? 0.4 : 1)), 'direct'); + if (!div) compare('direct'); + modal(); + } else if (mv === 'modal') { + modal(); + } else if (mv === 'poke') { + const k = r.w({ te: 3, teOff: 1, core: 2, fail: 2, event: 3, cash: 3, lvl: 2, ev: 4 }); + let p; + if (k === 'te') { const rules = []; for (let j = 0, n = 1 + r.int(4); j < n; j++) rules.push([r.int(7), r.int(1000), r.int(1000)]); p = [k, rules, r() < 0.85]; } + else p = [k, k === 'cash' ? r.int(3e7) : r.int(1e6), r.int(9)]; + const [e1, e2] = both('poke', p); + same('poke', e1, e2); + note('poke ' + JSON.stringify(p)); + if (!div) compare('poke ' + k); + } else if (mv === 'day') { + const n = 1 + r.int(opts.maxDays || 12); + note('days ' + n); + for (let d = 0; d < n && !div; d++) { + const [e1, e2] = both('day'); + same('day', e1, e2); + days++; + if (!div) compare('day ' + d); + // A window that opened over the day (an offer, a loan, a crisis) gets an answer. + if (!div && base.call('modalOpen')) modal(); + } + } + } + return { seed, start: st, moves: log.length, days, log, div, acts, errors: [...base.errors, ...head.errors] }; +} + +module.exports = { openPage, playGame, rng }; diff --git a/scripts/route-fuzz.js b/scripts/route-fuzz.js new file mode 100644 index 0000000..e202785 --- /dev/null +++ b/scripts/route-fuzz.js @@ -0,0 +1,84 @@ +#!/usr/bin/env node +// Many random games, played move for move on main's page and on this checkout's, compared after every move and day +// (scripts/lib/route-fuzz.js). For refactors that promise identical routing. +// +// node scripts/route-fuzz.js 300 games against origin/main +// node scripts/route-fuzz.js --games 50 --from 1000 games with seeds 1000..1049 +// node scripts/route-fuzz.js --seed 1234 -v one game, every move printed (to replay a divergence) +// node scripts/route-fuzz.js --save tests/fixtures/big-network.json --games 10 --moves 30 from a big network +// node scripts/route-fuzz.js --jobs 2 --base OLD.html --out fuzz.json +// Exit code 1 when any game diverged; each divergence is printed with its seed and its last moves. +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { fork } = require('node:child_process'); + +const args = process.argv.slice(2); +const opt = (k, d) => { const i = args.indexOf(k); return i >= 0 ? args[i + 1] : d; }; +const has = (k) => args.includes(k); + +function child() { + const { openPage, playGame } = require('./lib/route-fuzz'); + const base = openPage(opt('--base'), 'base'), head = openPage(opt('--head'), 'head'); + process.on('message', (m) => { + if (m.stop) process.exit(0); + const t = Date.now(); + let res; + try { res = playGame({ base, head }, m.seed, { moves: m.moves, save: m.save }); } catch (e) { res = { seed: m.seed, div: { where: 'driver crashed', error: String(e.stack || e) }, log: [] }; } + process.send({ seed: m.seed, ms: Date.now() - t, start: res.start, moves: res.moves, days: res.days, div: res.div, acts: res.acts, log: m.verbose || res.div ? res.log : null, errors: res.errors }); + }); + process.send({ ready: true }); +} + +function parent() { + const page = require('./lib/page'); + const tmp = (n) => path.join(os.tmpdir(), `tubes-fuzz-${process.pid}-${n}.html`); + const BASE = opt('--base', null) || (() => { const f = tmp('base'); fs.writeFileSync(f, page.assembleRef(opt('--base-ref', 'origin/main'))); return f; })(); + const HEAD = opt('--head', null) || (() => { const f = tmp('head'); fs.writeFileSync(f, page.assemble().html); return f; })(); + const one = opt('--seed', null); + const from = +opt('--from', 1), games = one ? 1 : +opt('--games', 300); + const seeds = one ? [+one] : Array.from({ length: games }, (_, i) => from + i); + const jobs = Math.max(1, Math.min(+opt('--jobs', Math.max(1, os.cpus().length)), seeds.length)); + const verbose = has('-v'); + const moves = opt('--moves', null) ? +opt('--moves') : undefined; + // --save FILE: every game starts from this save (a plain one, or an export from the menu) instead of a new network. + const RAW = opt('--save', null) ? JSON.parse(fs.readFileSync(opt('--save'), 'utf8')) : null; + const save = RAW ? JSON.stringify(RAW.app === 'series-of-tubes' && RAW.S ? RAW.S : RAW) : undefined; + const results = []; + let next = 0, live = 0; + const t0 = Date.now(); + const done = () => { + const div = results.filter((r) => r.div), tot = (k) => results.reduce((a, r) => a + (r[k] || 0), 0); + const kinds = {}; + for (const r of results) if (r.start) { const k = r.start.kind + (r.start.kind === 'normal' || r.start.kind === 'sandbox' ? ':' + r.start.preset : ''); kinds[k] = (kinds[k] || 0) + 1; } + console.log(`\n${results.length} games, ${tot('moves')} moves, ${tot('days')} days in ${((Date.now() - t0) / 1000).toFixed(0)} s. Starts: ${JSON.stringify(kinds)}`); + const acts = {};for (const r of results) for (const k in r.acts || {}) acts[k] = (acts[k] || 0) + r.acts[k]; + if (has('--acts')) console.log('Actions pressed: ' + Object.entries(acts).sort((a, b) => b[1] - a[1]).map(([k, n]) => k + ' ' + n).join(', ')); + const errs = [...new Set(results.flatMap((r) => r.errors || []))]; + if (errs.length) console.log('Page errors:\n' + errs.slice(0, 10).map((e) => ' ' + e.slice(0, 200)).join('\n')); + if (div.length) { + console.log(`\n${div.length} games diverged:`); + for (const r of div) console.log(JSON.stringify({ seed: r.seed, start: r.start, ...r.div }, null, 1).slice(0, 4000)); + } else console.log('No divergence: identical routing and game state in every game, cached and uncached.'); + const out = opt('--out', null); + if (out) fs.writeFileSync(out, JSON.stringify(results, null, 1)); + process.exit(div.length ? 1 : 0); + }; + for (let j = 0; j < jobs; j++) { + const c = fork(__filename, ['--child', '--base', BASE, '--head', HEAD], { stdio: ['ignore', 'inherit', 'inherit', 'ipc'] }); + live++; + const feed = () => { if (next < seeds.length) c.send({ seed: seeds[next++], moves, verbose, save }); else c.send({ stop: true }); }; + c.on('message', (m) => { + if (!m.ready) { + results.push(m); + const tag = m.div ? 'DIVERGED at ' + m.div.where : 'ok'; + console.log(`seed ${m.seed} ${m.start ? m.start.kind + ' ' + (m.start.id || m.start.preset) + ' ' + m.start.city : ''}: ${m.moves} moves, ${m.days} days, ${(m.ms / 1000).toFixed(1)} s, ${tag}`); + if (verbose && m.log) console.log(m.log.map((l) => ' ' + l).join('\n')); + } + feed(); + }); + c.on('exit', () => { if (--live === 0) done(); }); + } +} + +if (has('--child')) child(); else parent(); diff --git a/tests/route-fuzz.test.js b/tests/route-fuzz.test.js new file mode 100644 index 0000000..5958b27 --- /dev/null +++ b/tests/route-fuzz.test.js @@ -0,0 +1,30 @@ +// A few seeded random games (scripts/lib/route-fuzz.js), played move for move on main's page and on this checkout's: +// after every move and day both route identically and keep the same game state, and this checkout routes the same with +// the routing cache (RC) on and off. scripts/route-fuzz.js plays hundreds of these; here a handful, so CI stays quick. +// Without git's origin/main (a bare tarball), the base is this checkout's own page: the cache on/off check still runs. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const page = require('../scripts/lib/page'); +const { openPage, playGame } = require('../scripts/lib/route-fuzz'); + +function pages() { + const head = path.join(os.tmpdir(), `tubes-fuzz-test-${process.pid}-head.html`); + fs.writeFileSync(head, page.assemble().html); + let base = head; + try { const b = path.join(os.tmpdir(), `tubes-fuzz-test-${process.pid}-base.html`); fs.writeFileSync(b, page.assembleRef('origin/main')); base = b; } catch (e) {} + return { base: openPage(base, 'base'), head: openPage(head, 'head') }; +} + +test('random games route the same as main, with and without the routing cache, move for move', () => { + const P = pages(); + // Seeds that start a normal game, a sandbox, a scenario and a puzzle, with transit, exchanges, PNIs and RouteScript. + for (const seed of [20, 23, 27, 26]) { + const g = playGame(P, seed, { moves: 35 }); + assert.equal(g.div, null, `seed ${seed} diverged: ${JSON.stringify(g.div, null, 1)}`); + assert.ok(g.moves >= 35, `seed ${seed} played its moves`); + assert.deepEqual(g.errors, [], `seed ${seed}: no page errors`); + } +});