Files
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

48 lines
3.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {createHash,webcrypto} from 'node:crypto';
import {build} from 'esbuild';
const result=await build({stdin:{contents:"export * from './src/core/fleet/roverLogSave';export * from './src/core/fleet/roverLogTransfer';",resolveDir:process.cwd()},bundle:true,write:false,platform:'node',format:'esm'});
const {saveRoverLog,readLogBlocks,transferRoverLog}=await import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].contents).toString('base64'));
if(!globalThis.crypto)globalThis.crypto=webcrypto;
const frame=(raw,offset=0,total=raw.length,extra={})=>JSON.stringify({capture_id:'a'.repeat(32),snapshot:'b'.repeat(64),offset,next:offset+raw.length,total_bytes:total,data:Buffer.from(raw).toString('base64'),sha256:createHash('sha256').update(raw).digest('hex'),...extra})+'\n';
test('save opens native chooser before network; closes only a verified complete file',async()=>{
const oldWindow=globalThis.window,oldFetch=globalThis.fetch;const actions=[],chunks=[];
globalThis.window={showSaveFilePicker:async(options)=>{actions.push(['picker',options.suggestedName]);return {createWritable:async()=>({write:async c=>chunks.push(Buffer.from(c)),close:async()=>actions.push(['closed']),abort:async()=>actions.push(['aborted'])})};}};
globalThis.fetch=async url=>{actions.push(['fetch',url]);return new Response(frame('manifest\nrow1\nrow2\n'));};
try{await saveRoverLog('/rover','session.jsonl');assert.deepEqual(actions,[['picker','session.jsonl'],['fetch','/rover/diagnostics/export?format=chunks'],['closed']]);assert.equal(Buffer.concat(chunks).toString(),'manifest\nrow1\nrow2\n');}
finally{globalThis.window=oldWindow;globalThis.fetch=oldFetch;}
});
test('cancelled save dialog makes no export request',async()=>{
const oldWindow=globalThis.window,oldFetch=globalThis.fetch;
globalThis.window={showSaveFilePicker:async()=>{throw new DOMException('cancel','AbortError');}};
globalThis.fetch=()=>{throw Error('must not fetch');};
try{await assert.rejects(saveRoverLog('/rover','session.jsonl'),{name:'AbortError'});}
finally{globalThis.window=oldWindow;globalThis.fetch=oldFetch;}
});
test('interrupted response resumes exact snapshot after last verified written block',async()=>{
const oldFetch=globalThis.fetch,urls=[],bytes=[];
globalThis.fetch=async url=>{urls.push(url);return new Response(urls.length===1?frame('abc',0,6)+'{"partial":':frame('def',3,6));};
try{
await transferRoverLog('/rover',async data=>bytes.push(Buffer.from(data)));
assert.equal(Buffer.concat(bytes).toString(),'abcdef');assert.equal(urls.length,2);
const resumed=new URL(urls[1],'http://test').searchParams;
assert.equal(resumed.get('offset'),'3');assert.equal(resumed.get('total_bytes'),'6');
assert.equal(resumed.get('snapshot'),'b'.repeat(64));assert.equal(resumed.get('capture_id'),'a'.repeat(32));
}finally{globalThis.fetch=oldFetch;}
});
test('corruption, identity drift, duplicates, oversize and early EOF never confirm bad bytes',async()=>{
const cursor={capture_id:'a'.repeat(32),snapshot:'b'.repeat(64),total_bytes:6,offset:3};
for(const text of [frame('def',3,6,{sha256:'0'.repeat(64)}),frame('def',3,6,{snapshot:'c'.repeat(64)}),frame('abc',0,6),frame('def',3,7),frame('x'.repeat(65537),3,65540),'x'.repeat(110000)]){
let written=0;await assert.rejects(readLogBlocks(new Response(text),cursor,async()=>written++,()=>{}));assert.equal(written,0);
}
await assert.rejects(readLogBlocks(new Response(frame('abc',0,6)),undefined,async()=>{},()=>{}));
});
test('HTTP segmentation, including split JSON lines, does not change file bytes',async()=>{
const encoded=new TextEncoder().encode(frame('abc',0,6)+frame('def',3,6)),written=[];
const response=new Response(new ReadableStream({start(c){for(let i=0;i<encoded.length;i+=17)c.enqueue(encoded.slice(i,i+17));c.close();}}));
await readLogBlocks(response,undefined,async b=>written.push(Buffer.from(b)),()=>{});
assert.equal(Buffer.concat(written).toString(),'abcdef');
});