@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,223 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createEdge, createGraph, createNode } from './factories';
4
+ import { createEntityChangeNotifier } from './entity-changes';
5
+
6
+ function stubRaf() {
7
+ let now = 0;
8
+ let nextId = 1;
9
+ const callbacks = new Map<number, (time: number) => void>();
10
+ vi.stubGlobal('requestAnimationFrame', (callback: (time: number) => void) => {
11
+ const id = nextId++;
12
+ callbacks.set(id, callback);
13
+ return id;
14
+ });
15
+ vi.stubGlobal('cancelAnimationFrame', (id: number) => callbacks.delete(id));
16
+ return {
17
+ tick(ms: number) {
18
+ now += ms;
19
+ const pending = [...callbacks.values()];
20
+ callbacks.clear();
21
+ for (const callback of pending) callback(now);
22
+ },
23
+ };
24
+ }
25
+
26
+ function makeFlow() {
27
+ return createFlow({
28
+ graph: createGraph({
29
+ nodes: [
30
+ createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 }),
31
+ createNode({ id: 'b', x: 300, y: 0, width: 100, height: 50 }),
32
+ ],
33
+ edges: [
34
+ createEdge({
35
+ id: 'ab',
36
+ sourceId: 'a',
37
+ targetId: 'b',
38
+ label: { x: 120, y: 10, width: 40, height: 20 },
39
+ }),
40
+ ],
41
+ }),
42
+ });
43
+ }
44
+
45
+ afterEach(() => vi.unstubAllGlobals());
46
+
47
+ describe('layout animation sessions', () => {
48
+ it('keeps durable geometry final while visual node and label geometry animate', async () => {
49
+ const raf = stubRaf();
50
+ const flow = makeFlow();
51
+ const controls = flow.layout.apply(
52
+ {
53
+ positions: new Map([['a', { x: 100, y: 40 }]]),
54
+ edgeLabelPositions: new Map([['ab', { x: 220, y: 50 }]]),
55
+ },
56
+ { animation: { duration: 100, easing: (t) => t } },
57
+ );
58
+
59
+ expect(flow.getNode('a')).toMatchObject({ x: 100, y: 40 });
60
+ expect(flow.getEdge('ab')?.label).toMatchObject({ x: 220, y: 50 });
61
+ expect(flow.getEdge('ab')).toMatchObject({ x: 220, y: 50 });
62
+ expect(flow.internals.engine.visualBounds('a')).toMatchObject({ x: 0, y: 0 });
63
+ expect(flow.internals.engine.getAllEdgePaths().get('ab')?.labelBounds).toMatchObject({
64
+ x: 120,
65
+ y: 10,
66
+ });
67
+
68
+ raf.tick(0);
69
+ raf.tick(50);
70
+ expect(flow.internals.engine.visualBounds('a')).toMatchObject({ x: 50, y: 20 });
71
+ expect(flow.internals.engine.getAllEdgePaths().get('ab')?.labelBounds).toMatchObject({
72
+ x: 170,
73
+ y: 30,
74
+ });
75
+
76
+ raf.tick(50);
77
+ await controls.done;
78
+ expect(flow.internals.engine.visualBounds('a')).toMatchObject({ x: 100, y: 40 });
79
+ expect(flow.getSnapshot().layoutAnimation.status).toBe('idle');
80
+ });
81
+
82
+ it('animates a fixed label with moving endpoints when its final position is unchanged', () => {
83
+ const flow = makeFlow();
84
+ const controls = flow.layout.apply(
85
+ {
86
+ positions: new Map([['a', { x: 100, y: 0 }]]),
87
+ edgeLabelPositions: new Map([['ab', { x: 120, y: 10 }]]),
88
+ },
89
+ { animation: { duration: 100, easing: (t) => t } },
90
+ );
91
+
92
+ expect(flow.getSnapshot().layoutAnimation.offsets.get('a')).toEqual({
93
+ x: -100,
94
+ y: 0,
95
+ });
96
+ expect(flow.getSnapshot().layoutAnimation.offsets.get('ab')).toEqual({
97
+ x: -50,
98
+ y: 0,
99
+ });
100
+
101
+ controls.cancel();
102
+ flow.destroy();
103
+ });
104
+
105
+ it('cancel snaps to the durable final geometry', async () => {
106
+ const raf = stubRaf();
107
+ const flow = makeFlow();
108
+ const controls = flow.layout.apply(
109
+ { positions: new Map([['a', { x: 100, y: 0 }]]) },
110
+ { animation: { duration: 100, easing: (t) => t } },
111
+ );
112
+ raf.tick(0);
113
+ raf.tick(25);
114
+ expect(flow.internals.engine.visualBounds('a').x).toBe(25);
115
+
116
+ controls.cancel();
117
+ await controls.done;
118
+ raf.tick(75);
119
+ expect(flow.internals.engine.visualBounds('a').x).toBe(100);
120
+ });
121
+
122
+ it('replaces from the current visual position and resolves prior controls', async () => {
123
+ const raf = stubRaf();
124
+ const flow = makeFlow();
125
+ const first = flow.layout.apply(
126
+ { positions: new Map([['a', { x: 100, y: 0 }]]) },
127
+ { animation: { duration: 100, easing: (t) => t } },
128
+ );
129
+ raf.tick(0);
130
+ raf.tick(50);
131
+ expect(flow.internals.engine.visualBounds('a').x).toBe(50);
132
+
133
+ const second = flow.layout.apply(
134
+ { positions: new Map([['a', { x: 200, y: 0 }]]) },
135
+ { animation: { duration: 100, easing: (t) => t } },
136
+ );
137
+ await first.done;
138
+ expect(flow.internals.engine.visualBounds('a').x).toBe(50);
139
+ expect(flow.getNode('a')?.x).toBe(200);
140
+
141
+ raf.tick(0);
142
+ raf.tick(50);
143
+ expect(flow.internals.engine.visualBounds('a').x).toBe(125);
144
+ raf.tick(50);
145
+ await second.done;
146
+ expect(flow.internals.engine.visualBounds('a').x).toBe(200);
147
+ });
148
+
149
+ it('destroy resolves and clears an active animation', async () => {
150
+ const raf = stubRaf();
151
+ const flow = makeFlow();
152
+ const controls = flow.layout.apply(
153
+ { positions: new Map([['a', { x: 100, y: 0 }]]) },
154
+ { animation: { duration: 100 } },
155
+ );
156
+ raf.tick(0);
157
+ flow.destroy();
158
+ await controls.done;
159
+ expect(flow.internals.engine.visualBounds('a').x).toBe(100);
160
+ });
161
+
162
+ it('does not animate or apply a stale async result', async () => {
163
+ const flow = makeFlow();
164
+ let resolveLayout!: (result: Map<string, { x: number; y: number }>) => void;
165
+ const pending = flow.layout.run(
166
+ () => new Promise((resolve) => {
167
+ resolveLayout = resolve;
168
+ }),
169
+ { animation: { duration: 100 } },
170
+ );
171
+ flow.nodes.update('a', { x: 25 });
172
+ resolveLayout(new Map([['a', { x: 100, y: 0 }]]));
173
+
174
+ const response = await pending;
175
+ await response.animation.done;
176
+ expect(response.applied).toBe(false);
177
+ expect(flow.getNode('a')?.x).toBe(25);
178
+ expect(flow.getSnapshot().layoutAnimation.status).toBe('idle');
179
+ });
180
+
181
+ it('records one durable commit and no frame history', () => {
182
+ const raf = stubRaf();
183
+ const flow = makeFlow();
184
+ const commits = vi.fn();
185
+ flow.on('commit', commits);
186
+ flow.layout.apply(
187
+ { positions: new Map([['a', { x: 100, y: 0 }]]) },
188
+ { animation: { duration: 100, easing: (t) => t } },
189
+ );
190
+ expect(commits).toHaveBeenCalledTimes(1);
191
+ raf.tick(0);
192
+ raf.tick(25);
193
+ raf.tick(25);
194
+ expect(commits).toHaveBeenCalledTimes(1);
195
+
196
+ flow.history.undo();
197
+ expect(flow.getNode('a')?.x).toBe(0);
198
+ expect(flow.history.canUndo()).toBe(false);
199
+ expect(flow.getSnapshot().layoutAnimation.status).toBe('idle');
200
+ });
201
+
202
+ it('notifies animated nodes and their incident edges per visual frame', () => {
203
+ const raf = stubRaf();
204
+ const flow = makeFlow();
205
+ const notifier = createEntityChangeNotifier(flow.internals.store);
206
+ const nodeChanged = vi.fn();
207
+ const edgeChanged = vi.fn();
208
+ notifier.subscribe('a', nodeChanged);
209
+ notifier.subscribe('ab', edgeChanged);
210
+ flow.layout.apply(
211
+ { positions: new Map([['a', { x: 100, y: 0 }]]) },
212
+ { animation: { duration: 100, easing: (t) => t } },
213
+ );
214
+ nodeChanged.mockClear();
215
+ edgeChanged.mockClear();
216
+
217
+ raf.tick(0);
218
+ raf.tick(50);
219
+ expect(nodeChanged).toHaveBeenCalled();
220
+ expect(edgeChanged).toHaveBeenCalled();
221
+ notifier.destroy();
222
+ });
223
+ });
@@ -0,0 +1,414 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createEdge, createGraph, createNode } from './factories';
4
+ import { FlowGraphValidationError } from './validation';
5
+
6
+ function graph() {
7
+ return createGraph({
8
+ nodes: [
9
+ createNode({ id: 'root', x: 0, y: 0, width: 300, height: 200 }),
10
+ createNode({
11
+ id: 'a',
12
+ parentId: 'root',
13
+ x: 20,
14
+ y: 30,
15
+ width: 80,
16
+ height: 40,
17
+ }),
18
+ createNode({ id: 'b', x: 400, y: 50, width: 100, height: 60 }),
19
+ ],
20
+ edges: [
21
+ createEdge({
22
+ id: 'ab',
23
+ sourceId: 'a',
24
+ targetId: 'b',
25
+ waypoints: [{ x: 200, y: 60 }],
26
+ label: { x: 180, y: 50, width: 40, height: 20 },
27
+ }),
28
+ ],
29
+ });
30
+ }
31
+
32
+ describe('enriched layout contract', () => {
33
+ it('keeps the positions Map shorthand compatible', async () => {
34
+ const flow = createFlow({ graph: graph() });
35
+ const response = await flow.layout.run(async () =>
36
+ new Map([['a', { x: 100, y: 120 }]]),
37
+ );
38
+
39
+ expect(response.applied).toBe(true);
40
+ expect(response.result.positions.get('a')).toEqual({ x: 100, y: 120 });
41
+ expect(flow.getNode('a')).toMatchObject({ x: 100, y: 120 });
42
+ });
43
+
44
+ it('rounds applied layout positions while retaining raw algorithm output', async () => {
45
+ const input = graph();
46
+ input.nodes[1].x = 20.125;
47
+ input.nodes[1].width = 80.375;
48
+ const flow = createFlow({ graph: input });
49
+ const before = flow.toJSON().graph;
50
+ const response = await flow.layout.run(
51
+ async () =>
52
+ new Map([
53
+ ['a', { x: 10.49, y: 73.33333333333333 }],
54
+ ['b', { x: -0.4, y: -20.5 }],
55
+ ])
56
+ );
57
+
58
+ expect(flow.getNode('a')).toMatchObject({ x: 10, y: 73, width: 80.375 });
59
+ expect(flow.getNode('b')).toMatchObject({ x: 0, y: -20 });
60
+ expect(Object.is(flow.getNode('b')?.x, -0)).toBe(false);
61
+ expect(response.result.positions.get('a')?.y).toBe(73.33333333333333);
62
+ flow.history.undo();
63
+ expect(flow.toJSON().graph).toEqual(before);
64
+ flow.history.redo();
65
+ expect(flow.getNode('a')?.y).toBe(73);
66
+ flow.destroy();
67
+ });
68
+
69
+ it.each(['points', 'waypoints'] as const)(
70
+ 'rounds enriched positions and %s without rounding sizes or mutating input',
71
+ (kind) => {
72
+ const flow = createFlow({ graph: graph() });
73
+ const bounds = {
74
+ x: 10.49,
75
+ y: 73.33333333333333,
76
+ width: 80.375,
77
+ height: 40.125,
78
+ };
79
+ const label = { x: 20.5, y: -0.4, width: 42.375, height: 20.125 };
80
+ const points = [
81
+ { x: 100.49, y: 73.33333333333333 },
82
+ { x: 200.5, y: -0.4 },
83
+ ];
84
+ flow.layout.apply({
85
+ positions: new Map(),
86
+ nodeBounds: new Map([['a', bounds]]),
87
+ edgeLabelBounds: new Map([['ab', label]]),
88
+ ...(kind === 'waypoints'
89
+ ? { edgeLabelPositions: new Map([['ab', { x: 20.5, y: -0.4 }]]) }
90
+ : {}),
91
+ edgeRoutes: new Map([
92
+ ['ab', kind === 'points' ? { points } : { waypoints: points }],
93
+ ]),
94
+ });
95
+
96
+ expect(flow.getNode('a')).toMatchObject({
97
+ x: 10,
98
+ y: 73,
99
+ width: 80.375,
100
+ height: 40.125,
101
+ });
102
+ expect(flow.getEdge('ab')?.label).toMatchObject({
103
+ x: 21,
104
+ y: 0,
105
+ width: 42.375,
106
+ height: 20.125,
107
+ });
108
+ expect(flow.getEdge('ab')?.[kind]).toEqual([
109
+ { x: 100, y: 73 },
110
+ { x: 201, y: 0 },
111
+ ]);
112
+ expect(Object.is(flow.getEdge('ab')?.label?.y, -0)).toBe(false);
113
+ expect(bounds.y).toBe(73.33333333333333);
114
+ expect(points[0].y).toBe(73.33333333333333);
115
+ flow.destroy();
116
+ }
117
+ );
118
+
119
+ it('preserves fractional authored coordinates of fixed and untouched nodes', () => {
120
+ const input = graph();
121
+ input.nodes[1].x = 20.125;
122
+ input.nodes[2].y = 50.375;
123
+ const flow = createFlow({ graph: input });
124
+ const result = {
125
+ positions: new Map([['a', { x: 10.49, y: 73.33333333333333 }]]),
126
+ };
127
+ flow.layout.apply(result, { fixedEntityIds: new Set(['a']) });
128
+ expect(flow.getNode('a')?.x).toBe(20.125);
129
+ expect(flow.getNode('b')?.y).toBe(50.375);
130
+ flow.layout.apply(result, {
131
+ fixedEntityIds: new Set(['a']),
132
+ overrideFixedEntityIds: new Set(['a']),
133
+ });
134
+ expect(flow.getNode('a')).toMatchObject({ x: 10, y: 73 });
135
+ expect(flow.getNode('b')?.y).toBe(50.375);
136
+ flow.nodes.update('a', { x: 10.125 });
137
+ expect(flow.toJSON().graph.nodes.find((node) => node.id === 'a')?.x).toBe(
138
+ 10.125
139
+ );
140
+ flow.destroy();
141
+ });
142
+
143
+ it('atomically applies enriched geometry in one undo step', () => {
144
+ const flow = createFlow({ graph: graph() });
145
+ const before = flow.toJSON().graph;
146
+ flow.layout.apply({
147
+ positions: new Map([
148
+ ['a', { x: 110, y: 120 }],
149
+ ['b', { x: 510, y: 70 }],
150
+ ]),
151
+ nodeSizes: new Map([['a', { width: 140, height: 70 }]]),
152
+ nodeBounds: new Map([
153
+ ['b', { x: 500, y: 60, width: 160, height: 90 }],
154
+ ]),
155
+ edgeLabelBounds: new Map([
156
+ ['ab', { x: 260, y: 100, width: 90, height: 30 }],
157
+ ]),
158
+ edgeLabelPositions: new Map([['ab', { x: 270, y: 110 }]]),
159
+ edgeRoutes: new Map([
160
+ ['ab', { waypoints: [{ x: 220, y: 80 }, { x: 430, y: 90 }] }],
161
+ ]),
162
+ source: 'elk',
163
+ diagnostics: [{ severity: 'warning', message: 'tight spacing' }],
164
+ quality: { score: 0.92 },
165
+ });
166
+
167
+ expect(flow.getNode('a')).toMatchObject({
168
+ x: 110,
169
+ y: 120,
170
+ width: 140,
171
+ height: 70,
172
+ });
173
+ expect(flow.getNode('a')?.dx).toBeUndefined();
174
+ expect(flow.getNode('a')?.dy).toBeUndefined();
175
+ // Explicit positions override the x/y part of aggregate bounds.
176
+ expect(flow.getNode('b')).toMatchObject({
177
+ x: 510,
178
+ y: 70,
179
+ width: 160,
180
+ height: 90,
181
+ });
182
+ expect(flow.getEdge('ab')).toMatchObject({
183
+ label: { x: 270, y: 110, width: 90, height: 30 },
184
+ waypoints: [{ x: 220, y: 80 }, { x: 430, y: 90 }],
185
+ });
186
+
187
+ flow.history.undo();
188
+ expect(flow.toJSON().graph).toEqual(before);
189
+ expect(flow.history.canUndo()).toBe(false);
190
+ flow.history.redo();
191
+ expect(flow.getNode('a')).toMatchObject({ x: 110, y: 120 });
192
+ });
193
+
194
+ it('clears transient edge offsets when applying layout geometry', () => {
195
+ const input = graph();
196
+ input.edges[0].dx = 12;
197
+ input.edges[0].dy = 8;
198
+ const flow = createFlow({ graph: input });
199
+
200
+ flow.layout.apply({
201
+ positions: new Map([['a', { x: 110, y: 120 }]]),
202
+ edgeRoutes: new Map([['ab', [{ x: 220, y: 80 }]]]),
203
+ });
204
+
205
+ expect(flow.getEdge('ab')?.dx).toBeUndefined();
206
+ expect(flow.getEdge('ab')?.dy).toBeUndefined();
207
+ expect(flow.getEdge('ab')?.waypoints).toEqual([{ x: 220, y: 80 }]);
208
+ });
209
+
210
+ it('invalidates stale authoritative geometry when layout moves an endpoint', () => {
211
+ const input = graph();
212
+ Object.assign(input.edges[0], {
213
+ points: [{ x: 100, y: 100 }, { x: 300, y: 100 }],
214
+ routing: 'polyline',
215
+ svgPath: 'M 100 100 L 300 100',
216
+ });
217
+ const flow = createFlow({ graph: input });
218
+
219
+ flow.layout.apply({
220
+ positions: new Map([['a', { x: 120, y: 130 }]]),
221
+ });
222
+
223
+ expect(flow.getEdge('ab')).not.toHaveProperty('points');
224
+ expect(flow.getEdge('ab')).not.toHaveProperty('routing');
225
+ expect(flow.getEdge('ab')).not.toHaveProperty('svgPath');
226
+ });
227
+
228
+ it('replaces authoritative geometry with a layout-provided route', () => {
229
+ const input = graph();
230
+ Object.assign(input.edges[0], {
231
+ points: [{ x: 100, y: 100 }, { x: 300, y: 100 }],
232
+ routing: 'polyline',
233
+ svgPath: 'M 100 100 L 300 100',
234
+ });
235
+ const flow = createFlow({ graph: input });
236
+
237
+ flow.layout.apply({
238
+ positions: new Map(),
239
+ edgeRoutes: new Map([['ab', [{ x: 260, y: 140 }]]]),
240
+ });
241
+
242
+ expect(flow.getEdge('ab')).toMatchObject({
243
+ waypoints: [{ x: 260, y: 140 }],
244
+ });
245
+ expect(flow.getEdge('ab')).not.toHaveProperty('points');
246
+ expect(flow.getEdge('ab')).not.toHaveProperty('routing');
247
+ expect(flow.getEdge('ab')).not.toHaveProperty('svgPath');
248
+ });
249
+
250
+ it('accepts a complete authoritative route from a layout', () => {
251
+ const flow = createFlow({ graph: graph() });
252
+
253
+ flow.layout.apply({
254
+ positions: new Map(),
255
+ edgeRoutes: new Map([
256
+ [
257
+ 'ab',
258
+ {
259
+ points: [
260
+ { x: 100, y: 100 },
261
+ { x: 260, y: 140 },
262
+ { x: 400, y: 80 },
263
+ ],
264
+ routing: 'orthogonal',
265
+ },
266
+ ],
267
+ ]),
268
+ });
269
+
270
+ expect(flow.getEdge('ab')).toMatchObject({
271
+ points: [
272
+ { x: 100, y: 100 },
273
+ { x: 260, y: 140 },
274
+ { x: 400, y: 80 },
275
+ ],
276
+ routing: 'orthogonal',
277
+ });
278
+ expect(flow.getEdge('ab')?.waypoints).toBeUndefined();
279
+ });
280
+
281
+ it('keeps fixed/pinned entities unchanged unless explicitly overridden', () => {
282
+ const flow = createFlow({ graph: graph() });
283
+ const result = {
284
+ positions: new Map([
285
+ ['a', { x: 900, y: 900 }],
286
+ ['b', { x: 800, y: 800 }],
287
+ ]),
288
+ edgeRoutes: new Map([
289
+ ['ab', [{ x: 700, y: 700 }]],
290
+ ]),
291
+ };
292
+
293
+ flow.layout.apply(result, {
294
+ fixedEntityIds: new Set(['a']),
295
+ pinnedEntityIds: new Set(['ab']),
296
+ });
297
+ expect(flow.getNode('a')).toMatchObject({ x: 20, y: 30 });
298
+ expect(flow.getNode('b')).toMatchObject({ x: 800, y: 800 });
299
+ expect(flow.getEdge('ab')?.waypoints).toEqual([{ x: 200, y: 60 }]);
300
+
301
+ flow.layout.apply(
302
+ { ...result, overrideFixedEntityIds: new Set(['a', 'ab']) },
303
+ {
304
+ fixedEntityIds: new Set(['a']),
305
+ pinnedEntityIds: new Set(['ab']),
306
+ },
307
+ );
308
+ expect(flow.getNode('a')).toMatchObject({ x: 900, y: 900 });
309
+ expect(flow.getEdge('ab')?.waypoints).toEqual([{ x: 700, y: 700 }]);
310
+ });
311
+
312
+ it('forwards immutable cloned scope/options and returns source/diagnostics', async () => {
313
+ const flow = createFlow({ graph: graph() });
314
+ const metadata = { pass: { name: 'primary' } };
315
+ const previousResult = {
316
+ positions: new Map([['a', { x: 1, y: 2 }]]),
317
+ };
318
+ const algorithm = vi.fn(async (input, options) => {
319
+ expect(input).not.toBe(flow.getSnapshot().graph);
320
+ expect(Object.isFrozen(input)).toBe(true);
321
+ expect(Object.isFrozen(input.nodes)).toBe(true);
322
+ expect(Object.isFrozen(input.nodes[0])).toBe(true);
323
+ expect(Object.isFrozen(options)).toBe(true);
324
+ expect(options).toMatchObject({
325
+ force: true,
326
+ scope: { parentId: 'root', depth: 1 },
327
+ source: 'elk',
328
+ metadata: { pass: { name: 'primary' } },
329
+ });
330
+ expect([...(options.fixedEntityIds ?? [])]).toEqual(['a']);
331
+ expect([...(options.pinnedEntityIds ?? [])]).toEqual(['ab']);
332
+ expect(options.previousResult?.positions.get('a')).toEqual({ x: 1, y: 2 });
333
+ expect(() =>
334
+ (options.previousResult?.positions as Map<string, unknown>).set('a', {}),
335
+ ).toThrow(/readonly/i);
336
+ return {
337
+ positions: new Map([['a', { x: 300, y: 320 }]]),
338
+ overrideFixedEntityIds: new Set(['a']),
339
+ diagnostics: [{ message: 'done' }],
340
+ };
341
+ });
342
+
343
+ const response = await flow.layout.run(algorithm, {
344
+ force: true,
345
+ scope: { parentId: 'root', depth: 1 },
346
+ previousResult,
347
+ fixedEntityIds: new Set(['a']),
348
+ pinnedEntityIds: new Set(['ab']),
349
+ source: 'elk',
350
+ metadata,
351
+ });
352
+
353
+ expect(algorithm).toHaveBeenCalledOnce();
354
+ expect(response).toMatchObject({
355
+ applied: true,
356
+ source: 'elk',
357
+ diagnostics: [{ message: 'done' }],
358
+ });
359
+ expect(flow.getNode('a')).toMatchObject({ x: 300, y: 320 });
360
+ expect(metadata).toEqual({ pass: { name: 'primary' } });
361
+ expect(previousResult.positions.get('a')).toEqual({ x: 1, y: 2 });
362
+ });
363
+
364
+ it('cancels the prior run and rejects stale completion', async () => {
365
+ const flow = createFlow({ graph: graph() });
366
+ let finishFirst!: (value: Map<string, { x: number; y: number }>) => void;
367
+ let firstSignal: { aborted: boolean } | undefined;
368
+ const first = flow.layout.run(
369
+ (_graph, options) => {
370
+ firstSignal = options.signal;
371
+ return new Promise((resolve) => {
372
+ finishFirst = resolve;
373
+ });
374
+ },
375
+ { source: 'first' },
376
+ );
377
+ const second = flow.layout.run(
378
+ async () => new Map([['a', { x: 200, y: 210 }]]),
379
+ { source: 'second' },
380
+ );
381
+
382
+ expect(firstSignal?.aborted).toBe(true);
383
+ await expect(second).resolves.toMatchObject({ applied: true, source: 'second' });
384
+ finishFirst(new Map([['a', { x: 999, y: 999 }]]));
385
+ await expect(first).resolves.toMatchObject({ applied: false, source: 'first' });
386
+ expect(flow.getNode('a')).toMatchObject({ x: 200, y: 210 });
387
+ });
388
+
389
+ it('rejects invalid and unknown geometry atomically in deterministic order', () => {
390
+ const flow = createFlow({ graph: graph() });
391
+ const before = flow.getSnapshot();
392
+ let error: FlowGraphValidationError | undefined;
393
+ try {
394
+ flow.layout.apply({
395
+ positions: new Map([
396
+ ['zzz', { x: 0, y: 0 }],
397
+ ['aaa', { x: 0, y: 0 }],
398
+ ['a', { x: Number.NaN, y: 20 }],
399
+ ]),
400
+ });
401
+ } catch (caught) {
402
+ error = caught as FlowGraphValidationError;
403
+ }
404
+
405
+ expect(error).toBeInstanceOf(FlowGraphValidationError);
406
+ expect(error?.issues.map((issue) => issue.path)).toEqual([
407
+ '$.layout.positions.a',
408
+ '$.layout.positions.aaa',
409
+ '$.layout.positions.zzz',
410
+ ]);
411
+ expect(flow.getSnapshot()).toBe(before);
412
+ expect(flow.history.canUndo()).toBe(false);
413
+ });
414
+ });
@@ -0,0 +1,23 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { markerToSVG } from './marker';
3
+
4
+ describe('markerToSVG', () => {
5
+ it('resolves built-in and safe local custom marker references', () => {
6
+ expect(markerToSVG('arrow-closed', 'flow-1')).toBe(
7
+ 'url(#flow-1-arrow-closed)',
8
+ );
9
+ expect(markerToSVG('custom-marker', 'flow-1')).toBe('url(#custom-marker)');
10
+ expect(markerToSVG('url(#custom-marker)', 'flow-1')).toBe(
11
+ 'url(#custom-marker)',
12
+ );
13
+ });
14
+
15
+ it('rejects external and malformed marker references', () => {
16
+ expect(markerToSVG('url(https://attacker.example/m.svg#a)', 'flow-1')).toBeUndefined();
17
+ expect(markerToSVG('custom) url(#other', 'flow-1')).toBeUndefined();
18
+ });
19
+
20
+ it('accepts blank markers as a backwards-compatible no-op', () => {
21
+ expect(markerToSVG('', 'flow-1')).toBeUndefined();
22
+ });
23
+ });
package/src/marker.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { MarkerType } from './types';
2
+
3
+ const BUILT_IN_MARKERS = new Set<MarkerType>([
4
+ 'arrow',
5
+ 'arrow-closed',
6
+ 'circle',
7
+ ]);
8
+ const SAFE_MARKER_ID = /^[A-Za-z_][A-Za-z0-9_.:-]*$/;
9
+ const LOCAL_MARKER_URL = /^url\(#([A-Za-z_][A-Za-z0-9_.:-]*)\)$/;
10
+
11
+ /** Convert a marker contract value to a safe local SVG marker reference. */
12
+ export function markerToSVG(
13
+ marker: MarkerType | undefined,
14
+ builtInPrefix: string,
15
+ ): string | undefined {
16
+ if (!marker || marker === 'none') return undefined;
17
+ if (BUILT_IN_MARKERS.has(marker)) {
18
+ return `url(#${builtInPrefix}-${marker})`;
19
+ }
20
+ const localReference = LOCAL_MARKER_URL.exec(marker);
21
+ if (localReference) return `url(#${localReference[1]})`;
22
+ return SAFE_MARKER_ID.test(marker) ? `url(#${marker})` : undefined;
23
+ }