forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.test.ts
489 lines (445 loc) · 15.1 KB
/
metrics.test.ts
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import { mkdirSync } from "node:fs";
import { http, HttpResponse } from "msw";
import { vi } from "vitest";
import { version as wranglerVersion } from "../../package.json";
import { purgeConfigCaches, saveToConfigCache } from "../config-cache";
import { CI } from "../is-ci";
import { logger } from "../logger";
import { getMetricsConfig, getMetricsDispatcher } from "../metrics";
import {
CURRENT_METRICS_DATE,
readMetricsConfig,
USER_ID_CACHE_PATH,
writeMetricsConfig,
} from "../metrics/metrics-config";
import { writeAuthConfigFile } from "../user";
import { mockConsoleMethods } from "./helpers/mock-console";
import { clearDialogs, mockConfirm } from "./helpers/mock-dialogs";
import { useMockIsTTY } from "./helpers/mock-istty";
import { msw, mswSuccessOauthHandlers } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import type { MockInstance } from "vitest";
declare const global: { SPARROW_SOURCE_KEY: string | undefined };
vi.unmock("../metrics/metrics-config");
describe("metrics", () => {
const ORIGINAL_SPARROW_SOURCE_KEY = global.SPARROW_SOURCE_KEY;
const std = mockConsoleMethods();
runInTempDir();
beforeEach(async () => {
global.SPARROW_SOURCE_KEY = "MOCK_KEY";
logger.loggerLevel = "debug";
// Create a node_modules directory to store config-cache files
mkdirSync("node_modules");
});
afterEach(() => {
global.SPARROW_SOURCE_KEY = ORIGINAL_SPARROW_SOURCE_KEY;
purgeConfigCaches();
clearDialogs();
});
describe("getMetricsDispatcher()", () => {
const MOCK_DISPATCHER_OPTIONS = {
// By setting this to true we avoid the `getMetricsConfig()` logic in these tests.
sendMetrics: true,
offline: false,
};
// These tests should never hit the `/user` API endpoint.
const userRequests = mockUserRequest();
afterEach(() => {
expect(userRequests.count).toBe(0);
});
describe("identify()", () => {
it("should send a request to the default URL", async () => {
const request = mockMetricRequest(
{
event: "identify",
properties: {
category: "Workers",
wranglerVersion,
os: process.platform + ":" + process.arch,
a: 1,
b: 2,
},
},
{ "Sparrow-Source-Key": "MOCK_KEY" },
"identify"
);
const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
await dispatcher.identify({ a: 1, b: 2 });
expect(request.count).toBe(1);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should write a debug log if the dispatcher is disabled", async () => {
const requests = mockMetricRequest({}, {}, "identify");
const dispatcher = await getMetricsDispatcher({
...MOCK_DISPATCHER_OPTIONS,
sendMetrics: false,
});
await dispatcher.identify({ a: 1, b: 2 });
await flushPromises();
expect(requests.count).toBe(0);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should write a debug log if the request fails", async () => {
msw.use(
http.post("*/identify", async () => {
return HttpResponse.error();
})
);
const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
await dispatcher.identify({ a: 1, b: 2 });
await flushPromises();
expect(std.debug).toMatchInlineSnapshot(`
"Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
Metrics dispatcher: Failed to send request: Failed to fetch"
`);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should write a warning log if no source key has been provided", async () => {
global.SPARROW_SOURCE_KEY = undefined;
const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
await dispatcher.identify({ a: 1, b: 2 });
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Source Key not provided. Be sure to initialize before sending events. { type: 'identify', name: 'identify', properties: { a: 1, b: 2 } }"`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});
describe("sendEvent()", () => {
it("should send a request to the default URL", async () => {
const requests = mockMetricRequest(
{
event: "some-event",
properties: {
category: "Workers",
wranglerVersion,
os: process.platform + ":" + process.arch,
a: 1,
b: 2,
},
},
{
"Sparrow-Source-Key": "MOCK_KEY",
},
"event"
);
const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
expect(requests.count).toBe(1);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should write a debug log if the dispatcher is disabled", async () => {
const requests = mockMetricRequest({}, {}, "event");
const dispatcher = await getMetricsDispatcher({
...MOCK_DISPATCHER_OPTIONS,
sendMetrics: false,
});
await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
await flushPromises();
expect(requests.count).toBe(0);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should write a debug log if the request fails", async () => {
msw.use(
http.post("*/event", async () => {
return HttpResponse.error();
})
);
const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
await flushPromises();
expect(std.debug).toMatchInlineSnapshot(`
"Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
Metrics dispatcher: Failed to send request: Failed to fetch"
`);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("should write a warning log if no source key has been provided", async () => {
global.SPARROW_SOURCE_KEY = undefined;
const requests = mockMetricRequest({}, {}, "event");
const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
expect(requests.count).toBe(0);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Source Key not provided. Be sure to initialize before sending events. { type: 'event', name: 'some-event', properties: { a: 1, b: 2 } }"`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});
});
describe("getMetricsConfig()", () => {
let isCISpy: MockInstance;
const { setIsTTY } = useMockIsTTY();
beforeEach(() => {
// Default the mock TTY to interactive for all these tests.
setIsTTY(true);
isCISpy = vi.spyOn(CI, "isCI").mockReturnValue(false);
});
describe("enabled", () => {
it("should return the WRANGLER_SEND_METRICS environment variable for enabled if it is defined", async () => {
vi.stubEnv("WRANGLER_SEND_METRICS", "false");
expect(await getMetricsConfig({})).toMatchObject({
enabled: false,
});
vi.stubEnv("WRANGLER_SEND_METRICS", "true");
expect(await getMetricsConfig({})).toMatchObject({
enabled: true,
});
});
it("should return false if running in a CI environment", async () => {
isCISpy.mockReturnValue(true);
expect(await getMetricsConfig({})).toMatchObject({
enabled: false,
});
});
it("should return the sendMetrics argument for enabled if it is defined", async () => {
expect(
await getMetricsConfig({ sendMetrics: false, offline: false })
).toMatchObject({
enabled: false,
});
expect(
await getMetricsConfig({ sendMetrics: true, offline: false })
).toMatchObject({
enabled: true,
});
});
it("should return enabled false if the process is not interactive", async () => {
setIsTTY(false);
expect(
await getMetricsConfig({
sendMetrics: undefined,
offline: false,
})
).toMatchObject({
enabled: false,
});
});
it("should return enabled true if the user on this device previously agreed to send metrics", async () => {
await writeMetricsConfig({
permission: {
enabled: true,
date: new Date(2022, 6, 4),
},
});
expect(
await getMetricsConfig({
sendMetrics: undefined,
offline: false,
})
).toMatchObject({
enabled: true,
});
});
it("should return enabled false if the user on this device previously refused to send metrics", async () => {
await writeMetricsConfig({
permission: {
enabled: false,
date: new Date(2022, 6, 4),
},
});
expect(
await getMetricsConfig({
sendMetrics: undefined,
offline: false,
})
).toMatchObject({
enabled: false,
});
});
it("should accept and store permission granting to send metrics if the user agrees", async () => {
mockConfirm({
text: "Would you like to help improve Wrangler by sending usage metrics to Cloudflare?",
result: true,
});
expect(
await getMetricsConfig({
sendMetrics: undefined,
offline: false,
})
).toMatchObject({
enabled: true,
});
expect((await readMetricsConfig()).permission).toMatchObject({
enabled: true,
});
});
it("should accept and store permission declining to send metrics if the user declines", async () => {
mockConfirm({
text: "Would you like to help improve Wrangler by sending usage metrics to Cloudflare?",
result: false,
});
expect(
await getMetricsConfig({
sendMetrics: undefined,
offline: false,
})
).toMatchObject({
enabled: false,
});
expect((await readMetricsConfig()).permission).toMatchObject({
enabled: false,
});
});
it("should ignore the config if the permission date is older than the current metrics date", async () => {
mockConfirm({
text: "Would you like to help improve Wrangler by sending usage metrics to Cloudflare?",
result: false,
});
const OLD_DATE = new Date(2000);
await writeMetricsConfig({
permission: { enabled: true, date: OLD_DATE },
});
expect(
await getMetricsConfig({
sendMetrics: undefined,
offline: false,
})
).toMatchObject({
enabled: false,
});
const { permission } = await readMetricsConfig();
expect(permission?.enabled).toBe(false);
// The date should be updated to today's date
expect(permission?.date).toEqual(CURRENT_METRICS_DATE);
expect(std.out).toMatchInlineSnapshot(`
"Usage metrics tracking has changed since you last granted permission.
Your choice has been saved in the following file: test-xdg-config/metrics.json.
You can override the user level setting for a project in \`wrangler.toml\`:
- to disable sending metrics for a project: \`send_metrics = false\`
- to enable sending metrics for a project: \`send_metrics = true\`"
`);
});
});
describe("deviceId", () => {
it("should return a deviceId found in the config file", async () => {
await writeMetricsConfig({ deviceId: "XXXX-YYYY-ZZZZ" });
const { deviceId } = await getMetricsConfig({
sendMetrics: true,
offline: false,
});
expect(deviceId).toEqual("XXXX-YYYY-ZZZZ");
expect((await readMetricsConfig()).deviceId).toEqual(deviceId);
});
it("should create and store a new deviceId if none is found in the config file", async () => {
await writeMetricsConfig({});
const { deviceId } = await getMetricsConfig({
sendMetrics: true,
offline: false,
});
expect(deviceId).toMatch(
/[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}/
);
expect((await readMetricsConfig()).deviceId).toEqual(deviceId);
});
});
describe("userId", () => {
const userRequests = mockUserRequest();
it("should return a userId found in a cache file", async () => {
await saveToConfigCache(USER_ID_CACHE_PATH, {
userId: "CACHED_USER_ID",
});
const { userId } = await getMetricsConfig({
sendMetrics: true,
offline: false,
});
expect(userId).toEqual("CACHED_USER_ID");
expect(userRequests.count).toBe(0);
});
it("should fetch the userId from Cloudflare and store it in a cache file", async () => {
writeAuthConfigFile({ oauth_token: "DUMMY_TOKEN" });
const { userId } = await getMetricsConfig({
sendMetrics: true,
offline: false,
});
await flushPromises();
expect(userId).toEqual("MOCK_USER_ID");
expect(userRequests.count).toBe(1);
});
it("should not fetch the userId from Cloudflare if running in `offline` mode", async () => {
writeAuthConfigFile({ oauth_token: "DUMMY_TOKEN" });
const { userId } = await getMetricsConfig({
sendMetrics: true,
offline: true,
});
expect(userId).toBe(undefined);
expect(userRequests.count).toBe(0);
});
});
});
});
function mockUserRequest() {
const requests = { count: 0 };
beforeEach(() => {
msw.use(
...mswSuccessOauthHandlers,
http.get("*/user", () => {
requests.count++;
return HttpResponse.json(
{
success: true,
errors: [],
messages: [],
result: { id: "MOCK_USER_ID" },
},
{ status: 200 }
);
})
);
});
afterEach(() => {
requests.count = 0;
});
return requests;
}
function mockMetricRequest(
body: unknown,
header: unknown,
endpoint: "identify" | "event"
) {
const requests = { count: 0 };
msw.use(
http.post(
`*/${endpoint}`,
async ({ request }) => {
requests.count++;
expect(await request.json()).toEqual(body);
expect(request.headers).toContain(header);
return HttpResponse.json({}, { status: 200 });
},
{ once: true }
)
);
return requests;
}
// Forces a tick to allow the non-awaited fetch promise to resolve.
function flushPromises(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 0));
}