Files
NODEDC_MISSION_CORE/apps/control-station/test/vescTrends.test.mjs
T
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

80 lines
6.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import ts from 'typescript';
const load=async name=>{
const source=readFileSync(new URL(`../../../plugins/vesc/frontend/src/${name}.ts`,import.meta.url),'utf8');
return import('data:text/javascript;base64,'+Buffer.from(ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText).toString('base64'));
};
const {VescTrace,traceView,traceCsv,TRACE_LIMIT}=await load('trends');
const {TraceObserver}=await load('traceObserver');
const frame=(sample=1000,extra={})=>({fresh:true,instance:'source',devices:[{id:'device',uuid:'ABC',session_id:'session',sample_time_ms:sample,observed_at:'2026-10-18T10:00:00Z',age_ms:0,values:{input_voltage_v:48,input_current_a:2,duty:.25,erpm:0},...extra}]});
test('native values retain units; missing/non-finite measurements never become zero',()=>{
const t=new VescTrace();assert.equal(t.accept(frame(), 'device','abc',1000,0),true);
assert.equal(t.points[0].values.duty,25);assert.equal(t.points[0].values.input_power_w,96);
assert.equal(t.points[0].values.motor_temperature_c,undefined);
const v=traceView(t.points,'motor_temperature_c',0,2000);assert.equal(v.count,0);assert.equal(v.min,null);
t.accept(frame(1100,{values:{input_voltage_v:NaN,erpm:Infinity,duty:false}}),'device','abc',1100,0);
assert.equal(t.points.at(-1).values,null);
});
test('duplicate/reordered samples do not extend freshness or add fake flat-line points',()=>{
const t=new VescTrace();t.accept(frame(),'device','abc',1000,0);
t.accept(frame(),'device','abc',1700,0);t.accept(frame(900),'device','abc',1800,0);
assert.equal(t.points.length,1);assert.equal(t.freshUntil,2500);
t.accept(frame(),'device','abc',2501,0);assert.equal(t.points.at(-1).values,null);
for(let i=0;i<10;i++)t.gap(2600+i);assert.equal(t.points.length,2);
});
test('USB sessions, service restarts and missing frames cannot join plot segments',()=>{
const t=new VescTrace();t.accept(frame(),'device','abc',1000,0);t.accept(frame(1300),'device','abc',1300,0);
t.accept(frame(1600,{session_id:'new-session'}),'device','abc',1600,0);
t.accept({...frame(1,{session_id:'new-session'}),instance:'restarted'},'device','abc',1900,0);
t.gap(2100);t.accept(frame(400,{session_id:'new-session'}),'device','abc',2200,0);
const v=traceView(t.points,'erpm',0,3000);assert.equal(v.segments.length,4);
assert.equal(t.points.filter(p=>p.values===null).length,3);
});
test('identity ambiguity, stale transport, missing timestamps and slow HTTP fail closed',()=>{
const invalid=[{...frame(),fresh:false},{...frame(),instance:''},{...frame(),devices:frame().devices.concat(frame().devices)},
frame(1000,{uuid:'other'}),frame(1000,{session_id:''}),frame(1000,{age_ms:1600}),frame(1000,{age_ms:-1}),
frame(undefined,{sample_time_ms:undefined}),frame(1000,{observed_at:'broken'})];
for(const f of invalid){const t=new VescTrace();assert.equal(t.accept(f,'device','abc',1000,0),false);assert.equal(t.points.length,0);}
const t=new VescTrace();assert.equal(t.accept(frame(),'device','abc',1000,1500),false);
});
test('timestamp gap creates a break even when no explicit disconnect arrived',()=>{
const t=new VescTrace();t.accept(frame(),'device','abc',1000,0);t.accept(frame(3100),'device','abc',3100,0);
assert.equal(traceView(t.points,'erpm',0,4000).segments.length,2);
});
test('rolling trace is bounded by count, time and UTC day; clear resets sample identity',()=>{
const t=new VescTrace();for(let i=0;i<2500;i++)t.accept(frame(i),'device','abc',i,0);
assert.equal(t.points.length,TRACE_LIMIT);t.prune(400000);assert.equal(t.points.length,0);
t.accept(frame(400001),'device','abc',400001,0);t.prune(400002,'2099-01-01');assert.equal(t.points.length,0);
t.accept(frame(400003),'device','abc',400003,0);t.clear();assert.equal(t.accept(frame(1),'device','abc',1,0),true);
});
test('CSV records gaps, session provenance, empty absent fields and escaped spreadsheet text',()=>{
const t=new VescTrace();t.accept(frame(),'device','abc',1000,0);t.gap(2000);
const csv=traceCsv(t.points,{uuid:'abc',hardware:'=cmd,"bad"',version:'5.02'});
assert.match(csv,/duty \[%\]/);assert.match(csv,/"gap"/);assert.match(csv,/"session"/);assert.match(csv,/"'=cmd,""bad"""/);
assert.equal(csv.split('\r\n').length,4);assert.doesNotMatch(csv,/undefined|NaN|Infinity/);
});
test('observer is single-flight, suppresses late results after close and aborts owned read',async t=>{
t.mock.timers.enable({apis:['setInterval','setTimeout']});
let resolve,calls=0,signal;const received=[];
const observer=new TraceObserver(s=>{calls++;signal=s;return new Promise(r=>resolve=r);},v=>received.push(v),()=>{},()=>true,()=>1000);
await Promise.resolve();t.mock.timers.tick(1000);assert.equal(calls,1);
observer.close();assert.equal(signal.aborted,true);resolve(frame());await Promise.resolve();await Promise.resolve();assert.equal(received.length,0);
t.mock.timers.tick(5000);assert.equal(calls,1);
});
test('hidden view starts no requests; aborted/expired read never delivers a stale frame',async t=>{
t.mock.timers.enable({apis:['setInterval','setTimeout']});
let visible=false,calls=0,resolve,signal;const received=[];
const observer=new TraceObserver(s=>{calls++;signal=s;return new Promise(r=>resolve=r);},v=>received.push(v),()=>{},()=>visible,()=>1000);
await Promise.resolve();assert.equal(calls,0);visible=true;t.mock.timers.tick(500);await Promise.resolve();assert.equal(calls,1);
t.mock.timers.tick(1201);assert.equal(signal.aborted,true);resolve(frame());await Promise.resolve();await Promise.resolve();
assert.equal(received.filter(Boolean).length,0);observer.close();
});
test('pause retains the last numeric value as historical and expanded plot uses full height',()=>{
const t=new VescTrace();t.accept(frame(),'device','abc',1000,0);t.gap(1400);
const a=traceView(t.points,'input_voltage_v',0,2000,180),b=traceView(t.points,'input_voltage_v',0,2000,384);
assert.equal(a.last,48);assert.equal(t.freshUntil,-Infinity);assert.ok(b.segments[0][0].y>a.segments[0][0].y);
});