seriesoftubes/tests/supply.test.js
Frederic Jung 1b691ca9af
Supply chain: hardware prices drift up, shortages come and go, spares and price locks
hwMul() scales new purchases of routers (routerCost, swapCost), optics
(optCost, on top of tariffs) and own builds (dfBuildCost, ownCabCost):
free for 180 days, then +4%/year, capped at +30%. Monthly costs of what
you own do not move.

Shortages are a new random event after day 180, never two at once:
optics +50%, routers +40% or builds +30%, for 45-75 days. Counter-moves:
stock spare optics from a dark fiber link at today's price, or buy a
365-day price lock from the NOC (80k + 30k per POP, double during a
shortage), which freezes today's trend and ignores shortages.

Measured on a 16-POP European network ($530k/mo revenue): the curve
adds ~5% of revenue a year in year 1 and ~23% by year 5 if every dollar
is reinvested in hardware; a lock costs about a month of revenue.
2026-10-27 09:25:47 +01:00

73 lines
3.7 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
function setup(ev) {
ev("ACT.start({});S.cash=1e9;S.lvl=6;S.nextFail=S.nextEvent=1e9;for(const g of GOALS)S.done[g.id]=0;compute()");
}
test('hardware gets dearer over the years, gently, and only for new purchases', () => {
const { ev, errors } = load();
setup(ev);
const o = ev('Object.keys(OPT)[0]');
const at = d => JSON.parse(ev(`S.day=${d};JSON.stringify([optCost('${o}'),routerCost(1),dfBuildCost(500),ownCabCost(5000)])`));
const d0 = at(0);
assert.deepEqual(at(180), d0, 'nothing changes in the first six months');
const y1 = at(180 + 365);
assert.ok(y1.every((v, i) => Math.abs(v / d0[i] - 1.04) < 0.01), `+4% a year (${y1} vs ${d0})`);
const late = at(180 + 365 * 30);
assert.ok(late.every((v, i) => Math.abs(v / d0[i] - 1.3) < 0.01), 'capped at +30%');
// what you already own costs what it cost: monthly bills do not move
ev("S.day=0;ACT.buyTransit({city:'fra',net:'cog',tier:1});compute()");
const mo = ev('R.costMo');
ev('S.day=180+365*5;compute()');
assert.equal(ev('R.costMo'), mo);
assert.deepEqual(errors, []);
});
test('shortages hit one kind of hardware for a month or two; spares and a price lock soften them', () => {
const { ev, errors } = load();
setup(ev);
const o = ev('Object.keys(OPT)[0]');
ev('S.day=200');
const calm = JSON.parse(ev(`JSON.stringify([optCost('${o}'),routerCost(1),dfBuildCost(500)])`));
// a spare bought in calm times is already paid for
ev(`ACT.buySpare({o:'${o}'})`);
assert.equal(ev(`spareN('${o}')`), 1);
ev("Math.random=()=>0;hwShockStart()");
assert.equal(ev('S.hw.shock.k'), 'opt');
const n = ev('S.hw.shock.until-S.day');
assert.ok(n >= 45 && n <= 75, `a month or two (${n} days)`);
assert.equal(ev(`optCost('${o}')`), Math.round(ev(`OPT['${o}'].cost*hwTrend(S.day)*1.5`)));
assert.equal(ev('routerCost(1)'), calm[1], 'only optics');
assert.match(ev("document.getElementById('toasts').textContent"), /Laser shortage/);
// the NOC shows it, and a lock costs double during a shortage
ev("UI.sel=null;UI.left='noc';UI.leftOpen=true;renderLeft()");
assert.match(ev("document.getElementById('left').textContent"), /Supply chain[\s\S]*Optics \+50%/);
const dear = ev('hwLockCost()');
ev('S.hw.shock.until=S.day');
assert.equal(ev('hwLockCost()') * 2, dear);
// a lock keeps today's prices for a year, whatever happens
const cash = ev('S.cash');
ev('ACT.hwLock()');
assert.equal(cash - ev('S.cash'), ev('hwLockCost()'));
ev("S.day+=100;Math.random=()=>0.4;hwShockStart()");
assert.equal(ev('S.hw.shock.k'), 'rt');
assert.equal(ev('routerCost(1)'), calm[1], 'locked through a chip shortage');
assert.equal(ev("S.day+=300;dfBuildCost(500)"), Math.round((25000 + 500 * 120) * ev('hwTrend(S.day)')), 'and then it runs out');
// events: only after six months, and never two shortages at once
ev("S.day=100;S.hw={}");
assert.equal(ev("(()=>{const o=[];if(S.day>HW_FREE&&!hwShock())o.push(1);return o.length})()"), 0);
assert.deepEqual(errors, []);
});
test('saves: shortages and locks survive, garbage is repaired', () => {
const { ev, errors } = load();
setup(ev);
ev("S.hw={shock:{k:'opt',until:'90',mul:9},lock:{until:400,mul:'x'}};window.__a=JSON.parse(JSON.stringify(S));sanitize(__a)");
assert.deepEqual(JSON.parse(ev('JSON.stringify(__a.hw)')), { shock: { k: 'opt', until: 90, mul: 2 }, lock: { until: 400, mul: 1 } });
ev("S.hw={shock:{k:'nope'}};window.__b=JSON.parse(JSON.stringify(S));sanitize(__b);window.__c=JSON.parse(JSON.stringify(S));delete __c.hw;sanitize(__c)");
assert.equal(ev('JSON.stringify(__b.hw)'), '{}');
assert.equal(ev('JSON.stringify(__c.hw)'), '{}', 'old saves get an empty supply chain');
assert.deepEqual(errors, []);
});