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
|
"use strict";
const {
ExperimentAPI,
_ExperimentFeature: ExperimentFeature,
} = ChromeUtils.import("resource://nimbus/ExperimentAPI.jsm");
const { ExperimentFakes } = ChromeUtils.import(
"resource://testing-common/NimbusTestUtils.jsm"
);
async function setupForExperimentFeature() {
const sandbox = sinon.createSandbox();
const manager = ExperimentFakes.manager();
await manager.onStartup();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
return { sandbox, manager };
}
function setDefaultBranch(pref, value) {
let branch = Services.prefs.getDefaultBranch("");
branch.setStringPref(pref, value);
}
const TEST_FALLBACK_PREF = "testprefbranch.config";
const FAKE_FEATURE_MANIFEST = {
description: "Test feature",
exposureDescription: "Used in tests",
variables: {
enabled: {
type: "boolean",
fallbackPref: "testprefbranch.enabled",
},
config: {
type: "json",
fallbackPref: TEST_FALLBACK_PREF,
},
remoteValue: {
type: "boolean",
},
test: {
type: "boolean",
},
title: {
type: "string",
},
},
};
/**
* FOG requires a little setup in order to test it
*/
add_setup(function test_setup() {
// FOG needs a profile directory to put its data in.
do_get_profile();
// FOG needs to be initialized in order for data to flow.
Services.fog.initializeFOG();
});
add_task(async function test_ExperimentFeature_test_helper_ready() {
const { manager } = await setupForExperimentFeature();
await manager.store.ready();
const featureInstance = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
await ExperimentFakes.enrollWithRollout(
{
featureId: "foo",
value: { remoteValue: "mochitest", enabled: true },
},
{
manager,
}
);
Assert.equal(
featureInstance.getVariable("remoteValue"),
"mochitest",
"set by remote config"
);
});
add_task(async function test_record_exposure_event() {
const { sandbox, manager } = await setupForExperimentFeature();
const featureInstance = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
const exposureSpy = sandbox.spy(ExperimentAPI, "recordExposureEvent");
const getExperimentSpy = sandbox.spy(ExperimentAPI, "getExperimentMetaData");
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
// Clear any pre-existing data in Glean
Services.fog.testResetFOG();
featureInstance.recordExposureEvent();
Assert.ok(
exposureSpy.notCalled,
"should not emit an exposure event when no experiment is active"
);
// Check that there aren't any Glean exposure events yet
var exposureEvents = Glean.nimbusEvents.exposure.testGetValue();
Assert.equal(
undefined,
exposureEvents,
"no Glean exposure events before exposure"
);
await manager.store.addEnrollment(
ExperimentFakes.experiment("blah", {
branch: {
slug: "treatment",
features: [
{
featureId: "foo",
value: { enabled: false },
},
],
},
})
);
featureInstance.recordExposureEvent();
Assert.ok(
exposureSpy.calledOnce,
"should emit an exposure event when there is an experiment"
);
Assert.equal(getExperimentSpy.callCount, 2, "Should be called every time");
// Check that the Glean exposure event was recorded.
exposureEvents = Glean.nimbusEvents.exposure.testGetValue();
// We expect only one event
Assert.equal(1, exposureEvents.length);
// And that one event matches the expected
Assert.equal(
"blah",
exposureEvents[0].extra.experiment,
"Glean.nimbusEvents.exposure recorded with correct experiment slug"
);
Assert.equal(
"treatment",
exposureEvents[0].extra.branch,
"Glean.nimbusEvents.exposure recorded with correct branch slug"
);
Assert.equal(
"foo",
exposureEvents[0].extra.feature_id,
"Glean.nimbusEvents.exposure recorded with correct feature id"
);
sandbox.restore();
});
add_task(async function test_record_exposure_event_once() {
const { sandbox, manager } = await setupForExperimentFeature();
const featureInstance = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
const exposureSpy = sandbox.spy(ExperimentAPI, "recordExposureEvent");
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
// Clear any pre-existing data in Glean
Services.fog.testResetFOG();
await manager.store.addEnrollment(
ExperimentFakes.experiment("blah", {
branch: {
slug: "treatment",
features: [
{
featureId: "foo",
value: { enabled: false },
},
],
},
})
);
featureInstance.recordExposureEvent({ once: true });
featureInstance.recordExposureEvent({ once: true });
featureInstance.recordExposureEvent({ once: true });
Assert.ok(
exposureSpy.calledOnce,
"Should emit a single exposure event when the once param is true."
);
// Check that the Glean exposure event was recorded.
let exposureEvents = Glean.nimbusEvents.exposure.testGetValue();
// We expect only one event
Assert.equal(1, exposureEvents.length);
sandbox.restore();
});
add_task(async function test_allow_multiple_exposure_events() {
const { sandbox, manager } = await setupForExperimentFeature();
const featureInstance = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
const exposureSpy = sandbox.spy(ExperimentAPI, "recordExposureEvent");
// Clear any pre-existing data in Glean
Services.fog.testResetFOG();
let doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig(
{
featureId: "foo",
value: { enabled: false },
},
{ manager }
);
featureInstance.recordExposureEvent();
featureInstance.recordExposureEvent();
featureInstance.recordExposureEvent();
Assert.ok(exposureSpy.called, "Should emit exposure event");
Assert.equal(
exposureSpy.callCount,
3,
"Should emit an exposure event for each function call"
);
// Check that the Glean exposure event was recorded.
let exposureEvents = Glean.nimbusEvents.exposure.testGetValue();
// We expect 3 events
Assert.equal(3, exposureEvents.length);
sandbox.restore();
await doExperimentCleanup();
});
add_task(async function test_onUpdate_before_store_ready() {
let sandbox = sinon.createSandbox();
const feature = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
const stub = sandbox.stub();
const manager = ExperimentFakes.manager();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
sandbox.stub(manager.store, "getAllActive").returns([
ExperimentFakes.experiment("foo-experiment", {
branch: {
slug: "control",
features: [
{
featureId: "foo",
value: null,
},
],
},
}),
]);
// We register for updates before the store finished loading experiments
// from disk
feature.onUpdate(stub);
await manager.onStartup();
Assert.ok(
stub.calledOnce,
"Called on startup after loading experiments from disk"
);
Assert.equal(
stub.firstCall.args[1],
"feature-experiment-loaded",
"Called for the expected reason"
);
});
add_task(async function test_ExperimentFeature_test_ready_late() {
const { manager, sandbox } = await setupForExperimentFeature();
const stub = sandbox.stub();
const featureInstance = new ExperimentFeature(
"test-feature",
FAKE_FEATURE_MANIFEST
);
const rollout = ExperimentFakes.rollout("foo", {
branch: {
slug: "slug",
features: [
{
featureId: featureInstance.featureId,
value: {
title: "hello",
enabled: true,
},
},
],
},
});
sandbox.stub(manager.store, "getAllRollouts").returns([rollout]);
await manager.onStartup();
featureInstance.onUpdate(stub);
await featureInstance.ready();
Assert.ok(stub.notCalled, "We register too late to catch any events");
setDefaultBranch(TEST_FALLBACK_PREF, JSON.stringify({ foo: true }));
Assert.deepEqual(
featureInstance.getVariable("config"),
{ foo: true },
"Feature is ready even when initialized after store update"
);
Assert.equal(
featureInstance.getVariable("title"),
"hello",
"Returns the NimbusTestUtils rollout default value"
);
});
|