Chaos monkey: a visitor that snips tubes in On-call and BGP at 3 a.m.

Now and then a chaos monkey moves in for 120 to 240 days and snips a tube every
couple of weeks, weak spots first, instead of the usual random outages. Outlast
it without a customer noticing for a bonus and the Monkey-proof badge, or throw
it a banana. Drawn on the map in shades; gameplay rule F4 with its test.
This commit is contained in:
Frederic Jung 2026-10-30 10:54:25 +01:00
parent 90d4073d17
commit f4486ee955
No known key found for this signature in database
21 changed files with 347 additions and 4 deletions

View file

@ -0,0 +1,99 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
// A small network on a fixed seed: Frankfurt, Amsterdam, London and Paris in a ring, with a spur out to Zurich.
function net(preset) {
const { ev, errors, dom } = load();
ev(`beginGame('Monkey Testers','fra',${JSON.stringify({ mode: 'normal', preset })});if(UI.pendGame)ACT.pitchSkip()`);
ev("S.cash=1e9;S.lvl=4;for(const c of ['ams','lon','par','zrh'])ACT.buildPop({city:c})");
ev("ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1})");
ev("for(const [a,b] of [['fra','ams'],['ams','lon'],['lon','par'],['par','fra'],['fra','zrh']]){UI.pending={a,b};ACT.buildLink({tier:1});}closeModal()");
ev('for(let i=0;i<60;i++)dayTick();S.inc=[];for(const l of S.links)l.cut=0;compute()');
return { ev, errors, dom };
}
const link = (ev, a, b) => `S.links.find(l=>(l.a==='${a}'&&l.b==='${b}')||(l.a==='${b}'&&l.b==='${a}'))`;
test('the chaos monkey visits On-call and BGP at 3 a.m., never Change Freeze or Sandbox', () => {
for (const [preset, ok] of [['oncall', true], ['bgp', true], ['freeze', false]]) {
const { ev, errors } = net(preset);
assert.equal(ev('mkElig()'), ok, preset);
ev('S.monkey.next=S.day;dayTick()');
assert.equal(ev('!!S.monkey.on'), ok, preset);
assert.deepEqual(errors, []);
}
const { ev } = net('oncall');
ev("S.mode='sandbox'");
assert.equal(ev('mkElig()'), false);
ev("S.mode='normal';S.monkey.next=S.day;dayTick();S.scn={rules:{}}");
assert.equal(ev('mkOn()'), false, 'never in a scenario');
});
test('a visit lasts 120 to 240 days, snips every couple of weeks and pauses the usual failures', () => {
const { ev, errors } = net('oncall');
ev('S.monkey.next=S.day;dayTick()');
const len = ev('S.monkey.until-S.monkey.start');
assert.ok(len >= 120 && len <= 240, `visit of ${len} days`);
ev('S.nextFail=S.day');
const nf = ev('S.nextFail');
ev('for(let i=0;i<60;i++)dayTick()');
assert.ok(ev('S.monkey.n') >= 2, 'it snipped');
assert.equal(ev('S.nextFail'), nf, 'no ordinary failure fired while it was here');
assert.ok(ev("S.inc.some(i=>i.type==='cut')||S.stats.survived>0"));
// It leaves on time, and the ordinary failures wait a little before they start again.
ev('for(let i=0;i<200&&S.monkey.on;i++)dayTick()');
assert.equal(ev('S.monkey.on'), 0);
assert.ok(ev('S.monkey.next>S.day+200'));
assert.ok(ev('S.nextFail>=S.day'));
assert.deepEqual(errors, []);
});
test('weak spots are tubes with no other way round', () => {
const { ev } = net('oncall');
assert.equal(ev(`mkWeak(${link(ev, 'fra', 'zrh')})`), true);
assert.equal(ev(`mkWeak(${link(ev, 'fra', 'ams')})`), false);
});
test('outlasting it unnoticed pays a bonus and earns the badge; going home hurt pays nothing', () => {
const { ev, errors } = net('oncall');
ev('S.monkey.next=S.day;dayTick();S.monkey.n=4;S.monkey.hurt=0;S.monkey.until=S.day+1');
const cash = ev('S.cash'), bonus = ev('mkBonus()');
ev('dayTick();checkBadges()');
assert.equal(ev('S.monkey.on'), 0);
assert.equal(ev('S.stats.mkSurv'), 1);
assert.ok(ev('S.cash') >= cash + bonus - 1e6, 'bonus paid');
assert.ok(ev("S.ach.monkey!==undefined"));
ev('S.monkey.next=S.day;dayTick();S.monkey.n=4;S.monkey.hurt=2;S.monkey.until=S.day+1;dayTick()');
assert.equal(ev('S.stats.mkSurv'), 1, 'no bonus when customers felt it');
assert.deepEqual(errors, []);
});
test('a banana sends it home early, for a price', () => {
const { ev, errors, dom } = net('bgp');
ev("S.monkey.next=S.day;dayTick();UI.left='noc';renderLeft()");
assert.ok(dom.window.document.querySelector('[data-act=mkBanana]'), 'the NOC card offers a banana');
const cash = ev('S.cash'), cost = ev('mkBananaCost()');
ev('ACT.mkBanana({})');
assert.equal(ev('S.monkey.on'), 0);
assert.equal(ev('S.cash'), cash - cost);
assert.equal(ev('S.stats.mkSurv||0'), 0);
ev('S.cash=0;S.monkey.next=S.day;dayTick();ACT.mkBanana({})');
assert.equal(ev('S.monkey.on'), 1, 'no banana without the cash');
assert.deepEqual(errors, []);
});
test('saves keep the monkey; old and broken saves are repaired', () => {
const { ev, errors } = net('oncall');
ev('S.monkey.next=S.day;dayTick();const t=JSON.stringify(packSave(S));S=migrate(JSON.parse(t))');
assert.equal(ev('S.monkey.on'), 1);
assert.ok(ev('S.monkey.until>S.day'));
ev("const o=JSON.parse(JSON.stringify(packSave(S)));delete o.monkey;S=migrate(o)");
assert.deepEqual(JSON.parse(ev('JSON.stringify(S.monkey)')), { next: null, on: 0 });
ev("const o=JSON.parse(JSON.stringify(packSave(S)));o.monkey={on:1,until:'soon',n:-3,hurt:99,c:'<b>'};S=migrate(o)");
assert.equal(ev('S.monkey.n'), 0);
assert.equal(ev('S.monkey.hurt'), 0);
assert.equal(ev('S.monkey.c'), ev('S.home'));
assert.ok(ev('S.monkey.until>=S.monkey.start'));
ev('for(let i=0;i<5;i++)dayTick()');
assert.deepEqual(errors, []);
});

