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
|
"use strict";
const { ExperimentAPI } = ChromeUtils.import(
"resource://messaging-system/experiments/ExperimentAPI.jsm"
);
const { ExperimentFakes } = ChromeUtils.import(
"resource://testing-common/MSTestUtils.jsm"
);
const { TestUtils } = ChromeUtils.import(
"resource://testing-common/TestUtils.jsm"
);
const COLLECTION_ID_PREF = "messaging-system.rsexperimentloader.collection_id";
/**
* #getExperiment
*/
add_task(async function test_getExperiment_fromChild_slug() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const expected = ExperimentFakes.experiment("foo");
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => ExperimentFakes.childStore());
manager.store.addExperiment(expected);
// Wait to sync to child
await TestUtils.waitForCondition(
() => ExperimentAPI.getExperiment({ slug: "foo" }),
"Wait for child to sync"
);
Assert.equal(
ExperimentAPI.getExperiment({ slug: "foo" }).slug,
expected.slug,
"should return an experiment by slug"
);
sandbox.restore();
});
add_task(async function test_getExperiment_fromParent_slug() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const expected = ExperimentFakes.experiment("foo");
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
await ExperimentAPI.ready();
manager.store.addExperiment(expected);
Assert.equal(
ExperimentAPI.getExperiment({ slug: "foo" }).slug,
expected.slug,
"should return an experiment by slug"
);
sandbox.restore();
});
add_task(async function test_getExperimentMetaData() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const expected = ExperimentFakes.experiment("foo");
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
await ExperimentAPI.ready();
manager.store.addExperiment(expected);
let metadata = ExperimentAPI.getExperimentMetaData({ slug: expected.slug });
Assert.equal(
Object.keys(metadata.branch).length,
1,
"Should only expose one property"
);
Assert.equal(
metadata.branch.slug,
expected.branch.slug,
"Should have the slug prop"
);
sandbox.restore();
});
add_task(async function test_getExperiment_feature() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const expected = ExperimentFakes.experiment("foo", {
branch: {
slug: "treatment",
value: { title: "hi" },
feature: { featureId: "cfr", enabled: true },
},
});
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => ExperimentFakes.childStore());
manager.store.addExperiment(expected);
// Wait to sync to child
await TestUtils.waitForCondition(
() => ExperimentAPI.getExperiment({ featureId: "cfr" }),
"Wait for child to sync"
);
Assert.equal(
ExperimentAPI.getExperiment({ featureId: "cfr" }).slug,
expected.slug,
"should return an experiment by featureId"
);
sandbox.restore();
});
/**
* #getValue
*/
add_task(async function test_getValue() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const feature = {
featureId: "aboutwelcome",
enabled: true,
value: { title: "hi" },
};
const expected = ExperimentFakes.experiment("foo", {
branch: { slug: "treatment", feature },
});
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => ExperimentFakes.childStore());
manager.store.addExperiment(expected);
await TestUtils.waitForCondition(
() => ExperimentAPI.getExperiment({ slug: "foo" }),
"Wait for child to sync"
);
Assert.deepEqual(
ExperimentAPI.getFeatureValue({ featureId: "aboutwelcome" }),
feature.value,
"should return a Branch by feature"
);
Assert.deepEqual(
ExperimentAPI.getFeatureBranch({ featureId: "aboutwelcome" }),
expected.branch,
"should return an experiment branch by feature"
);
Assert.equal(
ExperimentAPI.getFeatureBranch({ featureId: "doesnotexist" }),
undefined,
"should return undefined if the experiment is not found"
);
sandbox.restore();
});
/**
* #isFeatureEnabled
*/
add_task(async function test_isFeatureEnabledDefault() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const FEATURE_ENABLED_DEFAULT = true;
const expected = ExperimentFakes.experiment("foo");
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
manager.store.addExperiment(expected);
Assert.deepEqual(
ExperimentAPI.isFeatureEnabled("aboutwelcome", FEATURE_ENABLED_DEFAULT),
FEATURE_ENABLED_DEFAULT,
"should return enabled true as default"
);
sandbox.restore();
});
add_task(async function test_isFeatureEnabled() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
const feature = {
featureId: "aboutwelcome",
enabled: false,
value: null,
};
const expected = ExperimentFakes.experiment("foo", {
branch: { slug: "treatment", feature },
});
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
manager.store.addExperiment(expected);
Assert.deepEqual(
ExperimentAPI.isFeatureEnabled("aboutwelcome", true),
feature.enabled,
"should return feature as disabled"
);
sandbox.restore();
});
/**
* #getRecipe
*/
add_task(async function test_getRecipe() {
const sandbox = sinon.createSandbox();
const RECIPE = ExperimentFakes.recipe("foo");
const collectionName = Services.prefs.getStringPref(COLLECTION_ID_PREF);
sandbox.stub(ExperimentAPI._remoteSettingsClient, "get").resolves([RECIPE]);
const recipe = await ExperimentAPI.getRecipe("foo");
Assert.deepEqual(
recipe,
RECIPE,
"should return an experiment recipe if found"
);
Assert.equal(
ExperimentAPI._remoteSettingsClient.collectionName,
collectionName,
"Loaded the expected collection"
);
sandbox.restore();
});
add_task(async function test_getRecipe_Failure() {
const sandbox = sinon.createSandbox();
sandbox.stub(ExperimentAPI._remoteSettingsClient, "get").throws();
const recipe = await ExperimentAPI.getRecipe("foo");
Assert.equal(recipe, undefined, "should return undefined if RS throws");
sandbox.restore();
});
/**
* #getAllBranches
*/
add_task(async function test_getAllBranches() {
const sandbox = sinon.createSandbox();
const RECIPE = ExperimentFakes.recipe("foo");
sandbox.stub(ExperimentAPI._remoteSettingsClient, "get").resolves([RECIPE]);
const branches = await ExperimentAPI.getAllBranches("foo");
Assert.deepEqual(
branches,
RECIPE.branches,
"should return all branches if found a recipe"
);
sandbox.restore();
});
add_task(async function test_getAllBranches_Failure() {
const sandbox = sinon.createSandbox();
sandbox.stub(ExperimentAPI._remoteSettingsClient, "get").throws();
const branches = await ExperimentAPI.getAllBranches("foo");
Assert.equal(branches, undefined, "should return undefined if RS throws");
sandbox.restore();
});
/**
* #on
* #off
*/
add_task(async function test_addExperiment_eventEmit_add() {
const sandbox = sinon.createSandbox();
const slugStub = sandbox.stub();
const featureStub = sandbox.stub();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "purple", enabled: true },
},
});
const store = ExperimentFakes.store();
sandbox.stub(ExperimentAPI, "_store").get(() => store);
await store.init();
await ExperimentAPI.ready();
ExperimentAPI.on("update", { slug: "foo" }, slugStub);
ExperimentAPI.on("update", { featureId: "purple" }, featureStub);
store.addExperiment(experiment);
Assert.equal(slugStub.callCount, 1);
Assert.equal(slugStub.firstCall.args[1].slug, experiment.slug);
Assert.equal(featureStub.callCount, 1);
Assert.equal(featureStub.firstCall.args[1].slug, experiment.slug);
});
add_task(async function test_updateExperiment_eventEmit_add_and_update() {
const sandbox = sinon.createSandbox();
const slugStub = sandbox.stub();
const featureStub = sandbox.stub();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "purple", enabled: true },
},
});
const store = ExperimentFakes.store();
sandbox.stub(ExperimentAPI, "_store").get(() => store);
await store.init();
await ExperimentAPI.ready();
store.addExperiment(experiment);
ExperimentAPI.on("update", { slug: "foo" }, slugStub);
ExperimentAPI.on("update", { featureId: "purple" }, featureStub);
store.updateExperiment(experiment.slug, experiment);
await TestUtils.waitForCondition(
() => slugStub.callCount == 2,
"Wait for `on` method to notify callback about the `add` event."
);
// Called twice, once when attaching the event listener (because there is an
// existing experiment with that name) and 2nd time for the update event
Assert.equal(slugStub.firstCall.args[1].slug, experiment.slug);
Assert.equal(featureStub.callCount, 2, "Called twice for feature");
Assert.equal(featureStub.firstCall.args[1].slug, experiment.slug);
});
add_task(async function test_updateExperiment_eventEmit_off() {
const sandbox = sinon.createSandbox();
const slugStub = sandbox.stub();
const featureStub = sandbox.stub();
const experiment = ExperimentFakes.experiment("foo", {
branch: {
slug: "variant",
feature: { featureId: "purple", enabled: true },
},
});
const store = ExperimentFakes.store();
sandbox.stub(ExperimentAPI, "_store").get(() => store);
await store.init();
await ExperimentAPI.ready();
ExperimentAPI.on("update", { slug: "foo" }, slugStub);
ExperimentAPI.on("update", { featureId: "purple" }, featureStub);
store.addExperiment(experiment);
ExperimentAPI.off("update:foo", slugStub);
ExperimentAPI.off("update:purple", featureStub);
store.updateExperiment(experiment.slug, experiment);
Assert.equal(slugStub.callCount, 1, "Called only once before `off`");
Assert.equal(featureStub.callCount, 1, "Called only once before `off`");
});
|