Files
NODEDC_MISSION_CORE/apps/control-station/test/observationSync.test.mjs
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

35 lines
3.0 KiB
JavaScript

import assert from 'node:assert/strict';
import {before,after,test} from 'node:test';
import {createServer} from './support/vite-server.mjs';
let server,sync;
before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});sync=await server.ssrLoadModule('/src/core/fleet/observationSync.ts');});
after(async()=>{await server?.close();});
const receipt=(document,revision=2)=>({document,revision,writable:true});
test('disjoint camera and layout edits merge while conflicting leaves and arrays remain explicit',()=>{
const base={camera:{yaw:0,flip:false},layout:{ratio:50,hidden:[]}},local={...base,camera:{...base.camera,yaw:90}},remote={...base,layout:{...base.layout,ratio:60}};
assert.deepEqual(sync.mergeObservation(base,local,remote),{value:{camera:local.camera,layout:remote.layout},conflicts:[]});
assert.deepEqual(sync.mergeObservation(base,local,{...base,camera:{...base.camera,yaw:180}}).conflicts,['camera.yaw']);
assert.deepEqual(sync.mergeObservation({a:1,b:2},{b:2},{a:1,b:3}),{value:{b:3},conflicts:[]});
assert.deepEqual(sync.mergeObservation([],['a'],['b']).conflicts,['']);
});
test('lost ACK is settled by equality without replay',async()=>{
let writes=0;const doc={ratio:55};const saved=await sync.saveObservation(1,{ratio:50},doc,async()=>receipt(doc,2),async()=>{writes++;throw Error('timeout');},v=>v);
assert.equal(saved.receipt.revision,2);assert.equal(writes,1);
});
test('revision conflict retries once with a fresh revision and merged document',async()=>{
const calls=[];const saved=await sync.saveObservation(1,{a:1,b:1},{a:2,b:1},async()=>receipt({a:1,b:2},3),async(rev,doc)=>{calls.push([rev,doc]);if(rev===1)throw Error('conflict');return receipt(doc,4);},v=>v);
assert.deepEqual(calls,[[1,{a:2,b:1}],[3,{a:2,b:2}]]);assert.deepEqual(saved.document,{a:2,b:2});
});
test('competing edits never overwrite and repeated failures are bounded',async()=>{
let writes=0;
await assert.rejects(sync.saveObservation(1,{x:1},{x:2},async()=>receipt({x:3}),async()=>{writes++;throw Error('conflict');},v=>v),/другом окне/);assert.equal(writes,1);
writes=0;await assert.rejects(sync.saveObservation(1,{x:1},{x:2},async()=>receipt({x:1}),async()=>{writes++;throw Error('down');},v=>v),/down/);assert.equal(writes,2);
});
test('next mount reads after pending save, failed writes do not poison queue, vehicles are independent',async()=>{
let release;const gate=new Promise(r=>{release=r;}),events=[];
const a=sync.observationSerial('rover',async()=>{events.push('write');await gate;events.push('ack');});
const b=sync.observationSerial('rover',async()=>events.push('read'));
await sync.observationSerial('other',async()=>events.push('other'));assert.deepEqual(events,['write','other']);release();await Promise.all([a,b]);assert.deepEqual(events,['write','other','ack','read']);
await assert.rejects(sync.observationSerial('rover',async()=>{throw Error('fail');}));assert.equal(await sync.observationSerial('rover',async()=>42),42);
});