@opendata-ai/openchart-vanilla 8.0.0-rc.2 → 8.0.0-rc.21

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 (145) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/chunk-DCODCLWI.js +234 -0
  3. package/dist/chunk-DCODCLWI.js.map +1 -0
  4. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  5. package/dist/chunk-EKAUQAAX.js.map +1 -0
  6. package/dist/{chunk-ENXSUXC5.js → chunk-KNT5AZ2O.js} +7309 -5490
  7. package/dist/chunk-KNT5AZ2O.js.map +1 -0
  8. package/dist/chunk-RM5XS6NR.js +113 -0
  9. package/dist/chunk-RM5XS6NR.js.map +1 -0
  10. package/dist/chunk-T32UHMZA.js +282 -0
  11. package/dist/chunk-T32UHMZA.js.map +1 -0
  12. package/dist/export-gif.d.ts +113 -0
  13. package/dist/export-gif.js +21 -0
  14. package/dist/export-gif.js.map +1 -0
  15. package/dist/index.d.ts +283 -24
  16. package/dist/index.js +2012 -763
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-Dvv8NYIv.d.ts} +117 -3
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.js +7 -69
  21. package/dist/static.js.map +1 -1
  22. package/dist/story/index.d.ts +3 -2
  23. package/dist/story/index.js +5 -3
  24. package/dist/story/index.js.map +1 -1
  25. package/dist/styles.css +1 -1
  26. package/dist/styles.css.map +1 -1
  27. package/package.json +14 -6
  28. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  29. package/dist/chunk-ENXSUXC5.js.map +0 -1
  30. package/src/__test-fixtures__/dom.ts +0 -70
  31. package/src/__test-fixtures__/specs.ts +0 -214
  32. package/src/__tests__/animation.test.ts +0 -358
  33. package/src/__tests__/annotations-render.test.ts +0 -201
  34. package/src/__tests__/compound-labels.test.ts +0 -122
  35. package/src/__tests__/continuous-legend.test.ts +0 -91
  36. package/src/__tests__/crosshair.test.ts +0 -130
  37. package/src/__tests__/edit-events.test.ts +0 -902
  38. package/src/__tests__/events.test.ts +0 -387
  39. package/src/__tests__/export.test.ts +0 -241
  40. package/src/__tests__/font-race.test.ts +0 -199
  41. package/src/__tests__/gradient-ids.test.ts +0 -159
  42. package/src/__tests__/gradient-utils.test.ts +0 -125
  43. package/src/__tests__/known-bugs-render.test.ts +0 -183
  44. package/src/__tests__/map.test.ts +0 -293
  45. package/src/__tests__/mount.test.ts +0 -357
  46. package/src/__tests__/pattern-fills.test.ts +0 -105
  47. package/src/__tests__/resize-timing.test.ts +0 -218
  48. package/src/__tests__/sankey.test.ts +0 -133
  49. package/src/__tests__/selection-events.test.ts +0 -869
  50. package/src/__tests__/series-search.test.ts +0 -311
  51. package/src/__tests__/static.test.ts +0 -141
  52. package/src/__tests__/svg-renderer.test.ts +0 -895
  53. package/src/__tests__/table-keyboard.test.ts +0 -361
  54. package/src/__tests__/table-mount.test.ts +0 -639
  55. package/src/__tests__/tilemap.test.ts +0 -158
  56. package/src/__tests__/tooltip.test.ts +0 -337
  57. package/src/__tests__/transition.test.ts +0 -1777
  58. package/src/__tests__/you-draw-it.test.ts +0 -273
  59. package/src/animation-vars.ts +0 -42
  60. package/src/animation.ts +0 -76
  61. package/src/barlist-mount.ts +0 -383
  62. package/src/barlist-renderer.ts +0 -267
  63. package/src/dom-helpers.ts +0 -17
  64. package/src/export.ts +0 -482
  65. package/src/gradient-utils.ts +0 -138
  66. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  67. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  68. package/src/graph/__tests__/interaction.test.ts +0 -205
  69. package/src/graph/__tests__/keyboard.test.ts +0 -653
  70. package/src/graph/__tests__/search.test.ts +0 -88
  71. package/src/graph/__tests__/simulation.test.ts +0 -233
  72. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  73. package/src/graph/__tests__/zoom.test.ts +0 -203
  74. package/src/graph/canvas-renderer.ts +0 -747
  75. package/src/graph/interaction.ts +0 -367
  76. package/src/graph/keyboard.ts +0 -208
  77. package/src/graph/search.ts +0 -50
  78. package/src/graph/simulation-worker-url.ts +0 -12
  79. package/src/graph/simulation-worker.ts +0 -278
  80. package/src/graph/simulation.ts +0 -422
  81. package/src/graph/spatial-index.ts +0 -121
  82. package/src/graph/types.ts +0 -47
  83. package/src/graph/worker-protocol.ts +0 -70
  84. package/src/graph/zoom.ts +0 -134
  85. package/src/graph-mount.ts +0 -888
  86. package/src/index.ts +0 -76
  87. package/src/interactions/chart-events.ts +0 -139
  88. package/src/interactions/crosshair.ts +0 -233
  89. package/src/interactions/drag-handler.ts +0 -175
  90. package/src/interactions/editing-drags.ts +0 -515
  91. package/src/interactions/index.ts +0 -25
  92. package/src/interactions/keyboard-nav.ts +0 -111
  93. package/src/interactions/legend-interaction.ts +0 -46
  94. package/src/interactions/selection.ts +0 -280
  95. package/src/interactions/tooltip-events.ts +0 -72
  96. package/src/map-camera.ts +0 -88
  97. package/src/map-mount.ts +0 -701
  98. package/src/map-renderer.ts +0 -338
  99. package/src/map-transition.ts +0 -83
  100. package/src/measure-text.ts +0 -134
  101. package/src/mount.ts +0 -1382
  102. package/src/pattern-utils.ts +0 -110
  103. package/src/renderers/annotations.ts +0 -388
  104. package/src/renderers/axes.ts +0 -351
  105. package/src/renderers/brand.ts +0 -87
  106. package/src/renderers/chrome.ts +0 -196
  107. package/src/renderers/endpoint-labels.ts +0 -150
  108. package/src/renderers/legend.ts +0 -311
  109. package/src/renderers/marks.ts +0 -561
  110. package/src/renderers/metrics.ts +0 -63
  111. package/src/renderers/svg-dom.ts +0 -40
  112. package/src/renderers/table-cells.ts +0 -446
  113. package/src/resize-observer.ts +0 -46
  114. package/src/sankey-mount.ts +0 -622
  115. package/src/sankey-renderer.ts +0 -614
  116. package/src/series-search.ts +0 -392
  117. package/src/static.ts +0 -251
  118. package/src/story/__tests__/camera-math.test.ts +0 -145
  119. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  120. package/src/story/__tests__/progress-math.test.ts +0 -95
  121. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  122. package/src/story/__tests__/tween.test.ts +0 -159
  123. package/src/story/camera-math.ts +0 -125
  124. package/src/story/create-chart-story.ts +0 -353
  125. package/src/story/crossfade.ts +0 -70
  126. package/src/story/index.ts +0 -39
  127. package/src/story/progress-math.ts +0 -85
  128. package/src/story/resolve-camera-target.ts +0 -119
  129. package/src/story/scroll-driver.ts +0 -206
  130. package/src/story/story-camera.ts +0 -49
  131. package/src/story/tween.ts +0 -112
  132. package/src/story/types.ts +0 -76
  133. package/src/svg-ids.ts +0 -23
  134. package/src/svg-renderer.ts +0 -383
  135. package/src/table-keyboard.ts +0 -266
  136. package/src/table-mount.ts +0 -567
  137. package/src/table-renderer.ts +0 -414
  138. package/src/text-edit-overlay.ts +0 -255
  139. package/src/theme-tokens.ts +0 -51
  140. package/src/tilemap-mount.ts +0 -511
  141. package/src/tilemap-renderer.ts +0 -396
  142. package/src/tooltip.ts +0 -177
  143. package/src/transition.ts +0 -2816
  144. package/src/you-draw-it.ts +0 -480
  145. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,713 +0,0 @@
