Merge #192: Add bulk peering, cache and PoP redundancy utilities (Columbo818)

This commit is contained in:
Frederic Jung 2026-10-28 15:25:15 +01:00
commit 370ac536f6
No known key found for this signature in database
14 changed files with 485 additions and 0 deletions

View file

@ -0,0 +1,120 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
function setup(t) {
const h = load();
t.after(() => h.dom.window.close());
h.ev("ACT.start({});S.cash=1e8;UI.speed=0;for(const g of GOALS)S.done[g.id]=0;compute()");
return h;
}
test('kit balances each provider with minimal moves while buying the missing hardware', t => {
const { ev, errors } = setup(t);
ev("for(const [net,n] of [['cfl',4],['ggl',3]])for(let i=0;i<n;i++)S.caches.push({id:net+i,city:'fra',net});S.pops.ams={day:0,routers:1};S.caches.push({id:'elsewhere',city:'ams',net:'cfl',site:'b'});compute()");
const before = ev('S.cash'), ids = ev('S.caches.map(c=>c.id).join()');
const quote = JSON.parse(ev("JSON.stringify(utilityKit('fra'))"));
assert.equal(quote.moves.length, 3);
ev("ACT.utilityKit({city:'fra',cost:utilityKit('fra').cost})");
assert.equal(ev('S.cash'), before - quote.cost);
assert.equal(ev("S.caches.filter(c=>c.city==='fra'&&c.net==='cfl'&&c.site==='b').length"), 2);
assert.equal(ev("S.caches.filter(c=>c.city==='fra'&&c.net==='ggl'&&c.site==='b').length"), 1);
assert.equal(ev("S.caches.find(c=>c.id==='elsewhere').site"), 'b');
assert.equal(ev('S.caches.map(c=>c.id).join()'), ids);
assert.equal(ev("utilityKit('fra').moves.length"), 0);
const stable = ev('JSON.stringify(S.caches)');
ev("ACT.utilityKit({city:'fra',cost:0})");
assert.equal(ev('JSON.stringify(S.caches)'), stable);
assert.deepEqual(errors, []);
});
test('an already equipped PoP can balance caches for free, even with negative cash', t => {
const { ev, errors } = setup(t);
ev("Object.assign(S.pops.fra,{gen:true,site2:true,routers:3});for(const [net,site] of [['cfl','a'],['ggl','b']])for(let i=0;i<4;i++)S.caches.push({id:net+i,city:'fra',net,site});S.cash=-1;compute();selectCity('fra');ACT.tab({t:'links'})");
assert.equal(ev("utilityKit('fra').moves.length"), 4, 'overall counts match but provider counts do not');
assert.equal(ev("document.querySelector('#panelBody [data-act=utilityKit]').disabled"), false);
ev("document.querySelector('#panelBody [data-act=utilityKit]').click()");
assert.equal(ev('S.cash'), -1);
assert.equal(ev('S.pops.fra.routers'), 3);
for (const net of ['cfl', 'ggl'])
assert.equal(ev(`S.caches.filter(c=>c.net==='${net}'&&c.site==='b').length`), 2);
assert.deepEqual(errors, []);
});
test('a rejected kit purchase never moves caches or partially installs hardware', t => {
const { ev, errors } = setup(t);
ev("S.caches=[{id:'c1',city:'fra',net:'cfl'},{id:'c2',city:'fra',net:'cfl'}];S.cash=1;compute()");
const before = ev('JSON.stringify({p:S.pops.fra,c:S.caches,cash:S.cash})');
ev("ACT.utilityKit({city:'fra',cost:utilityKit('fra').cost});ACT.utilityKit({city:'fra',cost:-1})");
assert.equal(ev('JSON.stringify({p:S.pops.fra,c:S.caches,cash:S.cash})'), before);
assert.deepEqual(errors, []);
});
test('odd provider totals are balanced across the whole PoP with the fewest moves', t => {
const { ev, errors } = setup(t);
ev("Object.assign(S.pops.fra,{gen:true,site2:true,routers:2});for(const [net,n] of [['nfx',3],['cfl',1],['ggl',1]])for(let i=0;i<n;i++)S.caches.push({id:net+i,city:'fra',net});compute()");
assert.equal(ev("utilityCacheMoves('fra').length"), 2, 'five boxes all in A require two moves');
ev("ACT.utilityKit({city:'fra',cost:0})");
assert.equal(ev("S.caches.filter(c=>c.site==='b').length"), 2);
assert.equal(ev("['nfx','cfl','ggl'].every(net=>Math.abs(S.caches.filter(c=>c.net===net&&c.site==='b').length-S.caches.filter(c=>c.net===net&&c.site!=='b').length)<=1)"), true);
assert.equal(ev("utilityCacheMoves('fra').length"), 0);
assert.deepEqual(errors, []);
});
test('cache batch fills missing provider/site pairs, keeps extra boxes and respects eligibility', t => {
const { ev, errors } = setup(t);
ev("Object.assign(S.pops.fra,{site2:true,routers:2});S.caches=[{id:'keep1',city:'fra',net:'cfl'},{id:'keep2',city:'fra',net:'cfl',site:'a'}];compute();R.demTo={cfl:NET.cfl.cache.min,ggl:NET.ggl.cache.min-1}");
const cash = ev('S.cash'), mo = ev('R.cost.caches');
assert.equal(ev("utilityCaches('fra').add.length"), 1);
ev("ACT.utilityCaches({city:'fra',quote:utilityCaches('fra').key})");
assert.equal(ev('S.cash'), cash, 'cache hardware is free');
assert.equal(ev('R.cost.caches'), mo + ev('CACHE_MO'));
assert.equal(ev("S.caches.filter(c=>c.net==='cfl'&&c.site==='b').length"), 1);
assert.equal(ev("S.caches.filter(c=>c.net==='cfl').length"), 3, 'extra existing A box kept');
assert.equal(ev("S.caches.some(c=>c.net==='ggl')"), false);
const stable = ev('JSON.stringify(S.caches)');
ev("R.demTo={cfl:NET.cfl.cache.min};ACT.utilityCaches({city:'fra',quote:utilityCaches('fra').key})");
assert.equal(ev('JSON.stringify(S.caches)'), stable);
assert.deepEqual(errors, []);
});
test('one-site requests add only A, then a new second site gets exactly its missing boxes', t => {
const { ev, errors } = setup(t);
ev("R.demTo=Object.fromEntries(NETS.filter(n=>n.cache).map(n=>[n.id,n.cache.min]));ACT.utilityCaches({city:'fra',quote:utilityCaches('fra').key})");
assert.equal(ev('S.caches.length'), 5);
assert.equal(ev("S.caches.every(c=>c.site==='a')"), true);
ev("ACT.utilityKit({city:'fra',cost:utilityKit('fra').cost});R.demTo=Object.fromEntries(NETS.filter(n=>n.cache).map(n=>[n.id,n.cache.min]));ACT.utilityCaches({city:'fra',quote:utilityCaches('fra').key})");
assert.equal(ev('S.caches.length'), 10);
assert.equal(ev("NETS.filter(n=>n.cache).every(n=>['a','b'].every(s=>S.caches.filter(c=>c.net===n.id&&c.site===s).length===1))"), true);
ev("save();S=migrate(JSON.parse(localStorage.getItem('sot-save-v1')));compute()");
assert.equal(ev('S.caches.length'), 10);
assert.equal(ev("S.caches.filter(c=>c.site==='b').length"), 5);
assert.deepEqual(errors, []);
});
test('cache requests reject stale eligibility and insufficient port capacity atomically', t => {
const { ev, errors } = setup(t);
ev("S.pops.fra.site2=true;for(let i=0;i<7;i++)S.caches.push({id:'full'+i,city:'fra',net:'nfx'});compute();R.demTo={cfl:NET.cfl.cache.min};window.q=utilityCaches('fra');ACT.utilities()");
assert.equal(ev('q.free'), 1);
assert.equal(ev('q.add.length'), 2);
assert.equal(ev("document.querySelector('[data-act=utilityCaches]').disabled"), true);
const before = ev('JSON.stringify(S.caches)');
ev("ACT.utilityCaches({city:'fra',quote:q.key})");
assert.equal(ev('JSON.stringify(S.caches)'), before);
ev("S.pops.fra.routers=2;R.demTo.ggl=NET.ggl.cache.min;ACT.utilityCaches({city:'fra',quote:q.key,modal:true})");
assert.equal(ev('JSON.stringify(S.caches)'), before);
assert.match(ev("document.getElementById('modalCard').textContent"), /quote changed/);
ev("ACT.utilityCaches({city:'not-a-city',quote:''})");
assert.deepEqual(errors, []);
});
test('balanced caches keep serving when either site fails', t => {
const { ev, errors } = setup(t);
ev("Object.assign(S.pops.fra,{site2:true,routers:2,rgen:RG.length-1,bb:{d:5000}});S.caches=[{id:'one',city:'fra',net:'ggl'},{id:'two',city:'fra',net:'ggl'}];compute();ACT.utilityKit({city:'fra',cost:utilityKit('fra').cost})");
for (const site of ['a', 'b']) {
ev(`S.inc=[{id:'site-failure',type:'site',ref:'fra',site:'${site}',until:S.day+2}];compute()`);
assert.ok(ev('Object.values(R.useCache).reduce((a,b)=>a+b,0)') > 0);
assert.equal(ev(`S.caches.filter(c=>(c.site||'a')==='${site}').every(c=>!R.useCache[c.id])`), true);
}
assert.deepEqual(errors, []);
});

