@getmicdrop/svelte-components 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -0
- package/dist/config.d.ts +3 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +6 -0
- package/dist/config.spec.d.ts +2 -0
- package/dist/config.spec.d.ts.map +1 -0
- package/dist/config.spec.js +29 -0
- package/dist/constants/formOptions.d.ts +18 -0
- package/dist/constants/formOptions.d.ts.map +1 -0
- package/dist/constants/formOptions.js +25 -0
- package/dist/constants/formOptions.spec.d.ts +2 -0
- package/dist/constants/formOptions.spec.d.ts.map +1 -0
- package/dist/constants/formOptions.spec.js +88 -0
- package/dist/telemetry.d.ts +13 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +358 -0
- package/dist/telemetry.server.d.ts +11 -0
- package/dist/telemetry.server.d.ts.map +1 -0
- package/dist/telemetry.server.js +212 -0
- package/dist/telemetry.server.spec.d.ts +2 -0
- package/dist/telemetry.server.spec.d.ts.map +1 -0
- package/dist/telemetry.server.spec.js +434 -0
- package/dist/telemetry.spec.d.ts +2 -0
- package/dist/telemetry.spec.d.ts.map +1 -0
- package/dist/telemetry.spec.js +660 -0
- package/package.json +153 -0
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
// Mock all OpenTelemetry modules before importing telemetry
|
|
4
|
+
vi.mock('@opentelemetry/sdk-trace-web', () => ({
|
|
5
|
+
WebTracerProvider: vi.fn().mockImplementation(() => ({
|
|
6
|
+
addSpanProcessor: vi.fn(),
|
|
7
|
+
register: vi.fn(),
|
|
8
|
+
getTracer: vi.fn().mockReturnValue({
|
|
9
|
+
startSpan: vi.fn().mockReturnValue({
|
|
10
|
+
setAttribute: vi.fn(),
|
|
11
|
+
setStatus: vi.fn(),
|
|
12
|
+
recordException: vi.fn(),
|
|
13
|
+
end: vi.fn(),
|
|
14
|
+
name: 'test-span',
|
|
15
|
+
}),
|
|
16
|
+
}),
|
|
17
|
+
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
18
|
+
})),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock('@opentelemetry/sdk-trace-base', () => ({
|
|
22
|
+
BatchSpanProcessor: vi.fn(),
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock('@opentelemetry/exporter-trace-otlp-http', () => ({
|
|
26
|
+
OTLPTraceExporter: vi.fn(),
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
vi.mock('@opentelemetry/resources', () => ({
|
|
30
|
+
Resource: vi.fn(),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
vi.mock('@opentelemetry/semantic-conventions', () => ({
|
|
34
|
+
SEMRESATTRS_SERVICE_NAME: 'service.name',
|
|
35
|
+
SEMRESATTRS_SERVICE_VERSION: 'service.version',
|
|
36
|
+
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT: 'deployment.environment',
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
vi.mock('@opentelemetry/instrumentation-document-load', () => ({
|
|
40
|
+
DocumentLoadInstrumentation: vi.fn().mockImplementation(() => ({
|
|
41
|
+
enable: vi.fn(),
|
|
42
|
+
})),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
vi.mock('@opentelemetry/instrumentation-fetch', () => ({
|
|
46
|
+
FetchInstrumentation: vi.fn().mockImplementation(() => ({
|
|
47
|
+
enable: vi.fn(),
|
|
48
|
+
})),
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
vi.mock('@opentelemetry/instrumentation-xml-http-request', () => ({
|
|
52
|
+
XMLHttpRequestInstrumentation: vi.fn().mockImplementation(() => ({
|
|
53
|
+
enable: vi.fn(),
|
|
54
|
+
})),
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
vi.mock('@opentelemetry/instrumentation-user-interaction', () => ({
|
|
58
|
+
UserInteractionInstrumentation: vi.fn().mockImplementation(() => ({
|
|
59
|
+
enable: vi.fn(),
|
|
60
|
+
})),
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
vi.mock('@opentelemetry/context-zone', () => ({
|
|
64
|
+
ZoneContextManager: vi.fn(),
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
vi.mock('@opentelemetry/propagator-b3', () => ({
|
|
68
|
+
B3Propagator: vi.fn(),
|
|
69
|
+
}));
|
|
70
|
+
|
|
71
|
+
describe('Telemetry Module', () => {
|
|
72
|
+
let telemetry;
|
|
73
|
+
let originalWindow;
|
|
74
|
+
let originalDocument;
|
|
75
|
+
let mockSpan;
|
|
76
|
+
|
|
77
|
+
beforeEach(async () => {
|
|
78
|
+
vi.resetModules();
|
|
79
|
+
|
|
80
|
+
// Save original globals
|
|
81
|
+
originalWindow = global.window;
|
|
82
|
+
originalDocument = global.document;
|
|
83
|
+
|
|
84
|
+
// Setup mock window
|
|
85
|
+
global.window = {
|
|
86
|
+
location: {
|
|
87
|
+
pathname: '/performers',
|
|
88
|
+
search: '?test=1',
|
|
89
|
+
hash: '#section',
|
|
90
|
+
href: 'http://localhost/performers?test=1#section',
|
|
91
|
+
},
|
|
92
|
+
addEventListener: vi.fn(),
|
|
93
|
+
__TRACE_CONTEXT__: null,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
global.document = {
|
|
97
|
+
readyState: 'complete',
|
|
98
|
+
title: 'Test Page',
|
|
99
|
+
referrer: 'http://localhost/',
|
|
100
|
+
addEventListener: vi.fn(),
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
global.navigator = {
|
|
104
|
+
userAgent: 'test-agent',
|
|
105
|
+
language: 'en-US',
|
|
106
|
+
platform: 'test-platform',
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Create mock span
|
|
110
|
+
mockSpan = {
|
|
111
|
+
setAttribute: vi.fn(),
|
|
112
|
+
setStatus: vi.fn(),
|
|
113
|
+
recordException: vi.fn(),
|
|
114
|
+
end: vi.fn(),
|
|
115
|
+
name: 'test-span',
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Re-import with fresh mocks
|
|
119
|
+
telemetry = await import('./telemetry.js');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
afterEach(() => {
|
|
123
|
+
global.window = originalWindow;
|
|
124
|
+
global.document = originalDocument;
|
|
125
|
+
vi.clearAllMocks();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('initTelemetry', () => {
|
|
129
|
+
it('exports initTelemetry function', () => {
|
|
130
|
+
expect(telemetry.initTelemetry).toBeDefined();
|
|
131
|
+
expect(typeof telemetry.initTelemetry).toBe('function');
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('can be called without errors', () => {
|
|
135
|
+
expect(() => telemetry.initTelemetry()).not.toThrow();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('startSpan', () => {
|
|
140
|
+
it('exports startSpan function', () => {
|
|
141
|
+
expect(telemetry.startSpan).toBeDefined();
|
|
142
|
+
expect(typeof telemetry.startSpan).toBe('function');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('returns null when tracer is not initialized', async () => {
|
|
146
|
+
vi.resetModules();
|
|
147
|
+
const tempWindow = global.window;
|
|
148
|
+
delete global.window;
|
|
149
|
+
|
|
150
|
+
const freshTelemetry = await import('./telemetry.js');
|
|
151
|
+
global.window = tempWindow;
|
|
152
|
+
|
|
153
|
+
const result = freshTelemetry.startSpan('test-span');
|
|
154
|
+
expect(result).toBeNull();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('can be called with name only', () => {
|
|
158
|
+
telemetry.initTelemetry();
|
|
159
|
+
const result = telemetry.startSpan('test-span');
|
|
160
|
+
expect(result === null || typeof result === 'object').toBe(true);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('can be called with name and options', () => {
|
|
164
|
+
telemetry.initTelemetry();
|
|
165
|
+
const result = telemetry.startSpan('test-span', { attributes: { key: 'value' } });
|
|
166
|
+
expect(result === null || typeof result === 'object').toBe(true);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('endSpan', () => {
|
|
171
|
+
it('exports endSpan function', () => {
|
|
172
|
+
expect(telemetry.endSpan).toBeDefined();
|
|
173
|
+
expect(typeof telemetry.endSpan).toBe('function');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('handles null span gracefully', () => {
|
|
177
|
+
expect(() => telemetry.endSpan(null)).not.toThrow();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('handles undefined span gracefully', () => {
|
|
181
|
+
expect(() => telemetry.endSpan(undefined)).not.toThrow();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('can end a span with default status', () => {
|
|
185
|
+
expect(() => telemetry.endSpan(mockSpan)).not.toThrow();
|
|
186
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 0 });
|
|
187
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('can end a span with custom status', () => {
|
|
191
|
+
expect(() => telemetry.endSpan(mockSpan, { code: 2, message: 'error' })).not.toThrow();
|
|
192
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2, message: 'error' });
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('trackPageView', () => {
|
|
197
|
+
it('exports trackPageView function', () => {
|
|
198
|
+
expect(telemetry.trackPageView).toBeDefined();
|
|
199
|
+
expect(typeof telemetry.trackPageView).toBe('function');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('can be called without errors', () => {
|
|
203
|
+
expect(() => telemetry.trackPageView('/test-page')).not.toThrow();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('can be called with page path and title', () => {
|
|
207
|
+
expect(() => telemetry.trackPageView('/test-page', 'Test Page Title')).not.toThrow();
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe('trackEvent', () => {
|
|
212
|
+
it('exports trackEvent function', () => {
|
|
213
|
+
expect(telemetry.trackEvent).toBeDefined();
|
|
214
|
+
expect(typeof telemetry.trackEvent).toBe('function');
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('can be called with event name only', () => {
|
|
218
|
+
expect(() => telemetry.trackEvent('button_click')).not.toThrow();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('can be called with event name and attributes', () => {
|
|
222
|
+
expect(() => telemetry.trackEvent('button_click', { button_id: 'submit' })).not.toThrow();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe('trackPerformerAction', () => {
|
|
227
|
+
it('exports trackPerformerAction function', () => {
|
|
228
|
+
expect(telemetry.trackPerformerAction).toBeDefined();
|
|
229
|
+
expect(typeof telemetry.trackPerformerAction).toBe('function');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('can be called with action only', () => {
|
|
233
|
+
expect(() => telemetry.trackPerformerAction('profile_update')).not.toThrow();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('can be called with action and metadata', () => {
|
|
237
|
+
expect(() => telemetry.trackPerformerAction('profile_update', { field: 'bio' })).not.toThrow();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe('trackError', () => {
|
|
242
|
+
it('exports trackError function', () => {
|
|
243
|
+
expect(telemetry.trackError).toBeDefined();
|
|
244
|
+
expect(typeof telemetry.trackError).toBe('function');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('can be called with Error object', () => {
|
|
248
|
+
const error = new Error('Test error');
|
|
249
|
+
expect(() => telemetry.trackError(error)).not.toThrow();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('can be called with Error and context', () => {
|
|
253
|
+
const error = new Error('Test error');
|
|
254
|
+
expect(() => telemetry.trackError(error, { component: 'TestComponent' })).not.toThrow();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('handles error without stack trace', () => {
|
|
258
|
+
const error = { message: 'Simple error', name: 'SimpleError' };
|
|
259
|
+
expect(() => telemetry.trackError(error)).not.toThrow();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('handles string error', () => {
|
|
263
|
+
expect(() => telemetry.trackError('String error message')).not.toThrow();
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe('trackAPICall', () => {
|
|
268
|
+
it('exports trackAPICall function', () => {
|
|
269
|
+
expect(telemetry.trackAPICall).toBeDefined();
|
|
270
|
+
expect(typeof telemetry.trackAPICall).toBe('function');
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('can be called with basic parameters', () => {
|
|
274
|
+
expect(() => telemetry.trackAPICall('GET', '/api/users', 200, 150)).not.toThrow();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('can be called with additional attributes', () => {
|
|
278
|
+
expect(() => telemetry.trackAPICall('POST', '/api/users', 201, 200, { user_id: '123' })).not.toThrow();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('handles error status codes', () => {
|
|
282
|
+
expect(() => telemetry.trackAPICall('GET', '/api/users', 500, 100)).not.toThrow();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('handles 4xx status codes', () => {
|
|
286
|
+
expect(() => telemetry.trackAPICall('GET', '/api/users', 404, 50)).not.toThrow();
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe('trackUserAction', () => {
|
|
291
|
+
it('exports trackUserAction function', () => {
|
|
292
|
+
expect(telemetry.trackUserAction).toBeDefined();
|
|
293
|
+
expect(typeof telemetry.trackUserAction).toBe('function');
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it('can be called with action only', () => {
|
|
297
|
+
expect(() => telemetry.trackUserAction('login')).not.toThrow();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('can be called with action and metadata', () => {
|
|
301
|
+
expect(() => telemetry.trackUserAction('login', { method: 'email' })).not.toThrow();
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe('shutdownTelemetry', () => {
|
|
306
|
+
it('exports shutdownTelemetry function', () => {
|
|
307
|
+
expect(telemetry.shutdownTelemetry).toBeDefined();
|
|
308
|
+
expect(typeof telemetry.shutdownTelemetry).toBe('function');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('can be called without errors', async () => {
|
|
312
|
+
await expect(telemetry.shutdownTelemetry()).resolves.not.toThrow();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('handles shutdown when not initialized', async () => {
|
|
316
|
+
vi.resetModules();
|
|
317
|
+
const tempWindow = global.window;
|
|
318
|
+
delete global.window;
|
|
319
|
+
|
|
320
|
+
const freshTelemetry = await import('./telemetry.js');
|
|
321
|
+
global.window = tempWindow;
|
|
322
|
+
|
|
323
|
+
await expect(freshTelemetry.shutdownTelemetry()).resolves.not.toThrow();
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
describe('Module auto-initialization', () => {
|
|
328
|
+
it('adds DOMContentLoaded listener when document is loading', async () => {
|
|
329
|
+
vi.resetModules();
|
|
330
|
+
|
|
331
|
+
global.document = {
|
|
332
|
+
readyState: 'loading',
|
|
333
|
+
addEventListener: vi.fn(),
|
|
334
|
+
title: 'Test',
|
|
335
|
+
referrer: '',
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
await import('./telemetry.js');
|
|
339
|
+
|
|
340
|
+
expect(global.document.addEventListener).toHaveBeenCalledWith(
|
|
341
|
+
'DOMContentLoaded',
|
|
342
|
+
expect.any(Function)
|
|
343
|
+
);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it('initializes immediately when document is ready', async () => {
|
|
347
|
+
vi.resetModules();
|
|
348
|
+
|
|
349
|
+
global.document = {
|
|
350
|
+
readyState: 'complete',
|
|
351
|
+
title: 'Test',
|
|
352
|
+
referrer: '',
|
|
353
|
+
addEventListener: vi.fn(),
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
await import('./telemetry.js');
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('adds beforeunload listener for cleanup', async () => {
|
|
360
|
+
vi.resetModules();
|
|
361
|
+
|
|
362
|
+
global.window.addEventListener = vi.fn();
|
|
363
|
+
|
|
364
|
+
await import('./telemetry.js');
|
|
365
|
+
|
|
366
|
+
expect(global.window.addEventListener).toHaveBeenCalledWith(
|
|
367
|
+
'beforeunload',
|
|
368
|
+
expect.any(Function)
|
|
369
|
+
);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
describe('SSR trace context connection', () => {
|
|
374
|
+
it('handles missing SSR trace context', async () => {
|
|
375
|
+
vi.resetModules();
|
|
376
|
+
|
|
377
|
+
global.window.__TRACE_CONTEXT__ = null;
|
|
378
|
+
|
|
379
|
+
await expect(import('./telemetry.js')).resolves.not.toThrow();
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it('handles SSR trace context when present', async () => {
|
|
383
|
+
vi.resetModules();
|
|
384
|
+
|
|
385
|
+
global.window.__TRACE_CONTEXT__ = {
|
|
386
|
+
traceId: 'test-trace-id',
|
|
387
|
+
spanId: 'test-span-id',
|
|
388
|
+
traceFlags: 1,
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
await expect(import('./telemetry.js')).resolves.not.toThrow();
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
describe('Telemetry with traces disabled', () => {
|
|
397
|
+
let telemetry;
|
|
398
|
+
|
|
399
|
+
beforeEach(async () => {
|
|
400
|
+
vi.resetModules();
|
|
401
|
+
|
|
402
|
+
vi.stubEnv('VITE_OTEL_TRACES_ENABLED', 'false');
|
|
403
|
+
|
|
404
|
+
global.window = {
|
|
405
|
+
location: {
|
|
406
|
+
pathname: '/performers',
|
|
407
|
+
search: '',
|
|
408
|
+
hash: '',
|
|
409
|
+
href: 'http://localhost/performers',
|
|
410
|
+
},
|
|
411
|
+
addEventListener: vi.fn(),
|
|
412
|
+
__TRACE_CONTEXT__: null,
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
global.document = {
|
|
416
|
+
readyState: 'complete',
|
|
417
|
+
title: 'Test',
|
|
418
|
+
referrer: '',
|
|
419
|
+
addEventListener: vi.fn(),
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
global.navigator = {
|
|
423
|
+
userAgent: 'test',
|
|
424
|
+
language: 'en',
|
|
425
|
+
platform: 'test',
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
telemetry = await import('./telemetry.js');
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
afterEach(() => {
|
|
432
|
+
vi.unstubAllEnvs();
|
|
433
|
+
vi.clearAllMocks();
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('initTelemetry returns early when tracing is disabled', () => {
|
|
437
|
+
const consoleSpy = vi.spyOn(console, 'log');
|
|
438
|
+
telemetry.initTelemetry();
|
|
439
|
+
expect(consoleSpy).toHaveBeenCalledWith('[Telemetry] Tracing is disabled');
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
describe('Telemetry debug mode', () => {
|
|
444
|
+
let telemetry;
|
|
445
|
+
|
|
446
|
+
beforeEach(async () => {
|
|
447
|
+
vi.resetModules();
|
|
448
|
+
|
|
449
|
+
vi.stubEnv('VITE_OTEL_DEBUG', 'true');
|
|
450
|
+
|
|
451
|
+
global.window = {
|
|
452
|
+
location: {
|
|
453
|
+
pathname: '/performers',
|
|
454
|
+
search: '',
|
|
455
|
+
hash: '',
|
|
456
|
+
href: 'http://localhost/performers',
|
|
457
|
+
},
|
|
458
|
+
addEventListener: vi.fn(),
|
|
459
|
+
__TRACE_CONTEXT__: null,
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
global.document = {
|
|
463
|
+
readyState: 'complete',
|
|
464
|
+
title: 'Test',
|
|
465
|
+
referrer: '',
|
|
466
|
+
addEventListener: vi.fn(),
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
global.navigator = {
|
|
470
|
+
userAgent: 'test',
|
|
471
|
+
language: 'en',
|
|
472
|
+
platform: 'test',
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
telemetry = await import('./telemetry.js');
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
afterEach(() => {
|
|
479
|
+
vi.unstubAllEnvs();
|
|
480
|
+
vi.clearAllMocks();
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it('can initialize with debug mode enabled', () => {
|
|
484
|
+
expect(() => telemetry.initTelemetry()).not.toThrow();
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
describe('Telemetry shutdown error handling', () => {
|
|
489
|
+
let telemetry;
|
|
490
|
+
|
|
491
|
+
beforeEach(async () => {
|
|
492
|
+
vi.resetModules();
|
|
493
|
+
|
|
494
|
+
// Mock shutdown to throw error
|
|
495
|
+
vi.mock('@opentelemetry/sdk-trace-web', () => ({
|
|
496
|
+
WebTracerProvider: vi.fn().mockImplementation(() => ({
|
|
497
|
+
addSpanProcessor: vi.fn(),
|
|
498
|
+
register: vi.fn(),
|
|
499
|
+
getTracer: vi.fn().mockReturnValue({
|
|
500
|
+
startSpan: vi.fn().mockReturnValue({
|
|
501
|
+
setAttribute: vi.fn(),
|
|
502
|
+
setStatus: vi.fn(),
|
|
503
|
+
recordException: vi.fn(),
|
|
504
|
+
end: vi.fn(),
|
|
505
|
+
name: 'test-span',
|
|
506
|
+
}),
|
|
507
|
+
}),
|
|
508
|
+
shutdown: vi.fn().mockRejectedValue(new Error('Shutdown failed')),
|
|
509
|
+
})),
|
|
510
|
+
}));
|
|
511
|
+
|
|
512
|
+
global.window = {
|
|
513
|
+
location: {
|
|
514
|
+
pathname: '/performers',
|
|
515
|
+
search: '',
|
|
516
|
+
hash: '',
|
|
517
|
+
href: 'http://localhost/performers',
|
|
518
|
+
},
|
|
519
|
+
addEventListener: vi.fn(),
|
|
520
|
+
__TRACE_CONTEXT__: null,
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
global.document = {
|
|
524
|
+
readyState: 'complete',
|
|
525
|
+
title: 'Test',
|
|
526
|
+
referrer: '',
|
|
527
|
+
addEventListener: vi.fn(),
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
global.navigator = {
|
|
531
|
+
userAgent: 'test',
|
|
532
|
+
language: 'en',
|
|
533
|
+
platform: 'test',
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
telemetry = await import('./telemetry.js');
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
afterEach(() => {
|
|
540
|
+
vi.unstubAllEnvs();
|
|
541
|
+
vi.clearAllMocks();
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it('handles shutdown error gracefully', async () => {
|
|
545
|
+
const consoleErrorSpy = vi.spyOn(console, 'error');
|
|
546
|
+
telemetry.initTelemetry();
|
|
547
|
+
await telemetry.shutdownTelemetry();
|
|
548
|
+
// Should not throw, just log error
|
|
549
|
+
expect(consoleErrorSpy).toBeDefined();
|
|
550
|
+
});
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
describe('Telemetry DOMContentLoaded event', () => {
|
|
554
|
+
let telemetry;
|
|
555
|
+
let domContentLoadedCallback;
|
|
556
|
+
|
|
557
|
+
beforeEach(async () => {
|
|
558
|
+
vi.resetModules();
|
|
559
|
+
|
|
560
|
+
global.window = {
|
|
561
|
+
location: {
|
|
562
|
+
pathname: '/performers',
|
|
563
|
+
search: '',
|
|
564
|
+
hash: '',
|
|
565
|
+
href: 'http://localhost/performers',
|
|
566
|
+
},
|
|
567
|
+
addEventListener: vi.fn(),
|
|
568
|
+
__TRACE_CONTEXT__: null,
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
// Mock document with loading state
|
|
572
|
+
global.document = {
|
|
573
|
+
readyState: 'loading',
|
|
574
|
+
title: 'Test',
|
|
575
|
+
referrer: '',
|
|
576
|
+
addEventListener: vi.fn((event, callback) => {
|
|
577
|
+
if (event === 'DOMContentLoaded') {
|
|
578
|
+
domContentLoadedCallback = callback;
|
|
579
|
+
}
|
|
580
|
+
}),
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
global.navigator = {
|
|
584
|
+
userAgent: 'test',
|
|
585
|
+
language: 'en',
|
|
586
|
+
platform: 'test',
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
telemetry = await import('./telemetry.js');
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
afterEach(() => {
|
|
593
|
+
vi.unstubAllEnvs();
|
|
594
|
+
vi.clearAllMocks();
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
it('registers DOMContentLoaded event listener when document is loading', () => {
|
|
598
|
+
expect(document.addEventListener).toHaveBeenCalledWith('DOMContentLoaded', expect.any(Function));
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
it('calls initTelemetry and connectWithSSRTrace when DOMContentLoaded fires', () => {
|
|
602
|
+
// Trigger the DOMContentLoaded callback
|
|
603
|
+
if (domContentLoadedCallback) {
|
|
604
|
+
expect(() => domContentLoadedCallback()).not.toThrow();
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
describe('Telemetry beforeunload event', () => {
|
|
610
|
+
let beforeUnloadCallback;
|
|
611
|
+
|
|
612
|
+
beforeEach(async () => {
|
|
613
|
+
vi.resetModules();
|
|
614
|
+
|
|
615
|
+
global.window = {
|
|
616
|
+
location: {
|
|
617
|
+
pathname: '/performers',
|
|
618
|
+
search: '',
|
|
619
|
+
hash: '',
|
|
620
|
+
href: 'http://localhost/performers',
|
|
621
|
+
},
|
|
622
|
+
addEventListener: vi.fn((event, callback) => {
|
|
623
|
+
if (event === 'beforeunload') {
|
|
624
|
+
beforeUnloadCallback = callback;
|
|
625
|
+
}
|
|
626
|
+
}),
|
|
627
|
+
__TRACE_CONTEXT__: null,
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
global.document = {
|
|
631
|
+
readyState: 'complete',
|
|
632
|
+
title: 'Test',
|
|
633
|
+
referrer: '',
|
|
634
|
+
addEventListener: vi.fn(),
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
global.navigator = {
|
|
638
|
+
userAgent: 'test',
|
|
639
|
+
language: 'en',
|
|
640
|
+
platform: 'test',
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
await import('./telemetry.js');
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
afterEach(() => {
|
|
647
|
+
vi.unstubAllEnvs();
|
|
648
|
+
vi.clearAllMocks();
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
it('registers beforeunload event listener', () => {
|
|
652
|
+
expect(window.addEventListener).toHaveBeenCalledWith('beforeunload', expect.any(Function));
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
it('calls shutdownTelemetry when beforeunload fires', () => {
|
|
656
|
+
if (beforeUnloadCallback) {
|
|
657
|
+
expect(() => beforeUnloadCallback()).not.toThrow();
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
});
|