-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathinitDatadogRum.test.ts
More file actions
243 lines (204 loc) · 6.94 KB
/
Copy pathinitDatadogRum.test.ts
File metadata and controls
243 lines (204 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const hoisted = vi.hoisted(() => {
const context: Record<string, unknown> = {}
return {
context,
fetch: vi.fn<typeof fetch>(),
getInitConfiguration: vi.fn(),
init: vi.fn(),
setGlobalContextProperty: vi.fn((key: string, value: unknown) => {
context[key] = value
})
}
})
vi.mock('@datadog/browser-rum', () => ({
datadogRum: hoisted
}))
import { rumBeforeSend } from './datadogRumBeforeSend'
import { initDatadogRum } from './initDatadogRum'
describe('initDatadogRum', () => {
beforeEach(() => {
vi.resetAllMocks()
for (const key of Object.keys(hoisted.context)) {
delete hoisted.context[key]
}
hoisted.setGlobalContextProperty.mockImplementation((key, value) => {
hoisted.context[key] = value
})
hoisted.fetch.mockResolvedValue(new Response(null, { status: 503 }))
hoisted.getInitConfiguration.mockReturnValue(undefined)
vi.stubGlobal('fetch', hoisted.fetch)
})
afterEach(() => {
vi.restoreAllMocks()
vi.unstubAllGlobals()
})
it.for([
{ hostname: 'cloud.comfy.org', env: 'prod-v2' },
{ hostname: 'stagingcloud.comfy.org', env: 'stg-v2' },
{ hostname: 'testcloud.comfy.org', env: 'test-v2' },
{ hostname: 'fe-pr-13691.testenvs.comfy.org', env: 'test-v2' }
])(
'initializes $hostname with the $env environment',
async ({ hostname, env }) => {
await initDatadogRum(hostname)
expect(hoisted.init).toHaveBeenCalledWith({
clientToken: 'pub7704486e5b64eb4ff6f62891cda45559',
applicationId: '041a9897-5516-4b1f-a245-1a9aa6895488',
site: 'us5.datadoghq.com',
service: 'comfy-cloud-frontend',
env,
version: __COMFYUI_FRONTEND_COMMIT__,
beforeSend: rumBeforeSend,
sessionSampleRate: 100,
sessionReplaySampleRate: 0,
allowedTracingUrls: [expect.any(RegExp)]
})
}
)
it('tags canary traffic with its bucket and frontend version', async () => {
let resolveProbe: (response: Response) => void
hoisted.fetch.mockReturnValue(
new Promise((resolve) => {
resolveProbe = resolve
})
)
hoisted.init.mockImplementation(() => {
expect(hoisted.context).toEqual({
bucket: 'canary',
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__,
version: __COMFYUI_FRONTEND_COMMIT__
})
})
const initialization = initDatadogRum('cloud.comfy.org')
expect(hoisted.init).not.toHaveBeenCalled()
resolveProbe!(
new Response(null, {
headers: {
'X-Frontend-Bucket': 'canary',
'X-Frontend-Version': __COMFYUI_FRONTEND_COMMIT__
}
})
)
await initialization
expect(hoisted.context).toEqual({
bucket: 'canary',
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__,
version: __COMFYUI_FRONTEND_COMMIT__
})
expect(hoisted.init).toHaveBeenCalledOnce()
})
it('serializes concurrent initialization', async () => {
let resolveProbe: (response: Response) => void
hoisted.fetch.mockReturnValue(
new Promise((resolve) => {
resolveProbe = resolve
})
)
const firstInitialization = initDatadogRum('cloud.comfy.org')
const secondInitialization = initDatadogRum('cloud.comfy.org')
resolveProbe!(
new Response(null, {
headers: {
'X-Frontend-Bucket': 'canary',
'X-Frontend-Version': __COMFYUI_FRONTEND_COMMIT__
}
})
)
await Promise.all([firstInitialization, secondInitialization])
expect(hoisted.fetch).toHaveBeenCalledOnce()
expect(hoisted.init).toHaveBeenCalledOnce()
expect(hoisted.context).toEqual({
bucket: 'canary',
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__,
version: __COMFYUI_FRONTEND_COMMIT__
})
})
it('defaults the bucket to stable and tracks its frontend version', async () => {
hoisted.fetch.mockResolvedValue(
new Response(null, {
headers: { 'X-Frontend-Version': __COMFYUI_FRONTEND_COMMIT__ }
})
)
await initDatadogRum('cloud.comfy.org')
expect(hoisted.context).toEqual({
bucket: 'stable',
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__,
version: __COMFYUI_FRONTEND_COMMIT__
})
})
it('leaves traffic unclassified when the frontend version is absent', async () => {
hoisted.fetch.mockResolvedValue(
new Response(null, {
headers: { 'X-Frontend-Bucket': 'canary' }
})
)
await initDatadogRum('cloud.comfy.org')
expect(hoisted.context).toEqual({
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__
})
})
it('leaves traffic unclassified when the probe reaches another version', async () => {
hoisted.fetch.mockResolvedValue(
new Response(null, {
headers: {
'X-Frontend-Bucket': 'stable',
'X-Frontend-Version': 'another-frontend-commit'
}
})
)
await initDatadogRum('cloud.comfy.org')
expect(hoisted.context).toEqual({
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__
})
})
it('leaves traffic unclassified when the header probe fails', async () => {
hoisted.fetch.mockResolvedValue(new Response(null, { status: 503 }))
await initDatadogRum('cloud.comfy.org')
expect(hoisted.context).toEqual({
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__
})
expect(hoisted.init).toHaveBeenCalledOnce()
})
it('initializes RUM when the header probe rejects', async () => {
hoisted.fetch.mockRejectedValue(new Error('network error'))
await initDatadogRum('cloud.comfy.org')
expect(hoisted.context).toEqual({
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__
})
expect(hoisted.init).toHaveBeenCalledOnce()
})
it('initializes RUM when the header probe times out', async () => {
const abortController = new AbortController()
vi.spyOn(AbortSignal, 'timeout').mockReturnValue(abortController.signal)
hoisted.fetch.mockImplementation(
(_input, init) =>
new Promise((_resolve, reject) => {
init?.signal?.addEventListener('abort', () => {
reject(new DOMException('Timed out', 'AbortError'))
})
})
)
const initialization = initDatadogRum('cloud.comfy.org')
abortController.abort()
await initialization
expect(hoisted.context).toEqual({
comfyui_frontend_version: __COMFYUI_FRONTEND_VERSION__
})
expect(hoisted.init).toHaveBeenCalledOnce()
})
it.for([
'localhost',
'testenvs.comfy.org',
'eviltestenvs.comfy.org',
'preview.testenvs.comfy.org.example.com'
])('does not initialize on unknown hostname %s', async (hostname) => {
await initDatadogRum(hostname)
expect(hoisted.init).not.toHaveBeenCalled()
})
it('does not initialize twice', async () => {
hoisted.getInitConfiguration.mockReturnValue({})
await initDatadogRum('cloud.comfy.org')
expect(hoisted.init).not.toHaveBeenCalled()
})
})