@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,1023 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { createFlowStore } from './store';
3
+ import { createFlowEngine } from './engine';
4
+ import { createEntityChangeNotifier } from './entity-changes';
5
+ import { createNode, createEdge, createGraph } from './factories';
6
+ import type { FlowNode } from './types';
7
+
8
+ // Group hierarchy through the store + engine: derived parents are a pure
9
+ // function of their children, so containment cannot drift and sizing is
10
+ // reversible — the two failure modes React Flow's expandParent is known for.
11
+
12
+ function makeRig(nodes: FlowNode[], edges = [] as ReturnType<typeof createEdge>[]) {
13
+ const store = createFlowStore(createGraph({ nodes, edges }));
14
+ const engine = createFlowEngine(store);
15
+ return { store, engine };
16
+ }
17
+
18
+ const group = (id: string, extra: Partial<FlowNode> = {}): FlowNode =>
19
+ createNode({
20
+ id,
21
+ width: 120,
22
+ height: 40,
23
+ group: { type: 'derived', padding: 16 },
24
+ ...extra,
25
+ });
26
+
27
+ describe('derived group bounds (engine)', () => {
28
+ it('always contain children plus padding and the content block', () => {
29
+ const { engine } = makeRig([
30
+ group('g'),
31
+ { ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }), parentId: 'g' },
32
+ { ...createNode({ id: 'b', x: 300, y: 320, width: 100, height: 50 }), parentId: 'g' },
33
+ ]);
34
+
35
+ const bounds = engine.resolvedBounds('g');
36
+ expect(bounds).toEqual({
37
+ x: 84, // child left 100 - padding 16
38
+ y: 144, // child top 200 - padding 16 - content 40
39
+ width: 332, // children span 300 + 2*16
40
+ height: 242, // content 40 + children span 170 + 2*16
41
+ });
42
+
43
+ const geometry = engine.getGroupGeometry('g')!;
44
+ expect(geometry.contentBounds.height).toBe(40);
45
+ expect(geometry.childArea).toEqual({ x: 84, y: 184, width: 332, height: 202 });
46
+ });
47
+
48
+ it('follows children live during a drag and is fully reversible (no ratchet)', () => {
49
+ const { store, engine } = makeRig([
50
+ group('g'),
51
+ { ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }), parentId: 'g' },
52
+ ]);
53
+ const before = engine.resolvedBounds('g');
54
+
55
+ store.trigger.select({ ids: ['a'] });
56
+ store.trigger.dragStart({ id: 'a' });
57
+ store.trigger.dragMove({ dx: 400, dy: 0 });
58
+ const during = engine.resolvedBounds('g');
59
+ expect(during.x).toBe(before.x + 400); // single child: bounds track it
60
+
61
+ // back to the start, commit — bit-identical bounds
62
+ store.trigger.dragMove({ dx: -400, dy: 0 });
63
+ store.trigger.dragEnd();
64
+ store.trigger.commitDrag();
65
+ expect(engine.resolvedBounds('g')).toEqual(before);
66
+ });
67
+
68
+ it('grows live while a child is being resized (before the resize commits)', () => {
69
+ const { store, engine } = makeRig([
70
+ group('g'),
71
+ { ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }), parentId: 'g' },
72
+ ]);
73
+ const before = engine.resolvedBounds('g');
74
+
75
+ // Grab the child's bottom-right handle and drag it 300px down+right.
76
+ store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
77
+ store.trigger.resizeMove({ x: 100 + 100 + 300, y: 200 + 50 + 300 });
78
+
79
+ // Mid-gesture (canvasState === 'resizing'), the parent must already reflect
80
+ // the child's LIVE bounds, not its committed resolvedBounds.
81
+ const during = engine.resolvedBounds('g');
82
+ expect(during.width).toBe(before.width + 300);
83
+ expect(during.height).toBe(before.height + 300);
84
+
85
+ // Commit and confirm the committed geometry matches the live geometry.
86
+ store.trigger.resizeEnd();
87
+ expect(engine.resolvedBounds('g')).toEqual(during);
88
+ });
89
+
90
+ it('derives recursively across levels (grandchild stretches grandparent)', () => {
91
+ const { store, engine } = makeRig([
92
+ group('outer', { width: 140 }),
93
+ { ...group('inner'), parentId: 'outer' },
94
+ { ...createNode({ id: 'a', x: 500, y: 500, width: 80, height: 40 }), parentId: 'inner' },
95
+ { ...createNode({ id: 'b', x: 620, y: 500, width: 80, height: 40 }), parentId: 'inner' },
96
+ ]);
97
+
98
+ const outerBefore = engine.resolvedBounds('outer');
99
+ expect(outerBefore.width).toBeGreaterThan(0);
100
+
101
+ // moving one of two grandchildren stretches inner AND outer
102
+ store.trigger.select({ ids: ['b'] });
103
+ store.trigger.dragStart({ id: 'b' });
104
+ store.trigger.dragMove({ dx: 0, dy: 300 });
105
+
106
+ const outerDuring = engine.resolvedBounds('outer');
107
+ expect(outerDuring.height).toBe(outerBefore.height + 300);
108
+
109
+ // single-child symmetry: a one-child chain translates instead of growing
110
+ store.trigger.dragMove({ dx: 0, dy: -300 });
111
+ store.trigger.dragEnd();
112
+ store.trigger.commitDrag();
113
+ expect(engine.resolvedBounds('outer')).toEqual(outerBefore);
114
+ });
115
+
116
+ it('dragging the group moves its children (derived position is exact)', () => {
117
+ const { store, engine } = makeRig([
118
+ group('g'),
119
+ { ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }), parentId: 'g' },
120
+ ]);
121
+ const before = engine.resolvedBounds('g');
122
+
123
+ store.trigger.select({ ids: ['g'] });
124
+ store.trigger.dragStart({ id: 'g' });
125
+ store.trigger.dragMove({ dx: 50, dy: 25 });
126
+ store.trigger.dragEnd();
127
+ store.trigger.commitDrag();
128
+
129
+ const after = engine.resolvedBounds('g');
130
+ expect(after.x).toBe(before.x + 50);
131
+ expect(after.y).toBe(before.y + 25);
132
+ const a = store.getSnapshot().context.graph.nodes.find((n) => n.id === 'a')!;
133
+ expect(a.x).toBe(150);
134
+ expect(a.y).toBe(225);
135
+ });
136
+
137
+ it('snaps the full derived group while dragging and on commit', () => {
138
+ const { store, engine } = makeRig(
139
+ [
140
+ group('g'),
141
+ {
142
+ ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }),
143
+ parentId: 'g',
144
+ },
145
+ {
146
+ ...createNode({ id: 'b', x: 220, y: 200, width: 100, height: 50 }),
147
+ parentId: 'g',
148
+ },
149
+ createNode({ id: 'target', x: 500, y: 150, width: 100, height: 50 }),
150
+ ],
151
+ [
152
+ createEdge({
153
+ id: 'ab',
154
+ sourceId: 'a',
155
+ targetId: 'b',
156
+ waypoints: [{ x: 190, y: 260 }],
157
+ label: { x: 170, y: 280, width: 60, height: 20 },
158
+ }),
159
+ ],
160
+ );
161
+ store.trigger.setSnapLines({ enabled: true, threshold: 8 });
162
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
163
+
164
+ store.trigger.select({ ids: ['g'] });
165
+ store.trigger.dragStart({ id: 'g' });
166
+ // Derived bounds begin at y=144. The raw drag puts their top edge at 147,
167
+ // close enough to snap to the target's y=150. The stored parent y=0 is
168
+ // intentionally nowhere near that guide.
169
+ store.trigger.dragMove({ dx: 0, dy: 3 });
170
+
171
+ const snapResult = engine.getSnapResult();
172
+ expect(snapResult?.snapRect.y).toBe(150);
173
+ expect(engine.visualBounds('g').y).toBe(150);
174
+ expect(engine.visualBounds('a').y).toBe(206);
175
+ expect(engine.getAllEdgePaths().get('ab')?.labelBounds?.y).toBe(286);
176
+ expect(
177
+ engine
178
+ .getAllEdgePaths()
179
+ .get('ab')
180
+ ?.segments.flatMap((segment) => segment.points),
181
+ ).toContainEqual({ x: 190, y: 266 });
182
+
183
+ store.trigger.dragEnd();
184
+ store.trigger.commitDrag({
185
+ snapOffset: {
186
+ x: snapResult!.snapRect.x - 84,
187
+ y: snapResult!.snapRect.y - 147,
188
+ },
189
+ });
190
+
191
+ expect(engine.resolvedBounds('g').y).toBe(150);
192
+ expect(store.getSnapshot().context.graph.nodes.find((n) => n.id === 'a')!.y)
193
+ .toBe(206);
194
+ expect(store.getSnapshot().context.graph.edges[0].label?.y).toBe(286);
195
+ expect(store.getSnapshot().context.graph.edges[0].waypoints).toEqual([
196
+ { x: 190, y: 266 },
197
+ ]);
198
+ });
199
+
200
+ it('applies snap-line correction to enclosing derived groups', () => {
201
+ const { store, engine } = makeRig([
202
+ group('outer'),
203
+ { ...group('inner'), parentId: 'outer' },
204
+ {
205
+ ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }),
206
+ parentId: 'inner',
207
+ },
208
+ {
209
+ ...createNode({ id: 'static', x: 300, y: 300, width: 100, height: 50 }),
210
+ parentId: 'outer',
211
+ },
212
+ createNode({ id: 'target', x: 500, y: 150, width: 100, height: 50 }),
213
+ ]);
214
+ store.trigger.setSnapLines({ enabled: true, threshold: 8 });
215
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
216
+
217
+ store.trigger.select({ ids: ['inner'] });
218
+ store.trigger.dragStart({ id: 'inner' });
219
+ store.trigger.dragMove({ dx: 0, dy: 3 });
220
+
221
+ expect(engine.getSnapResult()?.snapRect.y).toBe(150);
222
+ expect(engine.visualBounds('inner').y).toBe(150);
223
+ const previewOuter = engine.visualBounds('outer');
224
+
225
+ store.trigger.dragEnd();
226
+ store.trigger.commitDrag({ snapOffset: { x: 0, y: 3 } });
227
+ expect(engine.visualBounds('outer')).toEqual(previewOuter);
228
+ });
229
+
230
+ it('does not snap a child against its own derived ancestors', () => {
231
+ const { store, engine } = makeRig([
232
+ group('g'),
233
+ {
234
+ ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }),
235
+ parentId: 'g',
236
+ },
237
+ createNode({ id: 'target', x: 253, y: 200, width: 100, height: 50 }),
238
+ ]);
239
+ store.trigger.setSnapLines({ enabled: true, threshold: 8 });
240
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
241
+
242
+ store.trigger.select({ ids: ['a'] });
243
+ store.trigger.dragStart({ id: 'a' });
244
+ store.trigger.dragMove({ dx: 50, dy: 0 });
245
+
246
+ expect(engine.getSnapResult()?.snapRect.x).toBe(153);
247
+ expect(engine.visualBounds('a').x).toBe(153);
248
+ });
249
+
250
+ it('snaps a child against its fixed ancestor', () => {
251
+ const { store, engine } = makeRig([
252
+ createNode({
253
+ id: 'g',
254
+ x: 100,
255
+ y: 100,
256
+ width: 300,
257
+ height: 200,
258
+ group: { type: 'fixed' },
259
+ }),
260
+ {
261
+ ...createNode({ id: 'a', x: 110, y: 160, width: 100, height: 50 }),
262
+ parentId: 'g',
263
+ },
264
+ ]);
265
+ store.trigger.setSnapLines({ enabled: true, threshold: 5 });
266
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
267
+
268
+ store.trigger.select({ ids: ['a'] });
269
+ store.trigger.dragStart({ id: 'a' });
270
+ store.trigger.dragMove({ dx: -7, dy: 0 });
271
+
272
+ expect(engine.getSnapResult()?.snapRect.x).toBe(100);
273
+ expect(engine.visualBounds('a').x).toBe(100);
274
+ });
275
+
276
+ it('does not grid-shift owned waypoints during a label-only drag', () => {
277
+ const nodes = [
278
+ createNode({
279
+ id: 'g',
280
+ x: 0,
281
+ y: 0,
282
+ width: 400,
283
+ height: 240,
284
+ group: { type: 'fixed' },
285
+ }),
286
+ {
287
+ ...createNode({ id: 'a', x: 50, y: 100, width: 80, height: 40 }),
288
+ parentId: 'g',
289
+ },
290
+ {
291
+ ...createNode({ id: 'b', x: 250, y: 100, width: 80, height: 40 }),
292
+ parentId: 'g',
293
+ },
294
+ ];
295
+ const edges = [
296
+ createEdge({
297
+ id: 'ab',
298
+ sourceId: 'a',
299
+ targetId: 'b',
300
+ waypoints: [{ x: 170, y: 173 }],
301
+ labelAnchor: 'fixed',
302
+ label: { x: 140, y: 150, width: 60, height: 20 },
303
+ }),
304
+ ];
305
+ const store = createFlowStore(createGraph({ nodes, edges }), {
306
+ snapToGrid: true,
307
+ snapGrid: [10, 10],
308
+ });
309
+ const engine = createFlowEngine(store);
310
+
311
+ store.trigger.select({ ids: ['ab'] });
312
+ store.trigger.dragStart({ id: 'ab' });
313
+ store.trigger.dragMove({ dx: 13, dy: 17 });
314
+
315
+ expect(
316
+ engine
317
+ .getAllEdgePaths()
318
+ .get('ab')!
319
+ .segments.flatMap((segment) => segment.points),
320
+ ).toContainEqual({ x: 170, y: 173 });
321
+ });
322
+
323
+ it('grid-snaps carried labels and waypoints by their owner delta', () => {
324
+ const nodes = [
325
+ group('g'),
326
+ {
327
+ ...createNode({ id: 'a', x: 50, y: 100, width: 80, height: 40 }),
328
+ parentId: 'g',
329
+ },
330
+ {
331
+ ...createNode({ id: 'b', x: 200, y: 100, width: 80, height: 40 }),
332
+ parentId: 'g',
333
+ },
334
+ ];
335
+ const edges = [
336
+ createEdge({
337
+ id: 'ab',
338
+ sourceId: 'a',
339
+ targetId: 'b',
340
+ waypoints: [{ x: 160, y: 180 }],
341
+ label: { x: 120, y: 180, width: 60, height: 20 },
342
+ }),
343
+ ];
344
+ const store = createFlowStore(createGraph({ nodes, edges }), {
345
+ snapToGrid: true,
346
+ snapGrid: [10, 10],
347
+ });
348
+ const engine = createFlowEngine(store);
349
+
350
+ store.trigger.select({ ids: ['g'] });
351
+ store.trigger.dragStart({ id: 'g' });
352
+ store.trigger.dragMove({ dx: 13, dy: 17 });
353
+
354
+ const path = engine.getAllEdgePaths().get('ab')!;
355
+ expect(path.labelBounds).toMatchObject({ x: 130, y: 200 });
356
+ expect(path.segments.flatMap((segment) => segment.points)).toContainEqual({
357
+ x: 170,
358
+ y: 200,
359
+ });
360
+
361
+ store.trigger.dragEnd();
362
+ store.trigger.commitDrag();
363
+ expect(engine.getAllEdgePaths().get('ab')?.labelBounds).toMatchObject({
364
+ x: 130,
365
+ y: 200,
366
+ });
367
+ expect(store.getSnapshot().context.graph.edges[0].waypoints).toEqual([
368
+ { x: 170, y: 200 },
369
+ ]);
370
+ });
371
+
372
+ it('edges attach to the derived bounds, not the stored content rect', () => {
373
+ const { engine } = makeRig(
374
+ [
375
+ group('g'),
376
+ { ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }), parentId: 'g' },
377
+ createNode({ id: 'ext', x: 1000, y: 250, width: 100, height: 50 }),
378
+ ],
379
+ [createEdge({ id: 'e', sourceId: 'g', targetId: 'ext' })],
380
+ );
381
+
382
+ const endpoints = engine.getEdgeEndpoints('e')!;
383
+ const bounds = engine.resolvedBounds('g');
384
+ // source anchor sits on the derived right edge (target is to the right)
385
+ expect(endpoints.source.x).toBeCloseTo(bounds.x + bounds.width);
386
+ });
387
+
388
+ it('an empty derived group anchors at its stored position', () => {
389
+ const { engine } = makeRig([
390
+ group('g', { x: 70, y: 90, group: { type: 'derived', minChildArea: { width: 100, height: 60 } } }),
391
+ ]);
392
+ const bounds = engine.resolvedBounds('g');
393
+ expect(bounds.x).toBeCloseTo(70);
394
+ expect(bounds.y).toBeCloseTo(90);
395
+ });
396
+ });
397
+
398
+ describe('fixed group clamping (store)', () => {
399
+ const fixedRig = (clampChildren: 'x' | 'y' | 'both' | 'none') =>
400
+ makeRig([
401
+ createNode({
402
+ id: 'g',
403
+ x: 0,
404
+ y: 0,
405
+ width: 400,
406
+ height: 300,
407
+ group: { type: 'fixed', padding: 10, contentSize: { width: 0, height: 40 }, clampChildren },
408
+ }),
409
+ { ...createNode({ id: 'a', x: 50, y: 100, width: 80, height: 40 }), parentId: 'g' },
410
+ ]);
411
+
412
+ function dragChild(store: ReturnType<typeof makeRig>['store'], dx: number, dy: number) {
413
+ store.trigger.select({ ids: ['a'] });
414
+ store.trigger.dragStart({ id: 'a' });
415
+ store.trigger.dragMove({ dx, dy });
416
+ store.trigger.dragEnd();
417
+ store.trigger.commitDrag();
418
+ return store.getSnapshot().context.graph.nodes.find((n) => n.id === 'a')!;
419
+ }
420
+
421
+ it("clampChildren: 'both' keeps the child inside the child area", () => {
422
+ const { store } = fixedRig('both');
423
+ const a = dragChild(store, 1000, 1000);
424
+ // child area: x 10..390, y 50..290 → max child pos (310, 250)
425
+ expect(a.x).toBe(400 - 10 - 80);
426
+ expect(a.y).toBe(300 - 10 - 40);
427
+ });
428
+
429
+ it("clampChildren: 'x' clamps horizontally only", () => {
430
+ const { store } = fixedRig('x');
431
+ const a = dragChild(store, 1000, 1000);
432
+ expect(a.x).toBe(310);
433
+ expect(a.y).toBe(1100); // free vertically
434
+ });
435
+
436
+ it("clampChildren: 'none' (default) does not clamp", () => {
437
+ const { store } = fixedRig('none');
438
+ const a = dragChild(store, 1000, 1000);
439
+ expect(a.x).toBe(1050);
440
+ });
441
+ });
442
+
443
+ describe('edge path memo vs derived groups', () => {
444
+ it('edges into a derived group re-route when a child moves (no stale memo)', () => {
445
+ const { store, engine } = makeRig(
446
+ [
447
+ group('g'),
448
+ { ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }), parentId: 'g' },
449
+ createNode({ id: 'ext', x: 1000, y: 250, width: 100, height: 50 }),
450
+ ],
451
+ [createEdge({ id: 'e', sourceId: 'g', targetId: 'ext' })],
452
+ );
453
+
454
+ store.trigger.select({ ids: ['a'] });
455
+ store.trigger.dragStart({ id: 'a' });
456
+ store.trigger.dragMove({ dx: 0, dy: 100 });
457
+ const first = engine.getAllEdgePaths().get('e')!;
458
+
459
+ store.trigger.dragMove({ dx: 0, dy: 100 });
460
+ const second = engine.getAllEdgePaths().get('e')!;
461
+
462
+ // group bounds moved down with its only child → the source anchor must move
463
+ expect(second.segments[0].points[0].y).toBe(
464
+ first.segments[0].points[0].y + 100,
465
+ );
466
+ });
467
+ });
468
+
469
+ describe('edge-label containment (studio digraph semantics)', () => {
470
+ function labelRig() {
471
+ const { store, engine } = makeRig(
472
+ [
473
+ group('A'),
474
+ { ...createNode({ id: 'x', x: 100, y: 200, width: 100, height: 50 }), parentId: 'A' },
475
+ { ...createNode({ id: 'y', x: 320, y: 200, width: 100, height: 50 }), parentId: 'A' },
476
+ createNode({ id: 'ext', x: 1000, y: 200, width: 100, height: 50 }),
477
+ ],
478
+ [
479
+ createEdge({
480
+ id: 'xy',
481
+ sourceId: 'x',
482
+ targetId: 'y',
483
+ label: { x: 230, y: 300, width: 80, height: 24 },
484
+ }),
485
+ createEdge({
486
+ id: 'cross',
487
+ sourceId: 'x',
488
+ targetId: 'ext',
489
+ label: { x: 600, y: 600, width: 80, height: 24 },
490
+ }),
491
+ ],
492
+ );
493
+ return { store, engine };
494
+ }
495
+
496
+ it('an owned label contributes to the derived bounds; a cross-group label does not', () => {
497
+ const { engine } = labelRig();
498
+ const bounds = engine.resolvedBounds('A');
499
+ // xy's label bottom (324) + padding 16 extends A beyond the nodes (250 + 16)
500
+ expect(bounds.y + bounds.height).toBe(324 + 16);
501
+ // cross-group label at (600..680) does NOT stretch A horizontally
502
+ expect(bounds.x + bounds.width).toBe(420 + 16);
503
+ });
504
+
505
+ it('dragging an owned label grows the group and is fully reversible', () => {
506
+ const { store, engine } = labelRig();
507
+ const before = engine.resolvedBounds('A');
508
+
509
+ store.trigger.select({ ids: ['xy'] });
510
+ store.trigger.dragStart({ id: 'xy' });
511
+ store.trigger.dragMove({ dx: 400, dy: 0 });
512
+ const during = engine.resolvedBounds('A');
513
+ // label right edge moved from 310 (inside) to 710 — group right edge
514
+ // follows it: 710 + padding 16
515
+ expect(during.x + during.width).toBe(710 + 16);
516
+
517
+ store.trigger.dragMove({ dx: -400, dy: 0 });
518
+ store.trigger.dragEnd();
519
+ store.trigger.commitDrag();
520
+ expect(engine.resolvedBounds('A')).toEqual(before);
521
+ });
522
+
523
+ it("a group's own self-edge label does not constrain its bounds", () => {
524
+ const { engine } = makeRig(
525
+ [
526
+ group('A'),
527
+ { ...createNode({ id: 'x', x: 100, y: 200, width: 100, height: 50 }), parentId: 'A' },
528
+ ],
529
+ [
530
+ createEdge({
531
+ id: 'self',
532
+ sourceId: 'A',
533
+ targetId: 'A',
534
+ label: { x: 2000, y: 2000, width: 80, height: 24 },
535
+ }),
536
+ ],
537
+ );
538
+ const bounds = engine.resolvedBounds('A');
539
+ expect(bounds.x + bounds.width).toBeLessThan(1000);
540
+ });
541
+
542
+ it('the edgeOwner engine option overrides the default rule', () => {
543
+ const store = createFlowStore(
544
+ createGraph({
545
+ nodes: [
546
+ group('A'),
547
+ { ...createNode({ id: 'x', x: 100, y: 200, width: 100, height: 50 }), parentId: 'A' },
548
+ { ...createNode({ id: 'y', x: 320, y: 200, width: 100, height: 50 }), parentId: 'A' },
549
+ ],
550
+ edges: [
551
+ createEdge({
552
+ id: 'xy',
553
+ sourceId: 'x',
554
+ targetId: 'y',
555
+ label: { x: 230, y: 600, width: 80, height: 24 },
556
+ }),
557
+ ],
558
+ }),
559
+ );
560
+ const engine = createFlowEngine(store, { edgeOwner: () => null }); // everything root-owned
561
+ expect(engine.getEdgeOwner('xy')).toBeNull();
562
+ // label no longer stretches A downward
563
+ expect(engine.resolvedBounds('A').y + engine.resolvedBounds('A').height).toBe(250 + 16);
564
+ });
565
+
566
+ it('notifies the owner group when a contained label moves', () => {
567
+ const { store } = labelRig();
568
+ const notifier = createEntityChangeNotifier(store);
569
+ let groupNotified = 0;
570
+ notifier.subscribe('A', () => groupNotified++);
571
+
572
+ store.trigger.select({ ids: ['xy'] });
573
+ store.trigger.dragStart({ id: 'xy' });
574
+ groupNotified = 0;
575
+ store.trigger.dragMove({ dx: 10, dy: 0 });
576
+ expect(groupNotified).toBe(1);
577
+ });
578
+ });
579
+
580
+ describe('group collapse derivation', () => {
581
+ function collapseRig() {
582
+ return makeRig(
583
+ [
584
+ createNode({
585
+ id: 'outer',
586
+ x: 0,
587
+ y: 0,
588
+ width: 360,
589
+ height: 260,
590
+ group: { type: 'fixed', contentSize: { width: 0, height: 40 } },
591
+ }),
592
+ {
593
+ ...createNode({
594
+ id: 'inner',
595
+ x: 80,
596
+ y: 80,
597
+ width: 180,
598
+ height: 120,
599
+ group: { type: 'fixed', contentSize: { width: 0, height: 32 } },
600
+ }),
601
+ parentId: 'outer',
602
+ },
603
+ { ...createNode({ id: 'a', x: 110, y: 132, width: 60, height: 40 }), parentId: 'inner' },
604
+ { ...createNode({ id: 'b', x: 190, y: 132, width: 60, height: 40 }), parentId: 'inner' },
605
+ createNode({ id: 'ext', x: 520, y: 120, width: 100, height: 60 }),
606
+ ],
607
+ [
608
+ createEdge({ id: 'a-ext', sourceId: 'a', targetId: 'ext' }),
609
+ createEdge({ id: 'b-ext', sourceId: 'b', targetId: 'ext' }),
610
+ createEdge({ id: 'a-b', sourceId: 'a', targetId: 'b' }),
611
+ ],
612
+ );
613
+ }
614
+
615
+ it('hides collapsed descendants and reroutes boundary edges to a merged proxy', () => {
616
+ const { store, engine } = collapseRig();
617
+
618
+ store.trigger.collapseNode({ id: 'outer' });
619
+
620
+ expect(engine.getVisibleEntities().nodes.map((node) => node.id)).toEqual(['outer', 'ext']);
621
+ expect(engine.getVisibleEntities().edges.map((edge) => edge.id)).toEqual(['flow:folded:a-ext']);
622
+ expect(engine.getAllEdgePaths().has('a-ext')).toBe(false);
623
+ expect(engine.getAllEdgePaths().has('a-b')).toBe(false);
624
+ expect(engine.getAllEdgePaths().has('flow:folded:a-ext')).toBe(true);
625
+
626
+ const proxy = engine.getEdge('flow:folded:a-ext')!;
627
+ expect(proxy.sourceId).toBe('outer');
628
+ expect(proxy.targetId).toBe('ext');
629
+ expect((proxy.data as { foldedEdgeIds: string[] }).foldedEdgeIds).toEqual(['a-ext', 'b-ext']);
630
+ expect(engine.resolveEntityConfig(proxy.id)).toMatchObject({
631
+ selectable: false,
632
+ reconnectable: false,
633
+ deletable: false,
634
+ });
635
+ });
636
+
637
+ it('expand restores the exact original visible entities', () => {
638
+ const { store, engine } = collapseRig();
639
+ const before = engine.getVisibleEntities();
640
+
641
+ store.trigger.collapseNode({ id: 'outer' });
642
+ store.trigger.expandNode({ id: 'outer' });
643
+
644
+ expect(engine.getVisibleEntities()).toEqual(before);
645
+ });
646
+
647
+ it('nested collapse resolves boundary edges to the outermost visible ancestor', () => {
648
+ const { store, engine } = collapseRig();
649
+
650
+ store.trigger.collapseNode({ id: 'inner' });
651
+ expect(engine.getEdge('flow:folded:a-ext')?.sourceId).toBe('inner');
652
+
653
+ store.trigger.collapseNode({ id: 'outer' });
654
+ expect(engine.getEdge('flow:folded:a-ext')?.sourceId).toBe('outer');
655
+ expect(engine.getVisibleEntities().nodes.map((node) => node.id)).toEqual(['outer', 'ext']);
656
+ });
657
+
658
+ it('undo and redo restore collapse state', () => {
659
+ const { store, engine } = collapseRig();
660
+
661
+ store.trigger.collapseNode({ id: 'outer' });
662
+ expect(engine.getVisibleEntities().nodes.map((node) => node.id)).toEqual(['outer', 'ext']);
663
+
664
+ store.trigger.undo();
665
+ expect(engine.getVisibleEntities().nodes.map((node) => node.id).sort()).toEqual([
666
+ 'a',
667
+ 'b',
668
+ 'ext',
669
+ 'inner',
670
+ 'outer',
671
+ ]);
672
+
673
+ store.trigger.redo();
674
+ expect(engine.getVisibleEntities().nodes.map((node) => node.id)).toEqual(['outer', 'ext']);
675
+ });
676
+
677
+ it('viewport culling excludes descendants hidden by collapse', () => {
678
+ const { store, engine } = collapseRig();
679
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
680
+ store.trigger.collapseNode({ id: 'outer' });
681
+
682
+ expect(
683
+ engine.getRenderableEntities({ cull: 'viewport' }).nodes.map((node) => node.id),
684
+ ).toEqual(['outer', 'ext']);
685
+ });
686
+
687
+ // This is the set the UI (StatelyFlow's NodeLayer/EdgeLayer) mounts when
688
+ // viewport culling is off (fits viewport / unmeasured). It MUST already be
689
+ // visibility-filtered, or collapsed descendants, `hidden` entities, and stale
690
+ // boundary edges leak into the render alongside their folded proxies.
691
+ it('renderable set with cull:none excludes collapsed + hidden and swaps boundary edges for the folded proxy', () => {
692
+ const { store, engine } = makeRig(
693
+ [
694
+ createNode({
695
+ id: 'outer',
696
+ x: 0,
697
+ y: 0,
698
+ width: 360,
699
+ height: 260,
700
+ group: { type: 'fixed', contentSize: { width: 0, height: 40 } },
701
+ }),
702
+ { ...createNode({ id: 'a', x: 110, y: 132, width: 60, height: 40 }), parentId: 'outer' },
703
+ { ...createNode({ id: 'ext', x: 520, y: 120, width: 100, height: 60 }) },
704
+ { ...createNode({ id: 'ghost', x: 900, y: 120, width: 60, height: 40 }), hidden: true },
705
+ ],
706
+ [
707
+ createEdge({ id: 'a-ext', sourceId: 'a', targetId: 'ext' }),
708
+ { ...createEdge({ id: 'ext-ghost', sourceId: 'ext', targetId: 'ghost' }), hidden: true },
709
+ ],
710
+ );
711
+
712
+ store.trigger.collapseNode({ id: 'outer' });
713
+
714
+ const renderable = engine.getRenderableEntities({ cull: 'none' });
715
+ const nodeIds = renderable.nodes.map((node) => node.id);
716
+ const edgeIds = renderable.edges.map((edge) => edge.id);
717
+
718
+ // Collapsed descendant `a` and explicitly-`hidden` `ghost` are excluded.
719
+ expect(nodeIds).toEqual(['outer', 'ext']);
720
+ // Folded proxy renders; the original boundary edge `a-ext` and the hidden
721
+ // edge `ext-ghost` do not.
722
+ expect(edgeIds).toContain('flow:folded:a-ext');
723
+ expect(edgeIds).not.toContain('a-ext');
724
+ expect(edgeIds).not.toContain('ext-ghost');
725
+ });
726
+ });
727
+
728
+ describe('contained edges move with their dragged container', () => {
729
+ function containedRig() {
730
+ return makeRig(
731
+ [
732
+ group('A'),
733
+ { ...createNode({ id: 'x', x: 100, y: 200, width: 100, height: 50 }), parentId: 'A' },
734
+ { ...createNode({ id: 'y', x: 320, y: 200, width: 100, height: 50 }), parentId: 'A' },
735
+ createNode({ id: 'ext', x: 1000, y: 200, width: 100, height: 50 }),
736
+ ],
737
+ [
738
+ createEdge({
739
+ id: 'xy',
740
+ sourceId: 'x',
741
+ targetId: 'y',
742
+ label: { x: 230, y: 300, width: 80, height: 24 },
743
+ }),
744
+ createEdge({
745
+ id: 'cross',
746
+ sourceId: 'x',
747
+ targetId: 'ext',
748
+ label: { x: 600, y: 600, width: 80, height: 24 },
749
+ }),
750
+ ],
751
+ );
752
+ }
753
+ const label = (store: ReturnType<typeof containedRig>['store'], id: string) =>
754
+ store.getSnapshot().context.graph.edges.find((e) => e.id === id)!.label!;
755
+
756
+ it('dragging the group carries its owned label but not a cross-boundary label', () => {
757
+ const { store } = containedRig();
758
+ store.trigger.select({ ids: ['A'] });
759
+ store.trigger.dragStart({ id: 'A', transactionId: 'tx-a' });
760
+ store.trigger.dragMove({ dx: 70, dy: 40 });
761
+
762
+ expect(label(store, 'xy').dx ?? 0).toBe(70);
763
+ expect(label(store, 'xy').dy ?? 0).toBe(40);
764
+ expect(label(store, 'cross').dx ?? 0).toBe(0);
765
+
766
+ store.trigger.dragEnd();
767
+ store.trigger.commitDrag({ transactionId: 'tx-a' });
768
+ expect(label(store, 'xy').x).toBe(300);
769
+ expect(label(store, 'xy').y).toBe(340);
770
+ expect(label(store, 'cross').x).toBe(600);
771
+
772
+ store.trigger.undo();
773
+ expect(label(store, 'xy').x).toBe(230);
774
+ expect(label(store, 'xy').y).toBe(300);
775
+ });
776
+
777
+ it('carries a DOM-auto-sized owned label before it has a measured size', () => {
778
+ const { store } = makeRig(
779
+ [
780
+ group('A'),
781
+ { ...createNode({ id: 'x', x: 100, y: 200, width: 100, height: 50 }), parentId: 'A' },
782
+ { ...createNode({ id: 'y', x: 320, y: 200, width: 100, height: 50 }), parentId: 'A' },
783
+ ],
784
+ [
785
+ createEdge({
786
+ id: 'xy',
787
+ sourceId: 'x',
788
+ targetId: 'y',
789
+ labelAnchor: 'fixed',
790
+ label: { x: 0, y: 0, width: 0, height: 0, autoSize: true },
791
+ }),
792
+ ],
793
+ );
794
+ store.trigger.select({ ids: ['A'] });
795
+ store.trigger.dragStart({ id: 'A' });
796
+ store.trigger.dragMove({ dx: 30, dy: 20 });
797
+
798
+ expect(store.getSnapshot().context.graph.edges[0].label).toMatchObject({
799
+ dx: 30,
800
+ dy: 20,
801
+ });
802
+ });
803
+
804
+ it('directly multi-selecting both endpoints moves only the nodes; the label stays put', () => {
805
+ const { store } = containedRig();
806
+ // The user selects the two nodes themselves (not the containing group).
807
+ store.trigger.select({ ids: ['x', 'y'] });
808
+ store.trigger.dragStart({ id: 'x', transactionId: 'tx-xy' });
809
+ store.trigger.dragMove({ dx: 70, dy: 40 });
810
+
811
+ const nodes = store.getSnapshot().context.graph.nodes;
812
+ expect(nodes.find((n) => n.id === 'x')!.dx).toBe(70);
813
+ expect(nodes.find((n) => n.id === 'y')!.dx).toBe(70);
814
+ // Only nodes move — the (unselected) fixed label is not carried along.
815
+ expect(label(store, 'xy').dx ?? 0).toBe(0);
816
+ expect(label(store, 'xy').dy ?? 0).toBe(0);
817
+
818
+ store.trigger.dragEnd();
819
+ store.trigger.commitDrag({ transactionId: 'tx-xy' });
820
+ expect(label(store, 'xy').x).toBe(230);
821
+ expect(label(store, 'xy').y).toBe(300);
822
+ });
823
+
824
+ it('keyboard moveNodes on the group carries its owned fixed label', () => {
825
+ const { store } = containedRig();
826
+ store.trigger.select({ ids: ['A'] });
827
+ store.trigger.moveNodes({ direction: { x: 1, y: 0 }, factor: 10 });
828
+ store.trigger.commitDrag();
829
+ expect(label(store, 'xy').x).toBe(240);
830
+ expect(label(store, 'cross').x).toBe(600);
831
+ });
832
+
833
+ it('keyboard moveNodes on a selected label nudges it', () => {
834
+ const { store } = containedRig();
835
+ store.trigger.select({ ids: ['xy'] });
836
+ store.trigger.moveNodes({ direction: { x: 0, y: 1 }, factor: 5 });
837
+ store.trigger.commitDrag();
838
+ expect(label(store, 'xy').y).toBe(305);
839
+ });
840
+ });
841
+
842
+ describe('label carry respects anchor type (no double-move)', () => {
843
+ function selfEdgeRig(labelAnchor: number | 'fixed') {
844
+ return makeRig(
845
+ [createNode({ id: 'n', x: 100, y: 100, width: 100, height: 50 })],
846
+ [
847
+ createEdge({
848
+ id: 'self',
849
+ sourceId: 'n',
850
+ targetId: 'n',
851
+ labelAnchor,
852
+ label: { x: 120, y: 160, width: 60, height: 20 },
853
+ }),
854
+ ],
855
+ );
856
+ }
857
+ const selfLabel = (store: ReturnType<typeof makeRig>['store']) =>
858
+ store.getSnapshot().context.graph.edges[0].label!;
859
+
860
+ it('does not carry a numeric-anchored self-edge label (it already auto-follows)', () => {
861
+ const { store } = selfEdgeRig(0.5);
862
+ store.trigger.select({ ids: ['n'] });
863
+ store.trigger.dragStart({ id: 'n', transactionId: 't' });
864
+ store.trigger.dragMove({ dx: 0, dy: 80 });
865
+ // Without the anchor gate this would be 80 → combined with the auto-follow,
866
+ // the label would slide twice as fast as the node.
867
+ expect(selfLabel(store).dx ?? 0).toBe(0);
868
+ expect(selfLabel(store).dy ?? 0).toBe(0);
869
+ });
870
+
871
+ it('does not carry a fixed-anchored self-edge label', () => {
872
+ const { store } = selfEdgeRig('fixed');
873
+ store.trigger.select({ ids: ['n'] });
874
+ store.trigger.dragStart({ id: 'n', transactionId: 't' });
875
+ store.trigger.dragMove({ dx: 0, dy: 80 });
876
+ expect(selfLabel(store).dy ?? 0).toBe(0);
877
+ });
878
+
879
+ it('does not add an offset to an auto-anchored label owned by a dragged group', () => {
880
+ const { store, engine } = makeRig(
881
+ [
882
+ group('g'),
883
+ {
884
+ ...createNode({ id: 'a', x: 100, y: 200, width: 100, height: 50 }),
885
+ parentId: 'g',
886
+ },
887
+ {
888
+ ...createNode({ id: 'b', x: 320, y: 200, width: 100, height: 50 }),
889
+ parentId: 'g',
890
+ },
891
+ ],
892
+ [
893
+ createEdge({
894
+ id: 'ab',
895
+ sourceId: 'a',
896
+ targetId: 'b',
897
+ labelAnchor: 0.5,
898
+ label: { x: 230, y: 300, width: 80, height: 24 },
899
+ }),
900
+ ],
901
+ );
902
+ const before = engine.getAllEdgePaths().get('ab')!.labelBounds!;
903
+
904
+ store.trigger.select({ ids: ['g'] });
905
+ store.trigger.dragStart({ id: 'g', transactionId: 't' });
906
+ store.trigger.dragMove({ dx: 70, dy: 40 });
907
+
908
+ const edge = store.getSnapshot().context.graph.edges[0];
909
+ expect(edge.label!.dx ?? 0).toBe(0);
910
+ expect(edge.label!.dy ?? 0).toBe(0);
911
+ expect(engine.getAllEdgePaths().get('ab')!.labelBounds).toMatchObject({
912
+ x: before.x + 70,
913
+ y: before.y + 40,
914
+ });
915
+ });
916
+ });
917
+
918
+ describe('getToolbarAnchor', () => {
919
+ it('resolves nodes, labeled edges, label-less edges (1×1 midpoint), and selections', () => {
920
+ const { engine } = makeRig(
921
+ [
922
+ createNode({ id: 'n1', x: 0, y: 0, width: 100, height: 50 }),
923
+ createNode({ id: 'n2', x: 300, y: 0, width: 100, height: 50 }),
924
+ ],
925
+ [
926
+ createEdge({ id: 'labeled', sourceId: 'n1', targetId: 'n2', label: { x: 150, y: 80, width: 60, height: 20 } }),
927
+ createEdge({ id: 'bare', sourceId: 'n1', targetId: 'n2' }),
928
+ ],
929
+ );
930
+
931
+ expect(engine.getToolbarAnchor('n1')).toEqual({ x: 0, y: 0, width: 100, height: 50 });
932
+ expect(engine.getToolbarAnchor('labeled')).toEqual({ x: 150, y: 80, width: 60, height: 20 });
933
+
934
+ const bare = engine.getToolbarAnchor('bare')!;
935
+ expect(bare.width).toBe(1);
936
+ expect(bare.x).toBeGreaterThan(100); // somewhere along the path
937
+ expect(bare.x).toBeLessThan(300);
938
+
939
+ const union = engine.getToolbarAnchor(['n1', 'n2'])!;
940
+ expect(union).toEqual({ x: 0, y: 0, width: 400, height: 50 });
941
+
942
+ expect(engine.getToolbarAnchor('missing')).toBeNull();
943
+ });
944
+
945
+ it('uses the current edge-label position for bounds and toolbar anchors after drag, undo, and redo', () => {
946
+ const { store, engine } = makeRig(
947
+ [
948
+ createNode({ id: 'a', x: 0, y: 0 }),
949
+ createNode({ id: 'b', x: 300, y: 0 }),
950
+ ],
951
+ [
952
+ createEdge({
953
+ id: 'label',
954
+ sourceId: 'a',
955
+ targetId: 'b',
956
+ label: { x: 120, y: 80, width: 60, height: 20, data: {} },
957
+ }),
958
+ ],
959
+ );
960
+
961
+ store.trigger.updateMeasurements({
962
+ measurements: [
963
+ { id: 'label', rect: { x: 120, y: 80, width: 60, height: 20 } },
964
+ ],
965
+ });
966
+ const expectLabelGeometry = (x: number, y: number) => {
967
+ const expected = { x, y, width: 60, height: 20 };
968
+ expect(engine.getBounds('label')).toEqual(expected);
969
+ expect(engine.getToolbarAnchor('label')).toEqual(expected);
970
+ };
971
+
972
+ expectLabelGeometry(120, 80);
973
+
974
+ store.trigger.select({ ids: ['label'] });
975
+ store.trigger.dragStart({ id: 'label' });
976
+ store.trigger.dragMove({ dx: 50, dy: 30 });
977
+ store.trigger.dragEnd();
978
+ store.trigger.commitDrag();
979
+
980
+ expectLabelGeometry(170, 110);
981
+
982
+ store.trigger.undo();
983
+ expectLabelGeometry(120, 80);
984
+
985
+ store.trigger.redo();
986
+ expectLabelGeometry(170, 110);
987
+ });
988
+ });
989
+
990
+ describe('port-side endpoint directions', () => {
991
+ it('edges leave/enter perpendicular through the port side, not by geometry', () => {
992
+ const { engine } = makeRig(
993
+ [
994
+ {
995
+ ...createNode({ id: 's', x: 0, y: 0, width: 100, height: 60 }),
996
+ ports: [{ name: 'out', side: 'right' as const, direction: 'out' as const, x: 94, y: 24, width: 12, height: 12, data: {} }],
997
+ },
998
+ {
999
+ ...createNode({ id: 't', x: 40, y: 400, width: 100, height: 60 }),
1000
+ ports: [{ name: 'in', side: 'left' as const, direction: 'in' as const, x: -6, y: 24, width: 12, height: 12, data: {} }],
1001
+ },
1002
+ ],
1003
+ [createEdge({ id: 'e', sourceId: 's', targetId: 't', sourcePort: 'out', targetPort: 'in' })],
1004
+ );
1005
+ // nodes are stacked vertically — geometric bestDirection would say
1006
+ // bottom/top; the ports' sides must win (right/left), anchored at the
1007
+ // port centers.
1008
+ const endpoints = engine.getEdgeEndpoints('e')!;
1009
+ expect(endpoints.source).toEqual({ x: 100, y: 30 });
1010
+ expect(endpoints.target).toEqual({ x: 40, y: 430 });
1011
+ // perpendicular exit: with side-driven directions the bezier's first
1012
+ // control point extends horizontally from the source port
1013
+ const path = engine.getAllEdgePaths().get('e')!;
1014
+ const points = path.segments[0].points;
1015
+ // rightward exit: control point extends +x from the source even though
1016
+ // the target is to the left and below
1017
+ expect(points[1].x).toBeGreaterThan(points[0].x);
1018
+ expect(points[1].y).toBeCloseTo(points[0].y, 0);
1019
+ // leftward entry at the target
1020
+ expect(points[2].x).toBeLessThan(points[3].x);
1021
+
1022
+ });
1023
+ });