// The build standard's "Broadband: IPv6-only" row (96-pop-standard.js, the swap itself in 34-ipv6-only.js): every // broadband city still on dual stack starts its home-router swap, the most dual-stack homes first, priced as the city // panel prices it (the whole swap, paid as the routers go in), from the sheet's button or the intern's standing order, // within the order's budget and the cash floor. Only while IPv4 is scarce (rule ip4). const test = require('node:test'); const assert = require('node:assert/strict'); const { load } = require('./harness'); const { lit } = require('../scripts/lib/js-literal'); // On-call (IPv4 is scarce), plenty of cash, nothing breaking. Broadband in Amsterdam (3,000 homes), Frankfurt (1,300) and // Paris (500): their swaps cost $12k, $5.2k and $2k. const BASE = "ACT.start({});if(UI.pendGame)ACT.pitchSkip();S.rules=makeRules('oncall');S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;S.lvl=9;S.nextFail=S.nextEvent=1e9;UI.speed=0;S.offers=[];S.biz=[];" + "ACT.launchBB({city:'fra'});S.pops.fra.rgen=3;ACT.buyTransit({city:'fra',net:'lvl',tier:3});for(const c of ['ams','par']){ACT.buildPop({city:c});ACT.launchBB({city:c});}" + "S.pops.fra.bb.d=130;S.pops.ams.bb.d=300;S.pops.par.bb.d=50;compute()"; function game(t, opts) { const h = load(opts); t.after(() => h.dom.window.close()); h.ev(BASE); return h; } const J = (ev, x) => JSON.parse(ev(`JSON.stringify(${x})`)); const std = (ev, d) => ev(`S.popStd=${lit(d)}`); const v6 = (ev, plan = 'bstdPlan(false)') => J(ev, `${plan}.filter(j=>j.k==='v6').map(j=>[j.c,j.cost])`); const swapping = ev => J(ev, "['ams','fra','par'].filter(c=>S.pops[c].bb.v6e)"); const COST = { ams: 12000, fra: 5200, par: 2000 }; test('the row: every dual-stack broadband city, the most homes first, at the swap\'s own price and its IPv4 bill', (t) => { const h = game(t, { agent: true }); const { ev, errors } = h; assert.equal(ev('ip4On()'), true); assert.deepEqual(J(ev, "['ams','fra','par'].map(ip6Cost)"), [COST.ams, COST.fra, COST.par]); assert.deepEqual(v6(ev), [], 'no standard, no jobs'); std(ev, { v6: true }); assert.deepEqual(v6(ev), [['ams', COST.ams], ['fra', COST.fra], ['par', COST.par]], 'Amsterdam first: the most homes behind CGNAT'); assert.equal(ev('bstdLine(bstd())'), '1 more target'); // The plan's running costs are the IPv4 bill with every city done, against today's. const want = ev("(()=>{const eye=(R.rev&&R.rev.eyeballs)||0,m=b=>b.box+b.box64+b.refund,s={ip4:S.ip4,biz:S.biz,pops:{...S.pops}};for(const c of ['ams','fra','par'])s.pops[c]={...S.pops[c],bb:{...S.pops[c].bb,v6:1}};return m(ip4Bill(eye,s))-m(ip4Bill(eye));})()"); const q = J(ev, 'bstdQuote(false)'); assert.deepEqual([q.pops, q.n, q.cost], [3, 3, COST.ams + COST.fra + COST.par]); assert.equal(q.mo, Math.round(want)); // A city already swapping, one done, one with no homes yet: nothing to do there. ev("S.pops.ams.bb.v6e=S.day+40;S.pops.par.bb.v6=1;ACT.buildPop({city:'lon'});ACT.launchBB({city:'lon'});S.pops.lon.bb.d=0;compute()"); assert.deepEqual(v6(ev), [['fra', COST.fra]]); // A POP the intern is told to leave alone keeps its routers as they are. ev("ACT.opsHire({id:'annie'});closeModal();S.ops.skip.fra=['std']"); assert.deepEqual(v6(ev, 'bstdPlan(true)'), []); assert.deepEqual(v6(ev), [['fra', COST.fra]], 'the button is not the intern'); // The MCP tools: the target and the quote. ev("S.popStd=null;apiRun('bstdSet',{k:'v6'})"); assert.deepEqual(J(ev, 'S.popStd'), { rt: 1, v6: true }); assert.equal(J(ev, "apiRun('bstdApply',{},{dryRun:true})").quote.setup, COST.fra); assert.deepEqual(errors, []); }); test('the button starts every swap it can pay in full, on top of what the swaps under way still owe', (t) => { const { ev, dom, errors } = game(t); std(ev, { v6: true }); // Enough for Amsterdam and Paris, not for Frankfurt as well: the busiest first, then whatever still fits. ev(`S.cash=${COST.ams + COST.fra - 1};ACT.bstdOpen()`); const btn = dom.window.document.querySelector('#modalCard [data-act="bstdApply"]'); assert.equal(btn.textContent, `Bring everything up now · ${ev(`fmt$(${COST.ams + COST.fra + COST.par})`)}`); btn.click(); assert.deepEqual(swapping(ev), ['ams', 'par']); assert.equal(ev('S.cash'), COST.ams + COST.fra - 1, 'nothing paid yet: the routers are paid as they go in'); assert.equal(ev('ip6Owed()'), COST.ams + COST.par); const said = `2 jobs done for the build standard, ${ev(`fmt$(${COST.ams + COST.par})`)}.`; assert.ok(dom.window.document.getElementById('toasts').textContent.includes(said), 'the toast counts the swaps whole, as the quote did'); // Day by day the swaps pay what the button counted, and Frankfurt waits for the money. ev('for(let i=0;i<60;i++){S.day++;ip4Day(S.day);}compute()'); assert.deepEqual(J(ev, "['ams','par'].map(c=>S.pops[c].bb.v6)"), [1, 1]); assert.ok(Math.abs(ev('S.cash') - (COST.fra - 1 - COST.par)) < 1e-6, 'the swaps cost what the button counted: ' + ev('S.cash')); assert.deepEqual(v6(ev), [['fra', COST.fra]]); assert.deepEqual(errors, []); }); test("the intern rolls it out within the order's budget and the cash floor, and the shift log names the cities", (t) => { const { ev, errors } = game(t); std(ev, { v6: true }); ev("ACT.opsHire({id:'annie'});closeModal();S.ops.on.std=true;S.ops.floor=0;S.ops.cap.std=OPS_CAPS[0]"); // A $10k month: Amsterdam's $12k swap never fits it; Frankfurt and Paris do. const c0 = ev('S.cash'); ev('opsDay(S.day)'); assert.deepEqual(swapping(ev), ['fra', 'par']); assert.equal(ev('S.ops.spent.std'), COST.fra + COST.par, 'the whole swaps count against the month'); assert.equal(ev('S.cash'), c0, 'and are paid as the routers go in'); const e = J(ev, 'S.ops.log[0]'); assert.equal(e.up, undefined, 'nothing paid up front'); assert.ok(e.r.some(x => x[0] === 'broke'), 'Amsterdam waits for the budget'); assert.match(ev('opsLine(S.ops.log[0])'), /did 2 jobs for the build standard \(Frankfurt IPv6, Paris IPv6\)/); assert.equal(ev('S.ops.log[0].r.find(x=>x[0]===\'std\')[2].every(opsWhereOk)'), true, 'the log survives a save'); // No budget, a $50k floor and $15k above it: Amsterdam starts; Frankfurt does not fit on top of what Amsterdam owes, // Paris does. ev("for(const c of ['fra','par']){delete S.pops[c].bb.v6e;delete S.pops[c].bb.v6;}S.ops.cap.std=0;S.ops.floor=50000;S.cash=50000+15000;S.day++;compute();opsDay(S.day)"); assert.deepEqual(swapping(ev), ['ams', 'par']); assert.equal(ev('S.cash'), 65000); // Paid down, the next shift starts Frankfurt. ev('for(let i=0;i<60;i++){S.day++;ip4Day(S.day);}S.cash=1e6;compute();opsDay(S.day)'); assert.deepEqual(swapping(ev), ['fra']); assert.deepEqual(errors, []); }); // Only Amsterdam has homes to swap: one $12k swap. A quiet lite fiber Frankfurt–Paris for the line-system target. const AMS_ONLY = "S.pops.fra.bb.d=0;S.pops.par.bb.d=0;compute()"; const FIBER = "S.links.push({id:'q',a:'fra',b:'par',kind:'df',own:false,sys:'lite',ch:[],km:Math.round(kmBetween('fra','par')),sub:false,cable:null,mo:1000,trench:'q',cut:0,day:0,ready:0,spares:false});compute()"; test("what a swap still owes stays above the intern's cash floor for every job after it, in every order", (t) => { const { ev, errors } = game(t); ev(AMS_ONLY); ev("ACT.opsHire({id:'annie'});closeModal();for(const k in S.ops.on)S.ops.on[k]=false;S.ops.on.std=true;S.ops.floor=50000;S.ops.cap.std=1e6"); std(ev, { v6: true }); ev(`S.cash=50000+${COST.ams}+500;opsDay(S.day)`); assert.deepEqual(swapping(ev), ['ams']); assert.equal(ev('ip6Owed()'), COST.ams); // The next day the standard wants a generator at every POP too. The cash above the floor covers one only with the // swap's money: it waits. assert.equal(ev('GEN_COST'), 12000); std(ev, { v6: true, gen: true }); ev('S.day++;compute();opsDay(S.day)'); assert.deepEqual(J(ev, 'Object.keys(S.pops).filter(c=>S.pops[c].gen)'), []); assert.deepEqual(J(ev, 'S.ops.log[0].r'), [['broke', 1, ['std']]]); // With no income at all, the swap then pays its routers day by day and never takes the cash under the floor. const low = ev('(()=>{let m=S.cash;for(let i=0;i<60;i++){S.day++;ip6Day(S.day);m=Math.min(m,S.cash);}return m;})()'); assert.equal(ev('S.pops.ams.bb.v6'), 1); assert.ok(low >= 50000 - 1e-6, 'the floor held: ' + low); // A swap started by hand is owed too, and every order keeps it aside: the routers order here. ev("S.pops.ams.bb.d=600;delete S.pops.ams.bb.v6;compute();ACT.ip6Go({city:'ams'})"); assert.equal(ev('!!S.pops.ams.bb.v6e'), true); const owed = ev('ip6Owed()'); assert.ok(owed > 0); assert.deepEqual(J(ev, `(()=>{const r=[];for(const d of [-1,0]){S.cash=50000+1000+${owed}+d;r.push(opsShift().can('routers',1000));}return r;})()`), [false, true]); assert.deepEqual(errors, []); }); test('the button keeps what a swap it just started owes for the jobs after it in the same click', (t) => { const { ev, dom, errors } = game(t); ev(AMS_ONLY); ev(FIBER); std(ev, { v6: true, sys: 'roadm' }); const plan = J(ev, 'bstdPlan(false).map(j=>[j.k,j.w,j.cost])'); assert.deepEqual(plan.map((j) => j.slice(0, 2)), [['v6', 'ams IPv6'], ['sys', 'fra|par']], 'the swap first, then the fiber'); const sys = plan[1][2]; // A dollar short of both: the swap starts, and the fiber waits rather than spend the swap's routers. ev(`S.cash=${COST.ams + sys - 1};ACT.bstdApply({})`); assert.deepEqual(swapping(ev), ['ams']); assert.equal(ev("S.links.find(l=>l.id==='q').sys"), 'lite'); assert.equal(ev('S.cash'), COST.ams + sys - 1); assert.ok(dom.window.document.getElementById('toasts').textContent.includes(`One job done for the build standard, ${ev(`fmt$(${COST.ams})`)}. One thing is still below it`), "the toast: the swap, and the fiber still below the standard"); // A dollar more, and the next tap swaps the fiber with the swap's routers still in hand. ev('S.cash+=1;ACT.bstdApply({})'); assert.equal(ev("S.links.find(l=>l.id==='q').sys"), 'roadm'); assert.equal(ev('S.cash'), COST.ams); assert.equal(ev('ip6Owed()'), COST.ams); assert.deepEqual(errors, []); }); test('only while IPv4 is scarce: no row, no jobs and no count in the gentler rules; saves keep the target', (t) => { const { ev, dom, errors } = game(t); std(ev, { v6: true }); ev("S.rules=makeRules('freeze');compute()"); assert.equal(ev('ip4On()'), false); assert.deepEqual(v6(ev), []); assert.equal(ev('bstdLine(bstd())'), 'None'); ev('ACT.bstdOpen()'); const sel = [...dom.window.document.querySelectorAll('#modalCard select[data-chg="bstdSet"]')].map(s => JSON.parse(s.dataset.args).k); assert.ok(sel.length > 0 && !sel.includes('v6'), 'no IPv6-only list'); assert.doesNotMatch(dom.window.document.getElementById('modalCard').textContent, /IPv6/); // The target stays in the standard: back on scarce IPv4 it is there again. ev("closeModal();S.rules=makeRules('oncall');compute()"); assert.deepEqual(v6(ev).map(x => x[0]), ['ams', 'fra', 'par']); // Saves: the target round-trips; an old standard has none; junk is no target. const rt = d => J(ev, `(()=>{const s={popStd:${lit(d)}};popStdSanitize(s);return s.popStd;})()`); assert.deepEqual(rt({ rt: 1, v6: true }), { rt: 1, v6: true }); assert.deepEqual(rt({ g: 1, rt: 2 }), { g: 1, rt: 2 }); assert.equal(rt({ rt: 1, v6: 'yes' }), null); assert.deepEqual(J(ev, 'migrate(JSON.parse(JSON.stringify(S))).popStd'), { rt: 1, v6: true }); assert.deepEqual(errors, []); }); test('the sheet at 360 px, portrait and landscape, in every language: the row, its note and what is below it', () => { for (const lang of ['en', 'de', 'es', 'fr', 'it', 'pt']) { const { ev, dom, errors } = load(lang === 'en' ? {} : { lang }); ev(BASE); const doc = dom.window.document; for (const [w, hgt] of [[360, 740], [740, 360]]) { ev(`window.innerWidth=${w};window.innerHeight=${hgt};S.popStd=null;closeModal();ACT.bstdOpen()`); assert.equal(ev('isPhone()'), true, lang); const sel = () => [...doc.querySelectorAll('#modalCard select[data-chg="bstdSet"]')].find(s => JSON.parse(s.dataset.args).k === 'v6'); let s = sel(); assert.ok(s && s.closest('.ops-sel'), lang + ': two to a row with the other POP targets'); s.value = String(s.options.length - 1); s.dispatchEvent(new dom.window.Event('change', { bubbles: true })); assert.equal(ev('bstd().v6'), true, lang); assert.ok(ev('bstdShown()'), lang); s = sel(); assert.equal(s.options[s.selectedIndex].textContent, ev("tr('bstd.every_bb_city')"), lang); const txt = doc.getElementById('modalCard').textContent; assert.ok(txt.includes(ev("tr('bstd.v6_note')")), lang + ': the note'); assert.ok(txt.includes(ev(`fmt$(${COST.ams + COST.fra + COST.par})`)), lang + ': the bill'); assert.doesNotMatch(txt, /\b(bstd|ops|pop_std|common|ip6)\.[a-z_0-9]+/, lang + ': a raw label'); assert.doesNotMatch(txt, /\{[a-z]\w*\}|undefined|NaN/, lang); for (const b of doc.querySelectorAll('#modalCard button.b')) assert.ok(b.closest('.btns') || b.closest('.ops-h'), lang + ': ' + b.textContent); } // The city panel's line counts it. ev("closeModal();selectCity('fra');ACT.tab({t:'links'})"); assert.ok(doc.getElementById('panelBody').textContent.includes(ev("trn('bstd.n_more_targets',1)")), lang); assert.deepEqual(errors, [], lang); dom.window.close(); } });