Files

106 lines
6.6 KiB
JavaScript

import assert from 'node:assert/strict';
import {before,after,test} from 'node:test';
import {createServer} from './support/vite-server.mjs';
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',cpu:{load_percent:0},memory:{used_percent:15}},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 работает с ограничениями');
});
test('a pipeline-only heartbeat does not claim complete host telemetry', async () => {
const {workerConnectionStatus}=await server.ssrLoadModule('/src/core/system/workerConnectionStatus.ts');
const incomplete={...worker,node:{node_id:'test-node',cpu:{},memory:{used_percent:null}}};
assert.equal(workerConnectionStatus(incomplete).state,'unknown');
assert.equal(workerConnectionStatus(incomplete).label,'Телеметрия неполная');
assert.equal(workerConnectionStatus(worker).state,'online');
});
test('new worker selection distinguishes discovered, enrolled and prepared devices', async () => {
const {workerCandidate}=await server.ssrLoadModule('/src/core/system/workerConnections.ts');
const catalog={peers:[{id:'p',profile_id:'net',name:'node'}],workers:[],prepared:[]};
assert.equal(workerCandidate(catalog,'net','p').binding,null);
const ready={...catalog,prepared:[{id:'binding',contour_id:'c',hostname:'NODE'}]};
assert.equal(workerCandidate(ready,'net','p').binding.id,'binding');
const enrolled={...ready,workers:[{contour_id:'c',connections:[{profile_id:'net',peer_id:'p'}]}]};
assert.equal(workerCandidate(enrolled,'net','p').enrolled.contour_id,'c');
assert.equal(workerCandidate(enrolled,'net','p').binding,null);
assert.equal(workerCandidate({...ready,prepared:[...ready.prepared,...ready.prepared]},'net','p').binding,null);
});
test('system contains worker and VPS catalogs plus operational worker detail pages', async () => {
const {workspacesForRoot,workspaceById}=await server.ssrLoadModule('/src/productModel.ts');
assert.deepEqual(workspacesForRoot('system').map(x=>x.id),['workers','vps','modules','network']);
assert.equal(workspaceById('integrations'),null);
assert.equal(workspaceById('audit'),null);
});