@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,232 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createGraph, createNode } from './factories';
4
+ import type { FlowNode } from './types';
5
+ import { FlowGraphValidationError } from './validation';
6
+
7
+ function makeFlow(node: FlowNode, extraNodes: FlowNode[] = []) {
8
+ return createFlow({
9
+ graph: createGraph({ nodes: [node, ...extraNodes], edges: [] }),
10
+ });
11
+ }
12
+
13
+ function measure(
14
+ flow: ReturnType<typeof makeFlow>,
15
+ id: string,
16
+ rect: { x: number; y: number; width: number; height: number },
17
+ ) {
18
+ flow.internals.store.trigger.updateMeasurements({
19
+ measurements: [{ id, rect }],
20
+ });
21
+ }
22
+
23
+ describe('auto-size measurement anchors', () => {
24
+ it('keeps top-left fixed by default across first and later measurements', () => {
25
+ const flow = makeFlow(
26
+ createNode({
27
+ id: 'a',
28
+ x: 100,
29
+ y: 80,
30
+ width: 120,
31
+ height: 40,
32
+ autoSize: true,
33
+ }),
34
+ );
35
+
36
+ measure(flow, 'a', { x: 100, y: 80, width: 160, height: 60 });
37
+ measure(flow, 'a', { x: 100, y: 80, width: 220, height: 90 });
38
+
39
+ expect(flow.getNode('a')).toMatchObject({
40
+ x: 100,
41
+ y: 80,
42
+ width: 120,
43
+ height: 40,
44
+ measured: { width: 220, height: 90 },
45
+ });
46
+ expect(flow.getBounds('a')).toEqual({
47
+ x: 100,
48
+ y: 80,
49
+ width: 220,
50
+ height: 90,
51
+ });
52
+ });
53
+
54
+ it('preserves the visual center on first and subsequent measurements', () => {
55
+ const flow = makeFlow(
56
+ createNode({
57
+ id: 'a',
58
+ x: 200,
59
+ y: 300,
60
+ width: 0,
61
+ height: 0,
62
+ autoSize: true,
63
+ autoSizeAnchor: 'center',
64
+ }),
65
+ );
66
+
67
+ measure(flow, 'a', { x: 200, y: 300, width: 120, height: 50 });
68
+ expect(flow.getBounds('a')).toEqual({
69
+ x: 140,
70
+ y: 275,
71
+ width: 120,
72
+ height: 50,
73
+ });
74
+
75
+ measure(flow, 'a', { x: 140, y: 275, width: 200, height: 80 });
76
+ expect(flow.getBounds('a')).toEqual({
77
+ x: 100,
78
+ y: 260,
79
+ width: 200,
80
+ height: 80,
81
+ });
82
+ });
83
+
84
+ it('supports independent fractional anchors', () => {
85
+ const flow = makeFlow(
86
+ createNode({
87
+ id: 'a',
88
+ x: 10,
89
+ y: 20,
90
+ width: 100,
91
+ height: 40,
92
+ autoSize: true,
93
+ autoSizeAnchor: { x: 0.5, y: 0 },
94
+ }),
95
+ );
96
+
97
+ measure(flow, 'a', { x: 10, y: 20, width: 180, height: 100 });
98
+ expect(flow.getBounds('a')).toEqual({
99
+ x: -30,
100
+ y: 20,
101
+ width: 180,
102
+ height: 100,
103
+ });
104
+ });
105
+
106
+ it('preserves the transient visual anchor and survives drag commit', () => {
107
+ const flow = makeFlow(
108
+ createNode({
109
+ id: 'a',
110
+ x: 100,
111
+ y: 100,
112
+ dx: 30,
113
+ dy: 20,
114
+ width: 100,
115
+ height: 40,
116
+ autoSize: true,
117
+ autoSizeAnchor: 'center',
118
+ }),
119
+ );
120
+
121
+ measure(flow, 'a', { x: 130, y: 120, width: 200, height: 80 });
122
+ expect(flow.getBounds('a')).toEqual({
123
+ x: 80,
124
+ y: 100,
125
+ width: 200,
126
+ height: 80,
127
+ });
128
+ expect(flow.getNode('a')).toMatchObject({ dx: 30, dy: 20 });
129
+
130
+ flow.internals.store.trigger.commitDrag();
131
+ expect(flow.getBounds('a')).toEqual({
132
+ x: 80,
133
+ y: 100,
134
+ width: 200,
135
+ height: 80,
136
+ });
137
+ expect(flow.getNode('a')).toMatchObject({ x: 80, y: 100, dx: 0, dy: 0 });
138
+ });
139
+
140
+ it('uses absolute child coordinates without moving its parent', () => {
141
+ const parent = createNode({
142
+ id: 'parent',
143
+ x: 40,
144
+ y: 50,
145
+ width: 400,
146
+ height: 300,
147
+ });
148
+ const child = createNode({
149
+ id: 'child',
150
+ parentId: 'parent',
151
+ x: 100,
152
+ y: 120,
153
+ width: 100,
154
+ height: 40,
155
+ autoSize: true,
156
+ autoSizeAnchor: 'center',
157
+ });
158
+ const flow = makeFlow(child, [parent]);
159
+
160
+ measure(flow, 'child', { x: 100, y: 120, width: 180, height: 80 });
161
+ expect(flow.getBounds('child')).toEqual({
162
+ x: 60,
163
+ y: 100,
164
+ width: 180,
165
+ height: 80,
166
+ });
167
+ expect(flow.getNode('parent')).toMatchObject({ x: 40, y: 50 });
168
+ });
169
+
170
+ it('is derived, idempotent, non-undoable, and serialization-stable', () => {
171
+ const flow = makeFlow(
172
+ createNode({
173
+ id: 'a',
174
+ x: 100,
175
+ y: 80,
176
+ width: 100,
177
+ height: 40,
178
+ autoSize: true,
179
+ autoSizeAnchor: 'center',
180
+ }),
181
+ );
182
+ const graphChanged = vi.fn();
183
+ const commits = vi.fn();
184
+ flow.on('graphChanged', graphChanged);
185
+ flow.on('commit', commits);
186
+
187
+ measure(flow, 'a', { x: 100, y: 80, width: 200, height: 80 });
188
+ const after = flow.getSnapshot();
189
+ measure(flow, 'a', { x: 50, y: 60, width: 200, height: 80 });
190
+
191
+ expect(flow.getSnapshot()).toBe(after);
192
+ expect(flow.history.canUndo()).toBe(false);
193
+ expect(graphChanged).not.toHaveBeenCalled();
194
+ expect(commits).not.toHaveBeenCalled();
195
+ expect(flow.toJSON().graph.nodes[0]).toMatchObject({
196
+ x: 100,
197
+ y: 80,
198
+ width: 100,
199
+ height: 40,
200
+ autoSizeAnchor: 'center',
201
+ });
202
+ expect(flow.toJSON().graph.nodes[0].measured).toBeUndefined();
203
+ });
204
+
205
+ it('rejects invalid policies atomically', () => {
206
+ for (const autoSizeAnchor of [
207
+ 'left',
208
+ { x: -0.1, y: 0.5 },
209
+ { x: 0.5, y: 1.1 },
210
+ { x: Number.NaN, y: 0.5 },
211
+ ]) {
212
+ expect(() =>
213
+ makeFlow(
214
+ createNode({
215
+ id: 'a',
216
+ autoSize: true,
217
+ autoSizeAnchor: autoSizeAnchor as never,
218
+ }),
219
+ ),
220
+ ).toThrow(FlowGraphValidationError);
221
+ }
222
+
223
+ const flow = makeFlow(createNode({ id: 'a', autoSize: true }));
224
+ const before = flow.getSnapshot().graph;
225
+ expect(() =>
226
+ flow.nodes.update('a', {
227
+ autoSizeAnchor: { x: 2, y: 0 },
228
+ } as never),
229
+ ).toThrow(FlowGraphValidationError);
230
+ expect(flow.getSnapshot().graph).toBe(before);
231
+ });
232
+ });
@@ -0,0 +1,96 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ getMinimapProjection,
4
+ getMinimapViewportCenter,
5
+ } from './minimap';
6
+
7
+ const base = {
8
+ contentBounds: { x: 0, y: 0, width: 1000, height: 500 },
9
+ viewport: { x: 0, y: 0, zoom: 1 },
10
+ viewportSize: { width: 800, height: 600 },
11
+ width: 200,
12
+ height: 150,
13
+ };
14
+
15
+ describe('getMinimapProjection', () => {
16
+ it('round-trips points between canvas and minimap space', () => {
17
+ const projection = getMinimapProjection(base);
18
+ const canvasPoint = { x: 420, y: 130 };
19
+ const mini = projection.toMinimap(canvasPoint);
20
+ const back = projection.toCanvas(mini);
21
+ expect(back.x).toBeCloseTo(canvasPoint.x);
22
+ expect(back.y).toBeCloseTo(canvasPoint.y);
23
+ // minimap coords stay on the surface
24
+ expect(mini.x).toBeGreaterThanOrEqual(0);
25
+ expect(mini.x).toBeLessThanOrEqual(base.width);
26
+ });
27
+
28
+ it('covers content plus the visible area (lens stays on the minimap)', () => {
29
+ const projection = getMinimapProjection({
30
+ ...base,
31
+ // viewport panned far away from content
32
+ viewport: { x: -5000, y: 0, zoom: 1 },
33
+ });
34
+ const lens = projection.viewportRect;
35
+ expect(lens.x).toBeGreaterThanOrEqual(0);
36
+ expect(lens.x + lens.width).toBeLessThanOrEqual(base.width + 0.001);
37
+ });
38
+
39
+ it('the lens maps the visible canvas area', () => {
40
+ const projection = getMinimapProjection(base);
41
+ // visible area at zoom 1, origin 0 = 800x600 canvas units
42
+ const lensTopLeft = projection.toCanvas({
43
+ x: projection.viewportRect.x,
44
+ y: projection.viewportRect.y,
45
+ });
46
+ expect(lensTopLeft.x).toBeCloseTo(0);
47
+ expect(lensTopLeft.y).toBeCloseTo(0);
48
+ expect(projection.viewportRect.width / projection.scale).toBeCloseTo(800);
49
+ });
50
+
51
+ it('rectToMinimap scales rects uniformly', () => {
52
+ const projection = getMinimapProjection(base);
53
+ const rect = projection.rectToMinimap({ x: 0, y: 0, width: 100, height: 50 });
54
+ expect(rect.width).toBeCloseTo(100 * projection.scale);
55
+ expect(rect.height).toBeCloseTo(50 * projection.scale);
56
+ });
57
+
58
+ it('handles an empty graph without degenerating', () => {
59
+ const projection = getMinimapProjection({
60
+ ...base,
61
+ contentBounds: { x: 0, y: 0, width: 0, height: 0 },
62
+ });
63
+ expect(Number.isFinite(projection.scale)).toBe(true);
64
+ expect(projection.scale).toBeGreaterThan(0);
65
+ });
66
+ });
67
+
68
+ describe('getMinimapViewportCenter', () => {
69
+ const options = {
70
+ contentBounds: { x: 100, y: 200, width: 400, height: 200 },
71
+ viewportBounds: { x: 0, y: 0, width: 200, height: 100 },
72
+ };
73
+
74
+ it('preserves a grab offset and clamps a lens one viewport beyond content', () => {
75
+ expect(
76
+ getMinimapViewportCenter({
77
+ ...options,
78
+ point: { x: -1000, y: 1000 },
79
+ grabOffset: { x: -40, y: 10 },
80
+ }),
81
+ ).toEqual({ x: 0, y: 450 });
82
+ });
83
+
84
+ it('is idempotent at the navigation boundary', () => {
85
+ const first = getMinimapViewportCenter({
86
+ ...options,
87
+ point: { x: 1000, y: -1000 },
88
+ });
89
+ const second = getMinimapViewportCenter({
90
+ ...options,
91
+ point: first,
92
+ });
93
+ expect(first).toEqual({ x: 600, y: 150 });
94
+ expect(second).toEqual(first);
95
+ });
96
+ });
package/src/minimap.ts ADDED
@@ -0,0 +1,137 @@
1
+ import type { Point, Rect, Size, Viewport } from './types';
2
+ import { getVisibleBounds } from './viewport';
3
+ import { unionRect } from './spatial';
4
+
5
+ // Minimap projection — pure math, renderer-agnostic.
6
+ //
7
+ // Maps canvas (graph/world) space onto a fixed-size minimap surface:
8
+ // content bounds plus the currently visible area, padded and centered. A
9
+ // renderer (React, vanilla, canvas) draws node rects via `rectToMinimap` and
10
+ // the viewport lens via `viewportRect`, and maps pointer input back through
11
+ // `toCanvas` — no rendering logic lives here, no math lives in renderers.
12
+
13
+ export type MinimapProjectionOptions = {
14
+ /** Canvas-space bounds the minimap must cover (e.g. `getGraphBounds()`). */
15
+ contentBounds: Rect;
16
+ /** Current viewport — the lens rect derives from it. */
17
+ viewport: Viewport;
18
+ /** Main container size in screen px. */
19
+ viewportSize: Size;
20
+ /** Minimap surface size in px. */
21
+ width: number;
22
+ height: number;
23
+ /** Padding around content as a fraction of its size (default 0.1). */
24
+ padding?: number;
25
+ };
26
+
27
+ export type MinimapProjection = {
28
+ /** Canvas px → minimap px. */
29
+ scale: number;
30
+ /** The padded canvas-space rect the minimap covers. */
31
+ bounds: Rect;
32
+ /** The main view's visible area, in minimap coordinates (the "lens"). */
33
+ viewportRect: Rect;
34
+ toMinimap(point: Point): Point;
35
+ toCanvas(point: Point): Point;
36
+ rectToMinimap(rect: Rect): Rect;
37
+ };
38
+
39
+ export type MinimapViewportCenterOptions = {
40
+ /** Pointer position in canvas space. */
41
+ point: Point;
42
+ /** Pointer offset from the lens center, captured at pointer-down. */
43
+ grabOffset?: Point;
44
+ /** Graph bounds that minimap-driven navigation may move around. */
45
+ contentBounds: Rect;
46
+ /** Current visible canvas bounds; only its size is used. */
47
+ viewportBounds: Rect;
48
+ };
49
+
50
+ /**
51
+ * Resolve a minimap pointer to a bounded viewport center.
52
+ *
53
+ * This constraint applies only to minimap navigation. Each axis lets the lens
54
+ * move fully past the graph, stopping when its nearest edge reaches the graph.
55
+ */
56
+ export function getMinimapViewportCenter(
57
+ options: MinimapViewportCenterOptions,
58
+ ): Point {
59
+ const grabOffset = options.grabOffset ?? { x: 0, y: 0 };
60
+ const target = {
61
+ x: options.point.x - grabOffset.x,
62
+ y: options.point.y - grabOffset.y,
63
+ };
64
+ const halfWidth = Math.max(0, options.viewportBounds.width) / 2;
65
+ const halfHeight = Math.max(0, options.viewportBounds.height) / 2;
66
+ const minX = options.contentBounds.x - halfWidth;
67
+ const maxX =
68
+ options.contentBounds.x + options.contentBounds.width + halfWidth;
69
+ const minY = options.contentBounds.y - halfHeight;
70
+ const maxY =
71
+ options.contentBounds.y + options.contentBounds.height + halfHeight;
72
+
73
+ return {
74
+ x: Math.min(Math.max(target.x, minX), maxX),
75
+ y: Math.min(Math.max(target.y, minY), maxY),
76
+ };
77
+ }
78
+
79
+
80
+ export function getMinimapProjection(
81
+ options: MinimapProjectionOptions,
82
+ ): MinimapProjection {
83
+ const padding = options.padding ?? 0.1;
84
+ const content = options.contentBounds;
85
+ const visible = getVisibleBounds(options.viewport, options.viewportSize);
86
+
87
+ const padX = content.width * padding;
88
+ const padY = content.height * padding;
89
+ let bounds: Rect = {
90
+ x: content.x - padX,
91
+ y: content.y - padY,
92
+ width: content.width + padX * 2,
93
+ height: content.height + padY * 2,
94
+ };
95
+ // Always include the visible area so the lens never leaves the minimap.
96
+ if (visible.width > 0 && visible.height > 0) {
97
+ bounds = unionRect(bounds, visible);
98
+ }
99
+ if (bounds.width <= 0 || bounds.height <= 0) {
100
+ bounds = { x: bounds.x, y: bounds.y, width: 1, height: 1 };
101
+ }
102
+
103
+ const scale = Math.min(
104
+ options.width / bounds.width,
105
+ options.height / bounds.height,
106
+ );
107
+ // Center the covered area on the minimap surface.
108
+ const offsetX = (options.width - bounds.width * scale) / 2;
109
+ const offsetY = (options.height - bounds.height * scale) / 2;
110
+
111
+ const toMinimap = (point: Point): Point => ({
112
+ x: (point.x - bounds.x) * scale + offsetX,
113
+ y: (point.y - bounds.y) * scale + offsetY,
114
+ });
115
+ const toCanvas = (point: Point): Point => ({
116
+ x: (point.x - offsetX) / scale + bounds.x,
117
+ y: (point.y - offsetY) / scale + bounds.y,
118
+ });
119
+ const rectToMinimap = (rect: Rect): Rect => {
120
+ const origin = toMinimap(rect);
121
+ return {
122
+ x: origin.x,
123
+ y: origin.y,
124
+ width: rect.width * scale,
125
+ height: rect.height * scale,
126
+ };
127
+ };
128
+
129
+ return {
130
+ scale,
131
+ bounds,
132
+ viewportRect: rectToMinimap(visible),
133
+ toMinimap,
134
+ toCanvas,
135
+ rectToMinimap,
136
+ };
137
+ }
@@ -0,0 +1,178 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { createFlowStore } from './store';
3
+ import { createFlowEngine } from './engine';
4
+ import { createGraph } from './factories';
5
+ import { makeNestedFixture } from './__fixtures__/nested';
6
+
7
+ // REFRAME phase-3 gate: a nested statechart fixture (3 levels, 200 nodes)
8
+ // passes drag/undo/layout round-trips, with edge-label containment intact.
9
+
10
+ function makeRig() {
11
+ const { nodes, edges } = makeNestedFixture();
12
+ const store = createFlowStore(createGraph({ nodes, edges }));
13
+ const engine = createFlowEngine(store);
14
+ return { store, engine, nodes, edges };
15
+ }
16
+
17
+ function geometry(nodes: ReadonlyArray<{ id: string; x: number; y: number; dx?: number; dy?: number; parentId?: string | null }>) {
18
+ return nodes.map((node) => ({
19
+ id: node.id,
20
+ x: node.x,
21
+ y: node.y,
22
+ dx: node.dx ?? 0,
23
+ dy: node.dy ?? 0,
24
+ parentId: node.parentId ?? null,
25
+ }));
26
+ }
27
+
28
+ function rectContainsRect(
29
+ outer: { x: number; y: number; width: number; height: number },
30
+ inner: { x: number; y: number; width: number; height: number },
31
+ ): boolean {
32
+ return (
33
+ inner.x >= outer.x - 0.001 &&
34
+ inner.y >= outer.y - 0.001 &&
35
+ inner.x + inner.width <= outer.x + outer.width + 0.001 &&
36
+ inner.y + inner.height <= outer.y + outer.height + 0.001
37
+ );
38
+ }
39
+
40
+ describe('nested fixture gate (3 levels, 200 nodes)', () => {
41
+ it('has the gate shape: 200 nodes across 3 levels', () => {
42
+ const { nodes } = makeRig();
43
+ expect(nodes).toHaveLength(200);
44
+ const depths = new Map<string, number>();
45
+ for (const node of nodes) {
46
+ const depth = node.parentId ? (depths.get(node.parentId) ?? 0) + 1 : 0;
47
+ depths.set(node.id, depth);
48
+ }
49
+ expect(Math.max(...depths.values())).toBe(2); // 0,1,2 = 3 levels
50
+ });
51
+
52
+ it('every group contains its children and owned labels at rest', () => {
53
+ const { store, engine } = makeRig();
54
+ const c = store.getSnapshot().context;
55
+ for (const node of c.graph.nodes) {
56
+ if (!node.parentId) continue;
57
+ const parentArea = engine.getGroupGeometry(node.parentId)!.childArea;
58
+ expect(rectContainsRect(parentArea, engine.resolvedBounds(node.id))).toBe(true);
59
+ }
60
+ for (const edge of c.graph.edges) {
61
+ const owner = engine.getEdgeOwner(edge.id);
62
+ if (!owner || !edge.label) continue;
63
+ expect(
64
+ rectContainsRect(engine.getGroupGeometry(owner)!.childArea, {
65
+ x: edge.label.x,
66
+ y: edge.label.y,
67
+ width: edge.label.width,
68
+ height: edge.label.height,
69
+ }),
70
+ ).toBe(true);
71
+ }
72
+ });
73
+
74
+ it('leaf drag round-trip: ancestors track live, undo restores everything', () => {
75
+ const { store, engine } = makeRig();
76
+ const top = engine.resolvedBounds('top3');
77
+ const mid = engine.resolvedBounds('top3.mid2');
78
+ const graphBefore = store.getSnapshot().context.graph;
79
+
80
+ store.trigger.select({ ids: ['top3.mid2.leaf4'] });
81
+ store.trigger.dragStart({ id: 'top3.mid2.leaf4', transactionId: 'tx-leaf' });
82
+ store.trigger.dragMove({ dx: 500, dy: 700 });
83
+
84
+ // both ancestor levels stretch live
85
+ const midDuring = engine.resolvedBounds('top3.mid2');
86
+ const topDuring = engine.resolvedBounds('top3');
87
+ expect(midDuring.height).toBeGreaterThan(mid.height + 500);
88
+ expect(topDuring.height).toBeGreaterThan(top.height + 500);
89
+ // containment never breaks mid-drag
90
+ expect(
91
+ rectContainsRect(
92
+ engine.getGroupGeometry('top3.mid2')!.childArea,
93
+ engine.resolvedBounds('top3.mid2.leaf4'),
94
+ ),
95
+ ).toBe(true);
96
+
97
+ store.trigger.dragEnd();
98
+ store.trigger.commitDrag({ transactionId: 'tx-leaf' });
99
+ store.trigger.undo();
100
+
101
+ expect(geometry(store.getSnapshot().context.graph.nodes)).toEqual(
102
+ geometry(graphBefore.nodes),
103
+ );
104
+ expect(engine.resolvedBounds('top3')).toEqual(top);
105
+ });
106
+
107
+ it('mid-group drag round-trip: subtree moves as one undo step', () => {
108
+ const { store, engine } = makeRig();
109
+ const leafBefore = engine.resolvedBounds('top5.mid1.leaf3');
110
+ const topBefore = engine.resolvedBounds('top5');
111
+
112
+ store.trigger.select({ ids: ['top5.mid1'] });
113
+ store.trigger.dragStart({ id: 'top5.mid1', transactionId: 'tx-mid' });
114
+ store.trigger.dragMove({ dx: -120, dy: 90 });
115
+ store.trigger.dragEnd();
116
+ store.trigger.commitDrag({ transactionId: 'tx-mid' });
117
+
118
+ const leafAfter = engine.resolvedBounds('top5.mid1.leaf3');
119
+ expect(leafAfter.x).toBe(leafBefore.x - 120);
120
+ expect(leafAfter.y).toBe(leafBefore.y + 90);
121
+
122
+ store.trigger.undo();
123
+ expect(engine.resolvedBounds('top5.mid1.leaf3')).toEqual(leafBefore);
124
+ expect(engine.resolvedBounds('top5')).toEqual(topBefore);
125
+ });
126
+
127
+ it('layout round-trip: bulk position changes re-derive all groups; undo restores', () => {
128
+ const { store, engine } = makeRig();
129
+ const before = store.getSnapshot().context.graph;
130
+ const topBefore = engine.resolvedBounds('top0');
131
+
132
+ // a layout pass = one applyLayoutResult with new positions for all leaves
133
+ const c = store.getSnapshot().context;
134
+ const positions = new Map<string, { x: number; y: number }>();
135
+ for (const node of c.graph.nodes) {
136
+ if (node.parentId && !node.group) {
137
+ positions.set(node.id, { x: node.x + 50, y: node.y + 30 });
138
+ }
139
+ }
140
+ const edgeLabelPositions = new Map<string, { x: number; y: number }>();
141
+ for (const edge of c.graph.edges) {
142
+ if (edge.label) {
143
+ edgeLabelPositions.set(edge.id, {
144
+ x: edge.label.x + 50,
145
+ y: edge.label.y + 30,
146
+ });
147
+ }
148
+ }
149
+ store.trigger.applyLayoutResult({ positions, edgeLabelPositions });
150
+
151
+ const topAfter = engine.resolvedBounds('top0');
152
+ expect(topAfter.x).toBe(topBefore.x + 50);
153
+ expect(topAfter.width).toBe(topBefore.width); // pure translation
154
+
155
+ store.trigger.undo();
156
+ expect(geometry(store.getSnapshot().context.graph.nodes)).toEqual(
157
+ geometry(before.nodes),
158
+ );
159
+ });
160
+
161
+ it('owned-label drag stretches the mid group and reverses bit-identically', () => {
162
+ const { store, engine } = makeRig();
163
+ const before = engine.resolvedBounds('top1.mid0');
164
+
165
+ store.trigger.select({ ids: ['top1.mid0.e2'] });
166
+ store.trigger.dragStart({ id: 'top1.mid0.e2' });
167
+ store.trigger.dragMove({ dx: 0, dy: 800 });
168
+ // label e2 sits at y=370 (h 18); dragged +800, the group's bottom edge
169
+ // tracks it exactly: 370 + 800 + 18 + padding 16
170
+ const during = engine.resolvedBounds('top1.mid0');
171
+ expect(during.y + during.height).toBe(370 + 800 + 18 + 16);
172
+
173
+ store.trigger.dragMove({ dx: 0, dy: -800 });
174
+ store.trigger.dragEnd();
175
+ store.trigger.commitDrag();
176
+ expect(engine.resolvedBounds('top1.mid0')).toEqual(before);
177
+ });
178
+ });
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createFlowStore } from './store';
3
+ import { createFlowEngine } from './engine';
4
+ import { createEdge, createGraph, createNode, createPort } from './factories';
5
+
6
+ describe('measured port endpoints', () => {
7
+ it.each([false, true])('centers new connections with snapToGrid=%s', (snapToGrid) => {
8
+ const store = createFlowStore(createGraph({ edges: [], nodes: [
9
+ createNode({ id: 'a', x: 180, y: 96, width: 176, height: 72,
10
+ ports: [createPort({ name: 'out', direction: 'out', side: 'bottom' })] }),
11
+ createNode({ id: 'b', x: 536, y: 280, width: 176, height: 72,
12
+ ports: [createPort({ name: 'in', direction: 'in', side: 'top' })] }),
13
+ ] }), { snapToGrid, snapGrid: [16, 16] });
14
+ const engine = createFlowEngine(store);
15
+ const sourceBounds = engine.visualBounds('a');
16
+ const targetBounds = engine.visualBounds('b');
17
+ const source = { x: sourceBounds.x + 88, y: sourceBounds.y + 70 };
18
+ const target = { x: targetBounds.x + 88, y: targetBounds.y + 2 };
19
+ store.trigger.updateMeasurements({ measurements: [
20
+ { id: 'a:out', rect: { x: source.x - 8, y: source.y - 8, width: 16, height: 16 } },
21
+ { id: 'b:in', rect: { x: target.x - 8, y: target.y - 8, width: 16, height: 16 } },
22
+ ] });
23
+ store.trigger.createEdge({ edge: createEdge({ id: 'e', sourceId: 'a', targetId: 'b', sourcePort: 'out', targetPort: 'in' }) });
24
+ expect(engine.getEdgeEndpoints('e')).toEqual({ source, target });
25
+ expect(engine.getAllEdgePaths().get('e')!.segments[0].points[0]).toEqual(source);
26
+ });
27
+ });