@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.
- package/README.md +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createEdge, createGraph, createNode } from './factories';
|
|
3
|
+
import { createFlow, fromJSON } from './createFlow';
|
|
4
|
+
import { FlowGraphValidationError } from './validation';
|
|
5
|
+
|
|
6
|
+
function graph() {
|
|
7
|
+
return createGraph({
|
|
8
|
+
nodes: [
|
|
9
|
+
createNode({ id: 'a', x: 10, data: { count: 1, label: 'A' } }),
|
|
10
|
+
createNode({ id: 'b', x: 200, data: { count: 2, label: 'B' } }),
|
|
11
|
+
],
|
|
12
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('modern controller contracts', () => {
|
|
17
|
+
it.each([{ points: [] }, { points: [{ x: 10, y: 20 }] }])(
|
|
18
|
+
'accepts a short edge points array and falls back to normal routing',
|
|
19
|
+
({ points }) => {
|
|
20
|
+
const flow = createFlow({
|
|
21
|
+
graph: createGraph({
|
|
22
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b', x: 200 })],
|
|
23
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b', points })],
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(
|
|
28
|
+
flow.getAllEdgePaths().get('ab')?.segments[0]?.points.length,
|
|
29
|
+
).toBeGreaterThanOrEqual(2);
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
it.each(['', 'my marker', 'url(#a) url(#b)'])(
|
|
34
|
+
'accepts arbitrary edge marker names as renderer-sanitized decorations',
|
|
35
|
+
(marker) => {
|
|
36
|
+
expect(() =>
|
|
37
|
+
createFlow({
|
|
38
|
+
graph: createGraph({
|
|
39
|
+
nodes: [createNode({ id: 'a' })],
|
|
40
|
+
edges: [
|
|
41
|
+
createEdge({
|
|
42
|
+
id: 'aa',
|
|
43
|
+
sourceId: 'a',
|
|
44
|
+
targetId: 'a',
|
|
45
|
+
style: { markerStart: marker, markerEnd: marker },
|
|
46
|
+
}),
|
|
47
|
+
],
|
|
48
|
+
}),
|
|
49
|
+
}),
|
|
50
|
+
).not.toThrow();
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
it('validates initial graphs and raw setGraph atomically', () => {
|
|
55
|
+
expect(() => createFlow({
|
|
56
|
+
graph: createGraph({
|
|
57
|
+
nodes: [createNode({ id: 'same' }), createNode({ id: 'same' })],
|
|
58
|
+
edges: [],
|
|
59
|
+
}),
|
|
60
|
+
})).toThrow(FlowGraphValidationError);
|
|
61
|
+
|
|
62
|
+
const flow = createFlow({ graph: graph() });
|
|
63
|
+
const before = flow.getSnapshot().graph;
|
|
64
|
+
flow.internals.store.trigger.setGraph({
|
|
65
|
+
graph: createGraph({ nodes: [], edges: [createEdge({ id: 'bad', sourceId: 'x', targetId: 'y' })] }),
|
|
66
|
+
});
|
|
67
|
+
expect(flow.getSnapshot().graph).toBe(before);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('rejects malformed authoritative edge geometry on load', () => {
|
|
71
|
+
const invalidRoutes = [
|
|
72
|
+
{ points: [{ x: 0, y: 0 }, { x: 'bad', y: 10 }] },
|
|
73
|
+
{ routing: 'curved' },
|
|
74
|
+
{ svgPath: '<script>alert(1)</script>' },
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
for (const route of invalidRoutes) {
|
|
78
|
+
expect(() =>
|
|
79
|
+
fromJSON({
|
|
80
|
+
version: 1,
|
|
81
|
+
graph: {
|
|
82
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
83
|
+
edges: [{ ...createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }), ...route }],
|
|
84
|
+
},
|
|
85
|
+
viewport: { x: 0, y: 0, zoom: 1 },
|
|
86
|
+
}),
|
|
87
|
+
).toThrow(FlowGraphValidationError);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('invalidates unchanged authoritative geometry when controller reconnects', () => {
|
|
92
|
+
const input = graph();
|
|
93
|
+
input.nodes.push(createNode({ id: 'c', x: 400 }));
|
|
94
|
+
Object.assign(input.edges[0], {
|
|
95
|
+
points: [{ x: 10, y: 0 }, { x: 200, y: 0 }],
|
|
96
|
+
routing: 'polyline',
|
|
97
|
+
svgPath: 'M 10 0 L 200 0',
|
|
98
|
+
});
|
|
99
|
+
const flow = createFlow({ graph: input });
|
|
100
|
+
|
|
101
|
+
flow.edges.reconnect('ab', { newTargetId: 'c' });
|
|
102
|
+
|
|
103
|
+
expect(flow.getEdge('ab')).toMatchObject({ targetId: 'c' });
|
|
104
|
+
expect(flow.getEdge('ab')).not.toHaveProperty('points');
|
|
105
|
+
expect(flow.getEdge('ab')).not.toHaveProperty('routing');
|
|
106
|
+
expect(flow.getEdge('ab')).not.toHaveProperty('svgPath');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('preserves replacement authoritative geometry supplied with new endpoints', () => {
|
|
110
|
+
const input = graph();
|
|
111
|
+
input.nodes.push(createNode({ id: 'c', x: 400 }));
|
|
112
|
+
Object.assign(input.edges[0], {
|
|
113
|
+
points: [{ x: 10, y: 0 }, { x: 200, y: 0 }],
|
|
114
|
+
routing: 'polyline',
|
|
115
|
+
svgPath: 'M 10 0 L 200 0',
|
|
116
|
+
});
|
|
117
|
+
const flow = createFlow({ graph: input });
|
|
118
|
+
|
|
119
|
+
flow.edges.update('ab', {
|
|
120
|
+
targetId: 'c',
|
|
121
|
+
points: [{ x: 10, y: 0 }, { x: 400, y: 0 }],
|
|
122
|
+
routing: 'orthogonal',
|
|
123
|
+
svgPath: 'M 10 0 L 400 0',
|
|
124
|
+
} as any);
|
|
125
|
+
|
|
126
|
+
expect(flow.getEdge('ab')).toMatchObject({
|
|
127
|
+
targetId: 'c',
|
|
128
|
+
points: [{ x: 10, y: 0 }, { x: 400, y: 0 }],
|
|
129
|
+
routing: 'orthogonal',
|
|
130
|
+
svgPath: 'M 10 0 L 400 0',
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it.each(['route-first', 'node-first'] as const)(
|
|
135
|
+
'preserves transaction-authored edge geometry when updates are %s',
|
|
136
|
+
(order) => {
|
|
137
|
+
const flow = createFlow({ graph: graph() });
|
|
138
|
+
const route = {
|
|
139
|
+
points: [{ x: 50, y: 20 }, { x: 240, y: 20 }],
|
|
140
|
+
routing: 'orthogonal' as const,
|
|
141
|
+
svgPath: 'M 50 20 L 240 20',
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
flow.transaction((draft) => {
|
|
145
|
+
const updateRoute = () => draft.edges.update('ab', route);
|
|
146
|
+
const updateNode = () => draft.nodes.update('a', { x: 50 });
|
|
147
|
+
if (order === 'route-first') {
|
|
148
|
+
updateRoute();
|
|
149
|
+
updateNode();
|
|
150
|
+
} else {
|
|
151
|
+
updateNode();
|
|
152
|
+
updateRoute();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(flow.getEdge('ab')).toMatchObject(route);
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
it('preserves a transaction-authored group route when a child moves later', () => {
|
|
161
|
+
const flow = createFlow({
|
|
162
|
+
graph: createGraph({
|
|
163
|
+
nodes: [
|
|
164
|
+
createNode({ id: 'group', group: { type: 'derived', padding: 10 } }),
|
|
165
|
+
createNode({ id: 'child', parentId: 'group', x: 20, y: 20 }),
|
|
166
|
+
createNode({ id: 'target', x: 300 }),
|
|
167
|
+
],
|
|
168
|
+
edges: [createEdge({ id: 'edge', sourceId: 'group', targetId: 'target' })],
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
const route = {
|
|
172
|
+
points: [{ x: 100, y: 25 }, { x: 300, y: 25 }],
|
|
173
|
+
routing: 'orthogonal' as const,
|
|
174
|
+
svgPath: 'M 100 25 L 300 25',
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
flow.transaction((draft) => {
|
|
178
|
+
draft.edges.update('edge', route);
|
|
179
|
+
draft.nodes.update('child', { x: 50 });
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
expect(flow.getEdge('edge')).toMatchObject(route);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('invalidates a group route when a child moves outside a layout batch', () => {
|
|
186
|
+
const route = {
|
|
187
|
+
points: [{ x: 100, y: 25 }, { x: 300, y: 25 }],
|
|
188
|
+
routing: 'orthogonal' as const,
|
|
189
|
+
svgPath: 'M 100 25 L 300 25',
|
|
190
|
+
};
|
|
191
|
+
const flow = createFlow({
|
|
192
|
+
graph: createGraph({
|
|
193
|
+
nodes: [
|
|
194
|
+
createNode({ id: 'group', group: { type: 'derived', padding: 10 } }),
|
|
195
|
+
createNode({ id: 'child', parentId: 'group', x: 20, y: 20 }),
|
|
196
|
+
createNode({ id: 'target', x: 300 }),
|
|
197
|
+
],
|
|
198
|
+
edges: [
|
|
199
|
+
createEdge({
|
|
200
|
+
id: 'edge',
|
|
201
|
+
sourceId: 'group',
|
|
202
|
+
targetId: 'target',
|
|
203
|
+
...route,
|
|
204
|
+
}),
|
|
205
|
+
],
|
|
206
|
+
}),
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
flow.nodes.update('child', { x: 50 });
|
|
210
|
+
|
|
211
|
+
expect(flow.getEdge('edge')).not.toHaveProperty('points');
|
|
212
|
+
expect(flow.getEdge('edge')).not.toHaveProperty('routing');
|
|
213
|
+
expect(flow.getEdge('edge')).not.toHaveProperty('svgPath');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it.each(['create', 'delete'] as const)(
|
|
217
|
+
'invalidates a group route when a child is %sd',
|
|
218
|
+
(operation) => {
|
|
219
|
+
const flow = createFlow({
|
|
220
|
+
graph: createGraph({
|
|
221
|
+
nodes: [
|
|
222
|
+
createNode({ id: 'group', group: { type: 'derived', padding: 10 } }),
|
|
223
|
+
createNode({ id: 'child', parentId: 'group', x: 20, y: 20 }),
|
|
224
|
+
createNode({ id: 'target', x: 300 }),
|
|
225
|
+
],
|
|
226
|
+
edges: [
|
|
227
|
+
createEdge({
|
|
228
|
+
id: 'edge',
|
|
229
|
+
sourceId: 'group',
|
|
230
|
+
targetId: 'target',
|
|
231
|
+
points: [{ x: 100, y: 25 }, { x: 300, y: 25 }],
|
|
232
|
+
routing: 'orthogonal',
|
|
233
|
+
svgPath: 'M 100 25 L 300 25',
|
|
234
|
+
}),
|
|
235
|
+
],
|
|
236
|
+
}),
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
if (operation === 'create') {
|
|
240
|
+
flow.nodes.create({ id: 'new-child', parentId: 'group', x: 80, y: 20 });
|
|
241
|
+
} else {
|
|
242
|
+
flow.nodes.delete('child');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
expect(flow.getEdge('edge')).not.toHaveProperty('points');
|
|
246
|
+
expect(flow.getEdge('edge')).not.toHaveProperty('routing');
|
|
247
|
+
expect(flow.getEdge('edge')).not.toHaveProperty('svgPath');
|
|
248
|
+
},
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
it('preserves arbitrary marker data on load for renderer-side sanitization', () => {
|
|
252
|
+
const flow = fromJSON({
|
|
253
|
+
version: 1,
|
|
254
|
+
graph: {
|
|
255
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
256
|
+
edges: [
|
|
257
|
+
createEdge({
|
|
258
|
+
id: 'ab',
|
|
259
|
+
sourceId: 'a',
|
|
260
|
+
targetId: 'b',
|
|
261
|
+
style: { markerEnd: 'url(https://attacker.example/m.svg#a)' },
|
|
262
|
+
}),
|
|
263
|
+
],
|
|
264
|
+
},
|
|
265
|
+
viewport: { x: 0, y: 0, zoom: 1 },
|
|
266
|
+
});
|
|
267
|
+
expect(flow.getEdge('ab')?.style?.markerEnd).toBe(
|
|
268
|
+
'url(https://attacker.example/m.svg#a)',
|
|
269
|
+
);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('supports optional ids, retries generated collisions, and uses global ids', () => {
|
|
273
|
+
const createId = vi.fn(({ kind, attempt }) => attempt === 0 ? 'a' : `${kind}-new`);
|
|
274
|
+
const flow = createFlow({ graph: graph(), createId });
|
|
275
|
+
expect(flow.nodes.create({ data: { count: 0, label: 'new' } }).id).toBe('node-new');
|
|
276
|
+
expect(createId).toHaveBeenLastCalledWith(expect.objectContaining({ attempt: 1 }));
|
|
277
|
+
expect(() => flow.edges.create({ id: 'a', sourceId: 'a', targetId: 'b' })).toThrow(/already exists/);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('patches object data and fully replaces updater data', () => {
|
|
281
|
+
const flow = createFlow({ graph: graph() });
|
|
282
|
+
flow.nodes.setData('a', { count: 3 });
|
|
283
|
+
expect(flow.getNode('a')?.data).toEqual({ count: 3, label: 'A' });
|
|
284
|
+
flow.nodes.setData('a', () => ({ count: 9, label: 'replaced' }));
|
|
285
|
+
expect(flow.getNode('a')?.data).toEqual({ count: 9, label: 'replaced' });
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('commits a nested transaction once and round-trips inverse patches', () => {
|
|
289
|
+
const flow = createFlow({ graph: graph() });
|
|
290
|
+
const commits = vi.fn();
|
|
291
|
+
flow.on('commit', commits);
|
|
292
|
+
const before = flow.toJSON().graph;
|
|
293
|
+
const result = flow.transaction((draft) => {
|
|
294
|
+
draft.nodes.setData('a', { count: 3 });
|
|
295
|
+
const nested = flow.transaction(() => draft.edges.delete('ab'));
|
|
296
|
+
expect(nested.changeSet).toBeNull();
|
|
297
|
+
return 'done';
|
|
298
|
+
}, { origin: 'test', transactionId: 'tx' });
|
|
299
|
+
|
|
300
|
+
expect(result.value).toBe('done');
|
|
301
|
+
expect(result.changeSet?.transactionId).toBe('tx');
|
|
302
|
+
expect(commits).toHaveBeenCalledTimes(1);
|
|
303
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
304
|
+
const inverse = flow.applyChangeSet({
|
|
305
|
+
...result.changeSet!,
|
|
306
|
+
forward: result.changeSet!.inverse,
|
|
307
|
+
});
|
|
308
|
+
expect(inverse.ok).toBe(true);
|
|
309
|
+
expect(flow.toJSON().graph).toEqual(before);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('replaces a canonical external graph without creating local undo history', () => {
|
|
313
|
+
const flow = createFlow({ graph: graph() });
|
|
314
|
+
const commits = vi.fn();
|
|
315
|
+
flow.on('commit', commits);
|
|
316
|
+
flow.nodes.create({ id: 'local' });
|
|
317
|
+
flow.internals.store.trigger.updateParentNodeId({ parentNodeId: 'b' });
|
|
318
|
+
flow.selection.set(['a', 'b', 'local'], 'multi');
|
|
319
|
+
flow.highlights.set('active', [
|
|
320
|
+
{ id: 'a', meta: 'keep' },
|
|
321
|
+
{ id: 'b', meta: 'prune' },
|
|
322
|
+
]);
|
|
323
|
+
flow.internals.store.trigger.setHover({ ids: ['a', 'b'] });
|
|
324
|
+
flow.internals.store.trigger.setCollapsedNodes({ ids: ['a', 'b'] });
|
|
325
|
+
flow.internals.store.trigger.focus({ entityId: 'b' });
|
|
326
|
+
flow.internals.store.trigger.updateMeasurements({
|
|
327
|
+
measurements: [
|
|
328
|
+
{ id: 'a', rect: { x: 0, y: 0, width: 100, height: 60 } },
|
|
329
|
+
{ id: 'b', rect: { x: 200, y: 0, width: 100, height: 60 } },
|
|
330
|
+
],
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
const replacement = createGraph({
|
|
334
|
+
nodes: [
|
|
335
|
+
createNode({ id: 'a', x: 40, data: { count: 4, label: 'A+' } }),
|
|
336
|
+
createNode({ id: 'c', x: 300, data: { count: 3, label: 'C' } }),
|
|
337
|
+
],
|
|
338
|
+
edges: [],
|
|
339
|
+
});
|
|
340
|
+
const changeSet = flow.replaceGraph(replacement, {
|
|
341
|
+
origin: 'viz.machine',
|
|
342
|
+
transactionId: 'machine-revision-7',
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
expect(changeSet).toMatchObject({
|
|
346
|
+
origin: 'viz.machine',
|
|
347
|
+
transactionId: 'machine-revision-7',
|
|
348
|
+
});
|
|
349
|
+
expect(flow.getSnapshot().graph).toEqual(replacement);
|
|
350
|
+
expect([...flow.getSnapshot().selection]).toEqual(['a']);
|
|
351
|
+
expect([...flow.getSnapshot().hoveredEntityIds]).toEqual(['a']);
|
|
352
|
+
expect([...flow.getSnapshot().collapsedNodeIds]).toEqual(['a']);
|
|
353
|
+
expect([...flow.getSnapshot().measurements.keys()]).toEqual(['a']);
|
|
354
|
+
expect(flow.getSnapshot().focusedEntityId).toBeNull();
|
|
355
|
+
expect(flow.getSnapshot().parentNodeId).toBeNull();
|
|
356
|
+
expect([...flow.getSnapshot().highlights.get('active')!]).toEqual([
|
|
357
|
+
['a', 'keep'],
|
|
358
|
+
]);
|
|
359
|
+
expect(flow.history.canUndo()).toBe(false);
|
|
360
|
+
expect(flow.history.canRedo()).toBe(false);
|
|
361
|
+
expect(commits).toHaveBeenLastCalledWith(
|
|
362
|
+
expect.objectContaining({
|
|
363
|
+
origin: 'viz.machine',
|
|
364
|
+
transactionId: 'machine-revision-7',
|
|
365
|
+
}),
|
|
366
|
+
);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('cancels only replacement sessions whose addressed entities disappear', () => {
|
|
370
|
+
const replacement = createGraph({
|
|
371
|
+
nodes: [createNode({ id: 'a' })],
|
|
372
|
+
edges: [],
|
|
373
|
+
});
|
|
374
|
+
const makeFlow = () => createFlow({ graph: graph() });
|
|
375
|
+
|
|
376
|
+
const removedDrag = makeFlow();
|
|
377
|
+
removedDrag.internals.store.trigger.select({ ids: ['b'] });
|
|
378
|
+
removedDrag.internals.store.trigger.dragStart({ id: 'b' });
|
|
379
|
+
removedDrag.replaceGraph(replacement);
|
|
380
|
+
expect(removedDrag.getSnapshot()).toMatchObject({
|
|
381
|
+
canvasState: 'idle',
|
|
382
|
+
draggedEntityId: null,
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
const survivingDrag = makeFlow();
|
|
386
|
+
survivingDrag.internals.store.trigger.select({ ids: ['a'] });
|
|
387
|
+
survivingDrag.internals.store.trigger.dragStart({ id: 'a' });
|
|
388
|
+
survivingDrag.replaceGraph(replacement);
|
|
389
|
+
expect(survivingDrag.getSnapshot()).toMatchObject({
|
|
390
|
+
canvasState: 'dragging',
|
|
391
|
+
draggedEntityId: 'a',
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
const removedResize = makeFlow();
|
|
395
|
+
removedResize.internals.store.trigger.resizeStart({
|
|
396
|
+
id: 'b',
|
|
397
|
+
handle: 'bottom-right',
|
|
398
|
+
});
|
|
399
|
+
removedResize.replaceGraph(replacement);
|
|
400
|
+
expect(removedResize.getSnapshot()).toMatchObject({
|
|
401
|
+
canvasState: 'idle',
|
|
402
|
+
resizingEntityId: null,
|
|
403
|
+
resizeHandle: null,
|
|
404
|
+
resizeCorners: null,
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
const removedConnection = makeFlow();
|
|
408
|
+
removedConnection.internals.store.trigger.connectionStart({
|
|
409
|
+
sourceId: 'b',
|
|
410
|
+
sourcePort: null,
|
|
411
|
+
position: { x: 0, y: 0 },
|
|
412
|
+
});
|
|
413
|
+
removedConnection.internals.store.trigger.setConnectionClickStartHandle({
|
|
414
|
+
nodeId: 'b',
|
|
415
|
+
port: null,
|
|
416
|
+
});
|
|
417
|
+
removedConnection.replaceGraph(replacement);
|
|
418
|
+
expect(removedConnection.getSnapshot()).toMatchObject({
|
|
419
|
+
canvasState: 'idle',
|
|
420
|
+
connection: null,
|
|
421
|
+
connectionClickStartHandle: null,
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('supports reset retention and explicit replacement history policies', () => {
|
|
426
|
+
const initial = graph();
|
|
427
|
+
const replacement = createGraph({
|
|
428
|
+
nodes: [createNode({ id: 'a', x: 100 })],
|
|
429
|
+
edges: [],
|
|
430
|
+
});
|
|
431
|
+
const flow = createFlow({ graph: initial });
|
|
432
|
+
flow.selection.set(['a']);
|
|
433
|
+
flow.highlights.set('active', [{ id: 'a' }]);
|
|
434
|
+
flow.internals.store.trigger.setHover({ ids: ['a'] });
|
|
435
|
+
flow.internals.store.trigger.setCollapsedNodes({ ids: ['a'] });
|
|
436
|
+
flow.internals.store.trigger.updateMeasurements({
|
|
437
|
+
measurements: [
|
|
438
|
+
{ id: 'a', rect: { x: 0, y: 0, width: 100, height: 60 } },
|
|
439
|
+
],
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
flow.replaceGraph(replacement, {
|
|
443
|
+
retention: 'reset',
|
|
444
|
+
history: 'record',
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
expect(flow.getSnapshot().selection.size).toBe(0);
|
|
448
|
+
expect(flow.getSnapshot().hoveredEntityIds.size).toBe(0);
|
|
449
|
+
expect(flow.getSnapshot().collapsedNodeIds.size).toBe(0);
|
|
450
|
+
expect(flow.getSnapshot().highlights.size).toBe(0);
|
|
451
|
+
expect(flow.getSnapshot().measurements.size).toBe(0);
|
|
452
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
453
|
+
flow.history.undo();
|
|
454
|
+
expect(flow.getSnapshot().graph).toEqual(initial);
|
|
455
|
+
|
|
456
|
+
flow.nodes.create({ id: 'local' });
|
|
457
|
+
flow.replaceGraph(replacement, { history: 'ignore' });
|
|
458
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
459
|
+
flow.history.undo();
|
|
460
|
+
expect(flow.getSnapshot().graph).toEqual(initial);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it('validates external replacements atomically and treats equal graphs as no-ops', () => {
|
|
464
|
+
const flow = createFlow({ graph: graph() });
|
|
465
|
+
const commits = vi.fn();
|
|
466
|
+
flow.on('commit', commits);
|
|
467
|
+
flow.selection.set(['a']);
|
|
468
|
+
const before = flow.toJSON().graph;
|
|
469
|
+
|
|
470
|
+
expect(() =>
|
|
471
|
+
flow.replaceGraph(
|
|
472
|
+
createGraph({
|
|
473
|
+
nodes: [],
|
|
474
|
+
edges: [createEdge({ id: 'bad', sourceId: 'missing', targetId: 'also-missing' })],
|
|
475
|
+
}),
|
|
476
|
+
),
|
|
477
|
+
).toThrow(FlowGraphValidationError);
|
|
478
|
+
expect(flow.toJSON().graph).toEqual(before);
|
|
479
|
+
expect(commits).not.toHaveBeenCalled();
|
|
480
|
+
|
|
481
|
+
expect(flow.replaceGraph(before, { retention: 'reset' })).toBeNull();
|
|
482
|
+
expect(flow.getSnapshot().selection.size).toBe(0);
|
|
483
|
+
expect(commits).not.toHaveBeenCalled();
|
|
484
|
+
|
|
485
|
+
flow.selection.set(['a']);
|
|
486
|
+
const beforeNoOp = flow.getSnapshot();
|
|
487
|
+
expect(flow.replaceGraph(before, { history: 'ignore' })).toBeNull();
|
|
488
|
+
expect(flow.getSnapshot()).toBe(beforeNoOp);
|
|
489
|
+
expect(commits).not.toHaveBeenCalled();
|
|
490
|
+
|
|
491
|
+
expect(() =>
|
|
492
|
+
flow.replaceGraph(before, { history: 'invalid' as never }),
|
|
493
|
+
).toThrow(/history/i);
|
|
494
|
+
expect(() =>
|
|
495
|
+
flow.replaceGraph(before, { retention: 'invalid' as never }),
|
|
496
|
+
).toThrow(/retention/i);
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
it('does not emit commit events for transient drag frames', () => {
|
|
500
|
+
const flow = createFlow({ graph: graph() });
|
|
501
|
+
const commits = vi.fn();
|
|
502
|
+
flow.on('commit', commits);
|
|
503
|
+
|
|
504
|
+
flow.internals.store.trigger.select({ ids: ['a'] });
|
|
505
|
+
flow.internals.store.trigger.dragStart({ id: 'a' });
|
|
506
|
+
flow.internals.store.trigger.dragMove({ dx: 20, dy: 10 });
|
|
507
|
+
expect(commits).not.toHaveBeenCalled();
|
|
508
|
+
|
|
509
|
+
flow.internals.store.trigger.dragEnd();
|
|
510
|
+
flow.internals.store.trigger.commitDrag();
|
|
511
|
+
expect(commits).toHaveBeenCalledTimes(1);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it('does not apply layout after the graph changes', async () => {
|
|
515
|
+
let resolveLayout!: (value: Map<string, { x: number; y: number }>) => void;
|
|
516
|
+
const flow = createFlow({ graph: graph() });
|
|
517
|
+
const pending = flow.layout.run(
|
|
518
|
+
() => new Promise((resolve) => {
|
|
519
|
+
resolveLayout = resolve;
|
|
520
|
+
})
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
flow.nodes.update('a', { x: 40 });
|
|
524
|
+
resolveLayout(new Map([['a', { x: 999, y: 999 }]]));
|
|
525
|
+
|
|
526
|
+
await expect(pending).resolves.toMatchObject({ applied: false });
|
|
527
|
+
expect(flow.getNode('a')?.x).toBe(40);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it('rolls back transaction errors and rejects async callbacks', async () => {
|
|
531
|
+
const flow = createFlow({ graph: graph() });
|
|
532
|
+
const before = flow.toJSON();
|
|
533
|
+
expect(() => flow.transaction((draft) => {
|
|
534
|
+
draft.nodes.delete('a');
|
|
535
|
+
throw new Error('stop');
|
|
536
|
+
})).toThrow('stop');
|
|
537
|
+
expect(flow.toJSON()).toEqual(before);
|
|
538
|
+
expect(() => flow.transaction(() => Promise.resolve('no'))).toThrow(/synchronous/);
|
|
539
|
+
expect(flow.toJSON()).toEqual(before);
|
|
540
|
+
|
|
541
|
+
let continuation!: Promise<void>;
|
|
542
|
+
expect(() => flow.transaction((draft) => {
|
|
543
|
+
continuation = Promise.resolve().then(() => {
|
|
544
|
+
draft.nodes.create({ id: 'late' });
|
|
545
|
+
});
|
|
546
|
+
return continuation;
|
|
547
|
+
})).toThrow(/synchronous/);
|
|
548
|
+
await expect(continuation).rejects.toThrow(/transaction draft is closed/i);
|
|
549
|
+
expect(flow.toJSON()).toEqual(before);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it('reports malformed nested graph structures as validation errors', () => {
|
|
553
|
+
const node = createNode({ id: 'a' });
|
|
554
|
+
const malformed = [
|
|
555
|
+
{ nodes: [{ ...node, ports: {} }], edges: [] },
|
|
556
|
+
{
|
|
557
|
+
nodes: [node],
|
|
558
|
+
edges: [
|
|
559
|
+
{
|
|
560
|
+
...createEdge({ id: 'aa', sourceId: 'a', targetId: 'a' }),
|
|
561
|
+
waypoints: {},
|
|
562
|
+
},
|
|
563
|
+
],
|
|
564
|
+
},
|
|
565
|
+
{
|
|
566
|
+
nodes: [node],
|
|
567
|
+
edges: [
|
|
568
|
+
{
|
|
569
|
+
...createEdge({ id: 'aa', sourceId: 'a', targetId: 'a' }),
|
|
570
|
+
label: 'invalid',
|
|
571
|
+
},
|
|
572
|
+
],
|
|
573
|
+
},
|
|
574
|
+
];
|
|
575
|
+
|
|
576
|
+
for (const candidate of malformed) {
|
|
577
|
+
expect(() => createFlow({ graph: candidate as never })).toThrow(
|
|
578
|
+
FlowGraphValidationError,
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
expect(() =>
|
|
583
|
+
fromJSON(
|
|
584
|
+
{
|
|
585
|
+
version: 1,
|
|
586
|
+
graph: { nodes: {}, edges: [] },
|
|
587
|
+
viewport: { x: 0, y: 0, zoom: 1 },
|
|
588
|
+
},
|
|
589
|
+
{ dataSchemas: { node: { version: 1 } } },
|
|
590
|
+
),
|
|
591
|
+
).toThrow(FlowGraphValidationError);
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it('extracts nested portable content and remaps every structural id', () => {
|
|
595
|
+
const source = createFlow({ graph: graph() });
|
|
596
|
+
source.selection.set(['a', 'b'], 'multi');
|
|
597
|
+
const content = source.content.extract();
|
|
598
|
+
const target = createFlow({
|
|
599
|
+
graph: createGraph({ nodes: [], edges: [] }),
|
|
600
|
+
createId: ({ kind, sourceId }) => `${kind}:${sourceId}`,
|
|
601
|
+
});
|
|
602
|
+
const inserted = target.content.insert(content, { point: { x: 500, y: 50 } });
|
|
603
|
+
expect(inserted.ok).toBe(true);
|
|
604
|
+
if (!inserted.ok) return;
|
|
605
|
+
expect([...inserted.value.idMap]).toEqual([
|
|
606
|
+
['a', 'node:a'], ['b', 'node:b'], ['ab', 'edge:ab'],
|
|
607
|
+
]);
|
|
608
|
+
expect(target.getNode('node:a')?.x).toBe(500);
|
|
609
|
+
expect(target.getEdge('edge:ab')).toMatchObject({
|
|
610
|
+
sourceId: 'node:a', targetId: 'node:b',
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it('reroutes offset inserted content and translates its waypoints', () => {
|
|
615
|
+
const sourceGraph = graph();
|
|
616
|
+
Object.assign(sourceGraph.edges[0], {
|
|
617
|
+
points: [{ x: 30, y: 20 }, { x: 220, y: 20 }],
|
|
618
|
+
routing: 'polyline',
|
|
619
|
+
svgPath: 'M 30 20 L 220 20',
|
|
620
|
+
waypoints: [{ x: 120, y: 60 }],
|
|
621
|
+
});
|
|
622
|
+
const source = createFlow({ graph: sourceGraph });
|
|
623
|
+
const content = source.content.extract(['a', 'b']);
|
|
624
|
+
const target = createFlow({
|
|
625
|
+
graph: createGraph({ nodes: [], edges: [] }),
|
|
626
|
+
createId: ({ kind, sourceId }) => `${kind}:${sourceId}`,
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
expect(target.content.insert(content, { point: { x: 500, y: 50 } }).ok).toBe(true);
|
|
630
|
+
const edge = target.getEdge('edge:ab')!;
|
|
631
|
+
expect(edge).not.toHaveProperty('points');
|
|
632
|
+
expect(edge).not.toHaveProperty('routing');
|
|
633
|
+
expect(edge).not.toHaveProperty('svgPath');
|
|
634
|
+
expect(edge.waypoints).toEqual([{ x: 610, y: 110 }]);
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it('deletes descendants with their parent by default', () => {
|
|
638
|
+
const flow = createFlow({
|
|
639
|
+
graph: createGraph({
|
|
640
|
+
nodes: [
|
|
641
|
+
createNode({ id: 'parent' }),
|
|
642
|
+
createNode({ id: 'child', parentId: 'parent' }),
|
|
643
|
+
createNode({ id: 'grandchild', parentId: 'child' }),
|
|
644
|
+
createNode({ id: 'outside' }),
|
|
645
|
+
],
|
|
646
|
+
edges: [
|
|
647
|
+
createEdge({
|
|
648
|
+
id: 'boundary',
|
|
649
|
+
sourceId: 'grandchild',
|
|
650
|
+
targetId: 'outside',
|
|
651
|
+
}),
|
|
652
|
+
],
|
|
653
|
+
}),
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
expect(flow.nodes.delete('parent')).toBe(true);
|
|
657
|
+
expect(flow.getSnapshot().graph.nodes.map((node) => node.id)).toEqual([
|
|
658
|
+
'outside',
|
|
659
|
+
]);
|
|
660
|
+
expect(flow.getSnapshot().graph.edges).toEqual([]);
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
it('detaches copied subtree roots from parents outside the content', () => {
|
|
664
|
+
const source = createFlow({
|
|
665
|
+
graph: createGraph({
|
|
666
|
+
nodes: [
|
|
667
|
+
createNode({ id: 'parent' }),
|
|
668
|
+
createNode({ id: 'child', parentId: 'parent', x: 20, y: 30 }),
|
|
669
|
+
createNode({ id: 'grandchild', parentId: 'child', x: 40, y: 50 }),
|
|
670
|
+
],
|
|
671
|
+
edges: [],
|
|
672
|
+
}),
|
|
673
|
+
});
|
|
674
|
+
const content = source.content.extract(['child']);
|
|
675
|
+
|
|
676
|
+
expect(content?.nodes).toEqual([
|
|
677
|
+
expect.objectContaining({ id: 'child', parentId: null }),
|
|
678
|
+
expect.objectContaining({ id: 'grandchild', parentId: 'child' }),
|
|
679
|
+
]);
|
|
680
|
+
|
|
681
|
+
const target = createFlow({
|
|
682
|
+
graph: createGraph({ nodes: [], edges: [] }),
|
|
683
|
+
createId: ({ sourceId }) => `copy:${sourceId}`,
|
|
684
|
+
});
|
|
685
|
+
const inserted = target.content.insert(content, { preservePosition: true });
|
|
686
|
+
expect(inserted.ok).toBe(true);
|
|
687
|
+
expect(target.getNode('copy:child')).toMatchObject({
|
|
688
|
+
parentId: null,
|
|
689
|
+
x: 20,
|
|
690
|
+
y: 30,
|
|
691
|
+
});
|
|
692
|
+
expect(target.getNode('copy:grandchild')?.parentId).toBe('copy:child');
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
it('queries stable connections and reports capability denials', () => {
|
|
696
|
+
const flow = createFlow({ graph: graph() });
|
|
697
|
+
expect(flow.connections.get({ nodeId: 'a', direction: 'out' })[0]).toMatchObject({
|
|
698
|
+
edge: { id: 'ab' }, source: { nodeId: 'a' }, target: { nodeId: 'b' },
|
|
699
|
+
});
|
|
700
|
+
flow.edges.create({ id: 'aa', sourceId: 'a', targetId: 'a' });
|
|
701
|
+
expect(flow.connections.get({ nodeId: 'a' }).map(({ edge }) => edge.id)).toEqual([
|
|
702
|
+
'ab',
|
|
703
|
+
'aa',
|
|
704
|
+
]);
|
|
705
|
+
expect(
|
|
706
|
+
flow.connections.get({ nodeId: 'a', direction: 'in' }).map(({ edge }) => edge.id),
|
|
707
|
+
).toEqual(['aa']);
|
|
708
|
+
expect(flow.check('copy')).toMatchObject({ allowed: false, reason: { code: 'empty' } });
|
|
709
|
+
flow.selection.set(['a']);
|
|
710
|
+
expect(flow.can('copy')).toBe(true);
|
|
711
|
+
flow.mode.set('readonly');
|
|
712
|
+
expect(flow.can('delete')).toBe(false);
|
|
713
|
+
expect(flow.can('copy')).toBe(true);
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
it('rejects future documents without mutating and fromJSON throws', () => {
|
|
717
|
+
const flow = createFlow({ graph: graph() });
|
|
718
|
+
const before = flow.toJSON();
|
|
719
|
+
const result = flow.load({ ...before, version: 99 });
|
|
720
|
+
expect(result.ok).toBe(false);
|
|
721
|
+
expect(flow.toJSON()).toEqual(before);
|
|
722
|
+
expect(() => fromJSON({ ...before, version: 99 })).toThrow(/unsupported/);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
it('skips selector evaluation for unchanged declared context keys', () => {
|
|
727
|
+
const flow = createFlow({ graph: createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }) });
|
|
728
|
+
let calls = 0;
|
|
729
|
+
const changes: number[] = [];
|
|
730
|
+
const unsubscribe = flow.subscribe(
|
|
731
|
+
(context) => { calls++; return context.selection.size; },
|
|
732
|
+
(size) => changes.push(size),
|
|
733
|
+
{ keys: ['selection'] },
|
|
734
|
+
);
|
|
735
|
+
flow.viewport.panBy({ x: 10, y: 20 });
|
|
736
|
+
flow.nodes.setData('a', { label: 'updated' });
|
|
737
|
+
expect(calls).toBe(1);
|
|
738
|
+
flow.selection.set(['a']);
|
|
739
|
+
expect(calls).toBe(2);
|
|
740
|
+
expect(changes).toEqual([1]);
|
|
741
|
+
unsubscribe();
|
|
742
|
+
flow.destroy();
|
|
743
|
+
});
|