@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,2577 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { createFlowStore } from '../store';
3
+ import { createFlowEngine } from '../engine';
4
+ import { createFlow } from '../createFlow';
5
+ import { createNode, createEdge, createGraph, createPort } from '../factories';
6
+ import { createConnectionPreviewGraph } from '../connectionPreview';
7
+ import {
8
+ createFlowInputRuntime,
9
+ connectMode,
10
+ defaultInputModes,
11
+ entityPressMode,
12
+ keyboardMode,
13
+ keyboardNavMode,
14
+ reconnectMode,
15
+ type FlowInputEvent,
16
+ type FlowInputModifiers,
17
+ type FlowInputTarget,
18
+ type FlowKeymap,
19
+ } from './index';
20
+ import type { Point } from '../types';
21
+ import { connectionLifetime } from '../connection-lifetime';
22
+ import type { ConnectionEndedEvent } from '../store';
23
+
24
+ // DOM-free gesture tests. Every interaction that used to live as React
25
+ // handlers in StatelyFlow.tsx is exercised here against the store directly —
26
+ // this is the payoff of the input runtime: gestures are unit-testable.
27
+
28
+ const NO_MODS: FlowInputModifiers = {
29
+ shift: false,
30
+ meta: false,
31
+ ctrl: false,
32
+ alt: false,
33
+ };
34
+
35
+ function makeRig(opts: {
36
+ storeOptions?: Record<string, unknown>;
37
+ engineOptions?: Parameters<typeof createFlowEngine>[1];
38
+ keymap?: Partial<FlowKeymap>;
39
+ modes?: Parameters<typeof createFlowInputRuntime>[0]['modes'];
40
+ policies?: Parameters<typeof createFlowInputRuntime>[0]['policies'];
41
+ } = {}) {
42
+ const store = createFlowStore(
43
+ createGraph({
44
+ nodes: [
45
+ createNode({
46
+ id: 'a',
47
+ x: 0,
48
+ y: 0,
49
+ width: 100,
50
+ height: 50,
51
+ resizable: true,
52
+ ports: [
53
+ createPort({ name: 'out', side: 'bottom', direction: 'out', x: 50, y: 50 }),
54
+ ],
55
+ }),
56
+ createNode({
57
+ id: 'b',
58
+ x: 300,
59
+ y: 0,
60
+ width: 100,
61
+ height: 50,
62
+ ports: [
63
+ createPort({ name: 'in', side: 'top', direction: 'in', x: 50, y: 0 }),
64
+ ],
65
+ }),
66
+ createNode({
67
+ id: 'pinned',
68
+ x: 0,
69
+ y: 200,
70
+ width: 100,
71
+ height: 50,
72
+ draggable: false,
73
+ }),
74
+ ],
75
+ edges: [
76
+ createEdge({
77
+ id: 'e1',
78
+ sourceId: 'a',
79
+ targetId: 'b',
80
+ label: { x: 150, y: 25, width: 60, height: 20 },
81
+ }),
82
+ ],
83
+ }),
84
+ { dragThreshold: 3, ...opts.storeOptions },
85
+ );
86
+ const engine = createFlowEngine(store, opts.engineOptions);
87
+ const runtime = createFlowInputRuntime({
88
+ store,
89
+ engine,
90
+ keymap: opts.keymap,
91
+ modes: opts.modes,
92
+ policies: opts.policies,
93
+ });
94
+
95
+ const ctx = () => store.getSnapshot().context;
96
+ // zoom=1, viewport origin 0 → canvas point === screen point in these tests
97
+ const pointer = (
98
+ kind: 'pointer.down' | 'pointer.move' | 'pointer.up' | 'pointer.cancel',
99
+ point: Point,
100
+ extra: {
101
+ target?: FlowInputTarget;
102
+ screenPoint?: Point;
103
+ button?: number;
104
+ detail?: number;
105
+ pointerId?: number;
106
+ modifiers?: Partial<FlowInputModifiers>;
107
+ pointerType?: 'mouse' | 'pen' | 'touch';
108
+ } = {},
109
+ ): FlowInputEvent => ({
110
+ kind,
111
+ pointerId: extra.pointerId ?? 1,
112
+ pointerType: extra.pointerType ?? 'mouse',
113
+ button: extra.button ?? (kind === 'pointer.down' ? 0 : -1),
114
+ detail: extra.detail,
115
+ point,
116
+ screenPoint: extra.screenPoint ?? point,
117
+ target: extra.target ?? { kind: 'pane' },
118
+ modifiers: { ...NO_MODS, ...extra.modifiers },
119
+ });
120
+ const key = (
121
+ kind: 'key.down' | 'key.up',
122
+ k: string,
123
+ extra: {
124
+ editableTarget?: boolean;
125
+ modifiers?: Partial<FlowInputModifiers>;
126
+ } = {},
127
+ ): FlowInputEvent => ({
128
+ kind,
129
+ key: k,
130
+ editableTarget: extra.editableTarget ?? false,
131
+ modifiers: { ...NO_MODS, ...extra.modifiers },
132
+ });
133
+
134
+ return { store, engine, runtime, ctx, pointer, key };
135
+ }
136
+
137
+ const node = (id: string): FlowInputTarget => ({ kind: 'entity', id });
138
+ const label = (id: string): FlowInputTarget => ({
139
+ kind: 'entity',
140
+ id,
141
+ part: 'label',
142
+ });
143
+ const resizeHandle = (id: string, handle = 'bottom-right'): FlowInputTarget => ({
144
+ kind: 'entity',
145
+ id,
146
+ part: `resize-handle:${handle}`,
147
+ });
148
+ const edgeEndpoint = (
149
+ id: string,
150
+ end: 'source' | 'target',
151
+ ): FlowInputTarget => ({
152
+ kind: 'entity',
153
+ id,
154
+ part: `edge-endpoint:${end}`,
155
+ });
156
+
157
+ describe('input runtime — pane gestures', () => {
158
+ it('double-click zooms in and Shift double-click zooms out', () => {
159
+ const { runtime, ctx, pointer } = makeRig();
160
+
161
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { detail: 2 }));
162
+ expect(ctx().viewport.zoom).toBe(2);
163
+
164
+ runtime.send(
165
+ pointer('pointer.down', { x: 10, y: 10 }, {
166
+ detail: 2,
167
+ modifiers: { shift: true },
168
+ }),
169
+ );
170
+ expect(ctx().viewport.zoom).toBe(1);
171
+ });
172
+
173
+ it('a real double-click sequence (down/up/down/up) zooms in centered on the pointer', () => {
174
+ const { runtime, ctx, pointer } = makeRig();
175
+
176
+ // First click of the double-click.
177
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { detail: 1 }));
178
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { detail: 1 }));
179
+ // Second click — detail:2 marks it as the double-click.
180
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { detail: 2 }));
181
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { detail: 2 }));
182
+
183
+ expect(ctx().viewport.zoom).toBe(2);
184
+ // Zoom is centered on the pointer: the point under (10,10) stays put.
185
+ expect(ctx().viewport.x).toBe(-10);
186
+ expect(ctx().viewport.y).toBe(-10);
187
+ });
188
+
189
+ it('Shift + real double-click sequence zooms out', () => {
190
+ const { runtime, ctx, pointer } = makeRig();
191
+
192
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { detail: 1 }));
193
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { detail: 1 }));
194
+ runtime.send(
195
+ pointer('pointer.down', { x: 10, y: 10 }, {
196
+ detail: 2,
197
+ modifiers: { shift: true },
198
+ }),
199
+ );
200
+ runtime.send(
201
+ pointer('pointer.up', { x: 10, y: 10 }, {
202
+ detail: 2,
203
+ modifiers: { shift: true },
204
+ }),
205
+ );
206
+
207
+ expect(ctx().viewport.zoom).toBe(0.5);
208
+ });
209
+
210
+ it('two separate single clicks (both detail:1) do NOT zoom', () => {
211
+ const { runtime, ctx, pointer } = makeRig();
212
+
213
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { detail: 1 }));
214
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { detail: 1 }));
215
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { detail: 1 }));
216
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { detail: 1 }));
217
+
218
+ expect(ctx().viewport.zoom).toBe(1);
219
+ });
220
+
221
+ it('double-clicking an entity emits the entity double-click event without zooming (pane-only zoom)', () => {
222
+ const { store, runtime, ctx, pointer } = makeRig();
223
+ const events: unknown[] = [];
224
+ store.on('entityDoubleClicked', (event) => events.push(event));
225
+
226
+ // The double-click arrives as a down/up pair; entityPressMode owns the
227
+ // click session and emits entityDoubleClicked on detail === 2.
228
+ runtime.send(
229
+ pointer('pointer.down', { x: 10, y: 10 }, {
230
+ detail: 2,
231
+ target: node('a'),
232
+ }),
233
+ );
234
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { target: node('a') }));
235
+
236
+ // Entity double-click must NOT change zoom (react-flow parity).
237
+ expect(ctx().viewport.zoom).toBe(1);
238
+ // Emitted exactly once (no double-emission from a separate zoom mode).
239
+ expect(events).toHaveLength(1);
240
+ });
241
+
242
+ it('pane drag past the threshold pans and clears selection', () => {
243
+ const { runtime, ctx, pointer } = makeRig();
244
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }));
245
+ runtime.send(pointer('pointer.move', { x: 60, y: 30 }));
246
+
247
+ expect(ctx().canvasState).toBe('panning');
248
+ runtime.send(pointer('pointer.move', { x: 80, y: 40 }));
249
+ expect(ctx().viewport.x).toBe(70);
250
+ expect(ctx().viewport.y).toBe(30);
251
+
252
+ runtime.send(pointer('pointer.up', { x: 80, y: 40 }));
253
+ expect(ctx().canvasState).toBe('idle');
254
+ });
255
+
256
+ it('pane click (below threshold) clears selection and focus without panning', () => {
257
+ const { store, runtime, ctx, pointer } = makeRig();
258
+ store.trigger.select({ ids: ['a'] });
259
+ store.trigger.focus({ entityId: 'a' });
260
+
261
+ runtime.send(pointer('pointer.down', { x: 500, y: 10 }));
262
+ runtime.send(pointer('pointer.move', { x: 501, y: 10 }));
263
+ runtime.send(pointer('pointer.up', { x: 501, y: 10 }));
264
+
265
+ expect(ctx().selection.size).toBe(0);
266
+ expect(ctx().focusedEntityId).toBeNull();
267
+ expect(ctx().viewport.x).toBe(0);
268
+ expect(ctx().canvasState).toBe('idle');
269
+ });
270
+
271
+ it('pane drag past the threshold also clears focus', () => {
272
+ const { store, runtime, ctx, pointer } = makeRig();
273
+ store.trigger.select({ ids: ['a'] });
274
+ store.trigger.focus({ entityId: 'a' });
275
+
276
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }));
277
+ runtime.send(pointer('pointer.move', { x: 60, y: 30 }));
278
+
279
+ expect(ctx().focusedEntityId).toBeNull();
280
+ });
281
+
282
+ it('middle-button drag pans from any target', () => {
283
+ const { runtime, ctx, pointer } = makeRig();
284
+ runtime.send(
285
+ pointer('pointer.down', { x: 10, y: 10 }, { target: node('a'), button: 1 }),
286
+ );
287
+ runtime.send(pointer('pointer.move', { x: 40, y: 10 }));
288
+ expect(ctx().viewport.x).toBe(30);
289
+ runtime.send(pointer('pointer.up', { x: 40, y: 10 }));
290
+ expect(ctx().canvasState).toBe('idle');
291
+ });
292
+
293
+ it('space-held drag pans even when pressing a node', () => {
294
+ const { runtime, ctx, key, pointer } = makeRig();
295
+ runtime.send(key('key.down', ' '));
296
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
297
+ runtime.send(pointer('pointer.move', { x: 50, y: 10 }));
298
+
299
+ expect(ctx().viewport.x).toBe(40);
300
+ expect(ctx().graph.nodes[0].dx ?? 0).toBe(0);
301
+
302
+ runtime.send(pointer('pointer.up', { x: 50, y: 10 }));
303
+ runtime.send(key('key.up', ' '));
304
+ // space released: same gesture now drags the node again
305
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
306
+ runtime.send(pointer('pointer.move', { x: 50, y: 10 }));
307
+ expect(ctx().draggedEntityId).toBe('a');
308
+ runtime.send(pointer('pointer.up', { x: 50, y: 10 }));
309
+ });
310
+
311
+ it('can rebind the pan activation key', () => {
312
+ const { runtime, ctx, key, pointer } = makeRig({
313
+ keymap: { panActivation: 'p' },
314
+ });
315
+ runtime.send(key('key.down', 'p'));
316
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
317
+ runtime.send(pointer('pointer.move', { x: 50, y: 10 }));
318
+
319
+ expect(ctx().viewport.x).toBe(40);
320
+ expect(ctx().graph.nodes[0].dx ?? 0).toBe(0);
321
+ });
322
+
323
+ it('touch pane drag pans instead of box-selecting even with a multi modifier', () => {
324
+ const { runtime, ctx, pointer } = makeRig();
325
+
326
+ runtime.send(
327
+ pointer('pointer.down', { x: 10, y: 10 }, {
328
+ pointerType: 'touch',
329
+ modifiers: { shift: true },
330
+ }),
331
+ );
332
+ runtime.send(
333
+ pointer('pointer.move', { x: 60, y: 10 }, {
334
+ pointerType: 'touch',
335
+ modifiers: { shift: true },
336
+ }),
337
+ );
338
+
339
+ expect(ctx().canvasState).toBe('panning');
340
+ expect(ctx().selectionBox).toBeNull();
341
+ expect(ctx().viewport.x).toBe(50);
342
+ });
343
+
344
+ it('pane click emits paneClicked, while pane drag does not', () => {
345
+ const { store, runtime, pointer } = makeRig();
346
+ const clicks: unknown[] = [];
347
+ store.on('paneClicked', (event) => clicks.push(event));
348
+
349
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }));
350
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }));
351
+
352
+ expect(clicks).toEqual([
353
+ { type: 'paneClicked', point: { x: 10, y: 10 }, modifiers: NO_MODS },
354
+ ]);
355
+
356
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }));
357
+ runtime.send(pointer('pointer.move', { x: 20, y: 10 }));
358
+ runtime.send(pointer('pointer.up', { x: 20, y: 10 }));
359
+
360
+ expect(clicks).toHaveLength(1);
361
+ });
362
+
363
+ it('pane context menu emits without preventing default', () => {
364
+ const { store, runtime, pointer } = makeRig();
365
+ const events: unknown[] = [];
366
+ store.on('paneContextMenu', (event) => events.push(event));
367
+
368
+ const effects = runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { button: 2 }));
369
+
370
+ expect(events).toEqual([
371
+ { type: 'paneContextMenu', point: { x: 10, y: 10 }, modifiers: NO_MODS },
372
+ ]);
373
+ expect(effects.preventDefault).toBe(false);
374
+ });
375
+ });
376
+
377
+ describe('input runtime — entity gesture events', () => {
378
+ it('entity click emits entityClicked, while drag emits dragStarted and not entityClicked', () => {
379
+ const { store, runtime, pointer } = makeRig();
380
+ const clicks: unknown[] = [];
381
+ const drags: unknown[] = [];
382
+ store.on('entityClicked', (event) => clicks.push(event));
383
+ store.on('dragStarted', (event) => drags.push(event));
384
+
385
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
386
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { target: node('a') }));
387
+
388
+ expect(clicks).toEqual([
389
+ { type: 'entityClicked', id: 'a', point: { x: 10, y: 10 }, modifiers: NO_MODS },
390
+ ]);
391
+ expect(drags).toHaveLength(0);
392
+
393
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
394
+ runtime.send(pointer('pointer.move', { x: 20, y: 10 }));
395
+ runtime.send(pointer('pointer.up', { x: 20, y: 10 }));
396
+
397
+ expect(clicks).toHaveLength(1);
398
+ expect(drags).toHaveLength(1);
399
+ });
400
+
401
+ it('entity double-click and context menu emit dedicated events', () => {
402
+ const { store, runtime, pointer } = makeRig();
403
+ const doubles: unknown[] = [];
404
+ const menus: unknown[] = [];
405
+ store.on('entityDoubleClicked', (event) => doubles.push(event));
406
+ store.on('entityContextMenu', (event) => menus.push(event));
407
+
408
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a'), detail: 2 }));
409
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }, { target: node('a') }));
410
+ const effects = runtime.send(
411
+ pointer('pointer.down', { x: 10, y: 10 }, { target: node('a'), button: 2 }),
412
+ );
413
+
414
+ expect(doubles).toEqual([
415
+ { type: 'entityDoubleClicked', id: 'a', point: { x: 10, y: 10 }, modifiers: NO_MODS },
416
+ ]);
417
+ expect(menus).toEqual([
418
+ { type: 'entityContextMenu', id: 'a', point: { x: 10, y: 10 }, modifiers: NO_MODS },
419
+ ]);
420
+ expect(effects.preventDefault).toBe(false);
421
+ });
422
+ });
423
+
424
+ describe('input runtime — entity press and drag', () => {
425
+ it('pressing a node selects it; shift adds to the selection', () => {
426
+ const { runtime, ctx, pointer } = makeRig();
427
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
428
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }));
429
+ expect([...ctx().selection]).toEqual(['a']);
430
+
431
+ runtime.send(
432
+ pointer('pointer.down', { x: 310, y: 10 }, {
433
+ target: node('b'),
434
+ modifiers: { shift: true },
435
+ }),
436
+ );
437
+ runtime.send(pointer('pointer.up', { x: 310, y: 10 }));
438
+ expect([...ctx().selection].sort()).toEqual(['a', 'b']);
439
+ });
440
+
441
+ it('respects a custom multiSelectModifier', () => {
442
+ // multiSelectModifier: 'meta' — only meta adds to the selection; shift does not.
443
+ const meta = makeRig({ keymap: { multiSelectModifier: 'meta' } });
444
+ meta.runtime.send(meta.pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
445
+ meta.runtime.send(meta.pointer('pointer.up', { x: 10, y: 10 }));
446
+ expect([...meta.ctx().selection]).toEqual(['a']);
447
+
448
+ // shift+click does NOT multi-select → replaces the selection
449
+ meta.runtime.send(
450
+ meta.pointer('pointer.down', { x: 310, y: 10 }, {
451
+ target: node('b'),
452
+ modifiers: { shift: true },
453
+ }),
454
+ );
455
+ meta.runtime.send(meta.pointer('pointer.up', { x: 310, y: 10 }));
456
+ expect([...meta.ctx().selection]).toEqual(['b']);
457
+
458
+ // meta+click DOES multi-select → adds to the selection
459
+ meta.runtime.send(
460
+ meta.pointer('pointer.down', { x: 10, y: 10 }, {
461
+ target: node('a'),
462
+ modifiers: { meta: true },
463
+ }),
464
+ );
465
+ meta.runtime.send(meta.pointer('pointer.up', { x: 10, y: 10 }));
466
+ expect([...meta.ctx().selection].sort()).toEqual(['a', 'b']);
467
+
468
+ // multiSelectModifier: null disables multi-select entirely — even shift replaces.
469
+ const off = makeRig({ keymap: { multiSelectModifier: null } });
470
+ off.runtime.send(off.pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
471
+ off.runtime.send(off.pointer('pointer.up', { x: 10, y: 10 }));
472
+ off.runtime.send(
473
+ off.pointer('pointer.down', { x: 310, y: 10 }, {
474
+ target: node('b'),
475
+ modifiers: { shift: true },
476
+ }),
477
+ );
478
+ off.runtime.send(off.pointer('pointer.up', { x: 310, y: 10 }));
479
+ expect([...off.ctx().selection]).toEqual(['b']);
480
+ });
481
+
482
+ it('explicit multiSelectModifier "shift" means shift-only — meta-click does not multi-select', () => {
483
+ // Previously 'shift' was a sentinel for the legacy shift||meta||ctrl set;
484
+ // it now means exactly shift-only.
485
+ const rig = makeRig({ keymap: { multiSelectModifier: 'shift' } });
486
+ rig.runtime.send(rig.pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
487
+ rig.runtime.send(rig.pointer('pointer.up', { x: 10, y: 10 }));
488
+ expect([...rig.ctx().selection]).toEqual(['a']);
489
+
490
+ // meta+click does NOT multi-select → replaces the selection.
491
+ rig.runtime.send(
492
+ rig.pointer('pointer.down', { x: 310, y: 10 }, {
493
+ target: node('b'),
494
+ modifiers: { meta: true },
495
+ }),
496
+ );
497
+ rig.runtime.send(rig.pointer('pointer.up', { x: 310, y: 10 }));
498
+ expect([...rig.ctx().selection]).toEqual(['b']);
499
+
500
+ // shift+click DOES multi-select → adds to the selection.
501
+ rig.runtime.send(
502
+ rig.pointer('pointer.down', { x: 10, y: 10 }, {
503
+ target: node('a'),
504
+ modifiers: { shift: true },
505
+ }),
506
+ );
507
+ rig.runtime.send(rig.pointer('pointer.up', { x: 10, y: 10 }));
508
+ expect([...rig.ctx().selection].sort()).toEqual(['a', 'b']);
509
+ });
510
+
511
+ it('an array multiSelectModifier multi-selects on any listed modifier', () => {
512
+ // ctrl (listed) adds to the selection.
513
+ const rig = makeRig({ keymap: { multiSelectModifier: ['meta', 'ctrl'] } });
514
+ rig.runtime.send(rig.pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
515
+ rig.runtime.send(rig.pointer('pointer.up', { x: 10, y: 10 }));
516
+ rig.runtime.send(
517
+ rig.pointer('pointer.down', { x: 310, y: 10 }, {
518
+ target: node('b'),
519
+ modifiers: { ctrl: true },
520
+ }),
521
+ );
522
+ rig.runtime.send(rig.pointer('pointer.up', { x: 310, y: 10 }));
523
+ expect([...rig.ctx().selection].sort()).toEqual(['a', 'b']);
524
+
525
+ // shift (not listed) does NOT multi-select → single-selects the pressed node.
526
+ const other = makeRig({ keymap: { multiSelectModifier: ['meta', 'ctrl'] } });
527
+ other.runtime.send(other.pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
528
+ other.runtime.send(other.pointer('pointer.up', { x: 10, y: 10 }));
529
+ other.runtime.send(
530
+ other.pointer('pointer.down', { x: 310, y: 10 }, {
531
+ target: node('b'),
532
+ modifiers: { shift: true },
533
+ }),
534
+ );
535
+ other.runtime.send(other.pointer('pointer.up', { x: 310, y: 10 }));
536
+ expect([...other.ctx().selection]).toEqual(['b']);
537
+ });
538
+
539
+ it('pressing inside a multi-selection keeps it intact (group drag)', () => {
540
+ const { store, runtime, ctx, pointer } = makeRig();
541
+ store.trigger.select({ ids: ['a', 'b'], mode: 'multi' });
542
+
543
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
544
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
545
+
546
+ expect([...ctx().selection].sort()).toEqual(['a', 'b']);
547
+ expect(ctx().graph.nodes.find((n) => n.id === 'b')!.dx).toBe(20);
548
+ runtime.send(pointer('pointer.up', { x: 30, y: 10 }));
549
+ });
550
+
551
+ it('shift-clicking an entity in a multi-selection toggles it off', () => {
552
+ const { store, runtime, ctx, pointer } = makeRig();
553
+ store.trigger.select({ ids: ['a', 'b'], mode: 'multi' });
554
+
555
+ runtime.send(
556
+ pointer('pointer.down', { x: 10, y: 10 }, {
557
+ target: node('a'),
558
+ modifiers: { shift: true },
559
+ }),
560
+ );
561
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }));
562
+
563
+ expect([...ctx().selection]).toEqual(['b']);
564
+ });
565
+
566
+ it('command-clicking an entity in a multi-selection toggles it off', () => {
567
+ const { store, runtime, ctx, pointer } = makeRig();
568
+ store.trigger.select({ ids: ['a', 'b'], mode: 'multi' });
569
+
570
+ runtime.send(
571
+ pointer('pointer.down', { x: 10, y: 10 }, {
572
+ target: node('a'),
573
+ modifiers: { meta: true },
574
+ }),
575
+ );
576
+ runtime.send(pointer('pointer.up', { x: 10, y: 10 }));
577
+
578
+ expect([...ctx().selection]).toEqual(['b']);
579
+ });
580
+
581
+ it('drags a node past the threshold and commits on release', () => {
582
+ const { runtime, ctx, pointer } = makeRig();
583
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
584
+ // below threshold: no drag yet
585
+ runtime.send(pointer('pointer.move', { x: 11, y: 10 }));
586
+ expect(ctx().canvasState).toBe('idle');
587
+
588
+ runtime.send(pointer('pointer.move', { x: 30, y: 25 }));
589
+ expect(ctx().canvasState).toBe('dragging');
590
+ expect(ctx().draggedEntityId).toBe('a');
591
+
592
+ runtime.send(pointer('pointer.move', { x: 40, y: 30 }));
593
+ runtime.send(pointer('pointer.up', { x: 40, y: 30 }));
594
+
595
+ const a = ctx().graph.nodes.find((n) => n.id === 'a')!;
596
+ expect(ctx().canvasState).toBe('idle');
597
+ expect(a.x).toBe(30);
598
+ expect(a.y).toBe(20);
599
+ expect(a.dx).toBe(0);
600
+ });
601
+
602
+ it('a drag is one undo step (transaction)', () => {
603
+ const { store, runtime, ctx, pointer } = makeRig();
604
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
605
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
606
+ runtime.send(pointer('pointer.move', { x: 50, y: 10 }));
607
+ runtime.send(pointer('pointer.up', { x: 50, y: 10 }));
608
+ expect(ctx().graph.nodes[0].x).toBe(40);
609
+
610
+ store.trigger.undo();
611
+ expect(ctx().graph.nodes[0].x).toBe(0);
612
+ });
613
+
614
+ it('does not drag nodes with draggable: false (but still selects them)', () => {
615
+ const { runtime, ctx, pointer } = makeRig();
616
+ runtime.send(
617
+ pointer('pointer.down', { x: 10, y: 210 }, { target: node('pinned') }),
618
+ );
619
+ runtime.send(pointer('pointer.move', { x: 60, y: 210 }));
620
+
621
+ expect([...ctx().selection]).toEqual(['pinned']);
622
+ expect(ctx().canvasState).toBe('idle');
623
+ expect(ctx().graph.nodes.find((n) => n.id === 'pinned')!.dx ?? 0).toBe(0);
624
+ // and the pane below must not pan either — the press was consumed
625
+ expect(ctx().viewport.x).toBe(0);
626
+ });
627
+
628
+ it('supports gesture-time drag policy without losing entity clicks', () => {
629
+ const { store, runtime, pointer } = makeRig({
630
+ modes: [
631
+ entityPressMode({
632
+ drag: (event) => !event.modifiers.alt,
633
+ }),
634
+ ],
635
+ });
636
+ const events: unknown[] = [];
637
+ store.on('entityClicked', (event) => events.push(event));
638
+
639
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, {
640
+ target: node('a'),
641
+ modifiers: { alt: true },
642
+ }));
643
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }, {
644
+ modifiers: { alt: true },
645
+ }));
646
+ runtime.send(pointer('pointer.up', { x: 30, y: 10 }, {
647
+ modifiers: { alt: true },
648
+ }));
649
+
650
+ expect(store.getSnapshot().context.graph.nodes[0].x).toBe(0);
651
+ expect(events).toHaveLength(1);
652
+ });
653
+
654
+ it('pressing an edge selects it without starting a drag', () => {
655
+ const { runtime, ctx, pointer } = makeRig();
656
+ runtime.send(
657
+ pointer('pointer.down', { x: 150, y: 25 }, { target: node('e1') }),
658
+ );
659
+ runtime.send(pointer('pointer.move', { x: 180, y: 25 }));
660
+
661
+ expect([...ctx().selection]).toEqual(['e1']);
662
+ expect(ctx().canvasState).toBe('idle');
663
+ runtime.send(pointer('pointer.up', { x: 180, y: 25 }));
664
+ });
665
+
666
+ it('dragging an edge label moves and commits the label', () => {
667
+ const { runtime, ctx, pointer } = makeRig();
668
+ runtime.send(
669
+ pointer('pointer.down', { x: 150, y: 25 }, { target: label('e1') }),
670
+ );
671
+ runtime.send(pointer('pointer.move', { x: 170, y: 45 }));
672
+ runtime.send(pointer('pointer.up', { x: 170, y: 45 }));
673
+
674
+ const e1 = ctx().graph.edges[0];
675
+ expect(e1.label!.x).toBe(170);
676
+ expect(e1.label!.y).toBe(45);
677
+ expect(e1.label!.dx).toBe(0);
678
+ });
679
+
680
+ it('second pointer during a drag is swallowed (no competing gesture)', () => {
681
+ const { runtime, ctx, pointer } = makeRig();
682
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
683
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
684
+ expect(ctx().canvasState).toBe('dragging');
685
+
686
+ runtime.send(
687
+ pointer('pointer.down', { x: 500, y: 10 }, { pointerId: 2 }),
688
+ );
689
+ runtime.send(pointer('pointer.move', { x: 540, y: 10 }, { pointerId: 2 }));
690
+ expect(ctx().viewport.x).toBe(0); // no pan started
691
+
692
+ runtime.send(pointer('pointer.up', { x: 30, y: 10 }));
693
+ expect(ctx().canvasState).toBe('idle');
694
+ });
695
+
696
+ it('pointer.cancel ends a drag and commits (parity with previous adapter)', () => {
697
+ const { runtime, ctx, pointer } = makeRig();
698
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
699
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
700
+ runtime.send(pointer('pointer.cancel', { x: 30, y: 10 }));
701
+
702
+ expect(ctx().canvasState).toBe('idle');
703
+ expect(ctx().graph.nodes[0].x).toBe(20);
704
+ expect(runtime.getActiveSession()).toBeNull();
705
+ });
706
+ });
707
+
708
+ describe('input runtime — resize', () => {
709
+ it('ignores legacy resize handles', () => {
710
+ const { runtime, ctx, pointer } = makeRig();
711
+
712
+ runtime.send(
713
+ pointer('pointer.down', { x: 100, y: 50 }, { target: resizeHandle('a') }),
714
+ );
715
+ runtime.send(pointer('pointer.move', { x: 180, y: 90 }));
716
+ runtime.send(pointer('pointer.up', { x: 180, y: 90 }));
717
+
718
+ const a = ctx().graph.nodes.find((n) => n.id === 'a')!;
719
+ expect(ctx().canvasState).toBe('idle');
720
+ expect(a.style?.customWidth).toBeUndefined();
721
+ expect(a.style?.customHeight).toBeUndefined();
722
+ });
723
+ });
724
+
725
+ describe('input runtime — wheel', () => {
726
+ function wheel(
727
+ deltas: { deltaX?: number; deltaY: number; deltaMode?: number },
728
+ modifiers: Partial<FlowInputModifiers> = {},
729
+ ): FlowInputEvent {
730
+ return {
731
+ kind: 'wheel',
732
+ deltaX: deltas.deltaX ?? 0,
733
+ deltaY: deltas.deltaY,
734
+ deltaMode: deltas.deltaMode ?? 0,
735
+ point: { x: 100, y: 100 },
736
+ screenPoint: { x: 100, y: 100 },
737
+ target: { kind: 'pane' },
738
+ modifiers: { ...NO_MODS, ...modifiers },
739
+ };
740
+ }
741
+
742
+ it('plain wheel pans and requests preventDefault', () => {
743
+ const { runtime, ctx } = makeRig();
744
+ const effects = runtime.send(wheel({ deltaX: 10, deltaY: 20 }));
745
+ expect(effects.preventDefault).toBe(true);
746
+ expect(ctx().viewport.x).toBe(-10);
747
+ expect(ctx().viewport.y).toBe(-20);
748
+ });
749
+
750
+ it('ctrl-wheel (pinch) zooms around the pointer', () => {
751
+ const { runtime, ctx } = makeRig();
752
+ const before = ctx().viewport.zoom;
753
+ runtime.send(wheel({ deltaY: -100 }, { ctrl: true }));
754
+ expect(ctx().viewport.zoom).toBeGreaterThan(before);
755
+ });
756
+ });
757
+
758
+ describe('input runtime — pinch mode', () => {
759
+ it('zooms around the pinch center and pans by center travel', () => {
760
+ const { runtime, ctx } = makeRig();
761
+
762
+ runtime.send({
763
+ kind: 'pinch.start',
764
+ center: { x: 100, y: 100 },
765
+ screenCenter: { x: 100, y: 100 },
766
+ distance: 100,
767
+ pointerCount: 2,
768
+ modifiers: NO_MODS,
769
+ });
770
+ runtime.send({
771
+ kind: 'pinch.move',
772
+ center: { x: 110, y: 100 },
773
+ screenCenter: { x: 110, y: 100 },
774
+ distance: 200,
775
+ pointerCount: 2,
776
+ modifiers: NO_MODS,
777
+ });
778
+
779
+ expect(ctx().viewport.zoom).toBe(2);
780
+ expect(ctx().viewport.x).toBe(-90);
781
+ expect(ctx().viewport.y).toBe(-100);
782
+
783
+ runtime.send({
784
+ kind: 'pinch.end',
785
+ center: { x: 110, y: 100 },
786
+ screenCenter: { x: 110, y: 100 },
787
+ distance: 200,
788
+ pointerCount: 1,
789
+ modifiers: NO_MODS,
790
+ });
791
+ expect(runtime.getActiveSession()).toBeNull();
792
+ });
793
+ });
794
+
795
+ describe('input runtime — anchored pinch', () => {
796
+ function gesture(kind: 'pinch.start' | 'pinch.move', distance: number, x = 1200, y = 600): FlowInputEvent {
797
+ return { kind, center: { x: 872, y: 409.5 }, screenCenter: { x, y }, distance, pointerCount: 2, modifiers: NO_MODS };
798
+ }
799
+
800
+ it('keeps the original anchor while clamping and reversing zoom', () => {
801
+ const { store, runtime, ctx } = makeRig({ storeOptions: { minZoom: 0.5, maxZoom: 2 } });
802
+ store.trigger.setViewport({ x: 328, y: 190.5, zoom: 1 });
803
+ runtime.send(gesture('pinch.start', 100));
804
+ runtime.send(gesture('pinch.move', 300));
805
+ expect(ctx().viewport).toEqual({ x: -544, y: -219, zoom: 2 });
806
+ runtime.send(gesture('pinch.move', 200));
807
+ expect(ctx().viewport).toEqual({ x: -544, y: -219, zoom: 2 });
808
+ runtime.send(gesture('pinch.move', 100));
809
+ expect(ctx().viewport).toEqual({ x: 328, y: 190.5, zoom: 1 });
810
+ runtime.send(gesture('pinch.move', 10));
811
+ expect(ctx().viewport).toEqual({ x: 764, y: 395.25, zoom: 0.5 });
812
+ runtime.send(gesture('pinch.move', 50));
813
+ expect(ctx().viewport).toEqual({ x: 764, y: 395.25, zoom: 0.5 });
814
+ runtime.send(gesture('pinch.move', 100, 1250, 625));
815
+ expect(ctx().viewport).toEqual({ x: 378, y: 215.5, zoom: 1 });
816
+ });
817
+
818
+ it.each([
819
+ { canPan: true, canZoom: true, viewport: { x: -90, y: -80, zoom: 2 } },
820
+ { canPan: false, canZoom: true, viewport: { x: -100, y: -100, zoom: 2 } },
821
+ { canPan: true, canZoom: false, viewport: { x: 10, y: 20, zoom: 1 } },
822
+ { canPan: false, canZoom: false, viewport: { x: 0, y: 0, zoom: 1 } },
823
+ ])('respects pan=$canPan and zoom=$canZoom independently', ({ canPan, canZoom, viewport }) => {
824
+ const { runtime, ctx } = makeRig({ policies: { canPan, canZoom } });
825
+ runtime.send(gesture('pinch.start', 100, 100, 100));
826
+ runtime.send(gesture('pinch.move', 200, 110, 120));
827
+ expect(ctx().viewport).toEqual(viewport);
828
+ runtime.send(gesture('pinch.move', 100, 130, 150));
829
+ expect(ctx().viewport).toEqual({ x: canPan ? 30 : 0, y: canPan ? 50 : 0, zoom: 1 });
830
+ });
831
+
832
+ it('clamps to minimum zoom when a positive-distance gesture reaches zero distance', () => {
833
+ const { runtime, ctx } = makeRig({ storeOptions: { minZoom: 0.5, maxZoom: 2 } });
834
+ runtime.send(gesture('pinch.start', 100, 100, 100));
835
+ runtime.send(gesture('pinch.move', 200, 100, 100));
836
+ expect(ctx().viewport.zoom).toBe(2);
837
+ runtime.send(gesture('pinch.move', 0, 110, 120));
838
+ expect(ctx().viewport).toEqual({ x: 60, y: 70, zoom: 0.5 });
839
+ runtime.send(gesture('pinch.move', 100, 120, 130));
840
+ expect(ctx().viewport).toEqual({ x: 20, y: 30, zoom: 1 });
841
+ });
842
+
843
+ it('pans without dividing by zero and starts zooming at the first positive distance', () => {
844
+ const { runtime, ctx } = makeRig();
845
+ runtime.send(gesture('pinch.start', 0, 100, 100));
846
+ runtime.send(gesture('pinch.move', 0, 110, 120));
847
+ expect(ctx().viewport).toEqual({ x: 10, y: 20, zoom: 1 });
848
+ runtime.send(gesture('pinch.move', 100, 110, 120));
849
+ expect(ctx().viewport).toEqual({ x: 10, y: 20, zoom: 1 });
850
+ runtime.send(gesture('pinch.move', 200, 120, 130));
851
+ expect(ctx().viewport).toEqual({ x: -80, y: -70, zoom: 2 });
852
+ });
853
+ });
854
+
855
+ describe('input runtime — keyboard', () => {
856
+ it('Delete removes the selection and requests preventDefault', () => {
857
+ const { store, runtime, ctx, key } = makeRig();
858
+ store.trigger.select({ ids: ['a'] });
859
+ const effects = runtime.send(key('key.down', 'Delete'));
860
+ expect(effects.preventDefault).toBe(true);
861
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(false);
862
+ });
863
+
864
+ it('can rebind delete and leaves Delete/Backspace inert', () => {
865
+ const { store, runtime, ctx, key } = makeRig({ keymap: { delete: 'x' } });
866
+ store.trigger.select({ ids: ['a'] });
867
+ runtime.send(key('key.down', 'x'));
868
+ expect(ctx().graph.nodes.some((node) => node.id === 'a')).toBe(false);
869
+
870
+ const rig = makeRig({ keymap: { delete: 'x' } });
871
+ rig.store.trigger.select({ ids: ['a'] });
872
+ rig.runtime.send(rig.key('key.down', 'Delete'));
873
+ rig.runtime.send(rig.key('key.down', 'Backspace'));
874
+ expect(rig.ctx().graph.nodes.some((node) => node.id === 'a')).toBe(true);
875
+ });
876
+
877
+ it('can disable delete', () => {
878
+ const { store, runtime, ctx, key } = makeRig({ keymap: { delete: null } });
879
+ store.trigger.select({ ids: ['a'] });
880
+ runtime.send(key('key.down', 'Delete'));
881
+ runtime.send(key('key.down', 'Backspace'));
882
+ expect(ctx().graph.nodes.some((node) => node.id === 'a')).toBe(true);
883
+ });
884
+
885
+ it('does not delete on Cmd/Ctrl/Alt+Backspace (extra meta/ctrl/alt rejected)', () => {
886
+ // Cmd+Backspace
887
+ const cmd = makeRig();
888
+ cmd.store.trigger.select({ ids: ['a'] });
889
+ const cmdEffects = cmd.runtime.send(
890
+ cmd.key('key.down', 'Backspace', { modifiers: { meta: true } }),
891
+ );
892
+ expect(cmd.ctx().graph.nodes.some((n) => n.id === 'a')).toBe(true);
893
+ expect(cmdEffects.preventDefault).toBe(false);
894
+ expect(cmdEffects.handledBy).toBeNull();
895
+
896
+ // Ctrl+Backspace
897
+ const ctrl = makeRig();
898
+ ctrl.store.trigger.select({ ids: ['a'] });
899
+ ctrl.runtime.send(ctrl.key('key.down', 'Backspace', { modifiers: { ctrl: true } }));
900
+ expect(ctrl.ctx().graph.nodes.some((n) => n.id === 'a')).toBe(true);
901
+
902
+ // Alt+Delete
903
+ const alt = makeRig();
904
+ alt.store.trigger.select({ ids: ['a'] });
905
+ const altEffects = alt.runtime.send(
906
+ alt.key('key.down', 'Delete', { modifiers: { alt: true } }),
907
+ );
908
+ expect(alt.ctx().graph.nodes.some((n) => n.id === 'a')).toBe(true);
909
+ expect(altEffects.preventDefault).toBe(false);
910
+ });
911
+
912
+ it('tolerates extra shift: shift+Delete still deletes', () => {
913
+ const { store, runtime, ctx, key } = makeRig();
914
+ store.trigger.select({ ids: ['a'] });
915
+ runtime.send(key('key.down', 'Delete', { modifiers: { shift: true } }));
916
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(false);
917
+ });
918
+
919
+ it('matches mod bindings with meta or ctrl', () => {
920
+ const mac = makeRig();
921
+ mac.store.trigger.updateNode({ id: 'a', patch: { x: 10 } });
922
+ mac.runtime.send(mac.key('key.down', 'z', { modifiers: { meta: true } }));
923
+ expect(mac.ctx().graph.nodes.find((node) => node.id === 'a')?.x).toBe(0);
924
+
925
+ const win = makeRig();
926
+ win.store.trigger.updateNode({ id: 'a', patch: { x: 10 } });
927
+ win.runtime.send(win.key('key.down', 'z', { modifiers: { ctrl: true } }));
928
+ expect(win.ctx().graph.nodes.find((node) => node.id === 'a')?.x).toBe(0);
929
+ });
930
+
931
+ it('does nothing when typing into an editable target', () => {
932
+ const { store, runtime, ctx, key } = makeRig();
933
+ store.trigger.select({ ids: ['a'] });
934
+ const effects = runtime.send(
935
+ key('key.down', 'Backspace', { editableTarget: true }),
936
+ );
937
+ expect(effects.handledBy).toBeNull();
938
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(true);
939
+ });
940
+
941
+ it('mod+z undoes, mod+shift+z redoes', () => {
942
+ const { store, runtime, ctx, key } = makeRig();
943
+ store.trigger.deleteNode({ id: 'a' });
944
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(false);
945
+
946
+ runtime.send(key('key.down', 'z', { modifiers: { meta: true } }));
947
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(true);
948
+
949
+ runtime.send(
950
+ key('key.down', 'z', { modifiers: { meta: true, shift: true } }),
951
+ );
952
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(false);
953
+ });
954
+
955
+ it('Escape cancels an in-progress connection', () => {
956
+ const { store, runtime, ctx, key } = makeRig();
957
+ store.trigger.connectionStart({
958
+ sourceId: 'a',
959
+ sourcePort: null,
960
+ position: { x: 0, y: 0 },
961
+ });
962
+ runtime.send(key('key.down', 'Escape'));
963
+ expect(ctx().connection).toBeNull();
964
+ });
965
+ });
966
+
967
+ describe('input runtime — mode swap (the phase-2 definition of done)', () => {
968
+ it('setMode("readonly") disables dragging and deleting; pan and select still work', () => {
969
+ const { store, runtime, ctx, pointer, key } = makeRig();
970
+
971
+ // interactive: drag works
972
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
973
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
974
+ runtime.send(pointer('pointer.up', { x: 30, y: 10 }));
975
+ expect(ctx().graph.nodes[0].x).toBe(20);
976
+
977
+ // one value swap — no handler forks
978
+ store.trigger.setMode({ mode: 'readonly' });
979
+
980
+ runtime.send(pointer('pointer.down', { x: 30, y: 10 }, { target: node('a') }));
981
+ runtime.send(pointer('pointer.move', { x: 80, y: 10 }));
982
+ runtime.send(pointer('pointer.up', { x: 80, y: 10 }));
983
+ expect(ctx().graph.nodes[0].x).toBe(20); // unmoved
984
+ expect([...ctx().selection]).toEqual(['a']); // selection still works
985
+
986
+ runtime.send(key('key.down', 'Delete'));
987
+ expect(ctx().graph.nodes.some((n) => n.id === 'a')).toBe(true); // not deleted
988
+
989
+ // pan still works
990
+ runtime.send(pointer('pointer.down', { x: 500, y: 10 }));
991
+ runtime.send(pointer('pointer.move', { x: 560, y: 10 }));
992
+ expect(ctx().canvasState).toBe('panning');
993
+ runtime.send(pointer('pointer.up', { x: 560, y: 10 }));
994
+
995
+ // and back
996
+ store.trigger.setMode({ mode: 'interactive' });
997
+ runtime.send(pointer('pointer.down', { x: 30, y: 10 }, { target: node('a') }));
998
+ runtime.send(pointer('pointer.move', { x: 80, y: 10 }));
999
+ runtime.send(pointer('pointer.up', { x: 80, y: 10 }));
1000
+ expect(ctx().graph.nodes[0].x).toBe(70);
1001
+ });
1002
+ });
1003
+
1004
+ describe('input runtime — session lifecycle', () => {
1005
+ it('cancelSession applies the session cancel commands', () => {
1006
+ const { runtime, ctx, pointer } = makeRig();
1007
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
1008
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
1009
+ expect(runtime.getActiveSession()).toBe('entity-press');
1010
+
1011
+ runtime.cancelSession();
1012
+ expect(runtime.getActiveSession()).toBeNull();
1013
+ expect(ctx().canvasState).toBe('idle');
1014
+ expect(ctx().graph.nodes[0].x).toBe(0);
1015
+ expect(ctx().graph.nodes[0].dx).toBeUndefined();
1016
+ });
1017
+
1018
+ it('Escape rolls back an active drag', () => {
1019
+ const { runtime, ctx, pointer, key } = makeRig();
1020
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
1021
+ runtime.send(pointer('pointer.move', { x: 30, y: 10 }));
1022
+ runtime.send(key('key.down', 'Escape'));
1023
+
1024
+ expect(runtime.getActiveSession()).toBeNull();
1025
+ expect(ctx().canvasState).toBe('idle');
1026
+ expect(ctx().graph.nodes[0].x).toBe(0);
1027
+ expect(ctx().graph.nodes[0].dx).toBeUndefined();
1028
+ });
1029
+
1030
+ it('reports session effects for adapter pointer capture', () => {
1031
+ const { runtime, pointer } = makeRig();
1032
+ const down = runtime.send(
1033
+ pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }),
1034
+ );
1035
+ expect(down.sessionStarted).toBe(true);
1036
+ const up = runtime.send(pointer('pointer.up', { x: 10, y: 10 }));
1037
+ expect(up.sessionEnded).toBe(true);
1038
+ });
1039
+ });
1040
+
1041
+ describe('input runtime — keyboard navigation (a11y)', () => {
1042
+ it('arrow keys nudge the selection by 5px and commit (one undo step each)', () => {
1043
+ const { store, runtime, ctx, key } = makeRig();
1044
+ store.trigger.select({ ids: ['a'] });
1045
+
1046
+ const effects = runtime.send(key('key.down', 'ArrowRight'));
1047
+ expect(effects.preventDefault).toBe(true);
1048
+ expect(ctx().graph.nodes[0].x).toBe(5);
1049
+ expect(ctx().graph.nodes[0].dx ?? 0).toBe(0); // committed
1050
+
1051
+ runtime.send(key('key.down', 'ArrowDown'));
1052
+ expect(ctx().graph.nodes[0].y).toBe(5);
1053
+
1054
+ store.trigger.undo();
1055
+ expect(ctx().graph.nodes[0].y).toBe(0);
1056
+ expect(ctx().graph.nodes[0].x).toBe(5);
1057
+ store.trigger.undo();
1058
+ expect(ctx().graph.nodes[0].x).toBe(0);
1059
+ });
1060
+
1061
+ it('shift+arrow moves by the larger step (20px)', () => {
1062
+ const { store, runtime, ctx, key } = makeRig();
1063
+ store.trigger.select({ ids: ['a'] });
1064
+ runtime.send(key('key.down', 'ArrowRight', { modifiers: { shift: true } }));
1065
+ expect(ctx().graph.nodes[0].x).toBe(20);
1066
+ });
1067
+
1068
+ it('arrows do nothing without a selection', () => {
1069
+ const { runtime, ctx, key } = makeRig();
1070
+ const effects = runtime.send(key('key.down', 'ArrowRight'));
1071
+ expect(effects.handledBy).toBeNull();
1072
+ expect(ctx().graph.nodes[0].x).toBe(0);
1073
+ });
1074
+
1075
+ it('Enter selects the focused entity (shift adds to selection)', () => {
1076
+ const { store, runtime, ctx, key } = makeRig();
1077
+ store.trigger.select({ ids: ['b'] });
1078
+ store.trigger.focus({ entityId: 'a' });
1079
+
1080
+ runtime.send(key('key.down', 'Enter', { modifiers: { shift: true } }));
1081
+ expect([...ctx().selection].sort()).toEqual(['a', 'b']);
1082
+
1083
+ store.trigger.focus({ entityId: 'b' });
1084
+ runtime.send(key('key.down', 'Enter'));
1085
+ expect([...ctx().selection]).toEqual(['b']);
1086
+ });
1087
+
1088
+ it('Escape when idle clears selection and focus', () => {
1089
+ const { store, runtime, ctx, key } = makeRig();
1090
+ store.trigger.select({ ids: ['a'] });
1091
+ store.trigger.focus({ entityId: 'a' });
1092
+
1093
+ runtime.send(key('key.down', 'Escape'));
1094
+ expect(ctx().selection.size).toBe(0);
1095
+ expect(ctx().focusedEntityId).toBeNull();
1096
+ });
1097
+
1098
+ it('Escape cancels even while shift is held (extra-shift tolerance)', () => {
1099
+ const { store, runtime, ctx, key } = makeRig();
1100
+ store.trigger.select({ ids: ['a'] });
1101
+ store.trigger.focus({ entityId: 'a' });
1102
+
1103
+ runtime.send(key('key.down', 'Escape', { modifiers: { shift: true } }));
1104
+ expect(ctx().selection.size).toBe(0);
1105
+ expect(ctx().focusedEntityId).toBeNull();
1106
+ void store;
1107
+ });
1108
+
1109
+ it('Escape during a connection only cancels the connection', () => {
1110
+ const { store, runtime, ctx, key } = makeRig();
1111
+ store.trigger.select({ ids: ['a'] });
1112
+ store.trigger.connectionStart({
1113
+ sourceId: 'a',
1114
+ sourcePort: null,
1115
+ position: { x: 0, y: 0 },
1116
+ });
1117
+
1118
+ runtime.send(key('key.down', 'Escape'));
1119
+ expect(ctx().connection).toBeNull();
1120
+ expect(ctx().selection.has('a')).toBe(true);
1121
+ });
1122
+
1123
+ it('readonly mode: arrows do not move, Enter still selects', () => {
1124
+ const { store, runtime, ctx, key } = makeRig();
1125
+ store.trigger.setMode({ mode: 'readonly' });
1126
+ store.trigger.select({ ids: ['a'] });
1127
+
1128
+ runtime.send(key('key.down', 'ArrowRight'));
1129
+ expect(ctx().graph.nodes[0].x).toBe(0);
1130
+
1131
+ store.trigger.focus({ entityId: 'b' });
1132
+ runtime.send(key('key.down', 'Enter'));
1133
+ expect([...ctx().selection]).toEqual(['b']);
1134
+ });
1135
+ });
1136
+
1137
+ describe('input runtime — keyboard policies', () => {
1138
+ function makeKeyboardRig(options: {
1139
+ nav?: Parameters<typeof keyboardNavMode>[0];
1140
+ keyboard?: Parameters<typeof keyboardMode>[0];
1141
+ keymap?: Partial<FlowKeymap>;
1142
+ } = {}) {
1143
+ return makeRig({
1144
+ keymap: options.keymap,
1145
+ storeOptions: { connectOnClick: true },
1146
+ modes: [
1147
+ ...defaultInputModes.filter((mode) => mode.id !== 'keyboard' && mode.id !== 'keyboard-nav'),
1148
+ keyboardMode({ ...options.keyboard, keymap: options.keymap }),
1149
+ keyboardNavMode({ ...options.nav, keymap: options.keymap }),
1150
+ ],
1151
+ });
1152
+ }
1153
+
1154
+ it('allows literal activation bindings independently of application shortcuts', () => {
1155
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { activationKeys: ['x'] }, keymap: { panActivation: null } });
1156
+ store.trigger.focus({ entityId: 'a' });
1157
+ expect(runtime.send(key('key.down', 'Enter')).handledBy).toBeNull();
1158
+ expect(runtime.send(key('key.down', ' ')).preventDefault).toBe(false);
1159
+ expect(ctx().selection.size).toBe(0);
1160
+ expect(runtime.send(key('key.down', 'x')).preventDefault).toBe(true);
1161
+ expect([...ctx().selection]).toEqual(['a']);
1162
+ });
1163
+
1164
+ it.each([null, []] as const)('can disable focused activation with %s', (activationKeys) => {
1165
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { activationKeys } });
1166
+ store.trigger.focus({ entityId: 'a' });
1167
+ for (const activation of ['Enter', ' ', 'Spacebar']) {
1168
+ expect(runtime.send(key('key.down', activation)).handledBy).toBeNull();
1169
+ }
1170
+ expect(ctx().selection.size).toBe(0);
1171
+ });
1172
+
1173
+ it.each(['a', 'e1'])('can allow application shortcuts after activating focused %s', (id) => {
1174
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { preventDefault: false } });
1175
+ store.trigger.focus({ entityId: id });
1176
+ const effects = runtime.send(key('key.down', ' '));
1177
+ expect(effects.handledBy).toBe('keyboard-nav');
1178
+ expect(effects.preventDefault).toBe(false);
1179
+ expect([...ctx().selection]).toEqual([id]);
1180
+ if (id === 'a') {
1181
+ expect(runtime.send(key('key.down', 'ArrowRight')).preventDefault).toBe(true);
1182
+ expect(ctx().graph.nodes[0].x).toBe(5);
1183
+ }
1184
+ });
1185
+
1186
+ it.each(['a', 'e1'])('can preserve multiple selections when focused %s is already selected', (id) => {
1187
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { selectionBehavior: 'preserve' } });
1188
+ store.trigger.select({ ids: [id, 'b'] });
1189
+ store.trigger.focus({ entityId: id });
1190
+ runtime.send(key('key.down', 'Enter'));
1191
+ expect([...ctx().selection]).toEqual([id, 'b']);
1192
+ store.trigger.select({ ids: ['b'] });
1193
+ runtime.send(key('key.down', 'Enter'));
1194
+ expect([...ctx().selection]).toEqual([id]);
1195
+ });
1196
+
1197
+ it('uses the configured multi-select modifier rather than hardcoded Shift', () => {
1198
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { selectionBehavior: 'preserve' }, keymap: { multiSelectModifier: 'meta' } });
1199
+ store.trigger.focus({ entityId: 'a' });
1200
+ store.trigger.select({ ids: ['b'] });
1201
+ runtime.send(key('key.down', 'Enter', { modifiers: { shift: true } }));
1202
+ expect([...ctx().selection]).toEqual(['a']);
1203
+ store.trigger.select({ ids: ['a', 'b'] });
1204
+ runtime.send(key('key.down', 'Enter', { modifiers: { meta: true } }));
1205
+ expect([...ctx().selection]).toEqual(['b']);
1206
+ runtime.send(key('key.down', 'Enter', { modifiers: { meta: true } }));
1207
+ expect([...ctx().selection]).toEqual(['b', 'a']);
1208
+ });
1209
+
1210
+ it.each([null, 'b', 'e1', 'a:out'])('requires a selected focused node before nudging with focus %s', (focusedEntityId) => {
1211
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { nudge: 'focused-selection' } });
1212
+ store.trigger.select({ ids: ['a', 'e1', 'a:out'] });
1213
+ if (focusedEntityId) store.trigger.focus({ entityId: focusedEntityId });
1214
+ expect(runtime.send(key('key.down', 'ArrowRight')).handledBy).toBeNull();
1215
+ expect(ctx().graph.nodes[0].x).toBe(0);
1216
+ });
1217
+
1218
+ it('nudges the current selection when a selected node has focus', () => {
1219
+ const { store, runtime, ctx, key } = makeKeyboardRig({ nav: { nudge: 'focused-selection' } });
1220
+ store.trigger.select({ ids: ['a', 'b'] });
1221
+ store.trigger.focus({ entityId: 'a' });
1222
+ expect(runtime.send(key('key.down', 'ArrowRight')).preventDefault).toBe(true);
1223
+ expect(ctx().graph.nodes.map((node) => node.x)).toEqual([5, 305, 0]);
1224
+ store.trigger.undo();
1225
+ expect(ctx().graph.nodes.map((node) => node.x)).toEqual([0, 300, 0]);
1226
+ });
1227
+
1228
+ it('focused Escape removes only the focused selection and clears focus', () => {
1229
+ const { store, runtime, ctx, key } = makeKeyboardRig({ keyboard: { escapeSelection: 'focused' } });
1230
+ store.trigger.select({ ids: ['a', 'b'] });
1231
+ store.trigger.focus({ entityId: 'a' });
1232
+ runtime.send(key('key.down', 'Escape'));
1233
+ expect([...ctx().selection]).toEqual(['b']);
1234
+ expect(ctx().focusedEntityId).toBeNull();
1235
+ store.trigger.focus({ entityId: 'a' });
1236
+ runtime.send(key('key.down', 'Escape'));
1237
+ expect([...ctx().selection]).toEqual(['b']);
1238
+ expect(ctx().focusedEntityId).toBeNull();
1239
+ });
1240
+
1241
+ it('can preserve idle selection and focus on Escape', () => {
1242
+ const { store, runtime, ctx, key } = makeKeyboardRig({ keyboard: { escapeSelection: 'none' } });
1243
+ store.trigger.select({ ids: ['a', 'b'] });
1244
+ store.trigger.focus({ entityId: 'a' });
1245
+ runtime.send(key('key.down', 'Escape'));
1246
+ expect([...ctx().selection]).toEqual(['a', 'b']);
1247
+ expect(ctx().focusedEntityId).toBe('a');
1248
+ });
1249
+
1250
+ it.each(['all', 'focused', 'none'] as const)('Escape cancels active drag before applying %s selection policy', (escapeSelection) => {
1251
+ const { store, runtime, ctx, pointer, key } = makeKeyboardRig({ keyboard: { escapeSelection } });
1252
+ store.trigger.select({ ids: ['a', 'b'] });
1253
+ store.trigger.focus({ entityId: 'a' });
1254
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
1255
+ runtime.send(pointer('pointer.move', { x: 40, y: 10 }));
1256
+ expect(ctx().canvasState).toBe('dragging');
1257
+ const selection = [...ctx().selection];
1258
+ const focus = ctx().focusedEntityId;
1259
+ runtime.send(key('key.down', 'Escape'));
1260
+ expect(ctx().canvasState).toBe('idle');
1261
+ expect(ctx().graph.nodes[0].x).toBe(0);
1262
+ expect([...ctx().selection]).toEqual(selection);
1263
+ expect(ctx().focusedEntityId).toBe(focus);
1264
+ });
1265
+
1266
+ it.each(['all', 'focused', 'none'] as const)('Escape cancels pending clicks before applying %s selection policy', (escapeSelection) => {
1267
+ const { store, runtime, ctx, pointer, key } = makeKeyboardRig({ keyboard: { escapeSelection } });
1268
+ store.trigger.select({ ids: ['a', 'b'] });
1269
+ store.trigger.focus({ entityId: 'a' });
1270
+ const target: FlowInputTarget = { kind: 'port', nodeId: 'a', portName: 'out' };
1271
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target }));
1272
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target }));
1273
+ expect(ctx().connectionClickStartHandle).not.toBeNull();
1274
+ runtime.send(key('key.down', 'Escape'));
1275
+ expect(ctx().connection).toBeNull();
1276
+ expect(ctx().connectionClickStartHandle).toBeNull();
1277
+ expect([...ctx().selection]).toEqual(['a', 'b']);
1278
+ expect(ctx().focusedEntityId).toBe('a');
1279
+ });
1280
+
1281
+ it('ignores editable targets under every keyboard option', () => {
1282
+ const { store, runtime, ctx, key } = makeKeyboardRig({ keyboard: { escapeSelection: 'focused' }, nav: { selectionBehavior: 'preserve', preventDefault: false, activationKeys: ['x'] } });
1283
+ store.trigger.select({ ids: ['a', 'b'] });
1284
+ store.trigger.focus({ entityId: 'a' });
1285
+ for (const command of ['Escape', 'x', 'ArrowRight']) {
1286
+ expect(runtime.send(key('key.down', command, { editableTarget: true })).handledBy).toBeNull();
1287
+ }
1288
+ expect([...ctx().selection]).toEqual(['a', 'b']);
1289
+ expect(ctx().focusedEntityId).toBe('a');
1290
+ expect(ctx().graph.nodes[0].x).toBe(0);
1291
+ });
1292
+ });
1293
+
1294
+ describe('input runtime — connect mode', () => {
1295
+ const port = (nodeId: string, portName: string): FlowInputTarget => ({
1296
+ kind: 'port',
1297
+ nodeId,
1298
+ portName,
1299
+ });
1300
+
1301
+ it('dragging from a port starts a connection and tracks targets', () => {
1302
+ const { runtime, ctx, pointer } = makeRig();
1303
+ runtime.send(
1304
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1305
+ );
1306
+ expect(ctx().canvasState).toBe('connecting');
1307
+ expect(ctx().connection?.sourceId).toBe('a');
1308
+ expect(ctx().connection?.sourcePort).toBe('out');
1309
+
1310
+ runtime.send(
1311
+ pointer('pointer.move', { x: 350, y: 5 }, { target: port('b', 'in') }),
1312
+ );
1313
+ expect(ctx().connection?.targetId).toBe('b');
1314
+ expect(ctx().connection?.targetPort).toBe('in');
1315
+ expect(ctx().connection?.isValid).toBe(true);
1316
+
1317
+ runtime.send(pointer('pointer.up', { x: 350, y: 5 }, { target: port('b', 'in') }));
1318
+ expect(ctx().connection).toBeNull();
1319
+ expect(ctx().canvasState).toBe('idle');
1320
+ });
1321
+
1322
+ it.each(['forward', 'reverse'] as const)('completes a %s drag using the release target without a preceding move', (direction) => {
1323
+ const { store, runtime, ctx, pointer } = makeRig();
1324
+ const events: ConnectionEndedEvent[] = [];
1325
+ store.on('connectionEnded', (event) => events.push(event));
1326
+ const origin = direction === 'forward' ? { id: 'a', port: 'out', x: 50, y: 50 } : { id: 'b', port: 'in', x: 350, y: 5 };
1327
+ const target = direction === 'forward' ? { id: 'b', port: 'in', x: 350, y: 5 } : { id: 'a', port: 'out', x: 50, y: 50 };
1328
+ runtime.send(pointer('pointer.down', origin, { target: port(origin.id, origin.port) }));
1329
+ runtime.send(pointer('pointer.up', target, { target: port(target.id, target.port) }));
1330
+ expect(events).toHaveLength(1);
1331
+ expect(events[0].status).toBe('connected');
1332
+ expect(events[0].source).toEqual({ nodeId: origin.id, port: origin.port });
1333
+ expect(events[0].connection.targetId).toBe(target.id);
1334
+ expect(events[0].connection.targetPort).toBe(target.port);
1335
+ expect(ctx().connection).toBeNull();
1336
+ });
1337
+
1338
+ it.each(['forward', 'reverse'] as const)('shares preview and completion for %s click connections', (direction) => {
1339
+ const { store, runtime, ctx, pointer } = makeRig({ storeOptions: { connectOnClick: true } });
1340
+ const events: ConnectionEndedEvent[] = [];
1341
+ store.on('connectionEnded', (event) => events.push(event));
1342
+ const origin = direction === 'forward' ? { id: 'a', port: 'out', x: 50, y: 50 } : { id: 'b', port: 'in', x: 350, y: 5 };
1343
+ const target = direction === 'forward' ? { id: 'b', port: 'in', x: 350, y: 5 } : { id: 'a', port: 'out', x: 50, y: 50 };
1344
+ runtime.send(pointer('pointer.down', origin, { target: port(origin.id, origin.port) }));
1345
+ runtime.send(pointer('pointer.up', origin, { target: port(origin.id, origin.port) }));
1346
+ expect(events).toHaveLength(0);
1347
+ expect(runtime.getActiveSession()).toBeNull();
1348
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: origin.id, port: origin.port });
1349
+ expect(ctx().connection?.active).toBe(true);
1350
+ runtime.send(pointer('pointer.move', target));
1351
+ expect(ctx().connection?.isValid).toBe(true);
1352
+ expect(ctx().connection?.targetId).toBe(target.id);
1353
+ runtime.send(pointer('pointer.down', target, { target: port(target.id, target.port) }));
1354
+ runtime.send(pointer('pointer.up', target, { target: port(target.id, target.port) }));
1355
+ expect(events).toHaveLength(1);
1356
+ expect(events[0].status).toBe('connected');
1357
+ expect(events[0].source).toEqual({ nodeId: origin.id, port: origin.port });
1358
+ expect(ctx().connectionClickStartHandle).toBeNull();
1359
+ expect(ctx().connection).toBeNull();
1360
+ });
1361
+
1362
+ it('clears a pending click connection when its mode is removed', () => {
1363
+ let modes = [connectMode()];
1364
+ const { runtime, ctx, pointer } = makeRig({
1365
+ storeOptions: { connectOnClick: true },
1366
+ modes: () => modes,
1367
+ });
1368
+ const source = { x: 50, y: 50 };
1369
+ runtime.send(pointer('pointer.down', source, { target: port('a', 'out') }));
1370
+ runtime.send(pointer('pointer.up', source, { target: port('a', 'out') }));
1371
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1372
+ expect(runtime.getActiveSession()).toBeNull();
1373
+
1374
+ modes = [];
1375
+ runtime.reconcileModes();
1376
+ expect(ctx().connectionClickStartHandle).toBeNull();
1377
+ expect(ctx().connection).toBeNull();
1378
+ });
1379
+
1380
+ it.each(['loose', 'strict'] as const)('shares node-body release validation between click and drag in %s mode', (mode) => {
1381
+ const results: ConnectionEndedEvent[] = [];
1382
+ for (const connectOnClick of [false, true]) {
1383
+ const { store, runtime, ctx, pointer } = makeRig({
1384
+ storeOptions: { connectOnClick },
1385
+ engineOptions: { connection: { mode } },
1386
+ });
1387
+ store.on('connectionEnded', (event) => results.push(event));
1388
+ const body = { x: 390, y: 40 };
1389
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1390
+ if (connectOnClick) {
1391
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1392
+ runtime.send(pointer('pointer.down', body, { target: { kind: 'entity', id: 'b' } }));
1393
+ }
1394
+ runtime.send(pointer('pointer.up', body, { target: { kind: 'entity', id: 'b' } }));
1395
+ expect(ctx().connection).toBeNull();
1396
+ expect(ctx().connectionClickStartHandle).toBeNull();
1397
+ }
1398
+ expect(results).toHaveLength(2);
1399
+ expect(results.map((event) => event.status)).toEqual(mode === 'loose'
1400
+ ? ['connected', 'connected'] : ['invalid', 'invalid']);
1401
+ expect(results[1].target).toEqual({ kind: 'node', nodeId: 'b', port: null });
1402
+ });
1403
+
1404
+ it.each([0.25, 0.5, 1, 2])('keeps two-pixel click drift pending at zoom %s with or without a move event', (zoom) => {
1405
+ for (const move of [false, true]) {
1406
+ const { store, runtime, ctx, pointer } = makeRig({ storeOptions: { connectOnClick: true } });
1407
+ store.trigger.setViewport({ x: 0, y: 0, zoom });
1408
+ const origin = { x: 50, y: 50 };
1409
+ const screenOrigin = { x: origin.x * zoom, y: origin.y * zoom };
1410
+ const point = { x: origin.x + 2 / zoom, y: origin.y };
1411
+ const screenPoint = { x: screenOrigin.x + 2, y: screenOrigin.y };
1412
+ runtime.send(pointer('pointer.down', origin, { target: port('a', 'out'), screenPoint: screenOrigin }));
1413
+ if (move) {
1414
+ runtime.send(pointer('pointer.move', point, { target: port('a', 'out'), screenPoint }));
1415
+ expect(ctx().connection?.active).toBe(false);
1416
+ }
1417
+ runtime.send(pointer('pointer.up', point, { target: port('a', 'out'), screenPoint }));
1418
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1419
+ expect(ctx().connection?.active).toBe(true);
1420
+ }
1421
+ });
1422
+
1423
+ it.each([0.25, 1, 2])('activates a four-pixel connection drag at zoom %s', (zoom) => {
1424
+ const { store, runtime, ctx, pointer } = makeRig({ storeOptions: { connectOnClick: true } });
1425
+ store.trigger.setViewport({ x: 0, y: 0, zoom });
1426
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, {
1427
+ target: port('a', 'out'), screenPoint: { x: 50 * zoom, y: 50 * zoom },
1428
+ }));
1429
+ runtime.send(pointer('pointer.move', { x: 50 + 4 / zoom, y: 50 }, {
1430
+ target: port('a', 'out'), screenPoint: { x: 50 * zoom + 4, y: 50 * zoom },
1431
+ }));
1432
+ expect(ctx().connection?.active).toBe(true);
1433
+ expect(ctx().connectionClickStartHandle).toBeNull();
1434
+ });
1435
+
1436
+ it.each(['Escape', 'pane', 'origin', 'inspect', 'cancel'] as const)('cancels a pending click connection on %s', (action) => {
1437
+ const { store, runtime, ctx, pointer, key } = makeRig({ storeOptions: { connectOnClick: true } });
1438
+ const events: ConnectionEndedEvent[] = [];
1439
+ store.on('connectionEnded', (event) => events.push(event));
1440
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1441
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1442
+ if (action === 'Escape') runtime.send(key('key.down', 'Escape'));
1443
+ else if (action === 'cancel') runtime.send(pointer('pointer.cancel', { x: 50, y: 50 }));
1444
+ else runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: action === 'origin' ? port('a', 'out') : action === 'inspect' ? { kind: 'port', nodeId: 'b', portName: 'in', interaction: 'inspect' } : { kind: 'pane' } }));
1445
+ expect(ctx().connection).toBeNull();
1446
+ expect(ctx().connectionClickStartHandle).toBeNull();
1447
+ expect(events).toHaveLength(0);
1448
+ });
1449
+
1450
+ it.each(['cancelSession', 'Escape'] as const)('preserves callback-created pending lifetimes during %s', (action) => {
1451
+ for (const priorPending of [false, true]) {
1452
+ for (const sameEndpoint of [false, true]) {
1453
+ const { store, runtime, ctx, pointer, key } = makeRig({ modes: [{
1454
+ id: 'application-mode',
1455
+ start() {
1456
+ return { session: {
1457
+ update() {},
1458
+ cancel() {
1459
+ const nodeId = sameEndpoint ? 'a' : 'b';
1460
+ const port = sameEndpoint ? 'out' : 'in';
1461
+ store.trigger.connectionStart({ sourceId: nodeId, sourcePort: port, position: { x: 20, y: 20 } });
1462
+ store.trigger.setConnectionClickStartHandle({ nodeId, port });
1463
+ },
1464
+ } };
1465
+ },
1466
+ }] });
1467
+ if (priorPending) {
1468
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position: { x: 0, y: 0 } });
1469
+ store.trigger.setConnectionClickStartHandle({ nodeId: 'a', port: 'out' });
1470
+ }
1471
+ runtime.send(pointer('pointer.down', { x: 0, y: 0 }));
1472
+ if (action === 'cancelSession') runtime.cancelSession();
1473
+ else runtime.send(key('key.down', 'Escape'));
1474
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: sameEndpoint ? 'a' : 'b', port: sameEndpoint ? 'out' : 'in' });
1475
+ expect(ctx().connection?.startPosition).toEqual({ x: 20, y: 20 });
1476
+ expect(runtime.getActiveSession()).toBeNull();
1477
+ }
1478
+ }
1479
+ });
1480
+
1481
+ it.each(['cancelSession', 'Escape'] as const)('preserves a replacement opened by a standard cancellation observer during %s', (action) => {
1482
+ const { store, runtime, ctx, pointer, key } = makeRig({ storeOptions: { connectOnClick: true }, modes: [connectMode()] });
1483
+ let armed = false;
1484
+ let replaced = false;
1485
+ store.subscribe(snapshot => {
1486
+ if (!armed || replaced || snapshot.context.connection !== null) return;
1487
+ replaced = true;
1488
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position: { x: 20, y: 20 } });
1489
+ store.trigger.setConnectionClickStartHandle({ nodeId: 'a', port: 'out' });
1490
+ });
1491
+ runtime.send(pointer('pointer.down', { x: 0, y: 0 }, { target: port('a', 'out') }));
1492
+ armed = true;
1493
+ if (action === 'cancelSession') runtime.cancelSession();
1494
+ else runtime.send(key('key.down', 'Escape'));
1495
+ expect(replaced).toBe(true);
1496
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1497
+ expect(ctx().connection?.startPosition).toEqual({ x: 20, y: 20 });
1498
+ runtime.send(pointer('pointer.up', { x: 0, y: 0 }));
1499
+ expect(ctx().connection?.startPosition).toEqual({ x: 20, y: 20 });
1500
+ });
1501
+
1502
+ it('keeps connection ownership through moves and leaves published ownership unchanged during pure probes', () => {
1503
+ const { store, ctx } = makeRig();
1504
+ const position = { x: 0, y: 0 };
1505
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position });
1506
+ const lifetime = ctx().connection?.[connectionLifetime];
1507
+ expect(lifetime).toBeDefined();
1508
+ store.trigger.connectionMove({ position: { x: 20, y: 20 } });
1509
+ expect(ctx().connection?.[connectionLifetime]).toBe(lifetime);
1510
+ const snapshot = store.getSnapshot();
1511
+ store.can.connectionStart({ sourceId: 'a', sourcePort: 'out', position });
1512
+ store.transition(snapshot, { type: 'connectionStart', sourceId: 'a', sourcePort: 'out', position });
1513
+ expect(ctx().connection?.[connectionLifetime]).toBe(lifetime);
1514
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position });
1515
+ expect(ctx().connection?.[connectionLifetime]).not.toBe(lifetime);
1516
+ });
1517
+
1518
+ it('cancels the original pending lifetime after its pointer position changes in a cancellation callback', () => {
1519
+ const { store, runtime, ctx, pointer } = makeRig({ modes: [{
1520
+ id: 'application-mode',
1521
+ start() {
1522
+ return { session: {
1523
+ update() {},
1524
+ cancel() {
1525
+ store.trigger.connectionMove({ position: { x: 20, y: 20 } });
1526
+ },
1527
+ } };
1528
+ },
1529
+ }] });
1530
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position: { x: 0, y: 0 } });
1531
+ store.trigger.setConnectionClickStartHandle({ nodeId: 'a', port: 'out' });
1532
+ runtime.send(pointer('pointer.down', { x: 0, y: 0 }));
1533
+ runtime.cancelSession();
1534
+ expect(ctx().connectionClickStartHandle).toBeNull();
1535
+ expect(ctx().connection).toBeNull();
1536
+ });
1537
+
1538
+ it.each(['connection', 'handle'] as const)('preserves a pending lifetime when only its %s identity changes', (replacement) => {
1539
+ const { store, runtime, ctx, pointer } = makeRig({ modes: [{
1540
+ id: 'application-mode',
1541
+ start() {
1542
+ return { session: {
1543
+ update() {},
1544
+ cancel() {
1545
+ if (replacement === 'connection') {
1546
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position: { x: 20, y: 20 } });
1547
+ } else {
1548
+ store.trigger.setConnectionClickStartHandle({ nodeId: 'a', port: 'out' });
1549
+ }
1550
+ },
1551
+ } };
1552
+ },
1553
+ }] });
1554
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: 'out', position: { x: 0, y: 0 } });
1555
+ store.trigger.setConnectionClickStartHandle({ nodeId: 'a', port: 'out' });
1556
+ runtime.send(pointer('pointer.down', { x: 0, y: 0 }));
1557
+ runtime.cancelSession();
1558
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1559
+ expect(ctx().connection).not.toBeNull();
1560
+ });
1561
+
1562
+ it('does not retain clicks when disabled, and a threshold-crossing release still drags when enabled', () => {
1563
+ for (const connectOnClick of [false, true]) {
1564
+ const { store, runtime, ctx, pointer } = makeRig({ storeOptions: { connectOnClick } });
1565
+ const events: ConnectionEndedEvent[] = [];
1566
+ store.on('connectionEnded', (event) => events.push(event));
1567
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1568
+ runtime.send(pointer('pointer.up', { x: 350, y: 5 }, { target: port('b', 'in') }));
1569
+ expect(events[0].status).toBe('connected');
1570
+ expect(ctx().connectionClickStartHandle).toBeNull();
1571
+ }
1572
+ const { runtime, ctx, pointer } = makeRig();
1573
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1574
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1575
+ expect(ctx().connection).toBeNull();
1576
+ expect(ctx().connectionClickStartHandle).toBeNull();
1577
+ });
1578
+
1579
+ it.each([false, true])('honors application validation during drag and click completion with click mode %s', (connectOnClick) => {
1580
+ const { store, runtime, pointer } = makeRig({ storeOptions: { connectOnClick }, engineOptions: { canConnect: () => ({ valid: false, reason: 'duplicate' }) } });
1581
+ const events: ConnectionEndedEvent[] = [];
1582
+ store.on('connectionEnded', (event) => events.push(event));
1583
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1584
+ if (connectOnClick) {
1585
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1586
+ runtime.send(pointer('pointer.down', { x: 350, y: 5 }, { target: port('b', 'in') }));
1587
+ }
1588
+ runtime.send(pointer('pointer.up', { x: 350, y: 5 }, { target: port('b', 'in') }));
1589
+ expect(events).toHaveLength(1);
1590
+ expect(events[0].status).toBe('invalid');
1591
+ expect(events[0].connection.invalidReason).toBe('duplicate');
1592
+ });
1593
+
1594
+ it.each([false, true])('auto-creates output-to-input edges for reverse gestures with click mode %s', (connectOnClick) => {
1595
+ const { ctx, pointer } = makeRig();
1596
+ const flow = createFlow({ graph: ctx().graph, createEdgeOnConnect: true, store: { connectOnClick } });
1597
+ const runtime = createFlowInputRuntime({ store: flow.internals.store, engine: flow.internals.engine });
1598
+ const events: ConnectionEndedEvent[] = [];
1599
+ flow.on('connectionEnded', (event) => events.push(event));
1600
+ runtime.send(pointer('pointer.down', { x: 350, y: 5 }, { target: port('b', 'in') }));
1601
+ if (connectOnClick) {
1602
+ runtime.send(pointer('pointer.up', { x: 350, y: 5 }, { target: port('b', 'in') }));
1603
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1604
+ }
1605
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1606
+ expect(events).toHaveLength(1);
1607
+ expect(events[0].source).toEqual({ nodeId: 'b', port: 'in' });
1608
+ expect(flow.getContext().graph.edges).toHaveLength(2);
1609
+ expect(flow.getContext().graph.edges[1]).toMatchObject({ sourceId: 'a', sourcePort: 'out', targetId: 'b', targetPort: 'in' });
1610
+ flow.destroy();
1611
+ });
1612
+
1613
+ it('passes normalized reverse candidates to validation while directed validation remains strict', () => {
1614
+ const { engine } = makeRig({ engineOptions: { canConnect: (proposed) => proposed.sourceId === 'a' && proposed.targetId === 'b' } });
1615
+ expect(engine.validateConnectionDetailed({ sourceId: 'b', sourcePort: 'in', targetId: 'a', targetPort: 'out' })).toEqual({ valid: false, reason: 'source-port-direction' });
1616
+ expect(engine.getConnectionCandidate({ x: 56, y: 56 }, { source: { nodeId: 'b', port: 'in' } })).toMatchObject({ nodeId: 'a', portName: 'out', isValid: true });
1617
+ });
1618
+
1619
+ it.each(['deleteNode', 'deleteEntities', 'deleteSelected', 'setGraph', 'cancelSession'] as const)('clears a pending source on %s', (action) => {
1620
+ const { store, runtime, ctx, pointer } = makeRig({ storeOptions: { connectOnClick: true } });
1621
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1622
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1623
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1624
+ if (action === 'deleteNode') store.trigger.deleteNode({ id: 'a' });
1625
+ if (action === 'deleteEntities') store.trigger.deleteEntities({ nodeIds: ['a'] });
1626
+ if (action === 'deleteSelected') { store.trigger.select({ ids: ['a'] }); store.trigger.deleteSelected(); }
1627
+ if (action === 'setGraph') store.trigger.setGraph({ graph: ctx().graph });
1628
+ if (action === 'cancelSession') runtime.cancelSession();
1629
+ expect(ctx().connection).toBeNull();
1630
+ expect(ctx().connectionClickStartHandle).toBeNull();
1631
+ expect(ctx().canvasState).toBe('idle');
1632
+ });
1633
+
1634
+ it('clears pending connections when the controller is destroyed', () => {
1635
+ const { ctx, pointer } = makeRig();
1636
+ const flow = createFlow({ graph: ctx().graph, store: { connectOnClick: true } });
1637
+ const runtime = createFlowInputRuntime({ store: flow.internals.store, engine: flow.internals.engine });
1638
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1639
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1640
+ expect(flow.getContext().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1641
+ flow.destroy();
1642
+ expect(flow.getContext().connection).toBeNull();
1643
+ expect(flow.getContext().connectionClickStartHandle).toBeNull();
1644
+ });
1645
+
1646
+ it.each([false, true])('retains a click without movement with threshold zero and move event %s', (move) => {
1647
+ const { runtime, ctx, pointer } = makeRig({ storeOptions: { connectOnClick: true, connectionDragThreshold: 0 } });
1648
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1649
+ if (move) runtime.send(pointer('pointer.move', { x: 50, y: 50 }, { target: port('a', 'out') }));
1650
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1651
+ expect(ctx().connectionClickStartHandle).toEqual({ nodeId: 'a', port: 'out' });
1652
+ });
1653
+
1654
+ it.each([false, true])('rejects explicit same-direction ports with click mode %s instead of snapping to the node body', (connectOnClick) => {
1655
+ const { store, runtime, pointer } = makeRig({ storeOptions: { connectOnClick } });
1656
+ store.trigger.createNode({ node: createNode({ id: 'c', x: 600, y: 0, ports: [createPort({ name: 'out', direction: 'out', side: 'top', x: 50, y: 0 })] }) });
1657
+ const events: ConnectionEndedEvent[] = [];
1658
+ store.on('connectionEnded', (event) => events.push(event));
1659
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1660
+ if (connectOnClick) {
1661
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1662
+ runtime.send(pointer('pointer.down', { x: 650, y: 5 }, { target: port('c', 'out') }));
1663
+ }
1664
+ runtime.send(pointer('pointer.up', { x: 650, y: 5 }, { target: port('c', 'out') }));
1665
+ expect(events[0].status).toBe('invalid');
1666
+ expect(events[0].connection.invalidReason).toBe('target-port-direction');
1667
+ });
1668
+
1669
+ it.each([
1670
+ { connectOnClick: false, removed: 'a' },
1671
+ { connectOnClick: false, removed: 'b' },
1672
+ { connectOnClick: true, removed: 'a' },
1673
+ { connectOnClick: true, removed: 'b' },
1674
+ ])('cancels live named endpoints after removing ports from $removed with click mode $connectOnClick', ({ connectOnClick, removed }) => {
1675
+ const { ctx, pointer } = makeRig();
1676
+ const flow = createFlow({ graph: ctx().graph, createEdgeOnConnect: true, store: { connectOnClick } });
1677
+ const store = flow.internals.store;
1678
+ const runtime = createFlowInputRuntime({ store, engine: flow.internals.engine });
1679
+ const events: ConnectionEndedEvent[] = [];
1680
+ flow.on('connectionEnded', (event) => events.push(event));
1681
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }));
1682
+ if (connectOnClick) runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target: port('a', 'out') }));
1683
+ runtime.send(pointer('pointer.move', { x: 350, y: 5 }, { target: port('b', 'in') }));
1684
+ expect(flow.getContext().connection?.isValid).toBe(true);
1685
+ store.trigger.updateNode({ id: removed, patch: { ports: [] } });
1686
+ expect(flow.getContext().connection).toBeNull();
1687
+ expect(flow.getContext().connectionClickStartHandle).toBeNull();
1688
+ expect(flow.getContext().canvasState).toBe('idle');
1689
+ runtime.send(pointer('pointer.up', { x: 350, y: 5 }, { target: port('b', 'in') }));
1690
+ expect(flow.getContext().graph.edges).toHaveLength(1);
1691
+ expect(events).toHaveLength(0);
1692
+ flow.destroy();
1693
+ });
1694
+
1695
+ it('keeps body connections live when unrelated named ports are removed', () => {
1696
+ const { ctx } = makeRig();
1697
+ const flow = createFlow({ graph: ctx().graph, createEdgeOnConnect: true });
1698
+ const store = flow.internals.store;
1699
+ store.trigger.connectionStart({ sourceId: 'a', sourcePort: null, position: { x: 0, y: 0 }, active: true });
1700
+ store.trigger.connectionMove({ position: { x: 350, y: 20 }, targetId: 'b', targetPort: null, isValid: true });
1701
+ store.trigger.updateNode({ id: 'a', patch: { ports: [] } });
1702
+ store.trigger.updateNode({ id: 'b', patch: { ports: [] } });
1703
+ expect(flow.getContext().connection).toMatchObject({ sourceId: 'a', sourcePort: null, targetId: 'b', targetPort: null });
1704
+ store.trigger.connectionEnd({ target: { kind: 'node', nodeId: 'b', port: null } });
1705
+ expect(flow.getContext().graph.edges).toHaveLength(2);
1706
+ expect(flow.getContext().graph.edges[1]).toMatchObject({ sourceId: 'a', targetId: 'b' });
1707
+ flow.destroy();
1708
+ });
1709
+
1710
+ it('prunes removed port selection and focus through the shared node-update boundary', () => {
1711
+ const { store, ctx } = makeRig();
1712
+ const selections: Set<string>[] = [];
1713
+ store.trigger.select({ ids: ['a:out', 'b'] });
1714
+ store.trigger.focus({ entityId: 'a:out' });
1715
+ store.on('selectionChanged', (event) => selections.push(event.selection));
1716
+ store.trigger.updateNode({ id: 'a', patch: { ports: [] } });
1717
+ expect([...ctx().selection]).toEqual(['b']);
1718
+ expect(ctx().focusedEntityId).toBeNull();
1719
+ expect(selections).toHaveLength(1);
1720
+ expect([...selections[0]]).toEqual(['b']);
1721
+ });
1722
+
1723
+ it('rejects missing named ports without rejecting body endpoints or wildcard ports', () => {
1724
+ const { store, engine } = makeRig();
1725
+ store.trigger.updateNode({ id: 'a', patch: { ports: [] } });
1726
+ expect(engine.validateConnectionDetailed({ sourceId: 'a', sourcePort: 'out', targetId: 'b', targetPort: 'in' })).toEqual({ valid: false, reason: 'missing-source-port' });
1727
+ expect(engine.validateConnectionDetailed({ sourceId: 'a', targetId: 'b', targetPort: 'in' })).toEqual({ valid: true });
1728
+ store.trigger.updateNode({ id: 'b', patch: { ports: [] } });
1729
+ expect(engine.validateConnectionDetailed({ sourceId: 'a', targetId: 'b', targetPort: 'in' })).toEqual({ valid: false, reason: 'missing-target-port' });
1730
+ expect(engine.validateConnectionDetailed({ sourceId: 'a', targetId: 'b' })).toEqual({ valid: true });
1731
+ expect(engine.validateConnectionDetailed({ sourceId: 'a', sourcePort: '*', targetId: 'b', targetPort: '*' })).toEqual({ valid: true });
1732
+ });
1733
+
1734
+ it.each(['cancelSession', 'Escape'] as const)('clears pending connections when %s cancels a temporary middle-button pan', (cancel) => {
1735
+ const { runtime, ctx, pointer, key } = makeRig({ storeOptions: { connectOnClick: true } });
1736
+ const target = port('a', 'out');
1737
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target }));
1738
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target }));
1739
+ expect(ctx().connectionClickStartHandle).not.toBeNull();
1740
+ runtime.send(pointer('pointer.down', { x: 200, y: 200 }, { button: 1 }));
1741
+ runtime.send(pointer('pointer.move', { x: 230, y: 250 }));
1742
+ expect(runtime.getActiveSession()).toBe('pan');
1743
+ if (cancel === 'cancelSession') runtime.cancelSession();
1744
+ else runtime.send(key('key.down', 'Escape'));
1745
+ expect(runtime.getActiveSession()).toBeNull();
1746
+ expect(ctx().canvasState).toBe('idle');
1747
+ expect(ctx().connection).toBeNull();
1748
+ expect(ctx().connectionClickStartHandle).toBeNull();
1749
+ expect(ctx().viewport).toEqual({ x: 0, y: 0, zoom: 1 });
1750
+ });
1751
+
1752
+ it('cancels a pending connection after a completed temporary pan without clearing selection', () => {
1753
+ const { store, runtime, ctx, pointer, key } = makeRig({ storeOptions: { connectOnClick: true } });
1754
+ store.trigger.select({ ids: ['a', 'b'] });
1755
+ store.trigger.focus({ entityId: 'a' });
1756
+ const target = port('a', 'out');
1757
+ runtime.send(pointer('pointer.down', { x: 50, y: 50 }, { target }));
1758
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target }));
1759
+ runtime.send(pointer('pointer.down', { x: 200, y: 200 }, { button: 1 }));
1760
+ runtime.send(pointer('pointer.move', { x: 230, y: 250 }));
1761
+ runtime.send(pointer('pointer.up', { x: 230, y: 250 }));
1762
+ expect(runtime.getActiveSession()).toBeNull();
1763
+ expect(ctx().connectionClickStartHandle).not.toBeNull();
1764
+ runtime.send(key('key.down', 'Escape'));
1765
+ expect(ctx().connection).toBeNull();
1766
+ expect(ctx().connectionClickStartHandle).toBeNull();
1767
+ expect([...ctx().selection]).toEqual(['a', 'b']);
1768
+ expect(ctx().focusedEntityId).toBe('a');
1769
+ });
1770
+
1771
+ it('selects and focuses an inspection port without starting a connection', () => {
1772
+ const { store, runtime, ctx, pointer } = makeRig();
1773
+ const clicks: string[] = [];
1774
+ store.on('entityClicked', (event) => clicks.push(event.id));
1775
+ const target: FlowInputTarget = {
1776
+ kind: 'port',
1777
+ nodeId: 'a',
1778
+ portName: 'out',
1779
+ interaction: 'inspect',
1780
+ };
1781
+
1782
+ runtime.send(
1783
+ pointer('pointer.down', { x: 50, y: 50 }, { target }),
1784
+ );
1785
+ expect(ctx().canvasState).toBe('idle');
1786
+ expect(ctx().connection).toBeNull();
1787
+ expect([...ctx().selection]).toEqual(['a:out']);
1788
+ expect(ctx().focusedEntityId).toBe('a:out');
1789
+
1790
+ runtime.send(pointer('pointer.up', { x: 50, y: 50 }, { target }));
1791
+ expect(clicks).toEqual(['a:out']);
1792
+ });
1793
+
1794
+ it('adds an inspection port to the selection with a multi-select modifier', () => {
1795
+ const { store, runtime, ctx, pointer } = makeRig();
1796
+ store.trigger.select({ ids: ['b'] });
1797
+ const target: FlowInputTarget = {
1798
+ kind: 'port',
1799
+ nodeId: 'a',
1800
+ portName: 'out',
1801
+ interaction: 'inspect',
1802
+ };
1803
+
1804
+ runtime.send(
1805
+ pointer('pointer.down', { x: 50, y: 50 }, {
1806
+ target,
1807
+ modifiers: { shift: true },
1808
+ }),
1809
+ );
1810
+
1811
+ expect([...ctx().selection]).toEqual(['b', 'a:out']);
1812
+ });
1813
+
1814
+ it('magnet-snaps empty-space connection moves to the nearest valid port', () => {
1815
+ const { runtime, ctx, pointer } = makeRig();
1816
+ runtime.send(
1817
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1818
+ );
1819
+
1820
+ runtime.send(pointer('pointer.move', { x: 350, y: 20 }));
1821
+ expect(ctx().connection?.targetId).toBe('b');
1822
+ expect(ctx().connection?.targetPort).toBe('in');
1823
+ expect(ctx().connection?.pointerPosition).toEqual({ x: 356, y: 6 });
1824
+ expect(ctx().connection?.isValid).toBe(true);
1825
+
1826
+ runtime.send(pointer('pointer.up', { x: 350, y: 20 }));
1827
+ expect(ctx().connection).toBeNull();
1828
+ });
1829
+
1830
+ it('leaves connection dangling outside the magnet radius', () => {
1831
+ const { runtime, ctx, pointer } = makeRig();
1832
+ runtime.send(
1833
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1834
+ );
1835
+
1836
+ runtime.send(pointer('pointer.move', { x: 260, y: 120 }));
1837
+ expect(ctx().connection?.targetId).toBeNull();
1838
+ expect(ctx().connection?.targetPort).toBeNull();
1839
+ expect(ctx().connection?.pointerPosition).toEqual({ x: 260, y: 120 });
1840
+ });
1841
+
1842
+ it('strict connection mode requires a port and does not accept node-body fallback', () => {
1843
+ const { store, runtime, pointer } = makeRig({
1844
+ engineOptions: { connection: { mode: 'strict' } },
1845
+ });
1846
+
1847
+ runtime.send(
1848
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1849
+ );
1850
+ runtime.send(pointer('pointer.move', { x: 390, y: 45 }, { target: node('b') }));
1851
+
1852
+ expect(store.getSnapshot().context.connection?.targetId).toBe('b');
1853
+ expect(store.getSnapshot().context.connection?.targetPort).toBeNull();
1854
+ expect(store.getSnapshot().context.connection?.isValid).toBe(false);
1855
+ });
1856
+
1857
+ it('emits connectionEnded with the drop target', () => {
1858
+ const { store, runtime, pointer } = makeRig();
1859
+ const events: any[] = [];
1860
+ store.on('connectionEnded', (event) => events.push(event));
1861
+
1862
+ runtime.send(
1863
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1864
+ );
1865
+ runtime.send(
1866
+ pointer('pointer.move', { x: 350, y: 5 }, { target: port('b', 'in') }),
1867
+ );
1868
+ runtime.send(
1869
+ pointer('pointer.up', { x: 350, y: 5 }, { target: port('b', 'in') }),
1870
+ );
1871
+
1872
+ expect(events).toHaveLength(1);
1873
+ expect(events[0].source).toEqual({ nodeId: 'a', port: 'out' });
1874
+ expect(events[0].target).toEqual({ kind: 'node', nodeId: 'b', port: 'in' });
1875
+ });
1876
+
1877
+ it('dropping on the pane reports a canvas target with the drop position', () => {
1878
+ const { store, runtime, pointer } = makeRig();
1879
+ const events: any[] = [];
1880
+ store.on('connectionEnded', (event) => events.push(event));
1881
+
1882
+ runtime.send(
1883
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1884
+ );
1885
+ runtime.send(pointer('pointer.move', { x: 500, y: 400 }));
1886
+ runtime.send(pointer('pointer.up', { x: 500, y: 400 }));
1887
+
1888
+ expect(events[0].target).toEqual({
1889
+ kind: 'canvas',
1890
+ position: { x: 500, y: 400 },
1891
+ });
1892
+ });
1893
+
1894
+ it('default validity rejects same-direction ports and the source node', () => {
1895
+ const { runtime, ctx, pointer } = makeRig();
1896
+ runtime.send(
1897
+ pointer('pointer.down', { x: 350, y: 5 }, { target: port('b', 'in') }),
1898
+ );
1899
+ // dragging FROM an 'in' port to another 'in'-less target on same node
1900
+ runtime.send(
1901
+ pointer('pointer.move', { x: 350, y: 25 }, { target: node('b') }),
1902
+ );
1903
+ expect(ctx().connection?.isValid).toBe(false);
1904
+ runtime.send(pointer('pointer.up', { x: 350, y: 25 }, { target: node('b') }));
1905
+ });
1906
+
1907
+ it('a custom isValid override is consulted instead of the default', () => {
1908
+ const { store, engine, pointer } = makeRig();
1909
+ const runtime = createFlowInputRuntime({
1910
+ store,
1911
+ engine,
1912
+ modes: [
1913
+ ...defaultInputModes.filter((m) => m.id !== 'connect'),
1914
+ connectMode({ isValid: () => true }),
1915
+ ],
1916
+ });
1917
+ runtime.send(
1918
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1919
+ );
1920
+ runtime.send(pointer('pointer.move', { x: 500, y: 400 }));
1921
+ expect(store.getSnapshot().context.connection?.isValid).toBe(true);
1922
+ runtime.send(pointer('pointer.up', { x: 500, y: 400 }));
1923
+ });
1924
+
1925
+ it('Escape mid-connection cancels; release emits nothing', () => {
1926
+ const { store, runtime, ctx, pointer, key } = makeRig();
1927
+ const events: any[] = [];
1928
+ store.on('connectionEnded', (event) => events.push(event));
1929
+
1930
+ runtime.send(
1931
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1932
+ );
1933
+ runtime.send(key('key.down', 'Escape'));
1934
+ expect(ctx().connection).toBeNull();
1935
+
1936
+ runtime.send(pointer('pointer.up', { x: 60, y: 60 }));
1937
+ expect(events).toHaveLength(0);
1938
+ expect(runtime.getActiveSession()).toBeNull();
1939
+ });
1940
+
1941
+ it('readonly mode does not start connections from ports', () => {
1942
+ const { store, runtime, ctx, pointer } = makeRig();
1943
+ store.trigger.setMode({ mode: 'readonly' });
1944
+ runtime.send(
1945
+ pointer('pointer.down', { x: 50, y: 50 }, { target: port('a', 'out') }),
1946
+ );
1947
+ expect(ctx().connection).toBeNull();
1948
+ });
1949
+ });
1950
+
1951
+ describe('input runtime — edge reconnect mode', () => {
1952
+ it('can own a thresholded reconnect gesture without mutating a controlled graph', () => {
1953
+ const { store, engine, ctx, pointer } = makeRig({
1954
+ modes: [
1955
+ reconnectMode({ commit: false, dragThreshold: true, preview: false }),
1956
+ ],
1957
+ });
1958
+ const moved: any[] = [];
1959
+ const ended: any[] = [];
1960
+ store.on('connectMoved', (event) => moved.push(event));
1961
+ store.on('connectionEnded', (event) => ended.push(event));
1962
+
1963
+ const runtime = createFlowInputRuntime({
1964
+ store,
1965
+ engine,
1966
+ modes: [
1967
+ reconnectMode({ commit: false, dragThreshold: true, preview: false }),
1968
+ ],
1969
+ });
1970
+ runtime.send(
1971
+ pointer('pointer.down', { x: 300, y: 25 }, {
1972
+ target: edgeEndpoint('e1', 'target'),
1973
+ }),
1974
+ );
1975
+
1976
+ expect(ctx().connection?.active).toBe(false);
1977
+ runtime.send(pointer('pointer.move', { x: 302, y: 25 }));
1978
+ expect(ctx().connection?.active).toBe(false);
1979
+ expect(moved).toHaveLength(0);
1980
+
1981
+ runtime.send(
1982
+ pointer('pointer.move', { x: 304, y: 25 }, { target: node('pinned') }),
1983
+ );
1984
+ expect(ctx().connection?.active).toBe(true);
1985
+ expect(moved).toHaveLength(1);
1986
+ expect(engine.getRenderableEntities().edges.map((edge) => edge.id)).toContain('e1');
1987
+ expect(
1988
+ createConnectionPreviewGraph(ctx().graph, ctx().connection).edges.some(
1989
+ (edge) => edge.id === 'flow:connection-preview-edge',
1990
+ ),
1991
+ ).toBe(false);
1992
+
1993
+ runtime.send(
1994
+ pointer('pointer.up', { x: 50, y: 225 }, { target: node('pinned') }),
1995
+ );
1996
+
1997
+ expect(ctx().graph.edges.find((edge) => edge.id === 'e1')).toMatchObject({
1998
+ sourceId: 'a',
1999
+ targetId: 'b',
2000
+ });
2001
+ expect(ended).toHaveLength(1);
2002
+ expect(ended[0]).toMatchObject({
2003
+ status: 'connected',
2004
+ reconnectedEdgeId: 'e1',
2005
+ target: { kind: 'node', nodeId: 'pinned', port: null },
2006
+ });
2007
+ });
2008
+
2009
+ it('reports thresholded reconnect click and pointer cancellation without mutation', () => {
2010
+ const { store, engine, ctx, pointer } = makeRig();
2011
+ const ended: any[] = [];
2012
+ store.on('connectionEnded', (event) => ended.push(event));
2013
+ const runtime = createFlowInputRuntime({
2014
+ store,
2015
+ engine,
2016
+ modes: [
2017
+ reconnectMode({ commit: false, dragThreshold: true, preview: false }),
2018
+ ],
2019
+ });
2020
+
2021
+ runtime.send(
2022
+ pointer('pointer.down', { x: 300, y: 25 }, {
2023
+ target: edgeEndpoint('e1', 'target'),
2024
+ }),
2025
+ );
2026
+ runtime.send(pointer('pointer.up', { x: 300, y: 25 }));
2027
+ expect(ended.at(-1)).toMatchObject({
2028
+ status: 'cancelled',
2029
+ reconnectedEdgeId: 'e1',
2030
+ });
2031
+
2032
+ runtime.send(
2033
+ pointer('pointer.down', { x: 300, y: 25 }, {
2034
+ target: edgeEndpoint('e1', 'target'),
2035
+ }),
2036
+ );
2037
+ runtime.send(pointer('pointer.move', { x: 320, y: 25 }));
2038
+ runtime.send(pointer('pointer.cancel', { x: 320, y: 25 }));
2039
+ expect(ended.at(-1)).toMatchObject({
2040
+ status: 'cancelled',
2041
+ reconnectedEdgeId: 'e1',
2042
+ });
2043
+ expect(ctx().graph.edges.find((edge) => edge.id === 'e1')?.targetId).toBe('b');
2044
+ });
2045
+
2046
+ it('reconnects an edge target, preserves edge id, and records one undo step', () => {
2047
+ const { store, engine, runtime, ctx, pointer } = makeRig();
2048
+
2049
+ runtime.send(
2050
+ pointer('pointer.down', { x: 300, y: 25 }, {
2051
+ target: edgeEndpoint('e1', 'target'),
2052
+ }),
2053
+ );
2054
+
2055
+ expect(ctx().connection?.reconnectingEdgeId).toBe('e1');
2056
+ expect(engine.getRenderableEntities().edges.map((edge) => edge.id)).not.toContain('e1');
2057
+ expect(
2058
+ createConnectionPreviewGraph(ctx().graph, ctx().connection).edges.some(
2059
+ (edge) => edge.id === 'flow:connection-preview-edge',
2060
+ ),
2061
+ ).toBe(true);
2062
+
2063
+ runtime.send(
2064
+ pointer('pointer.move', { x: 50, y: 225 }, { target: node('pinned') }),
2065
+ );
2066
+ expect(ctx().connection?.isValid).toBe(true);
2067
+ runtime.send(
2068
+ pointer('pointer.up', { x: 50, y: 225 }, { target: node('pinned') }),
2069
+ );
2070
+
2071
+ const edge = ctx().graph.edges.find((candidate) => candidate.id === 'e1')!;
2072
+ expect(edge.sourceId).toBe('a');
2073
+ expect(edge.targetId).toBe('pinned');
2074
+ expect(ctx().connection).toBeNull();
2075
+
2076
+ store.trigger.undo();
2077
+ expect(ctx().graph.edges.find((candidate) => candidate.id === 'e1')?.targetId).toBe('b');
2078
+ });
2079
+
2080
+ it('reconnects to a derived group through its configured child-surface boundary', () => {
2081
+ const { store, engine, runtime, ctx, pointer } = makeRig();
2082
+ const ended: any[] = [];
2083
+ store.on('connectionEnded', (event) => ended.push(event));
2084
+ store.trigger.setGraph({
2085
+ graph: {
2086
+ ...ctx().graph,
2087
+ nodes: [
2088
+ ...ctx().graph.nodes.map((candidate) =>
2089
+ candidate.id === 'pinned'
2090
+ ? {
2091
+ ...candidate,
2092
+ group: {
2093
+ type: 'derived' as const,
2094
+ padding: 32,
2095
+ minChildArea: { width: 240, height: 180 },
2096
+ connectionBoundaryWidth: 10,
2097
+ },
2098
+ }
2099
+ : candidate,
2100
+ ),
2101
+ createNode({
2102
+ id: 'child',
2103
+ parentId: 'pinned',
2104
+ x: 20,
2105
+ y: 300,
2106
+ width: 40,
2107
+ height: 40,
2108
+ }),
2109
+ ],
2110
+ },
2111
+ });
2112
+ const geometry = engine.getGroupGeometry('pinned')!;
2113
+ const point = {
2114
+ x: geometry.bounds.x + 5,
2115
+ y:
2116
+ geometry.contentBounds.y +
2117
+ geometry.contentBounds.height +
2118
+ (geometry.bounds.y +
2119
+ geometry.bounds.height -
2120
+ geometry.contentBounds.y -
2121
+ geometry.contentBounds.height) /
2122
+ 2,
2123
+ };
2124
+
2125
+ runtime.send(
2126
+ pointer('pointer.down', { x: 300, y: 25 }, {
2127
+ target: edgeEndpoint('e1', 'target'),
2128
+ }),
2129
+ );
2130
+ runtime.send(pointer('pointer.move', point));
2131
+ runtime.send(pointer('pointer.up', point));
2132
+
2133
+ expect(ctx().graph.edges.find((candidate) => candidate.id === 'e1')).toMatchObject({
2134
+ sourceId: 'a',
2135
+ targetId: 'pinned',
2136
+ });
2137
+ expect(ended[0]).toMatchObject({
2138
+ status: 'connected',
2139
+ target: { kind: 'node', nodeId: 'pinned', port: null },
2140
+ });
2141
+ });
2142
+
2143
+ it('reconnects an edge source using the real edge direction for validation', () => {
2144
+ const { runtime, ctx, pointer } = makeRig();
2145
+
2146
+ runtime.send(
2147
+ pointer('pointer.down', { x: 0, y: 25 }, {
2148
+ target: edgeEndpoint('e1', 'source'),
2149
+ }),
2150
+ );
2151
+ runtime.send(
2152
+ pointer('pointer.move', { x: 50, y: 225 }, { target: node('pinned') }),
2153
+ );
2154
+ runtime.send(
2155
+ pointer('pointer.up', { x: 50, y: 225 }, { target: node('pinned') }),
2156
+ );
2157
+
2158
+ const edge = ctx().graph.edges.find((candidate) => candidate.id === 'e1')!;
2159
+ expect(edge.sourceId).toBe('pinned');
2160
+ expect(edge.targetId).toBe('b');
2161
+ });
2162
+
2163
+ it('drop on empty space cancels without changing the edge', () => {
2164
+ const { store, runtime, ctx, pointer } = makeRig();
2165
+
2166
+ runtime.send(
2167
+ pointer('pointer.down', { x: 300, y: 25 }, {
2168
+ target: edgeEndpoint('e1', 'target'),
2169
+ }),
2170
+ );
2171
+ runtime.send(pointer('pointer.move', { x: 500, y: 400 }));
2172
+ runtime.send(pointer('pointer.up', { x: 500, y: 400 }));
2173
+
2174
+ expect(ctx().graph.edges.find((candidate) => candidate.id === 'e1')?.targetId).toBe('b');
2175
+ store.trigger.undo();
2176
+ expect(ctx().graph.edges.find((candidate) => candidate.id === 'e1')?.targetId).toBe('b');
2177
+ });
2178
+
2179
+ it('reconnectable false falls through to normal edge selection', () => {
2180
+ const { store, runtime, ctx, pointer } = makeRig();
2181
+ store.trigger.updateEdge({ id: 'e1', patch: { reconnectable: false } });
2182
+
2183
+ const effects = runtime.send(
2184
+ pointer('pointer.down', { x: 300, y: 25 }, {
2185
+ target: edgeEndpoint('e1', 'target'),
2186
+ }),
2187
+ );
2188
+
2189
+ expect(effects.handledBy).toBe('entity-press');
2190
+ expect(ctx().connection).toBeNull();
2191
+ expect([...ctx().selection]).toEqual(['e1']);
2192
+ });
2193
+
2194
+ it('Escape cancels reconnect cleanly', () => {
2195
+ const { runtime, ctx, pointer, key } = makeRig();
2196
+
2197
+ runtime.send(
2198
+ pointer('pointer.down', { x: 300, y: 25 }, {
2199
+ target: edgeEndpoint('e1', 'target'),
2200
+ }),
2201
+ );
2202
+ runtime.send(pointer('pointer.move', { x: 50, y: 225 }, { target: node('pinned') }));
2203
+ runtime.send(key('key.down', 'Escape'));
2204
+ expect(ctx().connection).toBeNull();
2205
+
2206
+ runtime.send(pointer('pointer.up', { x: 50, y: 225 }, { target: node('pinned') }));
2207
+ expect(ctx().graph.edges.find((candidate) => candidate.id === 'e1')?.targetId).toBe('b');
2208
+ });
2209
+ });
2210
+
2211
+ describe('input runtime — group connection boundary', () => {
2212
+ it('can target a group connection boundary without selecting its body', () => {
2213
+ const { runtime, ctx, pointer } = makeRig();
2214
+ runtime.send(
2215
+ pointer('pointer.down', { x: 50, y: 225 }, {
2216
+ target: {
2217
+ kind: 'entity',
2218
+ id: 'pinned',
2219
+ part: 'connection-boundary',
2220
+ entityHasDragHandle: true,
2221
+ pressedDragHandle: false,
2222
+ },
2223
+ }),
2224
+ );
2225
+
2226
+ expect(ctx().selection.has('pinned')).toBe(false);
2227
+ });
2228
+ });
2229
+
2230
+ describe('input runtime — auto-pan helper', () => {
2231
+ it('returns a delta only while dragging/connecting near the edge', async () => {
2232
+ const { store, ctx } = makeRig();
2233
+ const { getAutoPanDelta } = await import('./index');
2234
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
2235
+
2236
+ // idle → null even at the edge
2237
+ expect(getAutoPanDelta(ctx(), { x: 2, y: 300 })).toBeNull();
2238
+
2239
+ store.trigger.select({ ids: ['a'] });
2240
+ store.trigger.dragStart({ id: 'a' });
2241
+
2242
+ // dragging at the left edge → leftward delta
2243
+ const delta = getAutoPanDelta(ctx(), { x: 2, y: 300 });
2244
+ expect(delta).not.toBeNull();
2245
+ expect(delta!.x).toBeLessThan(0);
2246
+
2247
+ // dragging in the middle → null
2248
+ expect(getAutoPanDelta(ctx(), { x: 400, y: 300 })).toBeNull();
2249
+
2250
+ // disabled option → null
2251
+ store.trigger.setAutoPanOptions({ autoPanOnDrag: false });
2252
+ expect(getAutoPanDelta(ctx(), { x: 2, y: 300 })).toBeNull();
2253
+ });
2254
+ });
2255
+
2256
+ describe('input runtime — profile-driven modes', () => {
2257
+ it('store profile input modes are resolved per store mode', async () => {
2258
+ const { profileModesForContext, createFlowInputRuntime, panePressMode } =
2259
+ await import('./index');
2260
+ const { defineFlowProfile } = await import('../profile');
2261
+
2262
+ let customStarts = 0;
2263
+ const customMode = {
2264
+ id: 'custom',
2265
+ priority: 100,
2266
+ start: (event: { kind: string }) => {
2267
+ if (event.kind !== 'pointer.down') return null;
2268
+ customStarts++;
2269
+ return { commands: [] };
2270
+ },
2271
+ };
2272
+ const profile = defineFlowProfile({
2273
+ defaultMode: 'interactive',
2274
+ modes: {
2275
+ interactive: { input: { modes: [customMode, panePressMode()] } },
2276
+ readonly: { input: { modes: [panePressMode()] } },
2277
+ },
2278
+ });
2279
+
2280
+ const { createFlowStore } = await import('../store');
2281
+ const { createFlowEngine } = await import('../engine');
2282
+ const store = createFlowStore(
2283
+ createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }),
2284
+ { profile },
2285
+ );
2286
+ const engine = createFlowEngine(store);
2287
+ const runtime = createFlowInputRuntime({
2288
+ store,
2289
+ engine,
2290
+ modes: profileModesForContext(profile),
2291
+ });
2292
+
2293
+ const down: FlowInputEvent = {
2294
+ kind: 'pointer.down',
2295
+ pointerId: 1,
2296
+ pointerType: 'mouse',
2297
+ button: 0,
2298
+ point: { x: 0, y: 0 },
2299
+ screenPoint: { x: 0, y: 0 },
2300
+ target: { kind: 'pane' },
2301
+ modifiers: NO_MODS,
2302
+ };
2303
+ runtime.send(down);
2304
+ expect(customStarts).toBe(1);
2305
+ runtime.send({ ...down, kind: 'pointer.up' } as FlowInputEvent);
2306
+
2307
+ store.trigger.setMode({ mode: 'readonly' });
2308
+ runtime.send(down);
2309
+ expect(customStarts).toBe(1); // readonly set has no custom mode
2310
+ runtime.send({ ...down, kind: 'pointer.up' } as FlowInputEvent);
2311
+ });
2312
+ });
2313
+
2314
+ describe('input runtime — drag handles', () => {
2315
+ it('entities declaring a drag handle only drag from it (still selectable anywhere)', () => {
2316
+ const { runtime, ctx, pointer } = makeRig();
2317
+ const handleTarget: FlowInputTarget = {
2318
+ kind: 'entity',
2319
+ id: 'a',
2320
+ entityHasDragHandle: true,
2321
+ pressedDragHandle: false,
2322
+ };
2323
+
2324
+ // press outside the handle: selects, never drags
2325
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: handleTarget }));
2326
+ runtime.send(pointer('pointer.move', { x: 60, y: 10 }));
2327
+ expect([...ctx().selection]).toEqual(['a']);
2328
+ expect(ctx().canvasState).toBe('idle');
2329
+ expect(ctx().graph.nodes[0].dx ?? 0).toBe(0);
2330
+ runtime.send(pointer('pointer.up', { x: 60, y: 10 }));
2331
+
2332
+ // press inside the handle: drags normally
2333
+ runtime.send(
2334
+ pointer('pointer.down', { x: 10, y: 10 }, {
2335
+ target: { ...handleTarget, pressedDragHandle: true },
2336
+ }),
2337
+ );
2338
+ runtime.send(pointer('pointer.move', { x: 60, y: 10 }));
2339
+ expect(ctx().canvasState).toBe('dragging');
2340
+ runtime.send(pointer('pointer.up', { x: 60, y: 10 }));
2341
+ expect(ctx().graph.nodes[0].x).toBe(50);
2342
+ });
2343
+ });
2344
+
2345
+ describe('input runtime — hover mode', () => {
2346
+ it('bare pointer moves track the hovered entity; repeats are free', () => {
2347
+ const { store, runtime, ctx, pointer } = makeRig();
2348
+ runtime.send(pointer('pointer.move', { x: 10, y: 10 }, { target: node('a') }));
2349
+ expect([...ctx().hoveredEntityIds]).toEqual(['a']);
2350
+
2351
+ const before = ctx();
2352
+ runtime.send(pointer('pointer.move', { x: 12, y: 10 }, { target: node('a') }));
2353
+ expect(ctx()).toBe(before); // unchanged hover → unchanged context
2354
+
2355
+ runtime.send(pointer('pointer.move', { x: 310, y: 10 }, { target: node('b') }));
2356
+ expect([...ctx().hoveredEntityIds]).toEqual(['b']);
2357
+
2358
+ runtime.send(pointer('pointer.move', { x: 500, y: 400 }));
2359
+ expect(ctx().hoveredEntityIds.size).toBe(0);
2360
+ void store;
2361
+ });
2362
+
2363
+ it('hover does not update during an active drag session', () => {
2364
+ const { runtime, ctx, pointer } = makeRig();
2365
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, { target: node('a') }));
2366
+ runtime.send(pointer('pointer.move', { x: 310, y: 10 }, { target: node('b') }));
2367
+ expect(ctx().hoveredEntityIds.size).toBe(0);
2368
+ runtime.send(pointer('pointer.up', { x: 310, y: 10 }));
2369
+ });
2370
+ });
2371
+
2372
+ describe('input runtime — clamped drags track the virtual pointer', () => {
2373
+ function makeClampRig() {
2374
+ const store = createFlowStore(
2375
+ createGraph({
2376
+ nodes: [
2377
+ createNode({
2378
+ id: 'box',
2379
+ x: 0,
2380
+ y: 0,
2381
+ width: 400,
2382
+ height: 300,
2383
+ group: {
2384
+ type: 'fixed',
2385
+ padding: 10,
2386
+ contentSize: { width: 0, height: 40 },
2387
+ clampChildren: 'both',
2388
+ },
2389
+ }),
2390
+ {
2391
+ ...createNode({ id: 'kid', x: 50, y: 100, width: 80, height: 40 }),
2392
+ parentId: 'box',
2393
+ },
2394
+ ],
2395
+ edges: [],
2396
+ }),
2397
+ { dragThreshold: 3 },
2398
+ );
2399
+ const engine = createFlowEngine(store);
2400
+ const runtime = createFlowInputRuntime({ store, engine });
2401
+ const ctx = () => store.getSnapshot().context;
2402
+ const kid = () => ctx().graph.nodes.find((n) => n.id === 'kid')!;
2403
+ const pointer = (
2404
+ kind: 'pointer.down' | 'pointer.move' | 'pointer.up',
2405
+ point: Point,
2406
+ target?: FlowInputTarget,
2407
+ ): FlowInputEvent => ({
2408
+ kind,
2409
+ pointerId: 1,
2410
+ pointerType: 'mouse',
2411
+ button: kind === 'pointer.down' ? 0 : -1,
2412
+ point,
2413
+ screenPoint: point,
2414
+ target: target ?? { kind: 'pane' },
2415
+ modifiers: NO_MODS,
2416
+ });
2417
+ return { store, runtime, ctx, kid, pointer };
2418
+ }
2419
+
2420
+ it('overshoot is virtual: the node stays pinned until the pointer returns', () => {
2421
+ const { runtime, kid, pointer } = makeClampRig();
2422
+ // child area x: 10..390 → max kid x = 310 (clamped)
2423
+ runtime.send(pointer('pointer.down', { x: 90, y: 120 }, { kind: 'entity', id: 'kid' }));
2424
+ runtime.send(pointer('pointer.move', { x: 1000, y: 120 })); // way past the wall
2425
+ expect(kid().x + (kid().dx ?? 0)).toBe(310); // pinned
2426
+
2427
+ // pointer reverses but is still far beyond the wall → node must NOT move
2428
+ runtime.send(pointer('pointer.move', { x: 700, y: 120 }));
2429
+ expect(kid().x + (kid().dx ?? 0)).toBe(310);
2430
+
2431
+ // pointer back inside the meaningful range → node tracks it exactly:
2432
+ // grab offset was (90 - 50) = 40, so pointer at 240 → node at 200
2433
+ runtime.send(pointer('pointer.move', { x: 240, y: 120 }));
2434
+ expect(kid().x + (kid().dx ?? 0)).toBe(200);
2435
+
2436
+ runtime.send(pointer('pointer.up', { x: 240, y: 120 }));
2437
+ expect(kid().x).toBe(200);
2438
+ });
2439
+ });
2440
+
2441
+ describe('input runtime — derived group snapping', () => {
2442
+ it('commits the same outer-bounds snap shown during the drag', () => {
2443
+ const store = createFlowStore(
2444
+ createGraph({
2445
+ nodes: [
2446
+ createNode({
2447
+ id: 'group',
2448
+ width: 120,
2449
+ height: 40,
2450
+ group: { type: 'derived', padding: 16 },
2451
+ }),
2452
+ {
2453
+ ...createNode({
2454
+ id: 'child',
2455
+ x: 100,
2456
+ y: 200,
2457
+ width: 100,
2458
+ height: 50,
2459
+ }),
2460
+ parentId: 'group',
2461
+ },
2462
+ createNode({
2463
+ id: 'target',
2464
+ x: 300,
2465
+ y: 150,
2466
+ width: 100,
2467
+ height: 50,
2468
+ }),
2469
+ ],
2470
+ edges: [],
2471
+ }),
2472
+ { dragThreshold: 3, enableSnapLines: true, snapThreshold: 8 },
2473
+ );
2474
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
2475
+ const engine = createFlowEngine(store);
2476
+ const runtime = createFlowInputRuntime({ store, engine });
2477
+ const pointer = (
2478
+ kind: 'pointer.down' | 'pointer.move' | 'pointer.up',
2479
+ point: Point,
2480
+ target: FlowInputTarget = { kind: 'pane' },
2481
+ ): FlowInputEvent => ({
2482
+ kind,
2483
+ pointerId: 1,
2484
+ pointerType: 'mouse',
2485
+ button: kind === 'pointer.down' ? 0 : -1,
2486
+ point,
2487
+ screenPoint: point,
2488
+ target,
2489
+ modifiers: NO_MODS,
2490
+ });
2491
+
2492
+ runtime.send(
2493
+ pointer('pointer.down', { x: 100, y: 160 }, node('group')),
2494
+ );
2495
+ runtime.send(pointer('pointer.move', { x: 100, y: 163 }));
2496
+ expect(engine.getSnapResult()?.snapRect.y).toBe(150);
2497
+ expect(engine.visualBounds('group').y).toBe(150);
2498
+
2499
+ runtime.send(pointer('pointer.up', { x: 100, y: 163 }));
2500
+
2501
+ expect(engine.resolvedBounds('group').y).toBe(150);
2502
+ expect(
2503
+ store.getSnapshot().context.graph.nodes.find((n) => n.id === 'child')!
2504
+ .y,
2505
+ ).toBe(206);
2506
+ });
2507
+
2508
+ it('commits the preview snap when selected edge points extend the static bounds', () => {
2509
+ const store = createFlowStore(
2510
+ createGraph({
2511
+ nodes: [
2512
+ createNode({ id: 'moving', x: 0, y: 0, width: 50, height: 50 }),
2513
+ createNode({ id: 'target', x: 105, y: 0, width: 50, height: 50 }),
2514
+ ],
2515
+ edges: [
2516
+ createEdge({
2517
+ id: 'route',
2518
+ sourceId: 'moving',
2519
+ targetId: 'moving',
2520
+ points: [
2521
+ { x: -1000, y: -1000 },
2522
+ { x: 1000, y: 1000 },
2523
+ ],
2524
+ }),
2525
+ ],
2526
+ }),
2527
+ { dragThreshold: 3, enableSnapLines: true, snapThreshold: 10 },
2528
+ );
2529
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
2530
+ store.trigger.select({ ids: ['moving', 'route'] });
2531
+ const engine = createFlowEngine(store);
2532
+ const runtime = createFlowInputRuntime({ store, engine });
2533
+ const pointer = (
2534
+ kind: 'pointer.down' | 'pointer.move' | 'pointer.up',
2535
+ point: Point,
2536
+ target: FlowInputTarget = { kind: 'pane' },
2537
+ ): FlowInputEvent => ({
2538
+ kind,
2539
+ pointerId: 1,
2540
+ pointerType: 'mouse',
2541
+ button: kind === 'pointer.down' ? 0 : -1,
2542
+ point,
2543
+ screenPoint: point,
2544
+ target,
2545
+ modifiers: NO_MODS,
2546
+ });
2547
+
2548
+ runtime.send(pointer('pointer.down', { x: 10, y: 10 }, node('moving')));
2549
+ runtime.send(pointer('pointer.move', { x: 60, y: 10 }));
2550
+ expect(engine.visualBounds('moving').x).toBe(55);
2551
+
2552
+ runtime.send(pointer('pointer.up', { x: 60, y: 10 }));
2553
+ expect(engine.resolvedBounds('moving').x).toBe(55);
2554
+ });
2555
+ });
2556
+
2557
+ describe('input runtime — auto-pan follows the pointer', () => {
2558
+ it('continues panning across frames when the dragged node is away from the edge', async () => {
2559
+ const { getAutoPanDelta } = await import('./index');
2560
+ const { store, engine, ctx } = makeRig();
2561
+ store.trigger.updateViewportSize({ width: 800, height: 600 });
2562
+ store.trigger.select({ ids: ['a'] });
2563
+ store.trigger.dragStart({ id: 'a' });
2564
+
2565
+ // Node 'a' sits at x 0..100 — near the LEFT edge, far from the RIGHT.
2566
+ // A held pointer at the RIGHT edge must still keep panning. This mirrors
2567
+ // the browser ticker, which calls the policy again after each pan.
2568
+ for (let frame = 0; frame < 3; frame += 1) {
2569
+ const delta = getAutoPanDelta(ctx(), { x: 798, y: 300 }, engine);
2570
+ expect(delta).not.toBeNull();
2571
+ expect(delta!.x).toBeGreaterThan(0);
2572
+ store.trigger.panBy({ x: -delta!.x, y: -delta!.y });
2573
+ }
2574
+
2575
+ expect(ctx().viewport.x).toBeLessThan(0);
2576
+ });
2577
+ });