import { test } from 'node:test'; import assert from 'node:assert/strict'; import { BoxGeometry } from 'three'; import { collider, checkAxis, chooseAxis, checkFiniteAxis } from './lib/assembly-axis-planner.mjs'; const box = (id, size, position) => collider(id, new BoxGeometry(...size).translate(...position)); test('a part fits down an open pocket while the side route hits the wall', () => { const moving = [box('part',[0.8,0.3,0.8],[0,0.25,0])]; const obstacles = [box('floor',[2,0.2,2],[0,0,0]), box('right',[0.2,1,2],[0.7,0.6,0]), box('left',[0.2,1,2],[-0.7,0.6,0])]; assert.equal(checkAxis(moving,obstacles,[1,0,0]).status,'blocked-or-touching'); assert.equal(chooseAxis(moving,obstacles).selected.name,'+Y'); }); test('conservative advancement cannot jump over a very thin obstruction', () => { const moving = [box('part',[0.001,0.001,0.001],[0,0,0])]; const obstacles = [box('foil',[0.000001,1,1],[0.02,0,0])]; assert.equal(checkAxis(moving,obstacles,[1,0,0]).status,'blocked-or-touching'); }); test('closed enclosure does not produce a fake valid axis or contained start', () => { const moving=[box('part',[0.1,0.1,0.1],[0,0,0])], walls=[]; for (let axis=0;axis<3;axis++) for (const sign of [-1,1]) { const size=[1,1,1],p=[0,0,0];size[axis]=0.1;p[axis]=sign*0.5; walls.push(box(`wall${axis}${sign}`,size,p)); } assert.equal(chooseAxis(moving,walls).selected,null); }); test('insufficient query budget reports review, never a collision-free result', () => { const moving=[box('part',[0.1,0.1,0.1],[0,0,0])], obstacles=[box('wall',[1,0.1,1],[0,-0.2,0])]; const result=checkAxis(moving,obstacles,[0,1,0],{maxIterations:0}); assert.equal(result.status,'needs-review'); }); test('part-local axes work when the complete assembly is tilted in the viewer', () => { const angle=0.65, make=(id,s,p)=>collider(id,new BoxGeometry(...s).translate(...p).rotateZ(angle)); const moving=[make('part',[0.8,0.3,0.8],[0,0.25,0])]; const obstacles=[make('floor',[2,0.2,2],[0,0,0]),make('right',[0.2,1,2],[0.7,0.6,0]),make('left',[0.2,1,2],[-0.7,0.6,0])]; const result=checkAxis(moving,obstacles,[-Math.sin(angle),Math.cos(angle),0]); assert.equal(result.status,'clear-to-seating'); }); test('touching subcomponents move as a rigid group without colliding with each other', () => { const moving=[box('a',[0.1,0.1,0.1],[0,0.15,0]),box('b',[0.1,0.1,0.1],[0.1,0.15,0])]; const obstacles=[box('floor',[1,0.2,1],[0,0,0])]; const result=checkAxis(moving,obstacles,[0,1,0]); assert.equal(result.status,'clear-to-seating'); }); test('finite entry does not start beyond an unrelated opposite limb', () => { const moving=[box('screw',[.002,.002,.004],[0,0,0])]; const obstacles=[box('opposite-leg',[.03,.03,.03],[0,0,.1])]; assert.equal(checkAxis(moving,obstacles,[0,0,1]).status,'blocked-or-touching'); const result=checkFiniteAxis(moving,obstacles,[0,0,1],.02); assert.equal(result.status,'foundation'); assert.equal(result.travel,.02); assert.equal(result.relevantObstacles,0); }); test('finite entry still detects a thin barrier and a contained start', () => { const moving=[box('screw',[.002,.002,.004],[0,0,0])]; const foil=[box('foil',[.03,.03,.000001],[0,0,.01])]; assert.equal(checkFiniteAxis(moving,foil,[0,0,1],.02).status,'blocked-or-touching'); const solid=[box('enclosure',[.1,.1,.1],[0,0,0])]; assert.equal(checkFiniteAxis(moving,solid,[0,0,1],.02).status,'blocked-or-touching'); }); test('clear short paths reach the seating endpoint without a floating-point stall', () => { const moving=[box('part',[.002,.002,.002],[0,.015,0])]; const obstacles=[box('base',[.1,.002,.1],[0,0,0])]; for(const minimumTravel of [.003,.035,.055,.07,.137]) { const result=checkAxis(moving,obstacles,[0,1,0],{minimumTravel}); assert.equal(result.status,'clear-to-seating'); assert.ok(result.uncheckedFinalDistance<=.00015); } });