@getmicdrop/svelte-components 5.5.5 → 5.6.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/calendar/AboutShow/AboutShow.svelte +172 -172
- package/dist/calendar/Calendar/MiniMonthCalendar.svelte +782 -782
- package/dist/calendar/FAQs/FAQs.svelte +75 -75
- package/dist/calendar/MonthSwitcher/MonthSwitcher.svelte +126 -126
- package/dist/calendar/OrderSummary/OrderSummary.svelte +367 -367
- package/dist/calendar/PublicCard/PublicCard.svelte +134 -134
- package/dist/calendar/ShowCard/ShowCard.svelte +157 -157
- package/dist/calendar/ShowTimeCard/ShowTimeCard.svelte +61 -61
- package/dist/components/Layout/AppShell.svelte +104 -0
- package/dist/components/Layout/AppShell.svelte.d.ts +26 -0
- package/dist/components/Layout/AppShell.svelte.d.ts.map +1 -0
- package/dist/components/Layout/ContentSection.svelte +80 -0
- package/dist/components/Layout/ContentSection.svelte.d.ts +23 -0
- package/dist/components/Layout/ContentSection.svelte.d.ts.map +1 -0
- package/dist/components/Layout/Grid.svelte +4 -4
- package/dist/components/Layout/Heading.svelte +81 -0
- package/dist/components/Layout/Heading.svelte.d.ts +24 -0
- package/dist/components/Layout/Heading.svelte.d.ts.map +1 -0
- package/dist/components/Layout/PageContainer.svelte +69 -0
- package/dist/components/Layout/PageContainer.svelte.d.ts +23 -0
- package/dist/components/Layout/PageContainer.svelte.d.ts.map +1 -0
- package/dist/components/Layout/Responsive.svelte +75 -0
- package/dist/components/Layout/Responsive.svelte.d.ts +19 -0
- package/dist/components/Layout/Responsive.svelte.d.ts.map +1 -0
- package/dist/components/Layout/Section.svelte +80 -80
- package/dist/components/Layout/ShowOnDesktop.svelte +37 -0
- package/dist/components/Layout/ShowOnDesktop.svelte.d.ts +16 -0
- package/dist/components/Layout/ShowOnDesktop.svelte.d.ts.map +1 -0
- package/dist/components/Layout/ShowOnMobile.svelte +37 -0
- package/dist/components/Layout/ShowOnMobile.svelte.d.ts +16 -0
- package/dist/components/Layout/ShowOnMobile.svelte.d.ts.map +1 -0
- package/dist/components/Layout/Sidebar.svelte +108 -108
- package/dist/components/Layout/Stack.spec.js +1 -1
- package/dist/components/Layout/Stack.svelte +6 -6
- package/dist/components/Layout/Text.svelte +87 -0
- package/dist/components/Layout/Text.svelte.d.ts +28 -0
- package/dist/components/Layout/Text.svelte.d.ts.map +1 -0
- package/dist/components/Layout/TwoColumn.svelte +108 -0
- package/dist/components/Layout/TwoColumn.svelte.d.ts +28 -0
- package/dist/components/Layout/TwoColumn.svelte.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/Heading.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/Heading.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/Heading.test.js +123 -0
- package/dist/components/Layout/__tests__/ShowOnDesktop.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/ShowOnDesktop.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/ShowOnDesktop.test.js +84 -0
- package/dist/components/Layout/__tests__/ShowOnMobile.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/ShowOnMobile.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/ShowOnMobile.test.js +80 -0
- package/dist/components/Layout/__tests__/Text.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/Text.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/Text.test.js +146 -0
- package/dist/components/Layout/__tests__/TwoColumn.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/TwoColumn.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/TwoColumn.test.js +129 -0
- package/dist/constants/validation.js +91 -91
- package/dist/constants/validation.spec.js +64 -64
- package/dist/datetime/__tests__/format.test.js +1 -1
- package/dist/datetime/__tests__/parse.test.js +1 -1
- package/dist/datetime/__tests__/timezone.test.js +1 -1
- package/dist/datetime/parse.js +1 -1
- package/dist/forms/createFormStore.svelte.js +1 -0
- package/dist/index.js +40 -40
- package/dist/patterns/data/DataGrid.svelte +45 -45
- package/dist/patterns/data/DataList.svelte +24 -24
- package/dist/patterns/data/DataTable.svelte +36 -36
- package/dist/patterns/forms/FormActions.spec.js +95 -95
- package/dist/patterns/forms/FormActions.stories.svelte +97 -97
- package/dist/patterns/forms/FormActions.svelte +46 -46
- package/dist/patterns/forms/FormGrid.svelte +33 -33
- package/dist/patterns/forms/FormSection.svelte +32 -32
- package/dist/patterns/forms/FormValidationSummary.stories.svelte +83 -83
- package/dist/patterns/forms/FormValidationSummary.svelte +74 -74
- package/dist/patterns/layout/Sidebar.svelte +39 -39
- package/dist/patterns/layout/index.d.ts +9 -0
- package/dist/patterns/layout/index.js +22 -0
- package/dist/patterns/navigation/BottomNav.stories.svelte +117 -117
- package/dist/patterns/navigation/BottomNav.svelte +64 -64
- package/dist/patterns/navigation/Header.stories.svelte +77 -77
- package/dist/patterns/navigation/Header.svelte +193 -193
- package/dist/patterns/page/PageHeader.svelte +18 -18
- package/dist/patterns/page/PageLayout.svelte +40 -40
- package/dist/patterns/page/PageLoader.spec.js +57 -57
- package/dist/patterns/page/PageLoader.stories.svelte +137 -137
- package/dist/patterns/page/PageLoader.svelte +24 -24
- package/dist/patterns/page/SectionHeader.svelte +29 -29
- package/dist/presets/badges.js +112 -112
- package/dist/presets/buttons.js +76 -76
- package/dist/presets/index.js +9 -9
- package/dist/primitives/Accordion/Accordion.stories.svelte +75 -75
- package/dist/primitives/Accordion/Accordion.svelte +42 -42
- package/dist/primitives/Accordion/AccordionItem.svelte +95 -95
- package/dist/primitives/Alert/Alert.spec.js +173 -173
- package/dist/primitives/Alert/Alert.stories.svelte +88 -88
- package/dist/primitives/Alert/Alert.svelte +27 -27
- package/dist/primitives/Avatar/Avatar.stories.svelte +94 -94
- package/dist/primitives/Avatar/Avatar.svelte +66 -66
- package/dist/primitives/Badges/Badge.spec.js +144 -144
- package/dist/primitives/Badges/Badge.stories.svelte +86 -86
- package/dist/primitives/Badges/Badge.svelte +79 -79
- package/dist/primitives/BottomSheet/BottomSheet.spec.js +136 -136
- package/dist/primitives/BottomSheet/BottomSheet.stories.svelte +83 -83
- package/dist/primitives/BottomSheet/BottomSheet.svelte +100 -100
- package/dist/primitives/Breadcrumb/Breadcrumb.spec.js +122 -122
- package/dist/primitives/Breadcrumb/Breadcrumb.stories.svelte +23 -23
- package/dist/primitives/Breadcrumb/Breadcrumb.svelte +89 -89
- package/dist/primitives/Button/Button.spec.js +223 -223
- package/dist/primitives/Button/Button.stories.svelte +76 -76
- package/dist/primitives/Button/Button.svelte +270 -270
- package/dist/primitives/Button/ButtonSaveDemo.spec.js +146 -146
- package/dist/primitives/Button/ButtonSaveDemo.svelte +25 -25
- package/dist/primitives/Button/ButtonVariantShowcase.svelte +129 -129
- package/dist/primitives/Card.spec.js +49 -49
- package/dist/primitives/Card.stories.svelte +22 -22
- package/dist/primitives/Card.svelte +28 -28
- package/dist/primitives/Checkbox/Checkbox.stories.svelte +84 -84
- package/dist/primitives/Checkbox/Checkbox.svelte +88 -88
- package/dist/primitives/DarkModeToggle.spec.js +390 -390
- package/dist/primitives/DarkModeToggle.stories.svelte +57 -57
- package/dist/primitives/DarkModeToggle.svelte +136 -136
- package/dist/primitives/Drawer/Drawer.stories.svelte +80 -80
- package/dist/primitives/Drawer/Drawer.svelte +120 -120
- package/dist/primitives/Dropdown/Dropdown.stories.svelte +137 -137
- package/dist/primitives/Dropdown/Dropdown.svelte +14 -14
- package/dist/primitives/Dropdown/DropdownItem.svelte +80 -80
- package/dist/primitives/Icons/ArrowLeft.svelte +8 -8
- package/dist/primitives/Icons/ArrowRight.svelte +8 -8
- package/dist/primitives/Icons/Availability.svelte +14 -14
- package/dist/primitives/Icons/Back.svelte +14 -14
- package/dist/primitives/Icons/CheckCircle.svelte +6 -6
- package/dist/primitives/Icons/CheckCircleOutline.svelte +15 -15
- package/dist/primitives/Icons/ChevronLeft.svelte +4 -4
- package/dist/primitives/Icons/ChevronRight.svelte +4 -4
- package/dist/primitives/Icons/Copy.svelte +15 -15
- package/dist/primitives/Icons/Cross.svelte +5 -5
- package/dist/primitives/Icons/DownArrow.svelte +8 -8
- package/dist/primitives/Icons/ErrorCircle.svelte +6 -6
- package/dist/primitives/Icons/FacebookIcon.svelte +2 -2
- package/dist/primitives/Icons/Home.svelte +15 -15
- package/dist/primitives/Icons/Icon.spec.js +169 -169
- package/dist/primitives/Icons/Icon.stories.svelte +100 -100
- package/dist/primitives/Icons/Icon.svelte +52 -52
- package/dist/primitives/Icons/IconGallery.stories.svelte +235 -235
- package/dist/primitives/Icons/Info.svelte +7 -7
- package/dist/primitives/Icons/InstagramIcon.svelte +4 -4
- package/dist/primitives/Icons/LogoInstagram.svelte +2 -2
- package/dist/primitives/Icons/Message.svelte +15 -15
- package/dist/primitives/Icons/MoonIcon.svelte +5 -5
- package/dist/primitives/Icons/More.svelte +21 -21
- package/dist/primitives/Icons/MoreHori.spec.js +61 -61
- package/dist/primitives/Icons/MoreHori.svelte +22 -22
- package/dist/primitives/Icons/Notification.svelte +14 -14
- package/dist/primitives/Icons/Payment.svelte +14 -14
- package/dist/primitives/Icons/Profile.svelte +21 -21
- package/dist/primitives/Icons/Reload.svelte +29 -29
- package/dist/primitives/Icons/Shows.svelte +21 -21
- package/dist/primitives/Icons/Signout.svelte +21 -21
- package/dist/primitives/Icons/SunIcon.svelte +8 -8
- package/dist/primitives/Icons/TiktokIcon.svelte +2 -2
- package/dist/primitives/Icons/TwitterIcon.svelte +2 -2
- package/dist/primitives/Icons/WarningIcon.spec.js +18 -18
- package/dist/primitives/Icons/WarningIcon.svelte +5 -5
- package/dist/primitives/Input/Input.spec.js +573 -573
- package/dist/primitives/Input/Input.stories.svelte +139 -139
- package/dist/primitives/Input/Input.svelte +417 -417
- package/dist/primitives/Input/Select.spec.js +212 -212
- package/dist/primitives/Input/Select.stories.svelte +112 -112
- package/dist/primitives/Input/Select.svelte +128 -128
- package/dist/primitives/Input/Textarea.stories.svelte +137 -137
- package/dist/primitives/Input/Textarea.svelte +35 -35
- package/dist/primitives/Label/Label.svelte +37 -37
- package/dist/primitives/Modal/Modal.spec.js +99 -99
- package/dist/primitives/Modal/Modal.stories.svelte +86 -86
- package/dist/primitives/Modal/Modal.svelte +158 -158
- package/dist/primitives/NumberInput/NumberInput.svelte +106 -106
- package/dist/primitives/Pagination/Pagination.stories.svelte +76 -76
- package/dist/primitives/Pagination/Pagination.svelte +261 -261
- package/dist/primitives/Radio/Radio.stories.svelte +80 -80
- package/dist/primitives/Radio/Radio.svelte +67 -67
- package/dist/primitives/Skeleton/CardPlaceholder.svelte +87 -87
- package/dist/primitives/Skeleton/ImagePlaceholder.svelte +59 -59
- package/dist/primitives/Skeleton/ListPlaceholder.svelte +76 -76
- package/dist/primitives/Skeleton/Skeleton.stories.svelte +151 -151
- package/dist/primitives/Skeleton/Skeleton.svelte +26 -26
- package/dist/primitives/Spinner/Spinner.spec.js +71 -71
- package/dist/primitives/Spinner/Spinner.stories.svelte +29 -29
- package/dist/primitives/Spinner/Spinner.svelte +20 -20
- package/dist/primitives/Tabs/TabItem.svelte +49 -49
- package/dist/primitives/Tabs/Tabs.stories.svelte +112 -112
- package/dist/primitives/Tabs/Tabs.svelte +123 -123
- package/dist/primitives/Toggle.spec.js +143 -143
- package/dist/primitives/Toggle.stories.svelte +92 -92
- package/dist/primitives/Toggle.svelte +71 -71
- package/dist/primitives/Typography/Typography.svelte +53 -53
- package/dist/primitives/ValidationError.spec.js +103 -103
- package/dist/primitives/ValidationError.stories.svelte +69 -69
- package/dist/primitives/ValidationError.svelte +29 -29
- package/dist/primitives/index.js +84 -84
- package/dist/recipes/CropImage/CropImage.spec.js +208 -208
- package/dist/recipes/CropImage/CropImage.stories.svelte +104 -104
- package/dist/recipes/CropImage/CropImage.svelte +238 -238
- package/dist/recipes/ImageUploader/ImageUploader.stories.svelte +125 -125
- package/dist/recipes/ImageUploader/ImageUploader.svelte +804 -804
- package/dist/recipes/SuperLogin/SuperLogin.spec.js +17 -17
- package/dist/recipes/Toaster/Toaster.stories.svelte +62 -62
- package/dist/recipes/feedback/EmptyState/EmptyState.svelte +1 -1
- package/dist/recipes/feedback/ErrorDisplay.spec.js +69 -69
- package/dist/recipes/feedback/ErrorDisplay.stories.svelte +101 -101
- package/dist/recipes/feedback/ErrorDisplay.svelte +1 -1
- package/dist/recipes/feedback/StatusIndicator/StatusIndicator.spec.js +133 -133
- package/dist/recipes/feedback/StatusIndicator/StatusIndicator.svelte +157 -157
- package/dist/recipes/fields/CheckboxField.svelte +85 -85
- package/dist/recipes/fields/FormField.svelte +58 -58
- package/dist/recipes/fields/RadioGroup.svelte +95 -95
- package/dist/recipes/fields/SelectField.svelte +80 -80
- package/dist/recipes/fields/TextareaField.svelte +97 -97
- package/dist/recipes/fields/ToggleField.svelte +60 -60
- package/dist/recipes/fields/index.js +7 -7
- package/dist/recipes/inputs/MultiSelect.spec.js +258 -258
- package/dist/recipes/inputs/MultiSelect.stories.svelte +133 -133
- package/dist/recipes/inputs/MultiSelect.svelte +256 -256
- package/dist/recipes/inputs/OTPInput.spec.js +251 -251
- package/dist/recipes/inputs/OTPInput.stories.svelte +162 -162
- package/dist/recipes/inputs/OTPInput.svelte +29 -29
- package/dist/recipes/inputs/PasswordInput.svelte +22 -22
- package/dist/recipes/inputs/PasswordStrengthIndicator/PasswordStrengthIndicator.svelte +117 -117
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.stories.svelte +123 -123
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte +326 -326
- package/dist/recipes/inputs/Search.svelte +37 -37
- package/dist/recipes/inputs/SelectDropdown.svelte +57 -57
- package/dist/recipes/modals/AlertModal.svelte +130 -130
- package/dist/recipes/modals/ConfirmationModal.spec.js +206 -206
- package/dist/recipes/modals/ConfirmationModal.stories.svelte +119 -119
- package/dist/recipes/modals/ConfirmationModal.svelte +152 -152
- package/dist/recipes/modals/InputModal.svelte +182 -182
- package/dist/recipes/modals/ModalStateManager.spec.js +100 -100
- package/dist/recipes/modals/ModalStateManager.svelte +77 -77
- package/dist/recipes/modals/ModalTestWrapper.svelte +65 -65
- package/dist/recipes/modals/StatusModal.svelte +206 -206
- package/dist/services/EventService.js +75 -75
- package/dist/services/EventService.spec.js +217 -217
- package/dist/services/ShowService.spec.js +345 -345
- package/dist/stores/auth.js +36 -36
- package/dist/stores/auth.spec.js +139 -139
- package/dist/stores/toaster.js +13 -13
- package/dist/stories/ButtonAuditReview.stories.svelte +14 -14
- package/dist/stories/ButtonAuditReview.svelte +427 -427
- package/dist/stories/PatternsGallery.stories.svelte +19 -19
- package/dist/stories/PatternsGallery.svelte +206 -206
- package/dist/stories/PrimitivesGallery.stories.svelte +19 -19
- package/dist/stories/PrimitivesGallery.svelte +725 -725
- package/dist/stories/RecipesGallery.stories.svelte +19 -19
- package/dist/stories/RecipesGallery.svelte +271 -271
- package/dist/stories/button-audit-manifest.json +11186 -11186
- package/dist/tailwind/preset.cjs +82 -82
- package/dist/telemetry.js +405 -405
- package/dist/telemetry.spec.js +1144 -1144
- package/dist/tokens/__tests__/typography-base.test.d.ts +2 -0
- package/dist/tokens/__tests__/typography-base.test.d.ts.map +1 -0
- package/dist/tokens/__tests__/typography-base.test.js +138 -0
- package/dist/tokens/tokens.css +87 -87
- package/dist/tokens/typography-base.css +163 -0
- package/dist/utils/apiConfig.spec.js +219 -219
- package/dist/utils/transitions.js +62 -62
- package/dist/utils/utils.js +354 -354
- package/package.json +292 -291
package/dist/telemetry.spec.js
CHANGED
|
@@ -1,1144 +1,1144 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
3
|
-
|
|
4
|
-
// Mock all OpenTelemetry modules before importing telemetry
|
|
5
|
-
vi.mock('@opentelemetry/sdk-trace-web', () => ({
|
|
6
|
-
WebTracerProvider: vi.fn().mockImplementation(() => ({
|
|
7
|
-
addSpanProcessor: vi.fn(),
|
|
8
|
-
register: vi.fn(),
|
|
9
|
-
getTracer: vi.fn().mockReturnValue({
|
|
10
|
-
startSpan: vi.fn().mockReturnValue({
|
|
11
|
-
setAttribute: vi.fn(),
|
|
12
|
-
setStatus: vi.fn(),
|
|
13
|
-
recordException: vi.fn(),
|
|
14
|
-
end: vi.fn(),
|
|
15
|
-
name: 'test-span',
|
|
16
|
-
}),
|
|
17
|
-
}),
|
|
18
|
-
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
19
|
-
})),
|
|
20
|
-
}));
|
|
21
|
-
|
|
22
|
-
vi.mock('@opentelemetry/sdk-trace-base', () => ({
|
|
23
|
-
BatchSpanProcessor: vi.fn(),
|
|
24
|
-
}));
|
|
25
|
-
|
|
26
|
-
vi.mock('@opentelemetry/exporter-trace-otlp-http', () => ({
|
|
27
|
-
OTLPTraceExporter: vi.fn(),
|
|
28
|
-
}));
|
|
29
|
-
|
|
30
|
-
vi.mock('@opentelemetry/resources', () => ({
|
|
31
|
-
Resource: vi.fn(),
|
|
32
|
-
}));
|
|
33
|
-
|
|
34
|
-
vi.mock('@opentelemetry/semantic-conventions', () => ({
|
|
35
|
-
SEMRESATTRS_SERVICE_NAME: 'service.name',
|
|
36
|
-
SEMRESATTRS_SERVICE_VERSION: 'service.version',
|
|
37
|
-
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT: 'deployment.environment',
|
|
38
|
-
}));
|
|
39
|
-
|
|
40
|
-
vi.mock('@opentelemetry/instrumentation-document-load', () => ({
|
|
41
|
-
DocumentLoadInstrumentation: vi.fn().mockImplementation(() => ({
|
|
42
|
-
enable: vi.fn(),
|
|
43
|
-
})),
|
|
44
|
-
}));
|
|
45
|
-
|
|
46
|
-
vi.mock('@opentelemetry/instrumentation-fetch', () => ({
|
|
47
|
-
FetchInstrumentation: vi.fn().mockImplementation(() => ({
|
|
48
|
-
enable: vi.fn(),
|
|
49
|
-
})),
|
|
50
|
-
}));
|
|
51
|
-
|
|
52
|
-
vi.mock('@opentelemetry/instrumentation-xml-http-request', () => ({
|
|
53
|
-
XMLHttpRequestInstrumentation: vi.fn().mockImplementation(() => ({
|
|
54
|
-
enable: vi.fn(),
|
|
55
|
-
})),
|
|
56
|
-
}));
|
|
57
|
-
|
|
58
|
-
vi.mock('@opentelemetry/instrumentation-user-interaction', () => ({
|
|
59
|
-
UserInteractionInstrumentation: vi.fn().mockImplementation(() => ({
|
|
60
|
-
enable: vi.fn(),
|
|
61
|
-
})),
|
|
62
|
-
}));
|
|
63
|
-
|
|
64
|
-
vi.mock('@opentelemetry/context-zone', () => ({
|
|
65
|
-
ZoneContextManager: vi.fn(),
|
|
66
|
-
}));
|
|
67
|
-
|
|
68
|
-
vi.mock('@opentelemetry/propagator-b3', () => ({
|
|
69
|
-
B3Propagator: vi.fn(),
|
|
70
|
-
}));
|
|
71
|
-
|
|
72
|
-
describe('Telemetry Module', () => {
|
|
73
|
-
let telemetry;
|
|
74
|
-
let originalWindow;
|
|
75
|
-
let originalDocument;
|
|
76
|
-
let mockSpan;
|
|
77
|
-
let mockTracer;
|
|
78
|
-
|
|
79
|
-
beforeEach(async () => {
|
|
80
|
-
vi.resetModules();
|
|
81
|
-
|
|
82
|
-
// Save original globals
|
|
83
|
-
originalWindow = global.window;
|
|
84
|
-
originalDocument = global.document;
|
|
85
|
-
|
|
86
|
-
// Create mock span
|
|
87
|
-
mockSpan = {
|
|
88
|
-
setAttribute: vi.fn(),
|
|
89
|
-
setStatus: vi.fn(),
|
|
90
|
-
recordException: vi.fn(),
|
|
91
|
-
end: vi.fn(),
|
|
92
|
-
name: 'test-span',
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// Create mock tracer
|
|
96
|
-
mockTracer = {
|
|
97
|
-
startSpan: vi.fn().mockReturnValue(mockSpan),
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
// Setup mock window
|
|
101
|
-
global.window = {
|
|
102
|
-
location: {
|
|
103
|
-
pathname: '/performers',
|
|
104
|
-
search: '?test=1',
|
|
105
|
-
hash: '#section',
|
|
106
|
-
href: 'http://localhost/performers?test=1#section',
|
|
107
|
-
},
|
|
108
|
-
addEventListener: vi.fn(),
|
|
109
|
-
__TRACE_CONTEXT__: null,
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
global.document = {
|
|
113
|
-
readyState: 'complete',
|
|
114
|
-
title: 'Test Page',
|
|
115
|
-
referrer: 'http://localhost/',
|
|
116
|
-
addEventListener: vi.fn(),
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
global.navigator = {
|
|
120
|
-
userAgent: 'test-agent',
|
|
121
|
-
language: 'en-US',
|
|
122
|
-
platform: 'test-platform',
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
// Re-import with fresh mocks
|
|
126
|
-
telemetry = await import('./telemetry.js');
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
afterEach(() => {
|
|
130
|
-
global.window = originalWindow;
|
|
131
|
-
global.document = originalDocument;
|
|
132
|
-
vi.clearAllMocks();
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
describe('initTelemetry', () => {
|
|
136
|
-
it('exports initTelemetry function', () => {
|
|
137
|
-
expect(telemetry.initTelemetry).toBeDefined();
|
|
138
|
-
expect(typeof telemetry.initTelemetry).toBe('function');
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('can be called without errors', () => {
|
|
142
|
-
expect(() => telemetry.initTelemetry()).not.toThrow();
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
describe('startSpan', () => {
|
|
147
|
-
it('exports startSpan function', () => {
|
|
148
|
-
expect(telemetry.startSpan).toBeDefined();
|
|
149
|
-
expect(typeof telemetry.startSpan).toBe('function');
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('returns null when tracer is not initialized', async () => {
|
|
153
|
-
vi.resetModules();
|
|
154
|
-
const tempWindow = global.window;
|
|
155
|
-
delete global.window;
|
|
156
|
-
|
|
157
|
-
const freshTelemetry = await import('./telemetry.js');
|
|
158
|
-
global.window = tempWindow;
|
|
159
|
-
|
|
160
|
-
const result = freshTelemetry.startSpan('test-span');
|
|
161
|
-
expect(result).toBeNull();
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it('can be called with name only', () => {
|
|
165
|
-
telemetry.initTelemetry();
|
|
166
|
-
const result = telemetry.startSpan('test-span');
|
|
167
|
-
expect(result === null || typeof result === 'object').toBe(true);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('can be called with name and options', () => {
|
|
171
|
-
telemetry.initTelemetry();
|
|
172
|
-
const result = telemetry.startSpan('test-span', { attributes: { key: 'value' } });
|
|
173
|
-
expect(result === null || typeof result === 'object').toBe(true);
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
describe('endSpan', () => {
|
|
178
|
-
it('exports endSpan function', () => {
|
|
179
|
-
expect(telemetry.endSpan).toBeDefined();
|
|
180
|
-
expect(typeof telemetry.endSpan).toBe('function');
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('handles null span gracefully', () => {
|
|
184
|
-
expect(() => telemetry.endSpan(null)).not.toThrow();
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('handles undefined span gracefully', () => {
|
|
188
|
-
expect(() => telemetry.endSpan(undefined)).not.toThrow();
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it('can end a span with default status', () => {
|
|
192
|
-
expect(() => telemetry.endSpan(mockSpan)).not.toThrow();
|
|
193
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 0 });
|
|
194
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('can end a span with custom status', () => {
|
|
198
|
-
expect(() => telemetry.endSpan(mockSpan, { code: 2, message: 'error' })).not.toThrow();
|
|
199
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2, message: 'error' });
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
describe('trackPageView', () => {
|
|
204
|
-
it('exports trackPageView function', () => {
|
|
205
|
-
expect(telemetry.trackPageView).toBeDefined();
|
|
206
|
-
expect(typeof telemetry.trackPageView).toBe('function');
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
it('can be called without errors', () => {
|
|
210
|
-
expect(() => telemetry.trackPageView('/test-page')).not.toThrow();
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('can be called with page path and title', () => {
|
|
214
|
-
expect(() => telemetry.trackPageView('/test-page', 'Test Page Title')).not.toThrow();
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it('returns early when tracer is not initialized', () => {
|
|
218
|
-
const result = telemetry.trackPageView('/test');
|
|
219
|
-
expect(result).toBeUndefined();
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
describe('trackEvent', () => {
|
|
224
|
-
it('exports trackEvent function', () => {
|
|
225
|
-
expect(telemetry.trackEvent).toBeDefined();
|
|
226
|
-
expect(typeof telemetry.trackEvent).toBe('function');
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('can be called with event name only', () => {
|
|
230
|
-
expect(() => telemetry.trackEvent('button_click')).not.toThrow();
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it('can be called with event name and attributes', () => {
|
|
234
|
-
expect(() => telemetry.trackEvent('button_click', { button_id: 'submit' })).not.toThrow();
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
describe('trackPerformerAction', () => {
|
|
239
|
-
it('exports trackPerformerAction function', () => {
|
|
240
|
-
expect(telemetry.trackPerformerAction).toBeDefined();
|
|
241
|
-
expect(typeof telemetry.trackPerformerAction).toBe('function');
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
it('can be called with action only', () => {
|
|
245
|
-
expect(() => telemetry.trackPerformerAction('profile_update')).not.toThrow();
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it('can be called with action and metadata', () => {
|
|
249
|
-
expect(() => telemetry.trackPerformerAction('profile_update', { field: 'bio' })).not.toThrow();
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
describe('trackError', () => {
|
|
254
|
-
it('exports trackError function', () => {
|
|
255
|
-
expect(telemetry.trackError).toBeDefined();
|
|
256
|
-
expect(typeof telemetry.trackError).toBe('function');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it('can be called with Error object', () => {
|
|
260
|
-
const error = new Error('Test error');
|
|
261
|
-
expect(() => telemetry.trackError(error)).not.toThrow();
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it('can be called with Error and context', () => {
|
|
265
|
-
const error = new Error('Test error');
|
|
266
|
-
expect(() => telemetry.trackError(error, { component: 'TestComponent' })).not.toThrow();
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it('handles error without stack trace', () => {
|
|
270
|
-
const error = { message: 'Simple error', name: 'SimpleError' };
|
|
271
|
-
expect(() => telemetry.trackError(error)).not.toThrow();
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('handles string error', () => {
|
|
275
|
-
expect(() => telemetry.trackError('String error message')).not.toThrow();
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
describe('trackAPICall', () => {
|
|
280
|
-
it('exports trackAPICall function', () => {
|
|
281
|
-
expect(telemetry.trackAPICall).toBeDefined();
|
|
282
|
-
expect(typeof telemetry.trackAPICall).toBe('function');
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it('can be called with basic parameters', () => {
|
|
286
|
-
expect(() => telemetry.trackAPICall('GET', '/api/users', 200, 150)).not.toThrow();
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it('can be called with additional attributes', () => {
|
|
290
|
-
expect(() => telemetry.trackAPICall('POST', '/api/users', 201, 200, { user_id: '123' })).not.toThrow();
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it('handles error status codes', () => {
|
|
294
|
-
expect(() => telemetry.trackAPICall('GET', '/api/users', 500, 100)).not.toThrow();
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
it('handles 4xx status codes', () => {
|
|
298
|
-
expect(() => telemetry.trackAPICall('GET', '/api/users', 404, 50)).not.toThrow();
|
|
299
|
-
});
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
describe('trackUserAction', () => {
|
|
303
|
-
it('exports trackUserAction function', () => {
|
|
304
|
-
expect(telemetry.trackUserAction).toBeDefined();
|
|
305
|
-
expect(typeof telemetry.trackUserAction).toBe('function');
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
it('can be called with action only', () => {
|
|
309
|
-
expect(() => telemetry.trackUserAction('login')).not.toThrow();
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
it('can be called with action and metadata', () => {
|
|
313
|
-
expect(() => telemetry.trackUserAction('login', { method: 'email' })).not.toThrow();
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
describe('shutdownTelemetry', () => {
|
|
318
|
-
it('exports shutdownTelemetry function', () => {
|
|
319
|
-
expect(telemetry.shutdownTelemetry).toBeDefined();
|
|
320
|
-
expect(typeof telemetry.shutdownTelemetry).toBe('function');
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
it('can be called without errors', async () => {
|
|
324
|
-
await expect(telemetry.shutdownTelemetry()).resolves.not.toThrow();
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
it('handles shutdown when not initialized', async () => {
|
|
328
|
-
vi.resetModules();
|
|
329
|
-
const tempWindow = global.window;
|
|
330
|
-
delete global.window;
|
|
331
|
-
|
|
332
|
-
const freshTelemetry = await import('./telemetry.js');
|
|
333
|
-
global.window = tempWindow;
|
|
334
|
-
|
|
335
|
-
await expect(freshTelemetry.shutdownTelemetry()).resolves.not.toThrow();
|
|
336
|
-
});
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
describe('Module auto-initialization', () => {
|
|
340
|
-
it('adds DOMContentLoaded listener when document is loading', async () => {
|
|
341
|
-
vi.resetModules();
|
|
342
|
-
|
|
343
|
-
global.document = {
|
|
344
|
-
readyState: 'loading',
|
|
345
|
-
addEventListener: vi.fn(),
|
|
346
|
-
title: 'Test',
|
|
347
|
-
referrer: '',
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
await import('./telemetry.js');
|
|
351
|
-
|
|
352
|
-
expect(global.document.addEventListener).toHaveBeenCalledWith(
|
|
353
|
-
'DOMContentLoaded',
|
|
354
|
-
expect.any(Function)
|
|
355
|
-
);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it('initializes immediately when document is ready', async () => {
|
|
359
|
-
vi.resetModules();
|
|
360
|
-
|
|
361
|
-
global.document = {
|
|
362
|
-
readyState: 'complete',
|
|
363
|
-
title: 'Test',
|
|
364
|
-
referrer: '',
|
|
365
|
-
addEventListener: vi.fn(),
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
await import('./telemetry.js');
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it('adds beforeunload listener for cleanup', async () => {
|
|
372
|
-
vi.resetModules();
|
|
373
|
-
|
|
374
|
-
global.window.addEventListener = vi.fn();
|
|
375
|
-
|
|
376
|
-
await import('./telemetry.js');
|
|
377
|
-
|
|
378
|
-
expect(global.window.addEventListener).toHaveBeenCalledWith(
|
|
379
|
-
'beforeunload',
|
|
380
|
-
expect.any(Function)
|
|
381
|
-
);
|
|
382
|
-
});
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
describe('SSR trace context connection', () => {
|
|
386
|
-
it('handles missing SSR trace context', async () => {
|
|
387
|
-
vi.resetModules();
|
|
388
|
-
|
|
389
|
-
global.window.__TRACE_CONTEXT__ = null;
|
|
390
|
-
|
|
391
|
-
await expect(import('./telemetry.js')).resolves.not.toThrow();
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
it('handles SSR trace context when present', async () => {
|
|
395
|
-
vi.resetModules();
|
|
396
|
-
|
|
397
|
-
global.window.__TRACE_CONTEXT__ = {
|
|
398
|
-
traceId: 'test-trace-id',
|
|
399
|
-
spanId: 'test-span-id',
|
|
400
|
-
traceFlags: 1,
|
|
401
|
-
};
|
|
402
|
-
|
|
403
|
-
await expect(import('./telemetry.js')).resolves.not.toThrow();
|
|
404
|
-
});
|
|
405
|
-
});
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
describe('Telemetry with traces disabled', () => {
|
|
409
|
-
let telemetry;
|
|
410
|
-
|
|
411
|
-
beforeEach(async () => {
|
|
412
|
-
vi.resetModules();
|
|
413
|
-
|
|
414
|
-
vi.stubEnv('VITE_OTEL_TRACES_ENABLED', 'false');
|
|
415
|
-
|
|
416
|
-
global.window = {
|
|
417
|
-
location: {
|
|
418
|
-
pathname: '/performers',
|
|
419
|
-
search: '',
|
|
420
|
-
hash: '',
|
|
421
|
-
href: 'http://localhost/performers',
|
|
422
|
-
},
|
|
423
|
-
addEventListener: vi.fn(),
|
|
424
|
-
__TRACE_CONTEXT__: null,
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
global.document = {
|
|
428
|
-
readyState: 'complete',
|
|
429
|
-
title: 'Test',
|
|
430
|
-
referrer: '',
|
|
431
|
-
addEventListener: vi.fn(),
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
global.navigator = {
|
|
435
|
-
userAgent: 'test',
|
|
436
|
-
language: 'en',
|
|
437
|
-
platform: 'test',
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
telemetry = await import('./telemetry.js');
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
afterEach(() => {
|
|
444
|
-
vi.unstubAllEnvs();
|
|
445
|
-
vi.clearAllMocks();
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
it('initTelemetry returns early when tracing is disabled', () => {
|
|
449
|
-
// Should return early without throwing when traces are disabled
|
|
450
|
-
expect(() => telemetry.initTelemetry({ tracesEnabled: false })).not.toThrow();
|
|
451
|
-
// Verify tracer is not initialized (trackPageView should be a no-op)
|
|
452
|
-
expect(() => telemetry.trackPageView('/test')).not.toThrow();
|
|
453
|
-
});
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
describe('Telemetry debug mode', () => {
|
|
457
|
-
let telemetry;
|
|
458
|
-
|
|
459
|
-
beforeEach(async () => {
|
|
460
|
-
vi.resetModules();
|
|
461
|
-
|
|
462
|
-
vi.stubEnv('VITE_OTEL_DEBUG', 'true');
|
|
463
|
-
|
|
464
|
-
global.window = {
|
|
465
|
-
location: {
|
|
466
|
-
pathname: '/performers',
|
|
467
|
-
search: '',
|
|
468
|
-
hash: '',
|
|
469
|
-
href: 'http://localhost/performers',
|
|
470
|
-
},
|
|
471
|
-
addEventListener: vi.fn(),
|
|
472
|
-
__TRACE_CONTEXT__: null,
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
global.document = {
|
|
476
|
-
readyState: 'complete',
|
|
477
|
-
title: 'Test',
|
|
478
|
-
referrer: '',
|
|
479
|
-
addEventListener: vi.fn(),
|
|
480
|
-
};
|
|
481
|
-
|
|
482
|
-
global.navigator = {
|
|
483
|
-
userAgent: 'test',
|
|
484
|
-
language: 'en',
|
|
485
|
-
platform: 'test',
|
|
486
|
-
};
|
|
487
|
-
|
|
488
|
-
telemetry = await import('./telemetry.js');
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
afterEach(() => {
|
|
492
|
-
vi.unstubAllEnvs();
|
|
493
|
-
vi.clearAllMocks();
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
it('can initialize with debug mode enabled', () => {
|
|
497
|
-
expect(() => telemetry.initTelemetry()).not.toThrow();
|
|
498
|
-
});
|
|
499
|
-
});
|
|
500
|
-
|
|
501
|
-
describe('Telemetry shutdown error handling', () => {
|
|
502
|
-
let telemetry;
|
|
503
|
-
|
|
504
|
-
beforeEach(async () => {
|
|
505
|
-
vi.resetModules();
|
|
506
|
-
|
|
507
|
-
// Mock shutdown to throw error
|
|
508
|
-
vi.mock('@opentelemetry/sdk-trace-web', () => ({
|
|
509
|
-
WebTracerProvider: vi.fn().mockImplementation(() => ({
|
|
510
|
-
addSpanProcessor: vi.fn(),
|
|
511
|
-
register: vi.fn(),
|
|
512
|
-
getTracer: vi.fn().mockReturnValue({
|
|
513
|
-
startSpan: vi.fn().mockReturnValue({
|
|
514
|
-
setAttribute: vi.fn(),
|
|
515
|
-
setStatus: vi.fn(),
|
|
516
|
-
recordException: vi.fn(),
|
|
517
|
-
end: vi.fn(),
|
|
518
|
-
name: 'test-span',
|
|
519
|
-
}),
|
|
520
|
-
}),
|
|
521
|
-
shutdown: vi.fn().mockRejectedValue(new Error('Shutdown failed')),
|
|
522
|
-
})),
|
|
523
|
-
}));
|
|
524
|
-
|
|
525
|
-
global.window = {
|
|
526
|
-
location: {
|
|
527
|
-
pathname: '/performers',
|
|
528
|
-
search: '',
|
|
529
|
-
hash: '',
|
|
530
|
-
href: 'http://localhost/performers',
|
|
531
|
-
},
|
|
532
|
-
addEventListener: vi.fn(),
|
|
533
|
-
__TRACE_CONTEXT__: null,
|
|
534
|
-
};
|
|
535
|
-
|
|
536
|
-
global.document = {
|
|
537
|
-
readyState: 'complete',
|
|
538
|
-
title: 'Test',
|
|
539
|
-
referrer: '',
|
|
540
|
-
addEventListener: vi.fn(),
|
|
541
|
-
};
|
|
542
|
-
|
|
543
|
-
global.navigator = {
|
|
544
|
-
userAgent: 'test',
|
|
545
|
-
language: 'en',
|
|
546
|
-
platform: 'test',
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
telemetry = await import('./telemetry.js');
|
|
550
|
-
});
|
|
551
|
-
|
|
552
|
-
afterEach(() => {
|
|
553
|
-
vi.unstubAllEnvs();
|
|
554
|
-
vi.clearAllMocks();
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
it('handles shutdown error gracefully', async () => {
|
|
558
|
-
const consoleErrorSpy = vi.spyOn(console, 'error');
|
|
559
|
-
telemetry.initTelemetry();
|
|
560
|
-
await telemetry.shutdownTelemetry();
|
|
561
|
-
// Should not throw, just log error
|
|
562
|
-
expect(consoleErrorSpy).toBeDefined();
|
|
563
|
-
});
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
describe('Telemetry DOMContentLoaded event', () => {
|
|
567
|
-
let telemetry;
|
|
568
|
-
let domContentLoadedCallback;
|
|
569
|
-
|
|
570
|
-
beforeEach(async () => {
|
|
571
|
-
vi.resetModules();
|
|
572
|
-
|
|
573
|
-
global.window = {
|
|
574
|
-
location: {
|
|
575
|
-
pathname: '/performers',
|
|
576
|
-
search: '',
|
|
577
|
-
hash: '',
|
|
578
|
-
href: 'http://localhost/performers',
|
|
579
|
-
},
|
|
580
|
-
addEventListener: vi.fn(),
|
|
581
|
-
__TRACE_CONTEXT__: null,
|
|
582
|
-
};
|
|
583
|
-
|
|
584
|
-
// Mock document with loading state
|
|
585
|
-
global.document = {
|
|
586
|
-
readyState: 'loading',
|
|
587
|
-
title: 'Test',
|
|
588
|
-
referrer: '',
|
|
589
|
-
addEventListener: vi.fn((event, callback) => {
|
|
590
|
-
if (event === 'DOMContentLoaded') {
|
|
591
|
-
domContentLoadedCallback = callback;
|
|
592
|
-
}
|
|
593
|
-
}),
|
|
594
|
-
};
|
|
595
|
-
|
|
596
|
-
global.navigator = {
|
|
597
|
-
userAgent: 'test',
|
|
598
|
-
language: 'en',
|
|
599
|
-
platform: 'test',
|
|
600
|
-
};
|
|
601
|
-
|
|
602
|
-
telemetry = await import('./telemetry.js');
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
afterEach(() => {
|
|
606
|
-
vi.unstubAllEnvs();
|
|
607
|
-
vi.clearAllMocks();
|
|
608
|
-
});
|
|
609
|
-
|
|
610
|
-
it('registers DOMContentLoaded event listener when document is loading', () => {
|
|
611
|
-
expect(document.addEventListener).toHaveBeenCalledWith('DOMContentLoaded', expect.any(Function));
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
it('calls initTelemetry and connectWithSSRTrace when DOMContentLoaded fires', () => {
|
|
615
|
-
// Trigger the DOMContentLoaded callback
|
|
616
|
-
if (domContentLoadedCallback) {
|
|
617
|
-
expect(() => domContentLoadedCallback()).not.toThrow();
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
});
|
|
621
|
-
|
|
622
|
-
describe('Telemetry beforeunload event', () => {
|
|
623
|
-
let beforeUnloadCallback;
|
|
624
|
-
|
|
625
|
-
beforeEach(async () => {
|
|
626
|
-
vi.resetModules();
|
|
627
|
-
|
|
628
|
-
global.window = {
|
|
629
|
-
location: {
|
|
630
|
-
pathname: '/performers',
|
|
631
|
-
search: '',
|
|
632
|
-
hash: '',
|
|
633
|
-
href: 'http://localhost/performers',
|
|
634
|
-
},
|
|
635
|
-
addEventListener: vi.fn((event, callback) => {
|
|
636
|
-
if (event === 'beforeunload') {
|
|
637
|
-
beforeUnloadCallback = callback;
|
|
638
|
-
}
|
|
639
|
-
}),
|
|
640
|
-
__TRACE_CONTEXT__: null,
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
global.document = {
|
|
644
|
-
readyState: 'complete',
|
|
645
|
-
title: 'Test',
|
|
646
|
-
referrer: '',
|
|
647
|
-
addEventListener: vi.fn(),
|
|
648
|
-
};
|
|
649
|
-
|
|
650
|
-
global.navigator = {
|
|
651
|
-
userAgent: 'test',
|
|
652
|
-
language: 'en',
|
|
653
|
-
platform: 'test',
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
await import('./telemetry.js');
|
|
657
|
-
});
|
|
658
|
-
|
|
659
|
-
afterEach(() => {
|
|
660
|
-
vi.unstubAllEnvs();
|
|
661
|
-
vi.clearAllMocks();
|
|
662
|
-
});
|
|
663
|
-
|
|
664
|
-
it('registers beforeunload event listener', () => {
|
|
665
|
-
expect(window.addEventListener).toHaveBeenCalledWith('beforeunload', expect.any(Function));
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
it('calls shutdownTelemetry when beforeunload fires', () => {
|
|
669
|
-
if (beforeUnloadCallback) {
|
|
670
|
-
expect(() => beforeUnloadCallback()).not.toThrow();
|
|
671
|
-
}
|
|
672
|
-
});
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
describe('getTelemetryConfig', () => {
|
|
676
|
-
let telemetry;
|
|
677
|
-
|
|
678
|
-
beforeEach(async () => {
|
|
679
|
-
vi.resetModules();
|
|
680
|
-
|
|
681
|
-
global.window = {
|
|
682
|
-
location: {
|
|
683
|
-
pathname: '/performers',
|
|
684
|
-
search: '',
|
|
685
|
-
hash: '',
|
|
686
|
-
href: 'http://localhost/performers',
|
|
687
|
-
},
|
|
688
|
-
addEventListener: vi.fn(),
|
|
689
|
-
__TRACE_CONTEXT__: null,
|
|
690
|
-
};
|
|
691
|
-
|
|
692
|
-
global.document = {
|
|
693
|
-
readyState: 'complete',
|
|
694
|
-
title: 'Test',
|
|
695
|
-
referrer: '',
|
|
696
|
-
addEventListener: vi.fn(),
|
|
697
|
-
};
|
|
698
|
-
|
|
699
|
-
global.navigator = {
|
|
700
|
-
userAgent: 'test',
|
|
701
|
-
language: 'en',
|
|
702
|
-
platform: 'test',
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
telemetry = await import('./telemetry.js');
|
|
706
|
-
});
|
|
707
|
-
|
|
708
|
-
afterEach(() => {
|
|
709
|
-
vi.unstubAllEnvs();
|
|
710
|
-
vi.clearAllMocks();
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
it('exports getTelemetryConfig function', () => {
|
|
714
|
-
expect(telemetry.getTelemetryConfig).toBeDefined();
|
|
715
|
-
expect(typeof telemetry.getTelemetryConfig).toBe('function');
|
|
716
|
-
});
|
|
717
|
-
|
|
718
|
-
it('returns telemetry configuration object', () => {
|
|
719
|
-
const config = telemetry.getTelemetryConfig();
|
|
720
|
-
expect(config).toBeDefined();
|
|
721
|
-
expect(config).toHaveProperty('serviceName');
|
|
722
|
-
expect(config).toHaveProperty('serviceVersion');
|
|
723
|
-
expect(config).toHaveProperty('environment');
|
|
724
|
-
expect(config).toHaveProperty('tracesEnabled');
|
|
725
|
-
});
|
|
726
|
-
});
|
|
727
|
-
|
|
728
|
-
describe('setupAutoTelemetry', () => {
|
|
729
|
-
let telemetry;
|
|
730
|
-
|
|
731
|
-
beforeEach(async () => {
|
|
732
|
-
vi.resetModules();
|
|
733
|
-
|
|
734
|
-
global.window = {
|
|
735
|
-
location: {
|
|
736
|
-
pathname: '/performers',
|
|
737
|
-
search: '',
|
|
738
|
-
hash: '',
|
|
739
|
-
href: 'http://localhost/performers',
|
|
740
|
-
},
|
|
741
|
-
addEventListener: vi.fn(),
|
|
742
|
-
__TRACE_CONTEXT__: null,
|
|
743
|
-
};
|
|
744
|
-
|
|
745
|
-
global.document = {
|
|
746
|
-
readyState: 'complete',
|
|
747
|
-
title: 'Test',
|
|
748
|
-
referrer: '',
|
|
749
|
-
addEventListener: vi.fn(),
|
|
750
|
-
};
|
|
751
|
-
|
|
752
|
-
global.navigator = {
|
|
753
|
-
userAgent: 'test',
|
|
754
|
-
language: 'en',
|
|
755
|
-
platform: 'test',
|
|
756
|
-
};
|
|
757
|
-
|
|
758
|
-
telemetry = await import('./telemetry.js');
|
|
759
|
-
});
|
|
760
|
-
|
|
761
|
-
afterEach(() => {
|
|
762
|
-
vi.unstubAllEnvs();
|
|
763
|
-
vi.clearAllMocks();
|
|
764
|
-
});
|
|
765
|
-
|
|
766
|
-
it('exports setupAutoTelemetry function', () => {
|
|
767
|
-
expect(telemetry.setupAutoTelemetry).toBeDefined();
|
|
768
|
-
expect(typeof telemetry.setupAutoTelemetry).toBe('function');
|
|
769
|
-
});
|
|
770
|
-
|
|
771
|
-
it('can be called without errors', () => {
|
|
772
|
-
expect(() => telemetry.setupAutoTelemetry()).not.toThrow();
|
|
773
|
-
});
|
|
774
|
-
|
|
775
|
-
it('does not throw when window is undefined', () => {
|
|
776
|
-
const tempWindow = global.window;
|
|
777
|
-
delete global.window;
|
|
778
|
-
|
|
779
|
-
expect(() => telemetry.setupAutoTelemetry()).not.toThrow();
|
|
780
|
-
|
|
781
|
-
global.window = tempWindow;
|
|
782
|
-
});
|
|
783
|
-
});
|
|
784
|
-
|
|
785
|
-
describe('Telemetry tracking with initialized tracer', () => {
|
|
786
|
-
let telemetry;
|
|
787
|
-
let mockSpan;
|
|
788
|
-
let mockTracer;
|
|
789
|
-
let mockTracerProvider;
|
|
790
|
-
|
|
791
|
-
beforeEach(async () => {
|
|
792
|
-
vi.resetModules();
|
|
793
|
-
|
|
794
|
-
// Create mock span
|
|
795
|
-
mockSpan = {
|
|
796
|
-
setAttribute: vi.fn().mockReturnThis(),
|
|
797
|
-
setStatus: vi.fn().mockReturnThis(),
|
|
798
|
-
recordException: vi.fn().mockReturnThis(),
|
|
799
|
-
end: vi.fn(),
|
|
800
|
-
name: 'test-span',
|
|
801
|
-
};
|
|
802
|
-
|
|
803
|
-
// Create mock tracer
|
|
804
|
-
mockTracer = {
|
|
805
|
-
startSpan: vi.fn().mockReturnValue(mockSpan),
|
|
806
|
-
};
|
|
807
|
-
|
|
808
|
-
// Create mock tracer provider
|
|
809
|
-
mockTracerProvider = {
|
|
810
|
-
addSpanProcessor: vi.fn(),
|
|
811
|
-
register: vi.fn(),
|
|
812
|
-
getTracer: vi.fn().mockReturnValue(mockTracer),
|
|
813
|
-
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
814
|
-
};
|
|
815
|
-
|
|
816
|
-
// Mock WebTracerProvider to return our mock
|
|
817
|
-
vi.doMock('@opentelemetry/sdk-trace-web', () => ({
|
|
818
|
-
WebTracerProvider: vi.fn().mockImplementation(() => mockTracerProvider),
|
|
819
|
-
}));
|
|
820
|
-
|
|
821
|
-
// Mock registerInstrumentations to avoid instrumentation errors
|
|
822
|
-
vi.doMock('@opentelemetry/instrumentation', () => ({
|
|
823
|
-
registerInstrumentations: vi.fn(),
|
|
824
|
-
}));
|
|
825
|
-
|
|
826
|
-
global.window = {
|
|
827
|
-
location: {
|
|
828
|
-
pathname: '/performers',
|
|
829
|
-
search: '?test=1',
|
|
830
|
-
hash: '#section',
|
|
831
|
-
href: 'http://localhost/performers?test=1#section',
|
|
832
|
-
},
|
|
833
|
-
addEventListener: vi.fn(),
|
|
834
|
-
__TRACE_CONTEXT__: null,
|
|
835
|
-
};
|
|
836
|
-
|
|
837
|
-
global.document = {
|
|
838
|
-
readyState: 'complete',
|
|
839
|
-
title: 'Test Page',
|
|
840
|
-
referrer: 'http://localhost/',
|
|
841
|
-
addEventListener: vi.fn(),
|
|
842
|
-
};
|
|
843
|
-
|
|
844
|
-
global.navigator = {
|
|
845
|
-
userAgent: 'test-agent',
|
|
846
|
-
language: 'en-US',
|
|
847
|
-
platform: 'test-platform',
|
|
848
|
-
};
|
|
849
|
-
|
|
850
|
-
telemetry = await import('./telemetry.js');
|
|
851
|
-
|
|
852
|
-
// Initialize telemetry to set up tracer
|
|
853
|
-
telemetry.initTelemetry();
|
|
854
|
-
});
|
|
855
|
-
|
|
856
|
-
afterEach(() => {
|
|
857
|
-
vi.clearAllMocks();
|
|
858
|
-
vi.doUnmock('@opentelemetry/sdk-trace-web');
|
|
859
|
-
vi.doUnmock('@opentelemetry/instrumentation');
|
|
860
|
-
});
|
|
861
|
-
|
|
862
|
-
describe('trackPageView with tracer', () => {
|
|
863
|
-
it('creates a span with page attributes', () => {
|
|
864
|
-
telemetry.trackPageView('/test-page', 'Test Title');
|
|
865
|
-
|
|
866
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('page_view', expect.objectContaining({
|
|
867
|
-
attributes: expect.objectContaining({
|
|
868
|
-
'page.path': '/test-page',
|
|
869
|
-
'page.title': 'Test Title',
|
|
870
|
-
}),
|
|
871
|
-
}));
|
|
872
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
873
|
-
});
|
|
874
|
-
|
|
875
|
-
it('uses document title when pageTitle is not provided', () => {
|
|
876
|
-
telemetry.trackPageView('/test-page');
|
|
877
|
-
|
|
878
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('page_view', expect.objectContaining({
|
|
879
|
-
attributes: expect.objectContaining({
|
|
880
|
-
'page.title': 'Test Page',
|
|
881
|
-
}),
|
|
882
|
-
}));
|
|
883
|
-
});
|
|
884
|
-
});
|
|
885
|
-
|
|
886
|
-
describe('trackEvent with tracer', () => {
|
|
887
|
-
it('creates a span with event attributes', () => {
|
|
888
|
-
telemetry.trackEvent('button_click', { button_id: 'submit' });
|
|
889
|
-
|
|
890
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('event.button_click', expect.objectContaining({
|
|
891
|
-
attributes: expect.objectContaining({
|
|
892
|
-
'event.name': 'button_click',
|
|
893
|
-
'button_id': 'submit',
|
|
894
|
-
}),
|
|
895
|
-
}));
|
|
896
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
897
|
-
});
|
|
898
|
-
|
|
899
|
-
it('creates a span without additional attributes', () => {
|
|
900
|
-
telemetry.trackEvent('page_load');
|
|
901
|
-
|
|
902
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('event.page_load', expect.objectContaining({
|
|
903
|
-
attributes: expect.objectContaining({
|
|
904
|
-
'event.name': 'page_load',
|
|
905
|
-
}),
|
|
906
|
-
}));
|
|
907
|
-
});
|
|
908
|
-
});
|
|
909
|
-
|
|
910
|
-
describe('trackError with tracer', () => {
|
|
911
|
-
it('creates a span with error details', () => {
|
|
912
|
-
const error = new Error('Test error');
|
|
913
|
-
error.stack = 'Error: Test error\n at line 1';
|
|
914
|
-
|
|
915
|
-
telemetry.trackError(error, { component: 'TestComponent' });
|
|
916
|
-
|
|
917
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('error', expect.objectContaining({
|
|
918
|
-
attributes: expect.objectContaining({
|
|
919
|
-
'error.message': 'Test error',
|
|
920
|
-
'error.stack': error.stack,
|
|
921
|
-
'error.type': 'Error',
|
|
922
|
-
}),
|
|
923
|
-
}));
|
|
924
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2, message: 'Test error' });
|
|
925
|
-
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
|
|
926
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
927
|
-
});
|
|
928
|
-
|
|
929
|
-
it('handles error without stack trace', () => {
|
|
930
|
-
const error = { message: 'Simple error', name: 'SimpleError' };
|
|
931
|
-
|
|
932
|
-
telemetry.trackError(error);
|
|
933
|
-
|
|
934
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('error', expect.objectContaining({
|
|
935
|
-
attributes: expect.objectContaining({
|
|
936
|
-
'error.message': 'Simple error',
|
|
937
|
-
'error.type': 'SimpleError',
|
|
938
|
-
}),
|
|
939
|
-
}));
|
|
940
|
-
});
|
|
941
|
-
|
|
942
|
-
it('handles string error', () => {
|
|
943
|
-
telemetry.trackError('String error');
|
|
944
|
-
|
|
945
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('error', expect.objectContaining({
|
|
946
|
-
attributes: expect.objectContaining({
|
|
947
|
-
'error.message': 'String error',
|
|
948
|
-
}),
|
|
949
|
-
}));
|
|
950
|
-
});
|
|
951
|
-
});
|
|
952
|
-
|
|
953
|
-
describe('trackAPICall with tracer', () => {
|
|
954
|
-
it('creates a span for successful API call', () => {
|
|
955
|
-
telemetry.trackAPICall('GET', '/api/users', 200, 150, { user_id: '123' });
|
|
956
|
-
|
|
957
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('api_call', expect.objectContaining({
|
|
958
|
-
attributes: expect.objectContaining({
|
|
959
|
-
'http.method': 'GET',
|
|
960
|
-
'http.url': '/api/users',
|
|
961
|
-
'http.status_code': 200,
|
|
962
|
-
'http.duration_ms': 150,
|
|
963
|
-
'user_id': '123',
|
|
964
|
-
}),
|
|
965
|
-
}));
|
|
966
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 0 });
|
|
967
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
968
|
-
});
|
|
969
|
-
|
|
970
|
-
it('creates a span for failed API call with 4xx', () => {
|
|
971
|
-
telemetry.trackAPICall('GET', '/api/users', 404, 50);
|
|
972
|
-
|
|
973
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2 });
|
|
974
|
-
});
|
|
975
|
-
|
|
976
|
-
it('creates a span for failed API call with 5xx', () => {
|
|
977
|
-
telemetry.trackAPICall('POST', '/api/users', 500, 100);
|
|
978
|
-
|
|
979
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2 });
|
|
980
|
-
});
|
|
981
|
-
});
|
|
982
|
-
|
|
983
|
-
describe('trackUserAction with tracer', () => {
|
|
984
|
-
it('creates a span for user action', () => {
|
|
985
|
-
telemetry.trackUserAction('login', { method: 'email' });
|
|
986
|
-
|
|
987
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('user.login', expect.objectContaining({
|
|
988
|
-
attributes: expect.objectContaining({
|
|
989
|
-
'user.action': 'login',
|
|
990
|
-
'user.metadata': JSON.stringify({ method: 'email' }),
|
|
991
|
-
}),
|
|
992
|
-
}));
|
|
993
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
994
|
-
});
|
|
995
|
-
|
|
996
|
-
it('creates a span without metadata', () => {
|
|
997
|
-
telemetry.trackUserAction('logout');
|
|
998
|
-
|
|
999
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('user.logout', expect.objectContaining({
|
|
1000
|
-
attributes: expect.objectContaining({
|
|
1001
|
-
'user.action': 'logout',
|
|
1002
|
-
}),
|
|
1003
|
-
}));
|
|
1004
|
-
});
|
|
1005
|
-
});
|
|
1006
|
-
|
|
1007
|
-
describe('trackPerformerAction with tracer', () => {
|
|
1008
|
-
it('creates a span for performer action', () => {
|
|
1009
|
-
telemetry.trackPerformerAction('profile_update', { field: 'bio' });
|
|
1010
|
-
|
|
1011
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('performer.profile_update', expect.objectContaining({
|
|
1012
|
-
attributes: expect.objectContaining({
|
|
1013
|
-
'performer.action': 'profile_update',
|
|
1014
|
-
'performer.metadata': JSON.stringify({ field: 'bio' }),
|
|
1015
|
-
}),
|
|
1016
|
-
}));
|
|
1017
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
1018
|
-
});
|
|
1019
|
-
|
|
1020
|
-
it('creates a span without metadata', () => {
|
|
1021
|
-
telemetry.trackPerformerAction('gig_accepted');
|
|
1022
|
-
|
|
1023
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('performer.gig_accepted', expect.objectContaining({
|
|
1024
|
-
attributes: expect.objectContaining({
|
|
1025
|
-
'performer.action': 'gig_accepted',
|
|
1026
|
-
}),
|
|
1027
|
-
}));
|
|
1028
|
-
});
|
|
1029
|
-
});
|
|
1030
|
-
|
|
1031
|
-
describe('shutdownTelemetry with tracer', () => {
|
|
1032
|
-
it('shuts down successfully', async () => {
|
|
1033
|
-
await telemetry.shutdownTelemetry();
|
|
1034
|
-
|
|
1035
|
-
expect(mockTracerProvider.shutdown).toHaveBeenCalled();
|
|
1036
|
-
// Debug logging is now controlled by consoleDebug config flag
|
|
1037
|
-
});
|
|
1038
|
-
});
|
|
1039
|
-
});
|
|
1040
|
-
|
|
1041
|
-
describe('connectWithSSRTrace functionality', () => {
|
|
1042
|
-
let telemetry;
|
|
1043
|
-
let mockSpan;
|
|
1044
|
-
let mockTracer;
|
|
1045
|
-
let mockTracerProvider;
|
|
1046
|
-
|
|
1047
|
-
beforeEach(async () => {
|
|
1048
|
-
vi.resetModules();
|
|
1049
|
-
|
|
1050
|
-
// Create mock span
|
|
1051
|
-
mockSpan = {
|
|
1052
|
-
setAttribute: vi.fn().mockReturnThis(),
|
|
1053
|
-
setStatus: vi.fn().mockReturnThis(),
|
|
1054
|
-
recordException: vi.fn().mockReturnThis(),
|
|
1055
|
-
end: vi.fn(),
|
|
1056
|
-
name: 'client.init',
|
|
1057
|
-
};
|
|
1058
|
-
|
|
1059
|
-
// Create mock tracer
|
|
1060
|
-
mockTracer = {
|
|
1061
|
-
startSpan: vi.fn().mockReturnValue(mockSpan),
|
|
1062
|
-
};
|
|
1063
|
-
|
|
1064
|
-
// Create mock tracer provider
|
|
1065
|
-
mockTracerProvider = {
|
|
1066
|
-
addSpanProcessor: vi.fn(),
|
|
1067
|
-
register: vi.fn(),
|
|
1068
|
-
getTracer: vi.fn().mockReturnValue(mockTracer),
|
|
1069
|
-
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
1070
|
-
};
|
|
1071
|
-
|
|
1072
|
-
// Mock WebTracerProvider to return our mock
|
|
1073
|
-
vi.doMock('@opentelemetry/sdk-trace-web', () => ({
|
|
1074
|
-
WebTracerProvider: vi.fn().mockImplementation(() => mockTracerProvider),
|
|
1075
|
-
}));
|
|
1076
|
-
|
|
1077
|
-
// Mock registerInstrumentations
|
|
1078
|
-
vi.doMock('@opentelemetry/instrumentation', () => ({
|
|
1079
|
-
registerInstrumentations: vi.fn(),
|
|
1080
|
-
}));
|
|
1081
|
-
|
|
1082
|
-
global.window = {
|
|
1083
|
-
location: {
|
|
1084
|
-
pathname: '/performers',
|
|
1085
|
-
search: '',
|
|
1086
|
-
hash: '',
|
|
1087
|
-
href: 'http://localhost/performers',
|
|
1088
|
-
},
|
|
1089
|
-
addEventListener: vi.fn(),
|
|
1090
|
-
__TRACE_CONTEXT__: {
|
|
1091
|
-
traceId: 'test-trace-123',
|
|
1092
|
-
spanId: 'test-span-456',
|
|
1093
|
-
traceFlags: 1,
|
|
1094
|
-
},
|
|
1095
|
-
};
|
|
1096
|
-
|
|
1097
|
-
global.document = {
|
|
1098
|
-
readyState: 'complete',
|
|
1099
|
-
title: 'Test Page',
|
|
1100
|
-
referrer: 'http://localhost/',
|
|
1101
|
-
addEventListener: vi.fn(),
|
|
1102
|
-
};
|
|
1103
|
-
|
|
1104
|
-
global.navigator = {
|
|
1105
|
-
userAgent: 'test-agent',
|
|
1106
|
-
language: 'en-US',
|
|
1107
|
-
platform: 'test-platform',
|
|
1108
|
-
};
|
|
1109
|
-
|
|
1110
|
-
telemetry = await import('./telemetry.js');
|
|
1111
|
-
});
|
|
1112
|
-
|
|
1113
|
-
afterEach(() => {
|
|
1114
|
-
vi.clearAllMocks();
|
|
1115
|
-
vi.doUnmock('@opentelemetry/sdk-trace-web');
|
|
1116
|
-
vi.doUnmock('@opentelemetry/instrumentation');
|
|
1117
|
-
});
|
|
1118
|
-
|
|
1119
|
-
it('connects with SSR trace context when available', () => {
|
|
1120
|
-
// Manually call initTelemetry which should also trigger connectWithSSRTrace
|
|
1121
|
-
telemetry.initTelemetry();
|
|
1122
|
-
telemetry.setupAutoTelemetry();
|
|
1123
|
-
|
|
1124
|
-
// Check that a span was created with SSR trace context
|
|
1125
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('client.init', expect.objectContaining({
|
|
1126
|
-
attributes: expect.objectContaining({
|
|
1127
|
-
'span.kind': 'client',
|
|
1128
|
-
'client.type': 'browser',
|
|
1129
|
-
'ssr.trace_id': 'test-trace-123',
|
|
1130
|
-
'ssr.span_id': 'test-span-456',
|
|
1131
|
-
}),
|
|
1132
|
-
links: expect.arrayContaining([
|
|
1133
|
-
expect.objectContaining({
|
|
1134
|
-
context: expect.objectContaining({
|
|
1135
|
-
traceId: 'test-trace-123',
|
|
1136
|
-
spanId: 'test-span-456',
|
|
1137
|
-
traceFlags: 1,
|
|
1138
|
-
}),
|
|
1139
|
-
}),
|
|
1140
|
-
]),
|
|
1141
|
-
}));
|
|
1142
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
1143
|
-
});
|
|
1144
|
-
});
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
3
|
+
|
|
4
|
+
// Mock all OpenTelemetry modules before importing telemetry
|
|
5
|
+
vi.mock('@opentelemetry/sdk-trace-web', () => ({
|
|
6
|
+
WebTracerProvider: vi.fn().mockImplementation(() => ({
|
|
7
|
+
addSpanProcessor: vi.fn(),
|
|
8
|
+
register: vi.fn(),
|
|
9
|
+
getTracer: vi.fn().mockReturnValue({
|
|
10
|
+
startSpan: vi.fn().mockReturnValue({
|
|
11
|
+
setAttribute: vi.fn(),
|
|
12
|
+
setStatus: vi.fn(),
|
|
13
|
+
recordException: vi.fn(),
|
|
14
|
+
end: vi.fn(),
|
|
15
|
+
name: 'test-span',
|
|
16
|
+
}),
|
|
17
|
+
}),
|
|
18
|
+
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
19
|
+
})),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
vi.mock('@opentelemetry/sdk-trace-base', () => ({
|
|
23
|
+
BatchSpanProcessor: vi.fn(),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
vi.mock('@opentelemetry/exporter-trace-otlp-http', () => ({
|
|
27
|
+
OTLPTraceExporter: vi.fn(),
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
vi.mock('@opentelemetry/resources', () => ({
|
|
31
|
+
Resource: vi.fn(),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
vi.mock('@opentelemetry/semantic-conventions', () => ({
|
|
35
|
+
SEMRESATTRS_SERVICE_NAME: 'service.name',
|
|
36
|
+
SEMRESATTRS_SERVICE_VERSION: 'service.version',
|
|
37
|
+
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT: 'deployment.environment',
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
vi.mock('@opentelemetry/instrumentation-document-load', () => ({
|
|
41
|
+
DocumentLoadInstrumentation: vi.fn().mockImplementation(() => ({
|
|
42
|
+
enable: vi.fn(),
|
|
43
|
+
})),
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
vi.mock('@opentelemetry/instrumentation-fetch', () => ({
|
|
47
|
+
FetchInstrumentation: vi.fn().mockImplementation(() => ({
|
|
48
|
+
enable: vi.fn(),
|
|
49
|
+
})),
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
vi.mock('@opentelemetry/instrumentation-xml-http-request', () => ({
|
|
53
|
+
XMLHttpRequestInstrumentation: vi.fn().mockImplementation(() => ({
|
|
54
|
+
enable: vi.fn(),
|
|
55
|
+
})),
|
|
56
|
+
}));
|
|
57
|
+
|
|
58
|
+
vi.mock('@opentelemetry/instrumentation-user-interaction', () => ({
|
|
59
|
+
UserInteractionInstrumentation: vi.fn().mockImplementation(() => ({
|
|
60
|
+
enable: vi.fn(),
|
|
61
|
+
})),
|
|
62
|
+
}));
|
|
63
|
+
|
|
64
|
+
vi.mock('@opentelemetry/context-zone', () => ({
|
|
65
|
+
ZoneContextManager: vi.fn(),
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
vi.mock('@opentelemetry/propagator-b3', () => ({
|
|
69
|
+
B3Propagator: vi.fn(),
|
|
70
|
+
}));
|
|
71
|
+
|
|
72
|
+
describe('Telemetry Module', () => {
|
|
73
|
+
let telemetry;
|
|
74
|
+
let originalWindow;
|
|
75
|
+
let originalDocument;
|
|
76
|
+
let mockSpan;
|
|
77
|
+
let mockTracer;
|
|
78
|
+
|
|
79
|
+
beforeEach(async () => {
|
|
80
|
+
vi.resetModules();
|
|
81
|
+
|
|
82
|
+
// Save original globals
|
|
83
|
+
originalWindow = global.window;
|
|
84
|
+
originalDocument = global.document;
|
|
85
|
+
|
|
86
|
+
// Create mock span
|
|
87
|
+
mockSpan = {
|
|
88
|
+
setAttribute: vi.fn(),
|
|
89
|
+
setStatus: vi.fn(),
|
|
90
|
+
recordException: vi.fn(),
|
|
91
|
+
end: vi.fn(),
|
|
92
|
+
name: 'test-span',
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Create mock tracer
|
|
96
|
+
mockTracer = {
|
|
97
|
+
startSpan: vi.fn().mockReturnValue(mockSpan),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Setup mock window
|
|
101
|
+
global.window = {
|
|
102
|
+
location: {
|
|
103
|
+
pathname: '/performers',
|
|
104
|
+
search: '?test=1',
|
|
105
|
+
hash: '#section',
|
|
106
|
+
href: 'http://localhost/performers?test=1#section',
|
|
107
|
+
},
|
|
108
|
+
addEventListener: vi.fn(),
|
|
109
|
+
__TRACE_CONTEXT__: null,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
global.document = {
|
|
113
|
+
readyState: 'complete',
|
|
114
|
+
title: 'Test Page',
|
|
115
|
+
referrer: 'http://localhost/',
|
|
116
|
+
addEventListener: vi.fn(),
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
global.navigator = {
|
|
120
|
+
userAgent: 'test-agent',
|
|
121
|
+
language: 'en-US',
|
|
122
|
+
platform: 'test-platform',
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Re-import with fresh mocks
|
|
126
|
+
telemetry = await import('./telemetry.js');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
afterEach(() => {
|
|
130
|
+
global.window = originalWindow;
|
|
131
|
+
global.document = originalDocument;
|
|
132
|
+
vi.clearAllMocks();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('initTelemetry', () => {
|
|
136
|
+
it('exports initTelemetry function', () => {
|
|
137
|
+
expect(telemetry.initTelemetry).toBeDefined();
|
|
138
|
+
expect(typeof telemetry.initTelemetry).toBe('function');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('can be called without errors', () => {
|
|
142
|
+
expect(() => telemetry.initTelemetry()).not.toThrow();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('startSpan', () => {
|
|
147
|
+
it('exports startSpan function', () => {
|
|
148
|
+
expect(telemetry.startSpan).toBeDefined();
|
|
149
|
+
expect(typeof telemetry.startSpan).toBe('function');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('returns null when tracer is not initialized', async () => {
|
|
153
|
+
vi.resetModules();
|
|
154
|
+
const tempWindow = global.window;
|
|
155
|
+
delete global.window;
|
|
156
|
+
|
|
157
|
+
const freshTelemetry = await import('./telemetry.js');
|
|
158
|
+
global.window = tempWindow;
|
|
159
|
+
|
|
160
|
+
const result = freshTelemetry.startSpan('test-span');
|
|
161
|
+
expect(result).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('can be called with name only', () => {
|
|
165
|
+
telemetry.initTelemetry();
|
|
166
|
+
const result = telemetry.startSpan('test-span');
|
|
167
|
+
expect(result === null || typeof result === 'object').toBe(true);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('can be called with name and options', () => {
|
|
171
|
+
telemetry.initTelemetry();
|
|
172
|
+
const result = telemetry.startSpan('test-span', { attributes: { key: 'value' } });
|
|
173
|
+
expect(result === null || typeof result === 'object').toBe(true);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('endSpan', () => {
|
|
178
|
+
it('exports endSpan function', () => {
|
|
179
|
+
expect(telemetry.endSpan).toBeDefined();
|
|
180
|
+
expect(typeof telemetry.endSpan).toBe('function');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('handles null span gracefully', () => {
|
|
184
|
+
expect(() => telemetry.endSpan(null)).not.toThrow();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('handles undefined span gracefully', () => {
|
|
188
|
+
expect(() => telemetry.endSpan(undefined)).not.toThrow();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('can end a span with default status', () => {
|
|
192
|
+
expect(() => telemetry.endSpan(mockSpan)).not.toThrow();
|
|
193
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 0 });
|
|
194
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('can end a span with custom status', () => {
|
|
198
|
+
expect(() => telemetry.endSpan(mockSpan, { code: 2, message: 'error' })).not.toThrow();
|
|
199
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2, message: 'error' });
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('trackPageView', () => {
|
|
204
|
+
it('exports trackPageView function', () => {
|
|
205
|
+
expect(telemetry.trackPageView).toBeDefined();
|
|
206
|
+
expect(typeof telemetry.trackPageView).toBe('function');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('can be called without errors', () => {
|
|
210
|
+
expect(() => telemetry.trackPageView('/test-page')).not.toThrow();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('can be called with page path and title', () => {
|
|
214
|
+
expect(() => telemetry.trackPageView('/test-page', 'Test Page Title')).not.toThrow();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('returns early when tracer is not initialized', () => {
|
|
218
|
+
const result = telemetry.trackPageView('/test');
|
|
219
|
+
expect(result).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe('trackEvent', () => {
|
|
224
|
+
it('exports trackEvent function', () => {
|
|
225
|
+
expect(telemetry.trackEvent).toBeDefined();
|
|
226
|
+
expect(typeof telemetry.trackEvent).toBe('function');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('can be called with event name only', () => {
|
|
230
|
+
expect(() => telemetry.trackEvent('button_click')).not.toThrow();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('can be called with event name and attributes', () => {
|
|
234
|
+
expect(() => telemetry.trackEvent('button_click', { button_id: 'submit' })).not.toThrow();
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe('trackPerformerAction', () => {
|
|
239
|
+
it('exports trackPerformerAction function', () => {
|
|
240
|
+
expect(telemetry.trackPerformerAction).toBeDefined();
|
|
241
|
+
expect(typeof telemetry.trackPerformerAction).toBe('function');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('can be called with action only', () => {
|
|
245
|
+
expect(() => telemetry.trackPerformerAction('profile_update')).not.toThrow();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('can be called with action and metadata', () => {
|
|
249
|
+
expect(() => telemetry.trackPerformerAction('profile_update', { field: 'bio' })).not.toThrow();
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('trackError', () => {
|
|
254
|
+
it('exports trackError function', () => {
|
|
255
|
+
expect(telemetry.trackError).toBeDefined();
|
|
256
|
+
expect(typeof telemetry.trackError).toBe('function');
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('can be called with Error object', () => {
|
|
260
|
+
const error = new Error('Test error');
|
|
261
|
+
expect(() => telemetry.trackError(error)).not.toThrow();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('can be called with Error and context', () => {
|
|
265
|
+
const error = new Error('Test error');
|
|
266
|
+
expect(() => telemetry.trackError(error, { component: 'TestComponent' })).not.toThrow();
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('handles error without stack trace', () => {
|
|
270
|
+
const error = { message: 'Simple error', name: 'SimpleError' };
|
|
271
|
+
expect(() => telemetry.trackError(error)).not.toThrow();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('handles string error', () => {
|
|
275
|
+
expect(() => telemetry.trackError('String error message')).not.toThrow();
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe('trackAPICall', () => {
|
|
280
|
+
it('exports trackAPICall function', () => {
|
|
281
|
+
expect(telemetry.trackAPICall).toBeDefined();
|
|
282
|
+
expect(typeof telemetry.trackAPICall).toBe('function');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('can be called with basic parameters', () => {
|
|
286
|
+
expect(() => telemetry.trackAPICall('GET', '/api/users', 200, 150)).not.toThrow();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('can be called with additional attributes', () => {
|
|
290
|
+
expect(() => telemetry.trackAPICall('POST', '/api/users', 201, 200, { user_id: '123' })).not.toThrow();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('handles error status codes', () => {
|
|
294
|
+
expect(() => telemetry.trackAPICall('GET', '/api/users', 500, 100)).not.toThrow();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('handles 4xx status codes', () => {
|
|
298
|
+
expect(() => telemetry.trackAPICall('GET', '/api/users', 404, 50)).not.toThrow();
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe('trackUserAction', () => {
|
|
303
|
+
it('exports trackUserAction function', () => {
|
|
304
|
+
expect(telemetry.trackUserAction).toBeDefined();
|
|
305
|
+
expect(typeof telemetry.trackUserAction).toBe('function');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('can be called with action only', () => {
|
|
309
|
+
expect(() => telemetry.trackUserAction('login')).not.toThrow();
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('can be called with action and metadata', () => {
|
|
313
|
+
expect(() => telemetry.trackUserAction('login', { method: 'email' })).not.toThrow();
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe('shutdownTelemetry', () => {
|
|
318
|
+
it('exports shutdownTelemetry function', () => {
|
|
319
|
+
expect(telemetry.shutdownTelemetry).toBeDefined();
|
|
320
|
+
expect(typeof telemetry.shutdownTelemetry).toBe('function');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('can be called without errors', async () => {
|
|
324
|
+
await expect(telemetry.shutdownTelemetry()).resolves.not.toThrow();
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('handles shutdown when not initialized', async () => {
|
|
328
|
+
vi.resetModules();
|
|
329
|
+
const tempWindow = global.window;
|
|
330
|
+
delete global.window;
|
|
331
|
+
|
|
332
|
+
const freshTelemetry = await import('./telemetry.js');
|
|
333
|
+
global.window = tempWindow;
|
|
334
|
+
|
|
335
|
+
await expect(freshTelemetry.shutdownTelemetry()).resolves.not.toThrow();
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
describe('Module auto-initialization', () => {
|
|
340
|
+
it('adds DOMContentLoaded listener when document is loading', async () => {
|
|
341
|
+
vi.resetModules();
|
|
342
|
+
|
|
343
|
+
global.document = {
|
|
344
|
+
readyState: 'loading',
|
|
345
|
+
addEventListener: vi.fn(),
|
|
346
|
+
title: 'Test',
|
|
347
|
+
referrer: '',
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
await import('./telemetry.js');
|
|
351
|
+
|
|
352
|
+
expect(global.document.addEventListener).toHaveBeenCalledWith(
|
|
353
|
+
'DOMContentLoaded',
|
|
354
|
+
expect.any(Function)
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('initializes immediately when document is ready', async () => {
|
|
359
|
+
vi.resetModules();
|
|
360
|
+
|
|
361
|
+
global.document = {
|
|
362
|
+
readyState: 'complete',
|
|
363
|
+
title: 'Test',
|
|
364
|
+
referrer: '',
|
|
365
|
+
addEventListener: vi.fn(),
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
await import('./telemetry.js');
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('adds beforeunload listener for cleanup', async () => {
|
|
372
|
+
vi.resetModules();
|
|
373
|
+
|
|
374
|
+
global.window.addEventListener = vi.fn();
|
|
375
|
+
|
|
376
|
+
await import('./telemetry.js');
|
|
377
|
+
|
|
378
|
+
expect(global.window.addEventListener).toHaveBeenCalledWith(
|
|
379
|
+
'beforeunload',
|
|
380
|
+
expect.any(Function)
|
|
381
|
+
);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe('SSR trace context connection', () => {
|
|
386
|
+
it('handles missing SSR trace context', async () => {
|
|
387
|
+
vi.resetModules();
|
|
388
|
+
|
|
389
|
+
global.window.__TRACE_CONTEXT__ = null;
|
|
390
|
+
|
|
391
|
+
await expect(import('./telemetry.js')).resolves.not.toThrow();
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('handles SSR trace context when present', async () => {
|
|
395
|
+
vi.resetModules();
|
|
396
|
+
|
|
397
|
+
global.window.__TRACE_CONTEXT__ = {
|
|
398
|
+
traceId: 'test-trace-id',
|
|
399
|
+
spanId: 'test-span-id',
|
|
400
|
+
traceFlags: 1,
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
await expect(import('./telemetry.js')).resolves.not.toThrow();
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
describe('Telemetry with traces disabled', () => {
|
|
409
|
+
let telemetry;
|
|
410
|
+
|
|
411
|
+
beforeEach(async () => {
|
|
412
|
+
vi.resetModules();
|
|
413
|
+
|
|
414
|
+
vi.stubEnv('VITE_OTEL_TRACES_ENABLED', 'false');
|
|
415
|
+
|
|
416
|
+
global.window = {
|
|
417
|
+
location: {
|
|
418
|
+
pathname: '/performers',
|
|
419
|
+
search: '',
|
|
420
|
+
hash: '',
|
|
421
|
+
href: 'http://localhost/performers',
|
|
422
|
+
},
|
|
423
|
+
addEventListener: vi.fn(),
|
|
424
|
+
__TRACE_CONTEXT__: null,
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
global.document = {
|
|
428
|
+
readyState: 'complete',
|
|
429
|
+
title: 'Test',
|
|
430
|
+
referrer: '',
|
|
431
|
+
addEventListener: vi.fn(),
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
global.navigator = {
|
|
435
|
+
userAgent: 'test',
|
|
436
|
+
language: 'en',
|
|
437
|
+
platform: 'test',
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
telemetry = await import('./telemetry.js');
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
afterEach(() => {
|
|
444
|
+
vi.unstubAllEnvs();
|
|
445
|
+
vi.clearAllMocks();
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it('initTelemetry returns early when tracing is disabled', () => {
|
|
449
|
+
// Should return early without throwing when traces are disabled
|
|
450
|
+
expect(() => telemetry.initTelemetry({ tracesEnabled: false })).not.toThrow();
|
|
451
|
+
// Verify tracer is not initialized (trackPageView should be a no-op)
|
|
452
|
+
expect(() => telemetry.trackPageView('/test')).not.toThrow();
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
describe('Telemetry debug mode', () => {
|
|
457
|
+
let telemetry;
|
|
458
|
+
|
|
459
|
+
beforeEach(async () => {
|
|
460
|
+
vi.resetModules();
|
|
461
|
+
|
|
462
|
+
vi.stubEnv('VITE_OTEL_DEBUG', 'true');
|
|
463
|
+
|
|
464
|
+
global.window = {
|
|
465
|
+
location: {
|
|
466
|
+
pathname: '/performers',
|
|
467
|
+
search: '',
|
|
468
|
+
hash: '',
|
|
469
|
+
href: 'http://localhost/performers',
|
|
470
|
+
},
|
|
471
|
+
addEventListener: vi.fn(),
|
|
472
|
+
__TRACE_CONTEXT__: null,
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
global.document = {
|
|
476
|
+
readyState: 'complete',
|
|
477
|
+
title: 'Test',
|
|
478
|
+
referrer: '',
|
|
479
|
+
addEventListener: vi.fn(),
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
global.navigator = {
|
|
483
|
+
userAgent: 'test',
|
|
484
|
+
language: 'en',
|
|
485
|
+
platform: 'test',
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
telemetry = await import('./telemetry.js');
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
afterEach(() => {
|
|
492
|
+
vi.unstubAllEnvs();
|
|
493
|
+
vi.clearAllMocks();
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it('can initialize with debug mode enabled', () => {
|
|
497
|
+
expect(() => telemetry.initTelemetry()).not.toThrow();
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
describe('Telemetry shutdown error handling', () => {
|
|
502
|
+
let telemetry;
|
|
503
|
+
|
|
504
|
+
beforeEach(async () => {
|
|
505
|
+
vi.resetModules();
|
|
506
|
+
|
|
507
|
+
// Mock shutdown to throw error
|
|
508
|
+
vi.mock('@opentelemetry/sdk-trace-web', () => ({
|
|
509
|
+
WebTracerProvider: vi.fn().mockImplementation(() => ({
|
|
510
|
+
addSpanProcessor: vi.fn(),
|
|
511
|
+
register: vi.fn(),
|
|
512
|
+
getTracer: vi.fn().mockReturnValue({
|
|
513
|
+
startSpan: vi.fn().mockReturnValue({
|
|
514
|
+
setAttribute: vi.fn(),
|
|
515
|
+
setStatus: vi.fn(),
|
|
516
|
+
recordException: vi.fn(),
|
|
517
|
+
end: vi.fn(),
|
|
518
|
+
name: 'test-span',
|
|
519
|
+
}),
|
|
520
|
+
}),
|
|
521
|
+
shutdown: vi.fn().mockRejectedValue(new Error('Shutdown failed')),
|
|
522
|
+
})),
|
|
523
|
+
}));
|
|
524
|
+
|
|
525
|
+
global.window = {
|
|
526
|
+
location: {
|
|
527
|
+
pathname: '/performers',
|
|
528
|
+
search: '',
|
|
529
|
+
hash: '',
|
|
530
|
+
href: 'http://localhost/performers',
|
|
531
|
+
},
|
|
532
|
+
addEventListener: vi.fn(),
|
|
533
|
+
__TRACE_CONTEXT__: null,
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
global.document = {
|
|
537
|
+
readyState: 'complete',
|
|
538
|
+
title: 'Test',
|
|
539
|
+
referrer: '',
|
|
540
|
+
addEventListener: vi.fn(),
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
global.navigator = {
|
|
544
|
+
userAgent: 'test',
|
|
545
|
+
language: 'en',
|
|
546
|
+
platform: 'test',
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
telemetry = await import('./telemetry.js');
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
afterEach(() => {
|
|
553
|
+
vi.unstubAllEnvs();
|
|
554
|
+
vi.clearAllMocks();
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('handles shutdown error gracefully', async () => {
|
|
558
|
+
const consoleErrorSpy = vi.spyOn(console, 'error');
|
|
559
|
+
telemetry.initTelemetry();
|
|
560
|
+
await telemetry.shutdownTelemetry();
|
|
561
|
+
// Should not throw, just log error
|
|
562
|
+
expect(consoleErrorSpy).toBeDefined();
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
describe('Telemetry DOMContentLoaded event', () => {
|
|
567
|
+
let telemetry;
|
|
568
|
+
let domContentLoadedCallback;
|
|
569
|
+
|
|
570
|
+
beforeEach(async () => {
|
|
571
|
+
vi.resetModules();
|
|
572
|
+
|
|
573
|
+
global.window = {
|
|
574
|
+
location: {
|
|
575
|
+
pathname: '/performers',
|
|
576
|
+
search: '',
|
|
577
|
+
hash: '',
|
|
578
|
+
href: 'http://localhost/performers',
|
|
579
|
+
},
|
|
580
|
+
addEventListener: vi.fn(),
|
|
581
|
+
__TRACE_CONTEXT__: null,
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
// Mock document with loading state
|
|
585
|
+
global.document = {
|
|
586
|
+
readyState: 'loading',
|
|
587
|
+
title: 'Test',
|
|
588
|
+
referrer: '',
|
|
589
|
+
addEventListener: vi.fn((event, callback) => {
|
|
590
|
+
if (event === 'DOMContentLoaded') {
|
|
591
|
+
domContentLoadedCallback = callback;
|
|
592
|
+
}
|
|
593
|
+
}),
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
global.navigator = {
|
|
597
|
+
userAgent: 'test',
|
|
598
|
+
language: 'en',
|
|
599
|
+
platform: 'test',
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
telemetry = await import('./telemetry.js');
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
afterEach(() => {
|
|
606
|
+
vi.unstubAllEnvs();
|
|
607
|
+
vi.clearAllMocks();
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it('registers DOMContentLoaded event listener when document is loading', () => {
|
|
611
|
+
expect(document.addEventListener).toHaveBeenCalledWith('DOMContentLoaded', expect.any(Function));
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it('calls initTelemetry and connectWithSSRTrace when DOMContentLoaded fires', () => {
|
|
615
|
+
// Trigger the DOMContentLoaded callback
|
|
616
|
+
if (domContentLoadedCallback) {
|
|
617
|
+
expect(() => domContentLoadedCallback()).not.toThrow();
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
describe('Telemetry beforeunload event', () => {
|
|
623
|
+
let beforeUnloadCallback;
|
|
624
|
+
|
|
625
|
+
beforeEach(async () => {
|
|
626
|
+
vi.resetModules();
|
|
627
|
+
|
|
628
|
+
global.window = {
|
|
629
|
+
location: {
|
|
630
|
+
pathname: '/performers',
|
|
631
|
+
search: '',
|
|
632
|
+
hash: '',
|
|
633
|
+
href: 'http://localhost/performers',
|
|
634
|
+
},
|
|
635
|
+
addEventListener: vi.fn((event, callback) => {
|
|
636
|
+
if (event === 'beforeunload') {
|
|
637
|
+
beforeUnloadCallback = callback;
|
|
638
|
+
}
|
|
639
|
+
}),
|
|
640
|
+
__TRACE_CONTEXT__: null,
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
global.document = {
|
|
644
|
+
readyState: 'complete',
|
|
645
|
+
title: 'Test',
|
|
646
|
+
referrer: '',
|
|
647
|
+
addEventListener: vi.fn(),
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
global.navigator = {
|
|
651
|
+
userAgent: 'test',
|
|
652
|
+
language: 'en',
|
|
653
|
+
platform: 'test',
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
await import('./telemetry.js');
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
afterEach(() => {
|
|
660
|
+
vi.unstubAllEnvs();
|
|
661
|
+
vi.clearAllMocks();
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
it('registers beforeunload event listener', () => {
|
|
665
|
+
expect(window.addEventListener).toHaveBeenCalledWith('beforeunload', expect.any(Function));
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
it('calls shutdownTelemetry when beforeunload fires', () => {
|
|
669
|
+
if (beforeUnloadCallback) {
|
|
670
|
+
expect(() => beforeUnloadCallback()).not.toThrow();
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
describe('getTelemetryConfig', () => {
|
|
676
|
+
let telemetry;
|
|
677
|
+
|
|
678
|
+
beforeEach(async () => {
|
|
679
|
+
vi.resetModules();
|
|
680
|
+
|
|
681
|
+
global.window = {
|
|
682
|
+
location: {
|
|
683
|
+
pathname: '/performers',
|
|
684
|
+
search: '',
|
|
685
|
+
hash: '',
|
|
686
|
+
href: 'http://localhost/performers',
|
|
687
|
+
},
|
|
688
|
+
addEventListener: vi.fn(),
|
|
689
|
+
__TRACE_CONTEXT__: null,
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
global.document = {
|
|
693
|
+
readyState: 'complete',
|
|
694
|
+
title: 'Test',
|
|
695
|
+
referrer: '',
|
|
696
|
+
addEventListener: vi.fn(),
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
global.navigator = {
|
|
700
|
+
userAgent: 'test',
|
|
701
|
+
language: 'en',
|
|
702
|
+
platform: 'test',
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
telemetry = await import('./telemetry.js');
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
afterEach(() => {
|
|
709
|
+
vi.unstubAllEnvs();
|
|
710
|
+
vi.clearAllMocks();
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
it('exports getTelemetryConfig function', () => {
|
|
714
|
+
expect(telemetry.getTelemetryConfig).toBeDefined();
|
|
715
|
+
expect(typeof telemetry.getTelemetryConfig).toBe('function');
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it('returns telemetry configuration object', () => {
|
|
719
|
+
const config = telemetry.getTelemetryConfig();
|
|
720
|
+
expect(config).toBeDefined();
|
|
721
|
+
expect(config).toHaveProperty('serviceName');
|
|
722
|
+
expect(config).toHaveProperty('serviceVersion');
|
|
723
|
+
expect(config).toHaveProperty('environment');
|
|
724
|
+
expect(config).toHaveProperty('tracesEnabled');
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
describe('setupAutoTelemetry', () => {
|
|
729
|
+
let telemetry;
|
|
730
|
+
|
|
731
|
+
beforeEach(async () => {
|
|
732
|
+
vi.resetModules();
|
|
733
|
+
|
|
734
|
+
global.window = {
|
|
735
|
+
location: {
|
|
736
|
+
pathname: '/performers',
|
|
737
|
+
search: '',
|
|
738
|
+
hash: '',
|
|
739
|
+
href: 'http://localhost/performers',
|
|
740
|
+
},
|
|
741
|
+
addEventListener: vi.fn(),
|
|
742
|
+
__TRACE_CONTEXT__: null,
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
global.document = {
|
|
746
|
+
readyState: 'complete',
|
|
747
|
+
title: 'Test',
|
|
748
|
+
referrer: '',
|
|
749
|
+
addEventListener: vi.fn(),
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
global.navigator = {
|
|
753
|
+
userAgent: 'test',
|
|
754
|
+
language: 'en',
|
|
755
|
+
platform: 'test',
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
telemetry = await import('./telemetry.js');
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
afterEach(() => {
|
|
762
|
+
vi.unstubAllEnvs();
|
|
763
|
+
vi.clearAllMocks();
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it('exports setupAutoTelemetry function', () => {
|
|
767
|
+
expect(telemetry.setupAutoTelemetry).toBeDefined();
|
|
768
|
+
expect(typeof telemetry.setupAutoTelemetry).toBe('function');
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
it('can be called without errors', () => {
|
|
772
|
+
expect(() => telemetry.setupAutoTelemetry()).not.toThrow();
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
it('does not throw when window is undefined', () => {
|
|
776
|
+
const tempWindow = global.window;
|
|
777
|
+
delete global.window;
|
|
778
|
+
|
|
779
|
+
expect(() => telemetry.setupAutoTelemetry()).not.toThrow();
|
|
780
|
+
|
|
781
|
+
global.window = tempWindow;
|
|
782
|
+
});
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
describe('Telemetry tracking with initialized tracer', () => {
|
|
786
|
+
let telemetry;
|
|
787
|
+
let mockSpan;
|
|
788
|
+
let mockTracer;
|
|
789
|
+
let mockTracerProvider;
|
|
790
|
+
|
|
791
|
+
beforeEach(async () => {
|
|
792
|
+
vi.resetModules();
|
|
793
|
+
|
|
794
|
+
// Create mock span
|
|
795
|
+
mockSpan = {
|
|
796
|
+
setAttribute: vi.fn().mockReturnThis(),
|
|
797
|
+
setStatus: vi.fn().mockReturnThis(),
|
|
798
|
+
recordException: vi.fn().mockReturnThis(),
|
|
799
|
+
end: vi.fn(),
|
|
800
|
+
name: 'test-span',
|
|
801
|
+
};
|
|
802
|
+
|
|
803
|
+
// Create mock tracer
|
|
804
|
+
mockTracer = {
|
|
805
|
+
startSpan: vi.fn().mockReturnValue(mockSpan),
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
// Create mock tracer provider
|
|
809
|
+
mockTracerProvider = {
|
|
810
|
+
addSpanProcessor: vi.fn(),
|
|
811
|
+
register: vi.fn(),
|
|
812
|
+
getTracer: vi.fn().mockReturnValue(mockTracer),
|
|
813
|
+
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
// Mock WebTracerProvider to return our mock
|
|
817
|
+
vi.doMock('@opentelemetry/sdk-trace-web', () => ({
|
|
818
|
+
WebTracerProvider: vi.fn().mockImplementation(() => mockTracerProvider),
|
|
819
|
+
}));
|
|
820
|
+
|
|
821
|
+
// Mock registerInstrumentations to avoid instrumentation errors
|
|
822
|
+
vi.doMock('@opentelemetry/instrumentation', () => ({
|
|
823
|
+
registerInstrumentations: vi.fn(),
|
|
824
|
+
}));
|
|
825
|
+
|
|
826
|
+
global.window = {
|
|
827
|
+
location: {
|
|
828
|
+
pathname: '/performers',
|
|
829
|
+
search: '?test=1',
|
|
830
|
+
hash: '#section',
|
|
831
|
+
href: 'http://localhost/performers?test=1#section',
|
|
832
|
+
},
|
|
833
|
+
addEventListener: vi.fn(),
|
|
834
|
+
__TRACE_CONTEXT__: null,
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
global.document = {
|
|
838
|
+
readyState: 'complete',
|
|
839
|
+
title: 'Test Page',
|
|
840
|
+
referrer: 'http://localhost/',
|
|
841
|
+
addEventListener: vi.fn(),
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
global.navigator = {
|
|
845
|
+
userAgent: 'test-agent',
|
|
846
|
+
language: 'en-US',
|
|
847
|
+
platform: 'test-platform',
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
telemetry = await import('./telemetry.js');
|
|
851
|
+
|
|
852
|
+
// Initialize telemetry to set up tracer
|
|
853
|
+
telemetry.initTelemetry();
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
afterEach(() => {
|
|
857
|
+
vi.clearAllMocks();
|
|
858
|
+
vi.doUnmock('@opentelemetry/sdk-trace-web');
|
|
859
|
+
vi.doUnmock('@opentelemetry/instrumentation');
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
describe('trackPageView with tracer', () => {
|
|
863
|
+
it('creates a span with page attributes', () => {
|
|
864
|
+
telemetry.trackPageView('/test-page', 'Test Title');
|
|
865
|
+
|
|
866
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('page_view', expect.objectContaining({
|
|
867
|
+
attributes: expect.objectContaining({
|
|
868
|
+
'page.path': '/test-page',
|
|
869
|
+
'page.title': 'Test Title',
|
|
870
|
+
}),
|
|
871
|
+
}));
|
|
872
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
it('uses document title when pageTitle is not provided', () => {
|
|
876
|
+
telemetry.trackPageView('/test-page');
|
|
877
|
+
|
|
878
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('page_view', expect.objectContaining({
|
|
879
|
+
attributes: expect.objectContaining({
|
|
880
|
+
'page.title': 'Test Page',
|
|
881
|
+
}),
|
|
882
|
+
}));
|
|
883
|
+
});
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
describe('trackEvent with tracer', () => {
|
|
887
|
+
it('creates a span with event attributes', () => {
|
|
888
|
+
telemetry.trackEvent('button_click', { button_id: 'submit' });
|
|
889
|
+
|
|
890
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('event.button_click', expect.objectContaining({
|
|
891
|
+
attributes: expect.objectContaining({
|
|
892
|
+
'event.name': 'button_click',
|
|
893
|
+
'button_id': 'submit',
|
|
894
|
+
}),
|
|
895
|
+
}));
|
|
896
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
it('creates a span without additional attributes', () => {
|
|
900
|
+
telemetry.trackEvent('page_load');
|
|
901
|
+
|
|
902
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('event.page_load', expect.objectContaining({
|
|
903
|
+
attributes: expect.objectContaining({
|
|
904
|
+
'event.name': 'page_load',
|
|
905
|
+
}),
|
|
906
|
+
}));
|
|
907
|
+
});
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
describe('trackError with tracer', () => {
|
|
911
|
+
it('creates a span with error details', () => {
|
|
912
|
+
const error = new Error('Test error');
|
|
913
|
+
error.stack = 'Error: Test error\n at line 1';
|
|
914
|
+
|
|
915
|
+
telemetry.trackError(error, { component: 'TestComponent' });
|
|
916
|
+
|
|
917
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('error', expect.objectContaining({
|
|
918
|
+
attributes: expect.objectContaining({
|
|
919
|
+
'error.message': 'Test error',
|
|
920
|
+
'error.stack': error.stack,
|
|
921
|
+
'error.type': 'Error',
|
|
922
|
+
}),
|
|
923
|
+
}));
|
|
924
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2, message: 'Test error' });
|
|
925
|
+
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
|
|
926
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
927
|
+
});
|
|
928
|
+
|
|
929
|
+
it('handles error without stack trace', () => {
|
|
930
|
+
const error = { message: 'Simple error', name: 'SimpleError' };
|
|
931
|
+
|
|
932
|
+
telemetry.trackError(error);
|
|
933
|
+
|
|
934
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('error', expect.objectContaining({
|
|
935
|
+
attributes: expect.objectContaining({
|
|
936
|
+
'error.message': 'Simple error',
|
|
937
|
+
'error.type': 'SimpleError',
|
|
938
|
+
}),
|
|
939
|
+
}));
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
it('handles string error', () => {
|
|
943
|
+
telemetry.trackError('String error');
|
|
944
|
+
|
|
945
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('error', expect.objectContaining({
|
|
946
|
+
attributes: expect.objectContaining({
|
|
947
|
+
'error.message': 'String error',
|
|
948
|
+
}),
|
|
949
|
+
}));
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
describe('trackAPICall with tracer', () => {
|
|
954
|
+
it('creates a span for successful API call', () => {
|
|
955
|
+
telemetry.trackAPICall('GET', '/api/users', 200, 150, { user_id: '123' });
|
|
956
|
+
|
|
957
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('api_call', expect.objectContaining({
|
|
958
|
+
attributes: expect.objectContaining({
|
|
959
|
+
'http.method': 'GET',
|
|
960
|
+
'http.url': '/api/users',
|
|
961
|
+
'http.status_code': 200,
|
|
962
|
+
'http.duration_ms': 150,
|
|
963
|
+
'user_id': '123',
|
|
964
|
+
}),
|
|
965
|
+
}));
|
|
966
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 0 });
|
|
967
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
968
|
+
});
|
|
969
|
+
|
|
970
|
+
it('creates a span for failed API call with 4xx', () => {
|
|
971
|
+
telemetry.trackAPICall('GET', '/api/users', 404, 50);
|
|
972
|
+
|
|
973
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2 });
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
it('creates a span for failed API call with 5xx', () => {
|
|
977
|
+
telemetry.trackAPICall('POST', '/api/users', 500, 100);
|
|
978
|
+
|
|
979
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 2 });
|
|
980
|
+
});
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
describe('trackUserAction with tracer', () => {
|
|
984
|
+
it('creates a span for user action', () => {
|
|
985
|
+
telemetry.trackUserAction('login', { method: 'email' });
|
|
986
|
+
|
|
987
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('user.login', expect.objectContaining({
|
|
988
|
+
attributes: expect.objectContaining({
|
|
989
|
+
'user.action': 'login',
|
|
990
|
+
'user.metadata': JSON.stringify({ method: 'email' }),
|
|
991
|
+
}),
|
|
992
|
+
}));
|
|
993
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
it('creates a span without metadata', () => {
|
|
997
|
+
telemetry.trackUserAction('logout');
|
|
998
|
+
|
|
999
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('user.logout', expect.objectContaining({
|
|
1000
|
+
attributes: expect.objectContaining({
|
|
1001
|
+
'user.action': 'logout',
|
|
1002
|
+
}),
|
|
1003
|
+
}));
|
|
1004
|
+
});
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
describe('trackPerformerAction with tracer', () => {
|
|
1008
|
+
it('creates a span for performer action', () => {
|
|
1009
|
+
telemetry.trackPerformerAction('profile_update', { field: 'bio' });
|
|
1010
|
+
|
|
1011
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('performer.profile_update', expect.objectContaining({
|
|
1012
|
+
attributes: expect.objectContaining({
|
|
1013
|
+
'performer.action': 'profile_update',
|
|
1014
|
+
'performer.metadata': JSON.stringify({ field: 'bio' }),
|
|
1015
|
+
}),
|
|
1016
|
+
}));
|
|
1017
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
1018
|
+
});
|
|
1019
|
+
|
|
1020
|
+
it('creates a span without metadata', () => {
|
|
1021
|
+
telemetry.trackPerformerAction('gig_accepted');
|
|
1022
|
+
|
|
1023
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('performer.gig_accepted', expect.objectContaining({
|
|
1024
|
+
attributes: expect.objectContaining({
|
|
1025
|
+
'performer.action': 'gig_accepted',
|
|
1026
|
+
}),
|
|
1027
|
+
}));
|
|
1028
|
+
});
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
describe('shutdownTelemetry with tracer', () => {
|
|
1032
|
+
it('shuts down successfully', async () => {
|
|
1033
|
+
await telemetry.shutdownTelemetry();
|
|
1034
|
+
|
|
1035
|
+
expect(mockTracerProvider.shutdown).toHaveBeenCalled();
|
|
1036
|
+
// Debug logging is now controlled by consoleDebug config flag
|
|
1037
|
+
});
|
|
1038
|
+
});
|
|
1039
|
+
});
|
|
1040
|
+
|
|
1041
|
+
describe('connectWithSSRTrace functionality', () => {
|
|
1042
|
+
let telemetry;
|
|
1043
|
+
let mockSpan;
|
|
1044
|
+
let mockTracer;
|
|
1045
|
+
let mockTracerProvider;
|
|
1046
|
+
|
|
1047
|
+
beforeEach(async () => {
|
|
1048
|
+
vi.resetModules();
|
|
1049
|
+
|
|
1050
|
+
// Create mock span
|
|
1051
|
+
mockSpan = {
|
|
1052
|
+
setAttribute: vi.fn().mockReturnThis(),
|
|
1053
|
+
setStatus: vi.fn().mockReturnThis(),
|
|
1054
|
+
recordException: vi.fn().mockReturnThis(),
|
|
1055
|
+
end: vi.fn(),
|
|
1056
|
+
name: 'client.init',
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
// Create mock tracer
|
|
1060
|
+
mockTracer = {
|
|
1061
|
+
startSpan: vi.fn().mockReturnValue(mockSpan),
|
|
1062
|
+
};
|
|
1063
|
+
|
|
1064
|
+
// Create mock tracer provider
|
|
1065
|
+
mockTracerProvider = {
|
|
1066
|
+
addSpanProcessor: vi.fn(),
|
|
1067
|
+
register: vi.fn(),
|
|
1068
|
+
getTracer: vi.fn().mockReturnValue(mockTracer),
|
|
1069
|
+
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
// Mock WebTracerProvider to return our mock
|
|
1073
|
+
vi.doMock('@opentelemetry/sdk-trace-web', () => ({
|
|
1074
|
+
WebTracerProvider: vi.fn().mockImplementation(() => mockTracerProvider),
|
|
1075
|
+
}));
|
|
1076
|
+
|
|
1077
|
+
// Mock registerInstrumentations
|
|
1078
|
+
vi.doMock('@opentelemetry/instrumentation', () => ({
|
|
1079
|
+
registerInstrumentations: vi.fn(),
|
|
1080
|
+
}));
|
|
1081
|
+
|
|
1082
|
+
global.window = {
|
|
1083
|
+
location: {
|
|
1084
|
+
pathname: '/performers',
|
|
1085
|
+
search: '',
|
|
1086
|
+
hash: '',
|
|
1087
|
+
href: 'http://localhost/performers',
|
|
1088
|
+
},
|
|
1089
|
+
addEventListener: vi.fn(),
|
|
1090
|
+
__TRACE_CONTEXT__: {
|
|
1091
|
+
traceId: 'test-trace-123',
|
|
1092
|
+
spanId: 'test-span-456',
|
|
1093
|
+
traceFlags: 1,
|
|
1094
|
+
},
|
|
1095
|
+
};
|
|
1096
|
+
|
|
1097
|
+
global.document = {
|
|
1098
|
+
readyState: 'complete',
|
|
1099
|
+
title: 'Test Page',
|
|
1100
|
+
referrer: 'http://localhost/',
|
|
1101
|
+
addEventListener: vi.fn(),
|
|
1102
|
+
};
|
|
1103
|
+
|
|
1104
|
+
global.navigator = {
|
|
1105
|
+
userAgent: 'test-agent',
|
|
1106
|
+
language: 'en-US',
|
|
1107
|
+
platform: 'test-platform',
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1110
|
+
telemetry = await import('./telemetry.js');
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
afterEach(() => {
|
|
1114
|
+
vi.clearAllMocks();
|
|
1115
|
+
vi.doUnmock('@opentelemetry/sdk-trace-web');
|
|
1116
|
+
vi.doUnmock('@opentelemetry/instrumentation');
|
|
1117
|
+
});
|
|
1118
|
+
|
|
1119
|
+
it('connects with SSR trace context when available', () => {
|
|
1120
|
+
// Manually call initTelemetry which should also trigger connectWithSSRTrace
|
|
1121
|
+
telemetry.initTelemetry();
|
|
1122
|
+
telemetry.setupAutoTelemetry();
|
|
1123
|
+
|
|
1124
|
+
// Check that a span was created with SSR trace context
|
|
1125
|
+
expect(mockTracer.startSpan).toHaveBeenCalledWith('client.init', expect.objectContaining({
|
|
1126
|
+
attributes: expect.objectContaining({
|
|
1127
|
+
'span.kind': 'client',
|
|
1128
|
+
'client.type': 'browser',
|
|
1129
|
+
'ssr.trace_id': 'test-trace-123',
|
|
1130
|
+
'ssr.span_id': 'test-span-456',
|
|
1131
|
+
}),
|
|
1132
|
+
links: expect.arrayContaining([
|
|
1133
|
+
expect.objectContaining({
|
|
1134
|
+
context: expect.objectContaining({
|
|
1135
|
+
traceId: 'test-trace-123',
|
|
1136
|
+
spanId: 'test-span-456',
|
|
1137
|
+
traceFlags: 1,
|
|
1138
|
+
}),
|
|
1139
|
+
}),
|
|
1140
|
+
]),
|
|
1141
|
+
}));
|
|
1142
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
1143
|
+
});
|
|
1144
|
+
});
|