import { describe, expect, it } from 'bun:test' import { applyAutoLayout, computeAutoLayoutPositions, hasCrampedOrBackwardsLayout, } from '../src/admin/pages/advisorGraphLayout' describe('advisorGraphLayout', () => { it('lays out branched location resolution graph strictly left-to-right with bypass lane', () => { const nodes = [ { id: 'l0', position: { x: 40, y: 140 } }, { id: 'l1', position: { x: 320, y: 60 } }, { id: 'l2', position: { x: 320, y: 250 } }, { id: 'l3', position: { x: 640, y: 140 } }, { id: 'l4', position: { x: 940, y: 140 } }, ] const edges = [ { source: 'l0', target: 'l1' }, { source: 'l1', target: 'l3' }, { source: 'l1', target: 'l2' }, { source: 'l2', target: 'l3' }, { source: 'l3', target: 'l4' }, ] expect(hasCrampedOrBackwardsLayout(nodes, edges)).toBe(true) const next = computeAutoLayoutPositions(nodes, edges) expect(next.l0.x).toBeLessThan(next.l1.x) expect(next.l1.x).toBeLessThan(next.l2.x) expect(next.l2.x).toBeLessThan(next.l3.x) expect(next.l3.x).toBeLessThan(next.l4.x) expect(next.l2.y).toBeGreaterThan(next.l1.y) const arranged = applyAutoLayout({ nodes, edges }) expect(hasCrampedOrBackwardsLayout(arranged.nodes, arranged.edges)).toBe(false) }) it('centers parallel sibling branches vertically and avoids overlap', () => { const nodes = [ { id: 'trigger' }, { id: 'enrich' }, { id: 'analyze' }, { id: 'output' }, ] const edges = [ { source: 'trigger', target: 'enrich' }, { source: 'trigger', target: 'analyze' }, { source: 'enrich', target: 'output' }, { source: 'analyze', target: 'output' }, ] const pos = computeAutoLayoutPositions(nodes, edges) expect(pos.enrich.x).toBe(pos.analyze.x) expect(Math.abs(pos.enrich.y - pos.analyze.y)).toBeGreaterThanOrEqual(200) expect(pos.trigger.x).toBeLessThan(pos.enrich.x) expect(pos.enrich.x).toBeLessThan(pos.output.x) }) it('handles disconnected nodes and cycles without infinite loops', () => { const nodes = [{ id: 'a' }, { id: 'b' }, { id: 'orphan' }] const edges = [ { source: 'a', target: 'b' }, { source: 'b', target: 'a' }, { source: 'orphan', target: 'orphan' }, ] const pos = computeAutoLayoutPositions(nodes, edges) expect(Object.keys(pos)).toHaveLength(3) expect(Number.isFinite(pos.a.x) && Number.isFinite(pos.a.y)).toBe(true) expect(Number.isFinite(pos.b.x) && Number.isFinite(pos.b.y)).toBe(true) expect(Number.isFinite(pos.orphan.x) && Number.isFinite(pos.orphan.y)).toBe(true) }) })