// One owner per fiber: the build standard's order (std) and the optical order (optup) take fibers dark with the same // test (a routing without the fiber, in the route worker in play), share one budget of dark swaps a night, and never both // work on one fiber in a shift; the decom order leaves a wave the standard just upgraded. Change Freeze, no failures. const test = require('node:test'); const assert = require('node:assert/strict'); const { load } = require('./harness'); const { lit } = require('../scripts/lib/js-literal'); // Frankfurt (broadband, 100G Cogentle transit), Amsterdam and Paris with broadband, everyone on four Chonk 800s, level 7. const BASE = "Date.now=()=>1.7e12;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');" + "ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1});for(const c of ['ams','par','lon'])ACT.buildPop({city:c});" + "for(const c of ['fra','ams','par','lon']){S.pops[c].rgen=RG.findIndex(r=>r.n==='Chonk 800');S.pops[c].routers=4;}ACT.launchBB({city:'ams'});ACT.launchBB({city:'par'});compute()"; function game(t, extra = '', opts = {}) { const h = load(opts); t.after(() => h.dom.window.close()); h.ev(BASE); if (extra) h.ev(extra); return h; } const hire = (ev, id) => ev(`ACT.opsHire({id:'${id}'});S.ops.floor=0;S.ops.cap={std:0,optup:0};S.ops.on.std=true;closeModal()`); const std = (ev, d) => ev(`S.popStd=bstdPut({...bstd(),...${lit(d)}})`); // A leased dark fiber, built and lit with these optics; a wave. 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;})()`); const wave = (ev, a, b, tier) => ev(`(()=>{const id=uid();S.links.push({id,a:'${a}',b:'${b}',kind:'wave',tier:${tier},km:Math.round(kmBetween('${a}','${b}')),cut:0,mo:1,trench:id,day:S.day});return id;})()`); const L = (ev, id) => JSON.parse(ev(`JSON.stringify(S.links.find(l=>l.id===${lit(id)})||null)`)); const said = ev => JSON.parse(ev('JSON.stringify((S.ops.log[0]&&S.ops.log[0].d===S.day?S.ops.log[0].r:[]).map(x=>[x[0],x[1]]))')); // Count the fiber tests that route (opsDarkTrial()), on the page here: tests never use the worker. const COUNT = "window.__tr=0;const _t=opsDarkTrial;opsDarkTrial=function(){__tr++;return _t.apply(this,arguments);};"; test('both orders share one budget of dark swaps a night: one for Annie, two for Kevin, whoever does them', t => { for (const [id, want] of [['annie', 1], ['kevin', 2]]) { // Nobody on the far ends: the fibers really are quiet, whenever the shift routes again. const { ev, errors } = game(t, 'S.pops.ams.bb=null;S.pops.par.bb=null'); const ids = ['ams', 'par', 'lon'].map(c => fiber(ev, 'fra', c, ['zr4', 'zr4'])); ev('compute();for(const l of S.links)R.load[l.id]=0'); hire(ev, id); std(ev, { sys: 'roadm' }); ev('S.ops.on.optup=true;opsDay(S.day)'); assert.equal(ids.filter(x => L(ev, x).sys === 'roadm').length, want, id + ': fibers swapped tonight'); assert.deepEqual(said(ev), [['std', want]], id + ': the build standard went first, the optical order found no room left'); // Alone, the optical order has the same budget (the line system from the standard). ev('S.ops.on.std=false;S.day+=2;compute();for(const l of S.links)R.load[l.id]=0;opsDay(S.day)'); assert.equal(ids.filter(x => L(ev, x).sys === 'roadm').length, Math.min(3, 2 * want), id); assert.deepEqual(said(ev), [['optup', Math.min(want, 3 - want)]]); assert.deepEqual(errors, []); } }); test('a fiber the build standard worked on tonight is its own: the optical order takes it the next night', t => { // A full Rainbow-in-a-Box fiber to Amsterdam, running hot, and a big wave beside it for the day it goes dark. const { ev, errors } = game(t); const f = fiber(ev, 'fra', 'ams', Array(16).fill('zr4')); wave(ev, 'fra', 'ams', 3); const hot = `compute();R.load[${lit(f)}]=1e5;`; hire(ev, 'annie'); std(ev, { opt: 'fast' }); ev(`S.ops.on.optup=true;${hot}opsDay(S.day)`); assert.deepEqual([L(ev, f).sys, [...new Set(L(ev, f).ch)]], ['lite', ['zr8']], 'the standard swapped its optics; its line system stays tonight'); assert.deepEqual(said(ev), [['std', 1]], 'the optical order left it alone'); ev(`S.day++;${hot}opsDay(S.day)`); assert.equal(L(ev, f).sys, 'roadm', 'the next night, with nothing left for the standard there, the optical order moves it up'); assert.deepEqual(said(ev), [['optup', 1]]); assert.deepEqual(errors, []); }); test("the standard's order takes a fiber dark on the optical order's test: a way around, not just a parallel link, and never a little traffic with none", t => { // Amsterdam's broadband rides Frankfurt–Amsterdam; Paris closes a ring of fibers with room. const { ev, errors } = game(t); const a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']), b = fiber(ev, 'ams', 'par', ['zr4', 'zr4']), c = fiber(ev, 'par', 'fra', ['zr4', 'zr4']); ev(COUNT); hire(ev, 'annie'); std(ev, { sys: 'roadm' }); // Busy by the plan's own measure, and nothing parallel: the button waits for it, the intern routes and finds the ring. const busy = `compute();R.load[${lit(a)}]=capOf(S.links.find(l=>l.id===${lit(a)}))*0.9;`; ev(busy); assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdQuote(false).held)')), ['fra|ams'], 'the button: busy, nothing parallel'); assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdQuote(true).held)')), [], 'the intern: a routing decides'); const served = ev('R.served'); ev('opsDay(S.day)'); assert.equal(ev('__tr'), 1, 'one routing without the fiber'); assert.deepEqual([L(ev, a).sys, L(ev, b).sys, L(ev, c).sys], ['roadm', 'lite', 'lite'], 'the busy one first, one a night'); ev('compute()'); assert.ok(ev('R.served') >= served - 1e-6, 'and every gigabit still gets through, round the ring'); // A little traffic (well under the plan's "quiet") and no way around: the old test let it go dark; this one never. const g = game(t); const x = fiber(g.ev, 'fra', 'ams', ['zr4', 'zr4']); g.ev('compute()'); assert.ok(g.ev(`R.load[${lit(x)}]`) > 1 && !g.ev(`bstdBusy(S.links.find(l=>l.id===${lit(x)}))`), 'Amsterdam\'s broadband, under 10% of the fiber'); hire(g.ev, 'kevin'); std(g.ev, { sys: 'roadm', opt: 'fast' }); g.ev(COUNT + 'opsDay(S.day)'); assert.deepEqual([L(g.ev, x).sys, L(g.ev, x).eqDown], ['lite', undefined], 'Amsterdam is never cut off for a day'); assert.deepEqual(L(g.ev, x).ch, ['zr8', 'zr8'], 'it gets the fastest optics its line system takes'); assert.equal(g.ev('__tr'), 0, 'known without routing'); assert.deepEqual(errors, []); assert.deepEqual(g.errors, []); }); test('with two swaps a night, the second fiber is judged on the routing with the first one dark, not on the loads from before', t => { // The ring again, Kevin on shift. Once Frankfurt–Amsterdam is dark, Amsterdam's traffic rides Amsterdam–Paris, which // carried nothing before: its "nothing rides it" is asked again after the shift routes, and is no longer true. const { ev, errors } = game(t); const a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']), b = fiber(ev, 'ams', 'par', ['zr4', 'zr4']), c = fiber(ev, 'par', 'fra', ['zr4', 'zr4']); hire(ev, 'kevin'); std(ev, { sys: 'roadm' }); ev(`compute();R.load[${lit(a)}]=capOf(S.links.find(l=>l.id===${lit(a)}))*0.9;`); assert.equal(ev(`R.load[${lit(b)}]||0`), 0, 'Amsterdam–Paris is idle today'); const served = ev('R.served'); ev('opsDay(S.day)'); assert.deepEqual([L(ev, a).sys, L(ev, b).sys, L(ev, c).sys], ['roadm', 'lite', 'lite'], 'Amsterdam keeps one way in'); ev('compute()'); assert.ok(ev('R.served') >= served - 1e-6, 'and every gigabit still gets through'); assert.deepEqual(errors, []); }); test('two busy fibers side by side: one goes dark tonight, its twin carries it, and the twin goes the next night', t => { // Amsterdam's broadband split over two Frankfurt–Amsterdam fibers, a few gigabits each: busy, though under the // button's "quiet". Kevin has room for two swaps, but the twin waits rather than fail the test and wait a week. const { ev, errors } = game(t); const a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']), b = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']); hire(ev, 'kevin'); std(ev, { sys: 'roadm' }); ev('compute()'); assert.ok(ev(`R.load[${lit(a)}]`) > 1 && ev(`R.load[${lit(b)}]`) > 1, 'both carry Amsterdam'); ev('opsDay(S.day)'); assert.deepEqual([[L(ev, a).sys, L(ev, b).sys].sort(), ev('OPTUP_NO.size')], [['lite', 'roadm'], 0], 'one tonight, and no fiber failed'); ev('S.day++;compute();opsDay(S.day)'); assert.deepEqual([L(ev, a).sys, L(ev, b).sys], ['roadm', 'roadm'], 'the twin the next night'); assert.deepEqual(errors, []); }); test('a fiber that fails the test waits a week, for both orders, and gets the optics its line system takes meanwhile', t => { // The ring back from Amsterdam is two 10G waves: too thin for Amsterdam's broadband. const { ev, errors } = game(t); const a = fiber(ev, 'fra', 'ams', ['zr4', 'zr4']); wave(ev, 'ams', 'par', 0); wave(ev, 'par', 'fra', 0); ev('compute()'); assert.ok(ev(`R.load[${lit(a)}]`) > 10, 'more than the ring carries'); ev(COUNT); hire(ev, 'annie'); std(ev, { sys: 'roadm', opt: 'fast' }); ev('S.ops.on.optup=true;opsDay(S.day)'); assert.equal(ev('__tr'), 1, 'tested once'); assert.deepEqual([L(ev, a).sys, L(ev, a).ch], ['lite', ['zr4', 'zr4']], 'no swap, and no optics for a line system it did not get'); assert.equal(ev(`OPTUP_NO.get(${lit(a)})`), ev('S.day+OPTUP_WAIT')); for (let d = 1; d < 7; d++) ev('S.day++;compute();opsDay(S.day)'); assert.equal(ev('__tr'), 1, 'not tested again that week, by either order'); assert.deepEqual([L(ev, a).sys, L(ev, a).ch], ['lite', ['zr8', 'zr8']], 'the fastest optics the old line system takes'); assert.deepEqual(errors, []); }); test('an express riding the fiber goes dark with it in the test: its traffic has to get around too', t => { // Frankfurt–Brussels–Paris as an express over two bare ROADM fibers; Paris reaches its transit over it. const { ev, errors } = game(t, "ACT.buildPop({city:'bru'});S.pops.bru.rgen=S.pops.fra.rgen;S.lvl=8;" + "window.df=(a,b)=>{const id=uid();S.links.push({id,a,b,kind:'df',own:true,sys:'roadm',ch:[],km:kmBetween(a,b),sub:false,cable:null,mo:100,trench:id,cut:0,day:S.day,ready:S.day,spares:false,pairs:1});return id;};" + "window.s1=df('fra','bru');window.s2=df('bru','par');compute();ACT.xpBuild({a:'fra',b:'par',via:s1+','+s2,o:'zr4'});window.x=S.links.find(l=>l.kind==='xp').id;compute()"); const [s1, s2, x] = ['s1', 's2', 'x'].map(k => ev(k)); assert.ok(ev('R.load[x]') > 1 && !ev('(R.load[s1]||0)+(R.load[s2]||0)'), 'the express carries Paris; the fibers carry nothing of their own'); ev(COUNT + "window.__xd=[];const _c=compute;compute=function(){if(S.links.find(l=>l.id===s1).eqDown>S.day||S.links.find(l=>l.id===s2).eqDown>S.day)__xd.push(xpDown(S.links.find(l=>l.id===x)));return _c.apply(this,arguments);}"); hire(ev, 'annie'); std(ev, { sys: 'cl' }); const sys = () => [L(ev, s1).sys, L(ev, s2).sys]; // No way around either fiber: known without routing. assert.deepEqual(JSON.parse(ev('JSON.stringify(bstdQuote(true).held)')).sort(), ['bru|par', 'fra|bru']); ev('opsDay(S.day)'); assert.deepEqual([sys(), ev('__tr')], [['roadm', 'roadm'], 0]); // A wave beside the first fiber: it has a way around now, but Paris has none without the express. wave(ev, 'fra', 'bru', 1); ev('S.day++;compute();opsDay(S.day)'); assert.equal(ev('__tr'), 1, 'one routing without the fiber'); assert.deepEqual(JSON.parse(ev('JSON.stringify(__xd)')), [true], 'the express was dark in it'); assert.deepEqual(sys(), ['roadm', 'roadm'], 'Paris would be cut off: no swap'); // A wave to Paris carries it round: the swap goes ahead, with the express dark for the day. wave(ev, 'fra', 'par', 3); ev('OPTUP_NO.clear();S.day++;compute();opsDay(S.day)'); assert.deepEqual(sys().sort(), ['cl', 'roadm'], 'one of them, one a night'); assert.equal(ev('xpDown(S.links.find(l=>l.id===x))'), true); ev('compute()'); assert.ok(ev('R.load[x]||0') === 0 && ev('R.served') > 0); assert.deepEqual(errors, []); }); test('the decom order never hands back a wave the build standard upgraded that same shift', t => { const { ev, errors } = game(t); // A 800G wave and two 100G waves between Frankfurt and Amsterdam, in three trenches. const big = wave(ev, 'fra', 'ams', 3), w1 = wave(ev, 'fra', 'ams', 1), w2 = wave(ev, 'fra', 'ams', 1); hire(ev, 'annie'); ev("S.ops.on.decom=true;S.ops.p.decom={keep:false}"); std(ev, { wv: 2 }); // The plan sees a pair that needs all three once the small ones are 400G; the routing after the shift's jobs, which // decom reads, sees almost nothing on them. ev(`compute();R.load[${lit(big)}]=700;R.load[${lit(w1)}]=150;R.load[${lit(w2)}]=150`); assert.equal(ev('bstdQuote(true).n'), 2, 'both small waves go up'); ev('opsDay(S.day)'); const tiers = () => JSON.parse(ev("JSON.stringify(S.links.filter(l=>l.kind==='wave').map(l=>l.tier))")); assert.deepEqual(tiers(), [3, 2, 2], 'both upgraded, both kept today'); assert.deepEqual(said(ev), [['std', 2]]); // Tomorrow they are the decom order's to judge again. ev('S.day++;compute();opsDay(S.day)'); assert.deepEqual(tiers(), [3]); assert.deepEqual(said(ev), [['decom', 2]]); assert.deepEqual(errors, []); }); // In play the test is routed in the route worker. jsdom has no Worker: a second page plays it (rwServe() there), as in // tests/day-steps.test.js. The day is the day dayTick() plays, and the page itself never routes. test("in play, the standard's fiber test is routed by the route worker, and the day is the day dayTick() plays", t => { // The ring of fibers, Amsterdam's and Paris's broadband on it. const RING = "for(const [a,b] of [['fra','ams'],['ams','par'],['par','fra']]){const id=uid();S.links.push({id,a,b,kind:'df',own:false,sys:'lite',ch:['zr4','zr4'],km:Math.round(kmBetween(a,b)),sub:false,cable:null,mo:1000,trench:id,cut:0,day:S.day,ready:S.day,spares:false});}compute();" + "ACT.opsHire({id:'annie'});S.ops.floor=0;S.ops.cap={};S.ops.on.std=true;closeModal();S.popStd=bstdPut({...bstd(),sys:'roadm'});__reseed(4242);"; const open = seed => { const h = load({ seed }); t.after(() => h.dom.window.close()); h.dom.window.structuredClone = globalThis.structuredClone; return h; }; const ref = open(777), page = open(777), wk = open(1); for (const h of [ref, page]) { h.ev(BASE); h.ev(RING); } page.ev('window.__q=[];window.__later=[];RW.budget=0;RW.later=f=>__later.push(f);rwOn=()=>!RW.dead;rwStart=()=>RW.w||(RW.w={postMessage:m=>__q.push(structuredClone(m)),terminate(){__q.length=0;}});'); page.ev('window.__pc=0;const _c=compute;compute=function(){__pc++;return _c.apply(this,arguments);};'); wk.ev('window.__out=null;window.postMessage=(m,tr)=>{__out=structuredClone(m,{transfer:tr});};'); let trials = 0; const pump = () => { for (;;) { if (page.ev('__later.length')) { page.ev('__later.shift()()'); continue; } if (!page.ev('__q.length')) break; if (page.ev('!!(__q[0].trial&&__q[0].trial.dark)')) trials++; wk.dom.window.__m = page.ev('__q.shift()'); wk.ev('rwServe(__m)'); page.dom.window.__a = wk.ev('__out'); page.ev('rwDone(__a)'); } }; ref.ev('dayTick()'); page.ev('rwDay()'); pump(); assert.equal(trials, 1, 'the fiber test went to the worker'); assert.equal(page.ev('__pc'), 0, 'no routing on the page'); assert.equal(page.ev("S.links.filter(l=>l.sys==='roadm').length"), 1, 'and it passed: the rest of the ring carries its traffic'); const STATE = 'JSON.stringify({S,served:R.served,load:R.load})'; assert.equal(page.ev(STATE), ref.ev(STATE), 'the same day as dayTick()'); assert.deepEqual([...ref.errors, ...page.errors, ...wk.errors], []); }); // In play the shift rests between the standard's jobs (DAY_REST, rwRest()), and the player can tap meanwhile: every job // is checked again against the game as it is then, not as the plan found it at the start of the shift. test('the player acts while the shift rests: nothing past the standard, nothing paid twice, nothing claimed, and no snag for a POP gone', t => { // Every POP on one router. std: the standard; act: what the player does at the first rest after a job of the shift. const play = (std, act, setup = '') => { const { ev, errors } = game(t, "for(const c of ['fra','ams','par','lon'])S.pops[c].routers=1;compute()"); hire(ev, 'annie'); ev(`S.popStd=${std};${setup};window.__later=[];RW.budget=0;RW.later=f=>__later.push(f)`); ev('rwDay()'); let spent = null; while (ev('__later.length')) { if (spent === null && ev('S.ops.spent.std>0')) { spent = ev('S.ops.spent.std'); ev(act); } ev('__later.shift()()'); } assert.ok(spent > 0, 'the shift rested after a job'); return { ev, errors, spent }; }; const routers = ev => JSON.parse(ev('JSON.stringify(Object.fromEntries(Object.entries(S.pops).map(([c,p])=>[c,p.routers])))')); // The button brings every POP up to the standard while the shift rests after its first job. let { ev, errors, spent } = play('{rt:3}', 'ACT.bstdApply()'); assert.deepEqual(routers(ev), { fra: 3, ams: 3, par: 3, lon: 3 }, 'three routers each, as the standard says'); assert.equal(ev('S.ops.spent.std'), spent, 'the shift paid for nothing after the button'); assert.deepEqual(said(ev), [['std', 1]]); assert.deepEqual(errors, []); // The same with a model: the swaps the button did are not the shift's. ({ ev, errors, spent } = play("{g:RG.findIndex(r=>r.n==='Absolute Unit 1.6T'),rt:2}", 'ACT.bstdApply()', "for(const c in S.pops)S.pops[c].routers=2")); assert.deepEqual(JSON.parse(ev('JSON.stringify([...new Set(Object.values(S.pops).map(p=>RG[p.rgen].n))])')), ['Absolute Unit 1.6T']); assert.equal(ev('S.ops.spent.std'), spent); assert.deepEqual(said(ev), [['std', 1]], 'one swap, not four'); assert.deepEqual(errors, []); // A PNI the player orders by hand meanwhile, smaller than the plan's: the shift does not order it again as an upgrade. const net = "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, errors, spent } = play('{rt:2,pni:2}', `pniPlace(pniQuote('ams',${net},0))`, `S.rules.pnis='instant';window.__net=${net};S.pni.push({id:'P1',city:'fra',net:__net,tier:1,ready:S.day});compute();const _v=pniVal;pniVal=(id,c)=>id===__net&&c==='ams'?{..._v(id,c),worth:true,want:40,tier:1}:_v(id,c)`)); assert.deepEqual(JSON.parse(ev("JSON.stringify(S.pni.filter(x=>x.city==='ams').map(x=>[x.tier,!!x.up]))")), [[0, false]], 'the player\'s own, as ordered'); assert.deepEqual(said(ev), [['std', 4]], 'the routers only'); assert.deepEqual(errors, []); // Amsterdam decommissioned meanwhile: its jobs are left out, the order runs on, the other POPs get theirs. ({ ev, errors } = play('{rt:3}', "decommission(['ams'])")); assert.equal(ev('S.ops.on.std'), true, 'the order is still on'); assert.deepEqual(said(ev), [['std', 6]], 'no snag'); assert.deepEqual(routers(ev), { fra: 3, par: 3, lon: 3 }); assert.deepEqual(errors, []); });