import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {test} from 'node:test'; import ts from 'typescript'; import {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')}`; } const {EditorCamera}=await import(moduleUrl(new URL('../src/components/simulation/EditorCamera.ts',import.meta.url))); class Surface extends EventTarget { captures=new Set();clientWidth=1000;clientHeight=600; closest(){return null;}focus(){} getBoundingClientRect(){return {left:0,top:0,width:this.clientWidth,height:this.clientHeight};} setPointerCapture(id){this.captures.add(id);}hasPointerCapture(id){return this.captures.has(id);} releasePointerCapture(id){this.captures.delete(id);} } const tick=()=>new Promise(resolve=>setImmediate(resolve)); function fixture(){ globalThis.window=new EventTarget();globalThis.document={activeElement:null}; const surface=new Surface(),c=new EditorCamera(surface,surface);c.resize(1000,600);c.home([0,0,0]); const point=new Vector3(2,0,-1),screen=point.clone().project(c.camera); const x=(screen.x+1)*500,y=(1-screen.y)*300; function event(type,props={}){const e=new Event(type,{cancelable:true});Object.assign(e,{pointerId:1,button:0,clientX:x,clientY:y,...props});surface.dispatchEvent(e);return e;} return {surface,c,point,screen,x,y,event}; } test('Gaussian depth supplies off-centre orbit pivot without a collision mesh; buffered motion preserves projection',async()=>{ const f=fixture();let resolve;f.c.pickSurface=()=>new Promise(r=>resolve=r); const before=f.c.camera.position.clone();f.event('pointerdown');f.event('pointermove',{clientX:f.x+40,clientY:f.y+20}); assert.ok(f.c.camera.position.equals(before));assert.equal(f.c.rotationCenter,null); resolve(f.point.toArray());await tick(); assert.ok(f.c.rotationCenter.distanceTo(f.point)<1e-10); assert.ok(f.point.clone().project(f.c.camera).distanceTo(f.screen)<1e-10); assert.ok(f.c.camera.position.distanceTo(before)>.1); f.c.dispose(); }); test('released and superseded async picks cannot move the camera or replace the next pivot',async()=>{ const f=fixture(),pending=[];f.c.pickSurface=()=>new Promise(r=>pending.push(r)); f.event('pointerdown');f.event('pointermove',{clientX:f.x+20});f.event('pointerup'); const before=f.c.camera.position.clone();pending[0]([99,99,99]);await tick();assert.ok(f.c.camera.position.equals(before)); f.event('pointerdown');f.event('pointerdown',{pointerId:2});pending[1]([99,99,99]);await tick(); assert.equal(f.c.rotationCenter,null);pending[2](f.point.toArray());await tick(); assert.ok(f.c.rotationCenter.distanceTo(f.point)<1e-10);f.c.dispose(); }); test('view, resize, layer and mode changes invalidate in-flight surface picks',async()=>{ for(const cancel of [c=>c.home([1,0,1]),c=>c.top([[0,0,0],[10,0,10]]),c=>c.resize(800,600),c=>c.invalidateSurface(),c=>c.setAddMode(true),c=>c.setNavigation(false)]){ const f=fixture();let resolve;f.c.pickSurface=()=>new Promise(r=>resolve=r); f.event('pointerdown');f.event('pointermove',{clientX:f.x+20});cancel(f.c); const before=f.c.camera.position.clone();resolve(f.point.toArray());await tick(); assert.ok(f.c.camera.position.equals(before));assert.equal(f.c.rotationCenter,null);assert.equal(f.surface.captures.size,0);f.c.dispose(); } }); test('empty sky fallback remains on clicked ray instead of camera centre',async()=>{ const f=fixture();f.c.pickSurface=async()=>null;f.event('pointerdown');await tick(); const projected=f.c.rotationCenter.clone().project(f.c.camera); assert.ok(Math.abs(projected.x-f.screen.x)<1e-10);assert.ok(Math.abs(projected.y-f.screen.y)<1e-10); f.c.dispose(); }); test('waypoint add mode does not start orbit or visual picking',()=>{ const f=fixture();let picks=0;f.c.pickSurface=async()=>{picks++;return [0,0,0];};f.c.setAddMode(true);f.event('pointerdown'); assert.equal(picks,0);assert.equal(f.c.rotationCenter,null);f.c.dispose(); }); test('semantic editing reserves bare left drag for tools and Ctrl-left drag for orbit',()=>{ const f=fixture();f.c.semanticSelection=true; const before=f.c.camera.position.clone(); f.event('pointerdown');f.event('pointermove',{clientX:f.x+40});f.event('pointerup'); assert.ok(f.c.camera.position.equals(before));assert.equal(f.surface.captures.size,0); const down=f.event('pointerdown',{ctrlKey:true}); // Modifier changes mid-gesture must not change camera ownership. f.event('pointermove',{clientX:f.x+40,ctrlKey:false}); assert.equal(down.defaultPrevented,true);assert.ok(f.c.camera.position.distanceTo(before)>.1); f.event('pointerup');const ended=f.c.camera.position.clone(); f.event('pointermove',{clientX:f.x+80});assert.ok(f.c.camera.position.equals(ended)); f.c.dispose(); }); test('Ctrl-click mapped to the secondary button on macOS still orbits instead of panning',()=>{ const f=fixture();f.c.semanticSelection=true; const orientation=f.c.camera.quaternion.clone(); f.event('pointerdown',{ctrlKey:true,button:2});f.event('pointermove',{clientX:f.x+40}); assert.ok(f.c.camera.quaternion.angleTo(orientation)>.01);assert.ok(f.c.rotationCenter); f.event('pointercancel');assert.equal(f.surface.captures.size,0);f.c.dispose(); }); test('Ctrl-orbit accepts a semantic handle or pivot, but never an unrelated toolbar control',()=>{ for(const allowed of [false,true]){ const f=fixture();f.c.semanticSelection=true; const e=new Event('pointerdown',{cancelable:true}); Object.assign(e,{pointerId:1,button:0,ctrlKey:true,clientX:f.x,clientY:f.y}); const target={closest(selector){return allowed&&selector.includes('data-semantic-')?this:null;}}; Object.defineProperty(e,'target',{value:target}); const before=f.c.camera.position.clone();f.surface.dispatchEvent(e);f.event('pointermove',{clientX:f.x+40}); assert.equal(f.c.camera.position.equals(before),!allowed);f.c.dispose(); } }); test('leaving semantic editing restores unmodified left-drag orbit; right-drag pan is preserved',()=>{ const f=fixture();f.c.semanticSelection=true;const before=f.c.camera.position.clone(),q=f.c.camera.quaternion.clone(); f.event('pointerdown',{button:2});f.event('pointermove',{clientX:f.x+40});f.event('pointerup'); assert.ok(f.c.camera.position.distanceTo(before)>.1);assert.ok(f.c.camera.quaternion.equals(q)); f.c.semanticSelection=false;f.event('pointerdown');f.event('pointermove',{clientX:f.x+40}); assert.ok(f.c.camera.quaternion.angleTo(q)>.01);f.c.dispose(); }); const {cameraRay,screenSpan}=await import(moduleUrl(new URL('../src/core/simulation/editorProjection.ts',import.meta.url))); test('orthographic projection preserves focal-plane framing; depth never changes line width',()=>{ const f=fixture();f.c.top([[-20,0,-10],[20,0,10]]); const samples=[new Vector3(0,0,0),new Vector3(2,0,3)],before=samples.map(p=>p.clone().project(f.c.camera)); f.c.setProjection('orthographic');assert.equal(f.c.camera.isOrthographicCamera,true); samples.forEach((p,i)=>{const q=p.clone().project(f.c.camera);assert.ok(Math.hypot(q.x-before[i].x,q.y-before[i].y)<1e-8);}); const q=samples[1].clone().project(f.c.camera),above=samples[1].clone().add(new Vector3(0,10,0)).project(f.c.camera); assert.ok(Math.hypot(q.x-above.x,q.y-above.y)<1e-8); f.c.setProjection('perspective');samples.forEach((p,i)=>{const q=p.clone().project(f.c.camera);assert.ok(Math.hypot(q.x-before[i].x,q.y-before[i].y)<1e-8);});f.c.dispose(); }); test('orthographic cursor zoom keeps anchor fixed, pan is metric, resize preserves shape',()=>{ const f=fixture();f.c.top([[-20,0,-10],[20,0,10]]);f.c.setProjection('orthographic'); const p=new Vector3(4,0,2),q=p.clone().project(f.c.camera);f.c.pick=()=>p.toArray(); const span=screenSpan(f.c.camera,5); f.event('wheel',{clientX:(q.x+1)*500,clientY:(1-q.y)*300,deltaY:-120,deltaMode:0}); const after=p.clone().project(f.c.camera);assert.ok(Math.hypot(q.x-after.x,q.y-after.y)<1e-8);assert.ok(screenSpan(f.c.camera,5){ const f=fixture();f.c.top([[-20,0,-10],[20,0,10]]);f.c.setProjection('orthographic');const span=screenSpan(f.c.camera,5); for(const x of [-1,0,1])for(const y of [-1,0,1])for(const z of [-1,0,1]){ if(!x&&!y&&!z)continue;f.c.view([x,y,z]); const expected=new Vector3(x,y,z).normalize().negate(),direction=f.c.camera.getWorldDirection(new Vector3());assert.ok(direction.distanceTo(expected)<1e-8); const a=cameraRay(f.c.camera,.3,.4),b=cameraRay(f.c.camera,.8,.6);assert.ok(a.direction.distanceTo(b.direction)<1e-8);assert.ok(a.origin.distanceTo(b.origin)>1); assert.equal(screenSpan(f.c.camera,5),span);assert.ok(new Vector3().project(f.c.camera).length()<1.01); } f.c.dispose(); }); test('camera snapshot restores exact pose, orthographic scale and orbit anchor after reopening',()=>{ for(const projection of ['orthographic','perspective']){ const f=fixture();f.c.top([[-20,0,-10],[20,0,10]]);f.c.setProjection(projection);f.c.view([1,1,1]); const stored={...f.c.snapshot(),clip:[null,77]};f.c.dispose(); const next=fixture();next.c.restore(JSON.parse(JSON.stringify(stored))); assert.deepEqual(next.c.snapshot(),{position:stored.position,quaternion:stored.quaternion,anchor:stored.anchor,projection,span:stored.span}); const old=next.c.snapshot();next.c.resize(1400,800); assert.deepEqual(next.c.snapshot(),old);next.c.dispose(); } }); test('navigation blockers are independent and only their owners can release them',()=>{ const f=fixture();f.c.setProjection('orthographic'); const wheel=()=>f.event('wheel',{deltaX:0,deltaY:-120,deltaMode:0}); const span=()=>f.c.camera.top; try{ const initial=span();f.c.setNavigation(false);f.c.setNavigation(false,'semantic'); f.c.setNavigation(true);wheel();assert.equal(span(),initial,'planner release must not clear semantic lock'); f.c.setNavigation(false);f.c.setNavigation(true,'semantic');wheel();assert.equal(span(),initial,'semantic release must not clear planner lock'); f.c.setNavigation(true);wheel();assert.ok(span()