import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; import {createElement} from 'react'; import {renderToStaticMarkup} from 'react-dom/server'; let server,routerFresh,routerLabel,metric,collectTraffic,trafficPath,trafficRate,signalFraction,modemLabel; before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});({routerFresh,routerLabel,metric}=await server.ssrLoadModule('../../packages/router-ui/src/contracts.ts'));({collectTraffic,trafficPath,trafficRate,signalFraction,modemLabel}=await server.ssrLoadModule('../../packages/router-ui/src/overview.ts'));}); after(async()=>{await server?.close();}); test('fresh board heartbeat cannot revive stale radio data',()=>{ const state={available:true,fresh:true},device={state:'ready',observed_at:100}; assert.equal(routerFresh(state,device,110000),true); assert.equal(routerFresh(state,device,141000),false); assert.equal(routerLabel(state,device,141000),'Нет свежих данных'); assert.equal(routerFresh({...state,fresh:false},device,110000),false); }); test('missing radio sample is not displayed as zero or antenna health',()=>{ assert.equal(metric(null,'dB'),'Нет сведений'); assert.equal(metric(0,'dB'),'0 dB'); assert.equal(metric(-113,'dBm'),'-113 dBm'); }); test('overview distinguishes absent SIM, unavailable observations and missing measurements',()=>{ assert.equal(modemLabel({radio:'NOSIM'},true),'SIM не установлена'); assert.equal(modemLabel({radio:'NOSIM'},false),'Нет свежих данных'); assert.equal(modemLabel(undefined,true),'Нет сведений'); assert.equal(signalFraction(null),null); assert.equal(trafficRate(null),'—'); assert.equal(trafficRate(0),'0 кбит/с'); assert.equal(trafficRate(1500),'1,5 Мбит/с'); }); test('live graph deduplicates repeated snapshots and never invents zero traffic on loss',()=>{ const device={observed_at:100,down_kbps:20,up_kbps:null}; const first=collectTraffic({key:'',day:0,points:[]},'a',device,true,100); assert.deepEqual(first.points,[{at:100,down:20,up:null}]); assert.equal(collectTraffic(first,'a',device,true,103).points.length,1); assert.equal(collectTraffic(first,'a',{...device,observed_at:110},false,110).points.length,1); assert.deepEqual(collectTraffic(first,'a',device,false,401).points,[]); assert.deepEqual(collectTraffic(first,'new-runtime',device,false,110).points,[]); assert.deepEqual(collectTraffic(first,'a',device,false,86401).points,[]); }); test('traffic lines retain gaps and keep missing upload separate from recorded download',()=>{ const p=[{at:100,down:20,up:2},{at:110,down:21,up:null},{at:120,down:22,up:3},{at:200,down:23,up:4}]; assert.equal((trafficPath(p,'down',0,200,30).match(/M/g)||[]).length,2); assert.equal((trafficPath(p,'up',0,200,30).match(/M/g)||[]).length,3); assert.equal(trafficPath([{at:100,down:null,up:null}],'down',0,200,30),''); }); test('overview renders three slots without implying internet or traffic from unknown samples',async()=>{ const {RouterOverview}=await server.ssrLoadModule('../../packages/router-ui/src/RouterOverview.tsx'); const device={active_mode:'direct',active_wan:'wan1',internet:'unknown',address:'192.168.100.1',modems:[{slot:1,operator:'Test carrier',radio:'OK',connectivity:'online',rsrp:-96,technology:'lte',band:'B40',down_kbps:null,up_kbps:null},{slot:2,radio:'NOSIM'}],down_kbps:null,up_kbps:null}; const html=renderToStaticMarkup(createElement(RouterOverview,{device,ready:true,points:[{at:100,down:null,up:null},{at:110,down:null,up:null}]})); for(const slot of [1,2,3])assert.match(html,new RegExp(`