import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; let server,review,samples,plot,log; before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});review=await server.ssrLoadModule('../../plugins/vesc/frontend/src/sampleReview.ts');samples=await server.ssrLoadModule('../../plugins/vesc/frontend/src/samples.ts');plot=await server.ssrLoadModule('../../plugins/vesc/frontend/src/SignalPlot.tsx');log=await server.ssrLoadModule('../../plugins/vesc/frontend/src/logAnalysis.ts');}); after(async()=>{await server?.close();}); const row=i=>({index:i,i1:Math.sin(2*Math.PI*125*i/2000),i2:0,i3:0,v1:1,v2:2,v3:3,v0:4,current:0,switching_hz:20000,commutation:2,hall:3,phase_deg:180,dt_s:.0005,indexed:false,third_current_measured:false}); const passport=()=>({schema:'missioncore.vesc.sample-data/v1',identity:{uuid:'00112233445566778899AABB',hardware:'SYNTHETIC',version:'5.02'},session:'synthetic-usb-1',state:{capture_id:'synthetic-1',state:'complete',received:128,expected:128,mode:1,count:128,decimation:10,malformed:0},rows:Array.from({length:128},(_,i)=>row(i))}); const parse=v=>review.parseSampleRecord(JSON.stringify(v),'fixture.json'); const filter=extra=>({...review.defaultSampleFilter,...extra}); test('passport preserves source identity, approximate cadence and calculated-current provenance',()=>{const p=parse(passport());assert.equal(p.uuid,'00112233445566778899AABB');assert.equal(p.firmware,'5.02');assert.equal(p.points[1].at,.5);assert.equal(p.complete,true);assert.equal(p.thirdMeasured,false);assert.equal(p.capture,'synthetic-1');}); test('partial/unverified passports cannot be mistaken for full captures',()=>{for(const state of ['incomplete','unverified','timeout','cancelled']){const v=passport();v.state.state=state;v.state.expected=2000;assert.equal(parse(v).complete,false);}}); test('damaged, oversized or contradictory passports fail closed',()=>{ for(const change of [v=>v.schema='other',v=>v.rows[1].index=4,v=>v.rows[1].i1='3',v=>v.rows[1].i1=null,v=>v.rows[1].dt_s=-1,v=>v.rows[1].dt_s=2,v=>v.rows[1].hall=8,v=>v.state.received=100,v=>v.state.expected=2000,v=>v.state.malformed=1,v=>v.identity.uuid='wrong',v=>v.rows=[],v=>v.rows=Array.from({length:2001},(_,i)=>row(i)),v=>v.state.state='constructor']){const v=passport();change(v);assert.throws(()=>parse(v));} assert.throws(()=>review.parseSampleRecord(' '.repeat(4*1024*1024+1),'huge'));assert.throws(()=>review.parseSampleRecord('{','bad')); }); test('legacy CSV never borrows connected-controller identity; missing signals stay absent',()=>{ const csv='T;I1;I2;I3;V1;V2;V3;I_tot;V_zero;Phase\n0;1;2;-3;4;5;6;7;8;180;\n0.0001;2;3;-5;4;5;6;7;8;181;'; const p=review.parseSampleRecord(csv,'old.csv');assert.equal(p.uuid,null);assert.equal(p.complete,null);assert.equal(p.thirdMeasured,null);assert.equal(p.points[1].at,.1);assert.equal(p.points[1].values.hall,undefined);assert.equal(p.points[0].values.i3,-3); assert.equal(review.parseSampleRecord(samples.sampleCsv([row(0),row(1)]),'own.csv').points[1].values.hall,3); }); test('FIR matches Tool frequency-bin inverse transform and Hamming window',()=>{ const expected=[0,.03661165235168156,.125,.21338834764831843,.25,.21338834764831843,.125,.03661165235168156]; const actual=review.filterCoefficients(filter({kind:'fir',bits:3,cutoff:.125,hamming:false}));actual.forEach((v,i)=>assert.ok(Math.abs(v-expected[i])<1e-12,`${i}: ${v}`)); const windowed=review.filterCoefficients(filter({kind:'fir',bits:3,cutoff:.125,hamming:true}));windowed.forEach((v,i)=>assert.ok(Math.abs(v-expected[i]*(.54-.46*Math.cos(2*Math.PI*i/7)))<1e-12)); assert.deepEqual(review.filterCoefficients(filter({kind:'mean',bits:2})),[.25,.25,.25,.25]); for(const extra of [{bits:0},{bits:11},{cutoff:NaN},{cutoff:.6}])assert.throws(()=>review.filterCoefficients(filter({kind:'fir',...extra}))); }); test('Tool convolution and delay preserve unsupported edges as gaps, never zeros',()=>{ const points=Array.from({length:12},(_,i)=>({at:i,values:{i1:i,hall:i%8},gap:false})); const centered=review.filteredSamplePoints(points,'i1',filter({kind:'mean',bits:2}));assert.equal(centered[0].value,null);assert.equal(centered[1].value,null);assert.equal(centered[2].value,1.5);assert.equal(centered[9].value,8.5);assert.equal(centered[10].value,null); const delayed=review.filteredSamplePoints(points,'i1',filter({kind:'mean',bits:2,compensate:false}));assert.equal(delayed[3].value,null);assert.equal(delayed[4].value,1.5);assert.equal(delayed[11].value,8.5); assert.equal(review.filteredSamplePoints(points,'hall',filter({kind:'mean',bits:2}))[0].value,0); const absent=review.filteredSamplePoints(points.slice(0,4),'i1',filter({kind:'mean',bits:2}));assert.ok(absent.every(p=>p.value===null)); points[3].values.i1=null;assert.equal(review.filteredSamplePoints(points,'i1',filter({kind:'mean',bits:2}))[2].value,null); points[3].values.i1=3;points[3].at=3.2;assert.equal(review.filteredSamplePoints(points,'i1',filter({kind:'mean',bits:2}))[2].value,null); }); test('cursors use nearest real sample, keep holes and outside-record requests empty',()=>{ const points=[{at:0,value:1},{at:.1,value:null},{at:.2,value:3}];let m=review.sampleMeasurement(points,0,.2,.01,.19);assert.equal(m.first.at,0);assert.equal(m.second.at,.2);assert.equal(m.delta,2);assert.equal(m.dt,.2);assert.equal(m.count,2);assert.equal(m.rms,Math.sqrt(5)); m=review.sampleMeasurement(points,0,1,.1,.8);assert.equal(m.first,null);assert.equal(m.second,null);assert.equal(m.delta,null); }); test('submillisecond samples occupy full plot width instead of collapsing to the left',()=>{const view=plot.signalView([{at:0,value:1},{at:.01,value:2}],0,.01);assert.equal(view.segments[0][0].x,64);assert.equal(view.segments[0][1].x,780);}); test('filtering keeps frequency and rejects FFT over missing edges',()=>{ const points=samples.samplePoints(Array.from({length:1024},(_,i)=>row(i))),filtered=review.filteredSamplePoints(points,'i1',filter({kind:'mean',bits:3})); const mapped=filtered.map(p=>({at:p.at,values:{i1:p.value},gap:p.value===null}));assert.throws(()=>log.spectrum(mapped,'i1',0,512));const fft=log.spectrum(mapped,'i1',4,500);assert.ok(Math.abs(fft.peak.hz-125)<1e-8); }); test('responsive plot uses the measured viewport width without shrinking axis type',()=>{const points=[{at:0,value:1},{at:1,value:2}];for(const width of [320,480,960]){const v=plot.signalView(points,0,1,160,width);assert.equal(v.segments[0][0].x,64);assert.equal(v.segments[0][1].x,width-20);}});