import test from 'node:test'; import assert from 'node:assert/strict'; import {build} from 'esbuild'; const compiled=await build({entryPoints:[new URL('../src/core/fleet/routerObservation.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm'}); const {routerObservation,routerObservationKey}=await import('data:text/javascript;base64,'+Buffer.from(compiled.outputFiles[0].contents).toString('base64')); const device=()=>({id:'a',state:'ready',attached:true,observed_at:100,active_mode:'direct',selected_mode:'aggregation',active_slots:[1],internet:'unknown',modems:[1,2,3].map(slot=>({slot,operator:'op'+slot,connectivity:'online',radio:'enabled',rsrp:-90})),slot_map:[{slot:2,sim:1,position:'right'},{slot:3,sim:2,position:'center'},{slot:1,sim:3,position:'left'}],client_api:{details:{fallback:true},policy:{mode:'aggregation'}}}); test('viewport exposes actual SIM and physical order without inferring aggregation',()=>{ const v=routerObservation(device(),{available:true,fresh:true},101000); assert.deepEqual(v.channels.map(c=>c.label),['SIM1','SIM2','SIM3']); assert.equal(v.channels.find(c=>c.active).operator,'op1'); assert.equal(v.mode.current,'Прямой выход · SIM3'); assert.match(v.mode.message,/Агрегация задана/); assert.equal(v.internet,'Интернет не подтверждён'); assert.notEqual(routerObservationKey('a'),routerObservationKey('b')); }); test('stale transport, removed device and old device evidence cannot display a live path or signal',()=>{ for(const [d,snapshot,now] of [[device(),{available:true,fresh:false},101000],[{...device(),attached:false},{available:true},101000],[device(),{available:true},141000],[{...device(),state:'unreachable'},{available:true},101000]]){ const v=routerObservation(d,snapshot,now);assert.equal(v.ready,false);assert.equal(v.mode.activeSlot,null);assert.ok(v.channels.every(c=>c.rsrp==null&&!c.active)); } }); test('per-modem stale data and missing SIM do not reuse a previous strength; aggregation needs explicit mode',()=>{ const d=device();d.modems[0].radio_age_seconds=50;d.modems[1].radio='NOSIM';d.active_mode='aggregation';d.active_slots=[]; const v=routerObservation(d,{available:true,fresh:true},101000); assert.equal(v.mode.current,'Агрегация');assert.ok(v.channels.every(c=>!c.active)); assert.equal(v.channels[0].rsrp,null);assert.equal(v.channels[2].rsrp,null);assert.equal(v.channels[1].rsrp,-90); }); test('explicit per-channel freshness vetoes an otherwise fresh router snapshot',()=>{ const d=device();d.client_api.wan=[{slot:2,fresh:false}]; assert.equal(routerObservation(d,{available:true,fresh:true},101000).channels[0].rsrp,null); }); test('SIM indicators distinguish the effective exit from merely registered modems',()=>{ const d=device(),snapshot={available:true,fresh:true}; let v=routerObservation(d,snapshot,101000); assert.deepEqual(v.channels.map(c=>c.indicator),['neutral','neutral','success']); assert.equal(v.live,true);assert.equal(v.waiting,false); d.client_api.wan=[{slot:1,up:false,fresh:true}]; v=routerObservation(d,snapshot,101000); assert.equal(v.channels[2].indicator,'warning'); v=routerObservation(d,{available:true,fresh:false},101000); assert.equal(v.live,false);assert.equal(v.waiting,true); assert.ok(v.channels.every(c=>c.indicator==='neutral')); v=routerObservation({...d,attached:false},snapshot,101000); assert.equal(v.live,false);assert.equal(v.waiting,false); }); test('active indicator follows raw modem identity for every physical SIM permutation',()=>{ const maps=[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]; for(const order of maps)for(const active of [1,2,3]){ const d=device(); d.slot_map=order.map((slot,index)=>({slot,sim:index+1,position:['right','center','left'][index]})); d.modems.reverse(); // Payload order is independent of physical slot identity. d.active_slots=[active];d.selected_mode='aggregation'; d.client_api.policy.preferred_sim='M'+(active%3+1); const v=routerObservation(d,{available:true,fresh:true},101000); assert.deepEqual(v.channels.map(c=>c.indicator),order.map(slot=>slot===active?'success':'neutral')); assert.equal(v.channels.find(c=>c.active).operator,'op'+active); assert.equal(v.mode.current,`Прямой выход · SIM${order.indexOf(active)+1}`); } }); test('observed aggregation lights each connected fresh WAN in physical SIM order',()=>{ for(const active_mode of ['aggregation','proxy']){ const d=device();d.active_mode=active_mode;d.active_slots=[];d.client_api.details={}; d.client_api.wan=[3,1,2].map(slot=>({slot,up:true,fresh:true,sim_present:true,modem_present:true})); let v=routerObservation(d,{available:true,fresh:true},101000); assert.deepEqual(v.channels.map(c=>c.indicator),['success','success','success']); assert.ok(v.channels.every(c=>!c.active&&c.indicatorLabel==='Канал агрегации доступен')); for(const veto of [{up:false},{fresh:false},{sim_present:false},{modem_present:false}]){ d.client_api.wan=[{slot:2,up:true,fresh:true,...veto},{slot:1,up:true,fresh:true},{slot:3,up:true,fresh:true}]; v=routerObservation(d,{available:true,fresh:true},101000); assert.deepEqual(v.channels.map(c=>c.indicator),['neutral','success','success']); } } }); test('configured, pending, fallback or stale aggregation cannot invent green SIMs',()=>{ for(const details of [{pending:true},{fallback:true}]){ const d=device();d.active_mode='aggregation';d.active_slots=[];d.client_api.details=details; d.client_api.wan=[1,2,3].map(slot=>({slot,up:true,fresh:true})); assert.ok(routerObservation(d,{available:true,fresh:true},101000).channels.every(c=>c.indicator==='neutral')); } const d=device();d.active_mode='aggregation';d.active_slots=[];d.client_api.details={}; assert.ok(routerObservation(d,{available:true,fresh:true},101000).channels.every(c=>c.indicator==='neutral')); d.client_api.wan=[1,2,3].map(slot=>({slot,up:true,fresh:true})); assert.ok(routerObservation(d,{available:true,fresh:false},101000).channels.every(c=>c.indicator==='neutral')); d.modems[0].radio='NOSIM'; assert.equal(routerObservation(d,{available:true,fresh:true},101000).channels[2].indicator,'neutral'); });