PlotDirector/PlotLine.Tests/Browser/context-family-layout.cjs

8 lines
2.8 KiB
JavaScript

const fs=require('node:fs'),vm=require('node:vm'),assert=require('node:assert/strict');const sandbox={window:{}};vm.runInNewContext(fs.readFileSync('PlotLine/wwwroot/js/context-relationship-layout.js','utf8'),sandbox);const engine=sandbox.window.PlotDirectorContextRelationshipLayout;
const nodes=Array.from({length:15},(_,i)=>({id:i+1,name:'Person '+(i+1),birthDate:i===6?'2000-01-01':i===7?'2002-01-01':null}));let seq=0;const links=[];function add(a,b,type,reciprocal=false){links.push({id:++seq,a,b,type,reciprocal});return seq;}
add(1,2,'Spouse',true);add(1,3,'Parent');add(2,3,'Parent');add(3,4,'Spouse',true);add(3,5,'Parent');add(4,5,'Parent');add(5,6,'Spouse',true);add(5,10,'Former spouse',true);add(5,7,'Biological Parent');add(6,7,'Biological Parent');add(5,8,'Parent');add(6,8,'Parent');add(5,9,'Parent');add(10,9,'Parent');add(11,7,'Adoptive Parent');add(12,7,'Step Parent');add(3,15,'Child');add(1,14,'Grandparent');add(13,14,'Cousin');
const layout=engine.family(nodes,links),p=layout.positions;assert(p[1].y<p[3].y&&p[3].y<p[5].y&&p[5].y<p[7].y);assert.equal(p[5].y,p[6].y);assert.equal(p[5].y,p[10].y);assert(p[7].x<p[8].x);assert(layout.junctions.length>0);assert(layout.routes.some(r=>r.kind==='adoptive'));assert(layout.routes.some(r=>r.kind==='step'));
// Orthogonal connectors may cross with a visual bridge, but must not enter an unrelated card.
for(const route of layout.routes){const edge=links.find(l=>l.id===route.id);let cursor={x:0,y:0};const parts=[...route.d.matchAll(/([MHV])\s*(-?[\d.]+)(?:\s+(-?[\d.]+))?/g)];for(const part of parts){const next=part[1]==='M'?{x:+part[2],y:+part[3]}:part[1]==='H'?{x:+part[2],y:cursor.y}:{x:cursor.x,y:+part[2]};if(part[1]!=='M')for(const n of nodes){if(n.id===edge.a||n.id===edge.b)continue;const rect=p[n.id],inside=cursor.x===next.x?cursor.x>rect.x+1&&cursor.x<rect.x+179&&Math.max(Math.min(cursor.y,next.y),rect.y+1)<Math.min(Math.max(cursor.y,next.y),rect.y+65):cursor.y>rect.y+1&&cursor.y<rect.y+65&&Math.max(Math.min(cursor.x,next.x),rect.x+1)<Math.min(Math.max(cursor.x,next.x),rect.x+179);assert(!inside,`edge ${edge.id} crosses unrelated card ${n.id}`);}cursor=next;}}
assert.equal(JSON.stringify(engine.family(nodes,links).positions),JSON.stringify(layout.positions));const lone=engine.family([{id:1,name:'One known parent'},{id:2,name:'Child'}],[{id:1,a:1,b:2,type:'Parent'}]);assert.equal(lone.junctions.length,0);const coParents=engine.family([{id:1},{id:2},{id:3}],[{id:1,a:1,b:3,type:'Biological Parent'},{id:2,a:2,b:3,type:'Adoptive Parent'}]);assert.equal(coParents.junctions.length,0);console.log('PASS genealogical layout: generations, recorded unions, exact half-sibling parentage, biological/adoptive/step styles, birth order, deterministic positions, single/co-parent records create no inferred union, orthogonal routes avoid unrelated cards.');