forked from splitio/javascript-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.ts
More file actions
359 lines (303 loc) · 12.9 KB
/
index.spec.ts
File metadata and controls
359 lines (303 loc) · 12.9 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
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
import _ from 'lodash';
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
import { ISettings } from '../../../types';
import { OPTIMIZED, DEBUG } from '../../constants';
// Test targets:
import { settingsValidation } from '../index';
import { url } from '../url';
const minimalSettingsParams = {
// based on browser defaults
defaults: {
startup: {
requestTimeoutBeforeReady: 5,
retriesOnFailureBeforeReady: 1,
readyTimeout: 10,
eventsFirstPushWindow: 10
},
version: 'javascript-test',
},
runtime: () => ({ ip: false, hostname: false } as ISettings['runtime']),
logger: () => (loggerMock as ISettings['log']),
consent: () => undefined
};
describe('settingsValidation', () => {
test('check defaults', () => {
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token'
}
}, minimalSettingsParams);
expect(settings.urls).toEqual({
sdk: 'https://sdk.split.io/api',
events: 'https://events.split.io/api',
auth: 'https://auth.split.io/api',
streaming: 'https://streaming.split.io',
telemetry: 'https://telemetry.split.io/api',
});
expect(settings.sync.impressionsMode).toBe(OPTIMIZED);
expect(settings.sync.enabled).toBe(true);
});
test('override with default impressionMode if provided one is invalid', () => {
const config = {
core: { authorizationKey: 'dummy token' },
sync: { impressionsMode: 'some' }
};
let settings = settingsValidation(config, minimalSettingsParams);
expect(settings.sync.impressionsMode).toBe(OPTIMIZED);
expect(settings.scheduler.impressionsRefreshRate).toBe(300000); // Default
settings = settingsValidation({ ...config, scheduler: { impressionsRefreshRate: 10 } }, minimalSettingsParams);
expect(settings.sync.impressionsMode).toBe(OPTIMIZED);
expect(settings.scheduler.impressionsRefreshRate).toBe(10000);
});
test('numConcurrentSegmentFetches should be configurable', () => {
const config = {
core: { authorizationKey: 'dummy token' },
sync: { numConcurrentSegmentFetches: 20 },
};
let settings = settingsValidation(config, minimalSettingsParams);
expect(settings.sync.numConcurrentSegmentFetches).toEqual(20);
settings = settingsValidation(
{ ...config, sync: {numConcurrentSegmentFetches: 'asdf'} },
minimalSettingsParams
);
expect(settings.sync.numConcurrentSegmentFetches).toEqual(10);
});
test('impressionsMode should be configurable', () => {
const config = {
core: { authorizationKey: 'dummy token' },
sync: { impressionsMode: DEBUG }
};
let settings = settingsValidation(config, minimalSettingsParams);
expect(settings.sync.impressionsMode).toEqual(DEBUG);
expect(settings.scheduler.impressionsRefreshRate).toBe(60000); // Different default for DEBUG impressionsMode
settings = settingsValidation({ ...config, scheduler: { impressionsRefreshRate: 10 } }, minimalSettingsParams);
expect(settings.sync.impressionsMode).toBe(DEBUG);
expect(settings.scheduler.impressionsRefreshRate).toBe(10000);
});
test('urls should be configurable', () => {
const urls = {
sdk: 'sdk-url',
events: 'events-url',
auth: 'auth-url',
streaming: 'streaming-url',
telemetry: 'telemetry-url',
};
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token'
},
urls
}, minimalSettingsParams);
expect(settings.urls).toEqual(urls);
});
test('required properties should be always present', () => {
const locatorAuthorizationKey = _.property('core.authorizationKey');
const locatorSchedulerFeaturesRefreshRate = _.property('scheduler.featuresRefreshRate');
const locatorSchedulerSegmentsRefreshRate = _.property('scheduler.segmentsRefreshRate');
const locatorSchedulerTelemetryRefreshRate = _.property('scheduler.telemetryRefreshRate');
const locatorSchedulerImpressionsRefreshRate = _.property('scheduler.impressionsRefreshRate');
const locatorSchedulerEventsPushRate = _.property('scheduler.eventsPushRate');
const locatorUrlsSDK = _.property('urls.sdk');
const locatorUrlsEvents = _.property('urls.events');
const locatorStartupRequestTimeoutBeforeReady = _.property('startup.requestTimeoutBeforeReady');
const locatorStartupRetriesOnFailureBeforeReady = _.property('startup.retriesOnFailureBeforeReady');
const locatorStartupReadyTimeout = _.property('startup.readyTimeout');
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token'
},
scheduler: {
featuresRefreshRate: undefined,
segmentsRefreshRate: undefined,
metricsRefreshRate: undefined,
impressionsRefreshRate: undefined
},
urls: {
sdk: undefined,
events: undefined
},
startup: {
requestTimeoutBeforeReady: undefined,
retriesOnFailureBeforeReady: undefined,
readyTimeout: undefined
}
}, minimalSettingsParams);
expect(locatorAuthorizationKey(settings) !== undefined).toBe(true); // authorizationKey should be present
expect(locatorSchedulerFeaturesRefreshRate(settings) !== undefined).toBe(true); // scheduler.featuresRefreshRate should be present
expect(locatorSchedulerSegmentsRefreshRate(settings) !== undefined).toBe(true); // scheduler.segmentsRefreshRate should be present
expect(locatorSchedulerTelemetryRefreshRate(settings)).toBe(3600 * 1000); // scheduler.telemetryRefreshRate should be present
expect(locatorSchedulerImpressionsRefreshRate(settings) !== undefined).toBe(true); // scheduler.impressionsRefreshRate should be present
expect(locatorSchedulerEventsPushRate(settings) !== undefined).toBe(true); // scheduler.eventsPushRate should be present
expect(locatorUrlsSDK(settings) !== undefined).toBe(true); // urls.sdk should be present
expect(locatorUrlsEvents(settings) !== undefined).toBe(true); // urls.events should be present
expect(locatorStartupRequestTimeoutBeforeReady(settings) !== undefined).toBe(true); // startup.requestTimeoutBeforeReady should be present
expect(locatorStartupRetriesOnFailureBeforeReady(settings) !== undefined).toBe(true); // startup.retriesOnFailureBeforeReady should be present
expect(locatorStartupReadyTimeout(settings) !== undefined).toBe(true); // startup.readyTimeout should be present
});
test('streamingEnabled should be overwritable and true by default', () => {
const settingsWithStreamingDisabled = settingsValidation({
core: {
authorizationKey: 'dummy token',
},
streamingEnabled: false
}, minimalSettingsParams);
const settingsWithStreamingEnabled = settingsValidation({
core: {
authorizationKey: 'dummy token'
}
}, minimalSettingsParams);
expect(settingsWithStreamingDisabled.streamingEnabled).toBe(false); // When creating a setting instance, it will have the provided value for streamingEnabled
expect(settingsWithStreamingEnabled.streamingEnabled).toBe(true); // If streamingEnabled is not provided, it will be true.
});
test('sync.enabled should be overwritable and true by default', () => {
const settingsWithSyncEnabled = settingsValidation({
core: {
authorizationKey: 'dummy token',
}
}, minimalSettingsParams);
const settingsWithSyncDisabled = settingsValidation({
core: {
authorizationKey: 'dummy token'
},
sync: {
enabled: false
}
}, minimalSettingsParams);
expect(settingsWithSyncDisabled.sync.enabled).toBe(false); // If sync.enabled is not provided, it will be true.
expect(settingsWithSyncEnabled.sync.enabled).toBe(true); // When creating a setting instance, it will have the provided value for sync.enabled
});
const storageMock = () => { };
const integrationsMock = [() => { }];
test('overwrites storage with the result of the given storage validator', () => {
const storageValidatorResult = () => { };
const storageValidatorMock = jest.fn(() => storageValidatorResult);
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token'
},
storage: storageMock,
integrations: integrationsMock
// @ts-ignore
}, { ...minimalSettingsParams, storage: storageValidatorMock });
expect(settings.integrations).toBe(integrationsMock);
expect(settings.storage).toBe(storageValidatorResult);
expect(storageValidatorMock).toBeCalledWith(settings);
});
test('overwrites integrations with the result of the given integrations validator', () => {
const integrationsValidatorResult = () => { };
const integrationsValidatorMock = jest.fn(() => integrationsValidatorResult);
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token'
},
storage: storageMock,
integrations: integrationsMock
// @ts-ignore
}, { ...minimalSettingsParams, integrations: integrationsValidatorMock });
expect(settings.storage).toBe(storageMock);
expect(settings.integrations).toBe(integrationsValidatorResult);
expect(integrationsValidatorMock).toBeCalledWith(settings);
});
test('ignores key in server-side', () => {
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token',
key: 'ignored'
}
}, minimalSettingsParams);
expect(settings.core.key).toBe(undefined);
});
test('validates and sanitizes key and traffic type in client-side', () => {
const clientSideValidationParams = { ...minimalSettingsParams, acceptKey: true, acceptTT: true };
const samples = [{
key: ' valid-key ', settingsKey: 'valid-key', // key string is trimmed
trafficType: 'VALID-TT', settingsTrafficType: 'valid-tt', // TT is converted to lowercase
}, {
key: undefined, settingsKey: false, // undefined key is not valid in client-side
trafficType: undefined, settingsTrafficType: undefined,
}, {
key: null, settingsKey: false,
trafficType: null, settingsTrafficType: false,
}, {
key: true, settingsKey: false,
trafficType: true, settingsTrafficType: false,
}, {
key: 1.5, settingsKey: '1.5', // finite number as key is parsed
trafficType: 100, settingsTrafficType: false,
}, {
key: { matchingKey: 100, bucketingKey: ' BUCK ' }, settingsKey: { matchingKey: '100', bucketingKey: 'BUCK' },
trafficType: {}, settingsTrafficType: false,
}];
samples.forEach(({ key, trafficType, settingsKey, settingsTrafficType }) => {
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token',
key,
trafficType
}
}, clientSideValidationParams);
expect(settings.core.key).toEqual(settingsKey);
expect(settings.core.trafficType).toEqual(settingsTrafficType);
});
});
test('validates and sanitizes key, while traffic type is ignored', () => {
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token',
key: true,
trafficType: true
}
}, { ...minimalSettingsParams, acceptKey: true });
expect(settings.core.key).toEqual(false); // key is validated
expect(settings.core.trafficType).toEqual(true); // traffic type is ignored
});
// Not implemented yet
// test('validate min values', () => {
// const settings = settingsValidation({
// scheduler: {
// telemetryRefreshRate: 0,
// impressionsRefreshRate: 'invalid',
// }
// }, minimalSettingsParams);
// expect(settings.scheduler.telemetryRefreshRate).toBe(60000);
// expect(settings.scheduler.impressionsRefreshRate).toBe(60000);
// });
});
test('SETTINGS / urls should be correctly assigned', () => {
const settings = settingsValidation({
core: {
authorizationKey: 'dummy token'
}
}, minimalSettingsParams);
const baseSdkUrl = 'https://sdk.split.io/api';
const baseEventsUrl = 'https://events.split.io/api';
[
'/mySegments/nico',
'/mySegments/events@split',
'/mySegments/metrics@split',
'/mySegments/testImpressions@split',
'/mySegments/testImpressions',
'/mySegments/events',
'/mySegments/metrics',
'/splitChanges?since=-1',
'/splitChanges?since=100',
'/segmentChanges/segment1?since=100',
'/segmentChanges/events?since=100',
'/segmentChanges/beacon?since=100',
'/segmentChanges/metrics?since=100',
'/segmentChanges/testImpressions?since=100'
].forEach(relativeUrl => {
expect(url(settings, relativeUrl)).toBe(`${baseSdkUrl}${relativeUrl}`); // Our settings URL function should use ${baseSdkUrl} as base for ${relativeUrl}
});
[
'/metrics/times',
'/metrics/counters',
'/events/bulk',
'/events/beacon',
'/testImpressions/bulk',
'/testImpressions/beacon',
'/testImpressions/count/beacon'
].forEach(relativeUrl => {
expect(url(settings, relativeUrl)).toBe(`${baseEventsUrl}${relativeUrl}`); // Our settings URL function should use ${baseEventsUrl} as base for ${relativeUrl}
});
});