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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
Cu.importGlobalProperties(["fetch"]);
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
_ExperimentManager:
"resource://messaging-system/experiments/ExperimentManager.jsm",
ExperimentStore:
"resource://messaging-system/experiments/ExperimentStore.jsm",
NormandyUtils: "resource://normandy/lib/NormandyUtils.jsm",
FileTestUtils: "resource://testing-common/FileTestUtils.jsm",
_RemoteSettingsExperimentLoader:
"resource://messaging-system/lib/RemoteSettingsExperimentLoader.jsm",
Ajv: "resource://testing-common/ajv-4.1.1.js",
});
const PATH = FileTestUtils.getTempFile("shared-data-map").path;
XPCOMUtils.defineLazyGetter(this, "fetchExperimentSchema", async () => {
const response = await fetch(
"resource://testing-common/NimbusExperiment.schema.json"
);
const schema = await response.json();
if (!schema) {
throw new Error("Failed to load NimbusSchema");
}
return schema.definitions.NimbusExperiment;
});
const EXPORTED_SYMBOLS = ["ExperimentTestUtils", "ExperimentFakes"];
const ExperimentTestUtils = {
/**
* Checks if an experiment is valid acording to existing schema
* @param {NimbusExperiment} experiment
*/
async validateExperiment(experiment) {
const schema = await fetchExperimentSchema;
const ajv = new Ajv({ async: "co*", allErrors: true });
const validator = ajv.compile(schema);
validator(experiment);
if (validator.errors?.length) {
throw new Error(
"Experiment not valid:" + JSON.stringify(validator.errors, undefined, 2)
);
}
return experiment;
},
};
const ExperimentFakes = {
manager(store) {
return new _ExperimentManager({ store: store || this.store() });
},
store() {
return new ExperimentStore("FakeStore", { path: PATH, isParent: true });
},
waitForExperimentUpdate(ExperimentAPI, options) {
if (!options) {
throw new Error("Must specify an expected recipe update");
}
return new Promise(resolve => ExperimentAPI.on("update", options, resolve));
},
childStore() {
return new ExperimentStore("FakeStore", { isParent: false });
},
rsLoader() {
const loader = new _RemoteSettingsExperimentLoader();
// Replace RS client with a fake
Object.defineProperty(loader, "remoteSettingsClient", {
value: { get: () => Promise.resolve([]) },
});
// Replace xman with a fake
loader.manager = this.manager();
return loader;
},
experiment(slug, props = {}) {
return {
slug,
active: true,
enrollmentId: NormandyUtils.generateUuid(),
branch: {
slug: "treatment",
feature: {
featureId: "aboutwelcome",
enabled: true,
value: { title: "hello" },
},
...props,
},
source: "test",
isEnrollmentPaused: true,
...props,
};
},
recipe(slug = NormandyUtils.generateUuid(), props = {}) {
return {
// This field is required for populating remote settings
id: NormandyUtils.generateUuid(),
slug,
isEnrollmentPaused: false,
probeSets: [],
startDate: null,
endDate: null,
proposedEnrollment: 7,
referenceBranch: "control",
application: "firefox-desktop",
branches: [
{
slug: "control",
ratio: 1,
feature: { featureId: "aboutwelcome", enabled: true, value: null },
},
{
slug: "treatment",
ratio: 1,
feature: {
featureId: "aboutwelcome",
enabled: true,
value: { title: "hello" },
},
},
],
bucketConfig: {
namespace: "mstest-utils",
randomizationUnit: "normandy_id",
start: 0,
count: 100,
total: 1000,
},
userFacingName: "Messaging System recipe",
userFacingDescription: "Messaging System MSTestUtils recipe",
...props,
};
},
};
|