feat(routers): add observed router inventory and scoped controls

Separate router discovery, evidence and authorized control from device runtime. Add Node/Core contracts and reusable operator surfaces.
This commit is contained in:
DCCONSTRUCTIONS
2026-10-29 09:09:59 +03:00
parent 8b210258ca
commit 5d56237124
30 changed files with 3089 additions and 0 deletions
@@ -0,0 +1,75 @@
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(`<h2>SIM M${slot}</h2>`));
assert.match(html,/Интернет не подтверждён/);
assert.match(html,/SIM не установлена/);
assert.match(html,/Нет сведений/);
assert.match(html,/Ожидание следующего измерения для графика/);
assert.doesNotMatch(html,/0 кбит\/с/);
});
test('router controls require fresh capable Node and block concurrent operations',async()=>{
const {controlEnabled,availableSlots,controlMessage}=await server.ssrLoadModule('../../packages/router-ui/src/control.ts');
const d={id:'router-test',state:'ready',modems:[{slot:1,radio:'OK',ip:'10.0.0.1'},{slot:2,radio:'NOSIM',ip:''}]};
const s={control_version:1,runtime_id:'runtime',results:[]};
assert.equal(controlEnabled(s,d,true),true);
assert.equal(controlEnabled({...s,control_version:0},d,true),false);
assert.equal(controlEnabled(s,d,false),false);
assert.equal(controlEnabled({...s,results:[{state:'applying'}]},d,true),false);
assert.deepEqual(availableSlots(d),[1]);
assert.match(controlMessage({state:'unknown'}),/не подтверждён/);
assert.match(controlMessage({state:'complete',action:'reboot'}),/Перезагрузка подтверждена/);
});
test('control surface uses shared controls and does not claim support on old Node',async()=>{
const {RouterControls}=await server.ssrLoadModule('../../packages/router-ui/src/RouterControls.tsx');
const html=renderToStaticMarkup(createElement(RouterControls,{device:{id:'router-test',state:'ready',modems:[],active_mode:'direct'},value:{results:[]},transport:{},ready:true}));
assert.match(html,/требуется обновление Mission Core/);
assert.match(html,/Перезагрузить роутер/);
assert.match(html,/disabled/);
});