forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.test.ts
More file actions
376 lines (305 loc) · 13.7 KB
/
telemetry.test.ts
File metadata and controls
376 lines (305 loc) · 13.7 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
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
import { expect } from 'chai';
import * as sinon from 'sinon';
import TelemetryReporter from 'vscode-extension-telemetry';
import { ExtensionContext, workspace, ConfigurationTarget, window } from 'vscode';
import { TelemetryListener, telemetryListener as globalTelemetryListener } from '../../telemetry';
import { UserCancellationException } from '../../commandRunner';
import { fail } from 'assert';
import { ENABLE_TELEMETRY } from '../../config';
const sandbox = sinon.createSandbox();
describe('telemetry reporting', function() {
// setting preferences can trigger lots of background activity
// so need to bump up the timeout of this test.
this.timeout(10000);
let originalTelemetryExtension: boolean | undefined;
let originalTelemetryGlobal: boolean | undefined;
let isCanary: string;
let ctx: ExtensionContext;
let telemetryListener: TelemetryListener;
beforeEach(async () => {
try {
// in case a previous test has accidentally activated this extension,
// need to disable it first.
// Accidentaly activation may happen asynchronously due to activationEvents
// specified in the package.json.
globalTelemetryListener?.dispose();
ctx = createMockExtensionContext();
sandbox.stub(TelemetryReporter.prototype, 'sendTelemetryEvent');
sandbox.stub(TelemetryReporter.prototype, 'sendTelemetryException');
sandbox.stub(TelemetryReporter.prototype, 'dispose');
originalTelemetryExtension = workspace.getConfiguration().get<boolean>('codeQL.telemetry.enableTelemetry');
originalTelemetryGlobal = workspace.getConfiguration().get<boolean>('telemetry.enableTelemetry');
isCanary = (!!workspace.getConfiguration().get<boolean>('codeQL.canary')).toString();
// each test will default to telemetry being enabled
await enableTelemetry('telemetry', true);
await enableTelemetry('codeQL.telemetry', true);
telemetryListener = new TelemetryListener('my-id', '1.2.3', 'fake-key', ctx);
await wait(100);
} catch (e) {
console.error(e);
}
});
afterEach(async () => {
telemetryListener?.dispose();
// await wait(100);
try {
sandbox.restore();
await enableTelemetry('telemetry', originalTelemetryGlobal);
await enableTelemetry('codeQL.telemetry', originalTelemetryExtension);
} catch (e) {
console.error(e);
}
});
it('should initialize telemetry when both options are enabled', async () => {
await telemetryListener.initialize();
expect(telemetryListener._reporter).not.to.be.undefined;
const reporter: any = telemetryListener._reporter;
expect(reporter.extensionId).to.eq('my-id');
expect(reporter.extensionVersion).to.eq('1.2.3');
expect(reporter.userOptIn).to.eq(true); // enabled
});
it('should initialize telemetry when global option disabled', async () => {
try {
await enableTelemetry('telemetry', false);
await telemetryListener.initialize();
expect(telemetryListener._reporter).not.to.be.undefined;
const reporter: any = telemetryListener._reporter;
expect(reporter.userOptIn).to.eq(false); // disabled
} catch (e) {
fail(e as Error);
}
});
it('should not initialize telemetry when extension option disabled', async () => {
try {
await enableTelemetry('codeQL.telemetry', false);
await telemetryListener.initialize();
expect(telemetryListener._reporter).to.be.undefined;
} catch (e) {
fail(e as Error);
}
});
it('should not initialize telemetry when both options disabled', async () => {
await enableTelemetry('codeQL.telemetry', false);
await enableTelemetry('telemetry', false);
await telemetryListener.initialize();
expect(telemetryListener._reporter).to.be.undefined;
});
it('should dispose telemetry object when re-initializing and should not add multiple', async () => {
await telemetryListener.initialize();
expect(telemetryListener._reporter).not.to.be.undefined;
const firstReporter = telemetryListener._reporter;
await telemetryListener.initialize();
expect(telemetryListener._reporter).not.to.be.undefined;
expect(telemetryListener._reporter).not.to.eq(firstReporter);
expect(TelemetryReporter.prototype.dispose).to.have.been.calledOnce;
// initializing a third time continues to dispose
await telemetryListener.initialize();
expect(TelemetryReporter.prototype.dispose).to.have.been.calledTwice;
});
it('should reinitialize reporter when extension setting changes', async () => {
await telemetryListener.initialize();
expect(TelemetryReporter.prototype.dispose).not.to.have.been.called;
expect(telemetryListener._reporter).not.to.be.undefined;
// this disables the reporter
await enableTelemetry('codeQL.telemetry', false);
expect(telemetryListener._reporter).to.be.undefined;
expect(TelemetryReporter.prototype.dispose).to.have.been.calledOnce;
// creates a new reporter, but does not dispose again
await enableTelemetry('codeQL.telemetry', true);
expect(telemetryListener._reporter).not.to.be.undefined;
expect(TelemetryReporter.prototype.dispose).to.have.been.calledOnce;
});
it('should set userOprIn to false when global setting changes', async () => {
await telemetryListener.initialize();
const reporter: any = telemetryListener._reporter;
expect(reporter.userOptIn).to.eq(true); // enabled
await enableTelemetry('telemetry', false);
expect(reporter.userOptIn).to.eq(false); // disabled
});
it('should send an event', async () => {
await telemetryListener.initialize();
telemetryListener.sendCommandUsage('command-id', 1234, undefined);
expect(TelemetryReporter.prototype.sendTelemetryEvent).to.have.been.calledOnceWith('command-usage',
{
name: 'command-id',
status: 'Success',
isCanary
},
{ executionTime: 1234 });
expect(TelemetryReporter.prototype.sendTelemetryException).not.to.have.been.called;
});
it('should send a command usage event with an error', async () => {
await telemetryListener.initialize();
telemetryListener.sendCommandUsage('command-id', 1234, new UserCancellationException());
expect(TelemetryReporter.prototype.sendTelemetryEvent).to.have.been.calledOnceWith('command-usage',
{
name: 'command-id',
status: 'Cancelled',
isCanary
},
{ executionTime: 1234 });
expect(TelemetryReporter.prototype.sendTelemetryException).not.to.have.been.called;
});
it('should avoid sending an event when telemetry is disabled', async () => {
await telemetryListener.initialize();
await enableTelemetry('codeQL.telemetry', false);
telemetryListener.sendCommandUsage('command-id', 1234, undefined);
telemetryListener.sendCommandUsage('command-id', 1234, new Error());
expect(TelemetryReporter.prototype.sendTelemetryEvent).not.to.have.been.called;
expect(TelemetryReporter.prototype.sendTelemetryException).not.to.have.been.called;
});
it('should send an event when telemetry is re-enabled', async () => {
await telemetryListener.initialize();
await enableTelemetry('codeQL.telemetry', false);
await enableTelemetry('codeQL.telemetry', true);
telemetryListener.sendCommandUsage('command-id', 1234, undefined);
expect(TelemetryReporter.prototype.sendTelemetryEvent).to.have.been.calledOnceWith('command-usage',
{
name: 'command-id',
status: 'Success',
isCanary
},
{ executionTime: 1234 });
});
it('should filter undesired properties from telemetry payload', async () => {
await telemetryListener.initialize();
// Reach into the internal appInsights client to grab our telemetry processor.
const telemetryProcessor: Function =
((telemetryListener._reporter as any).appInsightsClient._telemetryProcessors)[0];
const envelop = {
tags: {
'ai.cloud.roleInstance': true,
other: true
},
data: {
baseData: {
properties: {
'common.remotename': true,
other: true
}
}
}
};
const res = telemetryProcessor(envelop);
expect(res).to.eq(true);
expect(envelop).to.deep.eq({
tags: {
other: true
},
data: {
baseData: {
properties: {
other: true
}
}
}
});
});
it('should request permission if popup has never been seen before', async () => {
sandbox.stub(window, 'showInformationMessage').resolvesArg(3 /* "yes" item */);
await ctx.globalState.update('telemetry-request-viewed', false);
await enableTelemetry('codeQL.telemetry', false);
await telemetryListener.initialize();
// Wait 50 ms for user's selection to propagate in settings.
await wait(50);
// Dialog opened, user clicks "yes" and telemetry enabled
expect(window.showInformationMessage).to.have.been.calledOnce;
expect(ENABLE_TELEMETRY.getValue()).to.eq(true);
expect(ctx.globalState.get('telemetry-request-viewed')).to.be.true;
});
it('should prevent telemetry if permission is denied', async () => {
sandbox.stub(window, 'showInformationMessage').resolvesArg(4 /* "no" item */);
await ctx.globalState.update('telemetry-request-viewed', false);
await enableTelemetry('codeQL.telemetry', true);
await telemetryListener.initialize();
// Dialog opened, user clicks "no" and telemetry disabled
expect(window.showInformationMessage).to.have.been.calledOnce;
expect(ENABLE_TELEMETRY.getValue()).to.eq(false);
expect(ctx.globalState.get('telemetry-request-viewed')).to.be.true;
});
it('should unchange telemetry if permission dialog is dismissed', async () => {
sandbox.stub(window, 'showInformationMessage').resolves(undefined /* cancelled */);
await ctx.globalState.update('telemetry-request-viewed', false);
// this causes requestTelemetryPermission to be called
await enableTelemetry('codeQL.telemetry', false);
// Dialog opened, and user closes without interacting with it
expect(window.showInformationMessage).to.have.been.calledOnce;
expect(ENABLE_TELEMETRY.getValue()).to.eq(false);
// dialog was canceled, so should not have marked as viewed
expect(ctx.globalState.get('telemetry-request-viewed')).to.be.false;
});
it('should unchange telemetry if permission dialog is cancelled if starting as true', async () => {
await enableTelemetry('codeQL.telemetry', false);
// as before, except start with telemetry enabled. It should _stay_ enabled if the
// dialog is canceled.
sandbox.stub(window, 'showInformationMessage').resolves(undefined /* cancelled */);
await ctx.globalState.update('telemetry-request-viewed', false);
// this causes requestTelemetryPermission to be called
await enableTelemetry('codeQL.telemetry', true);
// Dialog opened, and user closes without interacting with it
// Telemetry state should not have changed
expect(window.showInformationMessage).to.have.been.calledOnce;
expect(ENABLE_TELEMETRY.getValue()).to.eq(true);
// dialog was canceled, so should not have marked as viewed
expect(ctx.globalState.get('telemetry-request-viewed')).to.be.false;
});
it('should avoid showing dialog if global telemetry is disabled', async () => {
// when telemetry is disabled globally, we never want to show the
// opt in/out dialog. We just assume that codeql telemetry should
// remain disabled as well.
// If the user ever turns global telemetry back on, then we can
// show the dialog.
await enableTelemetry('telemetry', false);
await ctx.globalState.update('telemetry-request-viewed', false);
sandbox.stub(window, 'showInformationMessage');
await telemetryListener.initialize();
// popup should not be shown even though we have initialized telemetry
expect(window.showInformationMessage).not.to.have.been.called;
});
// This test is failing because codeQL.canary is not a registered configuration.
// We do not want to have it registered because we don't want this item
// appearing in the settings page. It needs to olny be set by users we tell
// about it to.
// At this point, I see no other way of testing re-requesting permission.
xit('should request permission again when user changes canary setting', async () => {
// initially, both canary and telemetry are false
await workspace.getConfiguration().update('codeQL.canary', false);
await enableTelemetry('codeQL.telemetry', false);
await ctx.globalState.update('telemetry-request-viewed', true);
await telemetryListener.initialize();
sandbox.stub(window, 'showInformationMessage').resolves(undefined /* cancelled */);
// set canary to true
await workspace.getConfiguration().update('codeQL.canary', true);
// now, we should have to click through the telemetry requestor again
expect(ctx.globalState.get('telemetry-request-viewed')).to.be.false;
expect(window.showInformationMessage).to.have.been.calledOnce;
});
function createMockExtensionContext(): ExtensionContext {
return {
globalState: {
_state: {
'telemetry-request-viewed': true
} as Record<string, any>,
get(key: string) {
return this._state[key];
},
update(key: string, val: any) {
this._state[key] = val;
}
}
} as any;
}
async function enableTelemetry(section: string, value: boolean | undefined) {
await workspace.getConfiguration(section).update(
'enableTelemetry', value, ConfigurationTarget.Global
);
// Need to wait some time since the onDidChangeConfiguration listeners fire
// asynchronously. Must ensure they to complete in order to have a successful test.
await wait(100);
}
async function wait(ms = 0) {
return new Promise(resolve =>
setTimeout(resolve, ms)
);
}
});