Add portable shared WireGuard profiles while retaining Tailscale and Worker boundaries. Bound rover command/telemetry delivery, recovery and diagnostic capture across Core and Node.
73 lines
5.2 KiB
JavaScript
73 lines
5.2 KiB
JavaScript
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/roverTransport.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm'});
|
|
const {RoverTransport}=await import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].contents).toString('base64'));
|
|
async function fixture(run){
|
|
const previous=globalThis.window;globalThis.window={location:{href:'http://127.0.0.1:8000/'}};const sockets=[];
|
|
const transport=new RoverTransport('/api/v1/fleet/fixture/rover',url=>{const s={url,readyState:1,bufferedAmount:0,sent:[],send(raw){this.sent.push(JSON.parse(raw));},close(){this.readyState=3;this.onclose?.();},message(value){this.onmessage?.({data:JSON.stringify(value)});}};sockets.push(s);return s;});
|
|
const s=sockets[0],state={fresh:true,controlling:false,snapshot:{state:'observing'}};
|
|
try{await run({transport,s,sockets,state,ready:()=>s.message({type:'state',clock_ms:10000,value:state})});}finally{transport.close();globalThis.window=previous;}
|
|
}
|
|
test('independent socket supplies state and acknowledgements without an HTTP fetch',async()=>fixture(async({transport,s,state,ready})=>{
|
|
assert.equal(s.url,'ws://127.0.0.1:8000/api/v1/fleet/fixture/rover-channel');
|
|
const pending=transport.view();ready();assert.deepEqual(await pending,state);
|
|
const result=transport.command('command',{sequence:1});assert.equal(s.sent.length,1);assert.ok(s.sent[0].expires_ms>=10400&&s.sent[0].expires_ms<10420);
|
|
s.message({type:'reply',id:s.sent[0].id,value:{accepted_sequence:1}});assert.deepEqual(await result,{accepted_sequence:1});
|
|
}));
|
|
test('unconfirmed connection and backpressure never queue a command',async()=>fixture(async({transport,s,ready})=>{
|
|
await assert.rejects(transport.command('arm',{}));assert.equal(s.sent.length,0);ready();s.bufferedAmount=4097;
|
|
await assert.rejects(transport.command('command',{}));assert.equal(s.sent.length,0);
|
|
}));
|
|
test('disconnect rejects in-flight work and reconnect never replays it',async()=>fixture(async({transport,s,sockets,ready})=>{
|
|
ready();const p=transport.command('command',{sequence:1});s.close();await assert.rejects(p);
|
|
await new Promise(resolve=>setTimeout(resolve,280));assert.equal(sockets.length,2);assert.equal(sockets[1].sent.length,0);
|
|
await assert.rejects(transport.command('command',{sequence:2}));assert.equal(sockets[1].sent.length,0);
|
|
}));
|
|
test('late replies cannot acknowledge a timed-out command',async()=>fixture(async({transport,s,ready})=>{
|
|
ready();const p=transport.command('command',{sequence:1});await assert.rejects(p,/не подтверждена/);
|
|
s.message({type:'reply',id:s.sent[0].id,value:{accepted_sequence:1}});assert.equal(s.sent.length,1);
|
|
}));
|
|
test('arm acknowledgement replaces the cached pre-arm view before caller can poll',async()=>fixture(async({transport,s,state,ready})=>{
|
|
ready();assert.equal((await transport.view()).controlling,false);
|
|
const p=transport.command('arm',{}),postArm={...state,controlling:true};
|
|
s.message({type:'reply',id:s.sent[0].id,value:{session_id:'new'},state:postArm,clock_ms:10002});
|
|
assert.equal((await p).session_id,'new');assert.equal((await transport.view()).controlling,true);
|
|
}));
|
|
|
|
test('neutral handoff is an explicit acknowledged owner action with a deadline',async()=>fixture(async({transport,s,ready})=>{
|
|
ready();const p=transport.command('hold',{session_id:'owner',sequence:5});
|
|
assert.equal(s.sent[0].action,'hold');assert.deepEqual(s.sent[0].body,{session_id:'owner',sequence:5});
|
|
assert.ok(s.sent[0].expires_ms>=10400&&s.sent[0].expires_ms<10420);
|
|
s.message({type:'reply',id:s.sent[0].id,value:{accepted_sequence:5}});
|
|
assert.equal((await p).accepted_sequence,5);
|
|
}));
|
|
|
|
|
|
test('silent OPEN channel is retired and reconnect cannot replay commands',async()=>fixture(async({transport,s,sockets,ready})=>{
|
|
ready();s.close=()=>{s.readyState=2;}; // Browser supplies no close callback.
|
|
await new Promise(resolve=>setTimeout(resolve,1790));
|
|
assert.equal(sockets.length,2);assert.equal(s.readyState,2);
|
|
s.message({type:'state',clock_ms:1,value:{fresh:true}});
|
|
await assert.rejects(transport.command('arm',{}));
|
|
assert.equal(sockets[1].sent.length,0);
|
|
}));
|
|
|
|
|
|
|
|
test('each fresh sample keeps its own 400ms expiry with bounded in-flight work',async()=>fixture(async({transport,s,ready})=>{
|
|
const original=globalThis.performance;let now=0;globalThis.performance={now:()=>now};
|
|
const pending=[];
|
|
try{
|
|
ready();
|
|
for(let sequence=1;sequence<=4;sequence++){now=(sequence-1)*50;pending.push(transport.command('command',{sequence,left:0,right:0}));}
|
|
assert.deepEqual(s.sent.map(x=>x.expires_ms),[10400,10450,10500,10550]);
|
|
now=200;await assert.rejects(transport.command('command',{sequence:5}));assert.equal(s.sent.length,4);
|
|
s.message({type:'reply',id:s.sent[3].id,value:{accepted_sequence:4}});await pending[3];
|
|
pending.push(transport.command('command',{sequence:5,left:0,right:0}));
|
|
assert.equal(s.sent[4].expires_ms,10600);assert.equal(s.sent[0].expires_ms,10400,'new sample must not extend old frame');
|
|
for(const frame of s.sent)s.message({type:'reply',id:frame.id,value:{accepted_sequence:frame.body.sequence}});
|
|
await Promise.all(pending);
|
|
}finally{globalThis.performance=original;}
|
|
}));
|