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 {normalizePlannerPreferences,DEFAULT_PLANNER_PREFERENCES,axisWorldLength}=await source('plannerPreferences'); const {zoomDisplacement}=await source('editorZoom'); test('preferences survive round trip, validate old/corrupt storage and clamp physical dimensions',()=>{ const value={...DEFAULT_PLANNER_PREFERENCES,waypoint:{...DEFAULT_PLANNER_PREFERENCES.waypoint,diameter:.8,height:3,color:'#00aaff',opacity:.5,frequency:0}}; assert.deepEqual(normalizePlannerPreferences(JSON.parse(JSON.stringify(value))),value); assert.deepEqual(normalizePlannerPreferences(null),DEFAULT_PLANNER_PREFERENCES); const invalid=normalizePlannerPreferences({pivot:{x:'url(x)',opacity:99},waypoint:{height:-9,diameter:NaN}}); assert.equal(invalid.pivot.x,DEFAULT_PLANNER_PREFERENCES.pivot.x); assert.equal(invalid.pivot.opacity,1);assert.equal(invalid.waypoint.height,.2);assert.equal(invalid.waypoint.diameter,.35); }); test('perspective projection preserves gizmo pixels at different depths and viewport heights',()=>{ for(const depth of [1,10,500])for(const height of [500,900,1600]) { const projected=axisWorldLength(depth,height,105)/(2*depth*Math.tan(Math.PI/6))*height; assert.ok(Math.abs(projected-105)<1e-8); } }); test('far zoom takes proportional steps and equal reverse wheel deltas return to original distance',()=>{ for(const depth of [2,20,200,2000]) { const next=depth-zoomDisplacement(depth,80); assert.ok(next>depth); const restored=next-zoomDisplacement(next,-80); assert.ok(Math.abs(restored-depth)<1e-8); assert.ok(zoomDisplacement(depth,-80)>depth*.1); } }); const {waypointHit}=await source('waypointHit'); test('waypoint picks complete sides, caps and a camera inside the beam at any physical size',()=>{ for(const radius of [.025,.5,1.5])for(const height of [.2,5,50]) { assert.notEqual(waypointHit([radius*.9,height*.9,5],[0,0,-1],[0,0,0],radius,height),null); assert.notEqual(waypointHit([radius*.9,height+1,0],[0,-1,0],[0,0,0],radius,height),null); assert.notEqual(waypointHit([0,height/2,0],[0,0,1],[0,0,0],radius,height),null); assert.equal(waypointHit([radius*1.1,height/2,5],[0,0,-1],[0,0,0],radius,height),null); } }); test('world-size centroid preference migrates to independent pixel size; emission is bounded',()=>{ const prefs=normalizePlannerPreferences({navigation:{centroidDiameter:5},waypoint:{emission:20,emissionColor:'invalid'}}); assert.equal(prefs.navigation.centroidPixels,14); assert.equal(prefs.waypoint.emission,5); for(const depth of [1,30,300]) { const pixels=axisWorldLength(depth,720,prefs.navigation.centroidPixels)/(2*depth*Math.tan(Math.PI/6))*720; assert.ok(Math.abs(pixels-14)<1e-8); } });