Merge v2 into architect: the chaos monkey in every puzzle, and in normal games

Starting a run or a puzzle now sends a monkey left over from an Architect review away.
This commit is contained in:
Frederic Jung 2026-10-30 12:33:33 +01:00
commit dc4c0f07d5
No known key found for this signature in database
28 changed files with 446 additions and 68 deletions

109
tests/chaos-monkey.test.js Normal file
View file

@ -0,0 +1,109 @@
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, []);
});
test('"Met the Chaos Monkey" comes from a visit, not from an ordinary fiber cut', () => {
const { ev, errors } = net('oncall');
ev("fireFailure('cut');checkBadges()");
assert.equal(ev('S.ach.backhoe===undefined'), true, 'a cut alone is not the monkey');
ev('S.monkey.next=S.day;dayTick();checkBadges()');
assert.notEqual(ev('S.ach.backhoe'), undefined);
assert.equal(ev("BADGES.find(b=>b.id==='backhoe').t"), 'Met the Chaos Monkey');
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, []);
});

View file

@ -154,7 +154,7 @@ test('rare badges hook into incidents and deals', () => {
const { ev, errors } = load();
base(ev);
ev("addInc({type:'cut',ref:'x',until:S.day+1,msg:'A crocodile took a bite. A ship dragged its anchor.'});checkGoals()");
assert.ok(ev("S.ach.croc!==undefined&&S.ach.anchor!==undefined&&S.ach.backhoe!==undefined"));
assert.ok(ev("S.ach.croc!==undefined&&S.ach.anchor!==undefined&&S.ach.backhoe===undefined"), "a cut alone earns no Met the Chaos Monkey");
ev("S.stats.survived=100;checkGoals()");
assert.ok(ev("S.ach.unbreak!==undefined"));
assert.equal(ev("BADGES.length>=46&&new Set(BADGES.map(b=>b.id)).size===BADGES.length"), true);

View file

@ -0,0 +1,42 @@
// The chaos monkey in every puzzle: it acts out the N-1 star on the map, and the stars never wait on it in tests.
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const lay = (ev, a, b, via, div = 1) => ev(`if(!S.pops['${a}'])ACT.pzPop({city:'${a}'});ACT.pzLinkTo({a:'${a}',b:'${b}'});ACT.pzVia({v:'${via}'});ACT.pzDiv({v:${div}});ACT.pzLink({tier:1});closeModal()`);
const live = (ev) => ev('UI.drawnAt=performance.now()'); // as if the map were being drawn
test('a new star sets the monkey loose, then the stars show; skipping goes straight to them', () => {
const { ev, errors } = load();
ev("ACT.pzStart({id:'bigboat'})");
live(ev);
lay(ev, 'fra', 'par', 'land'); lay(ev, 'fra', 'par', 'land');
assert.ok(ev('pzEval().goal'), 'delivered');
assert.ok(ev('!!UI.pzMk'), 'the monkey is reviewing the build');
assert.ok(ev('!!UI.mk'), 'and it is on the map');
assert.ok(ev("!!document.querySelector('#pzBox [data-act=pzMkSkip]')"), 'with a skip button');
assert.equal(ev('pzEval().goal'), true, 'its cuts never change the stars');
ev('ACT.pzMkSkip()');
assert.equal(ev('UI.pzMk'), null);
assert.ok(ev('S.links.every(l=>!l.cut)'), 'every tube whole again');
assert.match(ev("document.getElementById('toasts').textContent+document.getElementById('modalCard').textContent"), /⭐/);
assert.ok(ev("!!document.querySelector('#pzBox [data-act=pzMonkey]')"), 'and it can be let loose again');
assert.deepEqual(errors, []);
});
test('the monkey snips each trench in turn, and an edit sends it away', () => {
const { ev, errors } = load();
ev("ACT.pzStart({id:'bigboat'})");
lay(ev, 'fra', 'par', 'land'); lay(ev, 'fra', 'par', 'land');
ev("pzMonkey()");
assert.equal(ev('UI.pzMk.units.length'), 2);
assert.equal(ev('S.links.some(l=>l.cut)'), false, 'nothing cut while it runs');
ev('pzMkHit()');
assert.equal(ev('S.links.filter(l=>l.cut).length'), 1, 'one trench cut when it arrives');
ev('pzMkStep()');
assert.equal(ev('S.links.some(l=>l.cut)'), false, 'and whole again when it runs on');
lay(ev, 'fra', 'par', 'land');
assert.equal(ev('UI.pzMk'), null, 'a change to the build stops the review');
assert.equal(ev('UI.mk'), null);
assert.deepEqual(errors, []);
});

View file

@ -315,18 +315,18 @@ test('results card: stars, the next puzzle, a retry, a share line and stars left
ev("localStorage.removeItem('sot-puzzles');pzBegin(pzDef('eurotrip'))");
build(ev, REF.eurotrip.one);
assert.ok(ev("$('modal').hidden||!document.querySelector('#modalCard .pzres')"), 'two stars: no results card, play goes on');
assert.match(ev("document.getElementById('toasts').textContent"), /One backhoe away/, 'a cheer with the next step instead');
assert.match(ev("document.getElementById('toasts').textContent"), /One chaos monkey away/, 'a cheer with the next step instead');
assert.ok(ev("!!document.querySelector('[data-act=pzFinish]')"), 'and a Finish button on the card');
ev("document.querySelector('[data-act=pzFinish]').click()");
let card = ev("document.getElementById('modalCard').textContent");
assert.match(card, /Your stars so far/);
assert.equal(evalP(ev).stars, 2, 'delivered, and the budget star is on the house');
assert.match(card, /One backhoe away/);
assert.match(card, /One chaos monkey away/);
assert.match(card, /13 ⭐ left in this pack/);
assert.ok(ev("!!document.querySelector('#modalCard [data-act=pzRetry]')"));
build(ev, [['ams', 'lon', 'concerto', 1], ['ams', 'fra', 'land', 1]]);
card = ev("document.getElementById('modalCard').textContent");
assert.match(card, /A backhoe walks into your network/);
assert.match(card, /A chaos monkey walks into your network/);
assert.equal(ev("document.querySelectorAll('#modalCard .pzbig.on').length"), 3);
assert.ok(ev("document.querySelector('#modalCard .btns.cta').classList.contains('pzdone')"));
assert.match(ev("document.querySelector('#modalCard .pznext').textContent"), /Next puzzle →/);