📦 ${tr('Supply chain')}
+
${tr('Hardware prices')}${t>1.005?tr('+{p} since you started',{p:pct(t-1)}):tr('as when you started')}
+ ${s?`
${tr('Shortage')}${tr('{what} +{p} · {n} days left',{what:tr(HW_SHOCK[s.k].n),p:pct(s.mul-1),n:s.until-S.day})}
`:''}
+ ${l?`
${tr('Price lock')}${tr('until {date}, at +{p}',{date:dateStr(l.until),p:pct(l.mul-1)})}
`
+ :`
${tr('Routers, optics and new builds at today’s prices for a year, shortages included. Tariffs are the government’s, not your vendor’s.')}
${btn(tr('Lock prices for a year {cost}',{cost:fmt$(hwLockCost())}),'hwLock',{},'sm',S.cash`}
`;}
+const routerCost=g=>Math.round((ROUTER_COST+RG[g].cost*0.8)*hwMul('rt'));
// Old routers go on eBuy when you swap models or remove one (#82), like spare optics (#45). Chassis age faster.
-const ROUTER_RESALE=0.3,tradeIn=(g,n)=>Math.round(RG[g].cost*n*ROUTER_RESALE),swapCost=(c,g)=>{const p=S.pops[c],rt=p.routers||1;return Math.max(0,RG[g].cost*rt-tradeIn(p.rgen||0,rt));};
+const ROUTER_RESALE=0.3,tradeIn=(g,n)=>Math.round(RG[g].cost*n*ROUTER_RESALE),swapCost=(c,g)=>{const p=S.pops[c],rt=p.routers||1;return Math.max(0,Math.round(RG[g].cost*rt*hwMul('rt'))-tradeIn(p.rgen||0,rt));};
const RMAX=6;
// The plain, badge-less model of each class is the default upgrade path; the others are side-grades.
const mainRG=k=>RG.findIndex(r=>r.cls===k&&!r.badge);
@@ -720,7 +742,7 @@ const OPT={
tp32:{n:'Sienna WaveMagic 3.2T',g:3200,cost:130000,reach:1500,modes:[[3200,1500],[1600,4000],[1200,5500],[800,7000],[400,12000]],rg:4,lvl:8,sys:'roadm',blurb:tr('Twice the WaveMagic. Full speed only on shorter spans, and a cooling fan you can hear from the car park.')},
};
const dfLeaseMo=km=>Math.round(400+km*1.0);
-const dfBuildCost=km=>Math.round(25000+km*120);
+const dfBuildCost=km=>Math.round((25000+km*120)*hwMul('build'));
const dfBuildDays=km=>Math.round(120+km*0.25);
const ampCount=km=>Math.max(0,Math.floor(km/AMP_EVERY));
const fmtCap=g=>g>=1000?(+(g/1000).toFixed(1))+'T':g+'G';
@@ -1767,7 +1789,7 @@ function scenarioModal(){const wk=weekly(),h=scnHof();let best=null;try{best=JSO
function newGame(name,home){
S={v:1,name,asn:200000+Math.floor(_rand()*15000),home,day:0,cash:START_CASH,
ltv:2,t1v:2,auV:1,seen:NETS.map(n=>n.id),spares:{},pops:{[home]:{day:0,bb:null,boost:0,down:0,routers:1,gen:false}},links:[],nni:[],transit:[],ixp:{},rs:{},peers:{},pni:[],caches:[],biz:[],offers:[],myIx:{},
- done:{},inc:[],ev:{},priceMul:1,nextFail:45,nextEvent:40,usedNames:[],stats:{survived:0,incidents:0,peak:0},cnLic:{state:'none'},scrubs:[],scrubMode:'always',won:false,lost:false,neg:0,loans:[],loanOffers:[],loanStrikes:0,inbox:{items:[],down:{},tiny:{},rep:0,bot:false,log:[],seq:0}};
+ done:{},inc:[],ev:{},priceMul:1,nextFail:45,nextEvent:40,usedNames:[],stats:{survived:0,incidents:0,peak:0},cnLic:{state:'none'},scrubs:[],scrubMode:'always',won:false,lost:false,neg:0,loans:[],loanOffers:[],loanStrikes:0,inbox:{items:[],down:{},tiny:{},rep:0,bot:false,log:[],seq:0},hw:{}};
}
/* ================= HELPERS ================= */
@@ -2470,6 +2492,10 @@ function sanitize(s){
s.pni=s.pni.filter(x=>x&&isCity(x.city)&&isNet(x.net)).map(x=>(x.lacp=degO(x.lacp),x.site=['a','b','ab'].includes(x.site)?x.site:'a',x.id=sid(x.id),x.tier=tier(x.tier),x.n=lag(x.n),x.sacrif=!!x.sacrif,x));
// PNIs on order (#…): delivery day, a pending augment, and 1G for the one network that sells it
for(const x of s.pni){if(x.ready!==undefined)x.ready=num(x.ready,0);if(x.g1)x.g1=x.tier===0&&x.net==='dtk'?true:undefined;else delete x.g1;if(x.up){const u=x.up;x.up=u&&typeof u==='object'?{tier:tier(u.tier),n:lag(u.n),ready:num(u.ready,0),g1:!!u.g1&&x.net==='dtk'&&tier(u.tier)===0}:undefined;if(!x.up)delete x.up;}}
+ // Supply chain: a shortage and a price lock, both optional.
+ {const H=s.hw&&typeof s.hw==='object'&&!Array.isArray(s.hw)?s.hw:{},o={};const sh=H.shock,lk=H.lock;
+ if(sh&&typeof sh==='object'&&own(HW_SHOCK,sh.k))o.shock={k:sh.k,until:num(sh.until,0),mul:clamp(num(sh.mul,1),1,2)};
+ if(lk&&typeof lk==='object')o.lock={until:num(lk.until,0),mul:clamp(num(lk.mul,1),1,HW_CAP)};s.hw=o;}
{const I=s.inbox&&typeof s.inbox==='object'&&!Array.isArray(s.inbox)?s.inbox:{},o=v=>v&&typeof v==='object'&&!Array.isArray(v)?v:{},str=(v,n)=>String(v==null?'':v).slice(0,n);
const items=(Array.isArray(I.items)?I.items:[]).filter(x=>x&&typeof x==='object'&&(x.k==='req'||x.k==='mp')&&typeof x.ix==='string'&&isIx(x.ix)&&(x.net===undefined||isNet(x.net))&&(x.net!==undefined||typeof x.nm==='string')).slice(0,INBOX_MAX).map(x=>({id:int(x.id,0,1e9,0),k:x.k,ix:x.ix,net:x.net,nm:x.net?undefined:str(x.nm,40),asn:x.net?undefined:int(x.asn,1,4294967294,64512),day:num(x.day,0),stale:!!x.stale,yrs:int(x.yrs,1,30,1),say:int(x.say,0,REQ_SAY.length-1,0),bump:x.bump?1:undefined,key:x.k==='mp'?str(x.key,80):undefined,lim:x.k==='mp'?int(x.lim,1,1e7,100):undefined,got:x.k==='mp'?int(x.got,1,1e7,100):undefined,leak:x.k==='mp'?!!x.leak:undefined}));
const down={};for(const [k,v] of Object.entries(o(I.down)))if(typeof k==='string'&&k.length<80)down[k]=num(v,0);
@@ -2934,6 +2960,7 @@ function purgeInc(){S.inc=S.inc.filter(i=>{const T=i.type;if(LINKCUT_TR.includes
function fireEvent(){
const opts=['viral','price','grant'];
if(S.day>50)opts.push('sanction','tariff','hijack','landban','shutdown','redsea','baltic');
+ if(S.day>HW_FREE&&!hwShock())opts.push('hwshock');
if(S.day>90&&S.links.filter(l=>l.kind==='wave'&&!l.sub).length>=3)opts.push('ctrlplane');
if(Object.keys(S.pops).some(ruCity))opts.push('runet','runet');
if(S.pops.doh&&Object.keys(S.pops).some(c=>CITY[c].gcc||CITY[c].uae)&&!(S.ev.gulf>S.day))opts.push('gulf');
@@ -2959,6 +2986,7 @@ function fireEvent(){
toast(tr('
Faceplant rerouted Korea via Hong Kong.Seoul users get a fifth less of it for 20 days, however you connect.'),'bad');sfx('alert');return;}
if(e==='mega'){const c=pick(Object.keys(S.pops).filter(x=>CITY[x].saudi)),bw=pick([800,1600,3200]),start=S.day+15;S.offers.push({id:uid(),city:c,city2:null,name:tr('A futuristic city that is not built yet, demo day in {city}',{city:CITY[c].name}),type:'event',bw,rate:0,reqs:[],start,dur:4,fee:bw*4*90,exp:start-2});toast(tr('
A gigaproject wants a demo.{bw} for four days in {city}, to show off a city that is still mostly renderings. Check the Deals tab.',{bw:fmtG(bw),city:CITY[c].name}),'goal');}
if(e==='sanction'){S.ev.sanction=S.day+60;toast(tr('
Sanctions!The Republic of Paperworkistan sanctioned your router vendor’s favorite screw supplier. Spare parts are slow: router crashes take twice as long to fix for 60 days.'),'bad');sfx('alert');return;}
+ if(e==='hwshock'){hwShockStart();return;}
if(e==='tariff'){S.ev.tariff=S.day+90;toast(tr('
Trade war on opticsTwo governments slapped tariffs on each other’s lasers. Optics cost 40% more for 90 days.'),'bad');sfx('alert');return;}
if(e==='hijack'){const nh=NETS.map(n=>n.id).filter(()=>Math.random()<0.6),old=S.ev.rpki&&S.ev.rpki.until>S.day?S.ev.rpki:null;S.ev.rpki=old?{until:Math.max(old.until,S.day+1),nets:[...new Set(old.nets.concat(nh))]}:{until:S.day+1,nets:nh};addInc({type:'hijack',ref:'rpki',until:S.day+1,msg:tr('A state-owned ISP “accidentally” announced your prefixes for 18 minutes. Their apology press release was suspiciously well written. Many networks are still routing your traffic into the void.'),cost:1500,fix:tr('Call everyone you know')});toast(tr('
BGP hijackSomeone else is announcing your prefixes. Reach is down until it is cleaned up.'),'bad');sfx('alert');return;}
if(e==='landban'){const lc=[...new Set(CABLE_PAIRS.flatMap(c=>[c.a,c.b]))];const c=pick(lc);S.ev.landban={city:c,until:S.day+45};toast(tr('
Landing permit dispute in {city}Two ministries both claim the beach. No new subsea links can land there for 45 days.',{city:CITY[c].name}),'bad');return;}
@@ -3431,6 +3459,8 @@ const ACT={
if(d>0&&!spend(REGEN_COST+Math.round((l.ch||[]).reduce((a,c)=>a+optCost(c),0)*REGEN_CH)))return;l.regen=n;toast(d>0?tr('
Regen site {n} is up.Optical spans on {a}–{b} are now {km} km.',{n,a:CITY[l.a].name,b:CITY[l.b].name,km:spanKm(l).toLocaleString()}):tr('
Regen site removed.'),'good');sfx('build');after();},
addCh:({id,o})=>{const l=S.links.find(x=>x.id===id),O=OPT[o];if(!l||!O||l.kind!=='df')return;const why=chWhy(l,o);if(why){toast(tr("
Can't add {optic}.{why}",{optic:O.n,why:esc(why)}),'bad');sfx('err');return;}if(!portGate(l.a,1)||!portGate(l.b,1))return;const sp=spareN(o)>0;if(!spend(chCost(l,o)))return;if(sp)S.spares[o]--;(l.ch=l.ch||[]).push(o);floater((CITY[l.a].x+CITY[l.b].x)/2,(CITY[l.a].y+CITY[l.b].y)/2-20,`+${O.n}`,'#FF4F9A');after();},
rmCh:({id,o})=>{const l=S.links.find(x=>x.id===id);if(!l||!OPT[o])return;const i=(l.ch||[]).lastIndexOf(o);if(i<0)return;l.ch.splice(i,1);S.spares=S.spares||{};S.spares[o]=(S.spares[o]||0)+1;floater((CITY[l.a].x+CITY[l.b].x)/2,(CITY[l.a].y+CITY[l.b].y)/2-20,tr('{optic} to spares',{optic:OPT[o].n}),'#7B61FF');after();},
+ hwLock:()=>{if(hwLock())return;const c=hwLockCost();if(!spend(c))return;S.hw=S.hw||{};S.hw.lock={until:S.day+HW_LOCK_DAYS,mul:hwTrend(S.day)};toast(tr('
Prices locked until {date}.Your vendor’s account manager is delighted. He gets a bonus for this.',{date:dateStr(S.hw.lock.until)}),'good');after();},
+ buySpare:({o})=>{if(!OPT[o]||OPT[o].lvl>lvl()||spareN(o)>=64)return;if(!spend(optCost(o)))return;S.spares=S.spares||{};S.spares[o]=(S.spares[o]||0)+1;sfx('build');after();},
sellSpare:({o})=>{if(!OPT[o]||spareN(o)<1)return;S.spares[o]--;const v=spareValue(o);S.cash+=v;toast(tr('
Sold a spare {optic} on eBuy.{price} back, which is {p} of what it cost new.',{optic:OPT[o].n,price:fmt$(v),p:pct(SPARE_RESALE)}),'good');sfx('coin');after();},
addPair:({id})=>{const l=S.links.find(x=>x.id===id);if(!l||l.kind!=='df'||!l.own||!isRdm(l.sys)||l.ready>S.day)return;const n=Math.max(1,l.pairs|0);if(n>=pairsMax(l))return;if((l.ch||[]).length+soldSlots(l)
Fill this pair first.
Light all {max} channels before you light another fiber pair.',{max:chMax(l)}),'bad');sfx('err');return;}if(!spend(pairCost(l)))return;l.pairs=n+1;toast(tr('Fiber pair {n} is lit: {a}–{b}.
Another ROADMaster 9000 and a fresh chain of amplifiers. Room for {m} more channels.',{n:n+1,a:CITY[l.a].name,b:CITY[l.b].name,m:DF_SYS[l.sys].max}),'good');sfx('build');after();},
upSys:({id})=>{const l=S.links.find(x=>x.id===id),to=l&&sysNext(l);if(!to||DF_SYS[to].lvl>lvl())return;if(!spend(sysUpCost(l)))return;l.sys=to;if(l.ready<=S.day){l.eqDown=S.day+1;toast(tr('Line system swap
All channels are dark for a day while the ROADMs go in.'),'bad');}after();},
@@ -3486,7 +3516,7 @@ function dfAllowed(a,b){if(isSub(a,b))return !!subDfCable(a,b);return cnCity(a)=
for free (only the regen sites' transponders are new), or you sell it on eBuy for part of the price. */
const SPARE_RESALE=0.4,spareN=o=>(S&&S.spares&&S.spares[o])|0,spareValue=o=>Math.round(optCost(o)*SPARE_RESALE);
function chCost(l,o){const r=REGEN_CH*(l.regen|0);return Math.round(optCost(o)*(spareN(o)>0?r:1+r));}
-function optCost(o){return Math.round(OPT[o].cost*(S&&S.ev&&S.ev.tariff>S.day?1.4:1));}
+function optCost(o){return Math.round(OPT[o].cost*hwMul('opt')*(S&&S.ev&&S.ev.tariff>S.day?1.4:1));}
// Line systems upgrade in place: Rainbow → ROADMaster → C+L. Every lit pair gets the new gear.
const sysNext=l=>l.sys==='lite'?'roadm':l.sys==='roadm'?'cl':null,sysUpCost=l=>{const t=sysNext(l);return t?DF_SYS[t].cost*(t==='cl'&&l.own?Math.max(1,l.pairs|0):1):0;};
function pairBtn(l){if(!l.own||!isRdm(l.sys)||l.ready>S.day)return '';const n=Math.max(1,l.pairs|0);if(n>=pairsMax(l))return `${tr('{n} fiber pairs lit, the trench is full',{n})}`;
@@ -3670,6 +3700,7 @@ function renderLeft(){checkLayer();
:`Auto-TE${lvl()<3?tr('Unlocks at customer level 3'):tr('Not installed')}
${tr('Traffic takes the shortest path, even when it is full. A traffic-engineering controller spills the overflow onto the next-best paths with room.')}
${btn(tr('Install Auto-TE
{cost} + {mo}/mo',{cost:fmt$(TE_COST),mo:fmt$(TE_MO)}),'buyTE',{},'pink sm',lvl()<3||S.cash
`;
const hot=S.links.filter(l=>!linkDown(l)&&capOf(l)>0&&(R.load[l.id]||0)/capOf(l)>=0.85).sort((a,b)=>(R.load[b.id]/capOf(b))-(R.load[a.id]/capOf(a)));
if(hot.length){h+=`${tr('Congested links')}
${S.te&&S.te.on?'':''+tr('Raise a full link’s metric so traffic prefers another path, upgrade it, or install Auto-TE.')+'
'}`+hot.map(l=>`${CITY[l.a].name} ⇄ ${CITY[l.b].name}
${capName(l)} · ${tr('{p} full',{p:pct((R.load[l.id]||0)/capOf(l))})}
${btn(tr('Inspect'),'selLink',{id:l.id},'pink sm')}
`).join('');}
+ h+=hwCard();
const slow=Object.keys(S.pops).filter(latSlow).sort((a,b)=>R.latF[a]-R.latF[b]);
if(slow.length)h+=`🐢 ${tr('Slow cities')}
${tr('Their users go out through transit far away. Past 50 ms round trip, broadband and internet services count as bad service.')}
`+slow.map(c=>{const f=R.latFar[c];return `${CITY[c].name} → ${CITY[f.to].name}
${tr('{p} of its traffic · {ms} ms round trip · service {sf}',{p:pct(Math.min(1,f.share)),ms:Math.round(f.ms),sf:pct(R.latF[c])})}
${btn(tr('Inspect'),'focusCity',{city:c},'ghost sm')}
`;}).join('');
const hotR=hotPorts();
@@ -3916,7 +3947,8 @@ function dfPanel(l){
for(const k of Object.keys(OPT)){const O=OPT[k],n=(l.ch||[]).filter(x=>x===k).length,why=chWhy(l,k);
const md=optMode(k,l),cc=chCost(l,k),sp=spareN(k);
h+=`${O.n} ${optModes(k,l).map(([g,r])=>tr('{g} to {km} km',{g:fmtG(g),km:r.toLocaleString()})).join(' · ')}
${esc(O.blurb)}${n?` · ${tr('{n} lit',{n})}`:''}${sp?` · ${trn('{n} spare on the shelf','{n} spares on the shelf',sp)}`:''}
${md&&!md.full?`
⚠️ ${tr('Runs at {g} per channel on this route (long-haul mode).',{g:fmtG(md.g)})} ${!l.sub&®enNeed(k,l)>(l.regen|0)?trn('{n} regen site would bring it back to {g}.','{n} regen sites would bring it back to {g}.',regenNeed(k,l),{g:fmtG(O.g)}):''}
`:''}${why?`
${esc(why)}
`:''}
${n?btn('−','rmCh',{id:l.id,o:k},'ghost sm'):''}${btn(sp?`+
${cc?tr('spare + {price}',{price:fmt$(cc)}):tr('spare')}`:`+
${fmt$(cc)}`,'addCh',{id:l.id,o:k},'sm',!!why||S.cash
`;}
- {const st=Object.keys(OPT).filter(k=>spareN(k)>0);h+=`${tr('Unplugged optics go on the spares shelf and are used first next time.')}
`;if(st.length)h+=`${tr('Spares shelf')}
`+st.map(k=>`${spareN(k)} × ${OPT[k].n}
${btn(tr('Sell one
{price}',{price:fmt$(spareValue(k))}),'sellSpare',{o:k},'ghost sm')}
`).join('');}
+ {const st=Object.keys(OPT).filter(k=>spareN(k)>0);h+=`${tr('Unplugged optics go on the spares shelf and are used first next time.')}
`;
+ {const used=[...new Set(l.ch||[])].filter(k=>OPT[k]&&OPT[k].lvl<=lvl());if(used.length)h+=`${used.map(k=>btn(tr('Stock a spare {optic} {price}',{optic:OPT[k].n,price:fmt$(optCost(k))}),'buySpare',{o:k},'ghost sm',S.cash=64)).join('')}
${tr('Spares bought now are already paid for when the next shortage hits.')}
`;}if(st.length)h+=`${tr('Spares shelf')}
`+st.map(k=>`${spareN(k)} × ${OPT[k].n}
${btn(tr('Sell one
{price}',{price:fmt$(spareValue(k))}),'sellSpare',{o:k},'ghost sm')}
`).join('');}
const rg=l.regen|0,rgAdd=REGEN_COST+Math.round((l.ch||[]).reduce((a,c)=>a+optCost(c),0)*REGEN_CH);
h+=`${tr('Line system')}
${esc(sysD.n)}${tr('{n} amplifiers',{n:amps})}
${tr('Regen sites')}${rg?tr('{n} · optical spans of {km} km',{n:rg,km:spanKm(l).toLocaleString()}):tr('none')}
${tr('A regen site turns the light back into bits halfway and sends it on fresh, so optics only have to reach the next site. Each needs a regen card for every lit channel. {price}/mo per site for the hut.',{price:fmt$(REGEN_MO)})}
${l.sub&&l.own?`${tr('No regen sites in the middle of the ocean: long cables run their optics in long-haul mode.')}
`:`${rg
{price}',{price:fmt$(rgAdd)}),'regen',{id:l.id,d:1},'sm',S.cash`}${esc(sysD.blurb)}
${(()=>{const t=sysNext(l);if(!t)return '';const T=DF_SYS[t],c=sysUpCost(l);return T.lvl>lvl()?btn(tr('{sys} level {n}',{sys:T.n,n:T.lvl}),'upSys',{id:l.id},'sm',true):btn(tr('Upgrade to {sys} {price}',{sys:T.n,price:fmt$(c)}),'upSys',{id:l.id},'sm',S.cash{price}/mo',{price:fmt$(Math.round(150+(l.ch||[]).length*80))}),'spares',{id:l.id},l.spares?'green sm':'sm')}
${tr('Spare optics and amplifier cards on the shelf turn multi-day hardware failures into one-day swaps.')}
`;
@@ -4355,6 +4387,7 @@ function showHelp(){
${tr('IX (Internet Exchange)')}${tr('A shared switch in a city. One port, many free peering sessions. The route server peers you with every open member at once.')}
PNI${tr('Private network interconnect: a cable straight to one network in the same building. For big traffic.')}
${tr('PNI lead times')}${tr('A cross-connect with a small network takes a few weeks. With the big content networks, Tier 1s and big eyeball networks, a new PNI or an augment usually takes about a month, but one order in seven slips to 6 to 12 months. The content networks and Tier 1s start at 100G. Doitsche Telekomm still sells 1G.')}
+ ${tr('Supply chain')}${tr('Routers, optics and new builds get dearer over the years, about 4% a year after the first six months, and now and then a shortage makes one of them a lot dearer for a month or two. What you already own keeps its price. Spares on the shelf and a yearly price lock from the NOC tab soften the blow.')}
${tr('Backhaul latency')}${tr('Users feel the round trip to wherever their traffic leaves your network for transit. Up to 20 ms (about 1,500 km) is fine; past 50 ms broadband and internet services count as bad service, which costs revenue and customers. Traffic over your own peering, PNIs and caches is not penalised.')}
${tr('Peering inbox')}${tr('Every IX port brings peering requests and max-prefix alerts, in the Networks tab. A session that blows past its limit is down until you look: raise the limit for growth, keep it down for a leak. Ignore your mail and selective networks get pickier.')}
${tr('Embedded cache')}${tr('A CDN\'s server in your rack. Serves most of that CDN\'s traffic locally, for the price of power.')}
@@ -4397,7 +4430,7 @@ const OWNCAB_SHIPS=['MV Cable Guy','Spool of Thought','Wet Willy','Reel Deal','S
const landing=c=>CABLE_PAIRS.some(p=>p.a===c||p.b===c);
const nearLanding=c=>CITIES.filter(x=>x.id!==c&&landing(x.id)).sort((x,y)=>kmBetween(c,x.id)-kmBetween(c,y.id))[0].id;
const ownCabKm=(a,b)=>seaRoutes(a,b)[0]?.km||Infinity;
-const ownCabCost=km=>2*OWNCAB_LAND+km*OWNCAB_KM+DF_SYS.roadm.cost;
+const ownCabCost=km=>Math.round((2*OWNCAB_LAND+km*OWNCAB_KM+DF_SYS.roadm.cost)*hwMul('build'));
const ownCabMo=km=>Math.round(km*OWNCAB_OM)+ampCount(km)*AMP_MO;
const ownCabDays=km=>Math.round(300+km*0.05);
// Marine routing uses the same coastline as the map, in geographic coordinates.
diff --git a/tests/supply.test.js b/tests/supply.test.js
new file mode 100644
index 0000000..7e2219a
--- /dev/null
+++ b/tests/supply.test.js
@@ -0,0 +1,73 @@
+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, []);
+});