import test from 'node:test'; import assert from 'node:assert/strict'; import {build} from 'esbuild'; const built=await build({entryPoints:[new URL('../src/core/fleet/roverTransport.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm'}); const {RoverTransport}=await import('data:text/javascript;base64,'+Buffer.from(built.outputFiles[0].contents).toString('base64')); // Deterministic browser handshake/scheduler, no network or motor commands. async function fixture(run){ const keys=['window','performance','setTimeout','clearTimeout'],previous=Object.fromEntries(keys.map(k=>[k,globalThis[k]])); let now=0,next=0;const timers=new Map(),sockets=[]; globalThis.window={location:{href:'http://127.0.0.1:8000/'}};globalThis.performance={now:()=>now}; globalThis.setTimeout=(fn,ms)=>{const id=++next;timers.set(id,{fn,at:now+ms});return id;};globalThis.clearTimeout=id=>timers.delete(id); const advance=ms=>{const end=now+ms;while(true){const item=[...timers].filter(([,t])=>t.at<=end).sort((a,b)=>a[1].at-b[1].at)[0];if(!item)break;now=item[1].at;timers.delete(item[0]);item[1].fn();}now=end;}; const transport=new RoverTransport('/api/v1/fleet/fixture/rover',()=>{ const socket={readyState:0,bufferedAmount:0,sent:[],send(raw){this.sent.push(raw);},close(){this.readyState=3;},open(){if(this.readyState!==0)return;this.readyState=1;this.onopen?.();},state(){this.onmessage?.({data:JSON.stringify({type:'state',clock_ms:10000+now,value:{fresh:true,controlling:false,snapshot:{state:'observing'}}})});}}; sockets.push(socket);return socket; }); try{await run({transport,sockets,advance,timers});}finally{transport.close();for(const k of keys)globalThis[k]=previous[k];} } test('a two-second initial handshake can finish without being killed by the live-state watchdog',async()=>fixture(async f=>{ const first=f.sockets[0];f.advance(2000); assert.equal(first.readyState,0,'handshake was cancelled before any live state existed'); await assert.rejects(f.transport.command('arm',{}));assert.equal(first.sent.length,0); first.open();first.state();assert.equal((await f.transport.view()).fresh,true); assert.equal(f.sockets.length,1); })); test('an opened socket without state still has the original 1.5-second watchdog',async()=>fixture(async f=>{ f.sockets[0].open();f.advance(1500);assert.equal(f.sockets[0].readyState,3); await assert.rejects(f.transport.command('arm',{})); f.advance(250);assert.equal(f.sockets.length,2);assert.equal(f.sockets[1].sent.length,0); })); test('a never-opening handshake has a bounded retry and close removes every timer',async()=>fixture(async f=>{ f.advance(10250);assert.equal(f.sockets.length,2);assert.equal(f.sockets[0].readyState,3); f.transport.close();assert.equal(f.timers.size,0);f.advance(30000);assert.equal(f.sockets.length,2); })); for(let run=1;run<=10;run++)test(`startup/recovery cycle ${run}: delayed handshake, stale input refusal, new connection`,async()=>fixture(async f=>{ const first=f.sockets[0];f.advance(1500+run*400);first.open();first.state(); assert.equal((await f.transport.view()).fresh,true);assert.equal(f.sockets.length,1); f.advance(601);await assert.rejects(f.transport.command('arm',{})); f.advance(1149);assert.equal(f.sockets.length,2);assert.equal(first.readyState,3); const second=f.sockets[1];f.advance(2000);second.open();second.state(); assert.equal((await f.transport.view()).fresh,true); assert.ok(f.sockets.every(s=>s.sent.length===0),'observation never sends a motor command'); f.transport.close();assert.equal(f.timers.size,0); }));