@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,676 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ visualPosition,
4
+ visualBounds,
5
+ resolvedPosition,
6
+ resolvedBounds,
7
+ snappedBounds,
8
+ getGraphBounds,
9
+ getSelectionBounds,
10
+ rectsIntersect,
11
+ rectContains,
12
+ unionRect,
13
+ snapToGrid,
14
+ snapPointToGrid,
15
+ boxToRect,
16
+ selectMode,
17
+ wheelZoomDelta,
18
+ wheelInteraction,
19
+ normalizeWheelPan,
20
+ isTrackpadPinch,
21
+ constrainViewport,
22
+ calcAutoPan,
23
+ } from './spatial';
24
+ import { createNode, createEdge } from './factories';
25
+
26
+
27
+ // resolvedPosition
28
+
29
+
30
+ describe('resolvedPosition', () => {
31
+ it('is a deprecated alias for visualPosition', () => {
32
+ const node = createNode({ id: 'a', x: 100, y: 200, dx: 10, dy: -5 });
33
+ expect(resolvedPosition(node)).toEqual(visualPosition(node));
34
+ });
35
+
36
+ it('returns x/y when no drag offset', () => {
37
+ const node = createNode({ id: 'a', x: 100, y: 200 });
38
+ expect(resolvedPosition(node)).toEqual({ x: 100, y: 200 });
39
+ });
40
+
41
+ it('adds dx/dy to position', () => {
42
+ const node = createNode({ id: 'a', x: 100, y: 200, dx: 10, dy: -5 });
43
+ expect(resolvedPosition(node)).toEqual({ x: 110, y: 195 });
44
+ });
45
+ });
46
+
47
+
48
+ // resolvedBounds
49
+
50
+
51
+ describe('resolvedBounds', () => {
52
+ it('is a deprecated alias for visualBounds', () => {
53
+ const node = createNode({ id: 'a', x: 10, y: 20, dx: 5, dy: 3 });
54
+ expect(resolvedBounds(node)).toEqual(visualBounds(node));
55
+ });
56
+
57
+ it('returns full rect with default dimensions', () => {
58
+ const node = createNode({ id: 'a', x: 10, y: 20, width: 150, height: 50 });
59
+ expect(resolvedBounds(node)).toEqual({ x: 10, y: 20, width: 150, height: 50 });
60
+ });
61
+
62
+ it('includes drag offset', () => {
63
+ const node = createNode({ id: 'a', x: 10, y: 20, dx: 5, dy: 3 });
64
+ const b = resolvedBounds(node);
65
+ expect(b.x).toBe(15);
66
+ expect(b.y).toBe(23);
67
+ });
68
+
69
+ it('uses customWidth/customHeight from style when set', () => {
70
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50, style: { customWidth: 200, customHeight: 80 } });
71
+ const b = resolvedBounds(node);
72
+ expect(b.width).toBe(200);
73
+ expect(b.height).toBe(80);
74
+ });
75
+ });
76
+
77
+
78
+ // snappedBounds
79
+
80
+
81
+ describe('snappedBounds', () => {
82
+ it('returns resolvedBounds when no grid and no snap offset', () => {
83
+ const node = createNode({ id: 'a', x: 13, y: 27 });
84
+ expect(snappedBounds(node, null)).toEqual(resolvedBounds(node));
85
+ });
86
+
87
+ it('applies snap offset', () => {
88
+ const node = createNode({ id: 'a', x: 10, y: 20 });
89
+ const b = snappedBounds(node, null, { x: 3, y: -2 });
90
+ expect(b.x).toBe(13);
91
+ expect(b.y).toBe(18);
92
+ });
93
+
94
+ it('applies grid snapping', () => {
95
+ const node = createNode({ id: 'a', x: 13, y: 27 });
96
+ const b = snappedBounds(node, [10, 10]);
97
+ expect(b.x).toBe(10);
98
+ expect(b.y).toBe(30);
99
+ });
100
+
101
+ it('applies snap offset then grid', () => {
102
+ const node = createNode({ id: 'a', x: 10, y: 20, dx: 3, dy: 2 });
103
+ const b = snappedBounds(node, [10, 10], { x: 1, y: 1 });
104
+ // x = 10+3+1 = 14 → snap to 10; y = 20+2+1 = 23 → snap to 20
105
+ expect(b.x).toBe(10);
106
+ expect(b.y).toBe(20);
107
+ });
108
+ });
109
+
110
+
111
+ // getGraphBounds
112
+
113
+
114
+ describe('getGraphBounds', () => {
115
+ it('returns zero rect for empty graph', () => {
116
+ expect(getGraphBounds([])).toEqual({ x: 0, y: 0, width: 0, height: 0 });
117
+ });
118
+
119
+ it('computes bounds of single node', () => {
120
+ const node = createNode({ id: 'a', x: 10, y: 20, width: 100, height: 50 });
121
+ expect(getGraphBounds([node])).toEqual({ x: 10, y: 20, width: 100, height: 50 });
122
+ });
123
+
124
+ it('computes bounds of multiple nodes', () => {
125
+ const nodes = [
126
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
127
+ createNode({ id: 'b', x: 100, y: 100, width: 50, height: 50 }),
128
+ ];
129
+ expect(getGraphBounds(nodes)).toEqual({ x: 0, y: 0, width: 150, height: 150 });
130
+ });
131
+
132
+ it('includes drag offset in bounds', () => {
133
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50, dx: 100, dy: 100 });
134
+ const b = getGraphBounds([node]);
135
+ expect(b.x).toBe(100);
136
+ expect(b.y).toBe(100);
137
+ });
138
+
139
+ it('includes edge labels in bounds', () => {
140
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 });
141
+ const edge = createEdge({
142
+ id: 'e1',
143
+ sourceId: 'a',
144
+ targetId: 'a',
145
+ label: { x: 200, y: 200, width: 60, height: 20, data: {} },
146
+ });
147
+ const b = getGraphBounds([node], [edge]);
148
+ expect(b.x).toBe(0);
149
+ expect(b.y).toBe(0);
150
+ expect(b.width).toBe(260); // 0..260
151
+ expect(b.height).toBe(220); // 0..220
152
+ });
153
+
154
+ it('includes edge label dx/dy in bounds', () => {
155
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 10, height: 10 });
156
+ const edge = createEdge({
157
+ id: 'e1',
158
+ sourceId: 'a',
159
+ targetId: 'a',
160
+ label: { x: 50, y: 50, dx: 10, dy: 5, width: 20, height: 10, data: {} },
161
+ });
162
+ const b = getGraphBounds([node], [edge]);
163
+ // label resolved: x=60, y=55, to x=80, y=65
164
+ expect(b.x).toBe(0);
165
+ expect(b.y).toBe(0);
166
+ expect(b.width).toBe(80);
167
+ expect(b.height).toBe(65);
168
+ });
169
+
170
+ it('includes authoritative edge points in graph bounds', () => {
171
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 });
172
+ const edge = createEdge({
173
+ id: 'e1',
174
+ sourceId: 'a',
175
+ targetId: 'a',
176
+ points: [{ x: -100, y: 20 }, { x: 200, y: 300 }],
177
+ });
178
+ expect(getGraphBounds([node], [edge])).toEqual({
179
+ x: -100,
180
+ y: 0,
181
+ width: 300,
182
+ height: 300,
183
+ });
184
+ });
185
+
186
+ it.each([
187
+ [[{ x: 1000, y: 1000 }]],
188
+ [[{ x: 1000, y: 1000 }, { x: Number.NaN, y: 0 }]],
189
+ ])('ignores non-authoritative edge points in graph bounds', (points) => {
190
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 });
191
+ const edge = createEdge({
192
+ id: 'e1',
193
+ sourceId: 'a',
194
+ targetId: 'a',
195
+ points,
196
+ });
197
+ expect(getGraphBounds([node], [edge])).toEqual({
198
+ x: 0,
199
+ y: 0,
200
+ width: 50,
201
+ height: 50,
202
+ });
203
+ });
204
+
205
+ it('ignores edges without labels', () => {
206
+ const node = createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 });
207
+ const edge = createEdge({ id: 'e1', sourceId: 'a', targetId: 'a' });
208
+ const b = getGraphBounds([node], [edge]);
209
+ expect(b).toEqual({ x: 0, y: 0, width: 50, height: 50 });
210
+ });
211
+
212
+ it('works with no edges param (backward compat)', () => {
213
+ const node = createNode({ id: 'a', x: 5, y: 5, width: 10, height: 10 });
214
+ expect(getGraphBounds([node])).toEqual({ x: 5, y: 5, width: 10, height: 10 });
215
+ });
216
+ });
217
+
218
+
219
+ // getSelectionBounds
220
+
221
+
222
+ describe('getSelectionBounds', () => {
223
+ it('returns null for empty selection', () => {
224
+ const nodes = [createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 })];
225
+ expect(getSelectionBounds(nodes, [], new Set())).toBeNull();
226
+ });
227
+
228
+ it('computes bounds for selected nodes', () => {
229
+ const nodes = [
230
+ createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
231
+ createNode({ id: 'b', x: 100, y: 100, width: 50, height: 50 }),
232
+ createNode({ id: 'c', x: 500, y: 500, width: 50, height: 50 }),
233
+ ];
234
+ const b = getSelectionBounds(nodes, [], new Set(['a', 'b']));
235
+ expect(b).toEqual({ x: 0, y: 0, width: 150, height: 150 });
236
+ });
237
+
238
+ it('includes selected edge labels', () => {
239
+ const nodes = [createNode({ id: 'a', x: 0, y: 0, width: 10, height: 10 })];
240
+ const edges = [
241
+ createEdge({
242
+ id: 'e1',
243
+ sourceId: 'a',
244
+ targetId: 'a',
245
+ label: { x: 100, y: 100, width: 40, height: 20, data: {} },
246
+ }),
247
+ ];
248
+ const b = getSelectionBounds(nodes, edges, new Set(['e1']));
249
+ // Only edge selected, not node
250
+ expect(b).toEqual({ x: 100, y: 100, width: 40, height: 20 });
251
+ });
252
+
253
+ it('includes authoritative points for a selected edge', () => {
254
+ const edge = createEdge({
255
+ id: 'e1',
256
+ sourceId: 'a',
257
+ targetId: 'a',
258
+ points: [{ x: -100, y: 20 }, { x: 200, y: 300 }],
259
+ });
260
+ expect(
261
+ getSelectionBounds([], [edge], new Set(['e1']), undefined, {
262
+ includeEdgePoints: true,
263
+ }),
264
+ ).toEqual({
265
+ x: -100,
266
+ y: 20,
267
+ width: 300,
268
+ height: 280,
269
+ });
270
+ });
271
+
272
+ it('excludes authoritative points by default for snap-compatible bounds', () => {
273
+ const edge = createEdge({
274
+ id: 'e1',
275
+ sourceId: 'a',
276
+ targetId: 'a',
277
+ points: [{ x: -100, y: 20 }, { x: 200, y: 300 }],
278
+ });
279
+ expect(getSelectionBounds([], [edge], new Set(['e1']))).toBeNull();
280
+ });
281
+
282
+ it.each([
283
+ [[{ x: 1000, y: 1000 }]],
284
+ [[{ x: 1000, y: 1000 }, { x: Number.NaN, y: 0 }]],
285
+ ])('ignores non-authoritative points for a selected edge', (points) => {
286
+ const edge = createEdge({
287
+ id: 'e1',
288
+ sourceId: 'a',
289
+ targetId: 'a',
290
+ points,
291
+ });
292
+ expect(
293
+ getSelectionBounds([], [edge], new Set(['e1']), undefined, {
294
+ includeEdgePoints: true,
295
+ }),
296
+ ).toBeNull();
297
+ });
298
+
299
+ it('combines node + edge label selection', () => {
300
+ const nodes = [createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 })];
301
+ const edges = [
302
+ createEdge({
303
+ id: 'e1',
304
+ sourceId: 'a',
305
+ targetId: 'a',
306
+ label: { x: 200, y: 200, width: 30, height: 20, data: {} },
307
+ }),
308
+ ];
309
+ const b = getSelectionBounds(nodes, edges, new Set(['a', 'e1']));
310
+ expect(b).toEqual({ x: 0, y: 0, width: 230, height: 220 });
311
+ });
312
+
313
+ it('ignores selected edges without labels', () => {
314
+ const nodes = [createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 })];
315
+ const edges = [createEdge({ id: 'e1', sourceId: 'a', targetId: 'a' })];
316
+ const b = getSelectionBounds(nodes, edges, new Set(['a', 'e1']));
317
+ expect(b).toEqual({ x: 0, y: 0, width: 50, height: 50 });
318
+ });
319
+ });
320
+
321
+
322
+ // rectsIntersect
323
+
324
+
325
+ describe('rectsIntersect', () => {
326
+ it('returns true for overlapping rects', () => {
327
+ expect(rectsIntersect(
328
+ { x: 0, y: 0, width: 50, height: 50 },
329
+ { x: 25, y: 25, width: 50, height: 50 },
330
+ )).toBe(true);
331
+ });
332
+
333
+ it('returns false for non-overlapping rects', () => {
334
+ expect(rectsIntersect(
335
+ { x: 0, y: 0, width: 50, height: 50 },
336
+ { x: 100, y: 100, width: 50, height: 50 },
337
+ )).toBe(false);
338
+ });
339
+
340
+ it('returns false for touching edges (not overlapping)', () => {
341
+ expect(rectsIntersect(
342
+ { x: 0, y: 0, width: 50, height: 50 },
343
+ { x: 50, y: 0, width: 50, height: 50 },
344
+ )).toBe(false);
345
+ });
346
+
347
+ it('returns true when one contains the other', () => {
348
+ expect(rectsIntersect(
349
+ { x: 0, y: 0, width: 100, height: 100 },
350
+ { x: 10, y: 10, width: 20, height: 20 },
351
+ )).toBe(true);
352
+ });
353
+ });
354
+
355
+
356
+ // unionRect
357
+
358
+
359
+ describe('unionRect', () => {
360
+ it('returns bounding rect of two disjoint rects', () => {
361
+ expect(unionRect(
362
+ { x: 0, y: 0, width: 10, height: 10 },
363
+ { x: 20, y: 30, width: 10, height: 10 },
364
+ )).toEqual({ x: 0, y: 0, width: 30, height: 40 });
365
+ });
366
+
367
+ it('returns outer rect when one is fully inside the other', () => {
368
+ expect(unionRect(
369
+ { x: 0, y: 0, width: 100, height: 100 },
370
+ { x: 10, y: 10, width: 20, height: 20 },
371
+ )).toEqual({ x: 0, y: 0, width: 100, height: 100 });
372
+ });
373
+
374
+ it('returns the same rect when both inputs are identical', () => {
375
+ expect(unionRect(
376
+ { x: 5, y: 5, width: 50, height: 50 },
377
+ { x: 5, y: 5, width: 50, height: 50 },
378
+ )).toEqual({ x: 5, y: 5, width: 50, height: 50 });
379
+ });
380
+ });
381
+
382
+
383
+ // snapToGrid / snapPointToGrid
384
+
385
+
386
+ describe('snapToGrid', () => {
387
+ it('snaps to nearest grid point', () => {
388
+ expect(snapToGrid(13, 10)).toBe(10);
389
+ expect(snapToGrid(17, 10)).toBe(20);
390
+ expect(snapToGrid(15, 10)).toBe(20); // round half up
391
+ });
392
+
393
+ it('exact grid values pass through', () => {
394
+ expect(snapToGrid(20, 10)).toBe(20);
395
+ });
396
+ });
397
+
398
+ describe('snapPointToGrid', () => {
399
+ it('snaps both x and y', () => {
400
+ expect(snapPointToGrid({ x: 13, y: 27 }, 10)).toEqual({ x: 10, y: 30 });
401
+ });
402
+ });
403
+
404
+
405
+ // boxToRect
406
+
407
+
408
+ describe('boxToRect', () => {
409
+ it('normalizes top-left to bottom-right', () => {
410
+ expect(boxToRect({ x: 10, y: 10 }, { x: 50, y: 50 })).toEqual({
411
+ x: 10, y: 10, width: 40, height: 40,
412
+ });
413
+ });
414
+
415
+ it('normalizes inverted points (bottom-right to top-left)', () => {
416
+ expect(boxToRect({ x: 50, y: 50 }, { x: 10, y: 10 })).toEqual({
417
+ x: 10, y: 10, width: 40, height: 40,
418
+ });
419
+ });
420
+
421
+ it('handles negative coordinates', () => {
422
+ const r = boxToRect({ x: -20, y: -30 }, { x: 10, y: 10 });
423
+ expect(r).toEqual({ x: -20, y: -30, width: 30, height: 40 });
424
+ });
425
+ });
426
+
427
+
428
+ // selectMode
429
+
430
+
431
+ describe('selectMode', () => {
432
+ it('returns single with no modifiers', () => {
433
+ expect(selectMode({})).toBe('single');
434
+ });
435
+
436
+ it('returns multi with shift', () => {
437
+ expect(selectMode({ shiftKey: true })).toBe('multi');
438
+ });
439
+
440
+ it('returns multi with meta', () => {
441
+ expect(selectMode({ metaKey: true })).toBe('multi');
442
+ });
443
+
444
+ it('returns multi with ctrl', () => {
445
+ expect(selectMode({ ctrlKey: true })).toBe('multi');
446
+ });
447
+ });
448
+
449
+
450
+ // rectContains
451
+
452
+
453
+ describe('rectContains', () => {
454
+ it('returns true when a fully contains b', () => {
455
+ expect(rectContains(
456
+ { x: 0, y: 0, width: 100, height: 100 },
457
+ { x: 10, y: 10, width: 20, height: 20 },
458
+ )).toBe(true);
459
+ });
460
+
461
+ it('returns false when b extends outside a', () => {
462
+ expect(rectContains(
463
+ { x: 0, y: 0, width: 50, height: 50 },
464
+ { x: 40, y: 40, width: 20, height: 20 },
465
+ )).toBe(false);
466
+ });
467
+
468
+ it('returns true when rects are identical', () => {
469
+ expect(rectContains(
470
+ { x: 10, y: 10, width: 50, height: 50 },
471
+ { x: 10, y: 10, width: 50, height: 50 },
472
+ )).toBe(true);
473
+ });
474
+
475
+ it('returns false when rects are disjoint', () => {
476
+ expect(rectContains(
477
+ { x: 0, y: 0, width: 10, height: 10 },
478
+ { x: 100, y: 100, width: 10, height: 10 },
479
+ )).toBe(false);
480
+ });
481
+ });
482
+
483
+
484
+ // wheelZoomDelta
485
+
486
+
487
+ describe('wheelZoomDelta', () => {
488
+ it('returns negative for scroll down (zoom out)', () => {
489
+ expect(wheelZoomDelta({ deltaY: 100, deltaMode: 0, ctrlKey: false })).toBeLessThan(0);
490
+ });
491
+
492
+ it('returns positive for scroll up (zoom in)', () => {
493
+ expect(wheelZoomDelta({ deltaY: -100, deltaMode: 0, ctrlKey: false })).toBeGreaterThan(0);
494
+ });
495
+
496
+ it('scales by deltaMode (Firefox line mode vs Chrome pixel mode)', () => {
497
+ const pixel = Math.abs(wheelZoomDelta({ deltaY: 3, deltaMode: 0, ctrlKey: false }));
498
+ const line = Math.abs(wheelZoomDelta({ deltaY: 3, deltaMode: 1, ctrlKey: false }));
499
+ // Line mode should produce ~25x larger delta (0.05/0.002 = 25)
500
+ expect(line / pixel).toBeCloseTo(25);
501
+ });
502
+
503
+ it('produces exponential zoom via Math.pow(2, delta)', () => {
504
+ const delta = wheelZoomDelta({ deltaY: 100, deltaMode: 0, ctrlKey: false });
505
+ const newZoom = 1.0 * Math.pow(2, delta);
506
+ expect(newZoom).toBeGreaterThan(0);
507
+ expect(newZoom).toBeLessThan(1); // scroll down = zoom out
508
+ });
509
+ });
510
+
511
+
512
+ // normalizeWheelPan
513
+
514
+
515
+ describe('normalizeWheelPan', () => {
516
+ it('inverts deltas for natural panning', () => {
517
+ const pan = normalizeWheelPan({ deltaX: 10, deltaY: 20, deltaMode: 0 });
518
+ expect(pan.x).toBe(-10);
519
+ expect(pan.y).toBe(-20);
520
+ });
521
+
522
+ it('scales Firefox line-mode deltas', () => {
523
+ const pixel = normalizeWheelPan({ deltaX: 3, deltaY: 3, deltaMode: 0 });
524
+ const line = normalizeWheelPan({ deltaX: 3, deltaY: 3, deltaMode: 1 });
525
+ expect(Math.abs(line.x)).toBe(Math.abs(pixel.x) * 20);
526
+ });
527
+ });
528
+
529
+
530
+ // isTrackpadPinch
531
+
532
+
533
+ describe('isTrackpadPinch', () => {
534
+ it('detects ctrlKey as pinch', () => {
535
+ expect(isTrackpadPinch({ ctrlKey: true, metaKey: false })).toBe(true);
536
+ });
537
+
538
+ it('detects metaKey as pinch', () => {
539
+ expect(isTrackpadPinch({ ctrlKey: false, metaKey: true })).toBe(true);
540
+ });
541
+
542
+ it('returns false without modifier', () => {
543
+ expect(isTrackpadPinch({ ctrlKey: false, metaKey: false })).toBe(false);
544
+ });
545
+ });
546
+
547
+
548
+ // wheelInteraction
549
+
550
+
551
+ describe('wheelInteraction', () => {
552
+ it('zooms for pinch wheel over the canvas by default', () => {
553
+ expect(wheelInteraction({ ctrlKey: true, metaKey: false })).toBe('zoom');
554
+ });
555
+
556
+ it('blocks native page zoom when pinch zoom is disabled', () => {
557
+ expect(
558
+ wheelInteraction(
559
+ { ctrlKey: true, metaKey: false },
560
+ { zoomOnPinch: false },
561
+ ),
562
+ ).toBe('block');
563
+ });
564
+
565
+ it('blocks pinch wheel while a floating layer is open', () => {
566
+ expect(
567
+ wheelInteraction(
568
+ { ctrlKey: true, metaKey: false },
569
+ { floatingLayerOpen: true },
570
+ ),
571
+ ).toBe('block');
572
+ });
573
+
574
+ it('pans for ordinary wheel only when panOnScroll is enabled', () => {
575
+ expect(
576
+ wheelInteraction(
577
+ { ctrlKey: false, metaKey: false },
578
+ { panOnScroll: true },
579
+ ),
580
+ ).toBe('pan');
581
+ expect(wheelInteraction({ ctrlKey: false, metaKey: false })).toBe(
582
+ 'ignore',
583
+ );
584
+ });
585
+ });
586
+
587
+
588
+ // constrainViewport
589
+
590
+
591
+ describe('constrainViewport', () => {
592
+ const extent: [{ x: number; y: number }, { x: number; y: number }] = [
593
+ { x: 0, y: 0 },
594
+ { x: 1000, y: 1000 },
595
+ ];
596
+
597
+ it('no-ops when viewport is within extent', () => {
598
+ const vp = { x: -100, y: -100, zoom: 1 };
599
+ const result = constrainViewport(vp, { width: 800, height: 600 }, extent);
600
+ expect(result.x).toBe(vp.x);
601
+ expect(result.y).toBe(vp.y);
602
+ });
603
+
604
+ it('clamps viewport panned too far left (positive x)', () => {
605
+ // Panned so canvas origin is way to the right — visible area starts at negative canvas x
606
+ const vp = { x: 500, y: 0, zoom: 1 };
607
+ const result = constrainViewport(vp, { width: 800, height: 600 }, extent);
608
+ // Should push back so visible left edge >= extent left (0)
609
+ expect(result.x).toBeLessThanOrEqual(0);
610
+ });
611
+
612
+ it('centers when zoomed out beyond extent', () => {
613
+ // Zoomed out so 800px viewport shows 8000 canvas units, extent is only 1000
614
+ const vp = { x: 0, y: 0, zoom: 0.1 };
615
+ const result = constrainViewport(vp, { width: 800, height: 600 }, extent);
616
+ // Should center: content (0-1000) centered in visible area (0-8000)
617
+ expect(result.zoom).toBe(0.1);
618
+ });
619
+ });
620
+
621
+
622
+ // calcAutoPan
623
+
624
+
625
+ describe('calcAutoPan', () => {
626
+ const bounds = { width: 800, height: 600 };
627
+
628
+ it('returns zero when cursor is in center', () => {
629
+ const delta = calcAutoPan({ x: 400, y: 300 }, bounds);
630
+ expect(delta.x).toBe(0);
631
+ expect(delta.y).toBe(0);
632
+ });
633
+
634
+ it('returns negative x when near left edge', () => {
635
+ const delta = calcAutoPan({ x: 5, y: 300 }, bounds);
636
+ expect(delta.x).toBeLessThan(0);
637
+ expect(delta.y).toBe(0);
638
+ });
639
+
640
+ it('returns positive x when near right edge', () => {
641
+ const delta = calcAutoPan({ x: 795, y: 300 }, bounds);
642
+ expect(delta.x).toBeGreaterThan(0);
643
+ expect(delta.y).toBe(0);
644
+ });
645
+
646
+ it('returns negative y when near top edge', () => {
647
+ const delta = calcAutoPan({ x: 400, y: 5 }, bounds);
648
+ expect(delta.x).toBe(0);
649
+ expect(delta.y).toBeLessThan(0);
650
+ });
651
+
652
+ it('returns positive y when near bottom edge', () => {
653
+ const delta = calcAutoPan({ x: 400, y: 595 }, bounds);
654
+ expect(delta.x).toBe(0);
655
+ expect(delta.y).toBeGreaterThan(0);
656
+ });
657
+
658
+ it('returns both axes when in corner', () => {
659
+ const delta = calcAutoPan({ x: 5, y: 5 }, bounds);
660
+ expect(delta.x).toBeLessThan(0);
661
+ expect(delta.y).toBeLessThan(0);
662
+ });
663
+
664
+ it('strength increases as cursor approaches edge', () => {
665
+ const far = calcAutoPan({ x: 20, y: 300 }, bounds);
666
+ const close = calcAutoPan({ x: 5, y: 300 }, bounds);
667
+ expect(Math.abs(close.x)).toBeGreaterThan(Math.abs(far.x));
668
+ });
669
+
670
+ it('caps auto-pan when the pointer overshoots the viewport', () => {
671
+ expect(calcAutoPan({ x: -1000, y: 1000 }, bounds, 50, 15)).toEqual({
672
+ x: -15,
673
+ y: 15,
674
+ });
675
+ });
676
+ });