@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.
- package/README.md +2 -1
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +19 -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,25 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createFlowBasedMathGraph, runFlowBasedMathGraph } from './flow-based-math';
|
|
3
|
+
|
|
4
|
+
describe('flow based math example graph', () => {
|
|
5
|
+
it('computes output = (input * 2) + 10 from the initial node id', () => {
|
|
6
|
+
const result = runFlowBasedMathGraph(createFlowBasedMathGraph(), 42);
|
|
7
|
+
|
|
8
|
+
expect(result.results).toEqual({ output: 94 });
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('supports record-form inputs keyed by node id', () => {
|
|
12
|
+
const result = runFlowBasedMathGraph(createFlowBasedMathGraph(), { input: -5 });
|
|
13
|
+
|
|
14
|
+
expect(result.results).toEqual({ output: 0 });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('exposes intermediate port values for visualization', () => {
|
|
18
|
+
const result = runFlowBasedMathGraph(createFlowBasedMathGraph(), 7);
|
|
19
|
+
|
|
20
|
+
expect(result.portValues.get('double')).toEqual({ out: 14 });
|
|
21
|
+
expect(result.portValues.get('offset')).toEqual({ out: 10 });
|
|
22
|
+
expect(result.portValues.get('add')).toEqual({ out: 24 });
|
|
23
|
+
expect(result.portValues.get('output')).toEqual({ in: 24 });
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { getTopologicalSort, type GraphNode } from '@statelyai/graph';
|
|
2
|
+
import { createEdge, createGraph, createNode } from '../factories';
|
|
3
|
+
import type { FlowGraph, FlowNode, FlowPort } from '../types';
|
|
4
|
+
|
|
5
|
+
export type MathPortData = { type: 'number' | 'string' };
|
|
6
|
+
|
|
7
|
+
export type MathNodeData = {
|
|
8
|
+
op: 'input' | 'output' | 'constant' | 'double' | 'add' | 'multiply' | 'negate';
|
|
9
|
+
value?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type MathFlowGraph = FlowGraph<MathNodeData, any> & {
|
|
13
|
+
id: string;
|
|
14
|
+
type: 'directed' | 'undirected';
|
|
15
|
+
initialNodeId?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type MathRunResult = {
|
|
19
|
+
results: Record<string, number>;
|
|
20
|
+
portValues: Map<string, Record<string, number>>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function createPort(
|
|
24
|
+
name: string,
|
|
25
|
+
side: FlowPort<MathPortData>['side'],
|
|
26
|
+
direction: FlowPort<MathPortData>['direction'],
|
|
27
|
+
x: number,
|
|
28
|
+
y: number,
|
|
29
|
+
): FlowPort<MathPortData> {
|
|
30
|
+
return {
|
|
31
|
+
name,
|
|
32
|
+
side,
|
|
33
|
+
direction,
|
|
34
|
+
x,
|
|
35
|
+
y,
|
|
36
|
+
width: 12,
|
|
37
|
+
height: 12,
|
|
38
|
+
data: { type: 'number' },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createFlowBasedMathGraph(): MathFlowGraph {
|
|
43
|
+
return createGraph<MathNodeData>({
|
|
44
|
+
id: 'flow-based-math',
|
|
45
|
+
mode: 'directed',
|
|
46
|
+
initialNodeId: 'input',
|
|
47
|
+
nodes: [
|
|
48
|
+
createNode<MathNodeData>({
|
|
49
|
+
id: 'input',
|
|
50
|
+
label: 'Input',
|
|
51
|
+
x: 60,
|
|
52
|
+
y: 200,
|
|
53
|
+
width: 160,
|
|
54
|
+
height: 80,
|
|
55
|
+
data: { op: 'input' },
|
|
56
|
+
ports: [createPort('out', 'right', 'out', 154, 40)],
|
|
57
|
+
}),
|
|
58
|
+
createNode<MathNodeData>({
|
|
59
|
+
id: 'double',
|
|
60
|
+
label: 'Multiply x2',
|
|
61
|
+
x: 320,
|
|
62
|
+
y: 200,
|
|
63
|
+
width: 180,
|
|
64
|
+
height: 96,
|
|
65
|
+
data: { op: 'multiply', value: 2 },
|
|
66
|
+
ports: [
|
|
67
|
+
createPort('in', 'left', 'in', -6, 40),
|
|
68
|
+
createPort('out', 'right', 'out', 174, 40),
|
|
69
|
+
],
|
|
70
|
+
}),
|
|
71
|
+
createNode<MathNodeData>({
|
|
72
|
+
id: 'offset',
|
|
73
|
+
label: 'Constant 10',
|
|
74
|
+
x: 320,
|
|
75
|
+
y: 60,
|
|
76
|
+
width: 180,
|
|
77
|
+
height: 80,
|
|
78
|
+
data: { op: 'constant', value: 10 },
|
|
79
|
+
ports: [createPort('out', 'bottom', 'out', 84, 74)],
|
|
80
|
+
}),
|
|
81
|
+
createNode<MathNodeData>({
|
|
82
|
+
id: 'add',
|
|
83
|
+
label: 'Add',
|
|
84
|
+
x: 600,
|
|
85
|
+
y: 120,
|
|
86
|
+
width: 180,
|
|
87
|
+
height: 140,
|
|
88
|
+
data: { op: 'add' },
|
|
89
|
+
ports: [
|
|
90
|
+
createPort('a', 'left', 'in', -6, 40),
|
|
91
|
+
createPort('b', 'left', 'in', -6, 100),
|
|
92
|
+
createPort('out', 'right', 'out', 174, 70),
|
|
93
|
+
],
|
|
94
|
+
}),
|
|
95
|
+
createNode<MathNodeData>({
|
|
96
|
+
id: 'output',
|
|
97
|
+
label: 'Output',
|
|
98
|
+
x: 880,
|
|
99
|
+
y: 168,
|
|
100
|
+
width: 160,
|
|
101
|
+
height: 80,
|
|
102
|
+
data: { op: 'output' },
|
|
103
|
+
ports: [createPort('in', 'left', 'in', -6, 40)],
|
|
104
|
+
}),
|
|
105
|
+
],
|
|
106
|
+
edges: [
|
|
107
|
+
createEdge({
|
|
108
|
+
id: 'e1',
|
|
109
|
+
sourceId: 'input',
|
|
110
|
+
targetId: 'double',
|
|
111
|
+
sourcePort: 'out',
|
|
112
|
+
targetPort: 'in',
|
|
113
|
+
}),
|
|
114
|
+
createEdge({
|
|
115
|
+
id: 'e2',
|
|
116
|
+
sourceId: 'double',
|
|
117
|
+
targetId: 'add',
|
|
118
|
+
sourcePort: 'out',
|
|
119
|
+
targetPort: 'a',
|
|
120
|
+
}),
|
|
121
|
+
createEdge({
|
|
122
|
+
id: 'e3',
|
|
123
|
+
sourceId: 'offset',
|
|
124
|
+
targetId: 'add',
|
|
125
|
+
sourcePort: 'out',
|
|
126
|
+
targetPort: 'b',
|
|
127
|
+
}),
|
|
128
|
+
createEdge({
|
|
129
|
+
id: 'e4',
|
|
130
|
+
sourceId: 'add',
|
|
131
|
+
targetId: 'output',
|
|
132
|
+
sourcePort: 'out',
|
|
133
|
+
targetPort: 'in',
|
|
134
|
+
}),
|
|
135
|
+
],
|
|
136
|
+
}) as MathFlowGraph;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function evaluateMathNode(
|
|
140
|
+
node: GraphNode<MathNodeData>,
|
|
141
|
+
inputs: Record<string, number>,
|
|
142
|
+
): Record<string, number> {
|
|
143
|
+
const { op, value } = node.data;
|
|
144
|
+
|
|
145
|
+
switch (op) {
|
|
146
|
+
case 'input':
|
|
147
|
+
return { out: inputs.out ?? value ?? 0 };
|
|
148
|
+
case 'constant':
|
|
149
|
+
return { out: value ?? 0 };
|
|
150
|
+
case 'double':
|
|
151
|
+
return { out: (inputs.in ?? 0) * 2 };
|
|
152
|
+
case 'multiply':
|
|
153
|
+
return { out: (inputs.in ?? 0) * (value ?? 1) };
|
|
154
|
+
case 'add':
|
|
155
|
+
return { out: (inputs.a ?? 0) + (inputs.b ?? 0) };
|
|
156
|
+
case 'negate':
|
|
157
|
+
return { out: -(inputs.in ?? 0) };
|
|
158
|
+
case 'output':
|
|
159
|
+
return { in: inputs.in ?? 0 };
|
|
160
|
+
default:
|
|
161
|
+
throw new Error(`Unknown op: ${(node.data as { op: string }).op}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function runFlowBasedMathGraph(
|
|
166
|
+
graph: MathFlowGraph,
|
|
167
|
+
inputs: Record<string, number> | number,
|
|
168
|
+
): MathRunResult {
|
|
169
|
+
const sorted = getTopologicalSort(graph as any);
|
|
170
|
+
if (!sorted) {
|
|
171
|
+
throw new Error('Cycle detected - flow graph must be acyclic');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const inputMap =
|
|
175
|
+
typeof inputs === 'number'
|
|
176
|
+
? { [graph.initialNodeId ?? sorted[0].id]: inputs }
|
|
177
|
+
: inputs;
|
|
178
|
+
|
|
179
|
+
const portValues = new Map<string, Record<string, number>>();
|
|
180
|
+
|
|
181
|
+
for (const node of sorted as FlowNode<MathNodeData>[]) {
|
|
182
|
+
const nodeInputs: Record<string, number> = {};
|
|
183
|
+
|
|
184
|
+
if (node.data.op === 'input') {
|
|
185
|
+
nodeInputs.out = inputMap[node.id] ?? node.data.value ?? 0;
|
|
186
|
+
} else {
|
|
187
|
+
for (const edge of graph.edges) {
|
|
188
|
+
if (edge.targetId !== node.id || !edge.targetPort || !edge.sourcePort) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const sourceValues = portValues.get(edge.sourceId);
|
|
193
|
+
if (sourceValues && edge.sourcePort in sourceValues) {
|
|
194
|
+
nodeInputs[edge.targetPort] = sourceValues[edge.sourcePort];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
portValues.set(node.id, evaluateMathNode(node, nodeInputs));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const results: Record<string, number> = {};
|
|
203
|
+
for (const node of graph.nodes) {
|
|
204
|
+
if (node.data.op === 'output') {
|
|
205
|
+
results[node.id] = portValues.get(node.id)?.in ?? 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return { results, portValues };
|
|
210
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createFlow } from './createFlow';
|
|
3
|
+
import { createEdge, createGraph, createNode } from './factories';
|
|
4
|
+
import { createFlowExternalEdit } from './external-edit';
|
|
5
|
+
|
|
6
|
+
function fixture() {
|
|
7
|
+
return createFlow({
|
|
8
|
+
graph: createGraph({
|
|
9
|
+
nodes: ['a', 'b', 'c', 'd'].map((id) =>
|
|
10
|
+
createNode({
|
|
11
|
+
id,
|
|
12
|
+
x: 10.25,
|
|
13
|
+
y: 20.75,
|
|
14
|
+
resizable: true,
|
|
15
|
+
width: 100,
|
|
16
|
+
height: 60,
|
|
17
|
+
data: { count: 0, name: id },
|
|
18
|
+
})
|
|
19
|
+
),
|
|
20
|
+
edges: [
|
|
21
|
+
createEdge({
|
|
22
|
+
id: 'ab',
|
|
23
|
+
sourceId: 'a',
|
|
24
|
+
targetId: 'b',
|
|
25
|
+
data: { count: 0 },
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('external edit sessions', () => {
|
|
33
|
+
it('merges only changed raw geometry fields and applies data updaters to latest data', () => {
|
|
34
|
+
const flow = fixture();
|
|
35
|
+
const edit = createFlowExternalEdit(flow);
|
|
36
|
+
flow.nodes.update('a', { y: 90.125, height: 80 });
|
|
37
|
+
flow.nodes.setData('a', { count: 10, name: 'remote' });
|
|
38
|
+
const commits = vi.fn();
|
|
39
|
+
flow.on('commit', commits);
|
|
40
|
+
const result = edit.commit([
|
|
41
|
+
{
|
|
42
|
+
kind: 'node.resize',
|
|
43
|
+
id: 'a',
|
|
44
|
+
bounds: { x: 30.2, y: 20.75, width: 120, height: 60 },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
kind: 'node.data',
|
|
48
|
+
id: 'a',
|
|
49
|
+
update: (data) => ({ ...data, count: data.count + 1 }),
|
|
50
|
+
},
|
|
51
|
+
]);
|
|
52
|
+
expect(result.ok).toBe(true);
|
|
53
|
+
expect(flow.getBounds('a')).toMatchObject({
|
|
54
|
+
x: 30,
|
|
55
|
+
y: 90.125,
|
|
56
|
+
width: 120,
|
|
57
|
+
height: 80,
|
|
58
|
+
});
|
|
59
|
+
expect(flow.getNode('a')?.data).toEqual({ count: 11, name: 'remote' });
|
|
60
|
+
expect(commits).toHaveBeenCalledTimes(1);
|
|
61
|
+
flow.destroy();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it.each([false, true])(
|
|
65
|
+
'returns local geometry and endpoints to baseline while retaining remote edits (%s)',
|
|
66
|
+
(remote) => {
|
|
67
|
+
const flow = fixture();
|
|
68
|
+
const edit = flow.beginExternalEdit();
|
|
69
|
+
if (remote) {
|
|
70
|
+
flow.nodes.update('a', { x: 90.125, width: 170 });
|
|
71
|
+
flow.edges.reconnect('ab', { newTargetId: 'c' });
|
|
72
|
+
}
|
|
73
|
+
expect(
|
|
74
|
+
edit.commit([
|
|
75
|
+
{ kind: 'node.resize', id: 'a', bounds: { x: 50, width: 200 } },
|
|
76
|
+
{ kind: 'node.resize', id: 'a', bounds: { x: 10.25, width: 100 } },
|
|
77
|
+
{ kind: 'edge.reconnect', id: 'ab', endpoints: { targetId: 'd' } },
|
|
78
|
+
{ kind: 'edge.reconnect', id: 'ab', endpoints: { targetId: 'b' } },
|
|
79
|
+
]).ok
|
|
80
|
+
).toBe(true);
|
|
81
|
+
expect(flow.getBounds('a')).toMatchObject({
|
|
82
|
+
x: remote ? 90.125 : 10.25,
|
|
83
|
+
width: remote ? 170 : 100,
|
|
84
|
+
});
|
|
85
|
+
expect(flow.getEdge('ab')?.targetId).toBe(remote ? 'c' : 'b');
|
|
86
|
+
flow.destroy();
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
it('does not author a concurrent observation when geometry returns to baseline', () => {
|
|
91
|
+
const flow = fixture();
|
|
92
|
+
flow.nodes.update('a', { autoSize: true });
|
|
93
|
+
const edit = flow.beginExternalEdit();
|
|
94
|
+
flow.measurements.update([
|
|
95
|
+
{ id: 'a', rect: { x: 10.25, y: 20.75, width: 150, height: 60 } },
|
|
96
|
+
]);
|
|
97
|
+
const before = flow.toJSON();
|
|
98
|
+
expect(
|
|
99
|
+
edit.commit([
|
|
100
|
+
{ kind: 'node.resize', id: 'a', bounds: { width: 200 } },
|
|
101
|
+
{ kind: 'node.resize', id: 'a', bounds: { width: 100 } },
|
|
102
|
+
])
|
|
103
|
+
).toEqual({ ok: true, value: null });
|
|
104
|
+
expect(flow.toJSON()).toEqual(before);
|
|
105
|
+
flow.destroy();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('records default commits and allows native history ownership', () => {
|
|
109
|
+
const flow = fixture();
|
|
110
|
+
expect(
|
|
111
|
+
createFlowExternalEdit(flow, { history: 'ignore' }).commit([
|
|
112
|
+
{ kind: 'node.move', id: 'a', position: { x: 40 } },
|
|
113
|
+
]).ok
|
|
114
|
+
).toBe(true);
|
|
115
|
+
expect(flow.history.canUndo()).toBe(false);
|
|
116
|
+
expect(
|
|
117
|
+
createFlowExternalEdit(flow).commit([
|
|
118
|
+
{ kind: 'node.move', id: 'a', position: { x: 60 } },
|
|
119
|
+
]).ok
|
|
120
|
+
).toBe(true);
|
|
121
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
122
|
+
flow.destroy();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('preserves an untouched fractional axis in a partial move', () => {
|
|
126
|
+
const flow = fixture();
|
|
127
|
+
expect(
|
|
128
|
+
flow.beginExternalEdit().commit([
|
|
129
|
+
{ kind: 'node.move', id: 'a', position: { x: 40 } },
|
|
130
|
+
]).ok,
|
|
131
|
+
).toBe(true);
|
|
132
|
+
expect(flow.getNode('a')).toMatchObject({ x: 40, y: 20.75 });
|
|
133
|
+
flow.destroy();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('commits text size and supplied position while preserving untouched coordinates', () => {
|
|
137
|
+
const flow = fixture();
|
|
138
|
+
const edit = createFlowExternalEdit(flow);
|
|
139
|
+
flow.nodes.update('a', { y: 88.125 });
|
|
140
|
+
expect(
|
|
141
|
+
edit.commit([
|
|
142
|
+
{
|
|
143
|
+
kind: 'node.contentSize',
|
|
144
|
+
id: 'a',
|
|
145
|
+
size: { width: 130.5 },
|
|
146
|
+
position: { x: 8.5, y: 20.75 },
|
|
147
|
+
},
|
|
148
|
+
]).ok
|
|
149
|
+
).toBe(true);
|
|
150
|
+
expect(flow.getBounds('a')).toMatchObject({
|
|
151
|
+
x: 9,
|
|
152
|
+
y: 88.125,
|
|
153
|
+
width: 130.5,
|
|
154
|
+
height: 60,
|
|
155
|
+
});
|
|
156
|
+
flow.destroy();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('merges changed endpoints while preserving the concurrent other endpoint', () => {
|
|
160
|
+
const flow = fixture();
|
|
161
|
+
const edit = createFlowExternalEdit(flow);
|
|
162
|
+
flow.edges.reconnect('ab', { newTargetId: 'c' });
|
|
163
|
+
expect(
|
|
164
|
+
edit.commit([
|
|
165
|
+
{
|
|
166
|
+
kind: 'edge.reconnect',
|
|
167
|
+
id: 'ab',
|
|
168
|
+
endpoints: { sourceId: 'd', targetId: 'b' },
|
|
169
|
+
},
|
|
170
|
+
]).ok
|
|
171
|
+
).toBe(true);
|
|
172
|
+
expect(flow.getEdge('ab')).toMatchObject({ sourceId: 'd', targetId: 'c' });
|
|
173
|
+
flow.destroy();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('skips removed nodes and same-id replacements', () => {
|
|
177
|
+
const flow = fixture();
|
|
178
|
+
const edit = createFlowExternalEdit(flow);
|
|
179
|
+
flow.nodes.delete('a');
|
|
180
|
+
flow.nodes.delete('b');
|
|
181
|
+
flow.nodes.create({ id: 'a', x: 99, data: { count: 5, name: 'new' } });
|
|
182
|
+
expect(
|
|
183
|
+
edit.commit([
|
|
184
|
+
{ kind: 'node.move', id: 'a', position: { x: 50 } },
|
|
185
|
+
{ kind: 'node.move', id: 'b', position: { x: 50 } },
|
|
186
|
+
])
|
|
187
|
+
).toEqual({ ok: true, value: null });
|
|
188
|
+
expect(flow.getNode('a')?.x).toBe(99);
|
|
189
|
+
expect(flow.getNode('b')).toBeUndefined();
|
|
190
|
+
flow.destroy();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('creates explicitly and merges successive commands against staged results', () => {
|
|
194
|
+
const flow = fixture();
|
|
195
|
+
const edit = createFlowExternalEdit(flow);
|
|
196
|
+
const result = edit.commit([
|
|
197
|
+
{
|
|
198
|
+
kind: 'node.create',
|
|
199
|
+
node: { id: 'new', x: 0, y: 0, data: { count: 1, name: 'new' } },
|
|
200
|
+
},
|
|
201
|
+
{ kind: 'node.move', id: 'new', position: { x: 50 } },
|
|
202
|
+
{ kind: 'node.move', id: 'new', position: { y: 70 } },
|
|
203
|
+
{
|
|
204
|
+
kind: 'edge.create',
|
|
205
|
+
edge: {
|
|
206
|
+
id: 'new-edge',
|
|
207
|
+
sourceId: 'a',
|
|
208
|
+
targetId: 'new',
|
|
209
|
+
data: { count: 1 },
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
kind: 'edge.data',
|
|
214
|
+
id: 'new-edge',
|
|
215
|
+
update: (data) => ({ count: data.count + 1 }),
|
|
216
|
+
},
|
|
217
|
+
]);
|
|
218
|
+
expect(result.ok).toBe(true);
|
|
219
|
+
expect(flow.getBounds('new')).toMatchObject({ x: 50, y: 70 });
|
|
220
|
+
expect(flow.getEdge('new-edge')?.data).toEqual({ count: 2 });
|
|
221
|
+
flow.destroy();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('edits recreated identities independently of the original baseline', () => {
|
|
225
|
+
const flow = fixture();
|
|
226
|
+
const edit = flow.beginExternalEdit();
|
|
227
|
+
expect(
|
|
228
|
+
edit.commit([
|
|
229
|
+
{ kind: 'node.delete', id: 'a' },
|
|
230
|
+
{
|
|
231
|
+
kind: 'node.create',
|
|
232
|
+
node: { id: 'a', x: 50, data: { count: 1, name: 'new' } },
|
|
233
|
+
},
|
|
234
|
+
{ kind: 'node.move', id: 'a', position: { x: 10.25 } },
|
|
235
|
+
{
|
|
236
|
+
kind: 'edge.create',
|
|
237
|
+
edge: { id: 'ab', sourceId: 'a', targetId: 'c', data: { count: 0 } },
|
|
238
|
+
},
|
|
239
|
+
{ kind: 'edge.reconnect', id: 'ab', endpoints: { targetId: 'b' } },
|
|
240
|
+
]).ok
|
|
241
|
+
).toBe(true);
|
|
242
|
+
expect(flow.getNode('a')?.x).toBe(10);
|
|
243
|
+
expect(flow.getEdge('ab')?.targetId).toBe('b');
|
|
244
|
+
flow.destroy();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('reads staged deletion effects before later edge edits', () => {
|
|
248
|
+
const flow = fixture();
|
|
249
|
+
const edit = createFlowExternalEdit(flow);
|
|
250
|
+
expect(
|
|
251
|
+
edit.commit([
|
|
252
|
+
{ kind: 'node.delete', id: 'a' },
|
|
253
|
+
{ kind: 'edge.data', id: 'ab', update: () => ({ count: 20 }) },
|
|
254
|
+
]).ok
|
|
255
|
+
).toBe(true);
|
|
256
|
+
expect(flow.getEdge('ab')).toBeUndefined();
|
|
257
|
+
flow.destroy();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('does not edit a same-id edge replacement', () => {
|
|
261
|
+
const flow = fixture();
|
|
262
|
+
const edit = createFlowExternalEdit(flow);
|
|
263
|
+
flow.edges.delete('ab');
|
|
264
|
+
flow.edges.create({
|
|
265
|
+
id: 'ab',
|
|
266
|
+
sourceId: 'c',
|
|
267
|
+
targetId: 'd',
|
|
268
|
+
data: { count: 10 },
|
|
269
|
+
});
|
|
270
|
+
expect(
|
|
271
|
+
edit.commit([
|
|
272
|
+
{ kind: 'edge.data', id: 'ab', update: () => ({ count: 99 }) },
|
|
273
|
+
])
|
|
274
|
+
).toEqual({ ok: true, value: null });
|
|
275
|
+
expect(flow.getEdge('ab')?.data).toEqual({ count: 10 });
|
|
276
|
+
flow.destroy();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('rejects reentrant session commits from commit listeners', () => {
|
|
280
|
+
const flow = fixture();
|
|
281
|
+
const edit = createFlowExternalEdit(flow);
|
|
282
|
+
let reentrantOk: boolean | undefined;
|
|
283
|
+
flow.on('commit', () => {
|
|
284
|
+
reentrantOk = edit.commit([]).ok;
|
|
285
|
+
});
|
|
286
|
+
expect(
|
|
287
|
+
edit.commit([{ kind: 'node.move', id: 'a', position: { x: 40 } }]).ok
|
|
288
|
+
).toBe(true);
|
|
289
|
+
expect(reentrantOk).toBe(false);
|
|
290
|
+
flow.destroy();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('rolls the entire batch back on collision and closes failed sessions', () => {
|
|
294
|
+
const flow = fixture();
|
|
295
|
+
const edit = createFlowExternalEdit(flow);
|
|
296
|
+
const commits = vi.fn();
|
|
297
|
+
flow.on('commit', commits);
|
|
298
|
+
expect(
|
|
299
|
+
edit.commit([
|
|
300
|
+
{ kind: 'node.move', id: 'a', position: { x: 50 } },
|
|
301
|
+
{
|
|
302
|
+
kind: 'node.create',
|
|
303
|
+
node: { id: 'b', data: { count: 1, name: 'collision' } },
|
|
304
|
+
},
|
|
305
|
+
]).ok
|
|
306
|
+
).toBe(false);
|
|
307
|
+
expect(flow.getNode('a')?.x).toBe(10.25);
|
|
308
|
+
expect(commits).not.toHaveBeenCalled();
|
|
309
|
+
expect(edit.commit([]).ok).toBe(false);
|
|
310
|
+
flow.destroy();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('cancel and controller destruction close sessions without content changes', () => {
|
|
314
|
+
const flow = fixture();
|
|
315
|
+
const cancelled = createFlowExternalEdit(flow);
|
|
316
|
+
cancelled.cancel();
|
|
317
|
+
cancelled.dispose();
|
|
318
|
+
expect(cancelled.commit([]).ok).toBe(false);
|
|
319
|
+
const pending = createFlowExternalEdit(flow);
|
|
320
|
+
flow.destroy();
|
|
321
|
+
expect(pending.commit([]).ok).toBe(false);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('uses core permissions and rolls back preceding writes', () => {
|
|
325
|
+
const flow = fixture();
|
|
326
|
+
flow.nodes.update('b', { resizable: false });
|
|
327
|
+
const edit = createFlowExternalEdit(flow);
|
|
328
|
+
expect(
|
|
329
|
+
edit.commit([
|
|
330
|
+
{ kind: 'node.move', id: 'a', position: { x: 50 } },
|
|
331
|
+
{ kind: 'node.resize', id: 'b', bounds: { width: 200 } },
|
|
332
|
+
]).ok
|
|
333
|
+
).toBe(false);
|
|
334
|
+
expect(flow.getNode('a')?.x).toBe(10.25);
|
|
335
|
+
flow.destroy();
|
|
336
|
+
});
|
|
337
|
+
});
|