// The build standard: one target state for every POP, port and fiber, set in its own sheet, applied to new POPs at // once, brought up by the intern's "Keep to the build standard" order or in one tap, and priced before anything changes. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { load } = require('./harness'); const { lit } = require('../scripts/lib/js-literal'); // Frankfurt (one Edge Lord, broadband, 100G Cogentle transit) and Amsterdam (one router, nothing on it yet), level 7, // PNIs up the moment they are ordered and Change Freeze (no fat fingers, no dice). const BASE = "ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;S.lvl=7;S.nextFail=S.nextEvent=1e9;UI.speed=0;S.rules=makeRules('freeze');S.rules.pnis='instant';" + "ACT.launchBB({city:'fra'});S.pops.fra.rgen=1;ACT.buyTransit({city:'fra',net:'cog',tier:1});ACT.buildPop({city:'ams'});compute()"; const AU = "RG.findIndex(r=>r.n==='Absolute Unit 3.2T')"; // Kevin by default; a fiber test that counts swaps a night hires Annie (Kevin does two: tests/bstd-owner.test.js). const hire = (ev, id = 'kevin') => ev(`ACT.opsHire({id:'${id}'});S.ops.floor=0;S.ops.cap.std=0;S.ops.on.std=true;closeModal()`); const shift = ev => ev('opsDay(S.day)'); function game(t, extra = '') { const h = load(); t.after(() => h.dom.window.close()); h.ev(BASE); if (extra) h.ev(extra); return h; } const pop = (ev, c) => JSON.parse(ev(`JSON.stringify(S.pops.${c})`)); const std = (ev, d) => ev(`S.popStd=${lit(d)}`); // A leased dark fiber between two POPs, built and lit with these optics. const fiber = (ev, a, b, ch, sys = 'lite') => ev(`(()=>{const id=uid();S.links.push({id,a:'${a}',b:'${b}',kind:'df',own:false,sys:'${sys}',ch:${lit(ch)},km:Math.round(kmBetween('${a}','${b}')),sub:false,cable:null,mo:1000,trench:id,cut:0,day:S.day,ready:S.day,spares:false});return id;})()`); test('the POP part: model, routers, second building and generator, the busiest POP first, at exactly what the plan priced', t => { const { ev, errors } = game(t); hire(ev); std(ev, { g: ev(AU), rt: 2, site2: true, gen: true }); const q = JSON.parse(ev('JSON.stringify(bstdQuote(true))')), c0 = ev('S.cash'); assert.equal(q.pops, 2); shift(ev); for (const c of ['fra', 'ams']) { const p = pop(ev, c); assert.deepEqual([p.rgen, p.routers, !!p.site2, !!p.gen], [ev(AU), 2, true, true], c); } assert.equal(c0 - ev('S.cash'), q.cost, 'the shift paid what the plan said'); // Frankfurt has one router carrying broadband: a second went in first, so the swap rolled through. assert.equal(ev("S.inc.some(i=>i.type==='maint')"), false, 'no maintenance window'); assert.match(ev('opsLine(S.ops.log[0])'), /build standard/); assert.match(ev('opsLine(S.ops.log[0])'), /Frankfurt/); assert.equal(ev('opsWhereOk(S.ops.log[0].r[0][2][0])'), true, 'the log survives a save'); assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdPlan(true))')), [], 'nothing left to do'); // Its monthly books include the building and the generator now. ev('compute()'); assert.equal(ev('opsMoNow()') > ev('2*(SITE2_MO+GEN_MO)'), true); assert.deepEqual(errors, []); }); test('a busy POP with one router never gets a maintenance window; never a step down; the core keeps its routers', t => { const { ev, errors } = game(t); hire(ev); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); // A small budget: only the extra router fits this month, so the swap waits for it (and never goes ahead alone). std(ev, { g: chonk, rt: 1 }); ev(`S.ops.cap.std=OPS_CAPS.find(c=>c>=routerCost(1,1)&&cx[0]==='broke')"), 'the log says the budget ran out'); ev('{const m0=monthIdx(S.day);while(monthIdx(S.day)===m0)S.day++;}S.ops.cap.std=0;compute();opsDay(S.day)'); assert.deepEqual([pop(ev, 'fra').routers, pop(ev, 'fra').rgen], [2, chonk], 'then the swap rolls through'); assert.equal(ev("S.inc.some(i=>i.type==='maint')"), false); // A smaller model as the standard: nobody is swapped back. std(ev, { g: 1, rt: 1 }); ev('S.day++;compute();opsDay(S.day)'); assert.equal(pop(ev, 'fra').rgen, chonk); // A POP in the backbone core is never swapped onto a model that cannot run it (the Whitebox 64 has no label stack for // RSVP-TE): the best main-line model no bigger than it that can, instead. const sonic = ev("RG.findIndex(r=>r.n==='Sonic the Whitebox 64')"); ev(`S.pops.ams.rgen=RG.findIndex(r=>r.n==='Arrrhipster 7280R');S.pops.ams.routers=2`); std(ev, { g: sonic, rt: 1 }); assert.equal(ev("bstdModel(S.popStd.g,'ams')"), sonic, 'a plain IGP backbone takes any model'); ev('S.core={lv:2,run:2}'); assert.ok(ev("inCore('ams')")); assert.equal(ev("bstdModel(S.popStd.g,'ams')"), chonk, 'RSVP-TE in the core: the Chonk 800 instead'); assert.deepEqual(errors, []); }); test('budget, cash floor, per-POP exceptions; without an intern nothing runs and no dice are rolled', t => { const { ev, errors } = game(t); std(ev, { rt: 2, gen: true }); ev('S.ops.on.std=true'); const r = ev('window.__n=0;window.__r=Math.random;Math.random=()=>{__n++;return __r()};const s=JSON.stringify(S);opsDay(S.day);Math.random=__r;s===JSON.stringify(S)&&__n===0'); assert.equal(r, true, 'no intern: nothing happens, nothing random'); hire(ev); // Amsterdam is excepted: the intern never touches it. ev("S.ops.skip.ams=['std']"); // The floor: only what leaves the floor clear. A generator is $12k, a router more. ev('S.ops.floor=50000;S.cash=50000+GEN_COST+1'); shift(ev); assert.deepEqual([pop(ev, 'fra').gen, pop(ev, 'fra').routers], [true, 1], 'the generator fit above the floor, the router did not'); assert.ok(ev('S.cash') >= 50000, 'never below the floor'); assert.deepEqual([pop(ev, 'ams').gen, pop(ev, 'ams').routers], [false, 1], 'the excepted POP is untouched'); assert.equal(ev("bstdQuote(true).pops"), 1, 'the intern\'s own view leaves the exception out'); assert.equal(ev("bstdQuote(false).pops"), 2, 'the sheet still shows it below the standard'); // A monthly cap of setup fees. ev("S.cash=1e8;S.ops.floor=0;S.ops.cap.std=10000;S.day++;opsDay(S.day)"); assert.equal(pop(ev, 'fra').routers, 1, 'a router is more than the budget'); assert.deepEqual(errors, []); }); test('the bank that forbids generators gets none; DE-KICKS is skipped; an exchange port where there is one', t => { const { ev, dom, errors } = game(t); hire(ev); ev("S.loans.push({id:'L1',bank:'storm',amount:1e6,covs:['nogen'],apr:0.05,term:24,pay:0,start:S.day,left:24})"); std(ev, { gen: true, ix: true }); assert.equal(ev('popStdQuote()'), null, 'a new POP is not quoted a generator either'); shift(ev); assert.equal(pop(ev, 'fra').gen, false); assert.equal(pop(ev, 'ams').gen, false); assert.equal(ev("COVS.nogen.ok()"), true, 'the covenant holds'); // Frankfurt's exchange is DE-KICKS: that wants a sales call (a quote window), so the intern leaves it alone. assert.equal(ev('isDk("fra")'), true); assert.equal(ev('S.ixp.fra'), undefined); assert.equal(ev('!!(S.dk&&S.dk.q)'), false, 'no DE-KICKS quote was asked for'); assert.equal(dom.window.document.getElementById('modal').hidden, true, 'and no window opened'); assert.equal(ev('S.ixp.ams'), 0, 'Amsterdam gets a 10G port'); ev('ACT.bstdApply()'); assert.equal(pop(ev, 'fra').gen, false, 'the button respects the covenant too'); assert.deepEqual(errors, []); }); test('ports: LAG members up to the standard, split over both buildings; PNIs spread to more cities; never fewer members', t => { const { ev, errors } = game(t); hire(ev); const net = ev("NETS.find(n=>n.pops.includes('fra')&&n.pops.includes('ams')&&pniMin(n.id)===0&&peerOk(n.id,'pni','ams').ok&&peerOk(n.id,'pni','fra').ok).id"); ev(`S.pops.fra.site2=true;S.pni.push({id:'P1',city:'fra',net:${lit(net)},tier:1,ready:S.day});ACT.buyIx({city:'ams',tier:1});(S.ixn=S.ixn||{}).ams=4;compute()`); // The PNI list says a second one in Amsterdam is worth it. ev(`const _v=pniVal;pniVal=(id,c)=>id===${lit(net)}&&c==='ams'?{..._v(id,c),worth:true,want:40,tier:1}:_v(id,c)`); std(ev, { lag: 2, split: true, pni: 2 }); shift(ev); const t0 = JSON.parse(ev('JSON.stringify(S.transit[0])')); assert.deepEqual([t0.n, t0.site], [2, 'ab'], 'transit: a second member, split over both buildings'); const p = JSON.parse(ev(`JSON.stringify(S.pni.find(x=>x.id==='P1'))`)); assert.deepEqual([p.n, p.site], [2, 'ab'], 'the PNI too'); assert.equal(ev('S.ixn.ams'), 4, 'a bigger LAG stays as it is'); assert.equal(ev(`S.pni.filter(x=>x.net===${lit(net)}).length`), 2, 'a PNI in a second city'); assert.ok(ev(`S.pni.some(x=>x.net===${lit(net)}&&x.city==='ams')`)); assert.equal(ev("S.ixSite&&S.ixSite.ams"), undefined, 'Amsterdam has one building: nothing to split'); assert.deepEqual(errors, []); }); test('fiber: the line system only on a quiet fiber or with a way around, one a shift; faster optics, old ones on the shelf; bigger waves', t => { const { ev, errors } = game(t); hire(ev, 'annie'); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); ev(`ACT.buildPop({city:'par'});for(const c of ['fra','ams','par']){S.pops[c].rgen=${chonk};S.pops[c].routers=4;}`); const a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']), b = fiber(ev, 'fra', 'par', ['zr4']); ev("compute();for(const l of S.links)R.load[l.id]=0"); std(ev, { sys: 'roadm', opt: 'fast' }); const cap0 = ev(`S.links.find(l=>l.id===${lit(a)}).ch.reduce((s,o)=>s+optG(o,S.links.find(l=>l.id===${lit(a)})),0)`); shift(ev); const L = id => JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(id)}))`)); assert.deepEqual([L(a).sys, L(b).sys].sort(), ['lite', 'roadm'], 'one fiber goes dark per shift'); const up = L(a).sys === 'roadm' ? a : b, other = up === a ? b : a; assert.equal(L(up).eqDown, ev('S.day+1')); assert.ok(L(up).ch.every(o => o === 'tp16'), 'its optics are the fastest that reach on the new line system: ' + L(up).ch); // The other one only waits its turn: no optics in between that its own swap would put on the shelf a day later. assert.ok(L(other).ch.every(o => o === 'zr4'), 'a quiet fiber waiting its turn keeps its optics: ' + L(other).ch); assert.equal(ev('S.spares.zr4'), L(up).ch.length, 'the old optics of the swapped fiber went on the shelf'); assert.equal(ev('S.spares.zr8'), undefined, 'and no stand-in optics were bought'); // Busy and alone between its cities: it keeps its line system, and gets the fastest optics that one takes. ev(`S.day+=2;compute();R.load[${lit(other)}]=capOf(S.links.find(l=>l.id===${lit(other)}))*0.9;opsDay(S.day)`); assert.equal(L(other).sys, 'lite', 'not while it is busy and alone'); assert.ok(L(other).ch.every(o => o === 'zr8'), 'the fastest the old line system takes: ' + L(other).ch); assert.equal(ev('S.spares.zr4'), 3, 'all the old optics went on the shelf'); // A parallel wave with room carries its load: now it can go dark for a day, and gets the bigger optics. ev(`const o=S.links.find(l=>l.id===${lit(other)});UI.lmode='wave';UI.pending={a:o.a,b:o.b};ACT.buildLink({tier:3});S.day++;compute();R.load[o.id]=capOf(o)*0.9;for(const l of S.links)if(l.kind==='wave')R.load[l.id]=0;opsDay(S.day)`); assert.equal(L(other).sys, 'roadm'); assert.ok(L(other).ch.every(o => o === 'tp16'), L(other).ch); assert.equal(ev('S.spares.zr8'), L(other).ch.length); const cap1 = ev(`S.links.find(l=>l.id===${lit(a)}).ch.reduce((s,o)=>s+optG(o,S.links.find(l=>l.id===${lit(a)})),0)`); assert.ok(cap1 > cap0, `capacity went up: ${cap0} → ${cap1}`); // Waves: a bigger one than the standard stays as it is; a smaller one goes up. const w = ev("S.links.find(l=>l.kind==='wave').id"); assert.equal(L(w).tier, 3); std(ev, { wv: 2 }); ev('S.day++;compute();opsDay(S.day)'); assert.equal(L(w).tier, 3, 'an 800G wave is never made smaller'); std(ev, { wv: 3 }); ev(`S.links.find(l=>l.id===${lit(w)}).tier=1;S.day++;compute();opsDay(S.day)`); assert.equal(L(w).tier, 3, 'a 100G wave goes up to 800G'); assert.deepEqual(errors, []); }); test('quiet fibers take their line system one a shift and buy nothing in between: the intern spends what the quote said', t => { const { ev, errors } = game(t); hire(ev, 'annie'); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); ev(`ACT.buildPop({city:'par'});ACT.buildPop({city:'lon'});for(const c of ['fra','ams','par','lon']){S.pops[c].rgen=${chonk};S.pops[c].routers=4;}`); const ids = ['ams', 'par', 'lon'].map(c => fiber(ev, 'fra', c, ['zr4', 'zr4'])); const quiet = 'compute();for(const l of S.links)R.load[l.id]=0'; ev(quiet); std(ev, { sys: 'roadm', opt: 'fast' }); const q = JSON.parse(ev('JSON.stringify(bstdQuote(true))')), c0 = ev('S.cash'); assert.deepEqual([q.links, q.held], [3, []]); for (let d = 1; d <= 3; d++) { ev(`${quiet};opsDay(S.day);S.day++`); const ls = ids.map(id => JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(id)}))`))); assert.equal(ls.filter(l => l.sys === 'roadm').length, d, 'one more fiber a shift'); for (const l of ls) assert.deepEqual(l.ch, l.sys === 'roadm' ? ['tp16', 'tp16'] : ['zr4', 'zr4'], 'tp16 on the new line system, the old optics until then'); assert.equal(ev('S.spares.zr8'), undefined, 'no optics bought only to be shelved a day later'); } assert.equal(c0 - ev('S.cash'), q.cost, 'three shifts spent exactly the quote'); assert.deepEqual(errors, []); }); test('an optics swap is checked at what it costs when it runs: a refused job before it never lets one past the floor or the cap', t => { const { ev, errors } = game(t); hire(ev); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); ev(`ACT.buildPop({city:'par'});for(const c of ['fra','ams','par']){S.pops[c].rgen=${chonk};S.pops[c].routers=4;}`); // Paris's fiber is the hotter one: its zr8s go to tp16 first and onto the shelf, where Amsterdam's swap expects them. const b = fiber(ev, 'fra', 'par', ['zr8', 'zr8'], 'roadm'), a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']); const loads = `compute();for(const l of S.links)R.load[l.id]=0;R.load[${lit(b)}]=50;`; ev(`S.spares={};${loads}`); std(ev, { opt: 'fast' }); assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdPlan(true).map(j=>[j.w,j.cost]))')), [['fra|par', 140000], ['fra|ams', 0]], 'priced as if the first one ran'); const ch = id => JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(id)}).ch)`)); // The first is more than the budget: the second is not free then, and the cap and the floor see its real price. ev(`S.ops.cap.std=10000;S.ops.floor=50000;S.cash=80000;S.ops.spent={};${loads}opsDay(S.day)`); assert.equal(ev('S.cash'), 80000, 'nothing spent past the cap or the floor'); assert.deepEqual([ch(a), ch(b)], [['zr4', 'zr4'], ['zr8', 'zr8']]); assert.ok(ev("S.ops.log[0].r.some(x=>x[0]==='broke')"), 'the log says the money ran out'); ev(`S.ops.cap.std=0;S.day++;${loads}opsDay(S.day)`); assert.equal(ev('S.cash'), 80000, 'no cap: the floor alone still holds'); // Exactly enough above the floor for Amsterdam's own zr8s, bought new: it goes ahead, down to the floor and no further. ev(`S.cash=50000+2*optCost('zr8');S.day++;${loads}opsDay(S.day)`); assert.deepEqual(ch(a), ['zr8', 'zr8']); assert.equal(ev('S.cash'), 50000); // The button checks the price at the click too: never more than the cash in hand. std(ev, { opt: 'fast' }); ev(`S.spares={};S.links.find(l=>l.id===${lit(a)}).ch=['zr4','zr4'];S.cash=30000;${loads}ACT.bstdApply()`); assert.deepEqual(ch(a), ['zr4', 'zr4'], 'two zr8 at $44k do not fit $30k'); assert.equal(ev('S.cash'), 30000); assert.deepEqual(errors, []); }); test('a busy fiber with nothing parallel keeps its line system: the sheet, the button and the MCP quote say what the click buys', t => { const h = load({ agent: true }); t.after(() => h.dom.window.close()); const { ev, errors } = h; ev(BASE); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); ev(`for(const c of ['fra','ams']){S.pops[c].rgen=${chonk};S.pops[c].routers=4;}`); const a = fiber(ev, 'fra', 'ams', Array(6).fill('zr4')); const busy = `compute();R.load[${lit(a)}]=capOf(S.links.find(l=>l.id===${lit(a)}))*0.9;`; // tp16 on the shelf: the quote must not count on a line-system swap that will not happen today. ev(`S.spares={tp16:6};${busy}`); std(ev, { sys: 'roadm', opt: 'fast' }); const q = JSON.parse(ev('JSON.stringify(bstdQuote(false))')); assert.deepEqual([q.held, q.links, q.n], [['fra|ams'], 1, 1]); assert.equal(q.cost, 6 * ev("optCost('zr8')"), 'six zr8 for the line system it keeps'); ev(`closeModal();${busy}`); const dry = JSON.parse(ev("JSON.stringify(apiRun('bstdApply',{},{dryRun:true}))")); assert.equal(dry.quote.setup, q.cost, 'the MCP quote is the same bill'); assert.match(dry.quote.blocked[0], /^Frankfurt–Amsterdam: its line system waits/); ev(`${busy}ACT.bstdOpen()`); const doc = h.dom.window.document; assert.match(doc.getElementById('modalCard').textContent, /One fiber keeps its line system for now/); assert.equal(doc.querySelector('#modalCard [data-act="bstdApply"]').textContent, `Bring everything up now · ${ev(`fmt$(${q.cost})`)}`); const c0 = ev('S.cash'); ev(`${busy}document.querySelector('#modalCard [data-act="bstdApply"]').click()`); assert.equal(c0 - ev('S.cash'), q.cost, 'the click spent the quote'); const l = JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(a)}))`)); assert.deepEqual([l.sys, l.ch], ['lite', Array(6).fill('zr8')], 'and bought what it listed'); assert.deepEqual(JSON.parse(ev('JSON.stringify(S.spares)')), { tp16: 6, zr4: 6 }); // Two busy fibers that carry each other: one goes dark, the other waits for tomorrow, and the quote knew. const b = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']); ev(`S.links.find(l=>l.id===${lit(a)}).ch=['zr4','zr4']`); const both = `compute();for(const id of [${lit(a)},${lit(b)}])R.load[id]=capOf(S.links.find(l=>l.id===id))*0.4;`; ev(both); const q2 = JSON.parse(ev('JSON.stringify(bstdQuote(false))')), c1 = ev('S.cash'); assert.deepEqual([q2.links, q2.held.length], [2, 1]); ev(`${both}ACT.bstdApply()`); assert.equal(c1 - ev('S.cash'), q2.cost); assert.deepEqual(JSON.parse(ev(`JSON.stringify([${lit(a)},${lit(b)}].map(id=>S.links.find(l=>l.id===id).sys).sort())`)), ['lite', 'roadm'], 'never both dark on the same day'); assert.deepEqual(errors, []); }); test('optics a new router model takes are on the bill with it, and wait for its swap', t => { const { ev, errors } = game(t); // Edge Lords take no zr8: the Absolute Unit does. ev('S.pops.ams.rgen=1;S.pops.fra.routers=2;S.pops.ams.routers=2'); const a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']); ev('compute()'); assert.equal(ev("RG[1].cls[j.k,j.w,j.dep?j.dep.length:0]))")); assert.deepEqual(plan.filter(j => j[0] === 'opt'), [['opt', 'fra|ams', 2]], 'the optics wait for both swaps'); // The intern: the swaps are over the budget, so the optics are not pulled out and put back. hire(ev); ev('S.ops.cap.std=50000;compute();opsDay(S.day)'); assert.deepEqual(JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(a)}).ch)`)), ['zr4', 'zr4']); assert.ok(!(ev('S.ops.spent.std') > 0)); // The button: the swaps and the optics, exactly the quote, and nothing left over. ev('S.ops.on.std=false;compute()'); const q = JSON.parse(ev('JSON.stringify(bstdQuote(false))')), c0 = ev('S.cash'); ev('ACT.bstdApply()'); assert.equal(c0 - ev('S.cash'), q.cost); assert.deepEqual(JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(a)}).ch)`)), ['zr8', 'zr8']); assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdQuote(false))')), { cost: 0, mo: 0, pops: 0, ports: 0, links: 0, n: 0, held: [] }, 'one tap brought everything up'); assert.deepEqual(errors, []); }); test('waves the decom order is about to hand back are never upgraded first', t => { const { ev, errors } = game(t); hire(ev); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); ev(`for(const c of ['fra','ams']){S.pops[c].rgen=${chonk};S.pops[c].routers=4;}`); ev("UI.lmode='wave';UI.pending={a:'fra',b:'ams'};ACT.buildLink({tier:3});for(let i=0;i<2;i++){UI.pending={a:'fra',b:'ams'};UI.route='diverse';ACT.buildLink({tier:1});}compute()"); const waves = () => JSON.parse(ev("JSON.stringify(S.links.filter(l=>l.kind==='wave').map(l=>l.tier))")); assert.deepEqual(waves(), [3, 1, 1]); std(ev, { wv: 2 }); assert.equal(ev('bstdQuote(true).n'), 2, 'without decom both small waves go up'); ev('S.ops.on.decom=true'); const q = JSON.parse(ev('JSON.stringify(bstdQuote(true))')); assert.equal(q.n, 1, 'decom keeps one spare in another trench and hands the other back: only the spare goes up'); const c0 = ev('S.cash'); ev('S.day++;compute();opsDay(S.day)'); assert.deepEqual(waves(), [3, 2], 'the spare went up, the other went back'); assert.equal(c0 - ev('S.cash'), q.cost, 'no month of rent paid on a wave that went back'); assert.deepEqual(JSON.parse(ev('JSON.stringify(S.ops.log[0].r.map(x=>[x[0],x[1]]))')), [['std', 1], ['decom', 1]]); assert.deepEqual(errors, []); }); test('a new POP gets the whole POP standard, priced on the button; the template\'s kit never pays for it twice', t => { const { ev, dom, errors } = game(t); std(ev, { g: ev(AU), rt: 2, site2: true, gen: true, lag: 2 }); const q = JSON.parse(ev('JSON.stringify(popStdQuote())')); assert.deepEqual([q.g, q.rt, q.site2, q.gen], [ev(AU), 2, true, true]); ev("selectCity('par');renderPanel()"); assert.match(dom.window.document.getElementById('panelBody').textContent, /Includes your build standard: 2 × Absolute Unit 3\.2T, second building, generator/); let c0 = ev('S.cash'); ev("ACT.buildPop({city:'par'})"); assert.equal(c0 - ev('S.cash'), ev("popCost('par')") + q.cost, 'the button\'s price is what it costs'); const p = pop(ev, 'par'); assert.deepEqual([p.rgen, p.routers, p.site2, p.gen], [ev(AU), 2, true, true]); // The template: broadband, transit, an IX port and the kit, all with the standard. The kit has nothing left to add. hire(ev); const tq = JSON.parse(ev("JSON.stringify(opsTplQuote('lon'))")); assert.equal(tq.kit, false, 'the standard already has the generator, the building and the second router'); assert.ok(tq.parts.includes('build standard')); c0 = ev('S.cash'); ev("ACT.opsNewPop({city:'lon'})"); assert.equal(c0 - ev('S.cash'), tq.cost, 'the template costs what it quoted'); assert.equal(pop(ev, 'lon').gen, true); // Without a generator in the standard, the kit adds just that. std(ev, { g: ev(AU), rt: 2, site2: true }); const tq2 = JSON.parse(ev("JSON.stringify(opsTplQuote('mad'))")); assert.equal(tq2.kit, true); c0 = ev('S.cash'); ev("ACT.opsNewPop({city:'mad'})"); assert.equal(c0 - ev('S.cash'), tq2.cost); assert.deepEqual([pop(ev, 'mad').gen, pop(ev, 'mad').routers], [true, 2]); assert.deepEqual(errors, []); }); test('"Bring everything up now" spends what the quote said and does what it listed; the quote changes nothing', t => { const { ev, errors } = game(t); ev(`S.pops.fra.routers=2;S.pops.fra.site2=true;ACT.buyIx({city:'ams',tier:0})`); const chonk = ev("RG.findIndex(r=>r.n==='Chonk 800')"); ev(`for(const c of ['fra','ams'])S.pops[c].rgen=${chonk}`); fiber(ev, 'fra', 'ams', ['zr4']); ev("compute()"); std(ev, { g: ev(AU), rt: 3, site2: true, gen: true, lag: 2, split: true, ix: true, sys: 'roadm', opt: 'fast' }); const s0 = ev('JSON.stringify(S)'); const q = JSON.parse(ev('JSON.stringify(bstdQuote(false))')); ev('ACT.bstdOpen()'); assert.equal(ev('JSON.stringify(S)'), s0, 'the quote and the sheet leave the game alone'); assert.ok(q.n > 0 && q.pops === 2 && q.ports >= 2 && q.links === 1, JSON.stringify(q)); const c0 = ev('S.cash'); ev("document.querySelector('#modalCard [data-act=\"bstdApply\"]').click()"); assert.equal(c0 - ev('S.cash'), q.cost, 'it cost exactly the quote'); assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdQuote(false))')), { cost: 0, mo: 0, pops: 0, ports: 0, links: 0, n: 0, held: [] }, 'and everything is up to the standard'); assert.match(ev("document.getElementById('toasts').textContent"), /jobs done for the build standard/); assert.match(ev("document.getElementById('modalCard').textContent"), /Everything meets the standard/); assert.equal(ev('S.ixSite.ams'), 'ab'); assert.deepEqual(errors, []); }); test('saves: an old {g, rt} standard loads unchanged and still means the same; junk is repaired; none stays none', t => { const { ev, errors } = game(t); const back = d => ev(`(()=>{const s=JSON.parse(JSON.stringify(S));s.popStd=${d};sanitize(s);return JSON.stringify(s.popStd);})()`); assert.deepEqual(JSON.parse(back(`{g:${ev(AU)},rt:2}`)), { g: ev(AU), rt: 2 }, 'an old standard is exactly the routers it was'); assert.equal(back('undefined'), 'null', 'an old save without one'); assert.equal(back('null'), 'null'); assert.equal(back("'x'"), 'null'); assert.deepEqual(JSON.parse(back("{g:'x',rt:'7',site2:'yes',gen:true,lag:3,split:1,ix:true,pni:9,sys:'warp',opt:'fast',wv:99}")), { rt: 7, gen: true, ix: true, opt: 'fast' }, 'each odd value reads as keep'); assert.equal(back('{g:999}'), 'null', 'nothing left is no standard'); // The old meaning: a new POP gets those routers, and nothing else is done anywhere unless the order is switched on. ev(`S=migrate(JSON.parse(JSON.stringify(Object.assign(S,{popStd:{g:${ev(AU)},rt:2}}))));delete S.ops.on.std;S=migrate(JSON.parse(JSON.stringify(S)))`); assert.equal(ev('S.ops.on.std'), false, 'an old save has the order off'); ev("ACT.buildPop({city:'par'})"); assert.deepEqual([pop(ev, 'par').rgen, pop(ev, 'par').routers, !!pop(ev, 'par').site2, !!pop(ev, 'par').gen], [ev(AU), 2, false, false]); // A model smaller than the basic router (the Mikrowtik has more ports) is still what a new POP gets, as it always was. const mk = ev("RG.findIndex(r=>r.n==='Mikrowtik CCR')"); assert.ok(ev(`RG[${mk}].cap { for (const lang of ['en', 'de', 'es', 'fr', 'it', 'pt']) { const h = load(lang === 'en' ? {} : { lang }); const { ev, dom, errors } = h; ev(BASE); dom.window.innerWidth = 360; const doc = dom.window.document; const leaks = () => { const txt = doc.getElementById('modalCard').textContent; assert.doesNotMatch(txt, /\b(bstd|ops|pop_std|common)\.[a-z_]+/, lang + ': a raw label'); assert.doesNotMatch(txt, /\{[a-z]\w*\}|undefined|NaN/, lang); }; ev('ACT.menu()'); doc.querySelector('#modalCard [data-act="bstdOpen"]').click(); assert.ok(ev('bstdShown()'), lang + ': from the menu'); leaks(); // Every target is a list; pick its last value and the standard follows, the sheet stays open and in focus. const keys = JSON.parse(ev('JSON.stringify(BSTD.map(x=>x.k))')); assert.equal(doc.querySelectorAll('#modalCard select[data-chg="bstdSet"]').length, keys.length); for (const k of keys) { const s = [...doc.querySelectorAll('#modalCard select[data-chg="bstdSet"]')].find(x => JSON.parse(x.dataset.args).k === k); assert.ok(s.closest('.ops-sel'), 'the Ops sheet\'s two-to-a-row lists'); s.value = String(s.options.length - 1); s.dispatchEvent(new dom.window.Event('change', { bubbles: true })); assert.ok(ev('bstdShown()'), lang + ' ' + k); assert.equal(doc.activeElement && doc.activeElement.dataset.args, JSON.stringify({ k }), 'the keyboard stays on the list'); assert.notEqual(ev(`bstd()[${lit(k)}]`), ev(`BSTDK[${lit(k)}].d`), lang + ' ' + k); } leaks(); if (lang === 'fr') assert.match(doc.getElementById('modalCard').textContent, /Répartis sur les deux bâtiments/, 'LAG is masculine'); // The Ops sheet has a way in before anyone is hired too. ev('ACT.opsOpen()'); assert.equal(ev('opsHired()'), false); doc.querySelector('#modalCard [data-act="bstdOpen"]').click(); assert.ok(ev('bstdShown()'), lang + ': from the Ops sheet with no intern'); // The intern's switch and budget sit in the sheet once one is hired, and stay there when used. ev("ACT.opsHire({id:'annie'});closeModal();ACT.bstdOpen()"); doc.querySelector('#modalCard [data-act="opsToggle"]').click(); assert.ok(ev('S.ops.on.std') && ev('bstdShown()'), lang); const cap = doc.querySelector('#modalCard select[data-chg="opsCap"]'); cap.value = String(cap.options.length - 1); cap.dispatchEvent(new dom.window.Event('change', { bubbles: true })); assert.equal(ev("opsCap('std')"), 0, lang); assert.ok(ev('bstdShown()'), lang); leaks(); // From the Ops sheet and the city panel. ev('ACT.opsOpen()'); doc.querySelector('#modalCard [data-act="bstdOpen"]').click(); assert.ok(ev('bstdShown()'), lang + ': from the Ops sheet'); ev("closeModal();selectCity('fra');ACT.tab({t:'links'})"); assert.doesNotMatch(doc.getElementById('panelBody').textContent, /\b(bstd|pop_std)\.[a-z_]+/, lang); doc.querySelector('#panelBody [data-act="bstdOpen"]').click(); assert.ok(ev('bstdShown()'), lang + ': from the city panel'); assert.deepEqual(errors, [], lang); dom.window.close(); } }); test('puzzles have no standard; the MCP tools see every action', () => { const { ev, errors } = load(); ev(BASE); std(ev, { rt: 2 }); ev("ACT.puzzleMenu();ACT.pzStart({id:PZ[0].id})"); assert.ok(ev('pzOn()')); assert.equal(ev('popStdQuote()'), null); assert.equal(ev('bstdPlan(false).length'), 0); ev("ACT.bstdSet({k:'rt'})"); assert.equal(ev('bstdShown()'), false); assert.deepEqual(errors, []); }); test('the big network: one shift of the order takes a few milliseconds', t => { const { ev, errors } = load({ seed: 3 }); t.after(() => ev('0')); const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8'); ev(`S=migrate(JSON.parse(${lit(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute()`); ev(`S.ops.intern='kevin';S.ops.on.std=true;S.ops.floor=0;S.ops.cap.std=0;S.rules.pnis='instant';S.popStd={g:${AU},rt:2,site2:true,gen:true,lag:2,split:true,ix:true,pni:2,sys:'cl',opt:'fast',wv:4}`); const plan = ev('(()=>{const t=performance.now();for(let i=0;i<5;i++)bstdPlan(true);return (performance.now()-t)/5;})()'); const jobs = ev('bstdPlan(true).length'); const shiftMs = ev('(()=>{const t=performance.now();opsDay(S.day);return performance.now()-t;})()'); console.log(`# big network: plan ${plan.toFixed(1)} ms (${jobs} jobs), one shift ${shiftMs.toFixed(1)} ms with its recompute`); assert.ok(jobs > 0); assert.ok(plan < 250, 'planning stays cheap: ' + plan); assert.ok(ev("S.ops.log[0].r.find(x=>x[0]==='std')[1]") <= ev('BSTD_MAX'), 'a few jobs a shift'); // Second buildings the month's budget cannot pay for: the LAG splits that need them wait with them, instead of being // tried (two opsMoNow runs each) and failing at every POP, every shift. ev('for(const t of S.transit)t.n=Math.max(2,t.n||1);S.ixn=S.ixn||{};for(const x in S.ixp)if(!S.myIx[x])S.ixn[x]=Math.max(2,S.ixn[x]||1);for(const p of S.pni)if(pniOn(p)&&!p.up)p.n=Math.max(2,p.n||1)'); ev('for(const k in S.ops.on)S.ops.on[k]=false;S.ops.on.std=true;S.popStd={rt:1,site2:true,split:true};S.ops.cap.std=OPS_CAPS[0];window.__m=0;const _m=opsMoNow;opsMoNow=function(){__m++;return _m.apply(this,arguments);};compute()'); assert.ok(ev("bstdPlan(true).filter(j=>j.k==='split'&&j.dep).length") > 100, 'many splits wait for a building'); for (let d = 0; d < 3; d++) { const ms = ev("(()=>{__m=0;S.day++;S.ops.mon=monthIdx(S.day);S.ops.spent.std=OPS_CAPS[0];const t=performance.now();opsDay(S.day);return performance.now()-t;})()"); const did = ev("(S.ops.log[0].d===S.day&&(S.ops.log[0].r.find(x=>x[0]==='std')||[])[1])||0"); console.log(`# big network, budget spent: shift ${ms.toFixed(1)} ms, ${did} splits done, ${ev('__m')} opsMoNow runs`); assert.equal(ev('__m'), 2 * did, 'every job tried was one that could be done'); assert.ok(did <= ev('BSTD_MAX')); } assert.deepEqual(errors, []); });