-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathpush-flag-sets.spec.js
More file actions
268 lines (211 loc) · 9.84 KB
/
Copy pathpush-flag-sets.spec.js
File metadata and controls
268 lines (211 loc) · 9.84 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
import { SplitFactory } from '../..';
import EventSourceMock, { setMockListener } from '../testUtils/eventSourceMock';
import { __setEventSource } from '@splitsoftware/splitio-commons/src/platform/getEventSource/node';
import { mockSegmentChanges } from '../testUtils';
import notification1 from '../mocks/message.SPLIT_UPDATE.FS.1.json';
import notification2 from '../mocks/message.SPLIT_UPDATE.FS.2.json';
import notification3 from '../mocks/message.SPLIT_UPDATE.FS.3.json';
import notification4None from '../mocks/message.SPLIT_UPDATE.FS.4None.json';
import notification4 from '../mocks/message.SPLIT_UPDATE.FS.4.json';
import notification5 from '../mocks/message.SPLIT_UPDATE.FS.5.json';
import notification6 from '../mocks/message.SPLIT_UPDATE.FS.6.json';
import notificationKill from '../mocks/message.SPLIT_UPDATE.FS.kill.json';
import authPushEnabled from '../mocks/auth.pushEnabled.node.json';
const baseUrls = {
sdk: 'https://sdk.baseurl',
auth: 'https://auth.baseurl/api'
};
const baseConfig = {
core: {
authorizationKey: '<fake-token>',
},
urls: baseUrls,
};
const MILLIS_FIRST_SPLIT_UPDATE_EVENT = 100;
const MILLIS_SECOND_SPLIT_UPDATE_EVENT = 200;
const MILLIS_THIRD_SPLIT_UPDATE_EVENT = 300;
const MILLIS_FOURTH_SPLIT_UPDATE_EVENT = 400;
const MILLIS_FIFTH_SPLIT_UPDATE_EVENT = 500;
export function testFlagSets(fetchMock, t) {
fetchMock.reset();
__setEventSource(EventSourceMock);
mockSegmentChanges(fetchMock, new RegExp(baseUrls.sdk + '/segmentChanges/*'), ['some-key']);
fetchMock.post('*', 200);
fetchMock.get(baseUrls.auth + '/v2/auth?s=1.3', function (url, opts) {
if (!opts.headers['Authorization']) t.fail('`/v2/auth` request must include `Authorization` header');
t.pass('auth success');
return { status: 200, body: authPushEnabled };
});
fetchMock.get(baseUrls.sdk + '/splitChanges?s=1.3&since=-1&rbSince=-1', function () {
return { status: 200, body: { ff: { d: [], s: -1, t: 0 }}};
});
fetchMock.get(baseUrls.sdk + '/splitChanges?s=1.3&since=0&rbSince=-1', function () {
return { status: 200, body: { ff: { d: [], s: 0, t: 1 } }};
});
fetchMock.get(baseUrls.sdk + '/splitChanges?s=1.3&since=-1&rbSince=-1&sets=set_1,set_2', function () {
return { status: 200, body: { ff: { d: [], s: -1, t: 0 } }};
});
fetchMock.get(baseUrls.sdk + '/splitChanges?s=1.3&since=0&rbSince=-1&sets=set_1,set_2', function () {
return { status: 200, body: { ff: { d: [], s: 0, t: 1 } }};
});
const configWithSets = {
...baseConfig,
sync: {
splitFilters: [{type: 'bySet', values: ['set_1', 'set_2']}]
}
};
t.test(async (assert) => {
let splitio, client, manager = [];
setMockListener((eventSourceInstance) => {
eventSourceInstance.emitOpen();
setTimeout(() => {
assert.deepEqual(manager.splits(), [], '1 - initialized without flags');
client.once(client.Event.SDK_UPDATE, async () => {
assert.equal(manager.splits().length, 1, '1 - update is processed and the flag is stored');
await client.destroy();
assert.end();
});
eventSourceInstance.emitMessage(notification1);
}, MILLIS_FIRST_SPLIT_UPDATE_EVENT);
});
splitio = SplitFactory(baseConfig);
client = splitio.client();
manager = splitio.manager();
}, 'SDK with no sets configured does not exclude updates');
t.test(async (assert) => {
let splitio, client, manager = [];
setMockListener((eventSourceInstance) => {
eventSourceInstance.emitOpen();
setTimeout(() => {
assert.deepEqual(manager.splits(), [], '2 - initialized without flags');
// Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"]
client.once(client.Event.SDK_UPDATE, () => {
assert.deepEqual(manager.split('workm').sets, ['set_1', 'set_2'], '2 - update is processed and the flag is stored');
});
eventSourceInstance.emitMessage(notification2);
}, MILLIS_FIRST_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// Receive a SPLIT_UPDATE with "sets":["set_1"]
client.once(client.Event.SDK_UPDATE, () => {
assert.deepEqual(manager.split('workm').sets, ['set_1'], '2 - update is processed and the flag is updated');
});
eventSourceInstance.emitMessage(notification3);
}, MILLIS_SECOND_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// Receive a SPLIT_UPDATE with "sets":[]
client.once(client.Event.SDK_UPDATE, async () => {
assert.deepEqual(manager.splits().length, 0, '2 - update is processed and the flag is removed');
await client.destroy();
assert.end();
});
eventSourceInstance.emitMessage(notification4None);
}, MILLIS_THIRD_SPLIT_UPDATE_EVENT);
});
splitio = SplitFactory(configWithSets);
client = splitio.client();
manager = splitio.manager();
}, 'SDK with sets configured deletes flag when change with empty sets is received');
t.test(async (assert) => {
let splitio, client, manager = [];
setMockListener((eventSourceInstance) => {
eventSourceInstance.emitOpen();
setTimeout(() => {
assert.deepEqual(manager.splits(), [], '3 - initialized without flags');
// Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"]
client.once(client.Event.SDK_UPDATE, () => {
assert.deepEqual(manager.split('workm').sets, ['set_1', 'set_2'], '3 - update is processed and the flag is stored');
});
eventSourceInstance.emitMessage(notification2);
}, MILLIS_FIRST_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// Receive a SPLIT_UPDATE with "sets":["set_1"]
client.once(client.Event.SDK_UPDATE, () => {
assert.deepEqual(manager.split('workm').sets, ['set_1'], '3 - update is processed and the flag is updated');
});
eventSourceInstance.emitMessage(notification3);
}, MILLIS_SECOND_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// Receive a SPLIT_UPDATE with "sets":["set_3"]
client.once(client.Event.SDK_UPDATE, () => {
assert.deepEqual(manager.splits().length, 0, '3 - update is processed and the flag is removed');
});
eventSourceInstance.emitMessage(notification4);
}, MILLIS_THIRD_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// Receive a SPLIT_UPDATE with "sets":["set_3", "set_4"]
// should not emit SPLIT_UPDATE
// Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"] from next test notification
client.once(client.Event.SDK_UPDATE, async () => {
assert.deepEqual(manager.split('workm').sets, ['set_1', 'set_2'], '3 - update is processed and the flag is stored');
await client.destroy();
assert.end();
});
eventSourceInstance.emitMessage(notification5);
}, MILLIS_FOURTH_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// emit a SPLIT_UPDATE with "sets":["set_1", "set_2"]
eventSourceInstance.emitMessage(notification6);
}, MILLIS_FIFTH_SPLIT_UPDATE_EVENT);
});
splitio = SplitFactory(configWithSets);
client = splitio.client();
manager = splitio.manager();
}, 'SDK with sets configured deletes flag when change with non-matching sets is received');
t.test(async (assert) => {
let splitio, client, manager = [];
setMockListener((eventSourceInstance) => {
fetchMock.getOnce(baseUrls.sdk + '/splitChanges?s=1.3&since=2&rbSince=-1&sets=set_1,set_2', async function () {
assert.pass('4 - A fetch is triggered due to the SPLIT_KILL');
await client.destroy();
assert.end();
return { status: 200, body: { ff: { d: [], s: 2, t: 3 } }};
});
eventSourceInstance.emitOpen();
setTimeout(() => {
assert.deepEqual(manager.splits(), [], '4 - initialized without flags');
// Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"]
client.once(client.Event.SDK_UPDATE, () => {
assert.equal(manager.split('workm').killed, false, '4 - update is processed and the flag is stored');
});
eventSourceInstance.emitMessage(notification2);
}, MILLIS_FIRST_SPLIT_UPDATE_EVENT);
setTimeout(() => {
// Receive a SPLIT_KILL for flag
client.once(client.Event.SDK_UPDATE, async () => {
assert.equal(manager.split('workm').killed, true, '4 - update is processed and the flag is updated');
});
eventSourceInstance.emitMessage(notificationKill);
}, MILLIS_SECOND_SPLIT_UPDATE_EVENT);
});
splitio = SplitFactory(configWithSets);
client = splitio.client();
manager = splitio.manager();
await client.ready();
}, 'SDK with sets configured updates flag when a SPLIT_KILL is received');
t.test(async (assert) => {
fetchMock.getOnce(baseUrls.sdk + '/splitChanges?s=1.3&since=1&rbSince=-1&sets=set_1,set_2', function () {
assert.pass('5 - A fetch is triggered due to the SPLIT_KILL');
return { status: 200, body: { ff: { d: [], s: 1, t: 5 } }};
});
let splitio, client, manager = [];
setMockListener((eventSourceInstance) => {
eventSourceInstance.emitOpen();
setTimeout(() => {
assert.deepEqual(manager.splits(), [], '5 - initialized without flags');
// Receive a SPLIT_KILL for flag that does not belongs to configured sets
client.once(client.Event.SDK_UPDATE, () => {
assert.fail('5 - SDK_UPDATE should not be emitted for flags that are not in configured sets ');
});
eventSourceInstance.emitMessage(notificationKill);
}, MILLIS_FIRST_SPLIT_UPDATE_EVENT);
setTimeout(async () => {
await client.destroy();
assert.end();
}, MILLIS_SECOND_SPLIT_UPDATE_EVENT);
});
splitio = SplitFactory(configWithSets);
client = splitio.client();
manager = splitio.manager();
await client.ready();
}, 'SDK with sets configured does not update flag when a SPLIT_KILL is received for a non-existing flag');
}