View file

@ -361,6 +361,27 @@ test('F3: a specialist path only brings customers you can already serve', () =>
assert.deepEqual(g.errors, []);
});
test('F4: the chaos monkey only visits On-call and BGP at 3 a.m., late, for 120 to 240 days, instead of the ordinary failures', () => {
for (const preset of ['freeze', 'oncall', 'bgp']) {
const g = load();
g.ev(`beginGame('Monkey','fra',{mode:'normal',preset:'${preset}'});if(UI.pendGame)ACT.pitchSkip()`);
g.ev("S.cash=1e9;S.lvl=4;for(const c of ['ams','lon','par'])ACT.buildPop({city:c});for(const [a,b] of [['fra','ams'],['ams','lon'],['lon','par'],['par','fra']]){UI.pending={a,b};ACT.buildLink({tier:1});}closeModal()");
g.ev('for(let i=0;i<2;i++)dayTick()');
if (preset === 'freeze') { assert.equal(g.ev('mkElig()'), false, 'F4: never in Change Freeze'); continue; }
assert.ok(g.ev('S.monkey.next') >= 180, 'F4: not before day 180');
g.ev('S.monkey.next=S.day;dayTick();S.nextFail=S.day');
const len = g.ev('S.monkey.until-S.monkey.start');
assert.ok(len >= 120 && len <= 240, `F4: a visit of ${len} days`);
g.ev('for(let i=0;i<40;i++)dayTick()');
assert.ok(g.ev('S.nextFail') <= g.ev('S.day'), 'F4: no ordinary failure fired during the visit');
assert.ok(g.ev("S.inc.filter(i=>i.type==='cut').every(i=>i.until-i.start<=(rule('chaos')==='spicy'?11:7))"), 'F4: snips heal within a week (spicy stretches by half)');
assert.deepEqual(g.errors, []);
}
const g = load();
g.ev("beginGame('Monkey','fra',{mode:'normal',preset:'oncall'});if(UI.pendGame)ACT.pitchSkip();S.mode='sandbox'");
assert.equal(g.ev('mkElig()'), false, 'F4: never in Sandbox');
});
test('no page errors while checking the rules', () => {
assert.deepEqual(errors, []);
});