-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluations-fallback.spec.js
More file actions
317 lines (227 loc) · 11.3 KB
/
evaluations-fallback.spec.js
File metadata and controls
317 lines (227 loc) · 11.3 KB
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
import sinon from 'sinon';
import { SplitFactory } from '../../';
const listener = {
logImpression: sinon.stub()
};
export default function (configInMemory, configInLocalStorage, fetchMock, assert) {
assert.test('FallbackTreatment / Split factory with no fallbackTreatment defined', async t => {
const splitio = SplitFactory(configInMemory);
const client = splitio.client();
await client.whenReady();
t.equal(client.getTreatment('non_existent_flag'), 'control', 'The evaluation will return `control` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag_2'), 'control', 'The evaluation will return `control` if the flag does not exist and no fallbackTreatment is defined');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / Split factory with global fallbackTreatment defined', async t => {
const config = {
...configInMemory,
fallbackTreatments: {
global: 'FALLBACK_TREATMENT'
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.equal(client.getTreatment('non_existent_flag'), 'FALLBACK_TREATMENT', 'The evaluation will return `FALLBACK_TREATMENT` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag_2'), 'FALLBACK_TREATMENT', 'The evaluation will return `FALLBACK_TREATMENT` if the flag does not exist and no fallbackTreatment is defined');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / Split factory with specific fallbackTreatment defined', async t => {
const config = {
...configInMemory,
fallbackTreatments: {
byFlag: {
'non_existent_flag': 'FALLBACK_TREATMENT',
}
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.equal(client.getTreatment('non_existent_flag'), 'FALLBACK_TREATMENT', 'The evaluation will return `FALLBACK_TREATMENT` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag_2'), 'control', 'The evaluation will return `control` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag'), 'FALLBACK_TREATMENT', 'The evaluation will return `FALLBACK_TREATMENT` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag_2'), 'control', 'The evaluation will return `control` if the flag does not exist and no fallbackTreatment is defined');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / flag override beats global fallbackTreatment', async t => {
const config = {
...configInMemory,
fallbackTreatments: {
global: 'OFF_FALLBACK',
byFlag: {
'my_flag': 'ON_FALLBACK',
}
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.equal(client.getTreatment('my_flag'), 'ON_FALLBACK', 'The evaluation will return `ON_FALLBACK` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag_2'), 'OFF_FALLBACK', 'The evaluation will return `OFF_FALLBACK` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('my_flag'), 'ON_FALLBACK', 'The evaluation will return `ON_FALLBACK` if the flag does not exist and no fallbackTreatment is defined');
t.equal(client.getTreatment('non_existent_flag_2'), 'OFF_FALLBACK', 'The evaluation will return `OFF_FALLBACK` if the flag does not exist and no fallbackTreatment is defined');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / override applies only when original is control', async t => {
const config = {
...configInMemory,
fallbackTreatments: {
global: 'OFF_FALLBACK'
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.equal(client.getTreatment('user_account_in_whitelist'), 'off', 'The evaluation will return the treatment defined in the flag if it exists');
t.equal(client.getTreatment('non_existent_flag'), 'OFF_FALLBACK', 'The evaluation will return `OFF_FALLBACK` if the flag does not exist and no fallbackTreatment is defined');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / override applies only when original is control - inLocalStorage', async t => {
const config = {
...configInLocalStorage,
fallbackTreatments: {
global: 'OFF_FALLBACK'
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.equal(client.getTreatment('user_account_in_whitelist'), 'off', 'The evaluation will return the treatment defined in the flag if it exists');
t.equal(client.getTreatment('non_existent_flag'), 'OFF_FALLBACK', 'The evaluation will return `OFF_FALLBACK` if the flag does not exist and no fallbackTreatment is defined');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / Impressions correctness with fallback when client is not ready', async t => {
const config = {
...configInMemory,
urls: {
events: 'https://events.fallbacktreatment/api'
},
fallbackTreatments: {
byFlag: {
'any_flag': 'OFF_FALLBACK'
}
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
t.equal(client.getTreatment('any_flag'), 'OFF_FALLBACK', 'The evaluation will return the fallbackTreatment if the client is not ready yet');
t.equal(client.getTreatment('user_account_in_whitelist'), 'control', 'The evaluation will return the fallbackTreatment if the client is not ready yet');
await client.whenReady();
fetchMock.postOnce(config.urls.events + '/testImpressions/bulk', (_, opts) => {
const payload = JSON.parse(opts.body);
function validateImpressionData(featureFlagName, expectedLabel) {
const impressions = payload.find(e => e.f === featureFlagName).i;
t.equal(impressions[0].r, expectedLabel, `${featureFlagName} impression with label ${expectedLabel}`);
}
validateImpressionData('any_flag', 'fallback - not ready');
validateImpressionData('user_account_in_whitelist', 'not ready');
t.end();
return 200;
});
await client.destroy();
});
assert.test('FallbackTreatment / Fallback dynamic config propagation', async t => {
const config = {
...configInMemory,
fallbackTreatments: {
global: { treatment: 'OFF_FALLBACK', config: '{"global": true}' },
byFlag: {
'my_flag': { treatment: 'ON_FALLBACK', config: '{"flag": true}' }
}
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.deepEqual(client.getTreatmentWithConfig('my_flag'), { treatment: 'ON_FALLBACK', config: '{"flag": true}' }, 'The evaluation will propagate the config along with the treatment from the fallbackTreatment');
t.deepEqual(client.getTreatmentWithConfig('non_existent_flag'), { treatment: 'OFF_FALLBACK', config: '{"global": true}' }, 'The evaluation will propagate the config along with the treatment from the fallbackTreatment');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / Fallback dynamic config propagation - inLocalStorage', async t => {
const config = {
...configInLocalStorage,
fallbackTreatments: {
global: { treatment: 'OFF_FALLBACK', config: '{"global": true}' },
byFlag: {
'my_flag': { treatment: 'ON_FALLBACK', config: '{"flag": true}' }
}
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.deepEqual(client.getTreatmentWithConfig('my_flag'), { treatment: 'ON_FALLBACK', config: '{"flag": true}' }, 'The evaluation will propagate the config along with the treatment from the fallbackTreatment');
t.deepEqual(client.getTreatmentWithConfig('non_existent_flag'), { treatment: 'OFF_FALLBACK', config: '{"global": true}' }, 'The evaluation will propagate the config along with the treatment from the fallbackTreatment');
await client.destroy();
t.end();
});
assert.test('FallbackTreatment / Evaluations non existing flags with fallback do not generate impressions', async t => {
const config = {
...configInMemory,
urls: {
events: 'https://events.fallbacktreatment/api'
},
fallbackTreatments: {
global: { treatment: 'OFF_FALLBACK', config: '{"global": true}' },
byFlag: {
'my_flag': { treatment: 'ON_FALLBACK', config: '{"flag": true}' }
}
}
};
config.impressionListener = listener;
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.deepEqual(client.getTreatmentWithConfig('my_flag'), { treatment: 'ON_FALLBACK', config: '{"flag": true}' }, 'The evaluation will propagate the config along with the treatment from the fallbackTreatment');
t.deepEqual(client.getTreatmentWithConfig('non_existent_flag'), { treatment: 'OFF_FALLBACK', config: '{"global": true}' }, 'The evaluation will propagate the config along with the treatment from the fallbackTreatment');
let POSTED_IMPRESSIONS_COUNT = 0;
fetchMock.postOnce(config.urls.events + '/testImpressions/bulk', (_, opts) => {
const payload = JSON.parse(opts.body);
t.equal(payload.length, 1, 'We should have just one impression for the two evaluated flags');
function validateImpressionData(featureFlagName, expectedLength) {
const impressions = payload.find(e => e.f === featureFlagName).i;
t.equal(impressions.length, expectedLength, `${featureFlagName} has ${expectedLength} impressions`);
}
validateImpressionData('my_flag', 1);
validateImpressionData('non_existent_flag', 0);
POSTED_IMPRESSIONS_COUNT = payload.reduce((acc, curr) => acc + curr.i.length, 0);
t.equal(POSTED_IMPRESSIONS_COUNT, 1, 'We should have just one impression in total.');
return 200;
});
setTimeout(() => {
t.equal(listener.logImpression.callCount, POSTED_IMPRESSIONS_COUNT, 'Impression listener should be called once per each impression generated.');
t.end();
}, 0);
await client.destroy();
});
assert.test('FallbackTreatment / LocalhostMode', async t => {
const config = {
...configInMemory,
core: {
...configInMemory.core,
authorizationKey: 'localhost',
},
fallbackTreatments: {
global: 'OFF_FALLBACK'
},
features: {
testing_split: 'on',
}
};
const splitio = SplitFactory(config);
const client = splitio.client();
await client.whenReady();
t.deepEqual(client.getTreatment('testing_split'), 'on', 'The evaluation should return the treatment defined in localhost mode');
t.deepEqual(client.getTreatment('non_existent_flag'), 'OFF_FALLBACK', 'The evaluation will return `OFF_FALLBACK` if the flag does not exist');
await client.destroy();
t.end();
});
}