@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,728 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { createFlowStore } from './store';
3
+ import { createFlowEngine, type FlowEngineOptions } from './engine';
4
+ import { createNode, createEdge, createGraph } from './factories';
5
+ import { straightRouter, type EdgeRouter } from './routing';
6
+
7
+ // Mechanism tests for the engine's per-snapshot caches (graph-index.ts +
8
+ // edge-path/snap-offset memoization). These encode the invariants the
9
+ // frame-budget benchmark (bench/) relies on:
10
+ // - repeated queries within one store transition reuse work
11
+ // - any state change invalidates, so results are never stale
12
+
13
+ function makeFlow(engineOptions: FlowEngineOptions = {}) {
14
+ const store = createFlowStore(
15
+ createGraph({
16
+ nodes: [
17
+ createNode({ id: 'a', x: 0, y: 0 }),
18
+ createNode({ id: 'b', x: 300, y: 0 }),
19
+ ],
20
+ edges: [createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })],
21
+ }),
22
+ );
23
+ return { store, engine: createFlowEngine(store, engineOptions) };
24
+ }
25
+
26
+ describe('engine per-snapshot caches', () => {
27
+ it('getAllEdgePaths returns the same Map instance while the context is unchanged', () => {
28
+ const { engine } = makeFlow();
29
+ const first = engine.getAllEdgePaths();
30
+ const second = engine.getAllEdgePaths();
31
+ expect(second).toBe(first);
32
+ });
33
+
34
+ it('getAllEdgePaths recomputes after a state change and reflects it', () => {
35
+ const { store, engine } = makeFlow();
36
+ const before = engine.getAllEdgePaths();
37
+ const sourceXBefore = before.get('e1')!.segments[0].points[0].x;
38
+
39
+ store.trigger.select({ ids: ['a'] });
40
+ store.trigger.dragStart({ id: 'a' });
41
+ store.trigger.dragMove({ dx: 50, dy: 0 });
42
+
43
+ const after = engine.getAllEdgePaths();
44
+ expect(after).not.toBe(before);
45
+ const sourceXAfter = after.get('e1')!.segments[0].points[0].x;
46
+ expect(sourceXAfter).toBe(sourceXBefore + 50);
47
+ });
48
+
49
+ it('caches per graph object so preview graphs do not collide with the store graph', () => {
50
+ const { engine, store } = makeFlow();
51
+ const main = engine.getAllEdgePaths();
52
+
53
+ const previewGraph = {
54
+ ...store.getSnapshot().context.graph,
55
+ edges: [
56
+ ...store.getSnapshot().context.graph.edges,
57
+ createEdge({ id: 'preview', sourceId: 'b', targetId: 'a' }),
58
+ ],
59
+ };
60
+ const preview = engine.getAllEdgePaths({ graph: previewGraph });
61
+
62
+ expect(preview).not.toBe(main);
63
+ expect(preview.has('preview')).toBe(true);
64
+ expect(main.has('preview')).toBe(false);
65
+ // both stay cached independently
66
+ expect(engine.getAllEdgePaths()).toBe(main);
67
+ expect(engine.getAllEdgePaths({ graph: previewGraph })).toBe(preview);
68
+ });
69
+
70
+ it('setGraph rejects duplicate ids atomically', () => {
71
+ const { store, engine } = makeFlow();
72
+ const before = store.getSnapshot().context.graph;
73
+ const errors: unknown[] = [];
74
+ store.on('error', (event) => errors.push(event.error));
75
+ store.trigger.setGraph({
76
+ graph: createGraph({
77
+ nodes: [
78
+ createNode({ id: 'dup', x: 1 }),
79
+ createNode({ id: 'dup', x: 2 }),
80
+ ],
81
+ edges: [],
82
+ }),
83
+ });
84
+ expect(store.getSnapshot().context.graph).toBe(before);
85
+ expect(engine.getNode('dup')).toBeUndefined();
86
+ expect(errors).toHaveLength(1);
87
+ });
88
+
89
+ it('resolvedBounds stays consistent across repeated per-node queries during a drag', () => {
90
+ const { store, engine } = makeFlow();
91
+ store.trigger.select({ ids: ['a'] });
92
+ store.trigger.dragStart({ id: 'a' });
93
+ store.trigger.dragMove({ dx: 10, dy: 5 });
94
+
95
+ // NodeLayer pattern: one resolvedBounds(id) call per node per frame.
96
+ const first = engine.resolvedBounds('a');
97
+ const second = engine.resolvedBounds('a');
98
+ expect(second).toEqual(first);
99
+ expect(first.x).toBe(10);
100
+ expect(first.y).toBe(5);
101
+ });
102
+
103
+ it('reuses paint order across viewport-only transitions', () => {
104
+ const { store, engine } = makeFlow();
105
+ const before = engine.getEntityOrder();
106
+
107
+ store.trigger.setViewport({ x: 50, y: 25, zoom: 2 });
108
+
109
+ expect(engine.getEntityOrder()).toBe(before);
110
+ });
111
+
112
+ it('invalidates paint order when selection changes its elevation', () => {
113
+ const { store, engine } = makeFlow();
114
+ const before = engine.getEntityOrder();
115
+
116
+ store.trigger.select({ ids: ['a'] });
117
+
118
+ expect(engine.getEntityOrder()).not.toBe(before);
119
+ });
120
+
121
+ it('reuses base paint order when only selection elevation changes', () => {
122
+ const { store, engine } = makeFlow();
123
+ const before = engine.getEntityOrder({ elevateSelected: false });
124
+
125
+ store.trigger.select({ ids: ['a'] });
126
+
127
+ expect(engine.getEntityOrder({ elevateSelected: false })).toBe(before);
128
+ expect(
129
+ engine
130
+ .getEntityOrder({ elevateSelected: false })
131
+ .nodes.map((node) => node.id),
132
+ ).toEqual(['a', 'b']);
133
+ });
134
+
135
+ it('caches paint z-indices and changes only selected entities', () => {
136
+ const { store, engine } = makeFlow();
137
+ const before = engine.getPaintZIndices();
138
+
139
+ expect(engine.getPaintZIndices()).toBe(before);
140
+
141
+ store.trigger.select({ ids: ['a'] });
142
+ const after = engine.getPaintZIndices();
143
+
144
+ expect(after).not.toBe(before);
145
+ expect(after.nodes.get('a')).not.toBe(before.nodes.get('a'));
146
+ expect(after.nodes.get('b')).toBe(before.nodes.get('b'));
147
+ expect(after.edges.get('e1')).toBe(before.edges.get('e1'));
148
+ expect(engine.getPaintZIndices()).toBe(after);
149
+ });
150
+
151
+ it('updates paint z-index visibility when the viewport changes', () => {
152
+ const { store, engine } = makeFlow({
153
+ isVisible: (entity, viewport) =>
154
+ entity.id === (viewport.zoom === 1 ? 'a' : 'b'),
155
+ });
156
+ const before = engine.getPaintZIndices();
157
+
158
+ expect([...before.nodes.keys()]).toEqual(['a']);
159
+
160
+ store.trigger.setViewport({ x: 0, y: 0, zoom: 2 });
161
+
162
+ expect([...engine.getPaintZIndices().nodes.keys()]).toEqual(['b']);
163
+ });
164
+
165
+ it('omits an edge while it is being reconnected', () => {
166
+ const { store, engine } = makeFlow();
167
+ const before = engine.getPaintZIndices();
168
+ expect([...before.edges.keys()]).toEqual(['e1']);
169
+
170
+ store.trigger.connectionStart({
171
+ sourceId: 'a',
172
+ sourcePort: null,
173
+ position: { x: 0, y: 0 },
174
+ reconnectingEdgeId: 'e1',
175
+ });
176
+
177
+ expect(engine.getPaintZIndices().edges.has('e1')).toBe(false);
178
+ });
179
+
180
+ it('reuses graph bounds across viewport-only transitions', () => {
181
+ const { store, engine } = makeFlow();
182
+ const before = engine.getGraphBounds();
183
+
184
+ store.trigger.setViewport({ x: 50, y: 25, zoom: 2 });
185
+
186
+ expect(engine.getGraphBounds()).toBe(before);
187
+ });
188
+
189
+ it('invalidates graph bounds when geometry changes', () => {
190
+ const { store, engine } = makeFlow();
191
+ const before = engine.getGraphBounds();
192
+
193
+ store.trigger.updateNode({ id: 'b', patch: { x: 600 } });
194
+
195
+ const after = engine.getGraphBounds();
196
+ expect(after).not.toBe(before);
197
+ expect(after.width).toBeGreaterThan(before.width);
198
+ });
199
+ });
200
+
201
+ describe('per-edge path memoization across snapshots', () => {
202
+ it('does not reroute a custom router for viewport-only changes', () => {
203
+ const store = createFlowStore(
204
+ createGraph({
205
+ nodes: [
206
+ createNode({ id: 'a', x: 0, y: 0 }),
207
+ createNode({ id: 'b', x: 300, y: 0 }),
208
+ ],
209
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
210
+ }),
211
+ );
212
+ let calls = 0;
213
+ const router: EdgeRouter = (edge, context) => {
214
+ calls++;
215
+ return straightRouter(edge, context);
216
+ };
217
+ const engine = createFlowEngine(store, { edgeRouter: router });
218
+ const before = engine.getAllEdgePaths().get('ab');
219
+ expect(calls).toBe(1);
220
+
221
+ store.trigger.setViewport({ x: 100, y: 50, zoom: 1.5 });
222
+
223
+ expect(engine.getAllEdgePaths().get('ab')).toBe(before);
224
+ expect(calls).toBe(1);
225
+
226
+ store.trigger.panStart({ x: 0, y: 0 });
227
+ store.trigger.pan({ x: 40, y: 20 });
228
+ expect(engine.getAllEdgePaths().get('ab')).toBe(before);
229
+ store.trigger.panEnd();
230
+
231
+ expect(engine.getAllEdgePaths().get('ab')).toBe(before);
232
+ expect(calls).toBe(1);
233
+ });
234
+
235
+ it('does not reroute for viewport changes when resolveEdgeSourceBounds ignores the viewport', () => {
236
+ const store = createFlowStore(
237
+ createGraph({
238
+ nodes: [
239
+ createNode({ id: 'a', x: 0, y: 0 }),
240
+ createNode({ id: 'b', x: 300, y: 0 }),
241
+ ],
242
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
243
+ }),
244
+ );
245
+ let calls = 0;
246
+ const router: EdgeRouter = (edge, context) => {
247
+ calls++;
248
+ return straightRouter(edge, context);
249
+ };
250
+ const engine = createFlowEngine(store, {
251
+ edgeRouter: router,
252
+ resolveEdgeSourceBounds: ({ sourceBounds }) => sourceBounds,
253
+ });
254
+ const before = engine.getAllEdgePaths().get('ab');
255
+
256
+ store.trigger.setViewport({ x: 100, y: 50, zoom: 1.5 });
257
+ store.trigger.panStart({ x: 0, y: 0 });
258
+ store.trigger.pan({ x: 40, y: 20 });
259
+ store.trigger.panEnd();
260
+ engine.getRenderableEntities({ cull: 'viewport' });
261
+
262
+ expect(engine.getAllEdgePaths().get('ab')).toBe(before);
263
+ expect(calls).toBe(1);
264
+
265
+ store.trigger.select({ ids: ['a'] });
266
+ store.trigger.dragStart({ id: 'a' });
267
+ store.trigger.dragMove({ dx: 50, dy: 0 });
268
+ expect(engine.getAllEdgePaths().get('ab')).not.toBe(before);
269
+ expect(calls).toBe(2);
270
+ });
271
+
272
+ it('reroutes for viewport changes when resolveEdgeSourceBounds reads the viewport', () => {
273
+ const store = createFlowStore(
274
+ createGraph({
275
+ nodes: [
276
+ createNode({ id: 'a', x: 0, y: 0 }),
277
+ createNode({ id: 'b', x: 300, y: 0 }),
278
+ ],
279
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
280
+ }),
281
+ );
282
+ const engine = createFlowEngine(store, {
283
+ resolveEdgeSourceBounds: ({ context, sourceBounds }) => ({
284
+ ...sourceBounds,
285
+ y: sourceBounds.y + context.viewport.x,
286
+ }),
287
+ });
288
+ const before = engine.getAllEdgePaths().get('ab')!;
289
+
290
+ store.trigger.setViewport({ x: 100, y: 0, zoom: 1 });
291
+
292
+ const after = engine.getAllEdgePaths().get('ab')!;
293
+ expect(after).not.toBe(before);
294
+ expect(after.segments[0].points[0].y).toBe(
295
+ before.segments[0].points[0].y + 100,
296
+ );
297
+ });
298
+
299
+ function makeRouterFlow() {
300
+ return createFlowStore(
301
+ createGraph({
302
+ nodes: [
303
+ createNode({ id: 'a', x: 0, y: 0, data: { color: 'gray' } }),
304
+ createNode({ id: 'b', x: 300, y: 0 }),
305
+ createNode({ id: 'obstacle', x: 150, y: 200 }),
306
+ ],
307
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
308
+ }),
309
+ );
310
+ }
311
+
312
+ it('reroutes a custom router when a graph field it read changes', () => {
313
+ const store = makeRouterFlow();
314
+ let calls = 0;
315
+ const engine = createFlowEngine(store, {
316
+ edgeRouter: (edge, context) => {
317
+ calls++;
318
+ void context.graph.nodes.find((node) => node.id === 'obstacle')?.x;
319
+ return straightRouter(edge, context);
320
+ },
321
+ });
322
+ engine.getAllEdgePaths();
323
+
324
+ store.trigger.updateNode({ id: 'obstacle', patch: { x: 200 } });
325
+ engine.getAllEdgePaths();
326
+
327
+ expect(calls).toBe(2);
328
+ });
329
+
330
+ it('reroutes a custom router when obstacles it read change', () => {
331
+ const store = makeRouterFlow();
332
+ let calls = 0;
333
+ const engine = createFlowEngine(store, {
334
+ edgeRouter: (edge, context) => {
335
+ calls++;
336
+ void context.obstacles;
337
+ return straightRouter(edge, context);
338
+ },
339
+ });
340
+ engine.getAllEdgePaths();
341
+
342
+ store.trigger.updateNode({ id: 'obstacle', patch: { x: 200 } });
343
+ engine.getAllEdgePaths();
344
+
345
+ expect(calls).toBe(2);
346
+ });
347
+
348
+ it('reroutes a custom router when bounds it looked up change', () => {
349
+ const store = makeRouterFlow();
350
+ let calls = 0;
351
+ const engine = createFlowEngine(store, {
352
+ edgeRouter: (edge, context) => {
353
+ calls++;
354
+ void context.getEntityBounds('obstacle');
355
+ return straightRouter(edge, context);
356
+ },
357
+ });
358
+ engine.getAllEdgePaths();
359
+
360
+ store.trigger.updateNode({ id: 'obstacle', patch: { x: 200 } });
361
+ engine.getAllEdgePaths();
362
+
363
+ expect(calls).toBe(2);
364
+ });
365
+
366
+ it('reuses custom router paths for graph changes the router never read', () => {
367
+ const store = makeRouterFlow();
368
+ let calls = 0;
369
+ const engine = createFlowEngine(store, {
370
+ edgeRouter: (edge, context) => {
371
+ calls++;
372
+ void context.obstacles;
373
+ void context.graph.nodes.map((node) => node.x);
374
+ return straightRouter(edge, context);
375
+ },
376
+ });
377
+ const before = engine.getAllEdgePaths().get('ab');
378
+
379
+ store.trigger.updateNode({ id: 'a', patch: { data: { color: 'red' } } });
380
+ expect(engine.getAllEdgePaths().get('ab')).toBe(before);
381
+ expect(calls).toBe(1);
382
+
383
+ store.trigger.updateNode({ id: 'b', patch: { x: 320 } });
384
+ expect(engine.getAllEdgePaths().get('ab')).not.toBe(before);
385
+ expect(calls).toBe(2);
386
+ });
387
+
388
+ it('reroutes a custom router when routing state outside the graph changes', () => {
389
+ const store = makeRouterFlow();
390
+ // Mirrors a router that reads appearance-region measurements through a
391
+ // closure and a routing policy flipped outside Flow.
392
+ let useCaption = false;
393
+ const engine = createFlowEngine(store, {
394
+ edgeRouter: (edge, context) => {
395
+ const caption = store.getSnapshot().context.measurements.get('a:caption');
396
+ const path = straightRouter(edge, context);
397
+ if (!useCaption || !caption) return path;
398
+ return { ...path, points: [path.points[0]!, { x: caption.x, y: caption.y }] };
399
+ },
400
+ });
401
+ store.trigger.updateMeasurements({
402
+ measurements: [{ id: 'a:caption', rect: { x: 300, y: 240, width: 80, height: 20 } }],
403
+ });
404
+ const before = engine.getAllEdgePaths().get('ab')!.segments[0]!.points;
405
+
406
+ useCaption = true;
407
+ store.trigger.updateMeasurements({
408
+ measurements: [{ id: 'unrelated', rect: { x: 0, y: 0, width: 10, height: 10 } }],
409
+ });
410
+ const after = engine.getAllEdgePaths().get('ab')!.segments[0]!.points;
411
+
412
+ expect(after).not.toEqual(before);
413
+ expect(after.at(-1)).toEqual({ x: 300, y: 240 });
414
+ });
415
+
416
+ it('reuses custom router paths across a graph replacement the router never read', () => {
417
+ const store = makeRouterFlow();
418
+ let calls = 0;
419
+ const engine = createFlowEngine(store, {
420
+ edgeRouter: (edge, context) => {
421
+ calls++;
422
+ void context.graph.nodes.map((node) => node.x);
423
+ return straightRouter(edge, context);
424
+ },
425
+ });
426
+ engine.getAllEdgePaths();
427
+
428
+ // Replacing the graph also resets idle layout offsets to a new empty map.
429
+ const { graph } = store.getSnapshot().context;
430
+ store.trigger.replaceGraph({
431
+ graph: {
432
+ ...graph,
433
+ nodes: graph.nodes.map((node) =>
434
+ node.id === 'a' ? { ...node, data: { color: 'red' } } : node,
435
+ ),
436
+ },
437
+ });
438
+ engine.getAllEdgePaths();
439
+
440
+ expect(calls).toBe(1);
441
+ });
442
+
443
+ it("never replays a custom router with 'snapshot' dependencies", () => {
444
+ const store = makeRouterFlow();
445
+ let calls = 0;
446
+ const engine = createFlowEngine(store, {
447
+ edgeRouterDependencies: 'snapshot',
448
+ edgeRouter: (edge, context) => {
449
+ calls++;
450
+ return straightRouter(edge, context);
451
+ },
452
+ });
453
+ engine.getAllEdgePaths();
454
+
455
+ store.trigger.updateNode({ id: 'a', patch: { data: { color: 'red' } } });
456
+ engine.getAllEdgePaths();
457
+ expect(calls).toBe(2);
458
+ });
459
+
460
+ it('reroutes a custom router that reads the changed data field', () => {
461
+ const store = makeRouterFlow();
462
+ let calls = 0;
463
+ const engine = createFlowEngine(store, {
464
+ edgeRouter: (edge, context) => {
465
+ calls++;
466
+ void (context.graph.nodes[0]?.data as { color?: string }).color;
467
+ return straightRouter(edge, context);
468
+ },
469
+ });
470
+ engine.getAllEdgePaths();
471
+
472
+ store.trigger.updateNode({ id: 'a', patch: { data: { color: 'red' } } });
473
+ engine.getAllEdgePaths();
474
+
475
+ expect(calls).toBe(2);
476
+ });
477
+
478
+ it('reroutes when a graph value the router returned unread changes', () => {
479
+ const store = createFlowStore(
480
+ createGraph({
481
+ nodes: [
482
+ createNode({ id: 'a', x: 0, y: 0 }),
483
+ createNode({ id: 'b', x: 300, y: 0 }),
484
+ ],
485
+ edges: [
486
+ createEdge({
487
+ id: 'ab',
488
+ sourceId: 'a',
489
+ targetId: 'b',
490
+ data: { points: [{ x: 0, y: 0 }, { x: 300, y: 0 }] },
491
+ }),
492
+ ],
493
+ }),
494
+ );
495
+ const engine = createFlowEngine(store, {
496
+ edgeRouter: (edge, context) => ({
497
+ points: (edge.data as { points: { x: number; y: number }[] }).points,
498
+ sourceDirection: context.sourceDirection,
499
+ targetDirection: context.targetDirection,
500
+ }),
501
+ });
502
+ const before = engine.getAllEdgePaths().get('ab')!.segments[0]!;
503
+ expect(before.points).toBe(store.getSnapshot().context.graph.edges[0]!.data!.points);
504
+
505
+ store.trigger.updateEdge({
506
+ id: 'ab',
507
+ patch: { data: { points: [{ x: 0, y: 0 }, { x: 150, y: 90 }, { x: 300, y: 0 }] } },
508
+ });
509
+
510
+ expect(engine.getAllEdgePaths().get('ab')!.segments[0]!.points).toHaveLength(3);
511
+ });
512
+
513
+ it('reroutes when an own-property check the router made changes', () => {
514
+ const store = makeRouterFlow();
515
+ let calls = 0;
516
+ const engine = createFlowEngine(store, {
517
+ edgeRouter: (edge, context) => {
518
+ calls++;
519
+ void Object.hasOwn(context.graph.nodes[0]!.data as object, 'blocked');
520
+ return straightRouter(edge, context);
521
+ },
522
+ });
523
+ engine.getAllEdgePaths();
524
+
525
+ store.trigger.updateNode({ id: 'a', patch: { data: { color: 'gray', blocked: true } } });
526
+ engine.getAllEdgePaths();
527
+
528
+ expect(calls).toBe(2);
529
+ });
530
+
531
+ it('tracks a replacement router after a previous one rejected tracking', () => {
532
+ const store = makeRouterFlow();
533
+ const engine = createFlowEngine(store, {
534
+ edgeRouter: (edge, context) => {
535
+ Object.freeze(context.graph);
536
+ return straightRouter(edge, context);
537
+ },
538
+ });
539
+ engine.getAllEdgePaths();
540
+
541
+ let calls = 0;
542
+ engine.setOptions({
543
+ edgeRouter: (edge, context) => {
544
+ calls++;
545
+ return straightRouter(edge, context);
546
+ },
547
+ });
548
+ engine.getAllEdgePaths();
549
+ store.trigger.updateNode({ id: 'a', patch: { data: { color: 'red' } } });
550
+ engine.getAllEdgePaths();
551
+
552
+ expect(calls).toBe(1);
553
+ });
554
+
555
+ it('reroutes every edge of a pass when any one call changes', () => {
556
+ const store = createFlowStore(
557
+ createGraph({
558
+ nodes: [
559
+ createNode({ id: 'a', x: 0, y: 0 }),
560
+ createNode({ id: 'b', x: 300, y: 0 }),
561
+ createNode({ id: 'c', x: 600, y: 0 }),
562
+ ],
563
+ edges: [
564
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
565
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
566
+ ],
567
+ }),
568
+ );
569
+ const routed: string[] = [];
570
+ const engine = createFlowEngine(store, {
571
+ edgeRouter: (edge, context) => {
572
+ routed.push(edge.id);
573
+ return straightRouter(edge, context);
574
+ },
575
+ });
576
+ engine.getAllEdgePaths();
577
+ routed.length = 0;
578
+
579
+ store.trigger.updateNode({ id: 'c', patch: { x: 650 } });
580
+ engine.getAllEdgePaths();
581
+
582
+ expect(routed).toEqual(['ab', 'bc']);
583
+ });
584
+
585
+ it('stops tracking a router that rejects the tracked graph view', () => {
586
+ const store = makeRouterFlow();
587
+ let calls = 0;
588
+ const engine = createFlowEngine(store, {
589
+ edgeRouter: (edge, context) => {
590
+ calls++;
591
+ // Freezing the view fails; freezing the stored graph does not.
592
+ Object.freeze(context.graph);
593
+ return straightRouter(edge, context);
594
+ },
595
+ });
596
+ expect(engine.getAllEdgePaths().get('ab')).toBeDefined();
597
+
598
+ store.trigger.updateNode({ id: 'a', patch: { data: { color: 'red' } } });
599
+ engine.getAllEdgePaths();
600
+ expect(calls).toBe(3);
601
+ });
602
+
603
+ it('reroutes only incident edges for endpoint-dependent custom routers', () => {
604
+ const store = createFlowStore(
605
+ createGraph({
606
+ nodes: [
607
+ createNode({ id: 'a', x: 0, y: 0 }),
608
+ createNode({ id: 'b', x: 300, y: 0 }),
609
+ createNode({ id: 'c', x: 600, y: 0 }),
610
+ createNode({ id: 'd', x: 900, y: 0 }),
611
+ ],
612
+ edges: [
613
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
614
+ createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
615
+ createEdge({ id: 'cd', sourceId: 'c', targetId: 'd' }),
616
+ ],
617
+ }),
618
+ );
619
+ let calls = 0;
620
+ const engine = createFlowEngine(store, {
621
+ edgeRouter: (edge, context) => {
622
+ calls++;
623
+ return straightRouter(edge, context);
624
+ },
625
+ edgeRouterDependencies: 'endpoints',
626
+ });
627
+ const before = engine.getAllEdgePaths();
628
+ calls = 0;
629
+
630
+ store.trigger.updateNode({ id: 'a', patch: { x: 50 } });
631
+ const after = engine.getAllEdgePaths();
632
+
633
+ expect(calls).toBe(1);
634
+ expect(after.get('ab')).not.toBe(before.get('ab'));
635
+ expect(after.get('bc')).toBe(before.get('bc'));
636
+ expect(after.get('cd')).toBe(before.get('cd'));
637
+ });
638
+
639
+ it('reuses path objects for edges whose endpoints did not move', () => {
640
+ const store = createFlowStore(
641
+ createGraph({
642
+ nodes: [
643
+ createNode({ id: 'a', x: 0, y: 0 }),
644
+ createNode({ id: 'b', x: 300, y: 0 }),
645
+ createNode({ id: 'c', x: 600, y: 0 }),
646
+ createNode({ id: 'd', x: 900, y: 0 }),
647
+ ],
648
+ edges: [
649
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
650
+ createEdge({ id: 'cd', sourceId: 'c', targetId: 'd' }),
651
+ ],
652
+ }),
653
+ );
654
+ const engine = createFlowEngine(store);
655
+
656
+ store.trigger.select({ ids: ['a'] });
657
+ store.trigger.dragStart({ id: 'a' });
658
+ store.trigger.dragMove({ dx: 10, dy: 0 });
659
+ const first = engine.getAllEdgePaths();
660
+
661
+ store.trigger.dragMove({ dx: 10, dy: 0 });
662
+ const second = engine.getAllEdgePaths();
663
+
664
+ // 'ab' depends on the dragged node — must be recomputed and move
665
+ expect(second.get('ab')).not.toBe(first.get('ab'));
666
+ expect(second.get('ab')!.segments[0].points[0].x).toBe(
667
+ first.get('ab')!.segments[0].points[0].x + 10,
668
+ );
669
+ // 'cd' is untouched — the exact same path object must be reused
670
+ expect(second.get('cd')).toBe(first.get('cd'));
671
+ });
672
+
673
+ it('invalidates untouched edges when the selection or snap state changes', () => {
674
+ const store = createFlowStore(
675
+ createGraph({
676
+ nodes: [
677
+ createNode({ id: 'a', x: 0, y: 0 }),
678
+ createNode({ id: 'b', x: 300, y: 0 }),
679
+ ],
680
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
681
+ }),
682
+ );
683
+ const engine = createFlowEngine(store);
684
+ const first = engine.getAllEdgePaths();
685
+
686
+ // selection identity changes the visual-bounds inputs → no stale reuse
687
+ store.trigger.select({ ids: ['a'] });
688
+ store.trigger.dragStart({ id: 'a' });
689
+ store.trigger.dragMove({ dx: 5, dy: 0 });
690
+ const second = engine.getAllEdgePaths();
691
+ expect(second.get('ab')!.segments[0].points[0].x).toBe(
692
+ first.get('ab')!.segments[0].points[0].x + 5,
693
+ );
694
+ });
695
+
696
+ it('getRenderableEntities without a viewport cull does not route edges', () => {
697
+ let routes = 0;
698
+ const countingRouter: EdgeRouter = (edge, context) => {
699
+ routes++;
700
+ return straightRouter(edge, context);
701
+ };
702
+ const { engine } = makeFlow({ edgeRouter: countingRouter });
703
+
704
+ const renderable = engine.getRenderableEntities({ cull: 'none' });
705
+ expect(renderable.edges.map((edge) => edge.id)).toEqual(['e1']);
706
+ expect(routes).toBe(0);
707
+
708
+ engine.getAllEdgePaths();
709
+ expect(routes).toBe(1);
710
+ });
711
+
712
+ it('keeps a recomputed edge path object when its geometry is unchanged', () => {
713
+ const { store, engine } = makeFlow();
714
+ const before = engine.getAllEdgePaths().get('e1');
715
+ const graph = store.getSnapshot().context.graph;
716
+
717
+ store.trigger.setGraph({
718
+ graph: {
719
+ ...graph,
720
+ nodes: graph.nodes.map((node) =>
721
+ node.id === 'a' ? { ...node, data: { color: 'red' } } : node,
722
+ ),
723
+ },
724
+ });
725
+
726
+ expect(engine.getAllEdgePaths().get('e1')).toBe(before);
727
+ });
728
+ });