feat(observatory): show present equipment and compact router and telemetry panes

This commit is contained in:
DCCONSTRUCTIONS
2026-11-21 20:58:25 +03:00
parent 3ff43dd2af
commit a50d638ced
22 changed files with 369 additions and 35 deletions
@@ -0,0 +1,65 @@
import assert from 'node:assert/strict';
import {before,after,test} from 'node:test';
import {readFile} from 'node:fs/promises';
import {createServer} from './support/vite-server.mjs';
let server,visibleSensors,visibleRouters,visibleIndependentDevices,hostOnline;
before(async()=>{
server=await createServer({appType:'custom',logLevel:'silent'});
({visibleSensors}=await server.ssrLoadModule('../../packages/sensor-ui/src/presence.ts'));
({visibleRouters}=await server.ssrLoadModule('../../packages/router-ui/src/contracts.ts'));
({visibleIndependentDevices}=await server.ssrLoadModule('./src/core/fleet/equipmentPresence.ts'));
({hostOnline}=await server.ssrLoadModule('./src/core/fleet/vps.ts'));
});
after(async()=>{await server?.close();});
test('ten identical cameras retain independent presence and return with the same identity/settings',()=>{
const items=Array.from({length:10},(_,i)=>({id:`camera-${i}`,model:'RealSense D455',name:`Camera ${i}`,online:true}));
const inventory={fresh:true,items};
assert.deepEqual(visibleSensors(inventory,true),items);
const missing={...inventory,items:items.map((v,i)=>i===3?{...v,online:false}:v)};
assert.equal(visibleSensors(missing,true).length,9);
assert.ok(!visibleSensors(missing,true).some(v=>v.id==='camera-3'));
assert.deepEqual(visibleSensors(inventory,true),items);
assert.equal(inventory.items.length,10);
assert.deepEqual(visibleSensors(inventory,false),[]);
assert.deepEqual(visibleSensors({...inventory,fresh:false},true),[]);
});
test('two routers are independent; stale, unreachable and disconnected observations never become rows',()=>{
const a={id:'router-a',attached:true,state:'ready',observed_at:100,internet:'unknown'};
const b={...a,id:'router-b'};
const snapshot={available:true,fresh:true,items:[a,b]};
assert.deepEqual(visibleRouters(snapshot,110000),[a,b]);
for(const change of [{attached:false},{state:'unreachable'},{observed_at:20}]){
assert.deepEqual(visibleRouters({...snapshot,items:[{...a,...change},b]},110000),[b]);
}
assert.deepEqual(visibleRouters({...snapshot,fresh:false},110000),[]);
assert.deepEqual(visibleRouters({...snapshot,available:false},110000),[]);
assert.deepEqual(visibleRouters(snapshot,140000),[]);
// A physically reachable device that needs credentials must stay discoverable.
for(const state of ['access_required','access_rejected','incompatible']){
assert.equal(visibleRouters({...snapshot,items:[{...a,state}]},110000).length,1);
}
assert.deepEqual(visibleRouters(snapshot,110000),[a,b]);
});
test('saved trackers require live instance evidence; a stalled request expires the last positive result',()=>{
const devices=[{id:'one'},{id:'two'}];
const telemetry={receivedAt:100000,unavailable:false,items:[{instance_id:'one',online:false},{instance_id:'two',online:true}]};
assert.deepEqual(visibleIndependentDevices(devices,telemetry,105000),[devices[1]]);
assert.deepEqual(visibleIndependentDevices(devices,{...telemetry,unavailable:true},105000),[]);
assert.deepEqual(visibleIndependentDevices(devices,telemetry,120000),[]);
assert.deepEqual(visibleIndependentDevices(devices,{...telemetry,items:telemetry.items.map(v=>({...v,online:true}))},105000),devices);
assert.equal(devices.length,2);
});
test('only paired VPS with current contact are present',()=>{
const host={id:'vps',state:'paired',last_sync_at:100};
assert.equal(hostOnline(host,110000),true);
assert.equal(hostOnline(host,130000),false);
assert.equal(hostOnline({...host,state:'pending'},110000),false);
assert.equal(hostOnline({...host,last_sync_at:null},110000),false);
});
test('Node mounts the shared router inventory inside the device section, not outside the board tree',async()=>{
const source=await readFile(new URL('../../node-agent/ui/src/NodeSensors.tsx',import.meta.url),'utf8');
assert.match(source,/devices=\{<>\{content\.devices\}<NodeRouters\/>/);
assert.doesNotMatch(source,/<NodeRouters\/><SensorWorkspace/);
});
@@ -0,0 +1,90 @@
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');
});