import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; let server,collectRadio,radioChanges,radioSegments,decodeChunk; before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});({collectRadio,radioChanges,radioSegments}=await server.ssrLoadModule('../../packages/router-ui/src/radioHistory.ts'));({decodeChunk}=await server.ssrLoadModule('../../packages/router-ui/src/evidence.ts'));}); after(async()=>{await server?.close();}); test('radio history expires, resets on boot, and does not relabel stale cache as new measurements',()=>{ const d={observed_at:100,client_api:{},modems:[{slot:2,radio_age_seconds:5,rsrp:-90,rsrq:-9,sinr:0,band:'B3',cell_id:'a'}]},empty={key:'',day:0,samples:[]}; const first=collectRadio(empty,'boot1',d,true,100);assert.equal(first.samples[0].at,95);assert.equal(first.samples[0].sinr,0); assert.equal(collectRadio(first,'boot1',{...d,observed_at:110,modems:[{...d.modems[0],radio_age_seconds:15}]},true,110).samples.length,1); assert.equal(collectRadio(first,'boot1',{...d,modems:[{...d.modems[0],radio_age_seconds:60}]},true,110).samples.length,1); assert.equal(collectRadio(first,'boot2',d,false,110).samples.length,0);assert.equal(collectRadio(first,'boot1',d,false,500).samples.length,0); assert.equal(collectRadio(first,'boot1',{...d,observed_at:86401},false,86401).samples.length,0); }); test('radio charts break across missing data, gaps and band/cell switches; changes never imply causality across a gap',()=>{ const p=(at,band='B3',cell='a',rsrp=-90)=>({at,slot:2,band,cell,rsrp,rsrq:null,sinr:0}); const points=[p(10),p(20),p(30,'B7'),p(40,'B7','b'),p(90,'B3','c'),p(100,'B3','c',null),p(110,'B3','c')]; assert.deepEqual(radioSegments(points,'rsrp',110).map(p=>p.length),[2,1,1,1,1]);assert.equal(radioChanges(points).length,2); assert.match(radioChanges(points)[0].text,/Сота/);assert.match(radioChanges(points)[1].text,/B3 → B7/); }); test('read chunks reject another router, reboot, changed document, wrong extent and partial chunks',()=>{ const q={resource:'events',id:'',offset:0,transfer:'',after:''};const c={operation_id:'op',device_id:'router',boot_id:'boot',resource:'events',id:'',offset:0,total:2,transfer:'a'.repeat(64),at:1,data:btoa('{}')}; assert.equal(new TextDecoder().decode(decodeChunk(c,q,'router','boot')),'{}'); for(const patch of [{device_id:'other'},{boot_id:'other'},{total:3},{offset:32768},{resource:'bundle'},{transfer:'bad'}])assert.throws(()=>decodeChunk({...c,...patch},q,'router','boot')); assert.throws(()=>decodeChunk(c,{...q,transfer:'b'.repeat(64)},'router','boot')); });