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}); });