const test = require('node:test'); const assert = require('node:assert/strict'); const { load } = require('./harness'); function base(ev) { ev("ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'})"); } test('#4 several transits in one city share the load instead of filling one by one', () => { const { ev, errors } = load(); base(ev); ev("ACT.buyTransit({city:'fra',net:'cog',tier:1});ACT.buyTransit({city:'fra',net:'lvl',tier:1});compute()"); const u = JSON.parse(ev("JSON.stringify(S.transit.map(t=>R.useT[t.id]||0))")); assert.equal(u.length, 2); assert.ok(u[0] > 0 && u[1] > 0, `both carry traffic (${u})`); assert.ok(Math.abs(u[0] - u[1]) / (u[0] + u[1]) < 0.35, `roughly balanced (${u})`); // local exits still win over far ones: a transit in London is not used while Frankfurt has room ev("ACT.buildPop({city:'lon'});UI.via=null;openLinkModal('fra','lon');ACT.buildLink({tier:3});closeModal();ACT.buyTransit({city:'lon',net:'tly',tier:1});compute()"); assert.equal(ev("R.useT[S.transit.find(t=>t.city==='lon').id]||0"), 0, 'remote transit stays idle'); // backup-only still means backup only ev("S.transit[1].pref='low';compute()"); assert.equal(ev("R.useT[S.transit[1].id]||0"), 0); assert.deepEqual(errors, []); }); test('#5 export writes a file and import restores it, with checks', () => { const { ev, dom, errors } = load(); const window = dom.window; base(ev); ev("S.name='Export Me';S.cash=1234567;compute()"); let blobText = null; window.URL.createObjectURL = b => { blobText = b; return 'blob:x'; }; window.URL.revokeObjectURL = () => {}; window.HTMLAnchorElement.prototype.click = function () { window.__dl = this.download; }; ev("ACT.exportSave()"); assert.match(window.__dl, /^series-of-tubes-export-me-\d{4}-\d\d-\d\d\.json$/); const txt = ev("JSON.stringify({app:'series-of-tubes',version:APP_VERSION,S})"); assert.ok(blobText); ev("S.cash=1;S.name='Other'"); assert.match(ev(`parseSave('{"nope":1}').err`), /does not look/); assert.match(ev(`parseSave('not json').err`), /not valid JSON/); assert.match(ev(`parseSave(JSON.stringify(Object.assign(JSON.parse(${JSON.stringify(txt)}).S,{lost:true}))).err`), /bankrupt/); ev(`UI.importing=parseSave(${JSON.stringify(txt)}).s;ACT.importGo()`); assert.equal(ev("S.name"), 'Export Me'); assert.equal(ev("S.cash"), 1234567); assert.equal(ev("JSON.parse(localStorage.getItem('sot-save-v1')).name"), 'Export Me', 'saved on this device'); ev("showHelp()"); assert.match(ev("document.getElementById('modalCard').textContent"), /Export save.*Import a save/); assert.deepEqual(errors, []); }); test('#6 full transit, IX, caches and routers show up in the NOC and on the map', () => { const { ev, errors } = load(); base(ev); ev("S.pops.fra.bb.d=5000;ACT.buyTransit({city:'fra',net:'cog',tier:0});ACT.buyIx({city:'ams',tier:0,signed:true});S.caches.push({id:'cx',city:'fra',net:'akm'});compute()"); ev("R.nodeLoad.fra=R.nodeCap.fra;UI.hotMap=hotByCity()"); const ks = JSON.parse(ev("JSON.stringify(hotPorts().map(h=>h.k))")); for (const k of ['t', 'c', 'r']) assert.ok(ks.includes(k), `${k} is reported hot (${ks})`); assert.ok(ev("UI.hotMap.fra&&UI.hotMap.fra.t>=0.9")); ev("ACT.leftTab({t:'noc'})"); // NOC reads R as it is now (router forced hot above) const noc = ev("document.getElementById('leftBody')?document.getElementById('leftBody').textContent:document.body.textContent"); assert.match(noc, /running hot/); assert.match(noc, /Akamaybe Box cache/); assert.match(noc, /transit/); ev("draw&&draw(performance.now())"); assert.deepEqual(errors, []); });