import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; let server,log,trends,plot,telemetry; before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});log=await server.ssrLoadModule('../../plugins/vesc/frontend/src/logAnalysis.ts');trends=await server.ssrLoadModule('../../plugins/vesc/frontend/src/trends.ts');plot=await server.ssrLoadModule('../../plugins/vesc/frontend/src/SignalPlot.tsx');telemetry=await server.ssrLoadModule('./src/components/rover/RoverTelemetry.tsx');}); after(async()=>{await server?.close();}); const rt=(rows)=>'ms_today;input_voltage;current_motor;duty_cycle;vesc_id;\n'+rows.map(r=>r.join(';')+';').join('\n'); test('original RT CSV preserves measured zero/missing values and converts PWM only once',()=>{ const v=log.parseVescLog(rt([[100,48,0,.5,1],[200,47,'',0,1]])); assert.equal(v.format,'VESC Tool · RT');assert.equal(v.duration,100);assert.equal(v.points[0].values.duty_cycle,50);assert.equal(v.points[1].values.current_motor,null); assert.equal(v.uuid,null);assert.equal(v.columns[0].unit,'В'); }); test('original rich Log Analysis headers retain arbitrary columns, units and missing cells',()=>{ const v=log.parseVescLog('t_day:Time:s:3:1:1;custom:Signal:A:2:0:0\n2;1\n2.1;\n2.2;3\n'); assert.equal(v.columns[0].label,'Signal');assert.equal(v.columns[0].unit,'A');assert.equal(v.points[1].values.custom,null);assert.ok(Math.abs(v.duration-200)<1e-8); }); test('Mission Core exported CSV round-trips measured units, metadata, escaped CSV and session gaps',()=>{ const p=[{at:10,source:'a',session:'one',sample:1,utc:'2026-10-18T01:00:00Z',values:{duty:20,input_voltage_v:48}},{at:20,source:'a',session:'one',sample:2,utc:'',values:null},{at:30,source:'b',session:'two',sample:3,utc:'2026-10-18T01:00:01Z',values:{duty:30}}]; const v=log.parseVescLog(trends.traceCsv(p,{uuid:'abc123',hardware:'board,"quoted"',version:'5.02'})); assert.equal(v.uuid,'abc123');assert.equal(v.hardware,'board,"quoted"');assert.equal(v.points[0].values['duty [%]'],20);assert.equal(v.points.at(-1).values['duty [%]'],30);assert.ok(v.points.some(p=>p.gap)); }); test('time wraps at midnight with explicit gap, regressions and mixed controllers are rejected',()=>{ const v=log.parseVescLog(rt([[86399900,48,0,0,1],[100,48,0,0,1]]));assert.equal(v.duration,200);assert.equal(v.points[1].gap,true); assert.throws(()=>log.parseVescLog(rt([[200,48,0,0,1],[100,48,0,0,1]])),/назад/); assert.throws(()=>log.parseVescLog(rt([[100,48,0,0,1],[200,48,0,0,2]])),/разные контроллеры/); }); test('parser rejects malformed widths, duplicate keys, formula, nonfinite and oversized input',()=>{ for(const value of ['ms_today;x;x\n1;2;3','ms_today;x\n1;2;3','ms_today;x\n1;=2+2','ms_today;x\n1;NaN','ms_today;x\n1;Infinity','ms_today;x\n1;"broken','ms_today;x\n-1;2','unknown;x\n1;2'])assert.throws(()=>log.parseVescLog(value)); assert.throws(()=>log.parseVescLog('x'.repeat(log.LOG_MAX_BYTES+1)),/размер/); assert.throws(()=>log.parseVescLog('ms_today;x\n'+Array.from({length:20001},(_,i)=>`${i};1`).join('\n')),/20 000/); }); test('Hann FFT finds known frequency/amplitude and removes DC without changing source',()=>{ const points=Array.from({length:1024},(_,i)=>({at:i*10,values:{x:5+3*Math.sin(2*Math.PI*12.5*i/100)},gap:false})); const result=log.spectrum(points,'x',0,10240);assert.equal(result.n,1024);assert.equal(result.rate,100);assert.equal(result.peak.hz,12.5);assert.ok(Math.abs(result.peak.amplitude-3)<.001);assert.ok(result.bins[0].amplitude<.001);assert.equal(points[0].values.x,5); }); test('FFT refuses gaps, duplicate time, irregular samples, missing values and short intervals',()=>{ const base=Array.from({length:64},(_,i)=>({at:i*10,values:{x:i},gap:false})); for(const data of [base.slice(0,31),base.map((p,i)=>i===30?{...p,gap:true}:p),base.map((p,i)=>i===30?{...p,at:p.at-10}:p),base.map((p,i)=>i===30?{...p,values:{x:null}}:p)])assert.throws(()=>log.spectrum(data,'x',0,1000)); assert.throws(()=>log.spectrum(base.map((p,i)=>({...p,at:p.at+(i===1?2:0)})),'x',0,1000),/неравномерен/); }); test('plot reduction retains a one-sample peak and does not join a gap',()=>{ const points=Array.from({length:10000},(_,i)=>({at:i,value:i===5000?100:i===6000?null:0})); const v=plot.signalView(points,0,10000);assert.equal(v.max,100);assert.equal(v.count,9999);assert.equal(v.segments.length,2);assert.ok(v.segments.flat().length<3000);assert.ok(v.segments[0].some(p=>p.y<40)); }); test('telemetry uses identity columns and never treats stale, wrong UUID, NaN or negative age as live',()=>{ const d=(n,age=20)=>({id:'d'+n,uuid:'uuid'+n,label:'device'+n,age_ms:age,values:{motor_current_a:0,fault_code:0}}); for(const count of [1,2,3,4]){const v=telemetry.roverTelemetryTable({fresh:true,snapshot:{devices:Array.from({length:count},(_,i)=>d(i))}});assert.equal(v.columns.length,count);assert.ok(v.rows.every(row=>row.cells.length===count));} for(const age of [1000,-1,NaN]){const v=telemetry.roverTelemetryTable({fresh:true,snapshot:{devices:[d(1,age)]}});assert.equal(v.rows.find(r=>r.id==='motor_current_a').cells[0],'—');assert.equal(v.rows.find(r=>r.id==='fault').cells[0],'—');} const stale=telemetry.roverTelemetryTable({fresh:false,snapshot:{devices:[d(1)]}});assert.equal(stale.columns.length,1);assert.equal(stale.rows.find(r=>r.id==='fault').cells[0],'—'); const mismatch=telemetry.roverTelemetryTable({fresh:true,snapshot:{devices:[d(1)],profile:{bindings:{left:{device_id:'d1',uuid:'other'}}}}});assert.equal(mismatch.columns.length,2);assert.equal(mismatch.rows.find(r=>r.id==='status').cells[0],'Нет связи'); });