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
|
"use strict";
const SYNC_DATA_PREF = "messaging-system.syncdatastore.data";
const { ExperimentFakes } = ChromeUtils.import(
"resource://testing-common/MSTestUtils.jsm"
);
const { ExperimentStore } = ChromeUtils.import(
"resource://messaging-system/experiments/ExperimentStore.jsm"
);
add_task(async function test_sharedDataMap_key() {
const store = new ExperimentStore();
// Outside of tests we use sharedDataKey for the profile dir filepath
// where we store experiments
Assert.ok(store._sharedDataKey, "Make sure it's defined");
});
add_task(async function test_usageBeforeInitialization() {
const store = ExperimentFakes.store();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "purple", enabled: true },
},
});
Assert.equal(store.getAll().length, 0, "It should not fail");
await store.init();
store.addExperiment(experiment);
Assert.equal(
store.getExperimentForFeature("purple"),
experiment,
"should return a matching experiment for the given feature"
);
});
add_task(async function test_event_add_experiment() {
const sandbox = sinon.createSandbox();
const store = ExperimentFakes.store();
const expected = ExperimentFakes.experiment("foo");
const updateEventCbStub = sandbox.stub();
// Setup ExperimentManager and child store for ExperimentAPI
await store.init();
// Set update cb
store.on("update:foo", updateEventCbStub);
// Add some data
store.addExperiment(expected);
Assert.equal(updateEventCbStub.callCount, 1, "Called once for add");
});
add_task(async function test_event_updates_main() {
const sandbox = sinon.createSandbox();
const store = ExperimentFakes.store();
const experiment = ExperimentFakes.experiment("foo");
const updateEventCbStub = sandbox.stub();
// Setup ExperimentManager and child store for ExperimentAPI
await store.init();
// Set update cb
store.on("update:aboutwelcome", updateEventCbStub);
store.addExperiment(experiment);
store.updateExperiment("foo", { active: false });
Assert.equal(
updateEventCbStub.callCount,
2,
"Should be called twice: add, update"
);
Assert.equal(
updateEventCbStub.secondCall.args[1].active,
false,
"Should be called with updated experiment status"
);
});
add_task(async function test_getExperimentForGroup() {
const store = ExperimentFakes.store();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "purple", enabled: true },
},
});
await store.init();
store.addExperiment(ExperimentFakes.experiment("bar"));
store.addExperiment(experiment);
Assert.equal(
store.getExperimentForFeature("purple"),
experiment,
"should return a matching experiment for the given feature"
);
});
add_task(async function test_recordExposureEvent() {
const manager = ExperimentFakes.manager();
const experiment = ExperimentFakes.experiment("foo");
const experimentData = {
experimentSlug: experiment.slug,
branchSlug: experiment.branch.slug,
featureId: experiment.branch.feature.featureId,
};
await manager.onStartup();
let exposureEvEmit = new Promise(resolve =>
manager.store.on("exposure", (ev, data) => resolve(data))
);
manager.store.addExperiment(experiment);
manager.store._emitExperimentExposure(experimentData);
let result = await exposureEvEmit;
Assert.deepEqual(
result,
experimentData,
"should return the same data as sent"
);
});
add_task(async function test_activateBranch() {
const store = ExperimentFakes.store();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "green", enabled: true },
},
});
await store.init();
store.addExperiment(experiment);
Assert.deepEqual(
store.activateBranch({ featureId: "green" }),
experiment.branch,
"Should return feature of active experiment"
);
});
add_task(async function test_activateBranch_activationEvent() {
const store = ExperimentFakes.store();
const sandbox = sinon.createSandbox();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "green", enabled: true },
},
});
await store.init();
store.addExperiment(experiment);
// Adding stub later because `addExperiment` emits update events
const stub = sandbox.stub(store, "emit");
// Call activateBranch to trigger an activation event
store.activateBranch({ featureId: "green" });
Assert.equal(stub.callCount, 1, "Called by doing activateBranch");
Assert.equal(stub.firstCall.args[0], "exposure", "Has correct event name");
Assert.equal(
stub.firstCall.args[1].experimentSlug,
experiment.slug,
"Has correct payload"
);
});
add_task(async function test_activateBranch_storeFailure() {
const store = ExperimentFakes.store();
const sandbox = sinon.createSandbox();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "green", enabled: true },
},
});
await store.init();
store.addExperiment(experiment);
// Adding stub later because `addExperiment` emits update events
const stub = sandbox.stub(store, "emit");
// Call activateBranch to trigger an activation event
sandbox.stub(store, "getAllActive").throws();
try {
store.activateBranch({ featureId: "green" });
} catch (e) {
/* This is expected */
}
Assert.equal(stub.callCount, 0, "Not called if store somehow fails");
});
add_task(async function test_activateBranch_noActivationEvent() {
const store = ExperimentFakes.store();
const sandbox = sinon.createSandbox();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "green", enabled: true },
},
});
await store.init();
store.addExperiment(experiment);
// Adding stub later because `addExperiment` emits update events
const stub = sandbox.stub(store, "emit");
// Call activateBranch to trigger an activation event
store.activateBranch({ featureId: "green", sendExposurePing: false });
Assert.equal(stub.callCount, 0, "Not called: sendExposurePing is false");
});
add_task(async function test_hasExperimentForFeature() {
const store = ExperimentFakes.store();
await store.init();
store.addExperiment(
ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "green", enabled: true },
},
})
);
store.addExperiment(
ExperimentFakes.experiment("foo2", {
branch: {
slug: "variant",
feature: { featureId: "yellow", enabled: true },
},
})
);
store.addExperiment(
ExperimentFakes.experiment("bar_expired", {
active: false,
branch: {
slug: "variant",
feature: { featureId: "purple", enabled: true },
},
})
);
Assert.equal(
store.hasExperimentForFeature(),
false,
"should return false if the input is empty"
);
Assert.equal(
store.hasExperimentForFeature(undefined),
false,
"should return false if the input is undefined"
);
Assert.equal(
store.hasExperimentForFeature("green"),
true,
"should return true if there is an experiment with any of the given groups"
);
Assert.equal(
store.hasExperimentForFeature("purple"),
false,
"should return false if there is a non-active experiment with the given groups"
);
});
add_task(async function test_getAll_getAllActive() {
const store = ExperimentFakes.store();
await store.init();
["foo", "bar", "baz"].forEach(slug =>
store.addExperiment(ExperimentFakes.experiment(slug, { active: false }))
);
store.addExperiment(ExperimentFakes.experiment("qux", { active: true }));
Assert.deepEqual(
store.getAll().map(e => e.slug),
["foo", "bar", "baz", "qux"],
".getAll() should return all experiments"
);
Assert.deepEqual(
store.getAllActive().map(e => e.slug),
["qux"],
".getAllActive() should return all experiments that are active"
);
});
add_task(async function test_addExperiment() {
const store = ExperimentFakes.store();
const exp = ExperimentFakes.experiment("foo");
await store.init();
store.addExperiment(exp);
Assert.equal(store.get("foo"), exp, "should save experiment by slug");
});
add_task(async function test_updateExperiment() {
const feature = { featureId: "cfr", enabled: true };
const experiment = Object.freeze(
ExperimentFakes.experiment("foo", { feature, active: true })
);
const store = ExperimentFakes.store();
await store.init();
store.addExperiment(experiment);
store.updateExperiment("foo", { active: false });
const actual = store.get("foo");
Assert.equal(actual.active, false, "should change updated props");
Assert.deepEqual(
actual.branch.feature,
feature,
"should not update other props"
);
});
add_task(async function test_sync_access_before_init() {
Services.prefs.clearUserPref(SYNC_DATA_PREF);
let store = ExperimentFakes.store();
Assert.equal(store.getAll().length, 0, "Start with an empty store");
const syncAccessExp = ExperimentFakes.experiment("foo", {
feature: { featureId: "newtab", enabled: "true" },
});
await store.init();
store.addExperiment(syncAccessExp);
let prefValue = JSON.parse(Services.prefs.getStringPref(SYNC_DATA_PREF));
Assert.ok(Object.keys(prefValue).length === 1, "Parsed stored experiment");
Assert.equal(
prefValue.foo.slug,
syncAccessExp.slug,
"Got back the experiment"
);
// New un-initialized store that should read the pref value
store = ExperimentFakes.store();
Assert.equal(store.getAll().length, 1, "Returns experiment from pref");
});
add_task(async function test_sync_access_update() {
Services.prefs.clearUserPref(SYNC_DATA_PREF);
let store = ExperimentFakes.store();
let experiment = ExperimentFakes.experiment("foo", {
feature: { featureId: "aboutwelcome", enabled: true },
});
await store.init();
store.addExperiment(experiment);
store.updateExperiment("foo", {
branch: {
...experiment.branch,
feature: { featureId: "aboutwelcome", enabled: true, value: "bar" },
},
});
store = ExperimentFakes.store();
let experiments = store.getAll();
Assert.equal(experiments.length, 1, "Got back 1 experiment");
Assert.equal(experiments[0].branch.feature.value, "bar", "Got updated value");
});
add_task(async function test_sync_features_only() {
Services.prefs.clearUserPref(SYNC_DATA_PREF);
let store = ExperimentFakes.store();
let experiment = ExperimentFakes.experiment("foo", {
feature: { featureId: "cfr", enabled: true },
});
await store.init();
store.addExperiment(experiment);
store = ExperimentFakes.store();
Assert.equal(store.getAll().length, 0, "cfr is not a sync access experiment");
});
add_task(async function test_sync_access_unenroll() {
Services.prefs.clearUserPref(SYNC_DATA_PREF);
let store = ExperimentFakes.store();
let experiment = ExperimentFakes.experiment("foo", {
feature: { featureId: "aboutwelcome", enabled: true },
active: true,
});
await store.init();
store.addExperiment(experiment);
store.updateExperiment("foo", { active: false });
store = ExperimentFakes.store();
let experiments = store.getAll();
Assert.equal(experiments.length, 0, "Unenrolled experiment is deleted");
});
add_task(async function test_sync_access_unenroll_2() {
Services.prefs.clearUserPref(SYNC_DATA_PREF);
let store = ExperimentFakes.store();
let experiment1 = ExperimentFakes.experiment("foo", {
feature: { featureId: "aboutwelcome", enabled: true },
});
let experiment2 = ExperimentFakes.experiment("bar", {
feature: { featureId: "aboutwelcome", enabled: true },
});
await store.init();
store.addExperiment(experiment1);
store.addExperiment(experiment2);
Assert.equal(store.getAll().length, 2, "2/2 experiments");
store.updateExperiment("bar", { active: false });
let other_store = ExperimentFakes.store();
Assert.equal(
other_store.getAll().length,
1,
"Unenrolled from 1/2 experiments"
);
store.updateExperiment("foo", { active: false });
Assert.equal(
other_store.getAll().length,
0,
"Unenrolled from 2/2 experiments"
);
Assert.equal(
Services.prefs.getStringPref(SYNC_DATA_PREF),
"{}",
"Empty store"
);
});
|