@statelyai/flow 0.13.4 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +5 -1
  2. package/dist/esm/index.d.ts +13 -2
  3. package/dist/esm/index.d.ts.map +1 -0
  4. package/dist/esm/index.js +39 -14
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/esm/layout/dagre.d.ts +2 -1
  7. package/dist/esm/layout/dagre.d.ts.map +1 -0
  8. package/dist/esm/layout/dagre.js +2 -0
  9. package/dist/esm/layout/dagre.js.map +1 -0
  10. package/dist/esm/layout/elk.d.ts +2 -1
  11. package/dist/esm/layout/elk.d.ts.map +1 -0
  12. package/dist/esm/layout/elk.js +2 -0
  13. package/dist/esm/layout/elk.js.map +1 -0
  14. package/dist/esm/layout/stately.d.ts +2 -1
  15. package/dist/esm/layout/stately.d.ts.map +1 -0
  16. package/dist/esm/layout/stately.js +2 -0
  17. package/dist/esm/layout/stately.js.map +1 -0
  18. package/dist/esm/shared.d.ts +2 -1
  19. package/dist/esm/shared.d.ts.map +1 -0
  20. package/dist/esm/shared.js +2 -0
  21. package/dist/esm/shared.js.map +1 -0
  22. package/package.json +4 -3
  23. package/src/__fixtures__/nested.ts +106 -0
  24. package/src/appearances.test.ts +428 -0
  25. package/src/appearances.ts +144 -0
  26. package/src/authoritative-route.ts +14 -0
  27. package/src/changeSet.test.ts +132 -0
  28. package/src/changeSet.ts +337 -0
  29. package/src/config.ts +41 -0
  30. package/src/configure.test.ts +212 -0
  31. package/src/connection-lifetime.ts +1 -0
  32. package/src/connectionPreview.test.ts +102 -0
  33. package/src/connectionPreview.ts +82 -0
  34. package/src/content.ts +203 -0
  35. package/src/createFlow.fit-frame.test.ts +256 -0
  36. package/src/createFlow.modern.test.ts +743 -0
  37. package/src/createFlow.test.ts +632 -0
  38. package/src/createFlow.ts +3463 -0
  39. package/src/createFlow.view.test.ts +212 -0
  40. package/src/createFlow.viewport-controls.test.ts +221 -0
  41. package/src/edge-geometry.ts +318 -0
  42. package/src/engine.cache.test.ts +728 -0
  43. package/src/engine.hierarchy.test.ts +1023 -0
  44. package/src/engine.test.ts +1965 -0
  45. package/src/engine.ts +3385 -0
  46. package/src/entity-changes.test.ts +309 -0
  47. package/src/entity-changes.ts +395 -0
  48. package/src/examples/flow-based-math.test.ts +25 -0
  49. package/src/examples/flow-based-math.ts +210 -0
  50. package/src/external-edit.test.ts +337 -0
  51. package/src/external-edit.ts +358 -0
  52. package/src/factories.test.ts +104 -0
  53. package/src/factories.ts +70 -0
  54. package/src/geometry.ts +13 -0
  55. package/src/graph-index.test.ts +56 -0
  56. package/src/graph-index.ts +194 -0
  57. package/src/hierarchy.test.ts +175 -0
  58. package/src/hierarchy.ts +330 -0
  59. package/src/highlights.test.ts +130 -0
  60. package/src/index.ts +469 -0
  61. package/src/input/index.ts +615 -0
  62. package/src/input/input.test.ts +2577 -0
  63. package/src/input/keymap.ts +111 -0
  64. package/src/input/modes.ts +1715 -0
  65. package/src/input/reparent.test.ts +214 -0
  66. package/src/layout/dagre.ts +138 -0
  67. package/src/layout/elk.ts +1382 -0
  68. package/src/layout/index.ts +12 -0
  69. package/src/layout/layout.test.ts +1248 -0
  70. package/src/layout/shared.ts +260 -0
  71. package/src/layout/stately-globals.d.ts +9 -0
  72. package/src/layout/stately.test.ts +246 -0
  73. package/src/layout/stately.ts +528 -0
  74. package/src/layout-animation.test.ts +223 -0
  75. package/src/layout-contract.test.ts +414 -0
  76. package/src/marker.test.ts +23 -0
  77. package/src/marker.ts +23 -0
  78. package/src/measurement-anchor.test.ts +232 -0
  79. package/src/minimap.test.ts +96 -0
  80. package/src/minimap.ts +137 -0
  81. package/src/nested-fixture.test.ts +178 -0
  82. package/src/port-measurement.test.ts +27 -0
  83. package/src/port-measurements.test.ts +187 -0
  84. package/src/profile.ts +124 -0
  85. package/src/projections.test.ts +103 -0
  86. package/src/projections.ts +295 -0
  87. package/src/render-invalidation.test.ts +62 -0
  88. package/src/render-invalidation.ts +55 -0
  89. package/src/renderer-geometry.test.ts +356 -0
  90. package/src/renderer-transactions.test.ts +234 -0
  91. package/src/routing/avoid.ts +417 -0
  92. package/src/routing/index.test.ts +361 -0
  93. package/src/routing/index.ts +366 -0
  94. package/src/routing/readTracking.test.ts +103 -0
  95. package/src/routing/readTracking.ts +233 -0
  96. package/src/selection-commands.test.ts +355 -0
  97. package/src/serialize.test.ts +167 -0
  98. package/src/serialize.ts +254 -0
  99. package/src/shapes/index.test.ts +45 -0
  100. package/src/shapes/index.ts +142 -0
  101. package/src/snap.test.ts +123 -0
  102. package/src/snap.ts +395 -0
  103. package/src/spatial.test.ts +676 -0
  104. package/src/spatial.ts +457 -0
  105. package/src/store/index.ts +4121 -0
  106. package/src/store.test.ts +2903 -0
  107. package/src/toolbar.test.ts +31 -0
  108. package/src/toolbar.ts +62 -0
  109. package/src/types.test.ts +15 -0
  110. package/src/types.ts +487 -0
  111. package/src/validation.ts +558 -0
  112. package/src/view.ts +172 -0
  113. package/src/viewport/animate.ts +285 -0
  114. package/src/viewport/index.ts +296 -0
  115. package/src/viewport.test.ts +569 -0
