Files
NODEDC_MISSION_CORE/apps/control-station/test/roverLocalEvidence.test.mjs
T
DCCONSTRUCTIONS e06ff9a214 feat(fleet): add shared WireGuard profiles and bounded rover transport
Add portable shared WireGuard profiles while retaining Tailscale and Worker boundaries. Bound rover command/telemetry delivery, recovery and diagnostic capture across Core and Node.
2026-10-29 09:10:01 +03:00

55 lines
4.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {build} from 'esbuild';
const result=await build({stdin:{contents:"export * from './roverDevEvents'; export * from './roverTransport';",resolveDir:new URL('../src/core/fleet/',import.meta.url).pathname,loader:'ts'},bundle:true,write:false,platform:'node',format:'esm'});
const {BrowserEvidence,connectRoverEvidence,roverDevEvent,RoverTransport}=await import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].contents).toString('base64'));
test('local browser events use only the operator-host endpoint while remote DEV is disabled',async()=>{
const previous=globalThis.fetch,calls=[];globalThis.fetch=async(url,options)=>{calls.push({url,body:JSON.parse(options.body)});return {ok:true,json:async()=>({accepted:true})};};
const capture=connectRoverEvidence('/api/v1/fleet/fixture/rover');
try{capture.set(null,'local');roverDevEvent('/api/v1/fleet/fixture/rover','request',{request_id:8,sequence:12});
await new Promise(r=>setTimeout(r,300));assert.equal(calls.length,1);
assert.equal(calls[0].url,'/api/v1/fleet/fixture/rover/diagnostics/local/events');
assert.equal(calls[0].body.events[0].reason,'local_capture_attached');assert.equal(calls[0].body.id,'local');assert.equal(calls[0].body.events.find(e=>e.event==='request').request_id,8);
}finally{capture.close();globalThis.fetch=previous;}
});
test('failed local transfers are bounded and never count a retry as a lost event',async()=>{
const old=globalThis.fetch;globalThis.fetch=async()=>{throw Error('offline');};
try{const e=new BrowserEvidence('/local',true);e.set('a');for(let n=0;n<300;n++)e.record('request',{sequence:n});
assert.equal(e.rows.length,256);assert.equal(e.dropped,44);await e.flush();
assert.equal(e.rows.length,256);assert.equal(e.dropped,44);
e.set('b');assert.equal(e.rows.length,0);
}finally{globalThis.fetch=old;}
});
test('old in-flight reply cannot disable or prepend rows to a newer capture',async()=>{
const old=globalThis.fetch;let done;globalThis.fetch=()=>new Promise(r=>done=r);
try{const e=new BrowserEvidence('/local',true);e.set('a');e.record('request');const f=e.flush();
e.set('b');e.record('request',{sequence:2});done({ok:true,json:async()=>({accepted:false})});await f;
assert.equal(e.id,'b');assert.equal(e.rows[0].sequence,2);
}finally{globalThis.fetch=old;}
});
test('closed local capture stops ingestion and oversized events are not retained',async()=>{
const old=globalThis.fetch;globalThis.fetch=async()=>({ok:true,json:async()=>({accepted:false})});
try{const e=new BrowserEvidence('/local',true);e.set('a');e.record('request',{reason:'x'.repeat(3000)});
assert.equal(e.rows.length,0);assert.equal(e.dropped,1);e.record('request');await e.flush();assert.equal(e.id,null);
}finally{globalThis.fetch=old;}
});
test('send and ack correlate with Core socket identity without altering the command envelope',async()=>{
const oldFetch=globalThis.fetch,oldWindow=globalThis.window,calls=[];
globalThis.window={location:{href:'http://127.0.0.1:8000/'}};
globalThis.fetch=async(url,options)=>{calls.push(JSON.parse(options.body));return {ok:true,json:async()=>({accepted:true})};};
let socket;
const capture=connectRoverEvidence('/api/v1/fleet/fixture/rover');capture.set(null,'local');
const transport=new RoverTransport('/api/v1/fleet/fixture/rover',()=>socket={readyState:1,bufferedAmount:0,sent:[],send(raw){this.sent.push(JSON.parse(raw));},close(){}});
try{
socket.onmessage({data:JSON.stringify({type:'state',socket_id:'core-socket',clock_ms:1000,value:{fresh:true}})});
const p=transport.command('command',{session_id:'session',sequence:3,left:0,right:0});
assert.deepEqual(Object.keys(socket.sent[0]).sort(),['action','body','expires_ms','id']);
socket.onmessage({data:JSON.stringify({type:'reply',socket_id:'core-socket',id:socket.sent[0].id,value:{accepted_sequence:3}})});
assert.equal((await p).accepted_sequence,3);await new Promise(r=>setTimeout(r,300));
const rows=calls.flatMap(v=>v.events),sent=rows.find(r=>r.reason==='socket_send'),ack=rows.find(r=>r.reason==='reply_received');
assert.equal(sent.sequence,3);assert.equal(sent.session_id,'session');
assert.equal(sent.socket_id,ack.socket_id);assert.equal(sent.request_id,ack.request_id);
}finally{transport.close();capture.close();globalThis.fetch=oldFetch;globalThis.window=oldWindow;}
});