import assert from 'node:assert/strict'; import test from 'node:test'; import {preferDockSizes,dockMinimum,constrainDockTree,balancedDockTree,decodeDockTree,dockIds,dockPosition,dockRects,moveDockPanel,pruneDockTree,reconcileDockTree,resizeDockSplit} from '../packages/ui-core/dist/dockTree.js'; const bounds={left:0,top:0,width:1200,height:800}; const initial=balancedDockTree(['a','b','c']); const rects=tree=>dockRects(tree,bounds); test('three asymmetric panes: center exchanges contents without changing slots',()=>{ const before=rects(initial),next=rects(moveDockPanel(initial,{activeId:'c',overId:'a',position:'center'})); assert.deepEqual(next.c,before.a);assert.deepEqual(next.a,before.c);assert.deepEqual(next.b,before.b); }); test('left to top/bottom edge of right pane gives one left and two right',()=>{ for(const position of ['top','bottom']){ const next=moveDockPanel(initial,{activeId:'a',overId:'c',position}),r=rects(next); assert.deepEqual(r.b,{left:0,top:0,width:600,height:800}); assert.deepEqual(r.a,{left:600,top:position==='top'?0:400,width:600,height:400}); assert.deepEqual(r.c,{left:600,top:position==='top'?400:0,width:600,height:400}); } }); test('right window moves to any edge of a left pane; left/right create a middle column',()=>{ for(const position of ['left','right','top','bottom']){ const next=moveDockPanel(initial,{activeId:'c',overId:'a',position}),r=rects(next); assert.deepEqual(r.b,{left:0,top:400,width:1200,height:400}); assert.equal(position==='left'?r.c.leftr.a.left:position==='top'?r.c.topr.a.top,true); } }); test('every source/destination/edge for 2..12 panes retains each identity and tiles the whole area',()=>{ for(let n=2;n<=12;n++){ const ids=Array.from({length:n},(_,i)=>String(i));let tree=balancedDockTree(ids); for(const activeId of ids)for(const overId of ids)for(const position of ['center','left','right','top','bottom']){ tree=moveDockPanel(tree,{activeId,overId,position}); assert.deepEqual([...dockIds(tree)].sort(),[...ids].sort()); const panels=Object.values(rects(tree)); assert.ok(Math.abs(panels.reduce((total,r)=>total+r.width*r.height,0)-bounds.width*bounds.height)<1e-6); for(let i=0;i{ const r={left:400,top:100,width:800,height:600}; assert.equal(dockPosition(r,405,400),'left');assert.equal(dockPosition(r,1195,400),'right'); assert.equal(dockPosition(r,800,105),'top');assert.equal(dockPosition(r,800,695),'bottom'); assert.equal(dockPosition(r,800,400),'center');assert.equal(dockPosition(r,399,400),null); assert.equal(moveDockPanel(initial,{activeId:'a',overId:'a',position:'left'}),initial); assert.equal(moveDockPanel(initial,{activeId:'missing',overId:'c',position:'center'}),initial); }); test('resize, focus, persistence and new/missing sources preserve identity',()=>{ const changed=resizeDockSplit(initial,'0',63); assert.equal(changed.first.ratio,63);assert.equal(initial.first.ratio,50); assert.deepEqual(decodeDockTree(JSON.parse(JSON.stringify(changed))),changed); assert.deepEqual(pruneDockTree(changed,['c']),{id:'c'}); const restored=reconcileDockTree(pruneDockTree(changed,['a','c']),['a','b','c','d']); assert.deepEqual(dockIds(restored).sort(),['a','b','c','d']); assert.deepEqual(decodeDockTree({axis:'x',ratio:50,first:{id:'a'},second:{id:'a'}}),{id:'a'}); assert.equal(decodeDockTree({axis:'x',ratio:Infinity}),null); }); test('recursive minimums protect all columns and rows after extreme resize',()=>{ const t={axis:'x',ratio:99,first:{id:'scene'},second:{axis:'y',ratio:1,first:{id:'telemetry'},second:{id:'camera'}}}; for(let n=1;n<=8;n++){ const sizes={telemetry:{width:144+88*n,height:280}},minimum=dockMinimum(t,sizes); for(const requested of [{width:1200,height:800},{width:100,height:100}]){ const b={left:0,top:0,width:Math.max(requested.width,minimum.width),height:Math.max(requested.height,minimum.height)}; const c=constrainDockTree(t,b,sizes),r=dockRects(c,b); assert.ok(r.telemetry.width+1e-8>=sizes.telemetry.width);assert.ok(r.telemetry.height+1e-8>=280); assert.ok(r.scene.width+1e-8>=160);assert.ok(r.camera.height+1e-8>=120); assert.deepEqual(constrainDockTree(c,b,sizes),c); } } }); test('new compact panel uses its preferred size without changing saved ratios',()=>{ const t={axis:'x',ratio:65,first:{id:'map'},second:{axis:'y',ratio:40,first:{id:'telemetry'},second:{id:'camera'}}}; const sizes={telemetry:{width:320,height:280}}; const initial=constrainDockTree(preferDockSizes(t,bounds,sizes),bounds,sizes),r=rects(initial); assert.ok(Math.abs(r.telemetry.width-320)<1e-8);assert.ok(Math.abs(r.telemetry.height-280)<1e-8); assert.deepEqual(preferDockSizes(t,bounds,{}),t); });