@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,62 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createGraph, createNode } from './factories';
4
+ import {
5
+ createFlowRenderApi,
6
+ type FlowRenderInvalidation,
7
+ } from './render-invalidation';
8
+
9
+ function fixture() {
10
+ return createFlow({
11
+ graph: createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }),
12
+ });
13
+ }
14
+
15
+ describe('render invalidation', () => {
16
+ it('notifies for transient selection, measurements and controller-only projections', () => {
17
+ const flow = fixture();
18
+ const render = createFlowRenderApi(flow);
19
+ const events: FlowRenderInvalidation[] = [];
20
+ render.subscribe((event) => events.push(event));
21
+ flow.selection.set(['a']);
22
+ expect(events.at(-1)?.reason).toBe('context');
23
+ const beforeMeasurement = events.length;
24
+ flow.measurements.update([
25
+ { id: 'a', rect: { x: 0, y: 0, width: 180, height: 90 } },
26
+ ]);
27
+ expect(events.length).toBeGreaterThan(beforeMeasurement);
28
+ const beforeProjection = events.length;
29
+ flow.projections.add({
30
+ projection: { id: 'highlight', nodes: { a: true } },
31
+ mapper: { getNodeAttrs: () => ({ className: 'highlighted' }) },
32
+ });
33
+ expect(
34
+ events
35
+ .slice(beforeProjection)
36
+ .some((event) => event.reason === 'projection')
37
+ ).toBe(true);
38
+ expect(
39
+ events.every((event, index) => event.revision === index + 1 && event.all)
40
+ ).toBe(true);
41
+ flow.destroy();
42
+ });
43
+
44
+ it('unsubscribes idempotently and stops after controller destruction', () => {
45
+ const flow = fixture();
46
+ const render = createFlowRenderApi(flow);
47
+ const listener = vi.fn();
48
+ const unsubscribe = render.subscribe(listener);
49
+ flow.nodes.move('a', { x: 10, y: 20 });
50
+ expect(listener).toHaveBeenCalled();
51
+ listener.mockClear();
52
+ unsubscribe();
53
+ unsubscribe();
54
+ flow.nodes.move('a', { x: 20, y: 20 });
55
+ expect(listener).not.toHaveBeenCalled();
56
+ render.subscribe(listener);
57
+ flow.destroy();
58
+ flow.nodes.update('a', { x: 30 });
59
+ expect(listener).not.toHaveBeenCalled();
60
+ render.dispose();
61
+ });
62
+ });
@@ -0,0 +1,55 @@
1
+ import type { FlowController } from './createFlow';
2
+
3
+ export type FlowRenderInvalidation = {
4
+ revision: number;
5
+ reason: 'context' | 'projection' | 'commit';
6
+ /** Notifications conservatively invalidate every rendered entity. */
7
+ all: true;
8
+ };
9
+
10
+ export type FlowRenderApi = {
11
+ subscribe: (
12
+ listener: (invalidation: FlowRenderInvalidation) => void
13
+ ) => () => void;
14
+ dispose: () => void;
15
+ };
16
+
17
+ /** Synchronous invalidation only; renderers own frame scheduling and coalescing. */
18
+ export function createFlowRenderApi<TNodeData, TEdgeData, TLabelData>(
19
+ flow: FlowController<TNodeData, TEdgeData, TLabelData>
20
+ ): FlowRenderApi {
21
+ const listeners = new Set<(invalidation: FlowRenderInvalidation) => void>();
22
+ let revision = 0;
23
+ let disposed = false;
24
+ const notify = (reason: FlowRenderInvalidation['reason']) => {
25
+ if (disposed) return;
26
+ const event: FlowRenderInvalidation = {
27
+ revision: ++revision,
28
+ reason,
29
+ all: true,
30
+ };
31
+ for (const listener of listeners) listener(event);
32
+ };
33
+ const context = flow.subscribe(() => notify('context'));
34
+ const projection = flow.projections.subscribe(() => notify('projection'));
35
+ const commits = flow.on('commit', () => notify('commit'));
36
+ const destruction = flow.on('destroy', () => dispose());
37
+ function dispose() {
38
+ if (disposed) return;
39
+ disposed = true;
40
+ context();
41
+ projection();
42
+ commits.unsubscribe();
43
+ destruction.unsubscribe();
44
+ listeners.clear();
45
+ }
46
+ return {
47
+ subscribe(listener) {
48
+ if (!disposed) listeners.add(listener);
49
+ return () => {
50
+ listeners.delete(listener);
51
+ };
52
+ },
53
+ dispose,
54
+ };
55
+ }
@@ -0,0 +1,356 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createEdge, createGraph, createNode, createPort } from './factories';
4
+
5
+ const makeFlow = (
6
+ node = createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 })
7
+ ) => createFlow({ graph: createGraph({ nodes: [node], edges: [] }) });
8
+
9
+ describe('renderer geometry', () => {
10
+ it('observes readonly geometry without authored changes, and removes caches', () => {
11
+ const flow = makeFlow(
12
+ createNode({
13
+ id: 'a',
14
+ x: 10,
15
+ y: 20,
16
+ width: 100,
17
+ height: 50,
18
+ autoSize: true,
19
+ autoSizeAnchor: 'center',
20
+ })
21
+ );
22
+ flow.configure({ store: { mode: 'readonly' } });
23
+ const before = flow.toJSON();
24
+ const commit = vi.fn();
25
+ flow.on('commit', commit);
26
+ const rect = { x: 10, y: 20, width: 120.25, height: 60.5 };
27
+ flow.measurements.update([{ id: 'a', rect }]);
28
+ rect.width = 999;
29
+ expect(flow.getBounds('a')?.width).toBe(120.25);
30
+ expect(flow.toJSON()).toEqual(before);
31
+ expect(commit).not.toHaveBeenCalled();
32
+ expect(flow.history.canUndo()).toBe(false);
33
+ const observation = flow.measurements.get('a')!;
34
+ observation.width = 888;
35
+ expect(flow.measurements.get('a')?.width).toBe(120.25);
36
+ flow.measurements.remove(['a', 'missing']);
37
+ expect(flow.measurements.get('a')).toBeUndefined();
38
+ expect(flow.getBounds('a')).toMatchObject({
39
+ x: 10,
40
+ y: 20,
41
+ width: 100,
42
+ height: 50,
43
+ });
44
+ expect(flow.toJSON()).toEqual(before);
45
+ flow.destroy();
46
+ });
47
+
48
+ it('drops observations for deleted and recreated identities atomically', () => {
49
+ const flow = makeFlow();
50
+ flow.measurements.update([
51
+ { id: 'a', rect: { x: 0, y: 0, width: 200, height: 80 } },
52
+ ]);
53
+ flow.transaction((draft) => {
54
+ draft.nodes.delete('a');
55
+ draft.nodes.create({ id: 'a', width: 50, height: 30 });
56
+ });
57
+ expect(flow.measurements.get('a')).toBeUndefined();
58
+ expect(flow.getBounds('a')).toMatchObject({ width: 50, height: 30 });
59
+ flow.measurements.update([
60
+ { id: 'a', rect: { x: 0, y: 0, width: 75, height: 40 } },
61
+ ]);
62
+ expect(() =>
63
+ flow.transaction((draft) => {
64
+ draft.nodes.delete('a');
65
+ throw new Error('abort');
66
+ })
67
+ ).toThrow('abort');
68
+ expect(flow.measurements.get('a')?.width).toBe(75);
69
+ flow.destroy();
70
+ });
71
+
72
+ it.each(['resize', 'setContentSize'] as const)(
73
+ 'settles %s after anchored measurement without a persistent offset',
74
+ (command) => {
75
+ const flow = makeFlow(
76
+ createNode({
77
+ id: 'a',
78
+ width: 100,
79
+ height: 50,
80
+ autoSize: true,
81
+ autoSizeAnchor: 'center',
82
+ resizable: true,
83
+ })
84
+ );
85
+ flow.measurements.update([
86
+ { id: 'a', rect: { x: 0, y: 0, width: 120, height: 50 } },
87
+ ]);
88
+ if (command === 'resize')
89
+ flow.nodes.resize('a', { x: 50, y: 0, width: 150, height: 50 });
90
+ else
91
+ flow.nodes.setContentSize(
92
+ 'a',
93
+ { width: 150, height: 50 },
94
+ { position: { x: 50, y: 0 } }
95
+ );
96
+ expect(flow.toJSON().graph.nodes[0]).toMatchObject({ x: 50, width: 150 });
97
+ flow.measurements.remove(['a']);
98
+ expect(flow.getBounds('a')).toMatchObject({ x: 50, width: 150 });
99
+ flow.destroy();
100
+ }
101
+ );
102
+
103
+ it('keeps label, port and route reports derived while legacy store callers stay supported', () => {
104
+ const flow = createFlow({
105
+ graph: createGraph({
106
+ nodes: [
107
+ createNode({
108
+ id: 'a',
109
+ autoSize: true,
110
+ ports: [
111
+ createPort({
112
+ name: 'out',
113
+ side: 'right',
114
+ direction: 'out',
115
+ x: 10,
116
+ y: 10,
117
+ }),
118
+ ],
119
+ }),
120
+ createNode({ id: 'b', x: 300 }),
121
+ ],
122
+ edges: [
123
+ createEdge({
124
+ id: 'ab',
125
+ sourceId: 'a',
126
+ targetId: 'b',
127
+ points: [
128
+ { x: 100, y: 20 },
129
+ { x: 300, y: 20 },
130
+ ],
131
+ label: { x: 150, y: 20, width: 20, height: 10 },
132
+ }),
133
+ ],
134
+ }),
135
+ });
136
+ const before = flow.toJSON();
137
+ flow.measurements.update([
138
+ { id: 'a', rect: { x: 0, y: 0, width: 150, height: 70 } },
139
+ { id: 'ab', rect: { x: 150, y: 20, width: 45, height: 25 } },
140
+ { id: 'a:out', rect: { x: 150, y: 30, width: 8, height: 8 } },
141
+ ]);
142
+ expect(flow.toJSON()).toEqual(before);
143
+ expect(flow.getBounds('a:out')).toMatchObject({
144
+ x: 150,
145
+ y: 30,
146
+ width: 8,
147
+ height: 8,
148
+ });
149
+ flow.measurements.remove(['a', 'ab', 'a:out']);
150
+ expect(flow.toJSON()).toEqual(before);
151
+ flow.destroy();
152
+ });
153
+
154
+ it('routes named ports from derived observations without writing port geometry', () => {
155
+ const flow = createFlow({
156
+ graph: createGraph({
157
+ nodes: [
158
+ createNode({
159
+ id: 'a',
160
+ ports: [
161
+ createPort({
162
+ name: 'out',
163
+ side: 'right',
164
+ direction: 'out',
165
+ x: 10,
166
+ y: 10,
167
+ width: 8,
168
+ height: 8,
169
+ }),
170
+ ],
171
+ }),
172
+ createNode({ id: 'b', x: 300 }),
173
+ ],
174
+ edges: [
175
+ createEdge({
176
+ id: 'ab',
177
+ sourceId: 'a',
178
+ sourcePort: 'out',
179
+ targetId: 'b',
180
+ }),
181
+ ],
182
+ }),
183
+ });
184
+ flow.internals.engine.getAllEdgePaths();
185
+ const before = flow.toJSON();
186
+ flow.measurements.update([
187
+ { id: 'a:out', rect: { x: 150, y: 30, width: 8, height: 8 } },
188
+ ]);
189
+ expect(
190
+ flow.internals.engine.getAllEdgePaths().get('ab')?.segments[0]?.points[0]
191
+ ).toEqual({ x: 154, y: 34 });
192
+ expect(flow.toJSON()).toEqual(before);
193
+ flow.measurements.remove(['a:out']);
194
+ expect(
195
+ flow.internals.engine.getAllEdgePaths().get('ab')?.segments[0]?.points[0]
196
+ ).toEqual({ x: 14, y: 14 });
197
+ flow.destroy();
198
+ });
199
+
200
+ it('validates measurement batches before any write', () => {
201
+ const flow = makeFlow();
202
+ expect(() =>
203
+ flow.measurements.update([
204
+ { id: 'a', rect: { x: 0, y: 0, width: 200, height: 100 } },
205
+ { id: 'b', rect: { x: Infinity, y: 0, width: 1, height: 1 } },
206
+ ])
207
+ ).toThrow();
208
+ expect(flow.measurements.get('a')).toBeUndefined();
209
+ expect(() =>
210
+ flow.measurements.update([JSON.parse('{"id":"a","rect":{"width":200}}')])
211
+ ).toThrow();
212
+ expect(flow.measurements.get('a')).toBeUndefined();
213
+ expect(() =>
214
+ flow.measurements.update([
215
+ { id: 'a', rect: { x: 0, y: 0, width: -1, height: 1 } },
216
+ ])
217
+ ).toThrow();
218
+ flow.destroy();
219
+ });
220
+
221
+ it('persists precise content growth despite resize locks, keeps data and undoes', () => {
222
+ const flow = makeFlow(
223
+ createNode({
224
+ id: 'a',
225
+ width: 100,
226
+ height: 50,
227
+ resizable: false,
228
+ draggable: false,
229
+ autoSize: true,
230
+ data: { text: 'hello', unrelated: 42 },
231
+ style: { customWidth: 90 },
232
+ })
233
+ );
234
+ flow.measurements.update([
235
+ { id: 'a', rect: { x: 0, y: 0, width: 110, height: 55 } },
236
+ ]);
237
+ flow.nodes.setContentSize(
238
+ 'a',
239
+ { width: 210.125, height: 80.75 },
240
+ { position: { x: -20.2, y: 5.9 } }
241
+ );
242
+ expect(flow.getBounds('a')).toMatchObject({
243
+ x: -20,
244
+ y: 6,
245
+ width: 210.125,
246
+ height: 80.75,
247
+ });
248
+ expect(flow.getNode('a')?.data).toEqual({ text: 'hello', unrelated: 42 });
249
+ expect(() =>
250
+ flow.nodes.resize('a', { x: 0, y: 0, width: 300, height: 100 })
251
+ ).toThrow();
252
+ flow.history.undo();
253
+ expect(flow.getNode('a')?.width).toBe(100);
254
+ flow.configure({ store: { mode: 'readonly' } });
255
+ expect(() =>
256
+ flow.nodes.setContentSize('a', { width: 300, height: 100 })
257
+ ).toThrow();
258
+ flow.destroy();
259
+ });
260
+
261
+ it('settles positions, preserves sizes, honors permissions and resize constraints', () => {
262
+ const flow = makeFlow(
263
+ createNode({
264
+ id: 'a',
265
+ width: 100,
266
+ height: 50,
267
+ draggable: false,
268
+ resizable: true,
269
+ resize: {
270
+ min: { width: 40, height: 20 },
271
+ max: { width: 200, height: 100 },
272
+ aspect: true,
273
+ },
274
+ })
275
+ );
276
+ expect(() => flow.nodes.move('a', { x: 1, y: 1 })).toThrow();
277
+ flow.nodes.resize('a', { x: -0.2, y: 5.8, width: 120.25, height: 30 });
278
+ expect(flow.getBounds('a')).toMatchObject({
279
+ x: 0,
280
+ y: 6,
281
+ width: 120.25,
282
+ height: 60.125,
283
+ });
284
+ expect(Object.is(flow.getNode('a')?.x, -0)).toBe(false);
285
+ flow.nodes.resize(
286
+ 'a',
287
+ { x: 1.2345, y: 2.3456, width: 500, height: 500 },
288
+ { positionPrecision: 3 }
289
+ );
290
+ expect(flow.getBounds('a')).toMatchObject({
291
+ x: 1.235,
292
+ y: 2.346,
293
+ width: 200,
294
+ height: 100,
295
+ });
296
+ flow.destroy();
297
+ });
298
+
299
+ it('uses movement constraints, grid and descendant movement', () => {
300
+ const flow = createFlow({
301
+ graph: createGraph({
302
+ nodes: [
303
+ createNode({ id: 'a', width: 100, height: 50 }),
304
+ createNode({ id: 'child', parentId: 'a', x: 10, y: 10 }),
305
+ ],
306
+ edges: [],
307
+ }),
308
+ store: {
309
+ snapToGrid: true,
310
+ snapGrid: [10, 10],
311
+ nodeExtent: [
312
+ { x: 0, y: 0 },
313
+ { x: 200, y: 200 },
314
+ ],
315
+ constrainNodeMovement: (_node, position) => ({
316
+ ...position,
317
+ y: Math.min(30, position.y),
318
+ }),
319
+ },
320
+ });
321
+ flow.nodes.move('a', { x: 500, y: 37 });
322
+ expect(flow.getBounds('a')).toMatchObject({ x: 100, y: 30 });
323
+ expect(flow.getNode('child')).toMatchObject({ x: 110, y: 40 });
324
+ flow.destroy();
325
+ });
326
+
327
+ it('combines semantic draft commands in one commit and undo', () => {
328
+ const flow = makeFlow();
329
+ const commit = vi.fn();
330
+ flow.on('commit', commit);
331
+ flow.transaction((draft) => {
332
+ draft.nodes.move('a', { x: 12.8, y: -0.1 });
333
+ draft.nodes.setContentSize('a', { width: 210.5, height: 90.5 });
334
+ expect(draft.getNodeBounds('a')).toMatchObject({ x: 13, width: 210.5 });
335
+ const detached = draft.getNode('a')!;
336
+ detached.x = 999;
337
+ expect(draft.getNode('a')?.x).toBe(13);
338
+ });
339
+ expect(commit).toHaveBeenCalledTimes(1);
340
+ expect(flow.getBounds('a')).toMatchObject({
341
+ x: 13,
342
+ y: 0,
343
+ width: 210.5,
344
+ height: 90.5,
345
+ });
346
+ flow.history.undo();
347
+ expect(flow.getBounds('a')).toMatchObject({
348
+ x: 0,
349
+ y: 0,
350
+ width: 100,
351
+ height: 50,
352
+ });
353
+ expect(flow.history.canUndo()).toBe(false);
354
+ flow.destroy();
355
+ });
356
+ });
@@ -0,0 +1,234 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import type { ProposedConnection, ConnectionValidationContext } from './engine';
4
+ import { createEdge, createGraph, createNode } from './factories';
5
+
6
+ describe('renderer transaction boundaries', () => {
7
+ it('ignores native edits in Flow history while still emitting commits', () => {
8
+ const flow = createFlow({
9
+ graph: createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }),
10
+ });
11
+ const commit = vi.fn();
12
+ flow.on('commit', commit);
13
+ flow.transaction((draft) => draft.nodes.move('a', { x: 20, y: 30 }), {
14
+ history: 'ignore',
15
+ origin: 'native',
16
+ });
17
+ expect(flow.getNode('a')).toMatchObject({ x: 20, y: 30 });
18
+ expect(flow.history.canUndo()).toBe(false);
19
+ expect(commit).toHaveBeenCalledOnce();
20
+ expect(commit.mock.calls[0][0].origin).toBe('native');
21
+ flow.destroy();
22
+ });
23
+
24
+ it('validates standalone connections once against the committed candidate', () => {
25
+ const canConnect = vi.fn(
26
+ (_edge: ProposedConnection, context: ConnectionValidationContext) =>
27
+ context.graph.edges.length === 1
28
+ );
29
+ const flow = createFlow({
30
+ graph: createGraph({
31
+ nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
32
+ edges: [],
33
+ }),
34
+ engine: { canConnect },
35
+ });
36
+ flow.edges.create({ id: 'edge', sourceId: 'a', targetId: 'b' });
37
+ expect(flow.getEdge('edge')).toBeDefined();
38
+ expect(canConnect).toHaveBeenCalledOnce();
39
+ flow.destroy();
40
+ });
41
+
42
+ it('validates connection policy against the completed draft and rolls back atomically', () => {
43
+ const canConnect = vi.fn(
44
+ (_edge: ProposedConnection, context: ConnectionValidationContext) =>
45
+ context.graph.nodes.every((node) => node.data !== 'blocked')
46
+ );
47
+ const flow = createFlow({ engine: { canConnect } });
48
+ const commit = vi.fn();
49
+ flow.on('commit', commit);
50
+ expect(() =>
51
+ flow.transaction((draft) => {
52
+ draft.nodes.create({ id: 'source' });
53
+ draft.nodes.create({ id: 'target', data: 'blocked' });
54
+ draft.edges.create({
55
+ id: 'edge',
56
+ sourceId: 'source',
57
+ targetId: 'target',
58
+ });
59
+ })
60
+ ).toThrow(/Connection/);
61
+ expect(flow.getSnapshot().graph.nodes).toHaveLength(0);
62
+ expect(flow.getSnapshot().graph.edges).toHaveLength(0);
63
+ expect(commit).not.toHaveBeenCalled();
64
+ expect(flow.history.canUndo()).toBe(false);
65
+ expect(canConnect.mock.calls[0][1].context.graph).toBe(
66
+ canConnect.mock.calls[0][1].graph
67
+ );
68
+ flow.transaction((draft) => {
69
+ draft.nodes.create({ id: 'source' });
70
+ draft.nodes.create({ id: 'target' });
71
+ draft.edges.create({
72
+ id: 'edge',
73
+ sourceId: 'source',
74
+ targetId: 'target',
75
+ });
76
+ });
77
+ expect(flow.getEdge('edge')).toMatchObject({
78
+ sourceId: 'source',
79
+ targetId: 'target',
80
+ });
81
+ expect(commit).toHaveBeenCalledOnce();
82
+ flow.destroy();
83
+ });
84
+
85
+ it('does not apply connection policy to automatic delete retargeting', () => {
86
+ const flow = createFlow({
87
+ graph: createGraph({
88
+ nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
89
+ edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
90
+ }),
91
+ store: {
92
+ onDeleteNodes: 'retarget',
93
+ profile: {
94
+ defaultMode: 'interactive',
95
+ modes: { interactive: {} },
96
+ policies: { canReconnect: false },
97
+ },
98
+ },
99
+ engine: { canConnect: (edge) => edge.sourceId !== edge.targetId },
100
+ });
101
+ expect(flow.nodes.delete('b')).toBe(true);
102
+ expect(flow.getEdge('ab')).toMatchObject({
103
+ sourceId: 'a',
104
+ targetId: 'a',
105
+ });
106
+ flow.destroy();
107
+ });
108
+ });
109
+
110
+ describe('renderer ownership and lifecycle', () => {
111
+ const makeFlow = () =>
112
+ createFlow({
113
+ graph: createGraph({
114
+ nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
115
+ edges: [],
116
+ }),
117
+ });
118
+
119
+ it('preserves recorded history across ignored edits and groups transaction ids', () => {
120
+ const flow = makeFlow();
121
+ flow.nodes.move('a', { x: 10, y: 0 }, { transactionId: 'gesture' });
122
+ flow.nodes.move('a', { x: 20, y: 0 }, { transactionId: 'gesture' });
123
+ flow.nodes.move('b', { x: 30, y: 0 }, { history: 'ignore' });
124
+ expect(flow.history.canUndo()).toBe(true);
125
+ flow.history.undo();
126
+ expect(flow.getNode('a')?.x).toBe(0);
127
+ expect(flow.history.canUndo()).toBe(false);
128
+ flow.destroy();
129
+ });
130
+
131
+ it('inherits outer ownership and rejects conflicting nested ownership', () => {
132
+ const flow = makeFlow();
133
+ flow.transaction(
134
+ () => {
135
+ flow.transaction((draft) => draft.nodes.move('a', { x: 10, y: 0 }));
136
+ expect(() => flow.transaction(() => {}, { history: 'record' })).toThrow(
137
+ /override history/
138
+ );
139
+ },
140
+ { history: 'ignore' }
141
+ );
142
+ expect(flow.getNode('a')?.x).toBe(10);
143
+ expect(flow.history.canUndo()).toBe(false);
144
+ flow.destroy();
145
+ });
146
+
147
+ it('validates final endpoint connectability without rechecking unchanged edges', () => {
148
+ const flow = makeFlow();
149
+ expect(() =>
150
+ flow.transaction((draft) => {
151
+ draft.edges.create({ id: 'edge', sourceId: 'a', targetId: 'b' });
152
+ draft.nodes.update('b', { connectable: false });
153
+ })
154
+ ).toThrow(/Connection/);
155
+ expect(flow.getEdge('edge')).toBeUndefined();
156
+ flow.edges.create({ id: 'edge', sourceId: 'a', targetId: 'b' });
157
+ flow.nodes.update('b', { connectable: false });
158
+ expect(flow.getEdge('edge')).toBeDefined();
159
+ expect(flow.can('edit')).toBe(true);
160
+ flow.mode.set('readonly');
161
+ expect(flow.can('edit')).toBe(false);
162
+ expect(() =>
163
+ flow.transaction((draft) =>
164
+ draft.edges.create({ id: 'other', sourceId: 'a', targetId: 'b' })
165
+ )
166
+ ).toThrow();
167
+ flow.destroy();
168
+ });
169
+
170
+ it('validates reconnected edges after all draft updates', () => {
171
+ const contexts: ConnectionValidationContext[] = [];
172
+ const flow = createFlow({
173
+ graph: createGraph({
174
+ nodes: ['a', 'b', 'c'].map((id) => createNode({ id })),
175
+ edges: [],
176
+ }),
177
+ engine: {
178
+ canConnect: (_edge, context) => {
179
+ contexts.push(context);
180
+ return context.target.data !== 'blocked';
181
+ },
182
+ },
183
+ });
184
+ flow.edges.create({ id: 'edge', sourceId: 'a', targetId: 'b' });
185
+ contexts.length = 0;
186
+ expect(() =>
187
+ flow.transaction((draft) => {
188
+ draft.edges.reconnect('edge', { newTargetId: 'c' });
189
+ draft.nodes.update('c', { data: 'blocked' });
190
+ })
191
+ ).toThrow(/Connection/);
192
+ expect(flow.getEdge('edge')?.targetId).toBe('b');
193
+ expect(flow.getNode('c')?.data).not.toBe('blocked');
194
+ expect(contexts).toHaveLength(1);
195
+ expect(contexts[0].graph).toBe(contexts[0].context.graph);
196
+ expect(contexts[0].target.data).toBe('blocked');
197
+ flow.destroy();
198
+ });
199
+
200
+ it('applies change sets without adding history when ignored', () => {
201
+ const source = makeFlow();
202
+ const result = source.transaction((draft) =>
203
+ draft.nodes.move('a', { x: 45, y: 0 })
204
+ );
205
+ const target = makeFlow();
206
+ expect(
207
+ target.applyChangeSet(result.changeSet!, { history: 'ignore' }).ok
208
+ ).toBe(true);
209
+ expect(target.getNode('a')?.x).toBe(45);
210
+ expect(target.history.canUndo()).toBe(false);
211
+ source.destroy();
212
+ target.destroy();
213
+ });
214
+
215
+ it('ends external sessions and render subscriptions on idempotent destroy', () => {
216
+ const flow = makeFlow();
217
+ const edit = flow.beginExternalEdit();
218
+ const render = vi.fn();
219
+ flow.subscribeRender(render);
220
+ flow.viewport.panBy({ x: 5, y: 0 });
221
+ expect(render).toHaveBeenCalled();
222
+ const destroy = vi.fn();
223
+ flow.on('destroy', destroy);
224
+ flow.destroy();
225
+ flow.destroy();
226
+ expect(destroy).toHaveBeenCalledOnce();
227
+ expect(destroy).toHaveBeenCalledWith({ type: 'destroy' });
228
+ expect(edit.commit([]).ok).toBe(false);
229
+ expect(() => flow.beginExternalEdit()).toThrow(/destroyed/);
230
+ render.mockClear();
231
+ flow.viewport.panBy({ x: 5, y: 0 });
232
+ expect(render).not.toHaveBeenCalled();
233
+ });
234
+ });