seriesoftubes/tests/modes.test.js
2026-10-25 10:29:49 +01:00

254 lines
13 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
test('sandbox: endless money, every unlock, nothing breaks, no badges', () => {
const { ev, errors } = load();
ev("ACT.modeSet({m:'sandbox'});ACT.start({})");
assert.equal(ev("S.mode"), 'sandbox');
assert.equal(ev("S.lvl"), 4);
ev("S.cash=5;for(let i=0;i<120;i++)dayTick()");
assert.ok(ev("S.cash") >= 1e8, 'money refills');
assert.equal(ev("S.stats.incidents"), 0, 'no failures');
assert.equal(ev("Object.keys(S.ach||{}).length"), 0, 'no badges in sandbox');
assert.match(ev("document.getElementById('hAsn').textContent"), /Sandbox/);
assert.deepEqual(errors, []);
});
test('scenarios: rules apply, goals complete, deadlines fail, seeds repeat', () => {
const { ev, errors } = load();
ev("ACT.scenarios()");
assert.match(ev("document.getElementById('modalCard').textContent"), /Weekly challenge/);
ev("ACT.startScn({id:'stg'})");
assert.equal(ev("S.home"), 'stg');
ev("ACT.buyTransit({city:'stg',net:'drz',tier:1})");
assert.equal(ev("S.transit.length"), 0, 'no transit in the Spätzle-IX scenario');
ev("ACT.startScn({id:'redsea',sure:1})");
assert.equal(ev("cablesOk('mrs','sin').length"), 0, 'Red Sea routes are gone');
assert.ok(ev("cablesFor('mrs','sin').length") > 0);
ev("ACT.startScn({id:'carnaval',sure:1})");
assert.ok(ev("S.biz.some(b=>b.type==='event'&&b.start===90)"), 'Carnaval is pre-booked');
ev("S.day=99;S.scn.done=0;dayTick()");
assert.ok(ev("!!S.scn.failed"), 'deadline passes');
assert.match(ev("document.getElementById('modalCard').textContent"), /Time's up/);
// weekly: same seed gives the same first random numbers
ev("ACT.startWeekly({sure:1})");const a = ev("[Math.random(),Math.random()].join()");
ev("ACT.startWeekly({sure:1})");const b = ev("[Math.random(),Math.random()].join()");
assert.equal(a, b);
ev("S.stats.peak=1234;S.day=364;dayTick()");
assert.match(ev("document.getElementById('modalCard').textContent"), /score/i);
assert.ok(ev("!!JSON.parse(localStorage.getItem('sot-scn')).weekly"));
assert.deepEqual(errors, []);
});
test('completing a scenario records it and unlocks a badge', () => {
const { ev, errors } = load();
ev("localStorage.removeItem('sot-scn');ACT.startScn({id:'spat'});R.reach=1;R.served=200;scnTick(10)");
assert.equal(ev("S.scn.done"), 10);
assert.ok(ev("!!JSON.parse(localStorage.getItem('sot-scn')).spat"));
ev("checkBadges()");
assert.ok(ev("S.ach.scen1!==undefined"));
assert.deepEqual(errors, []);
});
test('traceroute shows hops, the exit and the AS path', () => {
const { ev, errors } = load();
ev("ACT.start({});S.cash=1e9;S.lvl=4;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'})");
ev("ACT.buildPop({city:'ams'});UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:1});closeModal();ACT.buyTransit({city:'ams',net:'lvl',tier:1});ACT.buyIx({city:'fra',tier:1,signed:true});ACT.rs({city:'fra'});compute()");
let r = JSON.parse(ev("JSON.stringify(traceroute('fra',{net:'nfx'}))"));
assert.match(r.text, /traceroute to/);
assert.match(r.text, /ams/, 'goes via Amsterdam to transit');
assert.deepEqual(r.asPath.slice(0, 2), [ev('S.asn'), ev("NET.lvl.asn")]);
r = JSON.parse(ev("JSON.stringify(traceroute('fra',{net:'akm'}))"));
assert.match(r.why, /peering/i, 'open network over the IX route server');
r = JSON.parse(ev("JSON.stringify(traceroute('ams',{city:'fra'}))"));
assert.match(r.text, /fra/);
ev("S.links[0].cut=S.day+3;compute()");
r = JSON.parse(ev("JSON.stringify(traceroute('fra',{net:'nfx'}))"));
assert.ok(/unreachable/.test(r.text) || /peering/i.test(r.why));
ev("ACT.trace({city:'fra'})");
assert.match(ev("document.getElementById('modalCard').textContent"), /Traceroute/);
ev("ACT.share()");
assert.deepEqual(errors, []);
});
test('conferences: register, attend, and selective peers get easier for a while', () => {
const { ev, errors } = load();
ev("ACT.start({});S.cash=1e9;for(const g of GOALS)S.done[g.id]=0");
ev("S.day=confDay(CONFS.find(c=>c.c==='fra'),0)-20;ACT.leftTab({t:'deals'})");
assert.match(ev("document.body.textContent"), /DENOG/);
const key = ev("upcomingConfs(30).find(c=>c.c==='fra').key");
ev(`ACT.confGo({key:'${key}'})`);
const need0 = ev("(()=>{R.demTo.nfx=0;return peerOk('nfx','ix','fra').why})()");
ev("for(let i=0;i<21;i++)dayTick()");
assert.ok(ev("S.conf&&S.conf.until>S.day"), 'conference glow is on');
const need1 = ev("(()=>{R.demTo.nfx=0;return peerOk('nfx','ix','fra').why})()");
assert.notEqual(need0, need1, 'threshold dropped');
ev("checkGoals()");
assert.ok(ev("S.ach.hallway!==undefined"));
assert.deepEqual(errors, []);
});
test('the guided tutorial walks through the first steps and can be skipped', () => {
const { ev, dom, errors } = load();
dom.window.__TUBES_TUT__ = true;
ev("localStorage.removeItem('sot-tut');ACT.start({})");
assert.equal(ev("document.getElementById('coach').hidden"), false);
assert.match(ev("document.getElementById('coach').textContent"), /Launch broadband/);
ev("ACT.launchBB({city:S.home});renderAll()");
assert.match(ev("document.getElementById('coach').textContent"), /Step 2/);
ev("ACT.tutSkip()");
assert.equal(ev("document.getElementById('coach').hidden"), true);
assert.equal(ev("localStorage.getItem('sot-tut')"), '1');
assert.deepEqual(errors, []);
});
test('animations can be switched off, on or follow the system, and the choice sticks', () => {
const { ev, errors } = load();
ev("ACT.start({});ACT.menu()");
assert.match(ev("document.getElementById('modalCard').textContent"), /Settings/);
ev("ACT.settings()");
assert.match(ev("document.getElementById('modalCard').textContent"), /Animations/);
ev("ACT.animSet({v:'off'})");
assert.equal(ev("document.documentElement.classList.contains('no-anim')"), true);
assert.equal(ev("animOn()"), false);
ev("floater(0,0,'x','#000')");
assert.equal(ev("FLOATS.length"), 0, 'no floating text when off');
assert.equal(ev("localStorage.getItem('sot-anim')"), 'off');
ev("ACT.animSet({v:'on'})");
assert.equal(ev("document.documentElement.classList.contains('anim-force')"), true);
assert.equal(ev("animOn()"), true);
ev("ACT.soundSet({v:'off'})");
assert.equal(ev("UI.muted"), true);
assert.deepEqual(errors, []);
});
test('half speed and deeper zoom', () => {
const { ev, errors } = load();
ev("ACT.start({});ACT.speed({v:0.5})");
assert.equal(ev("UI.speed"), 0.5);
assert.equal(ev("document.getElementById('sp0.5').classList.contains('on')"), true);
ev("ACT.speed({v:7})");
assert.equal(ev("UI.speed"), 0.5, 'bogus speeds are ignored');
ev("cam.z=1;for(let i=0;i<40;i++)zoomAt(100,100,1.2)");
assert.equal(ev("cam.z"), 6);
assert.deepEqual(errors, []);
});
test('the map draws at every zoom, with and without animations, without throwing', () => {
const { ev, errors } = load();
ev("ACT.start({});S.cash=1e9;S.lvl=4;ACT.launchBB({city:'fra'});for(const c of ['ams','lon','nyc'])ACT.buildPop({city:c});UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:1});closeModal();compute()");
for (const z of [0.35, 0.8, 1.4, 3, 6]) ev(`cam.z=${z};draw(performance.now());draw(0)`);
ev("ACT.animSet({v:'off'});frame(performance.now()+16)");
assert.ok(ev("ctx&&ctx.calls.n") > 100, 'the canvas was actually drawn on');
assert.deepEqual(errors, []);
});
test('RouteScript: parses safely, reports errors helpfully, and changes routing', () => {
const { ev, errors } = load();
ev("ACT.start({});S.cash=1e9;S.lvl=4;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'})");
ev("ACT.buyTransit({city:'fra',net:'cog',tier:1});ACT.buyTransit({city:'fra',net:'lvl',tier:1});ACT.buyTE();compute()");
let p = JSON.parse(ev("JSON.stringify(teParse('spill at 80%\\ncap transit cogentle at 3 gbps\\nprefer ix for goggle\\nnonsense here\\navoid transit for gogle'))"));
assert.equal(p.errors.length, 2, 'the nonsense line and the typo fail');
assert.match(p.errors[1], /Did you mean Goggle/, 'typos get a suggestion');
p = JSON.parse(ev("JSON.stringify(teParse('avoid transit for zzzzqqq').errors)"));
assert.match(p[0], /unknown network/);
assert.match(ev("teParse('drain pariss').errors[0]"), /Did you mean Paris/);
assert.equal(ev("teParse('alert(1)').n"), 0, 'nothing is ever evaluated');
const cog = "R.useT[S.transit.find(t=>t.net==='cog').id]||0";
const before = ev(cog);
ev("ACT.teEdit();document.getElementById('teSrc').value='cap transit cogentle at 3 gbps';ACT.teDeploy()");
assert.ok(ev(cog) <= 3.0001 && before > 3, `Cogentle capped (${before} -> ${ev(cog)})`);
assert.ok(ev("R.useT[S.transit.find(t=>t.net==='lvl').id]") > 0, 'overflow went to the other upstream');
// a fat-fingered deploy earns its badge
ev("ACT.teEdit();document.getElementById('teSrc').value='drain frankfurt\\ncap transit level tree at 0 gbps\\ncap transit cogentle at 0 gbps';ACT.teDeploy();checkGoals()");
assert.ok(ev("S.ach.fat!==undefined"));
// errors block a deploy
ev("ACT.teEdit();document.getElementById('teSrc').value='spill at 5%';ACT.teDeploy()");
assert.match(ev("document.getElementById('modalCard').textContent"), /commit failed/);
ev("ACT.teClear()");
assert.equal(ev("S.te.script"), '');
assert.deepEqual(errors, []);
});
test('#27 new networks get a random name, and "Another one" never repeats it', () => {
const { ev, errors } = load();
assert.ok(ev("NETWORK_NAMES.every(n=>n.length<=24)&&new Set(NETWORK_NAMES).size===NETWORK_NAMES.length"));
const first = ev("document.getElementById('nmIn').value");
assert.ok(ev(`NETWORK_NAMES.includes(${JSON.stringify(first)})`));
let prev = first;
for (let i = 0; i < 40; i++) {
ev("ACT.randomName()");
const now = ev("document.getElementById('nmIn').value");
assert.notEqual(now, prev);
prev = now;
}
ev("document.getElementById('nmIn').value='My Own Net';ACT.start({})");
assert.equal(ev("S.name"), 'My Own Net');
assert.deepEqual(errors, []);
});
test('iPhone home-screen app fills the screen when iOS reports a short viewport', () => {
const { ev, dom, errors } = load();
const w = dom.window;
const set = (o, k, v) => Object.defineProperty(o, k, { value: v, configurable: true });
set(w.navigator, 'userAgent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148');
set(w.navigator, 'standalone', true);
set(w, 'screen', { width: 390, height: 844 });
set(w, 'innerWidth', 390); set(w, 'innerHeight', 797); // one status bar short
ev('resize()');
assert.equal(ev("document.documentElement.classList.contains('iosfix')"), true);
assert.equal(ev("document.documentElement.style.getPropertyValue('--app-h')"), '844px');
assert.equal(ev('CH'), 844, 'the map is drawn to the real bottom');
// landscape: the short side of the screen
set(w, 'innerWidth', 844); set(w, 'innerHeight', 370);
ev('resize()');
assert.equal(ev('CH'), 390);
// once fixed, iOS reports a full viewport: the fix must stay on, or the strip comes back
set(w, 'innerWidth', 390); set(w, 'innerHeight', 844);
ev('resize()');
assert.equal(ev("document.documentElement.classList.contains('iosfix')"), true);
assert.equal(ev('CH'), 844);
// Safari in a tab leaves the page alone
set(w.navigator, 'standalone', false); set(w, 'innerHeight', 700);
ev('resize()');
assert.equal(ev("document.documentElement.classList.contains('iosfix')"), false);
assert.equal(ev('CH'), 700);
assert.deepEqual(errors, []);
});
test('phones in the browser get the install pitch before a new game, then can still play', () => {
const { ev, dom, errors } = load();
const w = dom.window;
const set = (o, k, v) => Object.defineProperty(o, k, { value: v, configurable: true });
set(w.navigator, 'userAgent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Mobile/15E148 Safari/604.1');
set(w, 'innerWidth', 390); set(w, 'innerHeight', 760);
ev('showTitle()');
assert.match(ev("document.getElementById('modal').textContent"), /Best played as an app/, 'title screen card');
ev('ACT.start({})');
const modal = () => ev("document.getElementById('modal').textContent");
assert.equal(ev('!!S&&UI.started'), false, 'the game waits for the pitch');
assert.match(modal(), /Play it as an app/);
assert.match(modal(), /does not carry over/, 'iPhone players learn to install first');
assert.match(modal(), /Add to Home Screen/);
ev('ACT.pitchSkip()');
assert.equal(ev('UI.started&&!!S.pops[S.home]'), true, 'playing in the browser still works');
// only once per session
ev('ACT.newgame();ACT.start({})');
assert.equal(ev('UI.started'), true);
assert.deepEqual(errors, []);
});
test('the installed app and desktop browsers start straight away', () => {
for (const [w0, sa] of [[390, true], [1280, false]]) {
const { ev, dom, errors } = load();
const w = dom.window;
const set = (o, k, v) => Object.defineProperty(o, k, { value: v, configurable: true });
set(w.navigator, 'userAgent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15');
set(w.navigator, 'standalone', sa);
set(w, 'innerWidth', w0); set(w, 'innerHeight', 760);
ev('ACT.start({})');
assert.equal(ev('UI.started'), true, `width ${w0}, standalone ${sa}`);
assert.deepEqual(errors, []);
}
});