137
tests/utilities.test.js Normal file
View file

@ -0,0 +1,137 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
function setup(t, opts) {
const h = load(opts);
t.after(() => h.dom.window.close());
h.ev("ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;UI.speed=0;compute()");
return h;
}
test('redundancy kit matches normal purchases, including cash, monthly costs and save reload', t => {
const a = setup(t), b = setup(t);
const quote = JSON.parse(a.ev("JSON.stringify(utilityKit('fra'))"));
const cash = a.ev('S.cash'), monthly = a.ev('R.costMo');
a.ev("ACT.utilityKit({city:'fra',cost:utilityKit('fra').cost})");
b.ev("ACT.addGen({city:'fra'});ACT.addSite({city:'fra'});ACT.addRouter({city:'fra'})");
for (const expr of ['S.pops.fra', 'R.cost'])
assert.deepEqual(JSON.parse(a.ev(`JSON.stringify(${expr})`)), JSON.parse(b.ev(`JSON.stringify(${expr})`)));
assert.equal(a.ev('S.cash'), cash - quote.cost);
assert.equal(a.ev('S.cash'), b.ev('S.cash'));
assert.equal(a.ev('R.costMo'), monthly + quote.mo);
const before = a.ev('JSON.stringify(S)');
a.ev("ACT.utilityKit({city:'fra',cost:0})");
assert.equal(a.ev('JSON.stringify(S)'), before, 'repeat does not buy more hardware');
a.ev("save();S=migrate(JSON.parse(localStorage.getItem('sot-save-v1')));compute()");
assert.equal(a.ev('S.pops.fra.gen && S.pops.fra.site2 && S.pops.fra.routers===2'), true);
assert.deepEqual(a.errors, []);
assert.deepEqual(b.errors, []);
});
test('kit is all-or-nothing, rejects stale quotes and handles missing PoPs', t => {
const { ev, errors } = setup(t);
ev("window.quote=utilityKit('fra').cost;S.cash=quote-1");
const before = ev('JSON.stringify(S.pops.fra)');
ev("ACT.utilityKit({city:'fra',cost:quote})");
assert.equal(ev('JSON.stringify(S.pops.fra)'), before);
assert.equal(ev('S.cash'), ev('quote-1'));
ev("S.cash=1e8;ACT.utilityKit({city:'fra',cost:quote+1,modal:true})");
assert.equal(ev('JSON.stringify(S.pops.fra)'), before);
assert.match(ev("document.getElementById('modalCard').textContent"), /quote changed/);
ev("ACT.utilityKit({city:'not-a-city',cost:0});ACT.utilityKit({city:'ams',cost:0})");
assert.deepEqual(errors, []);
});
test('kit buys only missing components and preserves extra routers and service placement', t => {
const { ev, errors } = setup(t);
ev("S.pops.fra.routers=4;S.pops.fra.gen=true;S.pops.fra.rgen=3;S.transit.push({id:'test',city:'fra',net:'cog',tier:0,site:'a'});compute()");
const cash = ev('S.cash');
assert.equal(ev("utilityKit('fra').cost"), ev('SITE2_COST'));
ev("ACT.utilityKit({city:'fra',cost:utilityKit('fra').cost})");
assert.equal(ev('S.cash'), cash - ev('SITE2_COST'));
assert.equal(ev('S.pops.fra.routers'), 4);
assert.equal(ev('S.pops.fra.rgen'), 3);
assert.equal(ev('S.transit[0].site'), 'a');
assert.deepEqual(errors, []);
});
test('bulk peering covers rival IXs, respects policy and is idempotent', t => {
const { ev, errors } = setup(t);
ev("ACT.buildPop({city:'ams'});ACT.buyIx({city:'fra',tier:0,signed:true});ACT.buyIx({city:'ams',tier:0});ACT.buyIx({city:'ams~3',tier:0});ACT.rs({city:'ams'});compute()");
const expected = JSON.parse(ev("JSON.stringify(utilityPeers().flatMap(r=>r.requests.map(id=>id+'@'+r.ix)))"));
assert.ok(expected.length > 0);
assert.ok(expected.some(k => k.endsWith('@ams~3')));
const blocked = JSON.parse(ev("JSON.stringify(utilityPeers().flatMap(r=>r.blocked.map(b=>b.id+'@'+r.ix)))"));
assert.ok(blocked.length > 0);
const cash = ev('S.cash'), ports = ev('JSON.stringify(S.ixp)'), pnis = ev('JSON.stringify(S.pni)');
ev('ACT.utilityPeers()');
for (const k of expected) assert.equal(ev(`S.peers[${JSON.stringify(k)}]`), true, k);
for (const k of blocked) assert.equal(ev(`!!S.peers[${JSON.stringify(k)}]`), false, k);
assert.equal(ev('S.cash'), cash);
assert.equal(ev('JSON.stringify(S.ixp)'), ports);
assert.equal(ev('JSON.stringify(S.pni)'), pnis);
const peers = ev('JSON.stringify(S.peers)');
ev('ACT.utilityPeers()');
assert.equal(ev('JSON.stringify(S.peers)'), peers);
assert.equal(ev('utilityPeers().reduce((n,r)=>n+r.requests.length,0)'), 0);
assert.deepEqual(errors, []);
});
test('bulk peering skips disconnected IXs, route server coverage and existing down sessions', t => {
const { ev, errors } = setup(t);
assert.equal(ev('utilityPeers().length'), 0);
ev("ACT.utilityPeers();ACT.buyIx({city:'fra',tier:0,signed:true});ACT.rs({city:'fra'});S.peers['ggl@fra']=true;compute()");
assert.equal(ev("utilityPeers()[0].requests.includes('drz')"), false);
assert.equal(ev("utilityPeers()[0].requests.includes('ggl')"), false);
ev("ACT.cancelIx({city:'fra',sure:1,ok:1});ACT.utilityPeers()");
assert.equal(ev('utilityPeers().length'), 0);
assert.deepEqual(errors, []);
});
test('utilities are available from the menu and PoP Infra in every language', t => {
for (const lang of ['en', 'it', 'pt', 'fr', 'es']) {
const { ev, dom, errors } = setup(t, lang === 'en' ? {} : { lang });
ev('ACT.menu()');
assert.ok(dom.window.document.querySelector('[data-act="utilities"]'), lang);
ev("selectCity('fra');ACT.tab({t:'links'})");
assert.ok(dom.window.document.querySelector('#panelBody [data-act="utilityKit"]'), lang);
ev('ACT.utilities()');
assert.equal(dom.window.document.querySelector('#utilityCity').value, 'fra');
assert.ok(!dom.window.document.querySelector('#modalCard').textContent.includes('utilities.'), lang);
assert.deepEqual(errors, [], lang);
}
});
test('join all route servers preserves existing sessions and covers rival and remote IX ports', t => {
const { ev, errors } = setup(t);
ev("ACT.buyIx({city:'fra',tier:0,signed:true});ACT.rs({city:'fra'});window.fabric=FABS.find(f=>f.cities.includes('fra')&&f.cities.includes('ams')).id;S.links.push({id:'remote',a:'fra',b:'ams',kind:'vxc',prov:fabric,vt:1,km:kmBetween('fra','ams'),sub:false,mo:0,cut:0,day:S.day});ACT.buyIx({city:'ams',tier:0});ACT.buyIx({city:'ams~3',tier:0});compute()");
assert.equal(ev("isRemote('ams')"), true);
assert.deepEqual(JSON.parse(ev('JSON.stringify(utilityRouteServers().sort())')), ['ams', 'ams~3']);
const cash = ev('S.cash'), ports = ev('JSON.stringify(S.ixp)'), peers = ev('JSON.stringify(S.peers)');
ev('ACT.utilityRouteServers()');
assert.equal(ev("S.rs.fra && S.rs.ams && S.rs['ams~3']"), true);
assert.equal(ev("ixSessions('ams').has('drz')"), true);
assert.equal(ev('S.cash'), cash);
assert.equal(ev('JSON.stringify(S.ixp)'), ports);
assert.equal(ev('JSON.stringify(S.peers)'), peers);
const rs = ev('JSON.stringify(S.rs)');
ev('ACT.utilityRouteServers()');
assert.equal(ev('JSON.stringify(S.rs)'), rs, 'repeat never toggles a session off');
assert.equal(ev('utilityRouteServers().length'), 0);
assert.deepEqual(errors, []);
});
test('route-server batch leaves peering policy and player-owned exchanges alone', t => {
const { ev, errors } = setup(t);
ev('ACT.utilityRouteServers()');
assert.equal(ev('Object.keys(S.rs).length'), 0, 'does not join an IX without a port');
ev("ACT.buyIx({city:'fra',tier:0,signed:true});ACT.inboxPol({p:'closed'});ACT.utilityRouteServers()");
assert.equal(ev('ppol()'), 'closed');
assert.equal(ev('S.rs.fra'), true);
assert.equal(ev("ixSessions('fra').has('drz')"), false, 'does not bypass closed policy');
assert.match(ev("document.getElementById('modalCard').textContent"), /Off while your peering policy/);
ev("S.myIx.fra={members:[],fab:0};S.rs.fra=false;ACT.utilityRouteServers()");
assert.equal(ev('S.rs.fra'), false, 'owned exchange has no join-route-server control');
assert.deepEqual(errors, []);
});