@statelyai/flow 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +2 -1
  2. package/dist/esm/index.d.ts +5 -2
  3. package/dist/esm/index.d.ts.map +1 -0
  4. package/dist/esm/index.js +19 -14
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/esm/layout/dagre.d.ts +2 -1
  7. package/dist/esm/layout/dagre.d.ts.map +1 -0
  8. package/dist/esm/layout/dagre.js +2 -0
  9. package/dist/esm/layout/dagre.js.map +1 -0
  10. package/dist/esm/layout/elk.d.ts +2 -1
  11. package/dist/esm/layout/elk.d.ts.map +1 -0
  12. package/dist/esm/layout/elk.js +2 -0
  13. package/dist/esm/layout/elk.js.map +1 -0
  14. package/dist/esm/layout/stately.d.ts +2 -1
  15. package/dist/esm/layout/stately.d.ts.map +1 -0
  16. package/dist/esm/layout/stately.js +2 -0
  17. package/dist/esm/layout/stately.js.map +1 -0
  18. package/dist/esm/shared.d.ts +2 -1
  19. package/dist/esm/shared.d.ts.map +1 -0
  20. package/dist/esm/shared.js +2 -0
  21. package/dist/esm/shared.js.map +1 -0
  22. package/package.json +4 -3
  23. package/src/__fixtures__/nested.ts +106 -0
  24. package/src/appearances.test.ts +428 -0
  25. package/src/appearances.ts +144 -0
  26. package/src/authoritative-route.ts +14 -0
  27. package/src/changeSet.test.ts +132 -0
  28. package/src/changeSet.ts +337 -0
  29. package/src/config.ts +41 -0
  30. package/src/configure.test.ts +212 -0
  31. package/src/connection-lifetime.ts +1 -0
  32. package/src/connectionPreview.test.ts +102 -0
  33. package/src/connectionPreview.ts +82 -0
  34. package/src/content.ts +203 -0
  35. package/src/createFlow.fit-frame.test.ts +256 -0
  36. package/src/createFlow.modern.test.ts +743 -0
  37. package/src/createFlow.test.ts +632 -0
  38. package/src/createFlow.ts +3463 -0
  39. package/src/createFlow.view.test.ts +212 -0
  40. package/src/createFlow.viewport-controls.test.ts +221 -0
  41. package/src/edge-geometry.ts +318 -0
  42. package/src/engine.cache.test.ts +728 -0
  43. package/src/engine.hierarchy.test.ts +1023 -0
  44. package/src/engine.test.ts +1965 -0
  45. package/src/engine.ts +3385 -0
  46. package/src/entity-changes.test.ts +309 -0
  47. package/src/entity-changes.ts +395 -0
  48. package/src/examples/flow-based-math.test.ts +25 -0
  49. package/src/examples/flow-based-math.ts +210 -0
  50. package/src/external-edit.test.ts +337 -0
  51. package/src/external-edit.ts +358 -0
  52. package/src/factories.test.ts +104 -0
  53. package/src/factories.ts +70 -0
  54. package/src/geometry.ts +13 -0
  55. package/src/graph-index.test.ts +56 -0
  56. package/src/graph-index.ts +194 -0
  57. package/src/hierarchy.test.ts +175 -0
  58. package/src/hierarchy.ts +330 -0
  59. package/src/highlights.test.ts +130 -0
  60. package/src/index.ts +469 -0
  61. package/src/input/index.ts +615 -0
  62. package/src/input/input.test.ts +2577 -0
  63. package/src/input/keymap.ts +111 -0
  64. package/src/input/modes.ts +1715 -0
  65. package/src/input/reparent.test.ts +214 -0
  66. package/src/layout/dagre.ts +138 -0
  67. package/src/layout/elk.ts +1382 -0
  68. package/src/layout/index.ts +12 -0
  69. package/src/layout/layout.test.ts +1248 -0
  70. package/src/layout/shared.ts +260 -0
  71. package/src/layout/stately-globals.d.ts +9 -0
  72. package/src/layout/stately.test.ts +246 -0
  73. package/src/layout/stately.ts +528 -0
  74. package/src/layout-animation.test.ts +223 -0
  75. package/src/layout-contract.test.ts +414 -0
  76. package/src/marker.test.ts +23 -0
  77. package/src/marker.ts +23 -0
  78. package/src/measurement-anchor.test.ts +232 -0
  79. package/src/minimap.test.ts +96 -0
  80. package/src/minimap.ts +137 -0
  81. package/src/nested-fixture.test.ts +178 -0
  82. package/src/port-measurement.test.ts +27 -0
  83. package/src/port-measurements.test.ts +187 -0
  84. package/src/profile.ts +124 -0
  85. package/src/projections.test.ts +103 -0
  86. package/src/projections.ts +295 -0
  87. package/src/render-invalidation.test.ts +62 -0
  88. package/src/render-invalidation.ts +55 -0
  89. package/src/renderer-geometry.test.ts +356 -0
  90. package/src/renderer-transactions.test.ts +234 -0
  91. package/src/routing/avoid.ts +417 -0
  92. package/src/routing/index.test.ts +361 -0
  93. package/src/routing/index.ts +366 -0
  94. package/src/routing/readTracking.test.ts +103 -0
  95. package/src/routing/readTracking.ts +233 -0
  96. package/src/selection-commands.test.ts +355 -0
  97. package/src/serialize.test.ts +167 -0
  98. package/src/serialize.ts +254 -0
  99. package/src/shapes/index.test.ts +45 -0
  100. package/src/shapes/index.ts +142 -0
  101. package/src/snap.test.ts +123 -0
  102. package/src/snap.ts +395 -0
  103. package/src/spatial.test.ts +676 -0
  104. package/src/spatial.ts +457 -0
  105. package/src/store/index.ts +4121 -0
  106. package/src/store.test.ts +2903 -0
  107. package/src/toolbar.test.ts +31 -0
  108. package/src/toolbar.ts +62 -0
  109. package/src/types.test.ts +15 -0
  110. package/src/types.ts +487 -0
  111. package/src/validation.ts +558 -0
  112. package/src/view.ts +172 -0
  113. package/src/viewport/animate.ts +285 -0
  114. package/src/viewport/index.ts +296 -0
  115. package/src/viewport.test.ts +569 -0