1
- import type { ResolvedTheme } from '@opendata-ai/openchart-core';
2
- import { beforeEach, describe, expect, it } from 'vitest';
3
- import { GraphCanvasRenderer, labelThreshold, visibleRect } from '../canvas-renderer';
4
- import type { GraphRenderState, PositionedEdge, PositionedNode } from '../types';
5
-
6
- // ---------------------------------------------------------------------------
7
- // Recording canvas context proxy
8
- // ---------------------------------------------------------------------------
9
-
10
- interface DrawCall {
11
- method: string;
12
- args: unknown[];
13
- }
14
-
15
- function createRecordingCanvas(): {
16
- canvas: HTMLCanvasElement;
17
- calls: DrawCall[];
18
- } {
19
- const calls: DrawCall[] = [];
20
-
21
- // Methods we want to track
22
- const trackedMethods = [
23
- 'clearRect',
24
- 'fillRect',
25
- 'beginPath',
26
- 'arc',
27
- 'fill',
28
- 'stroke',
29
- 'moveTo',
30
- 'lineTo',
31
- 'fillText',
32
- 'strokeText',
33
- 'save',
34
- 'restore',
35
- 'translate',
36
- 'scale',
37
- 'setTransform',
38
- 'setLineDash',
39
- ];
40
-
41
- const fakeCtx: Record<string, unknown> = {
42
- globalAlpha: 1,
43
- fillStyle: '',
44
- strokeStyle: '',
45
- lineWidth: 1,
46
- font: '',
47
- textAlign: '',
48
- textBaseline: '',
49
- lineJoin: '',
50
- };
51
-
52
- for (const method of trackedMethods) {
53
- fakeCtx[method] = (...args: unknown[]) => {
54
- calls.push({ method, args });
55
- };
56
- }
57
-
58
- const canvas = {
59
- getContext: () => fakeCtx,
60
- width: 0,
61
- height: 0,
62
- style: { width: '', height: '' },
63
- } as unknown as HTMLCanvasElement;
64
-
65
- return { canvas, calls };
66
- }
67
-
68
- // ---------------------------------------------------------------------------
69
- // Test fixtures
70
- // ---------------------------------------------------------------------------
71
-
72
- function makeTheme(isDark = false): ResolvedTheme {
73
- return {
74
- isDark,
75
- colors: {
76
- categorical: ['#3b82f6', '#ef4444', '#22c55e'],
77
- sequential: {},
78
- diverging: {},
79
- background: isDark ? '#1a1a2e' : '#ffffff',
80
- text: isDark ? '#e0e0e0' : '#1a1a2e',
81
- gridline: '#cccccc',
82
- axis: '#666666',
83
- annotationFill: '#ffff00',
84
- annotationText: '#000000',
85
- },
86
- fonts: {
87
- family: 'Inter, sans-serif',
88
- mono: 'monospace',
89
- sizes: {
90
- title: 18,
91
- subtitle: 14,
92
- body: 12,
93
- small: 10,
94
- axisTick: 11,
95
- metricLabel: 10,
96
- metricValue: 22,
97
- },
98
- weights: { normal: 400, medium: 500, semibold: 600, bold: 700 },
99
- },
100
- spacing: {
101
- padding: 16,
102
- chromeGap: 4,
103
- chromeToChart: 12,
104
- chartToFooter: 8,
105
- axisMargin: 40,
106
- },
107
- borderRadius: 4,
108
- chrome: {
109
- eyebrow: { fontSize: 11, fontWeight: 510, color: '#06b6d4', lineHeight: 1.4 },
110
- title: { fontSize: 18, fontWeight: 600, color: '#1a1a2e', lineHeight: 1.2 },
111
- subtitle: { fontSize: 14, fontWeight: 400, color: '#666', lineHeight: 1.3 },
112
- source: { fontSize: 10, fontWeight: 400, color: '#999', lineHeight: 1.2 },
113
- byline: { fontSize: 10, fontWeight: 400, color: '#999', lineHeight: 1.2 },
114
- footer: { fontSize: 10, fontWeight: 400, color: '#999', lineHeight: 1.2 },
115
- },
116
- };
117
- }
118
-
119
- function makeNode(overrides: Partial<PositionedNode> & { id: string }): PositionedNode {
120
- return {
121
- x: 0,
122
- y: 0,
123
- radius: 5,
124
- fill: '#3b82f6',
125
- stroke: '#2563eb',
126
- strokeWidth: 1,
127
- label: undefined,
128
- labelPriority: 0.5,
129
- community: undefined,
130
- data: {},
131
- ...overrides,
132
- };
133
- }
134
-
135
- function makeEdge(
136
- source: string,
137
- target: string,
138
- overrides?: Partial<PositionedEdge>,
139
- ): PositionedEdge {
140
- return {
141
- source,
142
- target,
143
- sourceX: 0,
144
- sourceY: 0,
145
- targetX: 100,
146
- targetY: 100,
147
- stroke: '#999',
148
- strokeWidth: 1,
149
- style: 'solid',
150
- data: {},
151
- ...overrides,
152
- };
153
- }
154
-
155
- function makeState(overrides?: Partial<GraphRenderState>): GraphRenderState {
156
- return {
157
- nodes: [],
158
- edges: [],
159
- transform: { x: 0, y: 0, k: 1 },
160
- hoveredNodeId: null,
161
- selectedNodeIds: new Set(),
162
- adjacencyMap: new Map(),
163
- theme: makeTheme(),
164
- searchMatches: null,
165
- isGesturing: false,
166
- ...overrides,
167
- };
168
- }
169
-
170
- // ---------------------------------------------------------------------------
171
- // Tests: labelThreshold
172
- // ---------------------------------------------------------------------------
173
-
174
- describe('labelThreshold', () => {
175
- it('returns ~1 at very low zoom (only top priority labels visible)', () => {
176
- const t = labelThreshold(0.2);
177
- expect(t).toBeCloseTo(1, 5);
178
- });
179
-
180
- it('returns ~0 at high zoom (all labels visible)', () => {
181
- const t = labelThreshold(2.0);
182
- expect(t).toBeCloseTo(0, 5);
183
- });
184
-
185
- it('returns ~0.5 at midpoint zoom', () => {
186
- const t = labelThreshold(1.1);
187
- expect(t).toBeCloseTo(0.5, 1);
188
- });
189
-
190
- it('clamps below 0.2 zoom', () => {
191
- expect(labelThreshold(0.05)).toBe(1);
192
- });
193
-
194
- it('clamps above 2.0 zoom', () => {
195
- expect(labelThreshold(5.0)).toBe(0);
196
- });
197
- });
198
-
199
- // ---------------------------------------------------------------------------
200
- // Tests: visibleRect
201
- // ---------------------------------------------------------------------------
202
-
203
- describe('visibleRect', () => {
204
- it('computes correct bounds with identity transform', () => {
205
- const rect = visibleRect(800, 600, { x: 0, y: 0, k: 1 }, 0);
206
- expect(rect.minX).toBeCloseTo(0);
207
- expect(rect.minY).toBeCloseTo(0);
208
- expect(rect.maxX).toBe(800);
209
- expect(rect.maxY).toBe(600);
210
- });
211
-
212
- it('accounts for pan offset', () => {
213
- const rect = visibleRect(800, 600, { x: 100, y: 50, k: 1 }, 0);
214
- expect(rect).toEqual({ minX: -100, minY: -50, maxX: 700, maxY: 550 });
215
- });
216
-
217
- it('accounts for zoom', () => {
218
- const rect = visibleRect(800, 600, { x: 0, y: 0, k: 2 }, 0);
219
- expect(rect.minX).toBeCloseTo(0);
220
- expect(rect.minY).toBeCloseTo(0);
221
- expect(rect.maxX).toBe(400);
222
- expect(rect.maxY).toBe(300);
223
- });
224
-
225
- it('includes margin', () => {
226
- const rect = visibleRect(800, 600, { x: 0, y: 0, k: 1 }, 50);
227
- expect(rect.minX).toBe(-50);
228
- expect(rect.minY).toBe(-50);
229
- expect(rect.maxX).toBe(850);
230
- expect(rect.maxY).toBe(650);
231
- });
232
-
233
- it('handles combined pan + zoom', () => {
234
- // Canvas 400x300, pan(200, 100), zoom 2x
235
- const rect = visibleRect(400, 300, { x: 200, y: 100, k: 2 }, 0);
236
- expect(rect.minX).toBeCloseTo(-100);
237
- expect(rect.minY).toBeCloseTo(-50);
238
- expect(rect.maxX).toBeCloseTo(100);
239
- expect(rect.maxY).toBeCloseTo(100);
240
- });
241
- });
242
-
243
- // ---------------------------------------------------------------------------
244
- // Tests: DPR scaling
245
- // ---------------------------------------------------------------------------
246
-
247
- describe('GraphCanvasRenderer.resize', () => {
248
- it('sets canvas pixel dimensions with DPR scaling', () => {
249
- const { canvas } = createRecordingCanvas();
250
- const renderer = new GraphCanvasRenderer(canvas);
251
- // DPR is 1 in happy-dom (no window.devicePixelRatio), so canvas = css size
252
- renderer.resize(800, 600);
253
- expect(canvas.width).toBe(800);
254
- expect(canvas.height).toBe(600);
255
- // CSS sizing is handled by width/height: 100% in graph.css,
256
- // so resize() no longer sets inline style dimensions
257
- });
258
- });
259
-
260
- // ---------------------------------------------------------------------------
261
- // Tests: Render draw order
262
- // ---------------------------------------------------------------------------
263
-
264
- describe('GraphCanvasRenderer.render', () => {
265
- let canvas: HTMLCanvasElement;
266
- let calls: DrawCall[];
267
- let renderer: GraphCanvasRenderer;
268
-
269
- beforeEach(() => {
270
- const recording = createRecordingCanvas();
271
- canvas = recording.canvas;
272
- calls = recording.calls;
273
- renderer = new GraphCanvasRenderer(canvas);
274
- renderer.resize(800, 600);
275
- });
276
-
277
- it('clears canvas before drawing', () => {
278
- renderer.render(makeState());
279
- const clearIdx = calls.findIndex((c) => c.method === 'clearRect');
280
- expect(clearIdx).toBeGreaterThanOrEqual(0);
281
- });
282
-
283
- it('draws edges before nodes', () => {
284
- const nodes = [makeNode({ id: 'a', x: 100, y: 100 }), makeNode({ id: 'b', x: 200, y: 200 })];
285
- const edges = [makeEdge('a', 'b', { sourceX: 100, sourceY: 100, targetX: 200, targetY: 200 })];
286
-
287
- renderer.render(
288
- makeState({
289
- nodes,
290
- edges,
291
- adjacencyMap: new Map([
292
- ['a', new Set(['b'])],
293
- ['b', new Set(['a'])],
294
- ]),
295
- }),
296
- );
297
-
298
- // Find first edge draw (moveTo) and first node draw (arc for fill)
299
- const firstMoveTo = calls.findIndex((c) => c.method === 'moveTo');
300
- const firstArc = calls.findIndex((c) => c.method === 'arc');
301
-
302
- expect(firstMoveTo).toBeLessThan(firstArc);
303
- });
304
-
305
- it('draws labels after nodes', () => {
306
- const nodes = [makeNode({ id: 'a', x: 100, y: 100, label: 'Node A', labelPriority: 1 })];
307
-
308
- renderer.render(makeState({ nodes }));
309
-
310
- // Find last arc (node drawing) and first fillText (label drawing)
311
- let lastArc = -1;
312
- let firstFillText = -1;
313
- for (let i = 0; i < calls.length; i++) {
314
- if (calls[i].method === 'arc') lastArc = i;
315
- if (calls[i].method === 'fillText' && firstFillText === -1) firstFillText = i;
316
- }
317
-
318
- expect(lastArc).toBeGreaterThan(-1);
319
- expect(firstFillText).toBeGreaterThan(lastArc);
320
- });
321
-
322
- it('culls nodes outside viewport', () => {
323
- const nodes = [
324
- makeNode({ id: 'visible', x: 400, y: 300 }),
325
- makeNode({ id: 'offscreen', x: 5000, y: 5000 }),
326
- ];
327
-
328
- renderer.render(makeState({ nodes }));
329
-
330
- // Count arcs -- only the visible node should produce arcs
331
- const arcCalls = calls.filter((c) => c.method === 'arc');
332
- // 1 visible node => 1 arc in fill batch + 1 arc in stroke batch = 2 arcs
333
- expect(arcCalls.length).toBe(2);
334
- });
335
-
336
- it('renders dashed edges with setLineDash', () => {
337
- const nodes = [makeNode({ id: 'a', x: 100, y: 100 }), makeNode({ id: 'b', x: 200, y: 200 })];
338
- const edges = [
339
- makeEdge('a', 'b', {
340
- style: 'dashed',
341
- sourceX: 100,
342
- sourceY: 100,
343
- targetX: 200,
344
- targetY: 200,
345
- }),
346
- ];
347
-
348
- renderer.render(makeState({ nodes, edges }));
349
-
350
- const dashCalls = calls.filter(
351
- (c) =>
352
- c.method === 'setLineDash' &&
353
- Array.isArray(c.args[0]) &&
354
- (c.args[0] as number[]).length > 0,
355
- );
356
- expect(dashCalls.length).toBeGreaterThan(0);
357
- });
358
- });
359
-
360
- // ---------------------------------------------------------------------------
361
- // Recording canvas with property tracking
362
- // ---------------------------------------------------------------------------
363
-
364
- interface PropertyChange {
365
- property: string;
366
- value: unknown;
367
- }
368
-
369
- interface DrawCallWithContext {
370
- method: string;
371
- args: unknown[];
372
- }
373
-
374
- /**
375
- * Extended recording canvas that also captures property assignments
376
- * (fillStyle, strokeStyle, globalAlpha, etc.) interleaved with draw calls.
377
- * Returns a timeline of operations in order.
378
- */
379
- function createTrackingCanvas(): {
380
- canvas: HTMLCanvasElement;
381
- calls: DrawCallWithContext[];
382
- props: PropertyChange[];
383
- timeline: Array<{ type: 'call' | 'prop'; index: number }>;
384
- } {
385
- const calls: DrawCallWithContext[] = [];
386
- const props: PropertyChange[] = [];
387
- const timeline: Array<{ type: 'call' | 'prop'; index: number }> = [];
388
-
389
- const trackedMethods = [
390
- 'clearRect',
391
- 'fillRect',
392
- 'beginPath',
393
- 'arc',
394
- 'fill',
395
- 'stroke',
396
- 'moveTo',
397
- 'lineTo',
398
- 'fillText',
399
- 'strokeText',
400
- 'save',
401
- 'restore',
402
- 'translate',
403
- 'scale',
404
- 'setTransform',
405
- 'setLineDash',
406
- ];
407
-
408
- const trackedProps = ['globalAlpha', 'fillStyle', 'strokeStyle', 'lineWidth'];
409
- const internalState: Record<string, unknown> = {
410
- globalAlpha: 1,
411
- fillStyle: '',
412
- strokeStyle: '',
413
- lineWidth: 1,
414
- font: '',
415
- textAlign: '',
416
- textBaseline: '',
417
- lineJoin: '',
418
- };
419
-
420
- const handler: ProxyHandler<Record<string, unknown>> = {
421
- get(target, prop: string) {
422
- if (typeof target[prop] === 'function') return target[prop];
423
- return internalState[prop];
424
- },
425
- set(_target, prop: string, value: unknown) {
426
- internalState[prop] = value;
427
- if (trackedProps.includes(prop)) {
428
- props.push({ property: prop, value });
429
- timeline.push({ type: 'prop', index: props.length - 1 });
430
- }
431
- return true;
432
- },
433
- };
434
-
435
- const fakeCtxTarget: Record<string, unknown> = {};
436
- for (const method of trackedMethods) {
437
- fakeCtxTarget[method] = (...args: unknown[]) => {
438
- calls.push({ method, args });
439
- timeline.push({ type: 'call', index: calls.length - 1 });
440
- };
441
- }
442
-
443
- const fakeCtx = new Proxy(fakeCtxTarget, handler);
444
-
445
- const canvas = {
446
- getContext: () => fakeCtx,
447
- width: 0,
448
- height: 0,
449
- style: { width: '', height: '' },
450
- } as unknown as HTMLCanvasElement;
451
-
452
- return { canvas, calls, props, timeline };
453
- }
454
-
455
- // ---------------------------------------------------------------------------
456
- // Tests: Community coloring
457
- // ---------------------------------------------------------------------------
458
-
459
- describe('community coloring', () => {
460
- it('nodes in same community share fill color', () => {
461
- const { canvas, props } = createTrackingCanvas();
462
- const renderer = new GraphCanvasRenderer(canvas);
463
- renderer.resize(800, 600);
464
-
465
- // Two nodes in community A, one in community B
466
- // Community coloring is handled upstream (compilation assigns fill colors),
467
- // so we give same-community nodes the same fill here
468
- const nodes = [
469
- makeNode({ id: 'a1', x: 100, y: 100, fill: '#3b82f6', community: 'groupA' }),
470
- makeNode({ id: 'a2', x: 200, y: 100, fill: '#3b82f6', community: 'groupA' }),
471
- makeNode({ id: 'b1', x: 300, y: 100, fill: '#ef4444', community: 'groupB' }),
472
- ];
473
-
474
- renderer.render(makeState({ nodes }));
475
-
476
- // Collect all fillStyle values set during rendering
477
- const fillStyles = props
478
- .filter((p) => p.property === 'fillStyle')
479
- .map((p) => p.value as string);
480
-
481
- // The community A color and community B color should both appear
482
- expect(fillStyles).toContain('#3b82f6');
483
- expect(fillStyles).toContain('#ef4444');
484
- });
485
-
486
- it('distinct communities produce distinct fill colors in draw calls', () => {
487
- const { canvas, props } = createTrackingCanvas();
488
- const renderer = new GraphCanvasRenderer(canvas);
489
- renderer.resize(800, 600);
490
-
491
- const nodes = [
492
- makeNode({ id: 'a', x: 100, y: 100, fill: '#aaa', community: 'A' }),
493
- makeNode({ id: 'b', x: 200, y: 200, fill: '#bbb', community: 'B' }),
494
- ];
495
-
496
- renderer.render(makeState({ nodes }));
497
-
498
- const fillStyles = props
499
- .filter((p) => p.property === 'fillStyle')
500
- .map((p) => p.value as string);
501
-
502
- expect(fillStyles).toContain('#aaa');
503
- expect(fillStyles).toContain('#bbb');
504
- });
505
- });
506
-
507
- // ---------------------------------------------------------------------------
508
- // Tests: Hover highlighting
509
- // ---------------------------------------------------------------------------
510
-
511
- describe('hover highlighting', () => {
512
- it('hovered node drawn with full opacity, others at default', () => {
513
- const { canvas, props } = createTrackingCanvas();
514
- const renderer = new GraphCanvasRenderer(canvas);
515
- renderer.resize(800, 600);
516
-
517
- const nodes = [
518
- makeNode({ id: 'hovered', x: 100, y: 100, fill: '#3b82f6' }),
519
- makeNode({ id: 'other', x: 200, y: 200, fill: '#ef4444' }),
520
- ];
521
-
522
- renderer.render(
523
- makeState({
524
- nodes,
525
- hoveredNodeId: 'hovered',
526
- adjacencyMap: new Map(),
527
- }),
528
- );
529
-
530
- // The hovered node is drawn as a "special" node individually.
531
- // Its globalAlpha should be 1 (full opacity) before its arc call.
532
- // We check that globalAlpha=1 appears before at least one arc call.
533
- const alphaValues = props
534
- .filter((p) => p.property === 'globalAlpha')
535
- .map((p) => p.value as number);
536
-
537
- // Should have at least one globalAlpha=1 for the hovered node
538
- expect(alphaValues).toContain(1);
539
- });
540
-
541
- it('hovered node uses brightened fill color', () => {
542
- const { canvas, props } = createTrackingCanvas();
543
- const renderer = new GraphCanvasRenderer(canvas);
544
- renderer.resize(800, 600);
545
-
546
- const originalFill = '#3b82f6';
547
- const nodes = [makeNode({ id: 'hovered', x: 100, y: 100, fill: originalFill })];
548
-
549
- renderer.render(makeState({ nodes, hoveredNodeId: 'hovered' }));
550
-
551
- // The hovered node should have a fillStyle that is NOT the original
552
- // (it's brightened by +40 per channel). The hovered node is drawn individually
553
- // as a "special" node, so we should see a brightened fill somewhere.
554
- const fillStyles = props
555
- .filter((p) => p.property === 'fillStyle')
556
- .map((p) => p.value as string);
557
-
558
- // brighten('#3b82f6') would produce something like rgb(99, 170, 246+40 capped)
559
- // The brightened color should be different from the original
560
- const hasBrightened = fillStyles.some((f) => f.startsWith('rgb(') && f !== originalFill);
561
- expect(hasBrightened).toBe(true);
562
- });
563
- });
564
-
565
- // ---------------------------------------------------------------------------
566
- // Tests: Search highlighting
567
- // ---------------------------------------------------------------------------
568
-
569
- describe('search highlighting', () => {
570
- it('matching nodes drawn at full opacity, non-matching dimmed', () => {
571
- const { canvas, props } = createTrackingCanvas();
572
- const renderer = new GraphCanvasRenderer(canvas);
573
- renderer.resize(800, 600);
574
-
575
- const nodes = [
576
- makeNode({ id: 'match', x: 100, y: 100, fill: '#3b82f6' }),
577
- makeNode({ id: 'no-match', x: 200, y: 200, fill: '#3b82f6' }),
578
- ];
579
-
580
- renderer.render(
581
- makeState({
582
- nodes,
583
- searchMatches: new Set(['match']),
584
- }),
585
- );
586
-
587
- const alphaValues = props
588
- .filter((p) => p.property === 'globalAlpha')
589
- .map((p) => p.value as number);
590
-
591
- // Should see full opacity (1) for matched nodes
592
- expect(alphaValues).toContain(1);
593
- // Should see dimmed opacity (0.15) for non-matching nodes
594
- expect(alphaValues).toContain(0.15);
595
- });
596
-
597
- it('search-dimmed edges use reduced opacity', () => {
598
- const { canvas, props } = createTrackingCanvas();
599
- const renderer = new GraphCanvasRenderer(canvas);
600
- renderer.resize(800, 600);
601
-
602
- const nodes = [
603
- makeNode({ id: 'a', x: 100, y: 100 }),
604
- makeNode({ id: 'b', x: 200, y: 200 }),
605
- makeNode({ id: 'c', x: 300, y: 300 }),
606
- ];
607
- const edges = [
608
- makeEdge('a', 'b', { sourceX: 100, sourceY: 100, targetX: 200, targetY: 200 }),
609
- makeEdge('b', 'c', { sourceX: 200, sourceY: 200, targetX: 300, targetY: 300 }),
610
- ];
611
-
612
- renderer.render(
613
- makeState({
614
- nodes,
615
- edges,
616
- searchMatches: new Set(['a']),
617
- adjacencyMap: new Map([
618
- ['a', new Set(['b'])],
619
- ['b', new Set(['a', 'c'])],
620
- ['c', new Set(['b'])],
621
- ]),
622
- }),
623
- );
624
-
625
- const alphaValues = props
626
- .filter((p) => p.property === 'globalAlpha')
627
- .map((p) => p.value as number);
628
-
629
- // Should have reduced alpha for non-matching edges (0.15 * base alpha)
630
- const hasDimmed = alphaValues.some((a) => a > 0 && a < 0.15 + 0.01);
631
- expect(hasDimmed).toBe(true);
632
- });
633
- });
634
-
635
- // ---------------------------------------------------------------------------
636
- // Tests: Selected nodes
637
- // ---------------------------------------------------------------------------
638
-
639
- describe('selected nodes', () => {
640
- it('selected node drawn with selection ring (extra arc at radius + 3)', () => {
641
- const { canvas, calls } = createTrackingCanvas();
642
- const renderer = new GraphCanvasRenderer(canvas);
643
- renderer.resize(800, 600);
644
-
645
- const nodeRadius = 10;
646
- const nodes = [makeNode({ id: 'selected', x: 200, y: 200, radius: nodeRadius })];
647
-
648
- renderer.render(
649
- makeState({
650
- nodes,
651
- selectedNodeIds: new Set(['selected']),
652
- }),
653
- );
654
-
655
- // Selected nodes draw a fill arc (stroke reuses same path), then a
656
- // selection ring arc at radius + 3. So at least 2 arcs total.
657
- const arcCalls = calls.filter((c) => c.method === 'arc');
658
- expect(arcCalls.length).toBeGreaterThanOrEqual(2);
659
-
660
- // The selection ring arc should be at radius + 3 = 13
661
- const selectionRingArc = arcCalls.find((c) => {
662
- const radius = c.args[2] as number;
663
- return Math.abs(radius - (nodeRadius + 3)) < 0.5;
664
- });
665
- expect(selectionRingArc).not.toBeUndefined();
666
- });
667
-
668
- it('selection ring uses theme categorical color', () => {
669
- const { canvas, props } = createTrackingCanvas();
670
- const renderer = new GraphCanvasRenderer(canvas);
671
- renderer.resize(800, 600);
672
-
673
- const theme = makeTheme();
674
- const nodes = [makeNode({ id: 'selected', x: 200, y: 200, radius: 10 })];
675
-
676
- renderer.render(
677
- makeState({
678
- nodes,
679
- selectedNodeIds: new Set(['selected']),
680
- theme,
681
- }),
682
- );
683
-
684
- const strokeStyles = props
685
- .filter((p) => p.property === 'strokeStyle')
686
- .map((p) => p.value as string);
687
-
688
- // Selection ring should use theme.colors.categorical[0]
689
- expect(strokeStyles).toContain(theme.colors.categorical[0]);
690
- });
691
-
692
- it('selection ring has lineWidth of 2', () => {
693
- const { canvas, props } = createTrackingCanvas();
694
- const renderer = new GraphCanvasRenderer(canvas);
695
- renderer.resize(800, 600);
696
-
697
- const nodes = [makeNode({ id: 'selected', x: 200, y: 200, radius: 10 })];
698
-
699
- renderer.render(
700
- makeState({
701
- nodes,
702
- selectedNodeIds: new Set(['selected']),
703
- }),
704
- );
705
-
706
- const lineWidths = props
707
- .filter((p) => p.property === 'lineWidth')
708
- .map((p) => p.value as number);
709
-
710
- // Should see lineWidth=2 for the selection ring
711
- expect(lineWidths).toContain(2);
712
- });
713
- });