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}}); // Owner amendment 2026-10-27: retain operator intent through transport faults. // These are additional cases; pre-existing safety regressions stay in place. test('repeated transport faults retain request with bounded retry frequency',()=>{ const r=recovery(),admissions=[]; for(let at=0;at<=60000;at+=100){ if(r.next(stopped,at,true)){ admissions.push(at);r.connected(at+1);assert.equal(r.interrupt(at+2,'session-'+at),true); } } assert.ok(admissions.length>=5&&admissions.length<=11); for(let i=1;i=Math.min(8000,1000*2**Math.min(i-1,3))); assert.equal(r.waiting,true,'network retries cannot erase the operator request'); }); test('thirty seconds of observed ready resets backoff; short connections do not',()=>{ const r=recovery();r.next(stopped,0,true);assert.deepEqual(r.next(stopped,500,true),settings); r.connected(501);r.interrupt(502,'short');r.next(stopped,503,true); assert.equal(r.next(stopped,1003,true),null,'brief readiness must not bypass backoff'); assert.deepEqual(r.next(stopped,1500,true),settings); for(let at=1600;at<=31600;at+=200)r.connected(at); r.interrupt(31700,'stable');r.next(stopped,31700,true); assert.deepEqual(r.next(stopped,32200,true),settings); }); test('long total outage creates no arm attempts and preserves original settings',()=>{ const r=recovery(); for(let at=0;at<600000;at+=200)assert.equal(r.next({fresh:false,controlling:false,snapshot:{}},at,true),null); assert.equal(r.waiting,true); assert.equal(r.next(stopped,600000,true),null);assert.deepEqual(r.next(stopped,600500,true),settings); r.clear();assert.equal(r.next(stopped,900000,true),null,'explicit stop always cancels recovery'); }); test('failed admission stays rate bounded without ever getting a start result',()=>{ const r=recovery(),admissions=[]; for(let at=0;at<=60000;at+=100)if(r.next(stopped,at,true))admissions.push(at); assert.equal(admissions.length,10);assert.equal(r.waiting,true); assert.equal(r.next({...stopped,fresh:false},100000,true),null); }); test('explicit new operator request does not inherit the old retry delay',()=>{ const r=recovery();for(let at=0;at<=8000;at+=100)r.next(stopped,at,true); r.clear();r.remember(settings,stopped);r.interrupt(8100,'explicit-new'); assert.equal(r.next(stopped,8100,true),null);assert.deepEqual(r.next(stopped,8600,true),settings); });