@@ -0,0 +1,1248 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import {
3
+ createEdge,
4
+ createGraph,
5
+ createNode,
6
+ createPort,
7
+ deriveGroupGeometry,
8
+ type FlowGraph,
9
+ type LayoutResult,
10
+ type Rect,
11
+ } from '@statelyai/flow';
12
+ import ELK from 'elkjs/lib/elk.bundled.js';
13
+ import type { ELK as ElkInstance, ElkNode } from 'elkjs/lib/elk-api';
14
+ import { dagreLayout } from './dagre';
15
+ import { elkLayout, toElkGraph } from './elk';
16
+ import { graphForLayoutScope } from './shared';
17
+
18
+ function fixture(): FlowGraph {
19
+ return createGraph({
20
+ direction: 'down',
21
+ nodes: [
22
+ createNode({
23
+ id: 'group',
24
+ x: 0,
25
+ y: 0,
26
+ width: 260,
27
+ height: 160,
28
+ group: { type: 'fixed', contentSize: { width: 120, height: 40 } },
29
+ }),
30
+ createNode({
31
+ id: 'a',
32
+ parentId: 'group',
33
+ x: 20,
34
+ y: 60,
35
+ width: 120,
36
+ height: 50,
37
+ ports: [createPort({ name: 'out', side: 'bottom', direction: 'out' })],
38
+ }),
39
+ createNode({
40
+ id: 'b',
41
+ parentId: 'group',
42
+ x: 180,
43
+ y: 60,
44
+ width: 120,
45
+ height: 50,
46
+ ports: [createPort({ name: 'in', side: 'top', direction: 'in' })],
47
+ }),
48
+ createNode({ id: 'c', x: 360, y: 0, width: 120, height: 50 }),
49
+ ],
50
+ edges: [
51
+ createEdge({
52
+ id: 'ab',
53
+ sourceId: 'a',
54
+ targetId: 'b',
55
+ sourcePort: 'out',
56
+ targetPort: 'in',
57
+ label: { x: 0, y: 0, width: 60, height: 20 },
58
+ }),
59
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
60
+ ],
61
+ });
62
+ }
63
+
64
+ type PositionedRect = Rect & { id: string };
65
+
66
+ function positionedRects(
67
+ graph: FlowGraph,
68
+ positions: ReadonlyMap<string, { x: number; y: number }>,
69
+ ): PositionedRect[] {
70
+ return graph.nodes
71
+ .filter((node) => positions.has(node.id))
72
+ .map((node) => ({
73
+ id: node.id,
74
+ x: positions.get(node.id)!.x,
75
+ y: positions.get(node.id)!.y,
76
+ width: node.width,
77
+ height: node.height,
78
+ }));
79
+ }
80
+
81
+ function hasIntersections(graph: FlowGraph, rects: PositionedRect[]): boolean {
82
+ for (let i = 0; i < rects.length; i++) {
83
+ for (let j = i + 1; j < rects.length; j++) {
84
+ const a = rects[i];
85
+ const b = rects[j];
86
+ if (isAncestor(graph, a.id, b.id) || isAncestor(graph, b.id, a.id)) {
87
+ continue;
88
+ }
89
+ if (
90
+ a.x < b.x + b.width &&
91
+ a.x + a.width > b.x &&
92
+ a.y < b.y + b.height &&
93
+ a.y + a.height > b.y
94
+ ) {
95
+ return true;
96
+ }
97
+ }
98
+ }
99
+ return false;
100
+ }
101
+
102
+ function isAncestor(graph: FlowGraph, possibleAncestorId: string, nodeId: string): boolean {
103
+ const nodesById = new Map(graph.nodes.map((node) => [node.id, node]));
104
+ let parentId = nodesById.get(nodeId)?.parentId;
105
+ while (parentId) {
106
+ if (parentId === possibleAncestorId) return true;
107
+ parentId = nodesById.get(parentId)?.parentId;
108
+ }
109
+ return false;
110
+ }
111
+
112
+ function asLayoutResult(result: LayoutResult | Map<string, { x: number; y: number }>): LayoutResult {
113
+ return result instanceof Map ? { positions: result } : result;
114
+ }
115
+
116
+ /** True when child rect is fully inside parent rect (with a small epsilon). */
117
+ function contains(parent: PositionedRect, child: PositionedRect, epsilon = 0.5): boolean {
118
+ return (
119
+ child.x >= parent.x - epsilon &&
120
+ child.y >= parent.y - epsilon &&
121
+ child.x + child.width <= parent.x + parent.width + epsilon &&
122
+ child.y + child.height <= parent.y + parent.height + epsilon
123
+ );
124
+ }
125
+
126
+ describe('flow layout adapters', () => {
127
+ it('ELK returns overlap-free positions and edge label positions', async () => {
128
+ const graph = fixture();
129
+ const result = asLayoutResult(await elkLayout({ nodeSpacing: 80, rankSpacing: 100 })(graph));
130
+
131
+ expect(result.positions.size).toBe(graph.nodes.length);
132
+ expect(hasIntersections(graph, positionedRects(graph, result.positions))).toBe(false);
133
+ expect(result.edgeLabelPositions?.has('ab')).toBe(true);
134
+ expect(result.edgeLabelBounds?.get('ab')).toMatchObject({
135
+ width: 60,
136
+ height: 20,
137
+ });
138
+ expect(result.edgeRoutes?.has('ab')).toBe(true);
139
+ expect(result.source).toBe('elk');
140
+ expect(result.metadata).toMatchObject({ adapter: 'elk' });
141
+ });
142
+
143
+ it('preserves ELK label placement for flat graphs', async () => {
144
+ const graph = createGraph({
145
+ direction: 'right',
146
+ nodes: [
147
+ createNode({ id: 'a', width: 120, height: 60 }),
148
+ createNode({ id: 'b', width: 120, height: 60 }),
149
+ ],
150
+ edges: [
151
+ createEdge({
152
+ id: 'ab',
153
+ sourceId: 'a',
154
+ targetId: 'b',
155
+ label: { x: 0, y: 0, width: 80, height: 24 },
156
+ }),
157
+ ],
158
+ });
159
+ const raw = await new ELK().layout(toElkGraph(graph, { direction: 'right' }));
160
+ const rawLabel = raw.edges?.find((edge) => edge.id === 'ab')?.labels?.[0];
161
+ expect(rawLabel).toBeDefined();
162
+
163
+ const result = asLayoutResult(await elkLayout({ direction: 'right' })(graph));
164
+ expect(result.edgeLabelBounds?.get('ab')).toMatchObject({
165
+ x: rawLabel!.x,
166
+ y: rawLabel!.y,
167
+ width: rawLabel!.width,
168
+ height: rawLabel!.height,
169
+ });
170
+ });
171
+
172
+ it('does not push a self-edge label off its endpoint in a compound graph', async () => {
173
+ const graph = createGraph({
174
+ nodes: [
175
+ createNode({ id: 'self', width: 120, height: 60 }),
176
+ createNode({
177
+ id: 'group',
178
+ width: 240,
179
+ height: 160,
180
+ group: { type: 'fixed' },
181
+ }),
182
+ createNode({ id: 'child', parentId: 'group', width: 80, height: 40 }),
183
+ ],
184
+ edges: [
185
+ createEdge({
186
+ id: 'loop',
187
+ sourceId: 'self',
188
+ targetId: 'self',
189
+ label: { x: 0, y: 0, width: 80, height: 24 },
190
+ }),
191
+ ],
192
+ });
193
+ const raw = await new ELK().layout(toElkGraph(graph, {}));
194
+ const rawLabel = raw.edges?.find((edge) => edge.id === 'loop')?.labels?.[0];
195
+ expect(rawLabel).toBeDefined();
196
+
197
+ const result = asLayoutResult(await elkLayout()(graph));
198
+ expect(result.edgeLabelBounds?.get('loop')).toMatchObject({
199
+ x: rawLabel!.x,
200
+ y: rawLabel!.y,
201
+ });
202
+ });
203
+
204
+ it('ELK scope only applies listed node positions', async () => {
205
+ const graph = fixture();
206
+ const result = asLayoutResult(await elkLayout({ scope: { ids: ['a'] } })(graph));
207
+
208
+ expect([...result.positions.keys()]).toEqual(['a']);
209
+ });
210
+
211
+ it('ELK scope excludes out-of-scope nodes and edges before layout', () => {
212
+ const elkGraph = toElkGraph(
213
+ graphForLayoutScope(fixture(), { ids: ['group', 'a'] }),
214
+ { scope: { ids: ['group', 'a'] } },
215
+ );
216
+
217
+ expect(elkGraph.children?.map((node) => node.id)).toEqual(['group']);
218
+ expect(elkGraph.children?.[0]?.children?.map((node) => node.id)).toEqual([
219
+ 'a',
220
+ ]);
221
+ expect(elkGraph.edges ?? []).toEqual([]);
222
+ expect(elkGraph.children?.[0]?.edges ?? []).toEqual([]);
223
+ });
224
+
225
+ it('ELK scope rejects unknown node ids before invoking ELK', async () => {
226
+ await expect(
227
+ elkLayout({ scope: { ids: ['missing'] } })(fixture()),
228
+ ).rejects.toThrow('Layout scope contains unknown node ids: missing');
229
+ });
230
+
231
+ it('ELK sizes a compound group to contain its children (header cleared, no sibling overlap)', async () => {
232
+ const graph = createGraph({
233
+ direction: 'down',
234
+ nodes: [
235
+ createNode({
236
+ id: 'group',
237
+ x: 0,
238
+ y: 0,
239
+ width: 300,
240
+ height: 220,
241
+ // Fixed group with a 120x40 header on top; children must clear it.
242
+ group: { type: 'fixed', content: 'top', contentSize: { width: 120, height: 40 } },
243
+ }),
244
+ createNode({ id: 'a', parentId: 'group', x: 20, y: 60, width: 120, height: 50 }),
245
+ createNode({ id: 'b', parentId: 'group', x: 20, y: 130, width: 120, height: 50 }),
246
+ createNode({ id: 'd', parentId: 'group', x: 20, y: 200, width: 120, height: 50 }),
247
+ createNode({ id: 'ext', x: 400, y: 0, width: 120, height: 50 }),
248
+ ],
249
+ edges: [
250
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
251
+ createEdge({ id: 'bd', sourceId: 'b', targetId: 'd' }),
252
+ createEdge({ id: 'd-ext', sourceId: 'd', targetId: 'ext' }),
253
+ ],
254
+ });
255
+
256
+ // Run raw elkjs to read ELK's COMPUTED compound size — under
257
+ // hierarchyHandling=INCLUDE_CHILDREN ELK owns the compound's size and
258
+ // grows it around its children, so containment must be checked against
259
+ // that computed rect (LayoutResult only carries positions).
260
+ const laidOut = await new ELK().layout(toElkGraph(graph, { nodeSpacing: 60, rankSpacing: 90 }));
261
+ const groupNode = laidOut.children!.find((n) => n.id === 'group')!;
262
+ const parent: PositionedRect = {
263
+ id: 'group',
264
+ x: 0,
265
+ y: 0,
266
+ width: groupNode.width!,
267
+ height: groupNode.height!,
268
+ };
269
+ // Children positions are parent-relative in ELK's output.
270
+ const childRects: PositionedRect[] = groupNode.children!.map((c) => ({
271
+ id: c.id,
272
+ x: c.x!,
273
+ y: c.y!,
274
+ width: c.width!,
275
+ height: c.height!,
276
+ }));
277
+
278
+ // (a) Every child's rect is fully inside the (ELK-computed) parent box,
279
+ // and clears the 40px header (content block on top).
280
+ for (const child of childRects) {
281
+ expect(contains(parent, child)).toBe(true);
282
+ expect(child.y).toBeGreaterThanOrEqual(40 - 0.5);
283
+ }
284
+
285
+ // (b) No sibling overlaps among the children.
286
+ const childGraph = createGraph({
287
+ direction: 'down',
288
+ nodes: childRects.map((r) => createNode({ id: r.id, x: r.x, y: r.y, width: r.width, height: r.height })),
289
+ edges: [],
290
+ });
291
+ expect(hasIntersections(childGraph, childRects)).toBe(false);
292
+
293
+ // The whole-graph layout still returns a position for every node.
294
+ const result = asLayoutResult(await elkLayout({ nodeSpacing: 60, rankSpacing: 90 })(graph));
295
+ expect(result.positions.size).toBe(graph.nodes.length);
296
+ expect(result.nodeSizes?.get('group')).toEqual({
297
+ width: groupNode.width,
298
+ height: groupNode.height,
299
+ });
300
+ expect(result.nodeBounds?.get('group')).toMatchObject({
301
+ width: groupNode.width,
302
+ height: groupNode.height,
303
+ });
304
+ });
305
+
306
+ it('ELK keeps a compound transition target separate from root siblings', async () => {
307
+ const graph = createGraph({
308
+ direction: 'right',
309
+ nodes: [
310
+ createNode({ id: 'initial', x: 0, y: 0, width: 106, height: 60 }),
311
+ createNode({ id: 'another', x: 0, y: 0, width: 134, height: 60 }),
312
+ createNode({
313
+ id: 'parent',
314
+ x: 0,
315
+ y: 0,
316
+ width: 119,
317
+ height: 60,
318
+ group: { type: 'derived', content: 'top', padding: 28 },
319
+ }),
320
+ createNode({
321
+ id: 'child',
322
+ parentId: 'parent',
323
+ x: 0,
324
+ y: 0,
325
+ width: 168,
326
+ height: 113,
327
+ }),
328
+ createNode({
329
+ id: 'another-child',
330
+ parentId: 'parent',
331
+ x: 0,
332
+ y: 0,
333
+ width: 194,
334
+ height: 111,
335
+ }),
336
+ ],
337
+ edges: [
338
+ createEdge({
339
+ id: 'initial-next',
340
+ sourceId: 'initial',
341
+ targetId: 'another',
342
+ label: { x: 0, y: 0, width: 66, height: 40 },
343
+ }),
344
+ createEdge({
345
+ id: 'another-next',
346
+ sourceId: 'another',
347
+ targetId: 'parent',
348
+ label: { x: 0, y: 0, width: 66, height: 40 },
349
+ }),
350
+ createEdge({
351
+ id: 'child-next',
352
+ sourceId: 'child',
353
+ targetId: 'another-child',
354
+ label: { x: 0, y: 0, width: 66, height: 40 },
355
+ }),
356
+ ],
357
+ });
358
+
359
+ const result = asLayoutResult(
360
+ await elkLayout({
361
+ label: {
362
+ 'elk.edgeLabels.inline': true,
363
+ 'elk.edgeLabels.placement': 'CENTER',
364
+ },
365
+ })(graph),
366
+ );
367
+ const containedBounds = [
368
+ result.nodeBounds!.get('child')!,
369
+ result.nodeBounds!.get('another-child')!,
370
+ result.edgeLabelBounds!.get('child-next')!,
371
+ ];
372
+ expect(result.edgeLabelBounds!.get('child-next')).not.toMatchObject({
373
+ x: result.nodeBounds!.get('parent')!.x,
374
+ y: result.nodeBounds!.get('parent')!.y,
375
+ });
376
+ const left = Math.min(...containedBounds.map((bounds) => bounds.x));
377
+ const top = Math.min(...containedBounds.map((bounds) => bounds.y));
378
+ const right = Math.max(
379
+ ...containedBounds.map((bounds) => bounds.x + bounds.width),
380
+ );
381
+ const bottom = Math.max(
382
+ ...containedBounds.map((bounds) => bounds.y + bounds.height),
383
+ );
384
+ const parent = deriveGroupGeometry(
385
+ { width: 119, height: 60 },
386
+ { x: left, y: top, width: right - left, height: bottom - top },
387
+ { type: 'derived', content: 'top', padding: 28 },
388
+ ).bounds;
389
+ const another = result.nodeBounds!.get('another')!;
390
+ const overlaps =
391
+ parent.x < another.x + another.width &&
392
+ parent.x + parent.width > another.x &&
393
+ parent.y < another.y + another.height &&
394
+ parent.y + parent.height > another.y;
395
+
396
+ expect(overlaps).toBe(false);
397
+ });
398
+
399
+ it('ELK merges structural/factory scope as an induced graph', async () => {
400
+ const result = asLayoutResult(
401
+ await elkLayout({ scope: { ids: ['a', 'b', 'c'] } })(fixture(), {
402
+ scope: { parentId: 'group', depth: 1 },
403
+ }),
404
+ );
405
+
406
+ expect([...result.positions.keys()].sort()).toEqual(['a', 'b']);
407
+ expect(result.edgeLabelBounds?.has('ab')).toBe(true);
408
+ expect(result.edgeLabelBounds?.has('bc')).not.toBe(true);
409
+ });
410
+
411
+ it('ELK omits fixed/pinned results and merges previous/run metadata', async () => {
412
+ const result = asLayoutResult(
413
+ await elkLayout({ metadata: { factory: true } })(fixture(), {
414
+ fixedEntityIds: new Set(['a']),
415
+ pinnedEntityIds: new Set(['ab']),
416
+ previousResult: {
417
+ positions: new Map(),
418
+ metadata: { previous: true },
419
+ },
420
+ source: 'viz-elk',
421
+ metadata: { run: true },
422
+ }),
423
+ );
424
+
425
+ expect(result.positions.has('a')).toBe(false);
426
+ expect(result.positions.has('b')).toBe(true);
427
+ expect(result.edgeLabelPositions?.has('ab')).not.toBe(true);
428
+ expect(result.edgeRoutes?.has('ab')).not.toBe(true);
429
+ expect(result.source).toBe('viz-elk');
430
+ expect(result.metadata).toEqual({
431
+ previous: true,
432
+ factory: true,
433
+ run: true,
434
+ adapter: 'elk',
435
+ });
436
+ });
437
+
438
+ it('ELK cancellation and unknown fixed ids fail before layout deterministically', async () => {
439
+ const signal = {
440
+ aborted: true,
441
+ addEventListener() {},
442
+ removeEventListener() {},
443
+ };
444
+ await expect(elkLayout()(fixture(), { signal })).rejects.toMatchObject({
445
+ name: 'AbortError',
446
+ });
447
+ await expect(
448
+ elkLayout()(fixture(), {
449
+ fixedEntityIds: new Set(['missing-z', 'missing-a']),
450
+ }),
451
+ ).rejects.toThrow(
452
+ 'Layout fixed/pinned ids are unknown: missing-a, missing-z',
453
+ );
454
+ await expect(
455
+ elkLayout({
456
+ getBounds: (node) => ({
457
+ x: node.x,
458
+ y: node.y,
459
+ width: Number.NaN,
460
+ height: node.height,
461
+ }),
462
+ })(fixture()),
463
+ ).rejects.toThrow(
464
+ 'Layout bounds for node "group" must be finite with non-negative size',
465
+ );
466
+ });
467
+
468
+ it('ELK nests an intra-group edge inside the group and routes it within the group rect', async () => {
469
+ const graph = createGraph({
470
+ direction: 'down',
471
+ nodes: [
472
+ createNode({
473
+ id: 'group',
474
+ x: 0,
475
+ y: 0,
476
+ width: 300,
477
+ height: 220,
478
+ group: { type: 'derived', content: 'top', padding: 20 },
479
+ }),
480
+ createNode({ id: 'a', parentId: 'group', x: 20, y: 60, width: 120, height: 50 }),
481
+ createNode({ id: 'b', parentId: 'group', x: 20, y: 130, width: 120, height: 50 }),
482
+ createNode({ id: 'c', parentId: 'group', x: 20, y: 200, width: 120, height: 50 }),
483
+ createNode({ id: 'ext', x: 400, y: 0, width: 120, height: 50 }),
484
+ ],
485
+ edges: [
486
+ // Intra-group edges (both endpoints inside `group`) — must nest in and
487
+ // route inside the group.
488
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
489
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
490
+ // Cross-boundary edge — must stay at root.
491
+ createEdge({ id: 'c-ext', sourceId: 'c', targetId: 'ext' }),
492
+ ],
493
+ });
494
+
495
+ // The ELK graph the adapter builds must nest intra-group edges in the
496
+ // group and keep the cross-boundary edge at root.
497
+ const elkGraph = toElkGraph(graph, { nodeSpacing: 60, rankSpacing: 90 });
498
+ const groupElk = elkGraph.children!.find((n) => n.id === 'group')!;
499
+ const groupEdgeIds = (groupElk.edges ?? []).map((e) => e.id).sort();
500
+ const rootEdgeIds = (elkGraph.edges ?? []).map((e) => e.id).sort();
501
+ expect(groupEdgeIds).toEqual(['ab', 'bc']);
502
+ expect(rootEdgeIds).toEqual(['c-ext']);
503
+
504
+ // Real elkjs run: the intra-group edges' routes (start/bend/end points,
505
+ // group-relative) must fall inside the ELK-computed group rect.
506
+ const laidOut = await new ELK().layout(toElkGraph(graph, { nodeSpacing: 60, rankSpacing: 90 }));
507
+ const laidGroup = laidOut.children!.find((n) => n.id === 'group')!;
508
+ const groupRect: PositionedRect = {
509
+ id: 'group',
510
+ x: 0,
511
+ y: 0,
512
+ width: laidGroup.width!,
513
+ height: laidGroup.height!,
514
+ };
515
+ const groupEdges = laidGroup.edges ?? [];
516
+ expect(groupEdges.map((e) => e.id).sort()).toEqual(['ab', 'bc']);
517
+
518
+ const epsilon = 0.5;
519
+ const inside = (p: { x: number; y: number }) =>
520
+ p.x >= groupRect.x - epsilon &&
521
+ p.x <= groupRect.x + groupRect.width + epsilon &&
522
+ p.y >= groupRect.y - epsilon &&
523
+ p.y <= groupRect.y + groupRect.height + epsilon;
524
+
525
+ let checkedPoints = 0;
526
+ for (const edge of groupEdges) {
527
+ for (const section of edge.sections ?? []) {
528
+ const points = [section.startPoint, ...(section.bendPoints ?? []), section.endPoint];
529
+ for (const p of points) {
530
+ expect(inside(p)).toBe(true);
531
+ checkedPoints++;
532
+ }
533
+ }
534
+ }
535
+ // Guard against a vacuously-passing loop: ELK must have produced routes.
536
+ expect(checkedPoints).toBeGreaterThan(0);
537
+
538
+ // No regression: the whole-graph layout still returns a position for
539
+ // every node (including the cross-boundary endpoint `ext`).
540
+ const result = asLayoutResult(await elkLayout({ nodeSpacing: 60, rankSpacing: 90 })(graph));
541
+ expect(result.positions.size).toBe(graph.nodes.length);
542
+ expect(result.positions.has('ext')).toBe(true);
543
+ expect(result.edgeRoutes?.get('c-ext')).toEqual({ waypoints: [] });
544
+ });
545
+
546
+ it('keeps root edge labels between nodes when the graph also has nested nodes', async () => {
547
+ const graph = createGraph({
548
+ direction: 'right',
549
+ nodes: [
550
+ createNode({
551
+ id: 'group',
552
+ width: 300,
553
+ height: 220,
554
+ group: { type: 'derived', content: 'top', padding: 20 },
555
+ }),
556
+ createNode({ id: 'child', parentId: 'group', width: 100, height: 50 }),
557
+ createNode({ id: 'source', width: 100, height: 50 }),
558
+ createNode({ id: 'target', width: 300, height: 50 }),
559
+ ],
560
+ edges: [
561
+ createEdge({
562
+ id: 'root-edge',
563
+ sourceId: 'source',
564
+ targetId: 'target',
565
+ label: { x: 0, y: 0, width: 80, height: 24 },
566
+ }),
567
+ ],
568
+ });
569
+
570
+ const result = asLayoutResult(await elkLayout({ direction: 'right' })(graph));
571
+ const source = result.nodeBounds!.get('source')!;
572
+ const target = result.nodeBounds!.get('target')!;
573
+ const label = result.edgeLabelBounds!.get('root-edge')!;
574
+
575
+ expect(label.x).toBeGreaterThanOrEqual(source.x + source.width);
576
+ expect(label.x + label.width).toBeLessThanOrEqual(target.x);
577
+ });
578
+
579
+ it('keeps root edge labels out of an intervening compound node', async () => {
580
+ const graph = createGraph({
581
+ direction: 'down',
582
+ nodes: [
583
+ createNode({ id: 'source', width: 120, height: 60 }),
584
+ createNode({
585
+ id: 'group',
586
+ width: 360,
587
+ height: 60,
588
+ group: { type: 'derived', content: 'top', padding: 20 },
589
+ }),
590
+ createNode({ id: 'child-a', parentId: 'group', width: 120, height: 60 }),
591
+ createNode({ id: 'child-b', parentId: 'group', width: 180, height: 60 }),
592
+ createNode({ id: 'target', width: 140, height: 60 }),
593
+ ],
594
+ edges: [
595
+ createEdge({
596
+ id: 'crossing',
597
+ sourceId: 'source',
598
+ targetId: 'target',
599
+ label: { x: 0, y: 0, width: 170, height: 64 },
600
+ }),
601
+ createEdge({
602
+ id: 'reverse-to-group',
603
+ sourceId: 'target',
604
+ targetId: 'group',
605
+ label: { x: 0, y: 0, width: 110, height: 64 },
606
+ }),
607
+ ],
608
+ });
609
+
610
+ const result = asLayoutResult(await elkLayout({ direction: 'down' })(graph));
611
+ const group = result.nodeBounds!.get('group')!;
612
+ const source = result.nodeBounds!.get('source')!;
613
+ const target = result.nodeBounds!.get('target')!;
614
+ const overlaps = (a: Rect, b: Rect) =>
615
+ a.x < b.x + b.width &&
616
+ a.x + a.width > b.x &&
617
+ a.y < b.y + b.height &&
618
+ a.y + a.height > b.y;
619
+
620
+ expect(overlaps(result.edgeLabelBounds!.get('crossing')!, group)).toBe(false);
621
+ expect(overlaps(result.edgeLabelBounds!.get('reverse-to-group')!, group)).toBe(false);
622
+ expect(overlaps(source, group)).toBe(false);
623
+ expect(overlaps(target, group)).toBe(false);
624
+ });
625
+
626
+ it('keeps ancestor-descendant labels out of a derived group header', async () => {
627
+ const graph = createGraph({
628
+ direction: 'right',
629
+ nodes: [
630
+ createNode({
631
+ id: 'group',
632
+ width: 180,
633
+ height: 60,
634
+ group: { type: 'derived', content: 'top', direction: 'right', padding: 24 },
635
+ }),
636
+ createNode({ id: 'child', parentId: 'group', width: 120, height: 60 }),
637
+ ],
638
+ edges: [
639
+ createEdge({ id: 'group-to-child', sourceId: 'group', targetId: 'child', label: { x: 0, y: 0, width: 150, height: 64 } }),
640
+ createEdge({ id: 'child-to-group', sourceId: 'child', targetId: 'group', label: { x: 0, y: 0, width: 150, height: 64 } }),
641
+ ],
642
+ });
643
+
644
+ const result = asLayoutResult(await elkLayout({ direction: 'right' })(graph));
645
+ const group = result.nodeBounds!.get('group')!;
646
+ const header: Rect = { x: group.x, y: group.y, width: group.width, height: 60 };
647
+ const overlaps = (a: Rect, b: Rect) =>
648
+ a.x < b.x + b.width &&
649
+ a.x + a.width > b.x &&
650
+ a.y < b.y + b.height &&
651
+ a.y + a.height > b.y;
652
+
653
+ expect(overlaps(result.edgeLabelBounds!.get('group-to-child')!, header)).toBe(false);
654
+ expect(overlaps(result.edgeLabelBounds!.get('child-to-group')!, header)).toBe(false);
655
+ const child = result.nodeBounds!.get('child')!;
656
+ const groupToChild = result.edgeLabelBounds!.get('group-to-child')!;
657
+ const childToGroup = result.edgeLabelBounds!.get('child-to-group')!;
658
+ for (const label of [groupToChild, childToGroup]) {
659
+ expect(label.y).toBeGreaterThanOrEqual(header.y + header.height);
660
+ expect(label.y + label.height).toBeLessThanOrEqual(child.y);
661
+ }
662
+ expect(overlaps(groupToChild, childToGroup)).toBe(false);
663
+ });
664
+
665
+ it('uses measured group content size for ancestor label clearance', async () => {
666
+ const graph = createGraph({
667
+ direction: 'down',
668
+ nodes: [
669
+ createNode({
670
+ id: 'group',
671
+ width: 180,
672
+ height: 20,
673
+ group: { type: 'derived', content: 'top', padding: 24 },
674
+ }),
675
+ createNode({ id: 'child', parentId: 'group', width: 120, height: 60 }),
676
+ ],
677
+ edges: [
678
+ createEdge({
679
+ id: 'group-to-child',
680
+ sourceId: 'group',
681
+ targetId: 'child',
682
+ label: { x: 0, y: 0, width: 120, height: 48 },
683
+ }),
684
+ ],
685
+ });
686
+
687
+ const result = asLayoutResult(
688
+ await elkLayout({
689
+ direction: 'down',
690
+ getBounds: (node) => ({
691
+ x: node.x,
692
+ y: node.y,
693
+ width: node.width,
694
+ height: node.id === 'group' ? 200 : node.height,
695
+ }),
696
+ })(graph),
697
+ );
698
+ const group = result.nodeBounds!.get('group')!;
699
+ const child = result.nodeBounds!.get('child')!;
700
+ const label = result.edgeLabelBounds!.get('group-to-child')!;
701
+ expect(label.y).toBeGreaterThanOrEqual(group.y + 200);
702
+ expect(
703
+ Math.abs(
704
+ label.y + label.height / 2 - (group.y + 200 + child.y) / 2,
705
+ ),
706
+ ).toBeLessThanOrEqual(12);
707
+ });
708
+
709
+ it('keeps cross-hierarchy labels and root nodes separated', async () => {
710
+ const graph = createGraph({
711
+ direction: 'right',
712
+ nodes: [
713
+ createNode({ id: 'outside', width: 100, height: 60 }),
714
+ createNode({ id: 'parent', width: 140, height: 80, group: { type: 'derived', content: 'top', direction: 'right', padding: 28 } }),
715
+ createNode({ id: 'child', parentId: 'parent', width: 100, height: 60 }),
716
+ createNode({ id: 'sibling', parentId: 'parent', width: 100, height: 60 }),
717
+ createNode({ id: 'other', width: 120, height: 80, group: { type: 'derived', content: 'top', direction: 'right', padding: 28 } }),
718
+ createNode({ id: 'cousin', parentId: 'other', width: 100, height: 60 }),
719
+ ],
720
+ edges: [
721
+ createEdge({ id: 'outside-child', sourceId: 'outside', targetId: 'child', label: { x: 0, y: 0, width: 128, height: 60 } }),
722
+ createEdge({ id: 'parent-child', sourceId: 'parent', targetId: 'child', label: { x: 0, y: 0, width: 123, height: 60 } }),
723
+ createEdge({ id: 'child-outside', sourceId: 'child', targetId: 'outside', label: { x: 0, y: 0, width: 120, height: 60 } }),
724
+ createEdge({ id: 'child-parent', sourceId: 'child', targetId: 'parent', label: { x: 0, y: 0, width: 122, height: 60 } }),
725
+ createEdge({ id: 'child-cousin', sourceId: 'child', targetId: 'cousin', label: { x: 0, y: 0, width: 123, height: 60 } }),
726
+ createEdge({ id: 'cousin-sibling', sourceId: 'cousin', targetId: 'sibling', label: { x: 0, y: 0, width: 133, height: 60 } }),
727
+ ],
728
+ });
729
+
730
+ const result = asLayoutResult(
731
+ await elkLayout({ direction: 'right', graph: { 'elk.hierarchyHandling': 'SEPARATE_CHILDREN' } })(graph),
732
+ );
733
+ const overlaps = (a: Rect, b: Rect) =>
734
+ a.x < b.x + b.width &&
735
+ a.x + a.width > b.x &&
736
+ a.y < b.y + b.height &&
737
+ a.y + a.height > b.y;
738
+ for (const edge of graph.edges) {
739
+ const label = result.edgeLabelBounds!.get(edge.id)!;
740
+ for (const endpointId of [edge.sourceId, edge.targetId]) {
741
+ if (graph.nodes.find((node) => node.id === endpointId)?.group) continue;
742
+ expect(overlaps(label, result.nodeBounds!.get(endpointId)!), `${edge.id} overlaps ${endpointId}`).toBe(false);
743
+ }
744
+ }
745
+ for (const [first, second] of [
746
+ ['outside', 'parent'],
747
+ ['outside', 'other'],
748
+ ['parent', 'other'],
749
+ ] as const) {
750
+ expect(overlaps(result.nodeBounds!.get(first)!, result.nodeBounds!.get(second)!)).toBe(false);
751
+ }
752
+ });
753
+
754
+ it('reserves root space for a derived group expanded by an owned label', async () => {
755
+ const graph = createGraph({
756
+ direction: 'down',
757
+ nodes: [
758
+ createNode({ id: 'note', width: 288, height: 432 }),
759
+ createNode({ id: 'initial', width: 111, height: 42 }),
760
+ createNode({
761
+ id: 'group',
762
+ width: 563,
763
+ height: 42,
764
+ group: { type: 'derived', content: 'top', padding: 28 },
765
+ }),
766
+ createNode({ id: 'child-a', parentId: 'group', width: 135, height: 42 }),
767
+ createNode({ id: 'child-b', parentId: 'group', width: 211, height: 42 }),
768
+ createNode({ id: 'another', width: 130, height: 42 }),
769
+ ],
770
+ edges: [
771
+ createEdge({ id: 'initial-next', sourceId: 'initial', targetId: 'another', label: { x: 0, y: 0, width: 58, height: 40 } }),
772
+ createEdge({ id: 'another-group', sourceId: 'another', targetId: 'group', label: { x: 0, y: 0, width: 173, height: 64 } }),
773
+ createEdge({ id: 'another-initial', sourceId: 'another', targetId: 'initial', label: { x: 0, y: 0, width: 102, height: 64 } }),
774
+ createEdge({ id: 'group-initial', sourceId: 'group', targetId: 'initial', label: { x: 0, y: 0, width: 99, height: 74 } }),
775
+ createEdge({ id: 'inside', sourceId: 'child-a', targetId: 'child-b', label: { x: 0, y: 0, width: 58, height: 40 } }),
776
+ ],
777
+ });
778
+
779
+ const result = asLayoutResult(await elkLayout({ direction: 'down' })(graph));
780
+ const initial = result.nodeBounds!.get('initial')!;
781
+ const group = result.nodeBounds!.get('group')!;
782
+ const overlaps = (a: Rect, b: Rect) =>
783
+ a.x < b.x + b.width &&
784
+ a.x + a.width > b.x &&
785
+ a.y < b.y + b.height &&
786
+ a.y + a.height > b.y;
787
+
788
+ expect(overlaps(initial, group)).toBe(false);
789
+ expect(group).toEqual(
790
+ deriveGroupGeometry(
791
+ { width: 563, height: 42 },
792
+ [
793
+ result.nodeBounds!.get('child-a')!,
794
+ result.nodeBounds!.get('child-b')!,
795
+ result.edgeLabelBounds!.get('inside')!,
796
+ ].reduce((union, bounds) => ({
797
+ x: Math.min(union.x, bounds.x),
798
+ y: Math.min(union.y, bounds.y),
799
+ width:
800
+ Math.max(union.x + union.width, bounds.x + bounds.width) -
801
+ Math.min(union.x, bounds.x),
802
+ height:
803
+ Math.max(union.y + union.height, bounds.y + bounds.height) -
804
+ Math.min(union.y, bounds.y),
805
+ })),
806
+ { type: 'derived', content: 'top', padding: 28 },
807
+ ).bounds,
808
+ );
809
+ });
810
+
811
+ it('aligns ELK compound bounds with derived group geometry', async () => {
812
+ const graph = createGraph({
813
+ direction: 'right',
814
+ nodes: [
815
+ createNode({
816
+ id: 'group',
817
+ width: 500,
818
+ height: 50,
819
+ group: { type: 'derived', content: 'top', padding: 20 },
820
+ }),
821
+ createNode({ id: 'child', parentId: 'group', width: 100, height: 50 }),
822
+ ],
823
+ edges: [],
824
+ });
825
+
826
+ const result = asLayoutResult(await elkLayout({ direction: 'right' })(graph));
827
+ const group = result.nodeBounds!.get('group')!;
828
+ const child = result.nodeBounds!.get('child')!;
829
+ const derived = deriveGroupGeometry(
830
+ { width: 500, height: 50 },
831
+ child,
832
+ { type: 'derived', content: 'top', padding: 20 },
833
+ );
834
+
835
+ expect(group.width).toBeGreaterThanOrEqual(500);
836
+ expect(derived.bounds).toEqual(group);
837
+ });
838
+
839
+ it('ELK honors port sides via FIXED_SIDE constraints', async () => {
840
+ const graph = createGraph({
841
+ direction: 'down',
842
+ nodes: [
843
+ createNode({
844
+ id: 'n',
845
+ x: 0,
846
+ y: 0,
847
+ width: 120,
848
+ height: 80,
849
+ ports: [
850
+ createPort({ name: 'top', side: 'top', direction: 'in', width: 10, height: 10 }),
851
+ createPort({ name: 'bottom', side: 'bottom', direction: 'out', width: 10, height: 10 }),
852
+ ],
853
+ }),
854
+ ],
855
+ edges: [],
856
+ });
857
+
858
+ const elkGraph = toElkGraph(graph, {});
859
+ const node = elkGraph.children![0];
860
+ // The port-bearing node must carry FIXED_SIDE with correct ELK side tokens.
861
+ expect(node.layoutOptions?.['elk.portConstraints']).toBe('FIXED_SIDE');
862
+ const sideOf = (id: string) =>
863
+ node.ports!.find((p) => p.id === id)!.layoutOptions!['elk.port.side'];
864
+ expect(sideOf('n:top')).toBe('NORTH');
865
+ expect(sideOf('n:bottom')).toBe('SOUTH');
866
+
867
+ // Real elkjs run must place the north port above the south port.
868
+ const laidOut = await new ELK().layout(toElkGraph(graph, {}));
869
+ const laidNode = laidOut.children![0];
870
+ const topPort = laidNode.ports!.find((p) => p.id === 'n:top')!;
871
+ const bottomPort = laidNode.ports!.find((p) => p.id === 'n:bottom')!;
872
+ const centerY = (laidNode.height ?? 0) / 2;
873
+ expect(topPort.y).toBeLessThan(bottomPort.y!);
874
+ expect(topPort.y!).toBeLessThan(centerY);
875
+ expect(bottomPort.y!).toBeGreaterThan(centerY);
876
+ });
877
+
878
+ it("ELK honors a compound node's child rank direction", () => {
879
+ const graph = createGraph({
880
+ direction: 'down',
881
+ nodes: [
882
+ createNode({
883
+ id: 'group',
884
+ width: 120,
885
+ height: 40,
886
+ group: { type: 'derived', direction: 'right' },
887
+ }),
888
+ { ...createNode({ id: 'a', width: 100, height: 50 }), parentId: 'group' },
889
+ { ...createNode({ id: 'b', width: 100, height: 50 }), parentId: 'group' },
890
+ ],
891
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
892
+ });
893
+
894
+ const elkGraph = toElkGraph(graph, {
895
+ node: { 'elk.direction': 'DOWN' },
896
+ });
897
+ expect(elkGraph.children![0]!.layoutOptions).toMatchObject({
898
+ 'elk.direction': 'RIGHT',
899
+ });
900
+ });
901
+
902
+ it("preserves a compound's child direction with a cross-level edge", async () => {
903
+ const graph = createGraph({
904
+ direction: 'down',
905
+ nodes: [
906
+ createNode({
907
+ id: 'machine',
908
+ width: 120,
909
+ height: 40,
910
+ group: { type: 'derived', direction: 'down', padding: 20 },
911
+ }),
912
+ {
913
+ ...createNode({ id: 'outside', width: 100, height: 50 }),
914
+ parentId: 'machine',
915
+ },
916
+ {
917
+ ...createNode({
918
+ id: 'group',
919
+ width: 120,
920
+ height: 40,
921
+ group: { type: 'derived', direction: 'right', padding: 20 },
922
+ }),
923
+ parentId: 'machine',
924
+ },
925
+ { ...createNode({ id: 'a', width: 100, height: 50 }), parentId: 'group' },
926
+ { ...createNode({ id: 'b', width: 100, height: 50 }), parentId: 'group' },
927
+ ],
928
+ edges: [
929
+ createEdge({ id: 'outside-a', sourceId: 'outside', targetId: 'a' }),
930
+ createEdge({ id: 'a-b', sourceId: 'a', targetId: 'b' }),
931
+ ],
932
+ });
933
+
934
+ const result = asLayoutResult(await elkLayout({ direction: 'down' })(graph));
935
+ const a = result.nodeBounds!.get('a')!;
936
+ const b = result.nodeBounds!.get('b')!;
937
+ expect(b.x).toBeGreaterThan(a.x + a.width);
938
+ expect(b.y).toBe(a.y);
939
+ });
940
+
941
+ it('keeps repaired children inside a fixed compound', async () => {
942
+ const graph = createGraph({
943
+ direction: 'down',
944
+ nodes: [
945
+ createNode({ id: 'outside', width: 100, height: 50 }),
946
+ createNode({
947
+ id: 'group',
948
+ width: 260,
949
+ height: 240,
950
+ group: {
951
+ type: 'fixed',
952
+ direction: 'right',
953
+ content: 'top',
954
+ contentSize: { width: 120, height: 40 },
955
+ padding: 20,
956
+ },
957
+ }),
958
+ { ...createNode({ id: 'a', width: 100, height: 50 }), parentId: 'group' },
959
+ { ...createNode({ id: 'b', width: 100, height: 50 }), parentId: 'group' },
960
+ ],
961
+ edges: [
962
+ createEdge({ id: 'outside-a', sourceId: 'outside', targetId: 'a' }),
963
+ createEdge({ id: 'a-b', sourceId: 'a', targetId: 'b' }),
964
+ ],
965
+ });
966
+
967
+ const result = asLayoutResult(await elkLayout({ direction: 'down' })(graph));
968
+ const group = result.nodeBounds!.get('group')!;
969
+ for (const id of ['a', 'b']) {
970
+ const child = result.nodeBounds!.get(id)!;
971
+ expect(child.x).toBeGreaterThanOrEqual(group.x);
972
+ expect(child.y).toBeGreaterThanOrEqual(group.y);
973
+ expect(child.x + child.width).toBeLessThanOrEqual(group.x + group.width);
974
+ expect(child.y + child.height).toBeLessThanOrEqual(group.y + group.height);
975
+ }
976
+ });
977
+
978
+ it('keeps native routes when reversed declarations already follow the group axis', async () => {
979
+ const graph = createGraph({
980
+ direction: 'down',
981
+ nodes: [
982
+ createNode({ id: 'outside', width: 100, height: 50 }),
983
+ createNode({
984
+ id: 'group',
985
+ width: 120,
986
+ height: 40,
987
+ group: { type: 'derived', direction: 'right', padding: 20 },
988
+ }),
989
+ { ...createNode({ id: 'b', width: 100, height: 50 }), parentId: 'group' },
990
+ { ...createNode({ id: 'a', width: 100, height: 50 }), parentId: 'group' },
991
+ ],
992
+ edges: [
993
+ createEdge({ id: 'outside-a', sourceId: 'outside', targetId: 'a' }),
994
+ createEdge({
995
+ id: 'a-b',
996
+ sourceId: 'a',
997
+ targetId: 'b',
998
+ label: { x: 0, y: 0, width: 60, height: 30 },
999
+ }),
1000
+ ],
1001
+ });
1002
+
1003
+ const layoutSpy = vi.spyOn(ELK.prototype, 'layout');
1004
+ const result = asLayoutResult(await elkLayout({ direction: 'down' })(graph));
1005
+ const a = result.nodeBounds!.get('a')!;
1006
+ const b = result.nodeBounds!.get('b')!;
1007
+ expect(Math.abs(b.x - a.x)).toBeGreaterThan(Math.abs(b.y - a.y));
1008
+ expect(layoutSpy).toHaveBeenCalledTimes(1);
1009
+ layoutSpy.mockRestore();
1010
+ });
1011
+
1012
+ it('preserves separate hierarchy handling for cross-group sibling edges', () => {
1013
+ const graph = createGraph({
1014
+ nodes: [
1015
+ createNode({ id: 'left', group: { type: 'derived' } }),
1016
+ createNode({ id: 'right', group: { type: 'derived' } }),
1017
+ { ...createNode({ id: 'a' }), parentId: 'left' },
1018
+ { ...createNode({ id: 'b' }), parentId: 'right' },
1019
+ ],
1020
+ edges: [createEdge({ id: 'a-b', sourceId: 'a', targetId: 'b' })],
1021
+ });
1022
+
1023
+ expect(
1024
+ toElkGraph(graph, {
1025
+ graph: { 'elk.hierarchyHandling': 'SEPARATE_CHILDREN' },
1026
+ }).layoutOptions?.['elk.hierarchyHandling'],
1027
+ ).toBe('SEPARATE_CHILDREN');
1028
+ });
1029
+
1030
+ it.each([
1031
+ {
1032
+ node: { 'elk.layered.cycleBreaking.strategy': 'MODEL_ORDER' },
1033
+ graph: { 'elk.layered.cycleBreaking.strategy': 'GREEDY' },
1034
+ },
1035
+ { node: { layoutOptions: { 'elk.layered.cycleBreaking.strategy': 'MODEL_ORDER' } } },
1036
+ { graph: { 'elk.layered.cycleBreaking.strategy': 'MODEL_ORDER' } },
1037
+ ])('retains cycle strategy during mixed-direction repair: %j', async (options) => {
1038
+ const ids = ['a', 'b', 'c', 'd'];
1039
+ const graph = createGraph({
1040
+ nodes: [
1041
+ createNode({
1042
+ id: 'group',
1043
+ width: 120,
1044
+ height: 40,
1045
+ group: { type: 'derived', direction: 'down' },
1046
+ }),
1047
+ ...ids.map((id) => ({ ...createNode({ id, width: 100, height: 50 }), parentId: 'group' })),
1048
+ ],
1049
+ edges: ids.map((id, index) =>
1050
+ createEdge({ id: `edge-${id}`, sourceId: id, targetId: ids[(index + 1) % ids.length] }),
1051
+ ),
1052
+ });
1053
+ const originalLayout = ELK.prototype.layout;
1054
+ const localStrategies: unknown[] = [];
1055
+ const spy = vi
1056
+ .spyOn(ELK.prototype, 'layout')
1057
+ .mockImplementation(async function (this: ElkInstance, input, args) {
1058
+ const elkInput = input as ElkNode;
1059
+ if (elkInput.id.startsWith('direction:'))
1060
+ localStrategies.push(elkInput.layoutOptions?.['elk.layered.cycleBreaking.strategy']);
1061
+ const result = await originalLayout.call(this, elkInput, args) as ElkNode;
1062
+ // Reproduce a hierarchical backend result that ignores the group axis.
1063
+ if (elkInput.id === 'root')
1064
+ result.children
1065
+ ?.find((node) => node.id === 'group')
1066
+ ?.children?.forEach((child, index) => {
1067
+ child.x = index * 300;
1068
+ child.y = 0;
1069
+ });
1070
+ return result;
1071
+ });
1072
+ try {
1073
+ const result = asLayoutResult(await elkLayout({ direction: 'right', ...options })(graph));
1074
+ expect(localStrategies).toEqual(['MODEL_ORDER']);
1075
+ for (let i = 1; i < ids.length; i++) {
1076
+ expect(result.positions.get(ids[i])!.y).toBeGreaterThan(
1077
+ result.positions.get(ids[i - 1])!.y,
1078
+ );
1079
+ }
1080
+ } finally {
1081
+ spy.mockRestore();
1082
+ }
1083
+ });
1084
+
1085
+ it('preserves configured spacing during direction repair', async () => {
1086
+ const graph = createGraph({
1087
+ direction: 'down',
1088
+ nodes: [
1089
+ createNode({ id: 'outside', width: 100, height: 50 }),
1090
+ createNode({
1091
+ id: 'group',
1092
+ width: 120,
1093
+ height: 40,
1094
+ group: { type: 'derived', direction: 'right', padding: 20 },
1095
+ }),
1096
+ { ...createNode({ id: 'a', width: 100, height: 50 }), parentId: 'group' },
1097
+ { ...createNode({ id: 'b', width: 100, height: 50 }), parentId: 'group' },
1098
+ ],
1099
+ edges: [
1100
+ createEdge({ id: 'outside-a', sourceId: 'outside', targetId: 'a' }),
1101
+ createEdge({ id: 'a-b', sourceId: 'a', targetId: 'b' }),
1102
+ ],
1103
+ });
1104
+
1105
+ const result = asLayoutResult(
1106
+ await elkLayout({
1107
+ direction: 'down',
1108
+ graph: { 'elk.layered.spacing.nodeNodeBetweenLayers': '30' },
1109
+ })(graph),
1110
+ );
1111
+ const a = result.nodeBounds!.get('a')!;
1112
+ const b = result.nodeBounds!.get('b')!;
1113
+
1114
+ expect(b.x - (a.x + a.width)).toBeGreaterThanOrEqual(30);
1115
+ expect(b.x - (a.x + a.width)).toBeLessThan(90);
1116
+ });
1117
+
1118
+ it('preserves ELK node properties while accepting direct layout options', () => {
1119
+ const graph = createGraph({
1120
+ nodes: [createNode({ id: 'a', width: 100, height: 50 })],
1121
+ edges: [],
1122
+ });
1123
+
1124
+ const elkGraph = toElkGraph(graph, {
1125
+ node: {
1126
+ labels: [{ id: 'custom', text: 'A', width: 20, height: 10 }],
1127
+ layoutOptions: { 'elk.nodeSize.constraints': 'FIXED_SIZE' },
1128
+ 'elk.direction': 'DOWN',
1129
+ },
1130
+ });
1131
+
1132
+ expect(elkGraph.children![0]).toMatchObject({
1133
+ labels: [{ id: 'custom', text: 'A', width: 20, height: 10 }],
1134
+ layoutOptions: {
1135
+ 'elk.nodeSize.constraints': 'FIXED_SIZE',
1136
+ 'elk.direction': 'DOWN',
1137
+ },
1138
+ });
1139
+ });
1140
+
1141
+ it('reserves compound rank space for an inline edge label', async () => {
1142
+ const graph = createGraph({
1143
+ direction: 'down',
1144
+ nodes: [
1145
+ createNode({
1146
+ id: 'group',
1147
+ width: 120,
1148
+ height: 40,
1149
+ group: { type: 'derived', direction: 'right', padding: 24 },
1150
+ }),
1151
+ { ...createNode({ id: 'a', width: 168, height: 60 }), parentId: 'group' },
1152
+ { ...createNode({ id: 'b', width: 264, height: 60 }), parentId: 'group' },
1153
+ ],
1154
+ edges: [
1155
+ createEdge({
1156
+ id: 'ab',
1157
+ sourceId: 'a',
1158
+ targetId: 'b',
1159
+ label: { x: 0, y: 0, width: 58, height: 40 },
1160
+ }),
1161
+ ],
1162
+ });
1163
+
1164
+ const result = asLayoutResult(
1165
+ await elkLayout({
1166
+ direction: 'down',
1167
+ graph: { 'elk.layered.spacing.nodeNodeBetweenLayers': '30' },
1168
+ label: {
1169
+ 'elk.edgeLabels.inline': 'true',
1170
+ 'elk.edgeLabels.placement': 'CENTER',
1171
+ },
1172
+ })(graph),
1173
+ );
1174
+ const source = result.nodeBounds!.get('a')!;
1175
+ const target = result.nodeBounds!.get('b')!;
1176
+ const label = result.edgeLabelBounds!.get('ab')!;
1177
+
1178
+ expect(label.x).toBeGreaterThanOrEqual(source.x + source.width);
1179
+ expect(label.x + label.width).toBeLessThanOrEqual(target.x);
1180
+ expect(label.x - (source.x + source.width)).toBeGreaterThanOrEqual(24);
1181
+ expect(target.x - (label.x + label.width)).toBeGreaterThanOrEqual(24);
1182
+ expect(label.y + label.height / 2).toBe(
1183
+ source.y + source.height / 2,
1184
+ );
1185
+ });
1186
+
1187
+ it('reserves root rank space for edge labels in a hierarchical graph', () => {
1188
+ const graph = createGraph({
1189
+ direction: 'down',
1190
+ nodes: [
1191
+ createNode({ id: 'initial', width: 168, height: 60 }),
1192
+ createNode({ id: 'another', width: 200, height: 60 }),
1193
+ createNode({
1194
+ id: 'group',
1195
+ width: 120,
1196
+ height: 40,
1197
+ group: { type: 'derived', direction: 'right' },
1198
+ }),
1199
+ { ...createNode({ id: 'child', width: 168, height: 60 }), parentId: 'group' },
1200
+ ],
1201
+ edges: [
1202
+ createEdge({
1203
+ id: 'next',
1204
+ sourceId: 'initial',
1205
+ targetId: 'another',
1206
+ label: { x: 0, y: 0, width: 102, height: 64 },
1207
+ }),
1208
+ ],
1209
+ });
1210
+
1211
+ const elkGraph = toElkGraph(graph, {
1212
+ graph: { 'elk.layered.spacing.nodeNodeBetweenLayers': '30' },
1213
+ });
1214
+
1215
+ expect(
1216
+ Number(
1217
+ elkGraph.layoutOptions![
1218
+ 'elk.layered.spacing.nodeNodeBetweenLayers'
1219
+ ],
1220
+ ),
1221
+ ).toBeGreaterThanOrEqual(112);
1222
+ });
1223
+
1224
+ it('dagre returns overlap-free positions and edge label positions', async () => {
1225
+ const graph = fixture();
1226
+ const result = asLayoutResult(await dagreLayout({ nodeSpacing: 80, rankSpacing: 100 })(graph));
1227
+
1228
+ expect(result.positions.size).toBe(graph.nodes.length);
1229
+ expect(hasIntersections(graph, positionedRects(graph, result.positions))).toBe(false);
1230
+ expect(result.edgeLabelPositions?.has('ab')).toBe(true);
1231
+ expect(result.edgeLabelBounds?.get('ab')).toMatchObject({
1232
+ width: 60,
1233
+ height: 20,
1234
+ });
1235
+ expect(result.nodeBounds?.size).toBe(graph.nodes.length);
1236
+ expect(result.edgeRoutes).toBeUndefined();
1237
+ expect(result.source).toBe('dagre');
1238
+ expect(result.diagnostics).toEqual([
1239
+ expect.objectContaining({ code: 'flattened-hierarchy' }),
1240
+ ]);
1241
+ });
1242
+
1243
+ it('dagre rejects scoped layout clearly', async () => {
1244
+ await expect(dagreLayout({ scope: { ids: ['a'] } })(fixture())).rejects.toThrow(
1245
+ 'dagreLayout does not support scope/incremental layout',
1246
+ );
1247
+ });
1248
+ });