import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; let server,m,parse; before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});m=await server.ssrLoadModule('../../plugins/vesc/frontend/src/rotorHallReview.ts');parse=(await server.ssrLoadModule('../../plugins/vesc/frontend/src/sampleReview.ts')).parseSampleRecord;}); after(async()=>{await server?.close();}); const points=(states)=>states.map((h,i)=>({at:i,values:{hall:h,phase_deg:(358+i)%360,current:i%2?-3:2},gap:false})); const review=(p,from=0,to=2000)=>m.reviewRotorHall(p,from,to); test('six-state sequence and reverse direction are not a winding-order diagnosis',()=>{for(const s of [[1,3,2,6,4,5,1],[1,5,4,6,2,3,1]]){const r=review(points(s));assert.equal(r.transitions,6);assert.equal(r.events.filter(e=>e.kind==='change').length,6);assert.equal(r.counts.slice(1,7).filter(Boolean).length,6);}}); test('repeated state reports observed span, never stall or complete sector dwell',()=>{const r=review(points([1,1,1,1]));assert.equal(r.longest[1],3);assert.equal(r.transitions,0);assert.equal(r.events.length,0);assert.equal(r.peakCurrent,3);}); test('0 and 7 remain separate runs; unknown Hall is not silently zero',()=>{const r=review(points([0,0,7,null,2.5,NaN,8,1]));assert.deepEqual(r.counts,[2,1,0,0,0,0,0,1]);assert.equal(r.missing,4);assert.deepEqual(r.events.filter(e=>e.kind!=='change').map(e=>[e.kind,e.first,e.last]),[['code',0,1],['code',2,2],['missing',3,6]]);}); test('mapped commutation codes never become raw-input edges or a sensor diagnosis',()=>{const r=review(points([1,2,5]));assert.equal(r.transitions,2);assert.equal(r.multi,undefined);assert.equal(r.single,undefined);assert.deepEqual(r.events.map(e=>e.label),['Код Hall: 1 → 2','Код Hall: 2 → 5']);}); test('do not bridge a gap, missing state, missing phase or interval boundary',()=>{ const p=points([1,6,2,5]);p[1].gap=true;const r=review(p);assert.equal(r.transitions,1);assert.equal(r.missing,1);assert.equal(r.phaseCount,3); assert.equal(review(points([1,2]),1,1).transitions,0); const q=points([1,null,2]);q[1].values.phase_deg=null;assert.equal(review(q).transitions,0);assert.equal(review(q).phaseStep,null); }); test('phase wraps across 0 without reporting a 359 degree jump; half turn ambiguous',()=>{assert.equal(m.wrappedPhaseStep(359,1),2);assert.equal(m.wrappedPhaseStep(1,359),-2);const p=points([1,1,1]);p[0].values.phase_deg=0;p[1].values.phase_deg=180;p[2].values.phase_deg=360;const r=review(p);assert.equal(r.halfTurns,2);assert.equal(r.phaseStep,180);}); test('missing and out-of-range phase does not become measured mechanical angle',()=>{const p=points([1,1,1,1]);for(const [i,v] of [null,-1,361,Infinity].entries())p[i].values.phase_deg=v;assert.equal(review(p).phaseCount,0);assert.equal(review(p).phaseStep,null);}); test('event navigation keeps original indices and cursor times with bounded context',()=>{const p=points(Array(20).fill(1));p[12].values.hall=2;const r=review(p,10,15);const w=m.hallEventWindow(p,r.events[0]);assert.deepEqual(w,{from:6,to:17,first:11,second:12});}); test('empty interval and one sample never fabricate movement',()=>{const p=points([1]);assert.equal(review(p,2,3).count,0);const r=review(p);assert.equal(r.phaseStep,null);assert.equal(r.longest[1],0);assert.equal(r.transitions,0);}); test('bounded inputs and ordered finite clock enforced',()=>{for(const p of [Array.from({length:2001},(_,i)=>({...points([1])[0],at:i})),points([1,1]).map(p=>({...p,at:0})),[{at:NaN,values:{},gap:false}]])assert.throws(()=>review(p));assert.throws(()=>review(points([1]),2,1));}); test('old VESC CSV without Hall preserves unavailable channel; no fault claim',()=>{const csv='T;I1;I2;I3;V1;V2;V3;I_tot;V_zero;Phase\n0;0;0;0;0;0;0;0;0;10\n0.01;0;0;0;0;0;0;0;0;20';const r=parse(csv,'synthetic.csv');assert.equal(r.complete,null);const a=review(r.points);assert.equal(a.missing,2);assert.equal(a.counts[0],0);assert.equal(a.transitions,0);}); test('review never mutates source or interpolates skipped transitions',()=>{const p=points([1,6,1]),before=structuredClone(p);const r=review(p);assert.deepEqual(p,before);assert.equal(r.transitions,2);assert.equal(r.count,3);});