@statelyai/flow 0.14.0 → 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 +2 -1
  2. package/dist/esm/index.d.ts +5 -2
  3. package/dist/esm/index.d.ts.map +1 -0
  4. package/dist/esm/index.js +19 -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,1965 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { createFlowStore } from './store';
3
+ import { createFlowEngine, type FlowEngineOptions } from './engine';
4
+ import { createNode, createEdge, createGraph, createPort } from './factories';
5
+ import type { FlowNode, FlowEdge, Rect } from './types';
6
+ import { registerShape } from './shapes';
7
+ import { straightRouter } from './routing';
8
+ import type { EdgeRouter } from './routing';
9
+
10
+ function setup(nodes: FlowNode[] = [], edges: FlowEdge[] = [], engineOpts: FlowEngineOptions = {}) {
11
+ const store = createFlowStore(createGraph({ nodes, edges }));
12
+ const engine = createFlowEngine(store, engineOpts);
13
+ return { store, engine };
14
+ }
15
+
16
+
17
+ // Entity accessors
18
+
19
+
20
+ describe('engine entity accessors', () => {
21
+ it('uses an edge accessibility name from graph data across adapters', () => {
22
+ const { engine } = setup(
23
+ [createNode({ id: 'a', label: 'A' }), createNode({ id: 'b', label: 'B' })],
24
+ [
25
+ createEdge({ id: 'custom', sourceId: 'a', targetId: 'b', data: { ariaLabel: 'Approve order' } }),
26
+ createEdge({ id: 'default', sourceId: 'a', targetId: 'b' }),
27
+ ],
28
+ );
29
+
30
+ expect(engine.getAriaLabel('custom')).toBe('Approve order');
31
+ expect(engine.getAriaLabel('default')).toBe('Edge: A → B');
32
+ });
33
+
34
+ it('reuses custom-routed paths across viewport-only pan snapshots', () => {
35
+ const nodes = Array.from({ length: 55 }, (_, index) =>
36
+ createNode({
37
+ id: `n${index}`,
38
+ x: (index % 11) * 160,
39
+ y: Math.floor(index / 11) * 120,
40
+ width: 80,
41
+ height: 48,
42
+ }),
43
+ );
44
+ const edges = Array.from({ length: 148 }, (_, index) =>
45
+ createEdge({
46
+ id: `e${index}`,
47
+ sourceId: `n${index % nodes.length}`,
48
+ targetId: `n${(index * 7 + 1) % nodes.length}`,
49
+ }),
50
+ );
51
+ const router = vi.fn(straightRouter);
52
+ const { store, engine } = setup(nodes, edges, { edgeRouter: router });
53
+
54
+ store.trigger.panStart({ x: 0, y: 0 });
55
+ engine.getAllEdgePaths();
56
+ const callsBeforePanFrames = router.mock.calls.length;
57
+
58
+ for (let frame = 1; frame <= 120; frame++) {
59
+ store.trigger.pan({ x: frame, y: frame });
60
+ engine.getAllEdgePaths();
61
+ }
62
+
63
+ expect(callsBeforePanFrames).toBe(edges.length);
64
+ expect(router).toHaveBeenCalledTimes(callsBeforePanFrames);
65
+ });
66
+
67
+ it('getNode returns node by id', () => {
68
+ const { engine } = setup([createNode({ id: 'a', x: 10, y: 20 })]);
69
+ const node = engine.getNode('a');
70
+ expect(node).toBeDefined();
71
+ expect(node!.x).toBe(10);
72
+ });
73
+
74
+ it('getNode returns undefined for missing id', () => {
75
+ const { engine } = setup();
76
+ expect(engine.getNode('nope')).toBeUndefined();
77
+ });
78
+
79
+ it('visualBounds projects snapped drag bounds while preserving raw dx/dy', () => {
80
+ const store = createFlowStore(
81
+ createGraph({
82
+ nodes: [createNode({ id: 'a', x: 100, y: 100 })],
83
+ edges: [],
84
+ }),
85
+ { snapToGrid: true, snapGrid: [10, 10] },
86
+ );
87
+ const engine = createFlowEngine(store);
88
+
89
+ store.trigger.select({ ids: ['a'] });
90
+ store.trigger.dragStart({ id: 'a' });
91
+ store.trigger.dragMove({ dx: 13, dy: 17 });
92
+
93
+ const node = store.getSnapshot().context.graph.nodes[0];
94
+ expect(node.dx).toBe(13);
95
+ expect(node.dy).toBe(17);
96
+ expect(engine.visualBounds('a')).toMatchObject({ x: 110, y: 120 });
97
+ expect(engine.resolvedBounds('a')).toEqual(engine.visualBounds('a'));
98
+ expect(engine.resolvedPosition('a')).toEqual(engine.visualPosition('a'));
99
+ });
100
+
101
+ it('edge label path bounds project snapped drag bounds while preserving raw dx/dy', () => {
102
+ const edge = createEdge({
103
+ id: 'e1',
104
+ sourceId: 'a',
105
+ targetId: 'b',
106
+ labelAnchor: 'fixed',
107
+ label: { x: 100, y: 100, width: 80, height: 24 },
108
+ });
109
+ const store = createFlowStore(
110
+ createGraph({
111
+ nodes: [
112
+ createNode({ id: 'a', x: 0, y: 0 }),
113
+ createNode({ id: 'b', x: 300, y: 0 }),
114
+ ],
115
+ edges: [edge],
116
+ }),
117
+ { snapToGrid: true, snapGrid: [10, 10] },
118
+ );
119
+ const engine = createFlowEngine(store);
120
+
121
+ store.trigger.select({ ids: ['e1'] });
122
+ store.trigger.dragStart({ id: 'e1' });
123
+ store.trigger.dragMove({ dx: 13, dy: 17 });
124
+
125
+ const label = store.getSnapshot().context.graph.edges[0].label!;
126
+ expect(label.dx).toBe(13);
127
+ expect(label.dy).toBe(17);
128
+ expect(engine.getAllEdgePaths().get('e1')?.labelBounds).toMatchObject({
129
+ x: 110,
130
+ y: 120,
131
+ });
132
+ });
133
+
134
+ it('keeps custom-routed auto-size label paths stable across graph replacement', () => {
135
+ const graph = createGraph({
136
+ nodes: [
137
+ createNode({ id: 'a', x: 0, y: 0, width: 80, height: 40 }),
138
+ createNode({ id: 'b', x: 300, y: 0, width: 80, height: 40 }),
139
+ ],
140
+ edges: [
141
+ createEdge({
142
+ id: 'e1',
143
+ sourceId: 'a',
144
+ targetId: 'b',
145
+ labelAnchor: 0.5,
146
+ label: {
147
+ x: 0,
148
+ y: 0,
149
+ width: 0,
150
+ height: 0,
151
+ autoSize: true,
152
+ data: {},
153
+ },
154
+ }),
155
+ ],
156
+ });
157
+ const customRouter: EdgeRouter = (edge, context) => {
158
+ const path = straightRouter(edge, context);
159
+ return {
160
+ ...path,
161
+ svgPath: `M ${context.sourceBounds.width} ${context.sourceBounds.height} L ${context.targetBounds.width} ${context.targetBounds.height}`,
162
+ };
163
+ };
164
+ const store = createFlowStore(graph);
165
+ const engine = createFlowEngine(store, { edgeRouter: customRouter });
166
+ store.trigger.updateMeasurements({
167
+ measurements: [
168
+ { id: 'e1', rect: { x: 157.0625, y: 0, width: 65.875, height: 40 } },
169
+ ],
170
+ });
171
+ const measured = engine.getAllEdgePaths().get('e1')!;
172
+ expect(measured.segments.map((segment) => segment.svgPath)).toEqual([
173
+ 'M 80 40 L 65.875 40',
174
+ 'M 65.875 40 L 80 40',
175
+ ]);
176
+
177
+ store.trigger.replaceGraph({
178
+ graph: createGraph({
179
+ nodes: graph.nodes.map((node) => ({ ...node })),
180
+ edges: graph.edges.map((edge) => ({
181
+ ...edge,
182
+ label: edge.label ? { ...edge.label, width: 0, height: 0 } : undefined,
183
+ })),
184
+ }),
185
+ retention: 'preserve',
186
+ history: 'ignore',
187
+ });
188
+
189
+ expect(engine.getAllEdgePaths().get('e1')).toEqual(measured);
190
+ });
191
+
192
+ it('can route an edge from another edge label without changing graph endpoints', () => {
193
+ const branch = createEdge({
194
+ id: 'branch',
195
+ sourceId: 'a',
196
+ targetId: 'a',
197
+ labelAnchor: 'fixed',
198
+ label: { x: 100, y: 40, width: 40, height: 20 },
199
+ });
200
+ const child = createEdge<{ branchFrom?: string }>({
201
+ id: 'child',
202
+ sourceId: 'a',
203
+ targetId: 'b',
204
+ data: { branchFrom: 'branch' },
205
+ });
206
+ const { engine } = setup(
207
+ [
208
+ createNode({ id: 'a', x: 0, y: 40, width: 60, height: 40 }),
209
+ createNode({ id: 'b', x: 300, y: 40, width: 60, height: 40 }),
210
+ ],
211
+ [child, branch],
212
+ {
213
+ edgeRouter: straightRouter,
214
+ resolveEdgeSourceBounds: ({ edge, getEdgeLabelBounds }) => {
215
+ const branchFrom = (edge.data as { branchFrom?: string }).branchFrom;
216
+ return branchFrom ? getEdgeLabelBounds(branchFrom) : undefined;
217
+ },
218
+ },
219
+ );
220
+
221
+ const childPath = engine.getAllEdgePaths().get('child')?.segments[0];
222
+
223
+ expect(child.sourceId).toBe('a');
224
+ expect(child.targetId).toBe('b');
225
+ expect(childPath?.points[0]).toEqual({ x: 140, y: 50 });
226
+ expect(engine.getEdgeEndpoints('child')?.source).toEqual({ x: 140, y: 50 });
227
+ expect(engine.getEdgeSourceDependents('branch')).toEqual(['child']);
228
+ });
229
+
230
+ it('passes edge waypoints into the configured router', () => {
231
+ let seenWaypoints: readonly unknown[] | undefined;
232
+ const router: EdgeRouter = (_edge, ctx) => {
233
+ seenWaypoints = ctx.waypoints;
234
+ return straightRouter(_edge, ctx);
235
+ };
236
+ const { engine } = setup(
237
+ [
238
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 40 }),
239
+ createNode({ id: 'b', x: 300, y: 0, width: 100, height: 40 }),
240
+ ],
241
+ [
242
+ createEdge({
243
+ id: 'ab',
244
+ sourceId: 'a',
245
+ targetId: 'b',
246
+ waypoints: [{ x: 180, y: 90 }],
247
+ }),
248
+ ],
249
+ { edgeRouter: router },
250
+ );
251
+
252
+ expect(engine.getAllEdgePaths().get('ab')?.segments[0]?.points).toEqual([
253
+ { x: 100, y: 20 },
254
+ { x: 180, y: 90 },
255
+ { x: 300, y: 20 },
256
+ ]);
257
+ expect(seenWaypoints).toEqual([{ x: 180, y: 90 }]);
258
+ });
259
+
260
+ it('applies transient owned-edge offsets to waypoints', () => {
261
+ const { engine } = setup(
262
+ [
263
+ createNode({ id: 'a', x: 10, y: 10, width: 100, height: 40 }),
264
+ createNode({ id: 'b', x: 310, y: 10, width: 100, height: 40 }),
265
+ ],
266
+ [
267
+ createEdge({
268
+ id: 'ab',
269
+ sourceId: 'a',
270
+ targetId: 'b',
271
+ dx: 10,
272
+ dy: 5,
273
+ waypoints: [{ x: 180, y: 90 }],
274
+ }),
275
+ ],
276
+ { edgeRouter: straightRouter },
277
+ );
278
+
279
+ expect(engine.getAllEdgePaths().get('ab')?.segments[0]?.points).toEqual([
280
+ { x: 110, y: 30 },
281
+ { x: 190, y: 95 },
282
+ { x: 310, y: 30 },
283
+ ]);
284
+ });
285
+
286
+ it('preserves an authoritative labeled route without invoking the router', () => {
287
+ let routerCalls = 0;
288
+ const points = [
289
+ { x: -40, y: 20 },
290
+ { x: 80, y: 20 },
291
+ { x: 80, y: 180 },
292
+ { x: 420, y: 180 },
293
+ ];
294
+ const edge = createEdge({
295
+ id: 'ab',
296
+ sourceId: 'a',
297
+ targetId: 'b',
298
+ points,
299
+ routing: 'orthogonal',
300
+ svgPath: 'M -40 20 L 80 20 L 80 180 L 420 180',
301
+ label: { x: 120, y: 70, width: 96, height: 28 },
302
+ });
303
+ const { engine } = setup(
304
+ [
305
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 40 }),
306
+ createNode({ id: 'b', x: 300, y: 0, width: 100, height: 40 }),
307
+ ],
308
+ [edge],
309
+ {
310
+ edgeRouter: (routedEdge, context) => {
311
+ routerCalls++;
312
+ return straightRouter(routedEdge, context);
313
+ },
314
+ },
315
+ );
316
+
317
+ expect(engine.getAllEdgePaths().get('ab')).toEqual({
318
+ segments: [
319
+ {
320
+ points,
321
+ routing: 'orthogonal',
322
+ svgPath: 'M -40 20 L 80 20 L 80 180 L 420 180',
323
+ sourceDirection: 'right',
324
+ targetDirection: 'left',
325
+ },
326
+ ],
327
+ labelBounds: { x: 120, y: 70, width: 96, height: 28 },
328
+ });
329
+ expect(engine.getEdgeEndpoints('ab')).toEqual({
330
+ source: points[0],
331
+ target: points.at(-1),
332
+ });
333
+ expect(engine.getBounds('ab')).toEqual({
334
+ x: -40,
335
+ y: 20,
336
+ width: 460,
337
+ height: 160,
338
+ });
339
+ expect(engine.getGraphBounds()).toEqual({
340
+ x: -40,
341
+ y: 0,
342
+ width: 460,
343
+ height: 180,
344
+ });
345
+ expect(engine.entityAtPoint({ x: 80, y: 140 })?.id).toBe('ab');
346
+ expect(routerCalls).toBe(0);
347
+ });
348
+
349
+ it('does not expose stored authoritative coordinates through derived path data', () => {
350
+ const edge = createEdge({
351
+ id: 'ab',
352
+ sourceId: 'a',
353
+ targetId: 'b',
354
+ points: [{ x: 0, y: 20 }, { x: 300, y: 20 }],
355
+ });
356
+ const { engine } = setup(
357
+ [createNode({ id: 'a' }), createNode({ id: 'b', x: 300 })],
358
+ [edge],
359
+ );
360
+ const path = engine.getAllEdgePaths().get('ab')!;
361
+
362
+ path.segments[0]!.points[0]!.x = 999;
363
+ const endpoints = engine.getEdgeEndpoints('ab')!;
364
+ endpoints.target.x = 999;
365
+
366
+ expect(engine.getEdge('ab')?.points).toEqual([
367
+ { x: 0, y: 20 },
368
+ { x: 300, y: 20 },
369
+ ]);
370
+ });
371
+
372
+ it('passes the full edge and exact projection graph to both labeled segments', () => {
373
+ const edge = createEdge<{ route: string }, { event: string }>({
374
+ id: 'ab',
375
+ sourceId: 'a',
376
+ targetId: 'b',
377
+ data: { route: 'statechart' },
378
+ label: {
379
+ x: 120,
380
+ y: 50,
381
+ width: 80,
382
+ height: 24,
383
+ data: { event: 'NEXT' },
384
+ },
385
+ });
386
+ const calls: Array<{
387
+ edge: Readonly<FlowEdge<{ route: string }, { event: string }>>;
388
+ graph: unknown;
389
+ }> = [];
390
+ const router: EdgeRouter<{ route: string }, { event: string }> = (
391
+ routedEdge,
392
+ context,
393
+ ) => {
394
+ calls.push({ edge: routedEdge, graph: context.graph });
395
+ const first = context.getEntityBounds('projected-sibling');
396
+ if (first) (first as Rect).x = 999;
397
+ expect(context.getEntityBounds('projected-sibling')?.x).toBe(180);
398
+ return straightRouter(routedEdge, context);
399
+ };
400
+ const canonicalOnly = createNode({
401
+ id: 'canonical-only',
402
+ x: 500,
403
+ y: 0,
404
+ width: 40,
405
+ height: 40,
406
+ });
407
+ const { engine } = setup(
408
+ [
409
+ createNode({ id: 'a', x: 0, y: 0, width: 60, height: 40 }),
410
+ createNode({ id: 'b', x: 300, y: 0, width: 60, height: 40 }),
411
+ canonicalOnly,
412
+ ],
413
+ [edge],
414
+ { edgeRouter: router },
415
+ );
416
+ const projectionGraph = createGraph({
417
+ nodes: [
418
+ createNode({ id: 'a', x: 0, y: 0, width: 60, height: 40 }),
419
+ createNode({ id: 'b', x: 300, y: 0, width: 60, height: 40 }),
420
+ createNode({
421
+ id: 'projected-sibling',
422
+ x: 180,
423
+ y: 0,
424
+ width: 40,
425
+ height: 40,
426
+ }),
427
+ ],
428
+ edges: [edge],
429
+ });
430
+
431
+ const path = engine.getAllEdgePaths({ graph: projectionGraph }).get('ab');
432
+
433
+ expect(path?.segments).toHaveLength(2);
434
+ expect(calls).toHaveLength(2);
435
+ expect(calls.every((call) => call.edge === edge)).toBe(true);
436
+ expect(calls.every((call) => call.graph === projectionGraph)).toBe(true);
437
+ expect(calls[0].edge.data).toEqual({ route: 'statechart' });
438
+ expect(calls[0].edge.label?.data).toEqual({ event: 'NEXT' });
439
+ expect(projectionGraph.nodes.find((node) => node.id === 'projected-sibling')?.x).toBe(180);
440
+ expect(calls[0].graph).not.toBe(engine.getContext().graph);
441
+ });
442
+
443
+ it('dynamically configures routers with live detached bounds during drag', () => {
444
+ const seen: number[] = [];
445
+ const firstRouter: EdgeRouter = (edge, context) => ({
446
+ ...straightRouter(edge, context),
447
+ points: [
448
+ { x: 1, y: 1 },
449
+ { x: 2, y: 2 },
450
+ ],
451
+ });
452
+ const secondRouter: EdgeRouter = (edge, context) => {
453
+ seen.push(context.getEntityBounds('sibling')?.x ?? -1);
454
+ return straightRouter(edge, context);
455
+ };
456
+ const { store, engine } = setup(
457
+ [
458
+ createNode({ id: 'a', x: 0, y: 0, width: 40, height: 40 }),
459
+ createNode({ id: 'b', x: 300, y: 0, width: 40, height: 40 }),
460
+ createNode({ id: 'sibling', x: 120, y: 80, width: 40, height: 40 }),
461
+ ],
462
+ [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
463
+ { edgeRouter: firstRouter },
464
+ );
465
+
466
+ expect(engine.getAllEdgePaths().get('ab')?.segments[0].points).toEqual([
467
+ { x: 1, y: 1 },
468
+ { x: 2, y: 2 },
469
+ ]);
470
+ engine.setOptions({ edgeRouter: secondRouter });
471
+ engine.getAllEdgePaths();
472
+ store.trigger.select({ ids: ['sibling'] });
473
+ store.trigger.dragStart({ id: 'sibling' });
474
+ store.trigger.dragMove({ dx: 25, dy: 10 });
475
+ engine.getAllEdgePaths();
476
+
477
+ expect(seen).toEqual([120, 145]);
478
+ });
479
+
480
+ it('updates dependent edge paths while their source edge label is moving', () => {
481
+ const branch = createEdge({
482
+ id: 'branch',
483
+ sourceId: 'a',
484
+ targetId: 'a',
485
+ labelAnchor: 'fixed',
486
+ label: { x: 100, y: 40, width: 40, height: 20 },
487
+ });
488
+ const child = createEdge<{ branchFrom?: string }>({
489
+ id: 'child',
490
+ sourceId: 'a',
491
+ targetId: 'b',
492
+ data: { branchFrom: 'branch' },
493
+ });
494
+ const { store, engine } = setup(
495
+ [
496
+ createNode({ id: 'a', x: 0, y: 40, width: 60, height: 40 }),
497
+ createNode({ id: 'b', x: 300, y: 40, width: 60, height: 40 }),
498
+ ],
499
+ [branch, child],
500
+ {
501
+ edgeRouter: straightRouter,
502
+ resolveEdgeSourceBounds: ({ edge, getEdgeLabelBounds }) => {
503
+ const branchFrom = (edge.data as { branchFrom?: string }).branchFrom;
504
+ return branchFrom ? getEdgeLabelBounds(branchFrom) : undefined;
505
+ },
506
+ },
507
+ );
508
+
509
+ engine.getAllEdgePaths();
510
+ store.trigger.select({ ids: ['branch'] });
511
+ store.trigger.dragStart({ id: 'branch' });
512
+ store.trigger.dragMove({ dx: 20, dy: 10 });
513
+
514
+ expect(engine.getAllEdgePaths().get('child')?.segments[0]?.points[0]).toEqual({
515
+ x: 160,
516
+ y: 60,
517
+ });
518
+ });
519
+
520
+ it('passes sibling node bounds as custom router obstacles', () => {
521
+ const seen: number[][] = [];
522
+ const edgeRouter: EdgeRouter = (edge, context) => {
523
+ seen.push(context.obstacles.map((obstacle) => obstacle.x));
524
+ return straightRouter(edge, context);
525
+ };
526
+ const { store, engine } = setup(
527
+ [
528
+ createNode({ id: 'a', x: 0, y: 0, width: 40, height: 40 }),
529
+ createNode({ id: 'b', x: 300, y: 0, width: 40, height: 40 }),
530
+ createNode({ id: 'obstacle', x: 140, y: 0, width: 40, height: 40 }),
531
+ createNode({ id: 'child', parentId: 'obstacle', x: 150, y: 10, width: 10, height: 10 }),
532
+ ],
533
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
534
+ { edgeRouter },
535
+ );
536
+
537
+ engine.getAllEdgePaths();
538
+ store.trigger.updateNode({ id: 'obstacle', patch: { x: 180 } });
539
+ engine.getAllEdgePaths();
540
+
541
+ expect(seen).toEqual([[140], [180]]);
542
+ });
543
+
544
+ it('does not treat endpoint ancestor groups as routing obstacles', () => {
545
+ const seen: Rect[][] = [];
546
+ const edgeRouter: EdgeRouter = (edge, context) => {
547
+ seen.push(context.obstacles);
548
+ return straightRouter(edge, context);
549
+ };
550
+ const { engine } = setup(
551
+ [
552
+ createNode({
553
+ id: 'group',
554
+ x: 0,
555
+ y: 0,
556
+ width: 120,
557
+ height: 40,
558
+ group: { type: 'derived', content: 'top', padding: 20 },
559
+ }),
560
+ createNode({
561
+ id: 'child',
562
+ parentId: 'group',
563
+ x: 100,
564
+ y: 100,
565
+ width: 40,
566
+ height: 40,
567
+ }),
568
+ createNode({ id: 'outside', x: 400, y: 100, width: 80, height: 40 }),
569
+ ],
570
+ [createEdge({ id: 'cross-boundary', sourceId: 'child', targetId: 'outside' })],
571
+ { edgeRouter },
572
+ );
573
+
574
+ engine.getAllEdgePaths();
575
+
576
+ expect(seen).toEqual([[]]);
577
+ });
578
+
579
+ it('memoizes owner obstacles across sibling edges without changing routing', () => {
580
+ // Group owner with three sibling children and two edges under it. The
581
+ // obstacle set is shared per owner but each edge must still exclude only
582
+ // its own endpoints — verify the per-edge obstacle sets are exactly right.
583
+ const obstaclesByEdge = new Map<string, number[]>();
584
+ const edgeRouter: EdgeRouter = (edge, context) => {
585
+ obstaclesByEdge.set(
586
+ edge.sourceId,
587
+ context.obstacles.map((obstacle) => obstacle.x).sort((a, b) => a - b),
588
+ );
589
+ return straightRouter(edge, context);
590
+ };
591
+ const { engine } = setup(
592
+ [
593
+ createNode({
594
+ id: 'group',
595
+ group: { type: 'fixed', contentSize: { width: 0, height: 0 } },
596
+ x: 0,
597
+ y: 0,
598
+ width: 400,
599
+ height: 400,
600
+ }),
601
+ createNode({ id: 'n1', parentId: 'group', x: 10, y: 10, width: 20, height: 20 }),
602
+ createNode({ id: 'n2', parentId: 'group', x: 100, y: 10, width: 20, height: 20 }),
603
+ createNode({ id: 'n3', parentId: 'group', x: 200, y: 10, width: 20, height: 20 }),
604
+ ],
605
+ [
606
+ createEdge({ id: 'e1', sourceId: 'n1', targetId: 'n2' }),
607
+ createEdge({ id: 'e2', sourceId: 'n2', targetId: 'n3' }),
608
+ ],
609
+ { edgeRouter },
610
+ );
611
+
612
+ const paths = engine.getAllEdgePaths();
613
+ expect(paths.has('e1')).toBe(true);
614
+ expect(paths.has('e2')).toBe(true);
615
+
616
+ // e1 (n1→n2): only n3 remains as an obstacle. e2 (n2→n3): only n1 remains.
617
+ // Keyed by source node id — the router's edge arg carries no id.
618
+ expect(obstaclesByEdge.get('n1')).toEqual([200]);
619
+ expect(obstaclesByEdge.get('n2')).toEqual([10]);
620
+ });
621
+
622
+ it('getEdge returns edge by id', () => {
623
+ const { engine } = setup(
624
+ [createNode({ id: 'a' }), createNode({ id: 'b' })],
625
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
626
+ );
627
+ expect(engine.getEdge('e1')).toBeDefined();
628
+ });
629
+
630
+ it('passes graph, nodes, ports, and connection state to canConnect', () => {
631
+ const calls: unknown[] = [];
632
+ const { store, engine } = setup(
633
+ [
634
+ createNode({
635
+ id: 'a',
636
+ ports: [createPort({ name: 'out', side: 'right', direction: 'out' })],
637
+ }),
638
+ createNode({
639
+ id: 'b',
640
+ ports: [createPort({ name: 'in', side: 'left', direction: 'in' })],
641
+ }),
642
+ ],
643
+ [],
644
+ {
645
+ canConnect: (_proposed, context) => {
646
+ calls.push(context);
647
+ return (
648
+ context.graph.nodes.length === 2 &&
649
+ context.source.id === 'a' &&
650
+ context.target.id === 'b' &&
651
+ context.sourcePort?.name === 'out' &&
652
+ context.targetPort?.name === 'in' &&
653
+ context.connection?.sourceId === 'a'
654
+ );
655
+ },
656
+ },
657
+ );
658
+
659
+ store.trigger.connectionStart({
660
+ sourceId: 'a',
661
+ sourcePort: 'out',
662
+ position: { x: 0, y: 0 },
663
+ });
664
+
665
+ expect(
666
+ engine.validateConnection({
667
+ sourceId: 'a',
668
+ targetId: 'b',
669
+ sourcePort: 'out',
670
+ targetPort: 'in',
671
+ }),
672
+ ).toBe(true);
673
+ expect(calls).toHaveLength(1);
674
+ });
675
+
676
+ it('supports detailed connection validation results', () => {
677
+ const { engine } = setup(
678
+ [createNode({ id: 'a' }), createNode({ id: 'b', x: 100 })],
679
+ [],
680
+ {
681
+ canConnect: () => ({ valid: false, reason: 'policy-denied' }),
682
+ },
683
+ );
684
+
685
+ expect(engine.validateConnection({ sourceId: 'a', targetId: 'b' })).toBe(false);
686
+ expect(engine.validateConnectionDetailed({ sourceId: 'a', targetId: 'b' })).toEqual({
687
+ valid: false,
688
+ reason: 'policy-denied',
689
+ });
690
+ });
691
+
692
+ it('finds the nearest valid connection port within the magnet radius', () => {
693
+ const { engine } = setup([
694
+ createNode({
695
+ id: 'a',
696
+ x: 0,
697
+ y: 0,
698
+ ports: [createPort({ name: 'out', side: 'right', direction: 'out', x: 100, y: 20 })],
699
+ }),
700
+ createNode({
701
+ id: 'b',
702
+ x: 300,
703
+ y: 0,
704
+ ports: [createPort({ name: 'in', side: 'left', direction: 'in', x: 0, y: 20 })],
705
+ }),
706
+ ]);
707
+
708
+ expect(
709
+ engine.getConnectionCandidate(
710
+ { x: 315, y: 28 },
711
+ { source: { nodeId: 'a', port: 'out' } },
712
+ ),
713
+ ).toEqual({
714
+ nodeId: 'b',
715
+ portName: 'in',
716
+ position: { x: 306, y: 26 },
717
+ isValid: true,
718
+ });
719
+ });
720
+
721
+ it('falls back to node bodies in loose mode, but requires ports in strict mode', () => {
722
+ const nodes = [
723
+ createNode({
724
+ id: 'a',
725
+ x: 0,
726
+ y: 0,
727
+ ports: [createPort({ name: 'out', side: 'right', direction: 'out' })],
728
+ }),
729
+ createNode({
730
+ id: 'b',
731
+ x: 300,
732
+ y: 0,
733
+ width: 100,
734
+ height: 50,
735
+ ports: [createPort({ name: 'in', side: 'left', direction: 'in', x: 0, y: 0 })],
736
+ }),
737
+ ];
738
+ const loose = setup(nodes).engine;
739
+ const strict = setup(nodes, [], { connection: { mode: 'strict' } }).engine;
740
+
741
+ expect(
742
+ loose.getConnectionCandidate(
743
+ { x: 350, y: 25 },
744
+ { source: { nodeId: 'a', port: 'out' } },
745
+ ),
746
+ ).toMatchObject({ nodeId: 'b', portName: null });
747
+ expect(
748
+ strict.getConnectionCandidate(
749
+ { x: 390, y: 45 },
750
+ { source: { nodeId: 'a', port: 'out' } },
751
+ ),
752
+ ).toBeNull();
753
+
754
+ strict.setOptions({ connection: { radius: 60 } });
755
+ expect(
756
+ strict.getConnectionCandidate(
757
+ { x: 390, y: 45 },
758
+ { source: { nodeId: 'a', port: 'out' } },
759
+ ),
760
+ ).toBeNull();
761
+ expect(
762
+ strict.getConnectionCandidate(
763
+ { x: 312, y: 8 },
764
+ { source: { nodeId: 'a', port: 'out' } },
765
+ ),
766
+ ).toMatchObject({ nodeId: 'b', portName: 'in' });
767
+ });
768
+
769
+ it('loose-mode node-body tie-break picks the topmost painted node (child over parent group)', () => {
770
+ // A parent group fully contains a connectable child; the pointer sits over
771
+ // the child. Both node bodies are at distance 0 to the pointer, but the
772
+ // child paints on top of the group, so it must win the tie.
773
+ const nodes = [
774
+ createNode({
775
+ id: 'group',
776
+ x: 0,
777
+ y: 0,
778
+ width: 300,
779
+ height: 300,
780
+ group: { type: 'fixed', contentSize: { width: 300, height: 300 } },
781
+ ports: [createPort({ name: 'gout', side: 'right', direction: 'out' })],
782
+ }),
783
+ createNode({
784
+ id: 'child',
785
+ parentId: 'group',
786
+ x: 100,
787
+ y: 100,
788
+ width: 50,
789
+ height: 50,
790
+ }),
791
+ createNode({
792
+ id: 'src',
793
+ x: 500,
794
+ y: 0,
795
+ width: 40,
796
+ height: 40,
797
+ ports: [createPort({ name: 'out', side: 'right', direction: 'out' })],
798
+ }),
799
+ ];
800
+ const { engine } = setup(nodes);
801
+ const candidate = engine.getConnectionCandidate(
802
+ { x: 125, y: 125 },
803
+ { source: { nodeId: 'src', port: 'out' } },
804
+ );
805
+ expect(candidate).toMatchObject({ nodeId: 'child', portName: null });
806
+ });
807
+
808
+ it('limits configured group targets to their content and child-surface boundary', () => {
809
+ const nodes = [
810
+ createNode({
811
+ id: 'group',
812
+ x: 0,
813
+ y: 0,
814
+ width: 100,
815
+ height: 40,
816
+ group: {
817
+ type: 'derived',
818
+ padding: 20,
819
+ minChildArea: { width: 300, height: 200 },
820
+ connectionBoundaryWidth: 12,
821
+ },
822
+ }),
823
+ createNode({
824
+ id: 'child',
825
+ parentId: 'group',
826
+ x: 100,
827
+ y: 100,
828
+ width: 50,
829
+ height: 50,
830
+ }),
831
+ createNode({ id: 'source', x: 500, y: 0, width: 40, height: 40 }),
832
+ ];
833
+ const { engine } = setup(nodes);
834
+ const options = { source: { nodeId: 'source', port: null } };
835
+
836
+ expect(
837
+ engine.getConnectionCandidate({ x: 100, y: 60 }, options),
838
+ ).toMatchObject({ nodeId: 'group' });
839
+ expect(
840
+ engine.getConnectionCandidate({ x: 86, y: 180 }, options),
841
+ ).toMatchObject({ nodeId: 'group' });
842
+ expect(
843
+ engine.getConnectionCandidate({ x: 250, y: 220 }, options),
844
+ ).toBeNull();
845
+ expect(
846
+ engine.getConnectionCandidate({ x: 125, y: 125 }, options),
847
+ ).toMatchObject({ nodeId: 'child' });
848
+ });
849
+
850
+ it('keeps the full group body connectable when no boundary is configured', () => {
851
+ const { engine } = setup([
852
+ createNode({
853
+ id: 'group',
854
+ width: 100,
855
+ height: 40,
856
+ group: {
857
+ type: 'derived',
858
+ minChildArea: { width: 300, height: 200 },
859
+ },
860
+ }),
861
+ createNode({ id: 'source', x: 500, width: 40, height: 40 }),
862
+ ]);
863
+
864
+ expect(
865
+ engine.getConnectionCandidate(
866
+ { x: 150, y: 120 },
867
+ { source: { nodeId: 'source', port: null } },
868
+ ),
869
+ ).toMatchObject({ nodeId: 'group' });
870
+ });
871
+
872
+ it('filters connection candidates through connectable config and canConnect', () => {
873
+ const { engine } = setup(
874
+ [
875
+ createNode({
876
+ id: 'a',
877
+ ports: [createPort({ name: 'out', side: 'right', direction: 'out' })],
878
+ }),
879
+ createNode({
880
+ id: 'blocked',
881
+ x: 100,
882
+ ports: [createPort({ name: 'in', side: 'left', direction: 'in' })],
883
+ }),
884
+ createNode({
885
+ id: 'allowed',
886
+ x: 130,
887
+ ports: [createPort({ name: 'in', side: 'left', direction: 'in' })],
888
+ }),
889
+ ],
890
+ [],
891
+ {
892
+ connection: { radius: 50 },
893
+ canConnect: (proposed) => proposed.targetId === 'allowed',
894
+ },
895
+ );
896
+
897
+ expect(
898
+ engine.getConnectionCandidate(
899
+ { x: 106, y: 6 },
900
+ { source: { nodeId: 'a', port: 'out' } },
901
+ )?.nodeId,
902
+ ).toBe('allowed');
903
+
904
+ engine.setOptions({
905
+ config: {
906
+ connectable: (entity) => (entity as FlowNode | FlowEdge).id !== 'allowed',
907
+ },
908
+ });
909
+ expect(
910
+ engine.getConnectionCandidate(
911
+ { x: 136, y: 6 },
912
+ { source: { nodeId: 'a', port: 'out' } },
913
+ ),
914
+ ).toBeNull();
915
+ });
916
+
917
+ it('hides edges connected to hidden nodes', () => {
918
+ const { engine } = setup(
919
+ [
920
+ createNode({ id: 'a', x: 0, y: 0, hidden: true }),
921
+ createNode({ id: 'b', x: 200, y: 0 }),
922
+ createNode({ id: 'c', x: 400, y: 0 }),
923
+ ],
924
+ [
925
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
926
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
927
+ ],
928
+ );
929
+
930
+ expect(engine.getAllEdgePaths().has('ab')).toBe(false);
931
+ expect(engine.getAllEdgePaths().has('bc')).toBe(true);
932
+ expect(engine.getVisibleEntities().edges.map((edge) => edge.id)).toEqual(['bc']);
933
+ expect(engine.resolveEntityConfig('ab').hidden).toBe(true);
934
+ expect(engine.resolveEntityConfig('bc').hidden).toBe(false);
935
+ });
936
+
937
+ it('detects intersections using active drag offsets', () => {
938
+ const { store, engine } = setup([
939
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 }),
940
+ createNode({ id: 'b', x: 150, y: 0, width: 100, height: 100 }),
941
+ ]);
942
+
943
+ expect(engine.getIntersectingNodes('a')).toEqual([]);
944
+
945
+ store.trigger.select({ ids: ['a'] });
946
+ store.trigger.dragStart({ id: 'a' });
947
+ store.trigger.dragMove({ dx: 75, dy: 0 });
948
+
949
+ expect(engine.getIntersectingNodes('a').map((node) => node.id)).toEqual(['b']);
950
+ });
951
+
952
+ it('getEntity returns node or edge', () => {
953
+ const { engine } = setup(
954
+ [createNode({ id: 'a' })],
955
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'a' })],
956
+ );
957
+ expect(engine.getEntity('a')?.type).toBe('node');
958
+ expect(engine.getEntity('e1')?.type).toBe('edge');
959
+ });
960
+
961
+ it('resolves addressables and owners', () => {
962
+ const { engine } = setup([
963
+ createNode({
964
+ id: 'a',
965
+ ports: [createPort({ name: 'out', side: 'right', direction: 'out' })],
966
+ }),
967
+ ]);
968
+
969
+ expect(engine.getAddressable('a')).toMatchObject({ kind: 'node', id: 'a' });
970
+ expect(engine.getAddressable('a:out')).toMatchObject({
971
+ kind: 'port',
972
+ id: 'a:out',
973
+ nodeId: 'a',
974
+ portName: 'out',
975
+ });
976
+ expect(engine.getOwnerEntity('a:out')).toMatchObject({ id: 'a' });
977
+ expect(engine.getEntityRef('a:out')).toMatchObject({ kind: 'port' });
978
+ expect(engine.getAddressable('missing')).toBeUndefined();
979
+ expect(engine.getOwnerEntity('missing')).toBeUndefined();
980
+ expect(engine.getBounds('missing')).toBeUndefined();
981
+ });
982
+
983
+ it('passes the edge to function label anchors', () => {
984
+ const seen: string[] = [];
985
+ const { engine } = setup(
986
+ [
987
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 }),
988
+ createNode({ id: 'b', x: 200, y: 0, width: 100, height: 50 }),
989
+ ],
990
+ [
991
+ createEdge({
992
+ id: 'ab',
993
+ sourceId: 'a',
994
+ targetId: 'b',
995
+ label: { x: 0, y: 0, width: 40, height: 20 },
996
+ labelAnchor: (_source, _target, edge) => {
997
+ seen.push(edge.id);
998
+ return { x: 160, y: 25 };
999
+ },
1000
+ }),
1001
+ ],
1002
+ );
1003
+
1004
+ expect(engine.getAllEdgePaths().get('ab')?.labelBounds).toMatchObject({ x: 140, y: 15 });
1005
+ expect(seen).toEqual(['ab']);
1006
+ });
1007
+
1008
+ it('uses live measured size without letting stale measurements override node position', () => {
1009
+ const { store, engine } = setup([
1010
+ createNode({ id: 'a', x: 10, y: 20, width: 100, height: 50 }),
1011
+ ]);
1012
+
1013
+ expect(engine.getBounds('a')).toEqual({ x: 10, y: 20, width: 100, height: 50 });
1014
+
1015
+ store.trigger.updateMeasurements({
1016
+ measurements: [{ id: 'a', rect: { x: 12, y: 24, width: 110, height: 60 } }],
1017
+ });
1018
+
1019
+ expect(engine.getBounds('a')).toEqual({ x: 10, y: 20, width: 110, height: 60 });
1020
+
1021
+ store.trigger.applyLayoutResult({
1022
+ positions: new Map([['a', { x: 200, y: 300 }]]),
1023
+ });
1024
+
1025
+ expect(engine.getBounds('a')).toEqual({ x: 200, y: 300, width: 110, height: 60 });
1026
+
1027
+ store.trigger.removeMeasurements({ ids: ['a'] });
1028
+
1029
+ // Graph-owned (non-autoSize) node: its authored size is authoritative, so
1030
+ // removing the measurement reverts to it. The measured size is NOT baked
1031
+ // into node.width/height — doing so fed render → measure → render into a
1032
+ // growth loop.
1033
+ expect(engine.getBounds('a')).toEqual({ x: 200, y: 300, width: 100, height: 50 });
1034
+ });
1035
+
1036
+ it('does not reroute untouched edges after applying layout', () => {
1037
+ const { store, engine } = setup(
1038
+ [
1039
+ createNode({ id: 'a', x: 0 }),
1040
+ createNode({ id: 'b', x: 200 }),
1041
+ createNode({ id: 'c', x: 400 }),
1042
+ createNode({ id: 'd', x: 600 }),
1043
+ ],
1044
+ [
1045
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
1046
+ createEdge({ id: 'cd', sourceId: 'c', targetId: 'd' }),
1047
+ ],
1048
+ );
1049
+
1050
+ const before = engine.getAllEdgePaths();
1051
+ store.trigger.applyLayoutResult({
1052
+ positions: new Map([['a', { x: 100, y: 100 }]]),
1053
+ });
1054
+ const after = engine.getAllEdgePaths();
1055
+
1056
+ expect(after.get('ab')).not.toBe(before.get('ab'));
1057
+ expect(after.get('cd')).toBe(before.get('cd'));
1058
+ });
1059
+
1060
+ it('restores authored dimensions when node measurements are removed', () => {
1061
+ const { store, engine } = setup([
1062
+ createNode({ id: 'auto', x: 0, y: 0, width: 10, height: 10, autoSize: true }),
1063
+ createNode({ id: 'fixed', x: 0, y: 0, width: 10, height: 10 }),
1064
+ ]);
1065
+
1066
+ store.trigger.updateMeasurements({
1067
+ measurements: [
1068
+ { id: 'auto', rect: { x: 0, y: 0, width: 80, height: 40 } },
1069
+ { id: 'fixed', rect: { x: 0, y: 0, width: 80, height: 40 } },
1070
+ ],
1071
+ });
1072
+ store.trigger.removeMeasurements({ ids: ['auto', 'fixed'] });
1073
+
1074
+ expect(engine.getBounds('auto')).toMatchObject({ width: 10, height: 10 });
1075
+ expect(engine.getBounds('fixed')).toMatchObject({ width: 10, height: 10 });
1076
+ });
1077
+
1078
+ it('gets port bounds from measured or graph-owned geometry', () => {
1079
+ const { store, engine } = setup([
1080
+ createNode({
1081
+ id: 'a',
1082
+ x: 10,
1083
+ y: 20,
1084
+ ports: [
1085
+ createPort({
1086
+ name: 'out',
1087
+ side: 'right',
1088
+ direction: 'out',
1089
+ x: 100,
1090
+ y: 10,
1091
+ width: 8,
1092
+ height: 8,
1093
+ }),
1094
+ ],
1095
+ }),
1096
+ ]);
1097
+
1098
+ expect(engine.getBounds('a:out')).toEqual({ x: 110, y: 30, width: 8, height: 8 });
1099
+
1100
+ store.trigger.updateMeasurements({
1101
+ measurements: [{ id: 'a:out', rect: { x: 112, y: 32, width: 10, height: 10 } }],
1102
+ });
1103
+
1104
+ expect(engine.getBounds('a:out')).toEqual({ x: 112, y: 32, width: 10, height: 10 });
1105
+ });
1106
+ });
1107
+
1108
+
1109
+ // getGraphBounds (engine API)
1110
+
1111
+
1112
+ describe('engine.getGraphBounds', () => {
1113
+ it('includes nodes', () => {
1114
+ const { engine } = setup([
1115
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 }),
1116
+ ]);
1117
+ const b = engine.getGraphBounds();
1118
+ expect(b.width).toBe(100);
1119
+ expect(b.height).toBe(50);
1120
+ });
1121
+
1122
+ it('includes edge labels', () => {
1123
+ const { engine } = setup(
1124
+ [createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 })],
1125
+ [createEdge({
1126
+ id: 'e1',
1127
+ sourceId: 'a',
1128
+ targetId: 'a',
1129
+ label: { x: 200, y: 200, width: 40, height: 20, data: {} },
1130
+ })],
1131
+ );
1132
+ const b = engine.getGraphBounds();
1133
+ // bounds = (0,0) to (240, 220)
1134
+ expect(b.width).toBe(240);
1135
+ expect(b.height).toBe(220);
1136
+ });
1137
+
1138
+ it('keeps auto-size label routing stable across authored graph replacement', () => {
1139
+ const graph = createGraph({
1140
+ nodes: [
1141
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
1142
+ createNode({ id: 'b', x: 300, y: 0, width: 50, height: 50 }),
1143
+ ],
1144
+ edges: [
1145
+ createEdge({
1146
+ id: 'e1',
1147
+ sourceId: 'a',
1148
+ targetId: 'b',
1149
+ labelAnchor: 0.5,
1150
+ label: {
1151
+ x: 0,
1152
+ y: 0,
1153
+ width: 0,
1154
+ height: 0,
1155
+ autoSize: true,
1156
+ data: {},
1157
+ },
1158
+ }),
1159
+ ],
1160
+ });
1161
+ const store = createFlowStore(graph);
1162
+ const engine = createFlowEngine(store);
1163
+ expect(engine.getAllEdgePaths().get('e1')!.labelBounds).toMatchObject({
1164
+ width: 0,
1165
+ height: 0,
1166
+ });
1167
+ store.trigger.updateMeasurements({
1168
+ measurements: [
1169
+ { id: 'e1', rect: { x: 142.0625, y: 5, width: 65.875, height: 40 } },
1170
+ ],
1171
+ });
1172
+ const measuredPath = engine.getAllEdgePaths().get('e1')!;
1173
+
1174
+ expect(measuredPath.labelBounds).toEqual({
1175
+ x: 142.0625,
1176
+ y: 5,
1177
+ width: 65.875,
1178
+ height: 40,
1179
+ });
1180
+ expect(engine.getBounds('e1')).toEqual(measuredPath.labelBounds);
1181
+ expect(engine.getGraphBounds()).toEqual({
1182
+ x: 0,
1183
+ y: 0,
1184
+ width: 350,
1185
+ height: 50,
1186
+ });
1187
+
1188
+ store.trigger.replaceGraph({
1189
+ graph: createGraph({
1190
+ nodes: graph.nodes.map((node) => ({ ...node })),
1191
+ edges: graph.edges.map((edge) => ({
1192
+ ...edge,
1193
+ label: edge.label ? { ...edge.label, width: 0, height: 0 } : undefined,
1194
+ })),
1195
+ }),
1196
+ history: 'ignore',
1197
+ retention: 'preserve',
1198
+ });
1199
+
1200
+ const replacedPath = engine.getAllEdgePaths().get('e1')!;
1201
+ expect(replacedPath).toEqual(measuredPath);
1202
+ expect(store.getSnapshot().context.graph.edges[0].label).toMatchObject({
1203
+ width: 0,
1204
+ height: 0,
1205
+ autoSize: true,
1206
+ });
1207
+ });
1208
+
1209
+ it('does not route the graph when fixed label bounds answer an edge bounds query', () => {
1210
+ let routeCount = 0;
1211
+ const edgeRouter: EdgeRouter = (edge, context) => {
1212
+ routeCount++;
1213
+ return straightRouter(edge, context);
1214
+ };
1215
+ const { engine } = setup(
1216
+ [
1217
+ createNode({ id: 'a', x: 0, y: 0 }),
1218
+ createNode({ id: 'b', x: 300, y: 0 }),
1219
+ createNode({ id: 'c', x: 600, y: 0 }),
1220
+ ],
1221
+ [
1222
+ createEdge({
1223
+ id: 'ab',
1224
+ sourceId: 'a',
1225
+ targetId: 'b',
1226
+ label: { x: 120, y: 40, width: 60, height: 20 },
1227
+ }),
1228
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
1229
+ ],
1230
+ { edgeRouter },
1231
+ );
1232
+
1233
+ expect(engine.getBounds('ab')).toEqual({
1234
+ x: 120,
1235
+ y: 40,
1236
+ width: 60,
1237
+ height: 20,
1238
+ });
1239
+ expect(routeCount).toBe(0);
1240
+ });
1241
+ });
1242
+
1243
+
1244
+ // getSelectionBounds (engine API)
1245
+
1246
+
1247
+ describe('engine.getSelectionBounds', () => {
1248
+ it('returns null when nothing selected', () => {
1249
+ const { engine } = setup([createNode({ id: 'a' })]);
1250
+ expect(engine.getSelectionBounds()).toBeNull();
1251
+ });
1252
+
1253
+ it('returns bounds of selected node', () => {
1254
+ const { store, engine } = setup([
1255
+ createNode({ id: 'a', x: 10, y: 20, width: 100, height: 50 }),
1256
+ ]);
1257
+ store.trigger.select({ ids: ['a'] });
1258
+ const b = engine.getSelectionBounds();
1259
+ expect(b).toEqual({ x: 10, y: 20, width: 100, height: 50 });
1260
+ });
1261
+
1262
+ it('includes selected edge label in bounds', () => {
1263
+ const { store, engine } = setup(
1264
+ [createNode({ id: 'a', x: 0, y: 0, width: 10, height: 10 })],
1265
+ [createEdge({
1266
+ id: 'e1',
1267
+ sourceId: 'a',
1268
+ targetId: 'a',
1269
+ label: { x: 100, y: 100, width: 30, height: 20, data: {} },
1270
+ })],
1271
+ );
1272
+ store.trigger.select({ ids: ['a', 'e1'], mode: 'single' });
1273
+ const b = engine.getSelectionBounds();
1274
+ // (0,0)-(10,10) ∪ (100,100)-(130,120) = (0,0)-(130,120)
1275
+ expect(b).toEqual({ x: 0, y: 0, width: 130, height: 120 });
1276
+ });
1277
+
1278
+ it('includes authoritative points in static selection bounds', () => {
1279
+ const { store, engine } = setup(
1280
+ [createNode({ id: 'a', x: 0, y: 0, width: 10, height: 10 })],
1281
+ [createEdge({
1282
+ id: 'e1',
1283
+ sourceId: 'a',
1284
+ targetId: 'a',
1285
+ points: [{ x: -100, y: 20 }, { x: 200, y: 300 }],
1286
+ })],
1287
+ );
1288
+ store.trigger.select({ ids: ['e1'] });
1289
+
1290
+ expect(engine.getSelectionBounds()).toEqual({
1291
+ x: -100,
1292
+ y: 20,
1293
+ width: 300,
1294
+ height: 280,
1295
+ });
1296
+ });
1297
+ });
1298
+
1299
+
1300
+ // Coordinate conversion
1301
+
1302
+
1303
+ describe('engine coordinate conversion', () => {
1304
+ it('screenToCanvas + canvasToScreen round-trip', () => {
1305
+ const { store, engine } = setup();
1306
+ store.trigger.setViewport({ x: 50, y: -30, zoom: 2 });
1307
+ const canvas = engine.screenToCanvas({ x: 200, y: 300 });
1308
+ const screen = engine.canvasToScreen(canvas);
1309
+ expect(screen.x).toBeCloseTo(200);
1310
+ expect(screen.y).toBeCloseTo(300);
1311
+ });
1312
+ });
1313
+
1314
+
1315
+ // entityAtPoint — hit testing
1316
+
1317
+
1318
+ describe('engine.entityAtPoint', () => {
1319
+ it('returns node when point is inside', () => {
1320
+ const { engine } = setup([
1321
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 }),
1322
+ ]);
1323
+ const entity = engine.entityAtPoint({ x: 50, y: 50 });
1324
+ expect(entity).not.toBeNull();
1325
+ expect(entity!.id).toBe('a');
1326
+ });
1327
+
1328
+ it('returns null when point is outside all entities', () => {
1329
+ const { engine } = setup([
1330
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
1331
+ ]);
1332
+ expect(engine.entityAtPoint({ x: 500, y: 500 })).toBeNull();
1333
+ });
1334
+
1335
+ it('returns edge when point hits fixed edge label', () => {
1336
+ const { engine } = setup(
1337
+ [createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
1338
+ createNode({ id: 'b', x: 200, y: 0, width: 50, height: 50 })],
1339
+ [createEdge({
1340
+ id: 'e1',
1341
+ sourceId: 'a',
1342
+ targetId: 'b',
1343
+ label: { x: 100, y: 100, width: 40, height: 20, data: {} },
1344
+ })],
1345
+ );
1346
+ const entity = engine.entityAtPoint({ x: 110, y: 110 });
1347
+ expect(entity).not.toBeNull();
1348
+ expect(entity!.id).toBe('e1');
1349
+ expect(entity!.type).toBe('edge');
1350
+ });
1351
+
1352
+ it('returns edge when point is near an unlabeled edge path', () => {
1353
+ const { engine } = setup(
1354
+ [
1355
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
1356
+ createNode({ id: 'b', x: 200, y: 0, width: 50, height: 50 }),
1357
+ ],
1358
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
1359
+ { edgeRouter: straightRouter },
1360
+ );
1361
+
1362
+ const entity = engine.entityAtPoint({ x: 125, y: 28 });
1363
+ expect(entity).not.toBeNull();
1364
+ expect(entity!.id).toBe('e1');
1365
+ expect(entity!.type).toBe('edge');
1366
+ });
1367
+
1368
+ it('returns null when point is too far from an unlabeled edge path', () => {
1369
+ const { engine } = setup(
1370
+ [
1371
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
1372
+ createNode({ id: 'b', x: 200, y: 0, width: 50, height: 50 }),
1373
+ ],
1374
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
1375
+ { edgeRouter: straightRouter },
1376
+ );
1377
+
1378
+ expect(engine.entityAtPoint({ x: 125, y: 60 })).toBeNull();
1379
+ });
1380
+
1381
+ it('prefers nodes over edge labels (nodes render on top)', () => {
1382
+ const { engine } = setup(
1383
+ [createNode({ id: 'a', x: 100, y: 100, width: 50, height: 50 })],
1384
+ [createEdge({
1385
+ id: 'e1',
1386
+ sourceId: 'a',
1387
+ targetId: 'a',
1388
+ label: { x: 100, y: 100, width: 50, height: 50, data: {} },
1389
+ })],
1390
+ );
1391
+ // Both overlap at (110, 110) — node should win
1392
+ const entity = engine.entityAtPoint({ x: 110, y: 110 });
1393
+ expect(entity!.id).toBe('a');
1394
+ });
1395
+
1396
+ it('does not return a node hidden by a collapsed ancestor', () => {
1397
+ const { store, engine } = setup([
1398
+ createNode({
1399
+ id: 'group',
1400
+ x: 0,
1401
+ y: 0,
1402
+ width: 300,
1403
+ height: 300,
1404
+ group: { type: 'fixed', contentSize: { width: 300, height: 300 } },
1405
+ }),
1406
+ createNode({
1407
+ id: 'child',
1408
+ parentId: 'group',
1409
+ x: 100,
1410
+ y: 100,
1411
+ width: 50,
1412
+ height: 50,
1413
+ }),
1414
+ ]);
1415
+ // Before collapse: pointer over the child hits the child.
1416
+ expect(engine.entityAtPoint({ x: 125, y: 125 })!.id).toBe('child');
1417
+
1418
+ store.trigger.collapseNode({ id: 'group' });
1419
+ // After collapse: the child is hidden, so its old coords must not return it.
1420
+ const hit = engine.entityAtPoint({ x: 125, y: 125 });
1421
+ expect(hit?.id).not.toBe('child');
1422
+ });
1423
+
1424
+ it('returns the paint-topmost node when overlapping nodes are elevated by zIndex', () => {
1425
+ const { engine } = setup([
1426
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100, zIndex: 5 }),
1427
+ createNode({ id: 'b', x: 0, y: 0, width: 100, height: 100, zIndex: 0 }),
1428
+ ]);
1429
+ // 'a' has the higher zIndex ⇒ painted on top ⇒ hit first, even though 'b'
1430
+ // is later in the source array.
1431
+ expect(engine.entityAtPoint({ x: 50, y: 50 })!.id).toBe('a');
1432
+ });
1433
+
1434
+ it('returns the selected node when selection elevates it over an overlapping peer', () => {
1435
+ const { store, engine } = setup([
1436
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 }),
1437
+ createNode({ id: 'b', x: 0, y: 0, width: 100, height: 100 }),
1438
+ ]);
1439
+ // Same depth/zIndex: 'b' (later in array) paints on top by default.
1440
+ expect(engine.entityAtPoint({ x: 50, y: 50 })!.id).toBe('b');
1441
+ // Selecting 'a' boosts it above 'b'.
1442
+ store.trigger.select({ ids: ['a'] });
1443
+ expect(engine.entityAtPoint({ x: 50, y: 50 })!.id).toBe('a');
1444
+ });
1445
+ });
1446
+
1447
+ describe('engine configuration', () => {
1448
+ it('does not expose mutable label-anchor setters', () => {
1449
+ const { engine } = setup();
1450
+ expect('setLabelAnchor' in engine).toBe(false);
1451
+ });
1452
+
1453
+ it('resolves reconnectable from config predicates', () => {
1454
+ const { engine } = setup(
1455
+ [createNode({ id: 'a' }), createNode({ id: 'b' })],
1456
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
1457
+ {
1458
+ config: {
1459
+ reconnectable: (entity) => (entity as FlowNode | FlowEdge).type === 'edge' ? false : true,
1460
+ },
1461
+ },
1462
+ );
1463
+
1464
+ expect(engine.resolveEntityConfig('e1').reconnectable).toBe(false);
1465
+ });
1466
+ });
1467
+
1468
+
1469
+ // entitiesInRect — box select
1470
+
1471
+
1472
+ describe('engine.entitiesInRect', () => {
1473
+ it('finds nodes in rect', () => {
1474
+ const { engine } = setup([
1475
+ createNode({ id: 'a', x: 10, y: 10, width: 20, height: 20 }),
1476
+ createNode({ id: 'b', x: 500, y: 500, width: 20, height: 20 }),
1477
+ ]);
1478
+ const ids = engine.entitiesInRect({ x: 0, y: 0, width: 50, height: 50 });
1479
+ expect(ids).toContain('a');
1480
+ expect(ids).not.toContain('b');
1481
+ });
1482
+
1483
+ it('finds edges with labels in rect', () => {
1484
+ const { engine } = setup(
1485
+ [createNode({ id: 'a', x: 500, y: 500, width: 10, height: 10 }),
1486
+ createNode({ id: 'b', x: 600, y: 600, width: 10, height: 10 })],
1487
+ [createEdge({
1488
+ id: 'e1',
1489
+ sourceId: 'a',
1490
+ targetId: 'b',
1491
+ label: { x: 20, y: 20, width: 10, height: 10, data: {} },
1492
+ })],
1493
+ );
1494
+ const ids = engine.entitiesInRect({ x: 0, y: 0, width: 50, height: 50 });
1495
+ expect(ids).toContain('e1');
1496
+ expect(ids).not.toContain('a');
1497
+ });
1498
+
1499
+ it('ignores unlabeled edges whose path intersects the rect', () => {
1500
+ const { engine } = setup(
1501
+ [
1502
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
1503
+ createNode({ id: 'b', x: 200, y: 0, width: 50, height: 50 }),
1504
+ ],
1505
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
1506
+ { edgeRouter: straightRouter },
1507
+ );
1508
+
1509
+ const ids = engine.entitiesInRect({ x: 100, y: 20, width: 50, height: 20 });
1510
+ expect(ids).not.toContain('e1');
1511
+ });
1512
+ });
1513
+
1514
+
1515
+ // getSnapResult — snap lines via engine
1516
+
1517
+
1518
+ describe('engine.getSnapResult', () => {
1519
+ it('returns snap result when snap lines enabled and entities moved', () => {
1520
+ const storeWithSnap = createFlowStore(
1521
+ createGraph({
1522
+ nodes: [
1523
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50, dx: 2, dy: 0 }),
1524
+ createNode({ id: 'b', x: 100, y: 100, width: 50, height: 50 }),
1525
+ ],
1526
+ edges: [],
1527
+ }),
1528
+ { enableSnapLines: true, snapThreshold: 5 },
1529
+ );
1530
+ const snapEngine = createFlowEngine(storeWithSnap);
1531
+ storeWithSnap.trigger.select({ ids: ['a'] });
1532
+ storeWithSnap.trigger.updateViewportSize({ width: 1000, height: 1000 });
1533
+ // Drag node 'a' close to alignment with 'b'
1534
+ storeWithSnap.trigger.dragStart({ id: 'a' });
1535
+ storeWithSnap.trigger.dragMove({ dx: 101, dy: 100 });
1536
+
1537
+ const result = snapEngine.getSnapResult();
1538
+ // Should have snap data since 'a' is near alignment with 'b'
1539
+ expect(result).toBeDefined();
1540
+ });
1541
+
1542
+ it('bottom-snaps a larger selection when approaching from either side', () => {
1543
+ const getBottomSnap = (dy: number) => {
1544
+ const store = createFlowStore(
1545
+ createGraph({
1546
+ nodes: [
1547
+ createNode({
1548
+ id: 'moving',
1549
+ x: 0,
1550
+ y: 0,
1551
+ width: 200,
1552
+ height: 200,
1553
+ }),
1554
+ createNode({
1555
+ id: 'target',
1556
+ x: 50,
1557
+ y: 150,
1558
+ width: 50,
1559
+ height: 50,
1560
+ }),
1561
+ ],
1562
+ edges: [],
1563
+ }),
1564
+ { enableSnapLines: true, snapThreshold: 5 },
1565
+ );
1566
+ const engine = createFlowEngine(store);
1567
+ store.trigger.updateViewportSize({ width: 1000, height: 1000 });
1568
+ store.trigger.select({ ids: ['moving'] });
1569
+ store.trigger.dragStart({ id: 'moving' });
1570
+ store.trigger.dragMove({ dx: 0, dy });
1571
+ return engine.getSnapResult();
1572
+ };
1573
+
1574
+ expect(getBottomSnap(-2)?.snapRect.y).toBe(0);
1575
+ expect(getBottomSnap(2)?.snapRect.y).toBe(0);
1576
+ });
1577
+ });
1578
+
1579
+ describe('engine shape-aware endpoints', () => {
1580
+ it('uses registered shape anchors when no explicit ports are set', () => {
1581
+ registerShape('offset-anchor', {
1582
+ hitTest: () => true,
1583
+ edgeAnchor: (bounds, direction) => {
1584
+ switch (direction) {
1585
+ case 'left':
1586
+ return { x: bounds.x + 10, y: bounds.y + bounds.height / 2 };
1587
+ case 'right':
1588
+ return { x: bounds.x + bounds.width - 10, y: bounds.y + bounds.height / 2 };
1589
+ case 'top':
1590
+ return { x: bounds.x + bounds.width / 2, y: bounds.y + 10 };
1591
+ case 'bottom':
1592
+ return { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height - 10 };
1593
+ }
1594
+ },
1595
+ });
1596
+
1597
+ const { engine } = setup(
1598
+ [
1599
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50, shape: 'offset-anchor' }),
1600
+ createNode({ id: 'b', x: 200, y: 0, width: 100, height: 50, shape: 'offset-anchor' }),
1601
+ ],
1602
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
1603
+ );
1604
+
1605
+ const endpoints = engine.getEdgeEndpoints('e1');
1606
+ expect(endpoints).toEqual({
1607
+ source: { x: 90, y: 25 },
1608
+ target: { x: 210, y: 25 },
1609
+ });
1610
+ });
1611
+
1612
+ it('anchors edges on the facing sides for a vertical layout (source bottom → target top)', () => {
1613
+ // Regression: source/target fallback directions were swapped, so edges
1614
+ // left the far side of each node (top of source, bottom of target).
1615
+ const { engine } = setup(
1616
+ [
1617
+ createNode({ id: 'a', x: 0, y: 0, width: 150, height: 50 }),
1618
+ createNode({ id: 'b', x: 0, y: 200, width: 150, height: 50 }),
1619
+ ],
1620
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
1621
+ );
1622
+
1623
+ // Endpoint query: source on a's bottom edge, target on b's top edge.
1624
+ expect(engine.getEdgeEndpoints('e1')).toEqual({
1625
+ source: { x: 75, y: 50 },
1626
+ target: { x: 75, y: 200 },
1627
+ });
1628
+
1629
+ // Rendered path must agree with the endpoint query.
1630
+ const segments = engine.getAllEdgePaths().get('e1')!.segments;
1631
+ const points = segments[0].points;
1632
+ expect(points[0]).toEqual({ x: 75, y: 50 });
1633
+ expect(points[points.length - 1]).toEqual({ x: 75, y: 200 });
1634
+ });
1635
+
1636
+ it('uses explicit graph ports when sourcePort and targetPort are set', () => {
1637
+ const { engine } = setup(
1638
+ [
1639
+ createNode({
1640
+ id: 'a',
1641
+ x: 0,
1642
+ y: 0,
1643
+ width: 100,
1644
+ height: 50,
1645
+ ports: [
1646
+ { name: 'out', side: 'right', direction: 'out', x: 90, y: 20, width: 10, height: 10, data: {} },
1647
+ ],
1648
+ }),
1649
+ createNode({
1650
+ id: 'b',
1651
+ x: 200,
1652
+ y: 0,
1653
+ width: 100,
1654
+ height: 50,
1655
+ ports: [
1656
+ { name: 'in', side: 'left', direction: 'in', x: 0, y: 20, width: 10, height: 10, data: {} },
1657
+ ],
1658
+ }),
1659
+ ],
1660
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b', sourcePort: 'out', targetPort: 'in' })],
1661
+ );
1662
+
1663
+ const endpoints = engine.getEdgeEndpoints('e1');
1664
+ expect(endpoints).toEqual({
1665
+ source: { x: 95, y: 25 },
1666
+ target: { x: 205, y: 25 },
1667
+ });
1668
+ });
1669
+
1670
+ it('uses the closest graph ports when sourcePort and targetPort are wildcard', () => {
1671
+ const { engine } = setup(
1672
+ [
1673
+ createNode({
1674
+ id: 'a',
1675
+ x: 0,
1676
+ y: 0,
1677
+ width: 100,
1678
+ height: 80,
1679
+ ports: [
1680
+ { name: 'leftish', side: 'left', direction: 'out', x: 0, y: 35, width: 10, height: 10, data: {} },
1681
+ { name: 'rightish', side: 'right', direction: 'out', x: 90, y: 35, width: 10, height: 10, data: {} },
1682
+ ],
1683
+ }),
1684
+ createNode({
1685
+ id: 'b',
1686
+ x: 220,
1687
+ y: 0,
1688
+ width: 100,
1689
+ height: 80,
1690
+ ports: [
1691
+ { name: 'leftish', side: 'left', direction: 'in', x: 0, y: 35, width: 10, height: 10, data: {} },
1692
+ { name: 'rightish', side: 'right', direction: 'in', x: 90, y: 35, width: 10, height: 10, data: {} },
1693
+ ],
1694
+ }),
1695
+ ],
1696
+ [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b', sourcePort: '*', targetPort: '*' })],
1697
+ );
1698
+
1699
+ const endpoints = engine.getEdgeEndpoints('e1');
1700
+ expect(endpoints).toEqual({
1701
+ source: { x: 95, y: 40 },
1702
+ target: { x: 225, y: 40 },
1703
+ });
1704
+ });
1705
+ });
1706
+
1707
+ describe('engine renderable entities', () => {
1708
+ it('orders entities by depth, zIndex, and selected boost', () => {
1709
+ const parent = createNode({
1710
+ id: 'parent',
1711
+ x: 0,
1712
+ y: 0,
1713
+ width: 200,
1714
+ height: 200,
1715
+ zIndex: 100,
1716
+ });
1717
+ const siblingA = createNode({
1718
+ id: 'a',
1719
+ x: 0,
1720
+ y: 0,
1721
+ width: 50,
1722
+ height: 50,
1723
+ });
1724
+ const siblingB = createNode({
1725
+ id: 'b',
1726
+ x: 0,
1727
+ y: 0,
1728
+ width: 50,
1729
+ height: 50,
1730
+ zIndex: 1,
1731
+ });
1732
+ const siblingC = createNode({
1733
+ id: 'c',
1734
+ x: 0,
1735
+ y: 0,
1736
+ width: 50,
1737
+ height: 50,
1738
+ });
1739
+ const child = createNode({
1740
+ id: 'child',
1741
+ parentId: 'parent',
1742
+ x: 10,
1743
+ y: 10,
1744
+ width: 50,
1745
+ height: 50,
1746
+ });
1747
+ const { store, engine } = setup([child, siblingB, parent, siblingA, siblingC], []);
1748
+
1749
+ expect(engine.getEntityOrder().nodes.map((node) => node.id)).toEqual([
1750
+ 'a',
1751
+ 'c',
1752
+ 'b',
1753
+ 'parent',
1754
+ 'child',
1755
+ ]);
1756
+
1757
+ store.trigger.select({ ids: ['a'] });
1758
+ expect(engine.getEntityOrder().nodes.map((node) => node.id)).toEqual([
1759
+ 'c',
1760
+ 'a',
1761
+ 'b',
1762
+ 'parent',
1763
+ 'child',
1764
+ ]);
1765
+ });
1766
+
1767
+ it('can disable selected node elevation and opt into selected edge elevation', () => {
1768
+ const nodes = [
1769
+ createNode({ id: 'a', x: 0, y: 0 }),
1770
+ createNode({ id: 'b', x: 120, y: 0 }),
1771
+ createNode({ id: 'c', x: 240, y: 0 }),
1772
+ ];
1773
+ const edges = [
1774
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
1775
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
1776
+ ];
1777
+ const { store, engine } = setup(nodes, edges, {
1778
+ elevateOnSelect: { nodes: false, edges: true },
1779
+ });
1780
+
1781
+ store.trigger.select({ ids: ['a', 'ab'] });
1782
+
1783
+ expect(engine.getEntityOrder().nodes.map((node) => node.id)).toEqual([
1784
+ 'a',
1785
+ 'b',
1786
+ 'c',
1787
+ ]);
1788
+ expect(engine.getEntityOrder().edges.map((edge) => edge.id)).toEqual([
1789
+ 'bc',
1790
+ 'ab',
1791
+ ]);
1792
+ });
1793
+
1794
+ it('derives band-local paint z-indices', () => {
1795
+ const { store, engine } = setup([
1796
+ createNode({
1797
+ id: 'parent',
1798
+ x: 0,
1799
+ y: 0,
1800
+ width: 200,
1801
+ height: 200,
1802
+ group: { type: 'fixed' },
1803
+ }),
1804
+ createNode({ id: 'a', x: 0, y: 0 }),
1805
+ createNode({ id: 'b', x: 0, y: 0 }),
1806
+ createNode({ id: 'next', x: 0, y: 0, zIndex: 1 }),
1807
+ createNode({ id: 'child', parentId: 'parent', x: 10, y: 10 }),
1808
+ ]);
1809
+
1810
+ expect([...engine.getPaintZIndices().nodes.entries()]).toEqual([
1811
+ ['parent', 0],
1812
+ ['a', 2],
1813
+ ['b', 4],
1814
+ ['next', 6],
1815
+ ['child', 8],
1816
+ ]);
1817
+
1818
+ store.trigger.select({ ids: ['parent'] });
1819
+ const parentSelected = engine.getPaintZIndices();
1820
+ expect(parentSelected.nodes.get('parent')).toBe(5);
1821
+ expect(parentSelected.nodes.get('child')).toBe(8);
1822
+
1823
+ store.trigger.select({ ids: ['a'] });
1824
+ const siblingSelected = engine.getPaintZIndices();
1825
+ expect(siblingSelected.nodes.get('a')).toBe(5);
1826
+ expect(siblingSelected.nodes.get('b')).toBe(4);
1827
+ expect(siblingSelected.nodes.get('next')).toBe(6);
1828
+ });
1829
+
1830
+ it('respects node and edge selection elevation options', () => {
1831
+ const nodes = [
1832
+ createNode({ id: 'a', x: 0, y: 0 }),
1833
+ createNode({ id: 'b', x: 120, y: 0 }),
1834
+ createNode({ id: 'c', x: 240, y: 0 }),
1835
+ ];
1836
+ const edges = [
1837
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
1838
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
1839
+ ];
1840
+ const { store, engine } = setup(nodes, edges, {
1841
+ elevateOnSelect: { nodes: false },
1842
+ });
1843
+ const before = engine.getPaintZIndices();
1844
+
1845
+ store.trigger.select({ ids: ['a', 'ab'] });
1846
+ const after = engine.getPaintZIndices();
1847
+
1848
+ expect(after.nodes).toEqual(before.nodes);
1849
+ expect(after.edges).toEqual(before.edges);
1850
+ });
1851
+
1852
+ it('elevates only the selected edge when edge elevation is enabled', () => {
1853
+ const nodes = [
1854
+ createNode({ id: 'a', x: 0, y: 0 }),
1855
+ createNode({ id: 'b', x: 120, y: 0 }),
1856
+ createNode({ id: 'c', x: 240, y: 0 }),
1857
+ ];
1858
+ const edges = [
1859
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
1860
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
1861
+ ];
1862
+ const { store, engine } = setup(nodes, edges, {
1863
+ elevateOnSelect: { nodes: false, edges: true },
1864
+ });
1865
+ const before = engine.getPaintZIndices();
1866
+
1867
+ store.trigger.select({ ids: ['ab'] });
1868
+ const after = engine.getPaintZIndices();
1869
+
1870
+ expect(after.nodes).toEqual(before.nodes);
1871
+ expect(after.edges).toEqual(new Map([
1872
+ ['ab', 3],
1873
+ ['bc', 2],
1874
+ ]));
1875
+ });
1876
+
1877
+ it('culls nodes and edges to the viewport when requested', () => {
1878
+ const near = createNode({ id: 'near', x: 0, y: 0, width: 50, height: 50 });
1879
+ const far = createNode({ id: 'far', x: 1000, y: 1000, width: 50, height: 50 });
1880
+ const connecting = createEdge({ id: 'e1', sourceId: 'near', targetId: 'far' });
1881
+ const { store, engine } = setup([near, far], [connecting], { edgeRouter: straightRouter });
1882
+
1883
+ store.trigger.updateViewportSize({ width: 200, height: 200 });
1884
+ store.trigger.setViewport({ x: 0, y: 0, zoom: 1 });
1885
+
1886
+ const renderable = engine.getRenderableEntities({ cull: 'viewport' });
1887
+
1888
+ expect(renderable.nodes.map((node) => node.id)).toEqual(['near']);
1889
+ expect(renderable.edges.map((edge) => edge.id)).toEqual(['e1']);
1890
+ });
1891
+
1892
+ it('supports overscan when culling to the viewport', () => {
1893
+ const nearEdge = createNode({ id: 'a', x: 220, y: 0, width: 50, height: 50 });
1894
+ const { store, engine } = setup([nearEdge], []);
1895
+
1896
+ store.trigger.updateViewportSize({ width: 200, height: 200 });
1897
+ store.trigger.setViewport({ x: 0, y: 0, zoom: 1 });
1898
+
1899
+ expect(engine.getRenderableEntities({ cull: 'viewport' }).nodes).toHaveLength(0);
1900
+ expect(engine.getRenderableEntities({ cull: 'viewport', overscan: 40 }).nodes.map((node) => node.id)).toEqual(['a']);
1901
+ });
1902
+
1903
+ it('returns every entity when the whole graph fits inside the cull rect (fast path)', () => {
1904
+ // The graph is small and the viewport large, so getGraphBounds ⊆ cullRect
1905
+ // and the per-entity geometry tests are skipped. The result must still be
1906
+ // the full visible set — identical to what the strict cull would return.
1907
+ const a = createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 });
1908
+ const b = createNode({ id: 'b', x: 120, y: 0, width: 50, height: 50 });
1909
+ const connecting = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
1910
+ const { store, engine } = setup([a, b], [connecting], {
1911
+ edgeRouter: straightRouter,
1912
+ });
1913
+
1914
+ store.trigger.updateViewportSize({ width: 4000, height: 4000 });
1915
+ store.trigger.setViewport({ x: 0, y: 0, zoom: 1 });
1916
+
1917
+ const renderable = engine.getRenderableEntities({ cull: 'viewport' });
1918
+
1919
+ expect(renderable.nodes.map((node) => node.id)).toEqual(['a', 'b']);
1920
+ expect(renderable.edges.map((edge) => edge.id)).toEqual(['e1']);
1921
+ });
1922
+
1923
+ it('still applies visibility (not just geometry) on the fast path', () => {
1924
+ // A hidden node must be excluded even when the graph fits the cull rect:
1925
+ // the fast path skips geometry tests, not the visibility filter.
1926
+ const visible = createNode({ id: 'v', x: 0, y: 0, width: 50, height: 50 });
1927
+ const hidden = createNode({
1928
+ id: 'h',
1929
+ x: 120,
1930
+ y: 0,
1931
+ width: 50,
1932
+ height: 50,
1933
+ hidden: true,
1934
+ });
1935
+ const { store, engine } = setup([visible, hidden], []);
1936
+
1937
+ store.trigger.updateViewportSize({ width: 4000, height: 4000 });
1938
+ store.trigger.setViewport({ x: 0, y: 0, zoom: 1 });
1939
+
1940
+ expect(
1941
+ engine
1942
+ .getRenderableEntities({ cull: 'viewport' })
1943
+ .nodes.map((node) => node.id),
1944
+ ).toEqual(['v']);
1945
+ });
1946
+ });
1947
+
1948
+ it('keeps DOM-measured port endpoints attached during and after node movement', () => {
1949
+ const { store, engine } = setup([
1950
+ createNode({ id: 'a', x: 20, y: 30, width: 100, height: 50, ports: [
1951
+ createPort({ name: 'out', side: 'right', direction: 'out' }),
1952
+ ] }),
1953
+ createNode({ id: 'b', x: 300, width: 100, height: 50 }),
1954
+ ], [createEdge({ id: 'ab', sourceId: 'a', sourcePort: 'out', targetId: 'b' })]);
1955
+ store.trigger.updateMeasurements({ measurements: [
1956
+ { id: 'a:out', rect: { x: 110, y: 50, width: 12, height: 12 } },
1957
+ ] });
1958
+ expect(engine.getEdgeEndpoints('ab')?.source).toEqual({ x: 116, y: 56 });
1959
+ store.trigger.select({ ids: ['a'] });
1960
+ store.trigger.dragStart({ id: 'a' });
1961
+ store.trigger.dragMove({ dx: 40, dy: 20 });
1962
+ expect(engine.getEdgeEndpoints('ab')?.source).toEqual({ x: 156, y: 76 });
1963
+ store.trigger.dragEnd();
1964
+ expect(engine.getEdgeEndpoints('ab')?.source).toEqual({ x: 156, y: 76 });
1965
+ });