@splitsoftware/openfeature-js-split-provider 1.0.7 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,109 +1,241 @@
1
- export default async function(assert) {
2
-
3
- const shouldFailWithBadApiKeyTest = () => {
4
- assert.equal(1, 1);
5
- };
6
-
7
- const evalBooleanNullEmptyTest = () => {
8
- assert.equal(2, 2);
9
- };
10
-
11
- const evalBooleanControlTest = () => {
12
- assert.equal(1, 1);
13
- };
14
-
15
- const evalBooleanTrueTest = () => {
16
- assert.equal(1, 1);
17
- };
18
-
19
- const evalBooleanOnTest = () => {
20
- assert.equal(1, 1);
21
- };
22
-
23
- const evalBooleanFalseTest = () => {
24
- assert.equal(1, 1);
25
- };
26
-
27
- const evalBooleanOffTest = () => {
28
- assert.equal(1, 1);
29
- };
30
-
31
- const evalBooleanErrorTest = () => {
32
- assert.equal(1, 1);
33
- };
34
-
35
- const evalStringNullEmptyTest = () => {
36
- assert.equal(1, 1);
37
- };
38
-
39
- const evalStringControlTest = () => {
40
- assert.equal(1, 1);
41
- };
42
-
43
- const evalStringRegularTest = () => {
44
- assert.equal(1, 1);
45
- };
46
-
47
- const evalNumberNullEmptyTest = () => {
48
- assert.equal(1, 1);
49
- };
50
-
51
- const evalNumberControlTest = () => {
52
- assert.equal(1, 1);
53
- };
54
-
55
- const evalNumberRegularTest = () => {
56
- assert.equal(1, 1);
57
- };
58
-
59
- const evalNumberErrorTest = () => {
60
- assert.equal(1, 1);
61
- };
62
-
63
- const evalStructureNullEmptyTest = () => {
64
- assert.equal(1, 1);
65
- };
66
-
67
- const evalStructureControlTest = () => {
68
- assert.equal(1, 1);
69
- };
70
-
71
- const evalStructureRegularTest = () => {
72
- assert.equal(1, 1);
73
- };
74
-
75
- const evalStructureComplexTest = () => {
76
- assert.equal(1, 1);
77
- };
78
-
79
- const evalStructureErrorTest = () => {
80
- assert.equal(1, 1);
81
- };
82
-
83
- shouldFailWithBadApiKeyTest();
84
-
85
- evalBooleanNullEmptyTest();
86
- evalBooleanControlTest();
87
- evalBooleanTrueTest();
88
- evalBooleanOnTest();
89
- evalBooleanFalseTest();
90
- evalBooleanOffTest();
91
- evalBooleanErrorTest();
92
-
93
- evalStringNullEmptyTest();
94
- evalStringControlTest();
95
- evalStringRegularTest();
96
-
97
- evalNumberNullEmptyTest();
98
- evalNumberControlTest();
99
- evalNumberRegularTest();
100
- evalNumberErrorTest();
101
-
102
- evalStructureNullEmptyTest();
103
- evalStructureControlTest();
104
- evalStructureRegularTest();
105
- evalStructureComplexTest();
106
- evalStructureErrorTest();
107
-
108
- assert.end();
109
- }
1
+ /* eslint-disable jest/no-conditional-expect */
2
+ import { getLocalHostSplitClient, getSplitFactory } from '../testUtils';
3
+ import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
4
+
5
+ const cases = [
6
+ [
7
+ 'provider tests mode: splitClient',
8
+ () => ({ splitClient: getLocalHostSplitClient()}),
9
+
10
+ ],
11
+ [
12
+ 'provider tests mode: splitFactory',
13
+ getSplitFactory
14
+ ],
15
+ ];
16
+
17
+ describe.each(cases)('%s', (label, getOptions) => {
18
+
19
+ let provider;
20
+ let options;
21
+
22
+ beforeEach(() => {
23
+ options = getOptions();
24
+ provider = new OpenFeatureSplitProvider(options);
25
+ });
26
+
27
+ afterEach(async () => {
28
+ jest.clearAllMocks()
29
+ await provider.onClose();
30
+ });
31
+
32
+ test('evaluate Boolean null/empty test', async () => {
33
+ try {
34
+ await provider.resolveBooleanEvaluation('', false, { targetingKey: 'user1' });
35
+ } catch (e) {
36
+ expect(e.message).toBe('flagKey must be a non-empty string');
37
+ expect(e.code).toBe('FLAG_NOT_FOUND');
38
+ }
39
+ });
40
+
41
+ test('evaluate Boolean control test', async () => {
42
+ try {
43
+ await provider.resolveBooleanEvaluation('non-existent-feature', false, { targetingKey: 'user1' });
44
+ } catch (e) {
45
+ expect(e.message).toBe('Received the "control" value from Split.');
46
+ expect(e.code).toBe('FLAG_NOT_FOUND');
47
+ }
48
+ });
49
+
50
+ test('evaluate Boolean true test', async () => {
51
+ const details = await provider.resolveBooleanEvaluation('my_feature', false, { targetingKey: 'key' });
52
+ expect(details.value).toBe(true);
53
+ expect(details.variant).toBe('on');
54
+ expect(details.reason).toBe('TARGETING_MATCH');
55
+ expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to ON treatment"}' });
56
+ });
57
+
58
+ test('evaluate Boolean on test', async () => {
59
+ const details = await provider.resolveBooleanEvaluation('my_feature', true, { targetingKey: 'key' });
60
+ expect(details.value).toBe(true);
61
+ expect(details.variant).toBe('on');
62
+ expect(details.reason).toBe('TARGETING_MATCH');
63
+ expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to ON treatment"}' });
64
+ });
65
+
66
+ test('evaluate Boolean false test', async () => {
67
+ const details = await provider.resolveBooleanEvaluation('some_other_feature', true, { targetingKey: 'user1' });
68
+ expect(details.value).toBe(false);
69
+ expect(details.variant).toBe('off');
70
+ expect(details.reason).toBe('TARGETING_MATCH');
71
+ expect(details.flagMetadata).toEqual({ config: '' });
72
+ });
73
+
74
+ test('evaluate Boolean off test', async () => {
75
+ const details = await provider.resolveBooleanEvaluation('some_other_feature', false, { targetingKey: 'user1' });
76
+ expect(details.value).toBe(false);
77
+ expect(details.variant).toBe('off');
78
+ expect(details.reason).toBe('TARGETING_MATCH');
79
+ expect(details.flagMetadata).toEqual({ config: '' });
80
+ });
81
+
82
+ test('evaluate Boolean error test', async () => {
83
+ try {
84
+ await provider.resolveBooleanEvaluation('int_feature', false, { targetingKey: 'user1' });
85
+ } catch (e) {
86
+ expect(e.message).toBe('Invalid boolean value for 32');
87
+ expect(e.code).toBe('PARSE_ERROR');
88
+ }
89
+ });
90
+
91
+ test('evaluate String null/empty test', async () => {
92
+ try {
93
+ await provider.resolveStringEvaluation('', 'default', { targetingKey: 'user1' });
94
+ } catch (e) {
95
+ expect(e.message).toBe('flagKey must be a non-empty string');
96
+ expect(e.code).toBe('FLAG_NOT_FOUND');
97
+ }
98
+ });
99
+
100
+ test('evaluate String control test', async () => {
101
+ try {
102
+ await provider.resolveStringEvaluation('non-existent-feature', 'default', { targetingKey: 'user1' });
103
+ } catch (e) {
104
+ expect(e.message).toBe('Received the "control" value from Split.');
105
+ expect(e.code).toBe('FLAG_NOT_FOUND');
106
+ }
107
+ });
108
+
109
+ test('evaluate String regular test', async () => {
110
+ try {
111
+ await provider.resolveStringEvaluation('string_feature', 'default', { targetingKey: 'user1' });
112
+ } catch (e) {
113
+ expect(e.message).toBe('Received the "control" value from Split.');
114
+ expect(e.code).toBe('FLAG_NOT_FOUND');
115
+ }
116
+ });
117
+
118
+ test('evaluate String error test', async () => {
119
+ try {
120
+ await provider.resolveStringEvaluation('int_feature', 'default', { targetingKey: 'user1' });
121
+ } catch (e) {
122
+ expect(e.message).toBe('Invalid string value for 32');
123
+ expect(e.code).toBe('PARSE_ERROR');
124
+ }
125
+ });
126
+
127
+ test('evaluate Number null/empty test', async () => {
128
+ try {
129
+ await provider.resolveNumberEvaluation('', 0, { targetingKey: 'user1' });
130
+ } catch (e) {
131
+ expect(e.message).toBe('flagKey must be a non-empty string');
132
+ expect(e.code).toBe('FLAG_NOT_FOUND');
133
+ }
134
+ });
135
+
136
+ test('evaluate Number control test', async () => {
137
+ try {
138
+ await provider.resolveNumberEvaluation('non-existent-feature', 0, { targetingKey: 'user1' });
139
+ } catch (e) {
140
+ expect(e.message).toBe('Received the "control" value from Split.');
141
+ expect(e.code).toBe('FLAG_NOT_FOUND');
142
+ }
143
+ });
144
+
145
+ test('evaluate Number regular test', async () => {
146
+ const details = await provider.resolveNumberEvaluation('int_feature', 0, { targetingKey: 'user1' });
147
+ expect(details.value).toBe(32);
148
+ expect(details.variant).toBe('32');
149
+ expect(details.reason).toBe('TARGETING_MATCH');
150
+ expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to number treatment"}' });
151
+ });
152
+
153
+ test('evaluate Number error test', async () => {
154
+ try {
155
+ await provider.resolveNumberEvaluation('my_feature', 0, { targetingKey: 'user1' });
156
+ } catch (e) {
157
+ expect(e.message).toBe('Invalid numeric value off');
158
+ expect(e.code).toBe('PARSE_ERROR');
159
+ }
160
+ });
161
+
162
+ test('evaluate Structure null/empty test', async () => {
163
+ try {
164
+ await provider.resolveObjectEvaluation('', {}, { targetingKey: 'user1' });
165
+ } catch (e) {
166
+ expect(e.message).toBe('flagKey must be a non-empty string');
167
+ expect(e.code).toBe('FLAG_NOT_FOUND');
168
+ }
169
+ });
170
+
171
+ test('evaluate Structure control test', async () => {
172
+ try {
173
+ await provider.resolveObjectEvaluation('non-existent-feature', {}, { targetingKey: 'user1' });
174
+ } catch (e) {
175
+ expect(e.message).toBe('Received the "control" value from Split.');
176
+ expect(e.code).toBe('FLAG_NOT_FOUND');
177
+ }
178
+ });
179
+
180
+ test('evaluate Structure regular test', async () => {
181
+ const details = await provider.resolveObjectEvaluation('obj_feature', {}, { targetingKey: 'user1' });
182
+ expect(details.value).toEqual({ key: 'value' });
183
+ expect(details.variant).toBe('{"key": "value"}');
184
+ expect(details.reason).toBe('TARGETING_MATCH');
185
+ expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to obj treatment"}' });
186
+ });
187
+
188
+ test('evaluate Structure error test', async () => {
189
+ try {
190
+ await provider.resolveObjectEvaluation('int_feature', {}, { targetingKey: 'user1' });
191
+ } catch (e) {
192
+ expect(e.message).toBe('Error parsing 32 as JSON, ParseError: Flag value 32 had unexpected type number, expected "object"');
193
+ expect(e.code).toBe('PARSE_ERROR');
194
+ }
195
+ });
196
+
197
+ test('track: throws when missing eventName', async () => {
198
+ try {
199
+ await provider.track('', { targetingKey: 'u1', trafficType: 'user' }, {});
200
+ } catch (e) {
201
+ expect(e.message).toBe('Missing eventName, required to track');
202
+ expect(e.code).toBe('PARSE_ERROR');
203
+ }
204
+ });
205
+
206
+ test('track: throws when missing trafficType', async () => {
207
+ try {
208
+ await provider.track('evt', { targetingKey: 'u1' }, {});
209
+ } catch (e) {
210
+ expect(e.message).toBe('Missing trafficType variable, required to track');
211
+ expect(e.code).toBe('INVALID_CONTEXT');
212
+ }
213
+ });
214
+
215
+ test('track: throws when missing targetingKey', async () => {
216
+ try {
217
+ await provider.track('evt', { trafficType: 'user' }, {});
218
+ } catch (e) {
219
+ expect(e.message).toBe('Missing targetingKey, required to track');
220
+ expect(e.code).toBe('TARGETING_KEY_MISSING');
221
+ }
222
+ });
223
+
224
+ test('track: ok without details', async () => {
225
+ const trackSpy = jest.spyOn(options.splitClient ? options.splitClient : options.client(), 'track');
226
+ await provider.track('view', { targetingKey: 'u1', trafficType: 'user' }, null);
227
+ expect(trackSpy).toHaveBeenCalledTimes(1);
228
+ expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'view', undefined, {});
229
+ });
230
+
231
+ test('track: ok with details', async () => {
232
+ const trackSpy = jest.spyOn(options.splitClient ? options.splitClient : options.client(), 'track');
233
+ await provider.track(
234
+ 'purchase',
235
+ { targetingKey: 'u1', trafficType: 'user' },
236
+ { value: 9.99, properties: { plan: 'pro', beta: true } }
237
+ );
238
+ expect(trackSpy).toHaveBeenCalledTimes(1);
239
+ expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'purchase', 9.99, { plan: 'pro', beta: true });
240
+ });
241
+ });
@@ -32,7 +32,6 @@ export default class EventSource {
32
32
  this.url = url;
33
33
  this.withCredentials = eventSourceInitDict.withCredentials;
34
34
  this.readyState = 0;
35
- // eslint-disable-next-line no-undef
36
35
  this.__emitter = new EventEmitter();
37
36
  this.__eventSourceInitDict = arguments[1];
38
37
  sources[url] = this;
@@ -82,4 +81,4 @@ export default class EventSource {
82
81
 
83
82
  EventSource.CONNECTING = 0;
84
83
  EventSource.OPEN = 1;
85
- EventSource.CLOSED = 2;
84
+ EventSource.CLOSED = 2;
@@ -1,3 +1,5 @@
1
+ import { SplitFactory } from '@splitsoftware/splitio';
2
+
1
3
  const DEFAULT_ERROR_MARGIN = 50; // 0.05 secs
2
4
 
3
5
  /**
@@ -67,3 +69,44 @@ export function url(settings, target) {
67
69
  }
68
70
  return `${settings.urls.sdk}${target}`;
69
71
  }
72
+
73
+ const getRedisConfig = (redisPort) => ({
74
+ core: {
75
+ authorizationKey: 'SOME SDK KEY' // in consumer mode, SDK key is only used to track and log warning regarding duplicated SDK instances
76
+ },
77
+ mode: 'consumer',
78
+ storage: {
79
+ type: 'REDIS',
80
+ prefix: 'REDIS_NODE_UT',
81
+ options: {
82
+ url: `redis://localhost:${redisPort}/0`
83
+ }
84
+ },
85
+ sync: {
86
+ impressionsMode: 'DEBUG'
87
+ },
88
+ startup: {
89
+ readyTimeout: 36000 // 10hs
90
+ }
91
+ });
92
+
93
+ const config = {
94
+ core: {
95
+ authorizationKey: 'localhost'
96
+ },
97
+ features: './split.yaml',
98
+ }
99
+ /**
100
+ * get a Split client in localhost mode for testing purposes
101
+ */
102
+ export function getLocalHostSplitClient() {
103
+ return SplitFactory(config).client();
104
+ }
105
+
106
+ export function getRedisSplitClient(redisPort) {
107
+ return SplitFactory(getRedisConfig(redisPort)).client();
108
+ }
109
+
110
+ export function getSplitFactory() {
111
+ return SplitFactory(config);
112
+ }