import test from 'node:test'; import assert from 'node:assert/strict'; import {build} from 'esbuild'; const result=await build({entryPoints:[new URL('../src/core/fleet/roverRecovery.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm'}); const {RoverRecovery}=await import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].contents).toString('base64')); const settings={currentA:30,speed:70,mode:'duty'}; const stopped={fresh:true,controlling:false,snapshot:{supported:true,control_modes:['rpm','duty'],instance:'boot-A',state:'stopped',session_id:'old-session',release_confirmed:true,profile:{layout:'1x1',bindings:{left:{uuid:'left'},right:{uuid:'right'}}},devices:['left','right'].map(uuid=>({uuid,session_id:'usb-'+uuid,age_ms:10,values:{erpm:0,fault_code:0}}))}}; function recovery(){const r=new RoverRecovery();r.remember(settings,stopped);r.interrupt(0,'old-session');return r;} const change=patch=>({...stopped,snapshot:{...stopped.snapshot,...patch}}); test('recovery requires consecutive fresh stopped evidence and preserves exact settings',()=>{ const r=recovery();assert.equal(r.next(stopped,0,true),null); assert.equal(r.next(stopped,499,true),null);assert.deepEqual(r.next(stopped,500,true),settings); assert.equal(r.next(stopped,600,true),null);assert.equal(r.next(stopped,1200,true),null); assert.deepEqual(r.next(stopped,1500,true),settings,'retry delay is bounded and increases'); }); test('no retry with stale telemetry, moving/stale/faulted motors, missing devices or hidden UI',()=>{ for(const bad of [{...stopped,fresh:false},change({state:'stopping'}),...['age_ms','erpm','fault_code'].map(k=>change({devices:stopped.snapshot.devices.map(d=>({...d,age_ms:k==='age_ms'?501:10,values:{...d.values,[k]:k==='erpm'?60:1}}))}))]){ const r=recovery();r.next(stopped,0,true);assert.equal(r.next(bad,600,true),null);assert.equal(r.next(stopped,700,true),null);assert.deepEqual(r.next(stopped,1200,true),settings); } const r=recovery();r.next(stopped,0,true);assert.equal(r.next(stopped,600,false),null);assert.equal(r.next(stopped,700,true),null); }); test('restart, motor reassignment, RC, hardware fault and another owner prohibit recovery',()=>{ for(const bad of [change({devices:[]}),change({instance:'boot-B'}),change({profile:{layout:'2x2'}}),change({state:'receiver'}),change({state:'fault'}),change({release_confirmed:false}),change({control_modes:['rpm']}),{...change({session_id:'other-session'}),controlling:true}]){ const r=recovery();assert.ok(r.blocker(bad));assert.equal(r.next(bad,9000,true),null); } }); test('newly acquired own session is not mistaken for another operator',()=>{ const r=recovery(),v={...change({session_id:'new-session',state:'preparing'}),controlling:true}; assert.equal(r.blocker(v,'new-session'),null);assert.ok(r.blocker(v));r.connected(1000);assert.equal(r.waiting,false);assert.equal(r.next(stopped,9000,true),null); }); test('explicit Stop and reload cannot restore an old request',()=>{ const r=recovery();r.clear();assert.equal(r.next(stopped,9000,true),null);assert.equal(r.interrupt(10000,'old-session'),false); assert.equal(new RoverRecovery().next(stopped,9000,true),null); }); test('unconfirmed stopping waits without cancelling request; settled stop starts a fresh quiet interval',()=>{ const r=recovery(),pending=change({state:'stopping',release_confirmed:false,message:'Release not yet observed',last_stop:{session_id:'old-session',state:'stopping',message:'Release not yet observed'}}); for(const at of [0,500,2000,60000]){ assert.equal(r.blocker(pending),null); assert.equal(r.next(pending,at,true),null,'pending release never grants authority'); assert.equal(r.waiting,true); } const settled=change({last_stop:{...pending.snapshot.last_stop,recovered_at:'fixture'}}); assert.equal(r.next(settled,60100,true),null); assert.equal(r.next(settled,60599,true),null); assert.deepEqual(r.next(settled,60600,true),settings); }); test('pending release never masks device faults, identity changes, receiver or explicit Stop',()=>{ const pending=change({state:'stopping',release_confirmed:false}); for(const bad of [change({state:'fault',release_confirmed:false}),change({state:'stopped',last_stop:{session_id:'old-session',state:'fault',message:'Hardware fault',recovered_at:'later'}}),change({state:'receiver'}),change({state:'stopping',instance:'other-boot'}),change({state:'stopping',devices:stopped.snapshot.devices.map(d=>({...d,session_id:'reattached'}))})]){ const r=recovery();assert.equal(r.blocker(pending),null);assert.ok(r.blocker(bad));assert.equal(r.next(bad,90000,true),null); } const r=recovery();r.next(pending,0,true);r.clear();assert.equal(r.next(stopped,90000,true),null); }); test('a recovered neutral snapshot retains terminal history and cannot auto-rearm its failed session',()=>{ const r=recovery(),settled=change({state:'stopped',message:null,last_stop:{session_id:'old-session',state:'fault',message:'USB lost',recovered_at:'fixture'}}); assert.equal(r.blocker(settled),'USB lost');assert.equal(r.next(settled,9000,true),null); r.remember(settings,settled);r.started('new-session'); assert.equal(r.blocker({...change({state:'preparing',session_id:'new-session',last_stop:settled.snapshot.last_stop}),controlling:true},'new-session'),null); }); test('explicit retry can await its own preparation after a previous terminal fault',()=>{ const failed=change({state:'fault',release_confirmed:false,message:'Old USB disconnect'}),r=new RoverRecovery(); r.remember(settings,failed);r.started('new-session'); assert.equal(r.blocker({...failed,controlling:true},'new-session'),null); assert.ok(r.blocker(failed,'new-session'),'without a live Core lease the old fault still blocks'); assert.ok(r.blocker({...failed,controlling:true,snapshot:{...failed.snapshot,instance:'boot-B'}},'new-session')); const preparing={...change({session_id:'new-session',state:'preparing',release_confirmed:null}),controlling:true}; assert.equal(r.blocker(preparing,'new-session'),null); assert.ok(r.blocker({...failed,controlling:true},'new-session'),'after acknowledgement old states cannot be ignored'); assert.ok(r.blocker({...preparing,snapshot:{...preparing.snapshot,state:'fault',message:'New failure'}},'new-session')); }); test('explicit retry never permits automatic recovery from unconfirmed release',()=>{ const failed=change({state:'fault',release_confirmed:false}),r=new RoverRecovery(); r.remember(settings,failed);r.started('new-session');r.interrupt(0,'new-session'); assert.ok(r.blocker({...failed,controlling:true},'new-session')); assert.equal(r.next(failed,9000,true),null); }); test('same UUID after USB reconnect requires explicit arm and never auto-recovers',()=>{ const r=recovery(),reconnected=change({devices:stopped.snapshot.devices.map(d=>({...d,session_id:d.session_id+'-new'}))}); assert.ok(r.blocker(reconnected));assert.equal(r.next(reconnected,9000,true),null); const missing=change({devices:stopped.snapshot.devices.slice(0,1)});assert.ok(recovery().blocker(missing)); assert.equal(r.next(stopped,15000,true),null); }); test('legacy snapshots without attachment sessions cannot opt into automatic recovery',()=>{ const r=new RoverRecovery();r.remember(settings,change({devices:stopped.snapshot.devices.map(({session_id,...d})=>d)})); assert.equal(r.interrupt(0,'old-session'),false); }); test('unknown release evidence never admits an automatic rearm',()=>{ for(const release_confirmed of [null,undefined]){ const r=recovery(),unknown=change({release_confirmed}); assert.equal(r.next(unknown,0,true),null); assert.equal(r.next(unknown,1000,true),null); assert.equal(r.next(stopped,1100,true),null,'confirmed evidence starts its own quiet interval'); assert.deepEqual(r.next(stopped,1600,true),settings); } }); test('brief ready states cannot reset the automatic recovery budget',()=>{ const r=recovery(); for(let attempt=0;attempt<3;attempt++){ const base=attempt*10000; assert.equal(r.next(stopped,base,true),null); assert.deepEqual(r.next(stopped,base+5000,true),settings); r.connected(base+5100);r.connected(base+6000); assert.equal(r.interrupt(base+7000,'session-'+attempt),true); } assert.equal(r.waiting,true,'backoff retains operator request'); assert.equal(r.next(stopped,1000000,true),null,'first stopped observation alone cannot grant another lease'); r.remember(settings,stopped); assert.equal(r.waiting,false); assert.equal(r.interrupt(1000100,'explicit-new'),true); }); test('only thirty seconds of continuously ready observation restores retry budget',()=>{ const r=recovery();r.next(stopped,0,true);assert.deepEqual(r.next(stopped,500,true),settings); for(let at=600;at<=30600;at+=200)r.connected(at); assert.equal(r.interrupt(30700,'stable-session'),true); for(let attempt=0;attempt<3;attempt++){ const base=40000+attempt*10000; assert.equal(r.next(stopped,base,true),null); assert.deepEqual(r.next(stopped,base+5000,true),settings); r.connected(base+5100); assert.equal(r.interrupt(base+7000,'short-'+attempt),true); } }); test('separate short connections cannot add up to a stable connection',()=>{ const r=recovery(); for(let attempt=0;attempt<3;attempt++){ const base=attempt*50000; assert.equal(r.next(stopped,base,true),null); assert.deepEqual(r.next(stopped,base+5000,true),settings); for(let at=base+5100;at<=base+34100;at+=200)r.connected(at); assert.equal(r.interrupt(base+35000,'short-'+attempt),true); } assert.equal(r.waiting,true); }); test('a long unobserved gap cannot count as thirty seconds of stable connection',()=>{ const r=recovery(); for(let attempt=0;attempt<3;attempt++){ const base=attempt*60000; assert.equal(r.next(stopped,base,true),null); assert.deepEqual(r.next(stopped,base+5000,true),settings); r.connected(base+5100);r.connected(base+36000); assert.equal(r.interrupt(base+37000,'gap-'+attempt),true); } assert.equal(r.waiting,true); }); test('retry admission without a start result remains delayed and never ends request',()=>{ const r=recovery(); for(let attempt=0;attempt<3;attempt++){ const base=attempt*10000; assert.equal(r.next(stopped,base,true),null); assert.deepEqual(r.next(stopped,base+5000,true),settings); } assert.equal(r.next(stopped,30000,true),null); assert.equal(r.waiting,true);assert.deepEqual(r.next(stopped,38000,true),settings,'capped retry preserves request'); });