import test from 'node:test'; import assert from 'node:assert/strict'; import {build} from 'esbuild'; async function module(path){const result=await build({entryPoints:[new URL(path,import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm'});return import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].contents).toString('base64'));} const {PreviewMetrics}=await module('../../../packages/sensor-ui/src/previewMetrics.ts'); const {RoverTiming}=await module('../src/core/fleet/roverTiming.ts'); const stats=(extra={})=>new Map([['video',{id:'a',type:'inbound-rtp',kind:'video',timestamp:1000,bytesReceived:1e6,framesDecoded:100,packetsLost:2,framesDropped:1,jitterBufferDelay:50,jitterBufferEmittedCount:100,totalDecodeTime:2,...extra}],['pair',{type:'candidate-pair',state:'succeeded',nominated:true,currentRoundTripTime:.012}]]); test('preview reports interval latency, not the lifetime mean or capture-to-screen age',()=>{ const m=new PreviewMetrics();assert.equal(m.sample(stats()).jitter_buffer_ms,null); const v=m.sample(stats({timestamp:3000,bytesReceived:1.5e6,framesDecoded:130,packetsLost:3,framesDropped:3,jitterBufferDelay:53,jitterBufferEmittedCount:130,totalDecodeTime:2.06})); assert.equal(v.fps,15);assert.equal(v.mbps,2);assert.equal(v.jitter_buffer_ms,100);assert.equal(v.decode_ms,2);assert.equal(v.round_trip_ms,12);assert.equal(v.packets_lost,1);assert.equal(v.frames_dropped,2);assert.equal(v.scope,'receiver-interval');assert.equal(v.processing_ms,null); }); test('missing counters and replacement streams cannot manufacture zero latency',()=>{ const m=new PreviewMetrics();m.sample(stats()); assert.equal(m.sample(stats({id:'b',timestamp:3000,jitterBufferDelay:55,jitterBufferEmittedCount:130})).jitter_buffer_ms,null); assert.equal(m.sample(stats({id:'b',timestamp:3000})).fps,null); assert.equal(m.sample(new Map()),null); }); test('counter resets do not create negative rates or leaked endpoint data',()=>{ const m=new PreviewMetrics();m.sample(stats());const v=m.sample(stats({timestamp:3000,framesDecoded:1,bytesReceived:1,remoteAddress:'private',sdp:'secret'})); assert.equal(v.fps,null);assert.equal(v.mbps,null);assert.ok(!JSON.stringify(v).includes('private'));assert.ok(!JSON.stringify(v).includes('secret')); }); test('control timing separates scheduling gaps from HTTP time and remains bounded',()=>{ const oldDocument=globalThis.document,oldPerformance=globalThis.performance;let now=0; globalThis.document={hidden:false,hasFocus:()=>true,documentElement:{dataset:{}}};globalThis.performance={now:()=>now}; try{const m=new RoverTiming();m.tick(false);now=100;m.tick(false);assert.equal(JSON.parse(document.documentElement.dataset.roverTiming).events.length,0); now=500;document.hidden=true;m.tick(true);let v=JSON.parse(document.documentElement.dataset.roverTiming);assert.equal(v.events[0].kind,'heartbeat-gap');assert.equal(v.events[0].gap_ms,400);assert.equal(v.events[0].hidden,true); now=600;m.request('view',590,'transport');v=JSON.parse(document.documentElement.dataset.roverTiming);assert.equal(v.events.at(-1).duration_ms,10);assert.equal(v.events.at(-1).outcome,'transport'); for(let i=0;i<100;i++){now+=300;m.tick(false);}assert.equal(JSON.parse(document.documentElement.dataset.roverTiming).events.length,16); m.clear();assert.equal(document.documentElement.dataset.roverTiming,undefined); }finally{globalThis.document=oldDocument;globalThis.performance=oldPerformance;} });