Files
NODEDC_MISSION_CORE/apps/control-station/test/contourHealth.test.mjs
T

78 lines
4.8 KiB
JavaScript

import assert from 'node:assert/strict';
import {before,after,test} from 'node:test';
import {createServer} from 'vite';
let server,readContourSnapshot,contourSummary,workerAvailable;
before(async()=>{
server=await createServer({server:{middlewareMode:true,hmr:false},optimizeDeps:{noDiscovery:true,include:[]},appType:'custom'});
({readContourSnapshot,contourSummary,workerAvailable}=await server.ssrLoadModule('/src/core/system/contourHealth.ts'));
});
after(async()=>{await server?.close();});
const core={ok:true,service:'mission-core-control-plane',version:'test',plugin_runtimes:{ready:1,total:1}};
const contour={contour_id:'test-compute',display_name:'Test compute',expected_node_id:'test-node'};
const worker={schema_version:'missioncore.worker-telemetry/v1',profile:{},connection:{reachable:true,identity_matches:true},node:{node_id:'test-node'},runtimes:[],pipeline:{},network:{},history:[]};
const vehicle={id:'test-vehicle',name:'Test rover',enrollment:'paired',connectivity:'offline'};
function installFetch(t,{failed=[],data={}}={}){
const calls=[];
t.mock.method(globalThis,'fetch',async url=>{
calls.push(url);
if(failed.includes(url))throw new Error('unavailable');
const defaults={
'/api/health':core,
'/api/v1/system/contours':{schema_version:'missioncore.compute-contour-catalog/v1',contours:[contour]},
'/api/v1/system/contours/test-compute/telemetry?history=90':worker,
'/api/v1/fleet':{items:[vehicle]},
};
assert.ok(url in defaults,`Unexpected probe: ${url}`);
return new Response(JSON.stringify(url in data?data[url]:defaults[url]));
});
return calls;
}
test('actual registries determine node count and offline boards prevent all-online status',async t=>{
const calls=installFetch(t);
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(snapshot.workers[0].contour.display_name,'Test compute');
assert.deepEqual(contourSummary(snapshot),{tone:'warning',label:'Часть узлов не в сети',online:2,total:3});
assert.equal(calls.length,4);
});
test('failed checks discard previous online data and distinguish unknown from empty',async t=>{
installFetch(t,{failed:['/api/v1/fleet','/api/v1/system/contours/test-compute/telemetry?history=90']});
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(snapshot.vehicles,null);
assert.equal(snapshot.workers[0].telemetry,null);
assert.equal(snapshot.errors.length,2);
assert.deepEqual(contourSummary(snapshot),{tone:'warning',label:'Проверка неполная',online:1,total:null});
});
test('invalid health never becomes reachable; identity mismatch and stale worker are not online',async t=>{
installFetch(t,{data:{'/api/health':{ok:true}}});
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(snapshot.core,null);
assert.equal(workerAvailable({...worker,connection:{reachable:true,identity_matches:false}}),false);
assert.equal(workerAvailable({...worker,connection:{reachable:false,identity_matches:true}}),false);
assert.equal(workerAvailable({...worker,node:null}),false);
});
test('an empty configured contour contains only the operator, with no fabricated worker',async t=>{
installFetch(t,{data:{'/api/v1/system/contours':{schema_version:'missioncore.compute-contour-catalog/v1',contours:[]},'/api/v1/fleet':{items:[]}}});
assert.deepEqual(contourSummary(await readContourSnapshot(new AbortController().signal)),{tone:'success',label:'Все узлы в сети',online:1,total:1});
});
test('receiver failure leaves worker unknown instead of declaring it offline',async t=>{
installFetch(t,{data:{'/api/v1/system/contours/test-compute/telemetry?history=90':{...worker,node:null,connection:{reachable:false,identity_matches:false,error_code:'telemetry-receiver-unavailable'}}}});
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(contourSummary(snapshot).label,'Состояние части узлов не подтверждено');
const {workerConnectionStatus}=await server.ssrLoadModule('/src/core/system/workerConnectionStatus.ts');
const status=workerConnectionStatus(snapshot.workers[0].telemetry);
assert.equal(status.state,'unknown');
assert.equal(status.label,'Не в сети');
assert.equal(workerConnectionStatus({...worker,node:null,connection:{reachable:false,error_code:'telemetry-agent-stale'}}).label,'Не в сети');
assert.equal(workerConnectionStatus(worker).state,'online');
});
test('a responding Core with recording capacity pressure remains reachable',async t=>{
installFetch(t,{data:{'/api/health':{...core,ok:false,components:{recording_cache:{status:'capacity-pressure'}}}}});
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(contourSummary(snapshot).online,2);
assert.equal(contourSummary(snapshot).label,'Mission Core работает с ограничениями');
});