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