@react-native-windows/telemetry 0.0.0-canary.13 → 0.0.0-canary.131

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.
Files changed (55) hide show
  1. package/lib-commonjs/e2etest/telemetry.test.d.ts +26 -0
  2. package/lib-commonjs/e2etest/telemetry.test.js +572 -0
  3. package/lib-commonjs/e2etest/telemetry.test.js.map +1 -0
  4. package/lib-commonjs/index.d.ts +11 -7
  5. package/lib-commonjs/index.js +26 -14
  6. package/lib-commonjs/index.js.map +1 -1
  7. package/lib-commonjs/telemetry.d.ts +76 -29
  8. package/lib-commonjs/telemetry.js +429 -201
  9. package/lib-commonjs/telemetry.js.map +1 -1
  10. package/lib-commonjs/test/{sanitize.test.d.ts → basePropUtils.test.d.ts} +7 -7
  11. package/lib-commonjs/test/basePropUtils.test.js +145 -0
  12. package/lib-commonjs/test/basePropUtils.test.js.map +1 -0
  13. package/lib-commonjs/test/errorUtils.test.d.ts +7 -0
  14. package/lib-commonjs/test/errorUtils.test.js +160 -0
  15. package/lib-commonjs/test/errorUtils.test.js.map +1 -0
  16. package/lib-commonjs/test/nameUtils.test.d.ts +7 -0
  17. package/lib-commonjs/test/nameUtils.test.js +54 -0
  18. package/lib-commonjs/test/nameUtils.test.js.map +1 -0
  19. package/lib-commonjs/test/projectUtils.test.d.ts +7 -0
  20. package/lib-commonjs/test/projectUtils.test.js +88 -0
  21. package/lib-commonjs/test/projectUtils.test.js.map +1 -0
  22. package/lib-commonjs/test/sanitizeUtils.test.d.ts +7 -0
  23. package/lib-commonjs/test/sanitizeUtils.test.js +98 -0
  24. package/lib-commonjs/test/sanitizeUtils.test.js.map +1 -0
  25. package/lib-commonjs/test/versionUtils.test.d.ts +7 -0
  26. package/lib-commonjs/test/versionUtils.test.js +115 -0
  27. package/lib-commonjs/test/versionUtils.test.js.map +1 -0
  28. package/lib-commonjs/utils/basePropUtils.d.ts +92 -0
  29. package/lib-commonjs/utils/basePropUtils.js +216 -0
  30. package/lib-commonjs/utils/basePropUtils.js.map +1 -0
  31. package/lib-commonjs/{CodedError.d.ts → utils/errorUtils.d.ts} +93 -61
  32. package/lib-commonjs/utils/errorUtils.js +183 -0
  33. package/lib-commonjs/utils/errorUtils.js.map +1 -0
  34. package/lib-commonjs/utils/nameUtils.d.ts +7 -0
  35. package/lib-commonjs/utils/nameUtils.js +22 -0
  36. package/lib-commonjs/utils/nameUtils.js.map +1 -0
  37. package/lib-commonjs/utils/optionUtils.d.ts +45 -0
  38. package/lib-commonjs/utils/optionUtils.js +96 -0
  39. package/lib-commonjs/utils/optionUtils.js.map +1 -0
  40. package/lib-commonjs/utils/projectUtils.d.ts +50 -0
  41. package/lib-commonjs/utils/projectUtils.js +184 -0
  42. package/lib-commonjs/utils/projectUtils.js.map +1 -0
  43. package/lib-commonjs/utils/sanitizeUtils.d.ts +12 -0
  44. package/lib-commonjs/utils/sanitizeUtils.js +85 -0
  45. package/lib-commonjs/utils/sanitizeUtils.js.map +1 -0
  46. package/lib-commonjs/utils/versionUtils.d.ts +38 -0
  47. package/lib-commonjs/utils/versionUtils.js +156 -0
  48. package/lib-commonjs/utils/versionUtils.js.map +1 -0
  49. package/package.json +40 -22
  50. package/CHANGELOG.json +0 -200
  51. package/CHANGELOG.md +0 -95
  52. package/lib-commonjs/CodedError.js +0 -77
  53. package/lib-commonjs/CodedError.js.map +0 -1
  54. package/lib-commonjs/test/sanitize.test.js +0 -258
  55. package/lib-commonjs/test/sanitize.test.js.map +0 -1
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ *
5
+ * @format
6
+ */
7
+ import * as coreOneDS from '@microsoft/1ds-core-js';
8
+ import { Telemetry, TelemetryOptions } from '../telemetry';
9
+ export declare class TelemetryTest extends Telemetry {
10
+ protected static hasTestTelemetryProviders: boolean;
11
+ protected static testTelemetryProvidersRan: boolean;
12
+ /** Run at the beginning of each test. */
13
+ static startTest(options?: Partial<TelemetryOptions>): Promise<void>;
14
+ /** Run at the end of each test where telemetry was fired. */
15
+ static endTest(finalCallback?: () => void): void;
16
+ /** Sets that the telemetry provider has run. */
17
+ static setTestTelemetryProvidersRan(): void;
18
+ /** Retrieves the value of a common property.*/
19
+ static getCommonProperty(key: string): string | undefined;
20
+ /** Retrieves the version of the specified tool/package. */
21
+ static getVersion(key: string): string | null;
22
+ /** Retrieves the value of the preserveErrorMessages option. */
23
+ static getPreserveErrorMessages(): boolean;
24
+ /** Adds a telemetry processor, usually for verifying the envelope. */
25
+ static addTelemetryInitializer(telemetryInitializer: (envelope: coreOneDS.ITelemetryItem) => boolean): void;
26
+ }
@@ -0,0 +1,572 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ * Licensed under the MIT License.
5
+ *
6
+ * @format
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.TelemetryTest = void 0;
33
+ const path = __importStar(require("path"));
34
+ const telemetry_1 = require("../telemetry");
35
+ const basePropUtils = __importStar(require("../utils/basePropUtils"));
36
+ const errorUtils = __importStar(require("../utils/errorUtils"));
37
+ const projectUtils = __importStar(require("../utils/projectUtils"));
38
+ const versionUtils = __importStar(require("../utils/versionUtils"));
39
+ class CustomTestError extends Error {
40
+ constructor(message) {
41
+ super(message);
42
+ this.name = 'CustomTestError';
43
+ this.errno = '123';
44
+ }
45
+ }
46
+ class TelemetryTest extends telemetry_1.Telemetry {
47
+ /** Run at the beginning of each test. */
48
+ static async startTest(options) {
49
+ TelemetryTest.hasTestTelemetryProviders = false;
50
+ TelemetryTest.testTelemetryProvidersRan = false;
51
+ if (TelemetryTest.isEnabled()) {
52
+ telemetry_1.Telemetry.reset();
53
+ }
54
+ // Ensure that we don't actually fire events when testing
55
+ telemetry_1.Telemetry.isTestEnvironment = true;
56
+ await telemetry_1.Telemetry.setup(options);
57
+ }
58
+ /** Run at the end of each test where telemetry was fired. */
59
+ static endTest(finalCallback) {
60
+ telemetry_1.Telemetry.appInsightsCore?.flush(undefined /* isAsync */, () => {
61
+ // Your callback logic here
62
+ if (TelemetryTest.hasTestTelemetryProviders) {
63
+ expect(TelemetryTest.testTelemetryProvidersRan).toBe(true);
64
+ }
65
+ if (finalCallback) {
66
+ finalCallback();
67
+ }
68
+ });
69
+ }
70
+ /** Sets that the telemetry provider has run. */
71
+ static setTestTelemetryProvidersRan() {
72
+ TelemetryTest.testTelemetryProvidersRan = true;
73
+ }
74
+ /** Retrieves the value of a common property.*/
75
+ static getCommonProperty(key) {
76
+ return TelemetryTest.commonProperties[key];
77
+ }
78
+ /** Retrieves the version of the specified tool/package. */
79
+ static getVersion(key) {
80
+ return key in TelemetryTest.versionsProp
81
+ ? TelemetryTest.versionsProp[key]
82
+ : null;
83
+ }
84
+ /** Retrieves the value of the preserveErrorMessages option. */
85
+ static getPreserveErrorMessages() {
86
+ return TelemetryTest.options.preserveErrorMessages;
87
+ }
88
+ /** Adds a telemetry processor, usually for verifying the envelope. */
89
+ static addTelemetryInitializer(telemetryInitializer) {
90
+ TelemetryTest.appInsightsCore?.addTelemetryInitializer(telemetryInitializer);
91
+ TelemetryTest.hasTestTelemetryProviders = true;
92
+ }
93
+ }
94
+ exports.TelemetryTest = TelemetryTest;
95
+ test('setup() verify session id is valid and a common property', async () => {
96
+ await TelemetryTest.startTest();
97
+ const sessionId = TelemetryTest.getSessionId();
98
+ expect(sessionId).toBeDefined();
99
+ expect(sessionId).toHaveLength(32);
100
+ expect(sessionId).toBe(basePropUtils.getSessionId());
101
+ expect(TelemetryTest.getCommonProperty('sessionId')).toBe(sessionId);
102
+ TelemetryTest.endTest();
103
+ });
104
+ test('setup() verify static common property values with async sources', async () => {
105
+ await TelemetryTest.startTest();
106
+ const props = {
107
+ //deviceId: basePropUtils.deviceId,
108
+ deviceLocale: basePropUtils.deviceLocale,
109
+ };
110
+ for (const key in props) {
111
+ if (!(key in Object.prototype)) {
112
+ const value = TelemetryTest.getCommonProperty(key);
113
+ expect(value).toBeDefined();
114
+ expect(value).toBe(await props[key]());
115
+ }
116
+ }
117
+ TelemetryTest.endTest();
118
+ });
119
+ test('setup() verify static common property values with sync sources', async () => {
120
+ await TelemetryTest.startTest();
121
+ const props = {
122
+ deviceArchitecture: () => basePropUtils.deviceArchitecture(),
123
+ nodePlatform: () => basePropUtils.nodePlatform(),
124
+ deviceNumCPUs: () => basePropUtils.deviceNumCPUs().toString(),
125
+ deviceTotalMemory: () => basePropUtils.deviceTotalMemory().toString(),
126
+ ciCaptured: () => basePropUtils.captureCI().toString(),
127
+ ciType: () => basePropUtils.ciType(),
128
+ isMsftInternal: () => basePropUtils.isMsftInternal().toString(),
129
+ isTest: () => 'true',
130
+ };
131
+ for (const key in props) {
132
+ if (!(key in Object.prototype)) {
133
+ const value = TelemetryTest.getCommonProperty(key);
134
+ expect(value).toBeDefined();
135
+ expect(value).toBe(props[key]());
136
+ }
137
+ }
138
+ TelemetryTest.endTest();
139
+ });
140
+ test('setup() verify other common property values are defined', async () => {
141
+ await TelemetryTest.startTest();
142
+ const props = ['deviceDiskFreeSpace'];
143
+ for (const key of props) {
144
+ const value = TelemetryTest.getCommonProperty(key);
145
+ expect(value).toBeDefined();
146
+ }
147
+ TelemetryTest.endTest();
148
+ });
149
+ test('setup() verify tool versions are populated', async () => {
150
+ await TelemetryTest.startTest();
151
+ const props = {
152
+ node: versionUtils.getNodeVersion,
153
+ npm: versionUtils.getNpmVersion,
154
+ yarn: versionUtils.getYarnVersion,
155
+ VisualStudio: versionUtils.getVisualStudioVersion,
156
+ };
157
+ for (const key in props) {
158
+ if (!(key in Object.prototype)) {
159
+ const value = await props[key]();
160
+ expect(value).toBe(TelemetryTest.getVersion(key));
161
+ }
162
+ }
163
+ TelemetryTest.endTest();
164
+ });
165
+ test('tryUpdateVersionsProp() returns true for adding a new version', async () => {
166
+ await TelemetryTest.startTest();
167
+ const name = 'test';
168
+ const version = '1.0';
169
+ expect(await TelemetryTest.tryUpdateVersionsProp(name, async () => version)).toBe(true);
170
+ expect(TelemetryTest.getVersion(name)).toBe(version);
171
+ TelemetryTest.endTest();
172
+ });
173
+ test('tryUpdateVersionsProp() returns false for adding an existing version with refresh is false', async () => {
174
+ await TelemetryTest.startTest();
175
+ const name = 'test';
176
+ const version = '1.0';
177
+ expect(await TelemetryTest.tryUpdateVersionsProp(name, async () => version)).toBe(true);
178
+ let getValueCalled = false;
179
+ expect(await TelemetryTest.tryUpdateVersionsProp(name, async () => {
180
+ getValueCalled = true;
181
+ return version;
182
+ })).toBe(false);
183
+ expect(getValueCalled).toBe(false);
184
+ TelemetryTest.endTest();
185
+ });
186
+ test('tryUpdateVersionsProp() returns true for adding an existing version with refresh is true', async () => {
187
+ await TelemetryTest.startTest();
188
+ const name = 'test';
189
+ const version = '1.0';
190
+ expect(await TelemetryTest.tryUpdateVersionsProp(name, async () => version)).toBe(true);
191
+ let getValueCalled = false;
192
+ expect(await TelemetryTest.tryUpdateVersionsProp(name, async () => {
193
+ getValueCalled = true;
194
+ return version;
195
+ }, true)).toBe(true);
196
+ expect(getValueCalled).toBe(true);
197
+ TelemetryTest.endTest();
198
+ });
199
+ /** Returns the CommandStartInfo for our fake 'test-command'. */
200
+ function getTestCommandStartInfo() {
201
+ return {
202
+ commandName: 'test-command',
203
+ args: {
204
+ testArg1: 'true',
205
+ testArg2: '10',
206
+ testArg3: 'testValue',
207
+ },
208
+ options: {
209
+ testArg0: 'unsetArg',
210
+ testArg1: true,
211
+ testArg2: 10,
212
+ testArg3: 'testValue',
213
+ },
214
+ defaultOptions: {
215
+ testArg0: 'unsetArg',
216
+ testArg1: false,
217
+ testArg2: 0,
218
+ testArg3: 'defaultValue',
219
+ },
220
+ };
221
+ }
222
+ /** Returns the CommandEndInfo for our fake 'test-command'. */
223
+ function getTestCommandEndInfo(resultCode) {
224
+ return {
225
+ resultCode,
226
+ };
227
+ }
228
+ function getTestCommandProjectInfo() {
229
+ return {
230
+ id: projectUtils.getProjectId('test-app-project'),
231
+ platforms: ['windows'],
232
+ rnwLang: 'cpp',
233
+ usesTS: true,
234
+ usesRNConfig: false,
235
+ jsEngine: 'Hermes',
236
+ rnwSource: 'Source',
237
+ dependencies: [
238
+ {
239
+ id: projectUtils.getProjectId('test-module-project'),
240
+ platforms: ['android', 'windows'],
241
+ rnwLang: 'cpp',
242
+ },
243
+ ],
244
+ };
245
+ }
246
+ function getExtraProps() {
247
+ return {
248
+ extraProp1: true,
249
+ extraProp2: 1234,
250
+ extraProp3: 'test',
251
+ extraProp4: ['test'],
252
+ extraProp5: {
253
+ nestedProp1: true,
254
+ nestedProp2: 1234,
255
+ },
256
+ };
257
+ }
258
+ /** Asynchronously waits the number in ms. */
259
+ async function promiseDelay(ms) {
260
+ return new Promise(resolve => setTimeout(resolve, ms));
261
+ }
262
+ /** The body of the fake 'test-command' which will throw the provided error. */
263
+ async function testCommandBody(errorToThrow) {
264
+ await promiseDelay(100);
265
+ if (errorToThrow) {
266
+ throw errorToThrow;
267
+ }
268
+ }
269
+ /** Runs the complete 'test-command' with the right Telemetry setup and cleanup. */
270
+ async function runTestCommandE2E(commandBody) {
271
+ TelemetryTest.startCommand(getTestCommandStartInfo());
272
+ TelemetryTest.setProjectInfo(getTestCommandProjectInfo());
273
+ let errorCode = 'Success';
274
+ let caughtError;
275
+ try {
276
+ await commandBody();
277
+ }
278
+ catch (ex) {
279
+ caughtError = ex instanceof Error ? ex : new Error(String(ex));
280
+ errorCode =
281
+ caughtError instanceof errorUtils.CodedError
282
+ ? caughtError.type
283
+ : 'Unknown';
284
+ TelemetryTest.trackException(caughtError);
285
+ }
286
+ TelemetryTest.endCommand(getTestCommandEndInfo(errorCode), getExtraProps());
287
+ }
288
+ /** Verifies the contents of events fired during the 'test-command'. */
289
+ function verifyTestCommandTelemetryProcessor(caughtErrors, expectedResultCode, expectedError) {
290
+ return envelope => {
291
+ TelemetryTest.setTestTelemetryProvidersRan();
292
+ try {
293
+ // Processor has run, so the test can (potentially) pass
294
+ const properties = envelope.baseData;
295
+ expect(properties).toBeDefined();
296
+ // Verify basics
297
+ const commonProperties = properties.common;
298
+ expect(commonProperties.commandName).toBe('test-command');
299
+ // Verify versions info
300
+ const versions = properties.versions;
301
+ expect(versions).toBeDefined();
302
+ expect(Object.keys(versions).length).toBeGreaterThan(0);
303
+ for (const key of Object.keys(versions)) {
304
+ expect(versions[key]).toBe(TelemetryTest.getVersion(key));
305
+ }
306
+ // Verify project info
307
+ const project = properties.project;
308
+ expect(project).toStrictEqual(getTestCommandProjectInfo());
309
+ // Verify properties exclusive to error scenarios
310
+ if (envelope.name === telemetry_1.CodedErrorEventName) {
311
+ // Verify exception info
312
+ const exceptionData = envelope.data.exceptionData;
313
+ expect(exceptionData).toBeDefined();
314
+ expect(exceptionData.message).toBeDefined();
315
+ expect(exceptionData.message).not.toBe('');
316
+ expect(exceptionData.message).toBe(TelemetryTest.getPreserveErrorMessages()
317
+ ? errorUtils.sanitizeErrorMessage(expectedError?.message || 'None')
318
+ : '[Removed]');
319
+ // Verify coded error info
320
+ const codedError = envelope.data.codedError;
321
+ expect(codedError).toBeDefined();
322
+ expect(codedError.type).toBe(expectedError instanceof errorUtils.CodedError
323
+ ? expectedError.type
324
+ : 'Unknown');
325
+ // If the exception type is not CodedError but any data got copied into envelope.CodedError.data,
326
+ // for instance autolinking error info, build the expected CodedError.data.
327
+ let expectedCodedErrorData = {};
328
+ if (expectedError instanceof CustomTestError) {
329
+ expectedCodedErrorData = { errno: expectedError.errno };
330
+ }
331
+ expect(codedError.data).toStrictEqual(expectedError.data ??
332
+ expectedCodedErrorData);
333
+ }
334
+ else {
335
+ // If this is not error scenario, it must be a command successful event.
336
+ expect(envelope.name).toBe(telemetry_1.CommandEventName);
337
+ // Verify command info
338
+ const expectedInfo = getTestCommandStartInfo();
339
+ const command = envelope.data.command;
340
+ expect(command).toBeDefined();
341
+ expect(command.args).toStrictEqual(expectedInfo.args);
342
+ expect(command.options).toStrictEqual(expectedInfo.options);
343
+ expect(command.defaultOptions).toStrictEqual(expectedInfo.defaultOptions);
344
+ expect(command.durationInSecs).toBeGreaterThan(0);
345
+ expect(command.resultCode).toBe(expectedResultCode ?? 'Success');
346
+ // Verify extra props
347
+ const extraProps = getExtraProps();
348
+ expect(envelope.data?.additionalData).toStrictEqual(extraProps);
349
+ }
350
+ }
351
+ catch (ex) {
352
+ caughtErrors.push(ex instanceof Error ? ex : new Error(String(ex)));
353
+ }
354
+ return true;
355
+ };
356
+ }
357
+ test('Telemetry run test command end to end, verify event fires', async () => {
358
+ await TelemetryTest.startTest();
359
+ // AI eats errors thrown in telemetry processors
360
+ const caughtErrors = [];
361
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors));
362
+ await runTestCommandE2E(testCommandBody);
363
+ TelemetryTest.endTest(() => {
364
+ // Check if any errors were thrown
365
+ expect(caughtErrors).toHaveLength(0);
366
+ });
367
+ });
368
+ const testTelemetryOptions = [
369
+ { preserveErrorMessages: false },
370
+ { preserveErrorMessages: true },
371
+ ];
372
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with CodedError, verify events fire %s', async (options) => {
373
+ await TelemetryTest.startTest(options);
374
+ const expectedError = new errorUtils.CodedError('MSBuildError', 'test error');
375
+ // AI eats errors thrown in telemetry processors
376
+ const caughtErrors = [];
377
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, expectedError.type, expectedError));
378
+ await runTestCommandE2E(() => testCommandBody(expectedError));
379
+ TelemetryTest.endTest(() => {
380
+ // Check if any errors were thrown
381
+ expect(caughtErrors).toHaveLength(0);
382
+ });
383
+ });
384
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with CodedError (with error in message), verify events fire %s', async (options) => {
385
+ await TelemetryTest.startTest(options);
386
+ const expectedError = new errorUtils.CodedError('MSBuildError', 'error FOO2020: test error');
387
+ // AI eats errors thrown in telemetry processors
388
+ const caughtErrors = [];
389
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, expectedError.type, expectedError));
390
+ await runTestCommandE2E(() => testCommandBody(expectedError));
391
+ TelemetryTest.endTest(() => {
392
+ // Check if any errors were thrown
393
+ expect(caughtErrors).toHaveLength(0);
394
+ });
395
+ });
396
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with CodedError (with data), verify events fire %s', async (options) => {
397
+ await TelemetryTest.startTest(options);
398
+ const expectedError = new errorUtils.CodedError('MSBuildError', 'test error', { foo: 42 });
399
+ // AI eats errors thrown in telemetry processors
400
+ const caughtErrors = [];
401
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, expectedError.type, expectedError));
402
+ await runTestCommandE2E(() => testCommandBody(expectedError));
403
+ TelemetryTest.endTest(() => {
404
+ // Check if any errors were thrown
405
+ expect(caughtErrors).toHaveLength(0);
406
+ });
407
+ });
408
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with Error, verify events fire %s', async (options) => {
409
+ await TelemetryTest.startTest(options);
410
+ const expectedError = new Error('error FOO2020: test error');
411
+ // AI eats errors thrown in telemetry processors
412
+ const caughtErrors = [];
413
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, 'Unknown', expectedError));
414
+ await runTestCommandE2E(() => testCommandBody(expectedError));
415
+ TelemetryTest.endTest(() => {
416
+ // Check if any errors were thrown
417
+ expect(caughtErrors).toHaveLength(0);
418
+ });
419
+ });
420
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with Error (no message), verify events fire %s', async (options) => {
421
+ await TelemetryTest.startTest(options);
422
+ const expectedError = new Error();
423
+ // AI eats errors thrown in telemetry processors
424
+ const caughtErrors = [];
425
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, 'Unknown', expectedError));
426
+ await runTestCommandE2E(() => testCommandBody(expectedError));
427
+ TelemetryTest.endTest(() => {
428
+ // Check if any errors were thrown
429
+ expect(caughtErrors).toHaveLength(0);
430
+ });
431
+ });
432
+ function b(s) {
433
+ throw new Error('hello ' + s);
434
+ }
435
+ function a(s) {
436
+ b(s);
437
+ }
438
+ /** Verifies the contents of an exception's message and stack frames */
439
+ function getVerifyStackTelemetryProcessor(caughtErrors, expectedError) {
440
+ return envelope => {
441
+ try {
442
+ // Processor has run, so the test can (potentially) pass
443
+ TelemetryTest.setTestTelemetryProvidersRan();
444
+ if (envelope.name === telemetry_1.CodedErrorEventName) {
445
+ const data = envelope.data;
446
+ expect(data.exceptionData).toBeDefined();
447
+ expect(data.exceptionData.message).toBeDefined();
448
+ expect(data.exceptionData.message).not.toBe('');
449
+ expect(data.exceptionData.message).toBe(TelemetryTest.getPreserveErrorMessages()
450
+ ? errorUtils.sanitizeErrorMessage(expectedError.message || 'None')
451
+ : '[Removed]');
452
+ const stack = data.exceptionData.parsedStack;
453
+ expect(stack).toBeDefined();
454
+ expect(stack.length).toBeGreaterThan(2);
455
+ const filename = path.relative(process.cwd(), __filename);
456
+ expect(stack[0].functionName).toEqual('b');
457
+ expect(stack[1].functionName).toEqual('b');
458
+ expect(stack[2].functionName).toEqual('a');
459
+ expect(stack[0].filePath).toEqual(`[project_dir]\\???.ts(${filename.length})`);
460
+ expect(stack[1].filePath).toEqual(`[project_dir]\\???.ts(${filename.length})`);
461
+ expect(stack[2].filePath).toEqual(`[project_dir]\\???.ts(${filename.length})`);
462
+ }
463
+ }
464
+ catch (ex) {
465
+ caughtErrors.push(ex instanceof Error ? ex : new Error(String(ex)));
466
+ }
467
+ return true;
468
+ };
469
+ }
470
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with Error, verify sanitized message and stack %s', async (options) => {
471
+ await TelemetryTest.startTest(options);
472
+ const expectedError = new Error('hello world');
473
+ // AI eats errors thrown in telemetry processors
474
+ const caughtErrors = [];
475
+ TelemetryTest.addTelemetryInitializer(getVerifyStackTelemetryProcessor(caughtErrors, expectedError));
476
+ await runTestCommandE2E(async () => {
477
+ await promiseDelay(100);
478
+ a('world');
479
+ });
480
+ TelemetryTest.endTest(() => {
481
+ // Check if any errors were thrown
482
+ expect(caughtErrors).toHaveLength(0);
483
+ });
484
+ });
485
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with Error, verify sanitized message with path and stack %s', async (options) => {
486
+ await TelemetryTest.startTest(options);
487
+ const expectedError = new Error(`hello ${process.cwd()}`);
488
+ // AI eats errors thrown in telemetry processors
489
+ const caughtErrors = [];
490
+ TelemetryTest.addTelemetryInitializer(getVerifyStackTelemetryProcessor(caughtErrors, expectedError));
491
+ await runTestCommandE2E(async () => {
492
+ await promiseDelay(100);
493
+ a(process.cwd());
494
+ });
495
+ TelemetryTest.endTest(() => {
496
+ // Check if any errors were thrown
497
+ expect(caughtErrors).toHaveLength(0);
498
+ });
499
+ });
500
+ test.each(testTelemetryOptions)('A custom Error-based object with MS Build error info is copied into codedError.data appropriately by trackException()', async (options) => {
501
+ await TelemetryTest.startTest(options);
502
+ const expectedError = new CustomTestError('some message');
503
+ // AI eats errors thrown in telemetry processors
504
+ const caughtErrors = [];
505
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, 'Unknown', expectedError));
506
+ await runTestCommandE2E(() => testCommandBody(expectedError));
507
+ TelemetryTest.endTest(() => {
508
+ // Check if any errors were thrown
509
+ expect(caughtErrors).toHaveLength(0);
510
+ });
511
+ });
512
+ test.each(testTelemetryOptions)('Telemetry run test command end to end with CodedError, verifies PII is scrubbed if present in CodedError.', async (options) => {
513
+ await TelemetryTest.startTest(options);
514
+ const codedErrorInfo = new errorUtils.CodedError('MSBuildError', // type
515
+ 'test error', // message
516
+ {
517
+ fieldWithPath: 'Test Error occurred at C:\\some\\file\\path\\project.build.appxrecipe',
518
+ fieldWithNoPath: 'Test Error data',
519
+ fieldWithNoString: 14,
520
+ arrayField: [
521
+ 'No path',
522
+ 15,
523
+ 'Clean this path: C:\\some\\file\\path2\\project.build.appxrecipe',
524
+ [
525
+ 'No path',
526
+ 150,
527
+ 'Also clean this: C:\\some\\file\\path2\\project.build.appxrecipe',
528
+ ],
529
+ ],
530
+ someObject: {
531
+ fieldWithPath: 'Test Error occurred at C:\\some\\file\\path3\\project.build.appxrecipe',
532
+ fieldWithNoPath: 'Test Error data 2',
533
+ fieldWithNoString: 16,
534
+ nestedObject: {
535
+ fieldWithPath: 'Test Error occurred at C:\\some\\file\\path4\\project.build.appxrecipe',
536
+ fieldWithNoPath: 'Test Error data 3',
537
+ fieldWithNoString: 17, // expectation: no changes to this value.
538
+ },
539
+ },
540
+ });
541
+ const expectedError = new errorUtils.CodedError('MSBuildError', // type
542
+ 'test error', // message
543
+ {
544
+ fieldWithPath: 'Test Error occurred at [path]',
545
+ fieldWithNoPath: 'Test Error data',
546
+ fieldWithNoString: 14,
547
+ arrayField: [
548
+ 'No path',
549
+ 15,
550
+ 'Clean this path: [path]',
551
+ ['No path', 150, 'Also clean this: [path]'],
552
+ ],
553
+ someObject: {
554
+ fieldWithPath: 'Test Error occurred at [path]',
555
+ fieldWithNoPath: 'Test Error data 2',
556
+ fieldWithNoString: 16,
557
+ nestedObject: {
558
+ fieldWithPath: 'Test Error occurred at [path]',
559
+ fieldWithNoPath: 'Test Error data 3',
560
+ fieldWithNoString: 17,
561
+ },
562
+ },
563
+ });
564
+ const caughtErrors = [];
565
+ TelemetryTest.addTelemetryInitializer(verifyTestCommandTelemetryProcessor(caughtErrors, expectedError.type, expectedError));
566
+ await runTestCommandE2E(() => testCommandBody(codedErrorInfo));
567
+ TelemetryTest.endTest(() => {
568
+ // Check if any errors were thrown
569
+ expect(caughtErrors).toHaveLength(0);
570
+ });
571
+ });
572
+ //# sourceMappingURL=telemetry.test.js.map