@@ -0,0 +1,355 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createEdge, createGraph, createNode, createPort } from './factories';
4
+ import { resolvedBounds } from './spatial';
5
+ import type { FlowGraph, FlowNode } from './types';
6
+
7
+ function hierarchyGraph() {
8
+ return createGraph({
9
+ nodes: [
10
+ createNode({
11
+ id: 'outer',
12
+ x: 100,
13
+ y: 100,
14
+ width: 300,
15
+ height: 240,
16
+ data: { label: 'Outer', meta: { color: 'blue' } },
17
+ ports: [
18
+ createPort({
19
+ name: 'out',
20
+ side: 'right',
21
+ direction: 'out',
22
+ x: 290,
23
+ y: 100,
24
+ }),
25
+ ],
26
+ }),
27
+ createNode({
28
+ id: 'a',
29
+ parentId: 'outer',
30
+ x: 130,
31
+ y: 150,
32
+ width: 80,
33
+ height: 40,
34
+ data: { label: 'A' },
35
+ }),
36
+ createNode({
37
+ id: 'b',
38
+ parentId: 'outer',
39
+ x: 270,
40
+ y: 220,
41
+ width: 90,
42
+ height: 50,
43
+ data: { label: 'B' },
44
+ }),
45
+ createNode({ id: 'outside', x: 600, y: 100 }),
46
+ ],
47
+ edges: [
48
+ createEdge({
49
+ id: 'ab',
50
+ sourceId: 'a',
51
+ targetId: 'b',
52
+ data: { event: 'NEXT' },
53
+ waypoints: [{ x: 230, y: 180 }],
54
+ label: {
55
+ x: 220,
56
+ y: 190,
57
+ width: 70,
58
+ height: 24,
59
+ data: { text: 'NEXT' },
60
+ },
61
+ }),
62
+ createEdge({ id: 'external', sourceId: 'b', targetId: 'outside' }),
63
+ ],
64
+ });
65
+ }
66
+
67
+ function makeHierarchyFlow() {
68
+ const createId = vi.fn(
69
+ ({ sourceId, attempt }: { sourceId?: string; attempt: number; reason?: string }) =>
70
+ `clone-${sourceId}-${attempt}`,
71
+ );
72
+ return {
73
+ flow: createFlow({ graph: hierarchyGraph(), createId }),
74
+ createId,
75
+ };
76
+ }
77
+
78
+ function selectedIds(flow: ReturnType<typeof createFlow>) {
79
+ return [...flow.getSnapshot().selection];
80
+ }
81
+
82
+ describe('selection.duplicate', () => {
83
+ it('duplicates a hierarchy and internal edge through content ID machinery', () => {
84
+ const { flow, createId } = makeHierarchyFlow();
85
+ flow.selection.set(['outer']);
86
+
87
+ const result = flow.selection.duplicate({ offset: { x: 12, y: 18 } });
88
+ expect(result).not.toBeNull();
89
+ expect(result!.nodeIds).toHaveLength(3);
90
+ expect(result!.edgeIds).toHaveLength(1);
91
+ expect(selectedIds(flow)).toEqual([...result!.nodeIds, ...result!.edgeIds]);
92
+ expect(createId).toHaveBeenCalledTimes(4);
93
+ expect(createId.mock.calls.every(([context]) => context.reason === 'content')).toBe(true);
94
+
95
+ const cloneOuterId = result!.idMap.get('outer')!;
96
+ const cloneAId = result!.idMap.get('a')!;
97
+ const cloneBId = result!.idMap.get('b')!;
98
+ const cloneEdgeId = result!.idMap.get('ab')!;
99
+ const cloneOuter = flow.getNode(cloneOuterId)!;
100
+ const cloneA = flow.getNode(cloneAId)!;
101
+ const cloneB = flow.getNode(cloneBId)!;
102
+ const cloneEdge = flow.getEdge(cloneEdgeId)!;
103
+
104
+ expect(cloneOuter).toMatchObject({ x: 112, y: 118 });
105
+ expect(cloneOuter.parentId ?? null).toBeNull();
106
+ expect(cloneA).toMatchObject({ x: 142, y: 168, parentId: cloneOuterId });
107
+ expect(cloneB).toMatchObject({ x: 282, y: 238, parentId: cloneOuterId });
108
+ expect(cloneOuter.data).toEqual(flow.getNode('outer')!.data);
109
+ expect(cloneOuter.data).not.toBe(flow.getNode('outer')!.data);
110
+ expect(cloneOuter.ports).toEqual(flow.getNode('outer')!.ports);
111
+ expect(cloneEdge).toMatchObject({
112
+ sourceId: cloneAId,
113
+ targetId: cloneBId,
114
+ data: { event: 'NEXT' },
115
+ label: { x: 232, y: 208, data: { text: 'NEXT' } },
116
+ waypoints: [{ x: 242, y: 198 }],
117
+ });
118
+ expect(flow.getEdge('external')).toBeDefined();
119
+ expect(result!.idMap.has('external')).toBe(false);
120
+ });
121
+
122
+ it('retains an unselected external parent and uses the default offset', () => {
123
+ const { flow } = makeHierarchyFlow();
124
+ flow.selection.set(['a']);
125
+
126
+ const result = flow.selection.duplicate()!;
127
+ const clone = flow.getNode(result.idMap.get('a')!)!;
128
+ expect(clone).toMatchObject({
129
+ parentId: 'outer',
130
+ x: 154,
131
+ y: 174,
132
+ });
133
+ expect(result.edgeIds).toEqual([]);
134
+ });
135
+
136
+ it('does not retain unshifted authoritative geometry on cloned edges', () => {
137
+ const input = hierarchyGraph();
138
+ Object.assign(input.edges.find((edge) => edge.id === 'ab')!, {
139
+ points: [{ x: 130, y: 160 }, { x: 270, y: 230 }],
140
+ routing: 'polyline',
141
+ svgPath: 'M 130 160 L 270 230',
142
+ });
143
+ const flow = createFlow({
144
+ graph: input,
145
+ createId: ({ sourceId, attempt }) => `clone-${sourceId}-${attempt}`,
146
+ });
147
+ flow.selection.set(['outer']);
148
+
149
+ const result = flow.selection.duplicate({ offset: { x: 12, y: 18 } })!;
150
+ const clone = flow.getEdge(result.idMap.get('ab')!)!;
151
+
152
+ expect(clone).not.toHaveProperty('points');
153
+ expect(clone).not.toHaveProperty('routing');
154
+ expect(clone).not.toHaveProperty('svgPath');
155
+ expect(flow.getEdge('ab')).toHaveProperty('points');
156
+ });
157
+
158
+ it('is one undo step and redo restores the same clone graph', () => {
159
+ const { flow } = makeHierarchyFlow();
160
+ flow.selection.set(['outer']);
161
+ const before = flow.toJSON().graph;
162
+ const result = flow.selection.duplicate({ offset: { x: 0, y: 0 } })!;
163
+ const duplicated = flow.toJSON().graph;
164
+
165
+ flow.history.undo();
166
+ expect(flow.toJSON().graph).toEqual(before);
167
+ expect(flow.history.canUndo()).toBe(false);
168
+
169
+ flow.history.redo();
170
+ expect(flow.toJSON().graph).toEqual(duplicated);
171
+ expect(flow.getNode(result.idMap.get('outer')!)).toBeDefined();
172
+ });
173
+
174
+ it('no-ops when no node is selected or editing is ineligible', () => {
175
+ const { flow } = makeHierarchyFlow();
176
+ const before = flow.getSnapshot();
177
+ flow.selection.set(['ab']);
178
+ const selectedEdge = flow.getSnapshot();
179
+ expect(flow.selection.duplicate()).toBeNull();
180
+ expect(flow.getSnapshot()).toBe(selectedEdge);
181
+
182
+ flow.selection.set(['outer']);
183
+ flow.mode.set('readonly');
184
+ const readonly = flow.getSnapshot();
185
+ expect(flow.selection.duplicate()).toBeNull();
186
+ expect(flow.getSnapshot()).toBe(readonly);
187
+ expect(flow.toJSON().graph).toEqual(before.graph);
188
+ });
189
+ });
190
+
191
+ function geometryFlow(nodes: FlowNode[]) {
192
+ return createFlow({ graph: createGraph({ nodes, edges: [] }) });
193
+ }
194
+
195
+ describe('selection.align', () => {
196
+ it.each([
197
+ ['left', 'x', 15],
198
+ ['right', 'right', 260],
199
+ ['top', 'y', 25],
200
+ ['bottom', 'bottom', 230],
201
+ ['center-x', 'centerX', 137.5],
202
+ ['center-y', 'centerY', 127.5],
203
+ ] as const)('aligns %s from exact visual bounds', (alignment, metric, expected) => {
204
+ const flow = geometryFlow([
205
+ createNode({ id: 'a', x: 10, y: 20, dx: 5, dy: 5, width: 100, height: 50 }),
206
+ createNode({ id: 'b', x: 210, y: 170, dx: 0, dy: 0, width: 50, height: 60 }),
207
+ ]);
208
+ flow.selection.set(['a', 'b'], 'multi');
209
+ expect(flow.selection.align(alignment)).toBe(true);
210
+
211
+ const values = ['a', 'b'].map((id) => {
212
+ const bounds = resolvedBounds(flow.getNode(id)!);
213
+ if (metric === 'right') return bounds.x + bounds.width;
214
+ if (metric === 'bottom') return bounds.y + bounds.height;
215
+ if (metric === 'centerX') return bounds.x + bounds.width / 2;
216
+ if (metric === 'centerY') return bounds.y + bounds.height / 2;
217
+ return bounds[metric];
218
+ });
219
+ expect(values).toEqual([expected, expected]);
220
+ });
221
+
222
+ it('is undoable/redoable and no-ops with fewer than two nodes', () => {
223
+ const flow = geometryFlow([
224
+ createNode({ id: 'a', x: 0 }),
225
+ createNode({ id: 'b', x: 200 }),
226
+ ]);
227
+ flow.selection.set(['a']);
228
+ const one = flow.getSnapshot();
229
+ expect(flow.selection.align('left')).toBe(false);
230
+ expect(flow.getSnapshot()).toBe(one);
231
+
232
+ flow.selection.set(['a', 'b'], 'multi');
233
+ expect(flow.selection.align('left')).toBe(true);
234
+ expect(flow.getNode('b')!.x).toBe(0);
235
+ flow.history.undo();
236
+ expect(flow.getNode('b')!.x).toBe(200);
237
+ flow.history.redo();
238
+ expect(flow.getNode('b')!.x).toBe(0);
239
+ });
240
+
241
+ it('invalidates routes incident to aligned nodes', () => {
242
+ const flow = createFlow({
243
+ graph: createGraph({
244
+ nodes: [createNode({ id: 'a', x: 0 }), createNode({ id: 'b', x: 200 })],
245
+ edges: [
246
+ createEdge({
247
+ id: 'ab',
248
+ sourceId: 'a',
249
+ targetId: 'b',
250
+ points: [{ x: 0, y: 0 }, { x: 200, y: 0 }],
251
+ routing: 'polyline',
252
+ svgPath: 'M 0 0 L 200 0',
253
+ }),
254
+ ],
255
+ }),
256
+ });
257
+ flow.selection.set(['a', 'b'], 'multi');
258
+
259
+ flow.selection.align('left');
260
+
261
+ expect(flow.getEdge('ab')).not.toHaveProperty('points');
262
+ expect(flow.getEdge('ab')).not.toHaveProperty('routing');
263
+ expect(flow.getEdge('ab')).not.toHaveProperty('svgPath');
264
+ });
265
+ });
266
+
267
+ function positionsById(graph: FlowGraph) {
268
+ const flow = createFlow({ graph });
269
+ flow.selection.set(graph.nodes.map((node) => node.id), 'multi');
270
+ flow.selection.distribute('horizontal');
271
+ return Object.fromEntries(flow.getSnapshot().graph.nodes.map((node) => [node.id, node.x]));
272
+ }
273
+
274
+ describe('selection.distribute', () => {
275
+ it('keeps deterministic endpoints and equal horizontal gaps', () => {
276
+ const nodes = [
277
+ createNode({ id: 'b', x: 0, y: 0, width: 50 }),
278
+ createNode({ id: 'd', x: 400, y: 0, width: 50 }),
279
+ createNode({ id: 'c', x: 200, y: 0, width: 50 }),
280
+ createNode({ id: 'a', x: 0, y: 0, width: 50 }),
281
+ ];
282
+ const forward = positionsById(createGraph({ nodes, edges: [] }));
283
+ const reverse = positionsById(
284
+ createGraph({ nodes: [...nodes].reverse(), edges: [] }),
285
+ );
286
+ expect(forward).toEqual(reverse);
287
+ expect(forward.a).toBe(0);
288
+ expect(forward.d).toBe(400);
289
+ expect(forward.b).toBeCloseTo(400 / 3);
290
+ expect(forward.c).toBeCloseTo(800 / 3);
291
+ });
292
+
293
+ it('distributes vertically using varied visual heights', () => {
294
+ const flow = geometryFlow([
295
+ createNode({ id: 'a', y: 0, height: 40 }),
296
+ createNode({ id: 'b', y: 80, height: 80 }),
297
+ createNode({ id: 'c', y: 300, height: 60 }),
298
+ ]);
299
+ flow.selection.set(['a', 'b', 'c'], 'multi');
300
+ expect(flow.selection.distribute('vertical')).toBe(true);
301
+ const bounds = ['a', 'b', 'c'].map((id) => resolvedBounds(flow.getNode(id)!));
302
+ const gaps = [bounds[1]!.y - (bounds[0]!.y + bounds[0]!.height), bounds[2]!.y - (bounds[1]!.y + bounds[1]!.height)];
303
+ expect(gaps[0]).toBeCloseTo(gaps[1]!);
304
+ });
305
+
306
+ it('requires three nodes and is one undo/redo step', () => {
307
+ const flow = geometryFlow([
308
+ createNode({ id: 'a', x: 0 }),
309
+ createNode({ id: 'b', x: 100 }),
310
+ createNode({ id: 'c', x: 400 }),
311
+ ]);
312
+ flow.selection.set(['a', 'b']);
313
+ const two = flow.getSnapshot();
314
+ expect(flow.selection.distribute('horizontal')).toBe(false);
315
+ expect(flow.getSnapshot()).toBe(two);
316
+
317
+ flow.selection.set(['a', 'b', 'c'], 'multi');
318
+ expect(flow.selection.distribute('horizontal')).toBe(true);
319
+ const distributed = flow.getNode('b')!.x;
320
+ flow.history.undo();
321
+ expect(flow.getNode('b')!.x).toBe(100);
322
+ expect(flow.history.canUndo()).toBe(false);
323
+ flow.history.redo();
324
+ expect(flow.getNode('b')!.x).toBe(distributed);
325
+ });
326
+
327
+ it('invalidates routes incident to distributed nodes', () => {
328
+ const flow = createFlow({
329
+ graph: createGraph({
330
+ nodes: [
331
+ createNode({ id: 'a', x: 0 }),
332
+ createNode({ id: 'b', x: 100 }),
333
+ createNode({ id: 'c', x: 400 }),
334
+ ],
335
+ edges: [
336
+ createEdge({
337
+ id: 'ab',
338
+ sourceId: 'a',
339
+ targetId: 'b',
340
+ points: [{ x: 0, y: 0 }, { x: 100, y: 0 }],
341
+ routing: 'orthogonal',
342
+ svgPath: 'M 0 0 L 100 0',
343
+ }),
344
+ ],
345
+ }),
346
+ });
347
+ flow.selection.set(['a', 'b', 'c'], 'multi');
348
+
349
+ flow.selection.distribute('horizontal');
350
+
351
+ expect(flow.getEdge('ab')).not.toHaveProperty('points');
352
+ expect(flow.getEdge('ab')).not.toHaveProperty('routing');
353
+ expect(flow.getEdge('ab')).not.toHaveProperty('svgPath');
354
+ });
355
+ });
@@ -0,0 +1,167 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createEdge, createGraph, createNode, createPort } from './factories';
3
+ import { FlowGraphValidationError } from './validation';
4
+ import { parseFlowSerialized, serializeFlowGraph } from './serialize';
5
+
6
+ function document(overrides: Record<string, unknown> = {}) {
7
+ return {
8
+ version: 1,
9
+ graph: createGraph({
10
+ nodes: [createNode({ id: 'a', data: { value: 1 } })],
11
+ edges: [],
12
+ }),
13
+ viewport: { x: 10, y: 20, zoom: 2 },
14
+ ...overrides,
15
+ };
16
+ }
17
+
18
+ describe('parseFlowSerialized', () => {
19
+ it('upgrades versionless documents and returns detached data', () => {
20
+ const input = document({ version: undefined });
21
+ const parsed = parseFlowSerialized(input);
22
+
23
+ expect(parsed).toMatchObject({
24
+ version: 1,
25
+ viewport: { x: 10, y: 20, zoom: 2 },
26
+ });
27
+ expect(parsed.graph).not.toBe(input.graph);
28
+ expect(parsed.graph.nodes[0]).not.toBe(
29
+ (input.graph as ReturnType<typeof createGraph>).nodes[0],
30
+ );
31
+ });
32
+
33
+ it.each([
34
+ [null, 'invalid_document'],
35
+ [{ version: 2, graph: {} }, 'unsupported_version'],
36
+ [{ version: 1 }, 'invalid_graph'],
37
+ ])('rejects invalid documents without partially parsing them', (input, code) => {
38
+ try {
39
+ parseFlowSerialized(input);
40
+ throw new Error('expected parsing to fail');
41
+ } catch (error) {
42
+ expect(error).toBeInstanceOf(FlowGraphValidationError);
43
+ expect((error as FlowGraphValidationError).issues[0]?.code).toBe(code);
44
+ }
45
+ });
46
+
47
+ it('migrates stored data synchronously and records current versions', () => {
48
+ const parsed = parseFlowSerialized<{ value: number }>(
49
+ document({ dataVersions: { node: 1 } }),
50
+ {
51
+ node: {
52
+ version: 2,
53
+ migrate: (data, versions) => ({
54
+ value: (data as { value: number }).value + versions.toVersion,
55
+ }),
56
+ },
57
+ },
58
+ );
59
+
60
+ expect(parsed.graph.nodes[0].data).toEqual({ value: 3 });
61
+ expect(parsed.dataVersions).toEqual({ node: 2 });
62
+ });
63
+
64
+ it.each([
65
+ [{ version: 2 }, 'missing_data_migration'],
66
+ [{ version: 1, migrate: () => Promise.resolve({ value: 2 }) }, 'async_data_migration'],
67
+ ])('rejects unusable migration contracts', (nodeSchema, code) => {
68
+ expect(() =>
69
+ parseFlowSerialized(document({ dataVersions: { node: 0 } }), {
70
+ node: nodeSchema,
71
+ }),
72
+ ).toThrow(FlowGraphValidationError);
73
+
74
+ try {
75
+ parseFlowSerialized(document({ dataVersions: { node: 0 } }), {
76
+ node: nodeSchema,
77
+ });
78
+ } catch (error) {
79
+ expect((error as FlowGraphValidationError).issues[0]?.code).toBe(code);
80
+ }
81
+ });
82
+
83
+ it('rejects data newer than its schema and stale collapsed ids', () => {
84
+ expect(() =>
85
+ parseFlowSerialized(document({ dataVersions: { node: 2 } }), {
86
+ node: { version: 1 },
87
+ }),
88
+ ).toThrow(/newer than supported/);
89
+ expect(() =>
90
+ parseFlowSerialized(document({ collapsedNodeIds: ['missing'] })),
91
+ ).toThrow(/does not exist/);
92
+ });
93
+ });
94
+
95
+ describe('serializeFlowGraph', () => {
96
+ it('folds transient offsets and strips measured session state', () => {
97
+ const graph = createGraph({
98
+ nodes: [
99
+ createNode({
100
+ id: 'a',
101
+ x: 10,
102
+ y: 20,
103
+ dx: 5,
104
+ dy: -3,
105
+ width: 100,
106
+ height: 50,
107
+ autoSize: true,
108
+ measured: { width: 123, height: 45 },
109
+ style: { customWidth: 140, customHeight: 60 },
110
+ ports: [
111
+ createPort({ name: 'out', side: 'right', direction: 'out', x: 100, y: 20 }),
112
+ ],
113
+ resize: { min: { width: 80 }, max: { height: 200 } },
114
+ group: { type: 'derived', content: 'top' },
115
+ }),
116
+ ],
117
+ edges: [
118
+ createEdge({
119
+ id: 'ab',
120
+ sourceId: 'a',
121
+ targetId: 'a',
122
+ dx: 5,
123
+ dy: -2,
124
+ points: [{ x: 10, y: 20 }, { x: 90, y: 80 }],
125
+ routing: 'orthogonal',
126
+ svgPath: 'M 10 20 L 90 80',
127
+ waypoints: [{ x: 70, y: 80 }],
128
+ label: {
129
+ x: 30,
130
+ y: 40,
131
+ dx: 7,
132
+ dy: 8,
133
+ width: 50,
134
+ height: 20,
135
+ },
136
+ }),
137
+ ],
138
+ });
139
+
140
+ const serialized = serializeFlowGraph(graph);
141
+
142
+ expect(serialized.nodes[0]).toMatchObject({
143
+ id: 'a',
144
+ x: 15,
145
+ y: 17,
146
+ style: { customWidth: 140, customHeight: 60 },
147
+ resize: { min: { width: 80 }, max: { height: 200 } },
148
+ });
149
+ expect(serialized.nodes[0].dx).toBeUndefined();
150
+ expect(serialized.nodes[0].dy).toBeUndefined();
151
+ expect(serialized.nodes[0].measured).toBeUndefined();
152
+ expect(serialized.edges[0].label).toMatchObject({ x: 37, y: 48 });
153
+ expect(serialized.edges[0].waypoints).toEqual([{ x: 75, y: 78 }]);
154
+ expect(serialized.edges[0].points).toEqual([{ x: 10, y: 20 }, { x: 90, y: 80 }]);
155
+ expect(serialized.edges[0].points?.[0]).not.toBe(graph.edges[0].points?.[0]);
156
+ expect(serialized.edges[0].routing).toBe('orthogonal');
157
+ expect(serialized.edges[0].svgPath).toBe('M 10 20 L 90 80');
158
+ expect(serialized.edges[0].waypoints?.[0]).not.toBe(graph.edges[0].waypoints?.[0]);
159
+ expect(serialized.edges[0].dx).toBeUndefined();
160
+ expect(serialized.edges[0].dy).toBeUndefined();
161
+ expect(serialized.edges[0].label?.dx).toBeUndefined();
162
+ expect(serialized.edges[0].label?.dy).toBeUndefined();
163
+ expect(serialized).not.toBe(graph);
164
+ expect(serialized.nodes[0]).not.toBe(graph.nodes[0]);
165
+ expect(serialized.nodes[0].ports?.[0]).not.toBe(graph.nodes[0].ports?.[0]);
166
+ });
167
+ });