Merge the ruleset and four doors into v2

This commit is contained in:
Frederic Jung 2026-10-28 15:45:09 +01:00
commit fa4e30692e
No known key found for this signature in database
43 changed files with 582 additions and 82 deletions

View file

@ -4,8 +4,8 @@ const { load, seed } = require('./harness');
test('daily DDoS spawns share one routing calculation and score their first active day', () => {
const { ev, errors } = load();
// The busier DDoS schedule needs spicy chaos (or gaming customers): stand in for the chaos rule here.
ev("window.rule=k=>k==='chaos'?'spicy':undefined;ACT.start({});S.cash=1e9;S.day=50;S.nextFail=1e9;S.nextEvent=1e9;ACT.launchBB({city:'fra'});S.pops.fra.bb.d=200;ACT.buyTransit({city:'fra',net:'cog',tier:1});compute();window.__compute=compute;window.__random=Math.random;window.__computes=0;compute=()=>{window.__computes++;window.__compute()};Math.random=()=>0;dayTick();compute=window.__compute;Math.random=window.__random");
// The busier DDoS schedule needs spicy chaos (BGP at 3 a.m.) or gaming customers.
ev("ACT.start({});S.rules=makeRules('bgp');S.cash=1e9;S.day=50;S.nextFail=1e9;S.nextEvent=1e9;ACT.launchBB({city:'fra'});S.pops.fra.bb.d=200;ACT.buyTransit({city:'fra',net:'cog',tier:1});compute();window.__compute=compute;window.__random=Math.random;window.__computes=0;compute=()=>{window.__computes++;window.__compute()};Math.random=()=>0;dayTick();compute=window.__compute;Math.random=window.__random");
assert.equal(ev('ddosAll().length'), 2, 'both daily spawn opportunities were used');
assert.equal(ev('window.__computes'), 1, 'daily spawns do not reroute the whole network individually');
assert.ok(ev('ddosAll().every(a=>a.worst===ddosSf(a))'), 'first-day service is recorded after routing');
@ -794,7 +794,7 @@ test('DDoS frequency rises with level and signed gaming customers, with a rollin
ev("S.offers=Array.from({length:50},()=>({type:'game'}));S.biz=[];S.lvl=1");
assert.equal(ev('ddosRate()'), base, 'unsigned offers do not attract attacks');
// Force every random spawn opportunity (with spicy chaos on), then verify each rolling seven-day window.
ev("window.rule=k=>k==='chaos'?'spicy':undefined;");
ev("S.rules=makeRules('bgp');");
ev('window.__starts=[];window.__oldRandom=Math.random;Math.random=()=>0');
for (let day = 50; day < 66; day++) {
ev(`S.day=${day};ddosTick();window.__starts.push(...ddosAll().filter(a=>a.start===S.day).map(a=>a.start))`);
@ -1006,7 +1006,7 @@ test('RackNet and TeamSpooked require signed gaming customers, bypass Flowspec,
test('normal DDoS starts average half an attack per week without gaming and never exceed a seven-per-week expected rate', () => {
const { ev, errors } = load();
ev("window.rule=k=>k==='chaos'?'spicy':undefined;ACT.start({});S.day=50");
ev("ACT.start({});S.rules=makeRules('bgp');S.day=50");
const checks = JSON.parse(ev(`
const oldAttack=ddosAttack,oldRandom=Math.random,out=[];
try {
@ -1121,18 +1121,23 @@ test('the busier DDoS schedule needs spicy chaos or gaming customers; otherwise
ev("ACT.start({});S.cash=1e9;S.day=50;S.lvl=13;S.biz=[];S.nextFail=1e9;S.nextEvent=1e9");
const ticks = () => ev("(()=>{const old=Math.random,oa=ddosAttack;let n=0;ddosAttack=()=>{n++};Math.random=()=>0;try{for(let i=0;i<30;i++)ddosTick();}finally{Math.random=old;ddosAttack=oa;}return n;})()");
assert.equal(ticks(), 0, 'no spicy chaos, no gamers: no extra schedule');
// DDoS stays one of the regular incidents instead: fireFailure can still roll one.
// DDoS stays one of the regular incidents instead: some roll of fireFailure() still starts one.
assert.ok(ev("(()=>{const old=Math.random;try{for(let i=0;i<200;i++){const r=i/200;Math.random=()=>r;S.ev.ddos=null;S.ev.ddosMore=[];S.ddosStarts=[];fireFailure();if(ddosAll().length)return true;}return false;}finally{Math.random=old;}})()"), 'fireFailure can start an attack');
ev("S.ev.ddos=null;S.ev.ddosMore=[];S.ddosStarts=[];S.inc=[]");
ev("S.biz=[{type:'game',city:'fra',bw:5,rate:1,reqs:[]}]");
assert.ok(ticks() > 0, 'gaming customers bring their own attacks');
ev("S.biz=[];window.rule=k=>k==='chaos'?'spicy':'calm'");
ev("S.biz=[];S.rules=makeRules('bgp')");
assert.ok(ticks() > 0, 'spicy chaos brings more attacks');
ev("window.rule=k=>k==='chaos'?'calm':undefined");
assert.equal(ticks(), 0, 'any other chaos setting keeps them as on main');
ev("S.rules=makeRules('freeze')");
assert.equal(ticks(), 0, 'Change Freeze has no attacks on either schedule');
ev("S.biz=[{type:'game',city:'fra',bw:5,rate:1,reqs:[]}]");
assert.equal(ticks(), 0, 'not even with gamers');
assert.ok(ev("(()=>{const old=Math.random;try{for(let i=0;i<200;i++){const r=i/200;Math.random=()=>r;S.ev.ddos=null;S.ev.ddosMore=[];S.ddosStarts=[];fireFailure();if(ddosAll().length)return false;}return true;}finally{Math.random=old;S.inc=[];}})()"), 'and fireFailure never rolls one');
ev("S.rules=makeRules('bgp');S.biz=[]");
assert.ok(ev("(()=>{const old=Math.random;try{for(let i=0;i<200;i++){const r=i/200;Math.random=()=>r;S.ev.ddos=null;S.ev.ddosMore=[];S.ddosStarts=[];fireFailure();if(ddosAll().length)return false;}return true;}finally{Math.random=old;S.inc=[];}})()"), 'spicy attacks come from their own schedule only, never twice');
ev("S.rules=makeRules('oncall')");
// Flood Season, not spicy: one flood at a time, 3 to 7 days apart, however old the scenario is.
ev("delete window.rule;ACT.startScn({id:'flood',sure:1});S.biz=[]");
ev("ACT.startScn({id:'flood',sure:1});S.biz=[]");
const gaps = JSON.parse(ev("(()=>{const o=[];for(const d of [0,1000])for(const r of [0,0.999]){S.day=d;const old=Math.random;Math.random=()=>r;o.push(floodGap());Math.random=old;}return JSON.stringify(o);})()"));
assert.deepEqual(gaps, [3, 7, 3, 7]);
ev("S.scn.nextDdos=S.day;ddosTick()");

View file

@ -484,8 +484,8 @@ test('Flood Season keeps attacking and escalating after both success and timeout
const before = ev('S.day');
ev('S.scn.nextDdos=S.day+1;S.day+=8;dayTick()');
assert.ok(ev('ddosAll().at(-1).start') > before, 'subsequent attacks keep coming');
// Escalating frequency belongs to spicy chaos (stood in for here); calmer games keep the old 3 to 7 days.
ev("window.rule=k=>k==='chaos'?'spicy':undefined");
// Escalating frequency belongs to spicy chaos; calmer games keep the old 3 to 7 days.
ev("S.rules.chaos='spicy'");
const gaps = JSON.parse(ev('const old=Math.random;Math.random=()=>0.5;S.day=0;const early=floodGap();S.day=1000;const late=floodGap();Math.random=old;JSON.stringify([early,late])'));
assert.ok(gaps[1] < gaps[0], 'frequency keeps increasing with scenario age');
const sizes = JSON.parse(ev('const old=Math.random;Math.random=()=>0.9;S.day=0;ddosAttack();const early=ddosAll().at(-1).gbps;S.day=1000;ddosAttack();const late=ddosAll().at(-1).gbps;Math.random=old;JSON.stringify([early,late])'));

191
tests/rules.test.js Normal file
View file

@ -0,0 +1,191 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const start = (ev, o) => ev(`beginGame('Rule Testers','fra',${JSON.stringify(o)});if(UI.pendGame)ACT.pitchSkip()`);
// Days of a quiet-ish network until n failures have fired, on a fixed seed.
const fails = (ev, days) => { ev("S.cash=1e9;ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1});__reseed(4242)"); ev(`for(let i=0;i<${days};i++)dayTick()`); return ev('S.inc.length+S.stats.survived+(S.stats.incidents||0)'); };
test('an old save without rules loads as On-call, a sandbox save as Sandbox', () => {
const { ev, errors } = load();
ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip()");
const old = JSON.parse(ev('JSON.stringify(S)')); delete old.rules;
ev(`S=migrate(JSON.parse(${JSON.stringify(JSON.stringify(old))}))`);
assert.equal(ev('rulePreset()'), 'oncall');
assert.deepEqual(JSON.parse(ev('JSON.stringify(S.rules)')), JSON.parse(ev("JSON.stringify(makeRules('oncall'))")));
assert.equal(ev("rule('chaos')"), 'normal');
assert.equal(ev('chaosMul()'), 1);
old.mode = 'sandbox';
ev(`S=migrate(JSON.parse(${JSON.stringify(JSON.stringify(old))}))`);
assert.equal(ev('rulePreset()'), 'sandbox');
assert.equal(ev("rule('money')"), 'infinite');
assert.deepEqual(errors, []);
});
test('rules survive save and load, and junk is repaired', () => {
const { ev, errors } = load();
start(ev, { mode: 'normal', preset: 'bgp' });
assert.equal(ev('rulePreset()'), 'bgp');
ev("const t=JSON.stringify(packSave(S));S=migrate(JSON.parse(t))");
assert.equal(ev('rulePreset()'), 'bgp');
assert.equal(ev("rule('chaos')"), 'spicy');
ev("S.rules={preset:'freeze',chaos:'apocalypse',lead:42,market:'yes',mods:['ok',7,'<b>']};sanitize(S)");
assert.equal(ev('rulePreset()'), 'freeze');
assert.equal(ev("rule('chaos')"), 'calm', 'a bad value falls back to the preset');
assert.equal(ev("rule('lead')"), 'quick');
assert.equal(ev("rule('market')"), false);
assert.deepEqual(JSON.parse(ev('JSON.stringify(S.rules.mods)')), ['ok']);
ev("S.rules={preset:'nope'};sanitize(S)");
assert.equal(ev('rulePreset()'), 'oncall');
ev("S.rules='hello';sanitize(S)");
assert.equal(ev('rulePreset()'), 'oncall');
// a scenario always runs on On-call, whatever was asked for
ev("beginGame('X','nyc',{scn:SCN.find(s=>s.id==='spat'),preset:'freeze'})");
assert.equal(ev('rulePreset()'), 'oncall');
assert.deepEqual(errors, []);
});
test('the title screen: four doors, three Normal presets, a chaos picker for Sandbox, at 360 px', () => {
const { ev, dom, errors } = load();
const doc = dom.window.document;
ev('window.innerWidth=360;showTitle()');
assert.equal(doc.querySelectorAll('#modal .door').length, 4);
assert.equal(doc.querySelectorAll('#modal .preset').length, 3);
assert.equal(doc.querySelectorAll('#modal .chs').length, 4);
assert.equal(doc.getElementById('doorSandbox').hidden, true);
assert.ok(doc.querySelector('.preset[data-preset=oncall].on'), 'On-call is the default');
doc.querySelector('.preset[data-preset=freeze]').click();
assert.ok(doc.querySelector('.preset[data-preset=freeze].on'));
doc.querySelector('.door[data-door=sandbox]').click();
assert.equal(doc.getElementById('doorSandbox').hidden, false);
assert.equal(doc.getElementById('doorNormal').hidden, true);
doc.querySelector('.chs[data-chaos=spicy]').click();
doc.querySelector('.door[data-door=puzzle]').click();
assert.match(doc.getElementById('toasts').textContent, /coming soon/, 'puzzles are not here yet');
doc.querySelector('.door[data-door=normal]').click();
ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip()");
assert.equal(ev('rulePreset()'), 'freeze');
assert.match(ev("document.getElementById('hAsn').textContent"), /Change Freeze/);
assert.match(ev("document.getElementById('hAsn').title"), /locked/);
ev('showTitle()');
doc.querySelector('.door[data-door=sandbox]').click();
ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip()");
assert.equal(ev('S.mode'), 'sandbox');
assert.equal(ev("rule('chaos')"), 'spicy', 'the sandbox remembers the chaos it was given');
ev('showTitle()');
doc.querySelector('.door[data-door=scn]').click();
assert.match(doc.getElementById('modalCard').textContent, /Weekly challenge/);
assert.deepEqual(errors, []);
});
test('On-call shows no extra tag; the share card names other presets', () => {
const { ev, errors } = load();
start(ev, { mode: 'normal' });
assert.equal(ev('ruleTag()'), '');
assert.match(ev("document.getElementById('hAsn').textContent"), /^AS\d+$/);
start(ev, { mode: 'normal', preset: 'bgp' });
assert.match(ev('ruleTag()'), /BGP at 3/);
ev('ACT.share()');
assert.deepEqual(errors, []);
});
test('Sandbox lays its own subsea cable without being Tier 1, and passes the level gates', () => {
const { ev, errors } = load();
start(ev, { mode: 'sandbox' });
assert.equal(ev('gatesOpen()'), true);
assert.equal(ev('S.won'), false);
assert.equal(ev('lvl()'), ev('LEVELS.length'));
ev("ACT.buildPop({city:'lon'});ACT.buildPop({city:'nyc'});compute()");
assert.equal(ev("ownCabWhy('lon','nyc')"), '');
ev("UI.lmode='df';UI.via=null;openLinkModal('lon','nyc');ACT.ownCab()");
assert.match(ev("document.getElementById('modalCard').textContent"), /frankly rude amount of money/);
ev('ACT.orderCable()');
assert.ok(ev("S.links.some(l=>l.own&&l.sub)"), 'the cable is being laid');
// and the normal game still says Tier 1 only
start(ev, { mode: 'normal' });
assert.match(ev("ownCabWhy('lon','nyc')"), /Tier 1/);
assert.equal(ev('lvl()'), 1);
assert.deepEqual(errors, []);
});
test('Sandbox chaos: none by default, but it can be asked for', () => {
const { ev, errors } = load();
start(ev, { mode: 'sandbox' });
assert.equal(ev("rule('chaos')"), 'none');
ev('S.cash=5;for(let i=0;i<150;i++)dayTick()');
assert.equal(ev('S.stats.incidents'), 0);
assert.ok(ev('S.cash') >= 1e8);
start(ev, { mode: 'sandbox', rules: { chaos: 'spicy' } });
assert.equal(ev("rule('chaos')"), 'spicy');
ev("ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1});for(let i=0;i<200;i++)dayTick()");
assert.ok(ev('S.stats.incidents') > 0, 'spicy sandboxes break');
assert.deepEqual(errors, []);
});
test('Change Freeze breaks less than On-call, BGP at 3 a.m. more, on the same seed', () => {
const n = {};
for (const p of ['freeze', 'oncall', 'bgp']) {
const { ev, errors } = load();
start(ev, { mode: 'normal', preset: p });
n[p] = fails(ev, 720);
assert.deepEqual(errors, []);
}
assert.ok(n.freeze < n.oncall, `freeze ${n.freeze} < on-call ${n.oncall}`);
assert.ok(n.bgp > n.oncall, `bgp ${n.bgp} > on-call ${n.oncall}`);
});
test('chaos stretches spicy outages and leaves On-call ones alone', () => {
const { ev, errors } = load();
start(ev, { mode: 'normal', preset: 'bgp' });
ev("S.day=10;S.pops.fra.down=12;addInc({type:'crash',ref:'fra',until:12,msg:'x',cost:0})");
assert.equal(ev('S.inc[0].until'), 13);
assert.equal(ev('S.pops.fra.down'), 13, 'what it holds down lasts as long');
start(ev, { mode: 'normal' });
ev("S.day=10;S.pops.fra.down=12;addInc({type:'crash',ref:'fra',until:12,msg:'x',cost:0})");
assert.equal(ev('S.inc[0].until'), 12);
assert.deepEqual(errors, []);
});
test('quick lead times: PNIs, dark fiber and own builds arrive in days (#151)', () => {
const { ev, errors } = load();
start(ev, { mode: 'normal', preset: 'freeze' });
ev("S.cash=1e9;S.lvl=4;R.demTo.nfx=1e4;ACT.pni({city:'fra',net:'nfx',tier:1})");
const p = JSON.parse(ev("JSON.stringify(S.pni.find(x=>x.net==='nfx'))"));
assert.ok(p && p.ready - ev('S.day') <= 37, `PNI in days (${p && p.ready - ev('S.day')})`);
assert.ok(ev('dfBuildDays(500)') <= 40);
assert.equal(ev('leadDays(30)'), 3);
// On-call: as quoted
start(ev, { mode: 'normal' });
assert.equal(ev('leadDays(30)'), 30);
assert.equal(ev('dfBuildDays(500)'), 245);
assert.deepEqual(errors, []);
});
test('Change Freeze: no peering inbox, no hardware market, no sold-out routes', () => {
const { ev, errors } = load();
start(ev, { mode: 'normal', preset: 'freeze' });
ev("S.cash=1e9;ACT.buyIx({city:'fra',tier:1,signed:true});for(let i=0;i<60;i++)inboxDay(S.day+i)");
assert.equal(ev('S.inbox.items.length+S.inbox.seq'), 0, 'no mail');
ev('S.day=2000');
assert.equal(ev("hwMul('rt')"), 1, 'no inflation');
assert.equal(ev("hwCard()"), '');
assert.equal(ev("[0,1,2,3,4,5,6].some(t=>CITIES.slice(0,60).some(c=>soldOut('fra',c.id,t)))"), false);
start(ev, { mode: 'normal' });
ev('S.day=2000');
assert.ok(ev("hwMul('rt')") > 1, 'On-call keeps the market');
assert.deepEqual(errors, []);
});
test('no clock: dayTick advances no days, sends no bills, and the speed buttons hide', () => {
const { ev, errors } = load();
start(ev, { mode: 'normal' });
ev("S.rules=makeRules('puzzle');S.mode='puzzle';const c=S.cash;window.__c=c;for(let i=0;i<50;i++)dayTick();renderHUD()");
assert.equal(ev('S.day'), 0);
assert.equal(ev('S.cash'), ev('__c'));
assert.equal(ev("document.getElementById('sp1').hidden"), true);
ev("ACT.speed({v:4})");
assert.notEqual(ev('UI.speed'), 4);
ev("window.innerWidth=360;renderTabbar()");
assert.equal(ev("!!document.querySelector('#tabbar [data-act=phonePlay]')"), false);
assert.deepEqual(errors, []);
});