import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {test} from 'node:test'; import ts from 'typescript'; import {PerspectiveCamera,Vector3} from 'three'; function moduleUrl(url){ const code=ts.transpileModule(readFileSync(url,'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext}}).outputText .replace(/from "([^"]+)"/g,(_,name)=>`from "${name.startsWith('.')?moduleUrl(new URL(name+'.ts',url)):import.meta.resolve(name)}"`); return `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; } async function source(name){return import(moduleUrl(new URL(`../src/core/simulation/${name}.ts`,import.meta.url)));} const {panCamera,orbitCamera,navigationDepth}=await source('editorNavigation'); const {editWaypoints,waypointEditing,waypointShortcut}=await source('waypointEditing'); const {streamRecovery}=await source('streamRecovery'); const {panSensitivity,normalizePlannerPreferences}=await source('plannerPreferences'); test('pan follows the same pixel displacement at 2, 20 and 500 metre distances',()=>{ for(const distance of [2,20,500]){ const camera=new PerspectiveCamera(60,1.5,.05,3000);camera.position.set(0,0,distance);camera.updateMatrixWorld(); const anchor=new Vector3();const depth=navigationDepth(camera,anchor); panCamera(camera,depth,120,-80,720); const pixel=anchor.project(camera); assert.ok(Math.abs(pixel.x*1080/2-120)<1e-8); assert.ok(Math.abs(-pixel.y*720/2+80)<1e-8); } }); test('off-centre orbit pivot stays under cursor without jump, roll or radius drift',()=>{ const camera=new PerspectiveCamera(60,1.5,.05,3000);camera.position.set(0,5,10);camera.lookAt(0,0,0);camera.updateMatrixWorld(); const anchor=new Vector3(2,0,1),screen=anchor.clone().project(camera),radius=camera.position.distanceTo(anchor); for(let i=0;i<20;i++){ orbitCamera(camera,anchor,8,3,720); assert.ok(anchor.clone().project(camera).distanceTo(screen)<1e-10); assert.ok(Math.abs(camera.position.distanceTo(anchor)-radius)<1e-10); assert.ok(Math.abs(new Vector3(1,0,0).applyQuaternion(camera.quaternion).y)<1e-10); } }); const points=[{id:'a',position:[0,0,0]},{id:'b',position:[2,0,0]}]; test('selection never alters route; delete/undo restores order, coordinates and selection',()=>{ let s=waypointEditing(points);s=editWaypoints(s,{type:'select',id:'a'});s=editWaypoints(s,{type:'select',id:null}); assert.equal(s.history.length,0);assert.equal(s.sequence,0); s=editWaypoints(s,{type:'select',id:'a'});s=editWaypoints(s,{type:'delete'}); assert.deepEqual(s.points,[points[1]]);assert.equal(s.selected,null); s=editWaypoints(s,{type:'undo'});assert.deepEqual(s.points,points);assert.equal(s.selected,'a');assert.equal(s.sequence,2); }); test('drag creates one undo entry, click none, canceled drag restores coordinates',()=>{ let s=editWaypoints(waypointEditing(points),{type:'select',id:'b'}); s=editWaypoints(s,{type:'begin'});s=editWaypoints(s,{type:'commit'});assert.equal(s.history.length,0); s=editWaypoints(s,{type:'begin'}); for(let i=0;i<20;i++)s=editWaypoints(s,{type:'move',points:[points[0],{id:'b',position:[3+i,0,0]}]}); s=editWaypoints(s,{type:'commit'});assert.equal(s.history.length,1); s=editWaypoints(s,{type:'undo'});assert.deepEqual(s.points,points); s=editWaypoints(s,{type:'begin'});s=editWaypoints(s,{type:'move',points:[]});s=editWaypoints(s,{type:'cancel'});assert.deepEqual(s.points,points); }); test('new task isolates history and shortcuts preserve native text editing',()=>{ let s=waypointEditing(points);s=editWaypoints(s,{type:'replace',points:[]});s=editWaypoints(s,{type:'load',points}); assert.equal(s.history.length,0); const e={key:'z',code:'KeyZ',ctrlKey:false,metaKey:false,shiftKey:false,altKey:false}; assert.equal(waypointShortcut({...e,ctrlKey:true},false),'undo');assert.equal(waypointShortcut({...e,metaKey:true},false),'undo'); assert.equal(waypointShortcut({...e,metaKey:true},true),null); assert.equal(waypointShortcut({...e,key:'Delete',code:'Delete'},false),'delete'); assert.equal(waypointShortcut({...e,key:'Backspace',code:'Backspace'},false),'delete'); assert.equal(waypointShortcut({...e,key:'Delete',code:'Delete',ctrlKey:true},false),null); }); test('navigation preferences migrate, maintain ordered distances and interpolate smoothly',()=>{ const p=normalizePlannerPreferences({waypoint:{height:50},navigation:{nearDistance:3,farDistance:100,nearPan:.5,farPan:2}}); assert.equal(p.waypoint.height,50);assert.equal(p.navigation.zoom,1); assert.equal(panSensitivity(1,p.navigation),.5);assert.equal(panSensitivity(200,p.navigation),2); assert.equal(panSensitivity(51.5,p.navigation),1.25); const bad=normalizePlannerPreferences({navigation:{nearDistance:50,farDistance:2,zoom:NaN}}); assert.equal(bad.navigation.farDistance,51);assert.equal(bad.navigation.zoom,1); assert.deepEqual(normalizePlannerPreferences(JSON.parse(JSON.stringify(p))),p); }); test('video watchdog bounds first-frame wait, stall recovery and retry count',()=>{ assert.equal(streamRecovery(19999,0,null,null,0),'wait');assert.equal(streamRecovery(20000,0,null,null,0),'retry'); assert.equal(streamRecovery(20000,0,null,null,2),'failed');assert.equal(streamRecovery(20000,0,15000,null,0),'wait'); assert.equal(streamRecovery(25000,0,15000,null,1),'retry');assert.equal(streamRecovery(5000,0,null,3000,2),'failed'); }); const {viewedAiRun}=await source('viewedAiRun'); test('catalog discovery of a run cannot claim an idle or historical viewer',()=>{ const old={run_id:'old',world:{world_id:'forest'}},live={run_id:'new',world:{world_id:'forest'}}; const catalog={runtime:{active_run:live},runs:[old,live]}; assert.equal(viewedAiRun(catalog,'forest',null),undefined); assert.equal(viewedAiRun(catalog,'forest','old'),old); assert.equal(viewedAiRun(catalog,'forest','new'),live); assert.equal(viewedAiRun(catalog,'bamboo','new'),undefined); }); test('cube preferences migrate without changing older settings and validate saved presentation',()=>{ const p=normalizePlannerPreferences({pivot:{pixels:125},cube:{size:500,face:'red',opacity:-1,edgeOpacity:-2,text:'#123456'}}); assert.equal(p.pivot.pixels,125);assert.equal(p.cube.size,160);assert.equal(p.cube.face,'#292929');assert.equal(p.cube.opacity,.2);assert.equal(p.cube.text,'#123456'); assert.equal(normalizePlannerPreferences({}).cube.size,66); assert.equal(p.cube.edgeOpacity,0);assert.equal(normalizePlannerPreferences({}).cube.edgeOpacity,1); assert.deepEqual(normalizePlannerPreferences(JSON.parse(JSON.stringify(p))),p); });