diff --git a/src/game/85-incidents/40-chaos-monkey.js b/src/game/85-incidents/40-chaos-monkey.js index 5519bcf..b2a90ef 100644 --- a/src/game/85-incidents/40-chaos-monkey.js +++ b/src/game/85-incidents/40-chaos-monkey.js @@ -51,6 +51,8 @@ function mkLeave(why){const M=S.monkey,d=S.day,ok=!why&&M.n>0&&M.hurt===0;M.on=0 if(why==='banana')toast(tr('chaos_monkey.bribed'),'good'); else if(ok){const b=mkBonus();S.cash+=b;S.stats.mkSurv=(S.stats.mkSurv||0)+1;toast(tr('chaos_monkey.survived',{n:M.n,cash:fmt$(b)}),'goal');sfx('coin');track('monkey_survived');} else if(!why)toast(tr('chaos_monkey.went_home',{n:M.n,hurt:M.hurt}),''); + // Away, it is only its next visit: what a reload keeps of it (sanitize), so the live game is its own save. + S.monkey={next:M.next,on:0}; if(typeof mkEnd==='function')mkEnd(ok,true);} // The NOC tab's card while it is here. function mkCard(){if(!mkOn())return '';const M=S.monkey,c=mkBananaCost(); diff --git a/src/game/90-actions/30-route-script-and-tools.js b/src/game/90-actions/30-route-script-and-tools.js index 017ebd3..518b8c1 100644 --- a/src/game/90-actions/30-route-script-and-tools.js +++ b/src/game/90-actions/30-route-script-and-tools.js @@ -15,7 +15,7 @@ Object.assign(ACT,{ if(drop>0.1&&UI.slop===src){if(drop>0.3)S.stats.fat=1;toast(tr('route_script_and_tools.commit_complete_traffic_down_p_the',{p:pct(drop)}),'bad');sfx('alert');} else if(drop>0.3){S.stats.fat=1;toast(tr('route_script_and_tools.commit_complete_traffic_down_p_you',{p:pct(drop)}),'bad');sfx('alert');} else{if(saved>0.1&&drop<0.01)S.stats.kiddie=1;toast(`${tr('common.commit_complete')}
${saved>0.01?trn('route_script_and_tools.n_rule_live_transit_bill_down',p.n,{p:pct(saved)}):trn('route_script_and_tools.n_rule_live',p.n)}`,'good');sfx('build');}closeModal();UI.slop=undefined;checkGoals();}, - buyTE:()=>{if(S.te)return;if(lvl()<3){toast(tr('route_script_and_tools.auto_te_unlocks_at_customer_level'),'bad');sfx('err');return;}if(!coreTE()){toast(tr('core.auto_te_needs_an_rsvp_te_or'),'bad');sfx('err');return;}if(!spend(TE_COST))return;S.te={on:true,day:S.day};toast(tr('route_script_and_tools.auto_te_is_live_the_controller'),'good');after();}, + buyTE:()=>{if(S.te)return;if(lvl()<3){toast(tr('route_script_and_tools.auto_te_unlocks_at_customer_level'),'bad');sfx('err');return;}if(!coreTE()){toast(tr('core.auto_te_needs_an_rsvp_te_or'),'bad');sfx('err');return;}if(!spend(TE_COST))return;S.te={on:true,day:S.day,script:''};toast(tr('route_script_and_tools.auto_te_is_live_the_controller'),'good');after();}, teToggle:()=>{if(!S.te)return;S.te.on=!S.te.on;after();}, trace:({city})=>traceModal(city), traceRun:()=>{const f=$('trFrom'),t=$('trTo');traceModal(f&&f.value,t&&t.value);}, diff --git a/tests/backbone-core.test.js b/tests/backbone-core.test.js index f3cea64..6f38984 100644 --- a/tests/backbone-core.test.js +++ b/tests/backbone-core.test.js @@ -52,6 +52,7 @@ test('a new network runs plain IGP: no Auto-TE until the backbone is upgraded, o assert.equal(ev('S.core.run'), 2); ev('ACT.buyTE()'); assert.equal(ev('!!S.te'), true, 'RSVP-TE unlocks Auto-TE'); + assert.equal(ev('JSON.stringify(S.te)'), ev('JSON.stringify(migrate(JSON.parse(JSON.stringify(packSave(S)))).te)'), 'Auto-TE as a reload makes it'); // Customer levels gate each step. ev('S.lvl=3;ACT.coreUp()'); assert.equal(ev('S.core.lv'), 2, 'SR-MPLS needs customer level 5'); diff --git a/tests/chaos-monkey.test.js b/tests/chaos-monkey.test.js index 62ee02c..2d82ed3 100644 --- a/tests/chaos-monkey.test.js +++ b/tests/chaos-monkey.test.js @@ -14,6 +14,9 @@ function net(preset) { 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}'))`; +// The monkey as the game holds it, and as a reload makes it: once it has gone home they are the same (only its next +// visit), so a server that checks every action and day against the save path finds nothing to repair. +const asSaved = (ev) => [ev('JSON.stringify(S.monkey)'), ev('JSON.stringify(migrate(JSON.parse(JSON.stringify(packSave(S)))).monkey)')]; 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]]) { @@ -62,10 +65,16 @@ test('outlasting it unnoticed pays a bonus and earns the badge; going home hurt ev('dayTick();checkBadges()'); assert.equal(ev('S.monkey.on'), 0); assert.equal(ev('S.stats.mkSurv'), 1); + { const [live, saved] = asSaved(ev); assert.equal(live, saved, 'gone home: what a reload keeps'); } 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'); + { const [live, saved] = asSaved(ev); assert.equal(live, saved); } + // Sent home by the rules (chaos turned down mid-visit): the same. + ev("S.monkey.next=S.day;dayTick();S.rules.chaos='calm';dayTick()"); + assert.equal(ev('S.monkey.on'), 0); + { const [live, saved] = asSaved(ev); assert.equal(live, saved, 'sent home quietly'); } assert.deepEqual(errors, []); }); @@ -77,6 +86,7 @@ test('a banana sends it home early, for a price', () => { ev('ACT.mkBanana({})'); assert.equal(ev('S.monkey.on'), 0); assert.equal(ev('S.cash'), cash - cost); + { const [live, saved] = asSaved(ev); assert.equal(live, saved, 'bribed: what a reload keeps'); } 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'); diff --git a/tests/mp-sim-acts.test.js b/tests/mp-sim-acts.test.js index 4f6d48f..c5d968c 100644 --- a/tests/mp-sim-acts.test.js +++ b/tests/mp-sim-acts.test.js @@ -228,14 +228,22 @@ async function play(code, nid, ev, plays) { } const pick = (ev, keys) => JSON.parse(ev(`JSON.stringify(Object.fromEntries(${JSON.stringify(keys)}.filter(k=>UI[k]!==undefined&&UI[k]!==null).map(k=>[k,UI[k]])))`)); -// The gate (5.8 step 6) is right about buyTE: it writes S.te without the script a reload gives it. A todo until the game -// writes it in its saved shape (src/game/90-actions/30-route-script-and-tools.js, buyTE: script:''; see the T2 report). -test('buyTE leaves S.te as a reload makes it', { todo: 'game fix for the integrator: buyTE writes S.te={on:true,day:S.day,script:\'\'}' }, async (t) => { +// The gate (5.8 step 6) passes buyTE and the monkey's banana: the game writes S.te and S.monkey as a reload makes them, +// so the server runs both and leaves the network the page's clicks leave. +test('buyTE and the banana pass the gate, and leave the network the page leaves', async (t) => { const P = page({ save: bigScenic() }); t.after(() => P.close()); const code = 'B0YTE0', nid = 'buyt0001'; + P.ev("S.rules=makeRules('oncall');S.cash=Math.max(S.cash,1e9);S.monkey.next=S.day;mkTick(S.day);closeModal();compute();"); + assert.equal(P.ev('mkOn()'), true, 'the monkey is here'); await loadInto(code, nid, P); - for (const a of ['coreUp', 'coreUp']) checked(await E.act(code, nid, msg(a, {}))); - const r = checked(await E.act(code, nid, msg('buyTE', {}))); - assert.equal(r.outcome, 'applied', r.why); + for (const [act, run] of [['coreUp', 'ACT.coreUp()'], ['coreUp', 'ACT.coreUp()'], ['buyTE', 'ACT.buyTE()'], ['mkBanana', 'ACT.mkBanana({})']]) { + P.ev(run + ';closeModal();'); + const r = checked(await E.act(code, nid, msg(act, {}))); + assert.equal(r.outcome, 'applied', `${act}: ${r.why || ''}`); + const want = P.ev('JSON.stringify(S)'), got = (await E.eval(code, nid, 'return JSON.stringify(S)')).value; + assert.ok(want === got, `${act}:\n${want === got ? '' : differs(want, got)}`); + } + assert.deepEqual(JSON.parse(P.ev('JSON.stringify([!!S.te,S.monkey.on])')), [true, 0]); + assert.deepEqual(P.errors, []); }); diff --git a/tests/mp-sim-determinism.test.js b/tests/mp-sim-determinism.test.js index 4746f08..9ad61fd 100644 --- a/tests/mp-sim-determinism.test.js +++ b/tests/mp-sim-determinism.test.js @@ -23,10 +23,6 @@ const { load } = require('./harness'); const { CODE, MP, TEST, ROOMY, page, bigScenic, msg, ctxOf, begin, RSIG, checked, differs } = require('./lib/mp-sim-play'); const LONG = process.env.SIM_LONG === '1'; -/* Acts the gate refuses because the game leaves a key in a shape sanitize() repairs: game fixes for the integrator (the - T2 report has each patch). Take an entry out once its fix is in; any other refusal fails the audit. - buyTE writes S.te without its script; mkBanana (mkLeave) keeps a finished visit's fields in S.monkey. */ -const PENDING = { buyTE: 'te', mkBanana: 'monkey' }; const E = createEngine({ workers: 2, test: TEST, timeouts: ROOMY }); test.after(() => E.close()); // The big network with everything that keeps state between days on: the intern on every order, the sales desk (rep) @@ -62,9 +58,7 @@ test('realm vs jsdom: 60 days of the scenic big network with actions in between, 15: () => ({ act: 'renameGo', args: { name: 'Tubular Bells' }, page: () => ev("ACT.renameGo({name:'Tubular Bells'})") }), 18: () => ({ act: 'coreUp', args: {}, page: () => ev('ACT.coreUp()') }), 19: () => ({ act: 'coreUp', args: {}, page: () => ev('ACT.coreUp()') }), - // Auto-TE as buyTE makes it once the game writes S.te in its saved shape (script:'', see the T2 report): today the - // gate refuses buyTE, rightly, as a reload adds that script (tests/mp-sim-acts.test.js keeps it as a todo). - 21: () => ({ setup: "S.te={on:true,day:S.day,script:''};" }), + 21: () => ({ act: 'buyTE', args: {}, page: () => ev('ACT.buyTE()') }), 22: () => ({ act: 'teDeploy', args: { script }, ui: { slop: false }, page: () => ev(`ACT.teDeploy({script:${JSON.stringify(script)}})`) }), }; const outcomes = {}; @@ -89,7 +83,7 @@ test('realm vs jsdom: 60 days of the scenic big network with actions in between, } } // The plan really played: the network grew, a link went, the script is live. - for (const k of ['buildPop@5', 'buildLink@6', 'buyTransit@8', 'rmLink@12', 'renameGo@15', 'teDeploy@22']) assert.equal(outcomes[k], 'applied', k); + for (const k of ['buildPop@5', 'buildLink@6', 'buyTransit@8', 'rmLink@12', 'renameGo@15', 'buyTE@21', 'teDeploy@22']) assert.equal(outcomes[k], 'applied', k); assert.equal(ev('S.name'), 'Tubular Bells'); assert.equal(ev('!!(S.te&&S.te.on&&S.te.script)'), true, 'RouteScript is deployed'); assert.equal(ev('S.biz.filter(b=>b.route===\'scenic\').length') > 0, true, 'scenic circuits played'); @@ -329,7 +323,7 @@ test(`timer audit and gate: ${LONG ? 365 : 60} days with everything on, then eve assert.ok(btns.length > 150, `a real sweep: ${btns.length} buttons`); for (const q of ['mktCfg', 'mktHold', 'pathPin']) assert.ok(btns.some(([a]) => a === q), `the crawl found ${q}`); // By default the first three different buttons of each act (about 250); SIM_LONG every one (about 3,000). - const seen = new Set(), per = {}, outs = {}, caught = new Set(); + const seen = new Set(), per = {}, outs = {}; for (const [act, args] of btns) { const k = act + JSON.stringify(args); if (seen.has(k) || (!LONG && (per[act] || 0) >= 3)) continue; @@ -339,12 +333,9 @@ test(`timer audit and gate: ${LONG ? 365 : 60} days with everything on, then eve const r = checked(await S2.act(code, nid, body)); if (r.t === 'err') { fails++; continue; } outs[r.outcome] = (outs[r.outcome] || 0) + 1; - const g = /^sanitize would repair (\w+): (.*)$/.exec(r.why || ''); - if (g && PENDING[act] === g[2]) { caught.add(act); continue; } - assert.equal(g, null, `the gate rolled back ${act} ${JSON.stringify(args)}: ${r.why}`); + assert.ok(!/^sanitize would repair/.test(r.why || ''), `the gate rolled back ${act} ${JSON.stringify(args)}: ${r.why}`); if (r.outcome === 'error') assert.fail(`${act} ${JSON.stringify(args)}: ${r.why}`); } - if (caught.size) t.diagnostic(`the gate refused, rightly, until the game writes these keys as a reload does (T2 report): ${[...caught].map((a) => a + ' ' + PENDING[a]).join(', ')}`); assert.equal(fails, 0, 'no action timed out or broke its pipeline'); assert.ok(outs.applied > 20, `actions applied: ${JSON.stringify(outs)}`); [n, at] = await timers();