import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { test } from 'node:test'; import ts from 'typescript'; async function source(name) { const code=ts.transpileModule(readFileSync(new URL(`../src/core/simulation/${name}.ts`,import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext}}).outputText; return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`); } const {dragWaypoint}=await source('waypointDrag'),{beltPose}=await source('roverMotion'); const motion=JSON.parse(readFileSync(new URL('../public/rover-scene/dcd006-v020-motion.json',import.meta.url))); const near=(a,b)=>assert.ok(Math.abs(a-b)<1e-7,`${a} != ${b}`); test('axis pivot only moves the selected coordinate, including elevation',()=>{ const p=[3,4,5],o=[8,12,16]; for(const [axis,index] of [['x',0],['y',1],['z',2]]) { const target=[...p];target[index]+=2; const result=dragWaypoint(o,target.map((v,i)=>v-o[i]),p,axis); result.forEach((v,i)=>near(v,target[i])); dragWaypoint(o,p.map((v,i)=>v-o[i]),p,axis).forEach((v,i)=>near(v,p[i])); } assert.equal(dragWaypoint([0,0,0],[1,0,0],p,'x'),null); }); test('belt loops without a seam and reverse travel returns to the same position',()=>{ for(const side of ['L','R'])for(let i=0;i<44;i++) { const a=beltPose(motion,-.8,side,i), b=beltPose(motion,motion.length_m-.8,side,i); a.position.forEach((v,j)=>near(v,b.position[j]));near(Math.sin(a.angle),Math.sin(b.angle)); } }); test('belt travels backward at ground contact, forward on the upper run, both sides aligned',()=>{ for(const [,start,length,d] of motion.segments.filter(s=>s[0]==='line')) { const s=start+length/2-motion.phase_m; const a=beltPose(motion,s,'L',0), b=beltPose(motion,s+.0001,'L',0); near((b.position[0]-a.position[0])/.0001,d[2]); near((b.position[1]-a.position[1])/.0001,d[3]); const right=beltPose(motion,s,'R',0);near(a.position[0],right.position[0]);near(a.position[1],right.position[1]); near(right.position[2]-a.position[2],motion.track_width_m); } }); test('local animated rig follows motion and freezes at zero input', async()=>{ const {Entity}=await import('playcanvas'); const mathCode=ts.transpileModule(readFileSync(new URL('../src/core/simulation/roverMotion.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext}}).outputText; const mathUrl=`data:text/javascript;base64,${Buffer.from(mathCode).toString('base64')}`; let code=ts.transpileModule(readFileSync(new URL('../src/components/simulation/RoverTrackMotion.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext}}).outputText; code=code.replace('"../../core/simulation/roverMotion"',JSON.stringify(mathUrl)); const {RoverTrackMotion}=await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`); const root=new Entity(); for(const side of ['L','R'])for(let i=0;i<44;i++)root.addChild(new Entity(`${side}_BELT_${String(i).padStart(2,'0')}`)); for(const w of motion.wheels)root.addChild(new Entity(w.node)); const rig=new RoverTrackMotion(root,motion);rig.update(.1,.3,0); const expected=beltPose(motion,.03,'L',0), node=root.findByName('L_BELT_00'); const p=node.getLocalPosition();[p.x,p.y,p.z].forEach((v,i)=>near(v,expected.position[i])); const before=node.getLocalRotation().clone();rig.update(.1,0,0);assert.ok(node.getLocalRotation().equals(before)); rig.update(.1,-.3,0);const atOrigin=beltPose(motion,0,'L',0), reset=node.getLocalPosition(); [reset.x,reset.y,reset.z].forEach((v,i)=>near(v,atOrigin.position[i])); root.destroy(); });