-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathanalytics-service.ts
295 lines (242 loc) · 10.8 KB
/
analytics-service.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
import { AnalyticsService } from "../../../lib/services/analytics/analytics-service";
import { Yok } from "../../../lib/common/yok";
import * as stubs from "../../stubs";
import { assert } from "chai";
import { EventEmitter } from "events";
import { AnalyticsClients } from "../../../lib/common/constants";
const helpers = require("../../../lib/common/helpers");
const originalIsInteractive = helpers.isInteractive;
const trackFeatureUsage = "TrackFeatureUsage";
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("options", {});
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("staticConfig", {
disableAnalytics: false,
TRACK_FEATURE_USAGE_SETTING_NAME: trackFeatureUsage,
PATH_TO_BOOTSTRAP: "pathToBootstrap.js"
});
testInjector.register("prompter", {
});
testInjector.register("userSettingsService", {
getSettingValue: async (settingName: string): Promise<any> => {
return "true";
}
});
testInjector.register("analyticsSettingsService", {
canDoRequest: (): Promise<boolean> => Promise.resolve(true)
});
testInjector.register("osInfo", {});
testInjector.register("childProcess", {});
testInjector.register("processService", {
attachToProcessExitSignals: (context: any, callback: () => void): void => undefined
});
testInjector.register("projectDataService", {});
testInjector.register("mobileHelper", {});
return testInjector;
};
describe("analyticsService", () => {
afterEach(() => {
helpers.isInteractive = originalIsInteractive;
});
describe("trackInGoogleAnalytics", () => {
describe("does not track", () => {
const testScenario = async (configuration: {
disableAnalytics: boolean,
assertMessage: string,
userSettingsServiceOpts?: { trackFeatureUsageValue: string, defaultValue: string }
}) => {
const testInjector = createTestInjector();
const staticConfig = testInjector.resolve<Config.IStaticConfig>("staticConfig");
staticConfig.disableAnalytics = configuration.disableAnalytics;
configuration.userSettingsServiceOpts = configuration.userSettingsServiceOpts || { trackFeatureUsageValue: "false", defaultValue: "true" };
const userSettingsService = testInjector.resolve<any>("userSettingsService");
userSettingsService.getSettingValue = async (settingName: string): Promise<string> => {
if (settingName === trackFeatureUsage) {
return configuration.userSettingsServiceOpts.trackFeatureUsageValue;
}
return configuration.userSettingsServiceOpts.defaultValue;
};
let isChildProcessSpawned = false;
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
childProcess.spawn = (command: string, args?: string[], options?: any): any => {
isChildProcessSpawned = true;
};
const analyticsService = testInjector.resolve<IAnalyticsService>(AnalyticsService);
await analyticsService.trackInGoogleAnalytics({
googleAnalyticsDataType: GoogleAnalyticsDataType.Page,
customDimensions: {
customDimension1: "value1"
}
});
assert.isFalse(isChildProcessSpawned, configuration.assertMessage);
};
it("does not track when staticConfig's disableAnalytics is true", () => {
return testScenario({
disableAnalytics: true,
assertMessage: "When staticConfig.disableAnalytics is true, no child process should be started, i.e. we should not track anything."
});
});
it(`does not track when ${trackFeatureUsage} is not true`, async () => {
await testScenario({
disableAnalytics: false,
assertMessage: `When ${trackFeatureUsage} is false, no child process should be started, i.e. we should not track anything.`,
userSettingsServiceOpts: {
trackFeatureUsageValue: "false", defaultValue: "true"
}
});
await testScenario({
disableAnalytics: false,
assertMessage: `When ${trackFeatureUsage} is undefined, no child process should be started, i.e. we should not track anything.`,
userSettingsServiceOpts: {
trackFeatureUsageValue: undefined, defaultValue: "true"
}
});
});
});
const getSpawnedProcess = (): any => {
const spawnedProcess: any = new EventEmitter();
spawnedProcess.stdout = new EventEmitter();
spawnedProcess.stderr = new EventEmitter();
spawnedProcess.unref = (): void => undefined;
return spawnedProcess;
};
describe("does not fail", () => {
const assertExpectedError = async (testInjector: IInjector, opts: { isChildProcessSpawned: boolean, expectedErrorMessage: string }) => {
const analyticsService = testInjector.resolve<IAnalyticsService>(AnalyticsService);
await analyticsService.trackInGoogleAnalytics({
googleAnalyticsDataType: GoogleAnalyticsDataType.Page,
customDimensions: {
customDimension1: "value1"
}
});
assert.isTrue(opts.isChildProcessSpawned);
const logger = testInjector.resolve<stubs.LoggerStub>("logger");
assert.isTrue(logger.traceOutput.indexOf(opts.expectedErrorMessage) !== -1);
};
const setupTest = (expectedErrorMessage: string): any => {
const testInjector = createTestInjector();
const opts = {
isChildProcessSpawned: false,
expectedErrorMessage
};
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
return {
testInjector,
opts,
childProcess
};
};
it("when unable to start broker process", async () => {
const { testInjector, childProcess, opts } = setupTest("Unable to get broker instance due to error: Error: custom error");
childProcess.spawn = (command: string, args?: string[], options?: any): any => {
opts.isChildProcessSpawned = true;
throw new Error("custom error");
};
await assertExpectedError(testInjector, opts);
});
it("when broker cannot start for required timeout", async () => {
const { testInjector, childProcess, opts } = setupTest("Unable to get broker instance due to error: Error: Unable to start Analytics Broker process.");
const originalSetTimeout = setTimeout;
childProcess.spawn = (command: string, args?: string[], options?: any): any => {
opts.isChildProcessSpawned = true;
global.setTimeout = (callback: (...args: any[]) => void, ms: number, ...otherArgs: any[]) => originalSetTimeout(callback, 1);
return getSpawnedProcess();
};
await assertExpectedError(testInjector, opts);
global.setTimeout = originalSetTimeout;
});
it("when broker is not connected", async () => {
const { testInjector, childProcess, opts } = setupTest("Broker not found or not connected.");
childProcess.spawn = (command: string, args?: string[], options?: any): any => {
opts.isChildProcessSpawned = true;
const spawnedProcess: any = getSpawnedProcess();
spawnedProcess.connected = false;
spawnedProcess.send = (): void => undefined;
setTimeout(() => spawnedProcess.emit("message", AnalyticsMessages.BrokerReadyToReceive), 1);
return spawnedProcess;
};
await assertExpectedError(testInjector, opts);
});
it("when sending message fails", async () => {
const { testInjector, childProcess, opts } = setupTest("Error while trying to send message to broker: Error: Failed to sent data.");
childProcess.spawn = (command: string, args?: string[], options?: any): any => {
opts.isChildProcessSpawned = true;
const spawnedProcess: any = getSpawnedProcess();
spawnedProcess.connected = true;
spawnedProcess.send = (): void => {
throw new Error("Failed to sent data.");
};
setTimeout(() => spawnedProcess.emit("message", AnalyticsMessages.BrokerReadyToReceive), 1);
return spawnedProcess;
};
await assertExpectedError(testInjector, opts);
});
});
describe("sends correct message to broker", () => {
const setupTest = (expectedResult: any, dataToSend: any, terminalOpts?: { isInteractive: boolean }): { testInjector: IInjector, opts: any } => {
helpers.isInteractive = () => terminalOpts ? terminalOpts.isInteractive : true;
const testInjector = createTestInjector();
const opts = {
isChildProcessSpawned: false,
expectedResult,
dataToSend,
messageSent: <any>null
};
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
childProcess.spawn = (command: string, args?: string[], options?: any): any => {
opts.isChildProcessSpawned = true;
const spawnedProcess: any = getSpawnedProcess();
spawnedProcess.connected = true;
spawnedProcess.send = (msg: any, action: () => void): void => {
opts.messageSent = msg;
action();
};
setTimeout(() => spawnedProcess.emit("message", AnalyticsMessages.BrokerReadyToReceive), 1);
return spawnedProcess;
};
return {
testInjector,
opts
};
};
const assertExpectedResult = async (testInjector: IInjector, opts: { isChildProcessSpawned: boolean, expectedResult: any, messageSent: any, dataToSend: any }) => {
const analyticsService = testInjector.resolve<IAnalyticsService>(AnalyticsService);
await analyticsService.trackInGoogleAnalytics(opts.dataToSend);
assert.isTrue(opts.isChildProcessSpawned);
assert.deepEqual(opts.messageSent, opts.expectedResult);
};
const getDataToSend = (gaDataType: string): any => ({
googleAnalyticsDataType: gaDataType,
customDimensions: {
customDimension1: "value1"
}
});
const getExpectedResult = (gaDataType: string, analyticsClient?: string): any => ({
type: "googleAnalyticsData",
category: "CLI",
googleAnalyticsDataType: gaDataType,
customDimensions: { customDimension1: "value1", cd5: analyticsClient || "CLI" }
});
_.each([GoogleAnalyticsDataType.Page, GoogleAnalyticsDataType.Event], (googleAnalyticsDataType: string) => {
it(`when data is ${googleAnalyticsDataType}`, async () => {
const { testInjector, opts } = setupTest(getExpectedResult(googleAnalyticsDataType), getDataToSend(googleAnalyticsDataType));
await assertExpectedResult(testInjector, opts);
});
it(`when data is ${googleAnalyticsDataType} and terminal is not interactive`, async () => {
const { testInjector, opts } = setupTest(getExpectedResult(googleAnalyticsDataType, AnalyticsClients.Unknown), getDataToSend(googleAnalyticsDataType), { isInteractive: false });
await assertExpectedResult(testInjector, opts);
});
_.each([true, false], (isInteractive) => {
it(`when data is ${googleAnalyticsDataType} terminal is ${isInteractive ? "" : "not "}interactive and --analyticsClient is passed`, async () => {
const analyticsClient = "AnalyticsClient";
const { testInjector, opts } = setupTest(getExpectedResult(googleAnalyticsDataType, analyticsClient), getDataToSend(googleAnalyticsDataType), { isInteractive });
const options = testInjector.resolve<IOptions>("options");
options.analyticsClient = analyticsClient;
await assertExpectedResult(testInjector, opts);
});
});
});
});
});
});