v&&typeof v==='object'?{until:num(v.until,0),f:Math.min(1,Math.max(0,num(v.f,1)))}:null;
s.transit=s.transit.filter(t=>t&&isCity(t.city)&&isNet(t.net)).map(t=>(t.lacp=degO(t.lacp),t.site=['a','b','ab'].includes(t.site)?t.site:'a',t.id=sid(t.id),t.tier=tier(t.tier),t.n=lag(t.n),t.down=num(t.down,0),t.clean=!!t.clean,t));
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));
diff --git a/src/game/90-actions/60-links.js b/src/game/90-actions/60-links.js
index c816d7f..0e6faff 100644
--- a/src/game/90-actions/60-links.js
+++ b/src/game/90-actions/60-links.js
@@ -3,7 +3,7 @@ Object.assign(ACT,{
decomOpen:({city})=>{UI.decom=new Set(city&&S.pops[city]?[city]:[]);decomModal();},
decomToggle:({city})=>{if(!S.pops[city])return;UI.decom=UI.decom||new Set();if(UI.decom.has(city))UI.decom.delete(city);else UI.decom.add(city);decomModal();},
decomGo:()=>{const sel=[...(UI.decom||[])].filter(c=>S.pops[c]);if(!sel.length||sel.length>=Object.keys(S.pops).length)return;closeModal();UI.decom=null;UI.sel=null;decommission(sel);sfx('alert');after();},
- rmLink:({id})=>{if(UI.confirm!==id){UI.confirm=id;renderPanel();return;}{const l=S.links.find(x=>x.id===id),back=cabBack(l);if(back>0){S.cash+=back;toast(tr('links.name_is_called_off_the_cable',{name:esc(l.cname||tr('common.your_cable')),price:fmt$(back)}),'good');}}S.links=S.links.filter(l=>l.id!==id);UI.sel=null;UI.confirm=null;after();},
+ rmLink:({id})=>{if(UI.confirm!==id){UI.confirm=id;renderPanel();return;}{const l=S.links.find(x=>x.id===id),back=cabBack(l);if(back>0){S.cash+=back;toast(tr('links.name_is_called_off_the_cable',{name:esc(l.cname||tr('common.your_cable')),price:fmt$(back)}),'good');}}S.links=S.links.filter(l=>l.id!==id);prunePairMet();UI.sel=null;UI.confirm=null;after();},
fixInc:({id})=>{const i=S.inc.find(x=>x.id===id);if(!i||!(i.cost>0))return;if(i.slow&&i.rushed)return;if(!spend(i.cost))return;
if(i.slow&&i.until>S.day+1){i.rushed=true;i.cost=0;i.until=S.day+1;const p=S.pops[i.ref];if(p)p.down=Math.min(p.down,S.day+1);toast(tr('links.power_cycled_the_router_in_city',{city:esc(CITY[i.ref].name),chip:esc(RG[(p&&p.rgen)||0].chip)}),'good');after();return;}clearInc(i);toast(tr('links.fixed_what_resolved_early',{what:incTitle(i.type)}),'good');after();},
myIxUp:({city})=>{const m=S.myIx[city];if(!m)return;const f=(m.fab|0)+1,N=MYIX_FAB[f];if(!N)return;if(N.lvl>lvl()){toast(tr('links.a_fab_fabric_unlocks_at_customer',{fab:N.n,lv:N.lvl}),'bad');sfx('err');return;}if(!spend(N.cost))return;m.fab=f;toast(tr('links.ix_now_runs_a_fab_fabric',{ix:esc(m.name),fab:N.n,max:N.max}),'good');m.next=Math.min(m.next,S.day+3);after();},
@@ -26,8 +26,9 @@ Object.assign(ACT,{
const km=kmBetween(a,b),sub=isSub(a,b),mo=vxcMo(km,sub,t);if(!spend(mo))return;S.links.push({id:uid(),a,b,kind:'vxc',prov,vt:t,km,sub,mo,cut:0,day:S.day});closeModal();
toast(tr('links.circuit_up_in_59_seconds_vxc',{vxc:VXC[t].n,fab:esc(FAB[prov].name),a:CITY[a].name,b:CITY[b].name})+(S.pops[b]?'':' '+tr('links.now_grab_an_ix_port_there')),'good');if(!S.pops[b])selectCity(b);after();},
upVxc:({id,t})=>{t=+t;const l=S.links.find(x=>x.id===id);if(!l||l.kind!=='vxc'||!VXC[t]||t<=l.vt)return;const extra=VXC[t].g-VXC[l.vt].g;for(const c of [l.a,l.b]){const pt=nniPort(c,l.prov);if(pt&&nniUsed(c,l.prov)+extra>NNI[pt.tier].g){toast(tr('links.fabric_port_in_city_is_full',{city:CITY[c].name}),'bad');sfx('err');return;}}const mo=vxcMo(l.km,l.sub,t);if(!spend(mo))return;l.vt=t;l.mo=mo;after();},
- cancelVxc:({id})=>{if(UI.confirm!==id){UI.confirm=id;renderPanel();return;}S.links=S.links.filter(l=>l.id!==id);const rem=remoteCities();for(const c in S.ixp){if(!S.pops[ixCity(c)]&&!rem.includes(ixCity(c))){delete S.ixp[c];delete S.rs[c];for(const k in S.peers)if(k.endsWith('@'+c))delete S.peers[k];}}UI.sel=null;UI.confirm=null;after();},
+ cancelVxc:({id})=>{if(UI.confirm!==id){UI.confirm=id;renderPanel();return;}S.links=S.links.filter(l=>l.id!==id);prunePairMet();const rem=remoteCities();for(const c in S.ixp){if(!S.pops[ixCity(c)]&&!rem.includes(ixCity(c))){delete S.ixp[c];delete S.rs[c];for(const k in S.peers)if(k.endsWith('@'+c))delete S.peers[k];}}UI.sel=null;UI.confirm=null;after();},
setPref:({kind,id,v})=>{if(!own(PREF,v))return;if(kind!=='t'&&kind!=='p'&&!(isIx(id)&&S.ixp[id]!==undefined))return;if(kind==='t'){const t=S.transit.find(x=>x.id===id);if(t)t.pref=v;}else if(kind==='p'){const x=S.pni.find(y=>y.id===id);if(x)x.pref=v;}else{S.ixPref=S.ixPref||{};S.ixPref[id]=v;}sfx('click');after();},
- setMetric:({id,v})=>{if(!own(MET,v))return;const l=S.links.find(x=>x.id===id);if(!l)return;for(const x of pairLinks(l.a,l.b))x.metric=v;sfx('click');after();},
+ setMetric:({id,v})=>{if(!own(MET,v))return;const l=S.links.find(x=>x.id===id);if(!l)return;const m=S.pairMet||(S.pairMet={}),k=edgeKey(l);if(v==='normal')delete m[k];else m[k]=v;sfx('click');after();},
+ setStandby:({id,on})=>{const l=S.links.find(x=>x.id===id);if(!l)return;if(on)l.standby=true;else delete l.standby;sfx('click');after();},
goEgress:({city,k})=>{ACT.focusCity({city});UI.tab=k==='t'?'up':k==='l2'?'cust':k==='nni'?'links':'peer';renderPanel();},
});
diff --git a/src/game/90-actions/90-action-helpers.js b/src/game/90-actions/90-action-helpers.js
index 67c8055..0efe713 100644
--- a/src/game/90-actions/90-action-helpers.js
+++ b/src/game/90-actions/90-action-helpers.js
@@ -4,6 +4,8 @@ function landTrenches(a,b){const out=[];for(const l of pairLinks(a,b)){if(l.kind
// Which route a new wave joins: null = a new diverse route. 'same' is the old two-way choice (the main route).
function routeJoin(a,b){const ts=landTrenches(a,b),r=UI.route||'';if(r==='same')return ts[0]||null;if(r.startsWith('t:'))return ts.find(t=>t.k===r.slice(2))||null;return null;}
function pairLinks(a,b){return S.links.filter(l=>(l.a===a&&l.b===b)||(l.a===b&&l.b===a));}
+// A city pair's metric goes with its last link, so a new link between the same cities starts on normal.
+function prunePairMet(){const m=S.pairMet;if(!m)return;const k=new Set(S.links.map(edgeKey));for(const x of Object.keys(m))if(!k.has(x))delete m[x];}
// Short, busy subsea routes (London–Amsterdam, London–Paris) sell dark fiber pairs too.
function subDfCable(a,b){return cablesOk(a,b).filter(c=>CAB[c.cid].df).sort((x,y)=>x.km-y.km)[0]||null;}
function dfAllowed(a,b){if(isSub(a,b))return !!subDfCable(a,b);return cnCity(a)===cnCity(b)&&!uaePair(a,b)&&!landX(a,b);}
diff --git a/src/game/93-ui/60-decommission.js b/src/game/93-ui/60-decommission.js
index 148b08a..4386098 100644
--- a/src/game/93-ui/60-decommission.js
+++ b/src/game/93-ui/60-decommission.js
@@ -13,7 +13,7 @@ function decomModal(){const sel=[...(UI.decom||[])],im=decomImpact(sel),all=Obje
${left<1?`${tr('decommission.keep_at_least_one_pop')}
`:''}
${btn(sel.length?trn('decommission.decommission_n_pop_pay_fee',sel.length,{fee:fmt$(im.fee)}):tr('decommission.pick_a_pop'),'decomGo',{},'red',!sel.length||left<1)}${btn(tr('common.keep_them'),'closeModal',{},'ghost')}
`);}
function decommission(cities){const set=new Set(cities),im=decomImpact(cities),lids=new Set(im.links.map(l=>l.id)),bids=new Set(im.biz.map(b=>b.id));
- S.links=S.links.filter(l=>!lids.has(l.id));S.biz=S.biz.filter(b=>!bids.has(b.id));S.offers=S.offers.filter(o=>!bCities(o).some(c=>set.has(c)));
+ S.links=S.links.filter(l=>!lids.has(l.id));prunePairMet();S.biz=S.biz.filter(b=>!bids.has(b.id));S.offers=S.offers.filter(o=>!bCities(o).some(c=>set.has(c)));
S.transit=S.transit.filter(t=>!set.has(t.city));S.pni=S.pni.filter(x=>!set.has(x.city));S.caches=S.caches.filter(x=>!set.has(x.city));S.nni=(S.nni||[]).filter(x=>!set.has(x.city));
for(const c of cities){for(const x of ixIdsIn(c)){delete S.ixp[x];delete S.rs[x];if(S.ixn)delete S.ixn[x];if(S.ixSite)delete S.ixSite[x];for(const k in S.peers)if(k.endsWith('@'+x))delete S.peers[k];}delete S.myIx[c];S.scrubs=S.scrubs.filter(x=>x.city!==c);delete S.pops[c];}
const rem=remoteCities();for(const x in S.ixp){const c=ixCity(x);if(!S.pops[c]&&!rem.includes(c)){delete S.ixp[x];delete S.rs[x];for(const k in S.peers)if(k.endsWith('@'+x))delete S.peers[k];}}
diff --git a/src/game/93-ui/70-link-panels.js b/src/game/93-ui/70-link-panels.js
index 19b6eb7..0378b3f 100644
--- a/src/game/93-ui/70-link-panels.js
+++ b/src/game/93-ui/70-link-panels.js
@@ -51,8 +51,13 @@ function trafficSection(l){
if(u>=0.8&&egs.length){const d=R.egDesc[egs[0][0]];h+=`${tr('link_panels.biggest_feeder_egress_in_city_set',{egress:esc(d.lab),city:CITY[d.city].name})}
`;}
return h+``;
}
-function metricSection(l){const cur=l.metric||'normal';
- return `${tr('common.routing_metric')}
${tr('link_panels.your_routers_pick_the_lowest_cost')}
${Object.keys(MET).map(k=>btn(MET[k].l,'setMetric',{id:l.id,v:k},cur===k?'pink sm':'ghost sm')).join('')}
`;}
+function metricSection(l){const cur=pairMet(edgeKey(l)),pl=pairLinks(l.a,l.b),sb=!!l.standby;
+ let h=`${tr('common.routing_metric')}
${tr('link_panels.your_routers_pick_the_lowest_cost')}
${Object.keys(MET).map(k=>btn(MET[k].l,'setMetric',{id:l.id,v:k},cur===k?'pink sm':'ghost sm')).join('')}
`;
+ // Standby only means something next to another link; a link left on standby alone can still be taken off it.
+ if(pl.length>1||sb){const busy=(R.load[l.id]||0)>1e-3,alone=!pl.some(x=>x!==l&&!x.standby&&!linkDown(x));
+ const st=!sb?tr('link_panels.a_link_on_standby_carries_traffic'):!busy?tr('link_panels.on_standby_idle_while_the_other'):alone?tr('link_panels.on_standby_but_carrying_the_traffic'):tr('link_panels.on_standby_taking_the_overflow');
+ h+=`${tr('common.standby')}
${st}
${sb?btn(tr('link_panels.take_it_off_standby'),'setStandby',{id:l.id,on:0},'pink sm'):btn(tr('link_panels.put_this_link_on_standby'),'setStandby',{id:l.id,on:1},'ghost sm')}
`;}
+ return h+``;}
function chinaSection(c){const st=S.cnLic?S.cnLic.state:'none',loss=S.ev.gfw>S.day?0.35:0.2;
let h=`${tr('link_panels.behind_the_great_firewall')}
${tr('link_panels.mainland_china_plays_by_its_own',{list:CN_BLOCKED.slice(0,4).map(esc).join(', '),pct:pct(loss)})}
`;
if(st==='none')h+=`${tr('link_panels.foreign_networks_need_a_local_joint')}
${btn(tr('link_panels.apply_for_a_license_price_mo',{price:fmt$(CN_LIC),mo:fmt$(CN_JV_MO)}),'applyCn',{},'pink',S.casha+optCost(c),0)*REGEN_CH);
h+=`${tr('common.line_system')}
${esc(sysD.n)}${tr('link_panels.n_amplifiers',{n:amps})}
${tr('common.regen_sites')}${rg?tr('link_panels.n_optical_spans_of_km_km',{n:rg,km:spanKm(l).toLocaleString()}):tr('common.none')}
${tr('link_panels.a_regen_site_turns_the_light',{price:fmt$(REGEN_MO)})}
${l.sub&&l.own?`${tr('link_panels.no_regen_sites_in_the_middle')}
`:`${rg
`}${esc(sysD.blurb)}
${(()=>{const t=sysNext(l);if(!t)return '';const T=DF_SYS[t],c=sysUpCost(l);return T.lvl>lvl()?btn(tr('link_panels.sys_level_n',{sys:T.n,n:T.lvl}),'upSys',{id:l.id},'sm',true):btn(tr('link_panels.upgrade_to_sys_price',{sys:T.n,price:fmt$(c)}),'upSys',{id:l.id},'sm',S.cash
${tr('link_panels.spare_optics_and_amplifier_cards_on')}
`;
- {const sib=pairLinks(l.a,l.b).filter(x=>x!==l&&x.kind!=='vxc');h+=`${tr('common.redundancy')}
${sib.length?tr('link_panels.other_links_between_these_cities'):tr('link_panels.this_is_the_only_link_between')}
${sib.map(x=>`${esc(capName(x))} ${linkDown(x)?''+tr('common.cut')+'':''}
${(x.trench||x.id)===(l.trench||l.id)?tr('common.same_trench'):tr('common.diverse_route')}
${btn(tr('common.open'),'selLink',{id:x.id},'ghost sm')}
`).join('')}${btn(tr('link_panels.add_parallel_link'),'addParallel',{id:l.id},'pink sm')}
`;}
+ {const sib=pairLinks(l.a,l.b).filter(x=>x!==l&&x.kind!=='vxc');h+=`${tr('common.redundancy')}
${sib.length?tr('link_panels.other_links_between_these_cities'):tr('link_panels.this_is_the_only_link_between')}
${sib.map(x=>`${esc(capName(x))} ${linkDown(x)?''+tr('common.cut')+'':''}${x.standby?' '+tr('common.standby')+'':''}
${(x.trench||x.id)===(l.trench||l.id)?tr('common.same_trench'):tr('common.diverse_route')}
${btn(tr('common.open'),'selLink',{id:x.id},'ghost sm')}
`).join('')}${btn(tr('link_panels.add_parallel_link'),'addParallel',{id:l.id},'pink sm')}
`;}
h+=`${trafficSection(l)}${metricSection(l)}${btn(UI.confirm===l.id?tr('link_panels.really_give_it_up'):(l.own?tr('link_panels.abandon_the_fiber'):tr('link_panels.cancel_the_iru')),'rmLink',{id:l.id},'ghost sm')}${cabBack(l)>0?`${tr('link_panels.giving_it_up_now_returns_price',{price:fmt$(cabBack(l))})}
`:''} `;
return h;
}
diff --git a/src/game/96-map/60-draw.js b/src/game/96-map/60-draw.js
index 8b2a2a3..fe22967 100644
--- a/src/game/96-map/60-draw.js
+++ b/src/game/96-map/60-draw.js
@@ -27,7 +27,7 @@ function drawFrame(now){
// links
const far=lodFar(),PI=pairIdx(),LG=linkGroups(),GM=LG.map(g=>linkGeom(g[0],PI.get(edgeKey(g[0])))),busy=linesOnScreen(GM,ks)>MAP_BUSY;
for(let gi=0;gi1,up=many?grp.filter(x=>!linkDown(x)):null;
- const gm=GM[gi],cap=many?up.reduce((a,x)=>a+capOf(x),0):capOf(l),load=many?grp.reduce((a,x)=>a+(R.load[x.id]||0),0):(R.load[l.id]||0),u=cap?load/cap:0,df=l.kind==='df',bld=df&&l.ready>day,cut=!bld&&(many?!up.length:linkDown(l))&&!(df&&!(l.ch||[]).length),dark=df&&!bld&&!(l.ch||[]).length,vx=l.kind==='vxc';
+ const gm=GM[gi],cap=many?up.reduce((a,x)=>a+capOf(x),0):capOf(l),load=many?grp.reduce((a,x)=>a+(R.load[x.id]||0),0):(R.load[l.id]||0),u=cap?load/cap:0,df=l.kind==='df',bld=df&&l.ready>day,cut=!bld&&(many?!up.length:linkDown(l))&&!(df&&!(l.ch||[]).length),dark=df&&!bld&&!(l.ch||[]).length,vx=l.kind==='vxc',idle=!cut&&!bld&&load<1e-3&&(many?up.length>0&&up.every(x=>x.standby):!!l.standby);
const sel=UI.sel&&UI.sel.type==='link'&&grp.some(x=>x.id===UI.sel.id);let lab=null;
const w=(vx?2.2:(2.5+Math.max(...grp.map(tierW))*1.8+(many?Math.min(1.5,(grp.length-1)*0.5):0)))*sc*(far?0.5:1),np=gm.pts.length;
if(SX.length{ctx.beginPath();ctx.moveTo(SX[0],SY[0]);for(let i=1;i0.001?utilCol(u):'#7FD8EE'):utilCol(u);ctx.lineWidth=w;ctx.stroke();ctx.setLineDash([]);
+ ctx.strokeStyle='#1E2640';ctx.lineWidth=w+4;ctx.setLineDash(cut?[8,8]:vx?[2,7]:bld?[10,8]:idle?[4,6]:[]);path();ctx.stroke();
+ ctx.strokeStyle=bld?'#C9B27C':dark?'#5D6788':cut?'#FF4D5E':idle?'#9AA3BE':vx?FAB[l.prov].col:l.sub?(u>0.001?utilCol(u):'#7FD8EE'):utilCol(u);ctx.lineWidth=w;ctx.stroke();ctx.setLineDash([]);
// Still the same path: strokes keep it, so the dotted fiber line goes on top without building it again.
if(df&&!bld&&!dark&&!busy){ctx.strokeStyle='rgba(255,255,255,.75)';ctx.lineWidth=Math.max(1,w*0.25);ctx.setLineDash([1,6]);ctx.stroke();ctx.setLineDash([]);}
const at=t=>polyAtXY(SX,SY,gm.cum,t);
diff --git a/src/i18n/en.js b/src/i18n/en.js
index 78b4a71..0c27614 100644
--- a/src/i18n/en.js
+++ b/src/i18n/en.js
@@ -1250,6 +1250,7 @@
"common.spreadsheet_ipam":"Spreadsheet IPAM",
"common.stage_six":"Stage Six",
"common.standard":"Standard",
+"common.standby":"Standby",
"common.start_a_new_network":"Start a new network",
"common.start_building":"Start building",
"common.start_construction":"Start construction",
@@ -2093,6 +2094,7 @@
"link_modal.waves_on_the_same_route_share":"Waves on the same route share its fate: one backhoe cuts them all.",
"link_modal.you_already_have_n_link_here":{"one":"You already have {n} link here ({list}). Parallel links add capacity. How should this one be routed?","other":"You already have {n} links here ({list}). Parallel links add capacity. How should this one be routed?"},
"link_panels.a_bigger_fabric_carries_more_traffic":"A bigger fabric carries more traffic and has room for more members. Your router here still has to keep up.",
+"link_panels.a_link_on_standby_carries_traffic":"A link on standby carries traffic only when the other links between these cities are full or down.",
"link_panels.a_regen_site_turns_the_light":"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.",
"link_panels.abandon_the_fiber":"Abandon the fiber",
"link_panels.add_parallel_link":"Add parallel link",
@@ -2152,6 +2154,9 @@
"link_panels.not_present_in_any_of_your":"Not present in any of your cities yet",
"link_panels.nothing_spare_to_sell_on_this":"Nothing spare to sell on this fiber right now.",
"link_panels.off_while_your_peering_policy_is":"Off while your peering policy is {pol}",
+"link_panels.on_standby_but_carrying_the_traffic":"On standby, but carrying the traffic: no active link between these cities is up.",
+"link_panels.on_standby_idle_while_the_other":"On standby: idle while the other links between these cities have room.",
+"link_panels.on_standby_taking_the_overflow":"On standby: taking the overflow, because the other links between these cities are full.",
"link_panels.open_peer_but_not_on_any":"Open peer, but not on any IX where you have a POP. Nearest: {city}",
"link_panels.open_peer_but_not_on_any_bfbb":"Open peer, but not on any IX where you have a POP",
"link_panels.open_peer_join_the_route_server":"Open peer: join the route server at {ix}",
@@ -2169,6 +2174,7 @@
"link_panels.ports_at_p_of_the_usual":"Ports at {p}% of the usual price.",
"link_panels.ports_up_to_port_are_free":"Ports up to {port} are free.",
"link_panels.price_n_days":"{price} · {n} days",
+"link_panels.put_this_link_on_standby":"Put this link on standby",
"link_panels.ready_for_a_pni_in_city":"Ready for a PNI in {city}",
"link_panels.ready_in_n_days":"Ready in {n} days",
"link_panels.really_give_it_up":"Really give it up?",
@@ -2191,6 +2197,7 @@
"link_panels.spares_kit_on":"Spares kit on ✓",
"link_panels.stock_a_spare_optic_price":"Stock a spare {optic} {price}",
"link_panels.sys_level_n":"{sys} level {n}",
+"link_panels.take_it_off_standby":"Take it off standby",
"link_panels.there_s_no_ix_in_city":"There's no IX in {city} yet. Found one: networks join over time, pay you for ports, and peer with you for free.",
"link_panels.they_need_the_full_internet_your":"They need the full internet. Your reach is {pct}.",
"link_panels.this_fiber_is_dark_add_optics":"This fiber is dark. Add optics below to carry traffic.",
diff --git a/tests/link-metrics.test.js b/tests/link-metrics.test.js
new file mode 100644
index 0000000..e5ed30b
--- /dev/null
+++ b/tests/link-metrics.test.js
@@ -0,0 +1,139 @@
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { load } = require('./harness');
+
+// Waves between Frankfurt and Amsterdam (tier 3 is 800G, tier 0 is 10G), transit in Amsterdam, eyeballs in Frankfurt.
+function setup(ev, tiers = [3, 3]) {
+ ev("ACT.start({});S.cash=1e8;S.lvl=4;for(const g of GOALS)S.done[g.id]=0");
+ ev("ACT.buildPop({city:'ams'});for(const c in S.pops){S.pops[c].rgen=3;S.pops[c].routers=2}");
+ ev("ACT.launchBB({city:'fra'});ACT.buyTransit({city:'ams',net:'lvl',tier:3})");
+ tiers.forEach((t, i) => ev(`UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:${t}});closeModal();S.links[S.links.length-1].tag=${i}`));
+ ev("var L=i=>S.links.find(l=>l.tag===i),L1=L(0),L2=L(1);compute()");
+}
+const load_ = (ev, x) => ev(`R.load[${x}.id]||0`);
+const body = dom => dom.window.document.body.textContent;
+
+test('#122 the routing metric belongs to the city pair', () => {
+ const { ev, errors } = load();
+ setup(ev);
+ ev("ACT.setMetric({id:L2.id,v:'avoid'})");
+ assert.equal(ev("S.pairMet['ams|fra']"), 'avoid');
+ ev("ACT.selLink({id:L1.id});renderAll()");
+ assert.ok(ev("[...document.querySelectorAll('[data-act=\"setMetric\"]')].some(b=>b.className.includes('pink')&&/Avoid/.test(b.textContent))"), 'the other link shows the same metric');
+ ev("ACT.setMetric({id:L1.id,v:'normal'})");
+ assert.equal(ev("'ams|fra' in S.pairMet"), false);
+ assert.deepEqual(errors, []);
+});
+
+test('#122 a link on standby idles while the others have room and takes over when they fail', () => {
+ const { ev, dom, errors } = load();
+ setup(ev);
+ const total = ev("R.served");
+ assert.ok(load_(ev, 'L1') > 0 && load_(ev, 'L2') > 0, 'active links share the load');
+ ev("ACT.setStandby({id:L2.id,on:1});compute()");
+ assert.equal(ev("L2.standby"), true);
+ assert.ok(load_(ev, 'L1') > 0);
+ assert.equal(load_(ev, 'L2'), 0, 'standby link idles');
+ assert.ok(Math.abs(ev("R.served") - total) < 1e-6, 'nothing is lost');
+ ev("ACT.selLink({id:L2.id});renderAll()");
+ assert.match(body(dom), /On standby: idle while the other links/);
+ ev("L1.cut=S.day+5;compute();renderAll()");
+ assert.ok(load_(ev, 'L2') > 0, 'standby link takes over when the active one is cut');
+ assert.match(body(dom), /On standby, but carrying the traffic/);
+ // Every link on standby: they share the traffic like active ones, and the note does not claim the others are down.
+ ev("L1.cut=0;ACT.setStandby({id:L1.id,on:1});compute();renderAll()");
+ assert.ok(load_(ev, 'L1') > 0 && load_(ev, 'L2') > 0);
+ assert.match(body(dom), /no active link between these cities is up/);
+ ev("ACT.setStandby({id:L2.id,on:0})");
+ assert.equal(ev("'standby' in L2"), false);
+ assert.deepEqual(errors, []);
+});
+
+test('#122 a standby link takes the overflow when the active links are full', () => {
+ const { ev, dom, errors } = load();
+ setup(ev, [0, 3]); // 10G active, 800G next to it
+ const total = ev("R.served");
+ ev("ACT.setStandby({id:L2.id,on:1});compute()");
+ const cap = ev("capOf(L1)");
+ assert.ok(Math.abs(load_(ev, 'L1') - cap) < 1e-6, 'the active link fills up first');
+ assert.ok(load_(ev, 'L2') > 0, 'the rest spills onto the standby link');
+ assert.ok(Math.abs(ev("R.served") - total) < 1e-6, 'standby capacity is never wasted');
+ ev("ACT.selLink({id:L2.id});renderAll()");
+ assert.match(body(dom), /taking the overflow/);
+ assert.deepEqual(errors, []);
+});
+
+test('#122 the order links are built in does not change where traffic goes', () => {
+ const tiers = [3, 0, 3], standby = [true, false, false], seen = new Set();
+ for (const order of [[0, 1, 2], [2, 1, 0], [1, 0, 2], [2, 0, 1]]) {
+ const { ev, errors } = load();
+ ev("ACT.start({});S.cash=1e8;S.lvl=4;for(const g of GOALS)S.done[g.id]=0");
+ ev("ACT.buildPop({city:'ams'});for(const c in S.pops){S.pops[c].rgen=3;S.pops[c].routers=2}");
+ ev("ACT.launchBB({city:'fra'});ACT.buyTransit({city:'ams',net:'lvl',tier:3})");
+ for (const i of order) ev(`UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:${tiers[i]}});closeModal();const l=S.links[S.links.length-1];l.tag=${i};if(${standby[i]})ACT.setStandby({id:l.id,on:1})`);
+ ev("ACT.setMetric({id:S.links[0].id,v:'prefer'});compute()");
+ seen.add(ev("JSON.stringify([0,1,2].map(i=>Math.round((R.load[S.links.find(l=>l.tag===i).id]||0)*1e6)))"));
+ assert.deepEqual(errors, []);
+ }
+ assert.equal(seen.size, 1, [...seen].join(' vs '));
+});
+
+test('#122 a new link between the same cities adds capacity and keeps the pair metric', () => {
+ const { ev, errors } = load();
+ setup(ev, [0]);
+ ev("ACT.setMetric({id:L1.id,v:'avoid'});ACT.setStandby({id:L1.id,on:1});compute()");
+ const before = ev("R.served");
+ ev("UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:3});closeModal();compute()");
+ const nl = "S.links[S.links.length-1]";
+ assert.equal(ev(`'standby' in ${nl}`), false, 'a new link starts active');
+ assert.equal(ev("pairMet('ams|fra')"), 'avoid');
+ assert.ok(ev("R.served") > before, 'more capacity, more traffic');
+ // The metric leaves with the last link, so a fresh link later starts on normal.
+ for (const id of ev("S.links.map(l=>l.id)")) ev(`UI.confirm=null;ACT.rmLink({id:'${id}'});ACT.rmLink({id:'${id}'})`);
+ assert.equal(ev("'ams|fra' in S.pairMet"), false);
+ assert.deepEqual(errors, []);
+});
+
+test('#122 old saves keep routing as before: per-link metrics become the pair metric', () => {
+ const { ev, errors } = load();
+ setup(ev);
+ // Every link on avoid: the pair is on avoid.
+ ev("delete S.pairMet;L1.metric='avoid';L2.metric='avoid';S=migrate(JSON.parse(JSON.stringify(S)))");
+ assert.equal(ev("S.pairMet['ams|fra']"), 'avoid');
+ assert.equal(ev("S.links.some(l=>'metric' in l)"), false);
+ // Mixed metrics routed at the lowest one, all links carrying: no standby, normal.
+ ev("S.pairMet={};S.links[0].metric='backup';S.links[1].standby='yes';S=migrate(JSON.parse(JSON.stringify(S)));compute()");
+ assert.equal(ev("'ams|fra' in S.pairMet"), false);
+ assert.equal(ev("S.links.some(l=>'standby' in l||'metric' in l)"), false);
+ assert.ok(ev("S.links.every(l=>R.load[l.id]>0)"));
+ // Junk in the new field is dropped.
+ ev("S.pairMet={'ams|fra':'warp','fra|lon':'avoid'};S=migrate(JSON.parse(JSON.stringify(S)))");
+ assert.deepEqual(JSON.parse(ev("JSON.stringify(S.pairMet)")), {});
+ assert.deepEqual(errors, []);
+});
+
+test('#122 traceroute shows an active link, not one on standby', () => {
+ const { ev, errors } = load();
+ setup(ev);
+ ev("ACT.setStandby({id:L1.id,on:1})");
+ assert.equal(ev("trPaths('ams').path('fra')[0].l.id===L2.id"), true);
+ ev("ACT.setStandby({id:L1.id,on:0});ACT.setStandby({id:L2.id,on:1})");
+ assert.equal(ev("trPaths('ams').path('fra')[0].l.id===L1.id"), true);
+ assert.deepEqual(errors, []);
+});
+
+test('#122 the standby button shows next to another link, on desktop and phone', () => {
+ const { ev, dom, errors } = load();
+ setup(ev);
+ for (const w of [1280, 390]) {
+ ev(`window.innerWidth=${w};ACT.selLink({id:L1.id});renderAll()`);
+ const b = dom.window.document.querySelector('[data-act="setStandby"]');
+ assert.ok(b, `standby button at ${w}px`);
+ assert.match(b.textContent, /Put this link on standby/);
+ }
+ ev("S.links=S.links.filter(l=>l!==L2);ACT.selLink({id:L1.id});renderAll()");
+ assert.equal(dom.window.document.querySelector('[data-act="setStandby"]'), null, 'nothing to stand by for');
+ ev("L1.standby=true;renderAll()");
+ assert.match(dom.window.document.querySelector('[data-act="setStandby"]').textContent, /Take it off standby/);
+ assert.deepEqual(errors, []);
+});