@splitsoftware/openfeature-web-split-provider 1.0.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.
- package/CHANGES.txt +3 -0
- package/CONTRIBUTORS-GUIDE.md +28 -0
- package/LICENSE +13 -0
- package/README.md +137 -0
- package/es/index.js +1 -0
- package/es/lib/js-split-provider.js +197 -0
- package/lib/index.js +4 -0
- package/lib/lib/js-split-provider.js +200 -0
- package/package.json +78 -0
- package/src/__tests__/integration/debug.test.js +79 -0
- package/src/__tests__/integration/e2e-events.test.js +202 -0
- package/src/__tests__/integration/e2e.test.js +155 -0
- package/src/__tests__/integration/mock.test.js +99 -0
- package/src/__tests__/integration/working.test.js +76 -0
- package/src/__tests__/mocks/memberships.emiliano@split.io.json +15 -0
- package/src/__tests__/mocks/memberships.emmanuel@split.io.json +9 -0
- package/src/__tests__/mocks/memberships.nicolas@split.io.json +9 -0
- package/src/__tests__/mocks/splitchanges.since.-1.json +1732 -0
- package/src/__tests__/provider.spec.js +163 -0
- package/src/__tests__/webSuites/client.spec.js +210 -0
- package/src/index.ts +1 -0
- package/src/lib/js-split-provider.ts +271 -0
- package/types/index.d.ts +1 -0
- package/types/lib/js-split-provider.d.ts +25 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { OpenFeature } from '@openfeature/web-sdk';
|
|
2
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
3
|
+
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
4
|
+
|
|
5
|
+
describe('OpenFeature Split Provider - Debug Tests', () => {
|
|
6
|
+
let client;
|
|
7
|
+
let splitClient;
|
|
8
|
+
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
// Create with more debug options
|
|
11
|
+
const splitFactory = SplitFactory({
|
|
12
|
+
core: {
|
|
13
|
+
authorizationKey: 'localhost',
|
|
14
|
+
},
|
|
15
|
+
// Define the features directly
|
|
16
|
+
features: {
|
|
17
|
+
'my_feature': 'on',
|
|
18
|
+
'some_other_feature': 'off',
|
|
19
|
+
'int_feature': '32',
|
|
20
|
+
'obj_feature': '{"key": "value"}'
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
splitClient =splitFactory.client();
|
|
24
|
+
|
|
25
|
+
// Add direct Split client test to verify it works as expected
|
|
26
|
+
console.log('Direct Split client test:');
|
|
27
|
+
console.log('- my_feature:', splitClient.getTreatment('my_feature'));
|
|
28
|
+
console.log('- some_other_feature:', splitClient.getTreatment('some_other_feature'));
|
|
29
|
+
console.log('- int_feature:', splitClient.getTreatment('int_feature'));
|
|
30
|
+
console.log('- obj_feature:', splitClient.getTreatment('obj_feature'));
|
|
31
|
+
|
|
32
|
+
// Create provider
|
|
33
|
+
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
34
|
+
|
|
35
|
+
// Register provider
|
|
36
|
+
OpenFeature.setProvider(provider);
|
|
37
|
+
|
|
38
|
+
// Get client
|
|
39
|
+
client = OpenFeature.getClient('debug-test');
|
|
40
|
+
|
|
41
|
+
// Context we'll use in tests
|
|
42
|
+
const context = { targetingKey: 'test-user' };
|
|
43
|
+
|
|
44
|
+
// Test OpenFeature client directly in setup to debug issues
|
|
45
|
+
try {
|
|
46
|
+
const boolResult = await client.getBooleanValue('my_feature', false, context);
|
|
47
|
+
console.log('OpenFeature boolean test:', boolResult);
|
|
48
|
+
|
|
49
|
+
const stringResult = await client.getStringValue('some_other_feature', 'default', context);
|
|
50
|
+
console.log('OpenFeature string test:', stringResult);
|
|
51
|
+
|
|
52
|
+
const numberResult = await client.getNumberValue('int_feature', 0, context);
|
|
53
|
+
console.log('OpenFeature number test:', numberResult);
|
|
54
|
+
|
|
55
|
+
const objectResult = await client.getObjectValue('obj_feature', {}, context);
|
|
56
|
+
console.log('OpenFeature object test:', objectResult);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.error('Error in OpenFeature test:', e);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
afterAll(async () => {
|
|
63
|
+
if (splitClient) {
|
|
64
|
+
await splitClient.destroy();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Simple test to verify we can run a test at all
|
|
69
|
+
test('should create client and provider', () => {
|
|
70
|
+
expect(client).toBeDefined();
|
|
71
|
+
expect(splitClient).toBeDefined();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Basic test to validate Split direct API
|
|
75
|
+
test('Split client works directly', () => {
|
|
76
|
+
const treatment = splitClient.getTreatment('my_feature');
|
|
77
|
+
expect(treatment).toBe('on');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
|
|
2
|
+
import { SplitFactory, InLocalStorage } from '@splitsoftware/splitio-browserjs';
|
|
3
|
+
import fetchMock from 'jest-fetch-mock';
|
|
4
|
+
|
|
5
|
+
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
6
|
+
|
|
7
|
+
import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
|
|
8
|
+
import membershipsEmmanuel from '../mocks/memberships.emmanuel@split.io.json';
|
|
9
|
+
import membershipsEmiliano from '../mocks/memberships.emiliano@split.io.json';
|
|
10
|
+
|
|
11
|
+
// This is an end-to-end integration test that uses real clients (no mocks)
|
|
12
|
+
describe('OpenFeature Split Provider - E2E Integration Tests', () => {
|
|
13
|
+
const baseConfig = {
|
|
14
|
+
core: {
|
|
15
|
+
authorizationKey: 'key',
|
|
16
|
+
key: 'emmanuel@split.io'
|
|
17
|
+
},
|
|
18
|
+
scheduler: {
|
|
19
|
+
featuresRefreshRate: 3000,
|
|
20
|
+
segmentsRefreshRate: 3000,
|
|
21
|
+
impressionsRefreshRate: 3000
|
|
22
|
+
},
|
|
23
|
+
startup: {
|
|
24
|
+
requestTimeoutBeforeReady: 10,
|
|
25
|
+
eventsFirstPushWindow: 3000
|
|
26
|
+
},
|
|
27
|
+
urls: {
|
|
28
|
+
sdk: 'https://sdk.baseurl/readinessSuite1',
|
|
29
|
+
events: 'https://events.baseurl/readinessSuite1'
|
|
30
|
+
},
|
|
31
|
+
streamingEnabled: false,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Set up before all tests
|
|
35
|
+
beforeAll(async () => {
|
|
36
|
+
fetchMock.enableMocks();
|
|
37
|
+
fetchMock.mockIf(() => true, async req => {
|
|
38
|
+
if (req.url.includes('/splitChanges')) return { status: 200, body: JSON.stringify(splitChangesMock1) };
|
|
39
|
+
if (req.url.includes('/memberships/emmanuel')) return { status: 200, body: JSON.stringify(membershipsEmmanuel) };
|
|
40
|
+
if (req.url.includes('/testImpressions')) return { status: 200 };
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Clean up after tests
|
|
46
|
+
afterEach(async () => {
|
|
47
|
+
await OpenFeature.clearProviders();
|
|
48
|
+
await OpenFeature.clearHandlers();
|
|
49
|
+
await OpenFeature.close();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('Readiness events', () => {
|
|
53
|
+
|
|
54
|
+
test('should emit Stale event when split sdk is ready from cache', async () => {
|
|
55
|
+
const config = {
|
|
56
|
+
...baseConfig,
|
|
57
|
+
storage: InLocalStorage({
|
|
58
|
+
prefix: 'readyFromCache_1'
|
|
59
|
+
}),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const splitFactory = SplitFactory(config);
|
|
63
|
+
const client = OpenFeature.getClient('readyFromCache');
|
|
64
|
+
await new Promise((resolve, reject) => {
|
|
65
|
+
client.addHandler(ProviderEvents.Stale, () => {
|
|
66
|
+
const splitClient = splitFactory.client();
|
|
67
|
+
// should have emitted ready from cache
|
|
68
|
+
try {
|
|
69
|
+
expect(splitClient.__getStatus().isReadyFromCache).toBe(true);
|
|
70
|
+
resolve();
|
|
71
|
+
} catch {
|
|
72
|
+
reject('should be ready');
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
77
|
+
OpenFeature.setProvider(provider);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('should emit Ready event when split sdk is ready', async () => {
|
|
82
|
+
const splitFactory = SplitFactory(baseConfig);
|
|
83
|
+
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
84
|
+
OpenFeature.setProvider(provider);
|
|
85
|
+
const client = OpenFeature.getClient();
|
|
86
|
+
await new Promise((resolve, reject) => {
|
|
87
|
+
client.addHandler(ProviderEvents.Stale, () => {
|
|
88
|
+
reject('should not emit stale');
|
|
89
|
+
});
|
|
90
|
+
client.addHandler(ProviderEvents.Ready, () => {
|
|
91
|
+
const splitClient = splitFactory.client();
|
|
92
|
+
try {
|
|
93
|
+
// should have emitted ready
|
|
94
|
+
expect(splitClient.__getStatus().isReady).toBe(true);
|
|
95
|
+
resolve();
|
|
96
|
+
} catch {
|
|
97
|
+
reject('should be ready');
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('should emit ready after timeout', async () => {
|
|
104
|
+
fetchMock.mockIf(() => true, async req => {
|
|
105
|
+
if (req.url.includes('/splitChanges')) return { status: 200, body: JSON.stringify(splitChangesMock1) };
|
|
106
|
+
if (req.url.includes('/memberships/emmanuel')) return { status: 200, body: JSON.stringify(membershipsEmmanuel) };
|
|
107
|
+
if (req.url.includes('/testImpressions')) return { status: 200 };
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const config = {
|
|
111
|
+
...baseConfig,
|
|
112
|
+
scheduler: {
|
|
113
|
+
featuresRefreshRate: 3,
|
|
114
|
+
segmentsRefreshRate: 0.25,
|
|
115
|
+
},
|
|
116
|
+
startup: {
|
|
117
|
+
readyTimeout: 0.2,
|
|
118
|
+
requestTimeoutBeforeReady: 0.1,
|
|
119
|
+
retriesOnFailureBeforeReady: 10
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
const splitFactory = SplitFactory(config);
|
|
123
|
+
const client = OpenFeature.getClient();
|
|
124
|
+
|
|
125
|
+
await new Promise((resolve, reject) => {
|
|
126
|
+
|
|
127
|
+
client.addHandler(ProviderEvents.Stale, () => {
|
|
128
|
+
reject('should not emit stale');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
client.addHandler(ProviderEvents.Ready, () => {
|
|
132
|
+
|
|
133
|
+
// initialize a new client and make it time out
|
|
134
|
+
OpenFeature.setContext({targetingKey: 'emiliano@split.io'});
|
|
135
|
+
client.addHandler(ProviderEvents.Error, () => {
|
|
136
|
+
|
|
137
|
+
// new client timed out, when ready should emit Configuration Changed event
|
|
138
|
+
client.addHandler(ProviderEvents.ConfigurationChanged, async () => {
|
|
139
|
+
const details = await client.getStringDetails('developers', 'default');
|
|
140
|
+
expect(details.value).toBe('on');
|
|
141
|
+
expect(details.flagKey).toBe('developers');
|
|
142
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
143
|
+
expect(details.variant).toBe('on');
|
|
144
|
+
expect(details.flagMetadata.config).toBe('{"color":"blue"}');
|
|
145
|
+
resolve();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// add timed out client membership to get it ready
|
|
149
|
+
fetchMock.mockIf(() => true, async req => {
|
|
150
|
+
if (req.url.includes('/splitChanges')) return { status: 200, body: JSON.stringify(splitChangesMock1) };
|
|
151
|
+
if (req.url.includes('/memberships/emmanuel')) return { status: 200, body: JSON.stringify(membershipsEmmanuel) };
|
|
152
|
+
if (req.url.includes('/memberships/emiliano')) return { status: 200, body: JSON.stringify(membershipsEmiliano) };
|
|
153
|
+
if (req.url.includes('/testImpressions')) return { status: 200 };
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
159
|
+
OpenFeature.setProvider(provider);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('should emit Error event when split sdk timed out', async () => {
|
|
164
|
+
// disable fetch mocks to force time out
|
|
165
|
+
fetchMock.disableMocks();
|
|
166
|
+
|
|
167
|
+
const config = {
|
|
168
|
+
...baseConfig,
|
|
169
|
+
startup: {
|
|
170
|
+
readyTimeout: 1,
|
|
171
|
+
requestTimeoutBeforeReady: 1,
|
|
172
|
+
retriesOnFailureBeforeReady: 1
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
const splitFactory = SplitFactory(config);
|
|
176
|
+
const client = OpenFeature.getClient('timedOut');
|
|
177
|
+
|
|
178
|
+
await new Promise((resolve, reject) => {
|
|
179
|
+
client.addHandler(ProviderEvents.Stale, () => {
|
|
180
|
+
reject('should not emit stale');
|
|
181
|
+
});
|
|
182
|
+
client.addHandler(ProviderEvents.Ready, () => {
|
|
183
|
+
reject('should not emit ready');
|
|
184
|
+
});
|
|
185
|
+
client.addHandler(ProviderEvents.Error, () => {
|
|
186
|
+
const splitClient = splitFactory.client();
|
|
187
|
+
// should have emitted ready from cache
|
|
188
|
+
const clientStatus = splitClient.__getStatus();
|
|
189
|
+
try {
|
|
190
|
+
expect(clientStatus.hasTimedout && !clientStatus.isReady).toBe(true);
|
|
191
|
+
resolve();
|
|
192
|
+
} catch {
|
|
193
|
+
reject('should time out');
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
198
|
+
OpenFeature.setProvider(provider);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
|
|
2
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
3
|
+
import fetchMock from 'jest-fetch-mock';
|
|
4
|
+
|
|
5
|
+
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
6
|
+
|
|
7
|
+
import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
|
|
8
|
+
import membershipsEmmanuel from '../mocks/memberships.emmanuel@split.io.json';
|
|
9
|
+
import membershipsEmiliano from '../mocks/memberships.emiliano@split.io.json';
|
|
10
|
+
import membershipsNicolas from '../mocks/memberships.nicolas@split.io.json';
|
|
11
|
+
|
|
12
|
+
// This is an end-to-end integration test that uses real clients (no mocks)
|
|
13
|
+
describe('OpenFeature Split Provider - E2E Integration Tests', () => {
|
|
14
|
+
let client;
|
|
15
|
+
|
|
16
|
+
// Set up before all tests
|
|
17
|
+
beforeAll(async () => {
|
|
18
|
+
fetchMock.enableMocks();
|
|
19
|
+
fetchMock.mockIf(() => true, async req => {
|
|
20
|
+
if (req.url.includes('/splitChanges')) return { status: 200, body: JSON.stringify(splitChangesMock1) };
|
|
21
|
+
if (req.url.includes('/memberships/emmanuel')) return { status: 200, body: JSON.stringify(membershipsEmmanuel) };
|
|
22
|
+
if (req.url.includes('/memberships/emiliano')) return { status: 200, body: JSON.stringify(membershipsEmiliano) };
|
|
23
|
+
if (req.url.includes('/memberships/nicolas')) return { status: 200, body: JSON.stringify(membershipsNicolas) };
|
|
24
|
+
if (req.url.includes('/testImpressions')) return { status: 200 };
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const config = {
|
|
28
|
+
core: {
|
|
29
|
+
authorizationKey: 'key',
|
|
30
|
+
key:'emmanuel@split.io'
|
|
31
|
+
},
|
|
32
|
+
urls: {
|
|
33
|
+
sdk: 'https://sdk.baseurl/readinessSuite1',
|
|
34
|
+
events: 'https://events.baseurl/readinessSuite1'
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const splitFactory = SplitFactory(config);
|
|
39
|
+
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
40
|
+
await OpenFeature.setProviderAndWait(provider);
|
|
41
|
+
|
|
42
|
+
client = OpenFeature.getClient('integration-test');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Clean up after all tests
|
|
46
|
+
afterAll(async () => {
|
|
47
|
+
await OpenFeature.close();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('Readiness events', () => {
|
|
51
|
+
test('should emit openfeature client ready', async () => {
|
|
52
|
+
await new Promise((resolve) => {
|
|
53
|
+
client.addHandler(ProviderEvents.Ready, (eventDetails) => {
|
|
54
|
+
expect(eventDetails).toEqual({ clientName: 'integration-test', domain: 'integration-test', providerName: 'split' });
|
|
55
|
+
resolve();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('Boolean evaluations', () => {
|
|
62
|
+
|
|
63
|
+
test('should correctly evaluate a boolean flag that is ON', async () => {
|
|
64
|
+
let result = client.getBooleanValue('splitters', false);
|
|
65
|
+
expect(result).toBe(true);
|
|
66
|
+
await OpenFeature.setContext({ targetingKey: "emiliano@split.io" });
|
|
67
|
+
result = client.getBooleanValue('splitters', false);
|
|
68
|
+
expect(result).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('should correctly evaluate a boolean flag that is OFF', async () => {
|
|
72
|
+
const result = await client.getBooleanValue('always_off', true);
|
|
73
|
+
expect(result).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('should return details for boolean flag evaluation with attributes', async () => {
|
|
77
|
+
|
|
78
|
+
// If `group` attribute has `value_without_config` value, it should return off treatment without config
|
|
79
|
+
await OpenFeature.setContext({trafficType: 'user', group: 'value_without_config'});
|
|
80
|
+
let details = await client.getBooleanDetails('split_with_config', false);
|
|
81
|
+
expect(details.value).toBe(false);
|
|
82
|
+
expect(details.flagKey).toBe('split_with_config');
|
|
83
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
84
|
+
expect(details.variant).toBe('off');
|
|
85
|
+
expect(details.flagMetadata.config).toBe('');
|
|
86
|
+
|
|
87
|
+
// If `group` attribute isn't present, it should return om treatment with configs
|
|
88
|
+
await OpenFeature.setContext({trafficType: 'user'});
|
|
89
|
+
details = await client.getBooleanDetails('split_with_config', false);
|
|
90
|
+
expect(details.value).toBe(true);
|
|
91
|
+
expect(details.flagKey).toBe('split_with_config');
|
|
92
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
93
|
+
expect(details.variant).toBe('on');
|
|
94
|
+
expect(details.flagMetadata.config).toBe('{"color":"brown","dimensions":{"height":12,"width":14},"text":{"inner":"click me"}}');
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('String evaluations', () => {
|
|
99
|
+
test('should correctly evaluate a string flag', async () => {
|
|
100
|
+
await OpenFeature.setContext({targetingKey: 'emmanuel@split.io', trafficType: 'account'});
|
|
101
|
+
const result = client.getStringValue('blacklist', 'default');
|
|
102
|
+
expect(result).toBe('not_allowed');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('should return details for string flag evaluation', async () => {
|
|
106
|
+
let details = await client.getStringDetails('developers', 'default');
|
|
107
|
+
expect(details.value).toBe('off');
|
|
108
|
+
expect(details.flagKey).toBe('developers');
|
|
109
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
110
|
+
expect(details.variant).toBe('off');
|
|
111
|
+
expect(details.flagMetadata.config).toBe('');
|
|
112
|
+
|
|
113
|
+
await OpenFeature.setContext({targetingKey: 'emiliano@split.io', trafficType: 'account'});
|
|
114
|
+
details = await client.getStringDetails('developers', 'default');
|
|
115
|
+
expect(details.value).toBe('on');
|
|
116
|
+
expect(details.flagKey).toBe('developers');
|
|
117
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
118
|
+
expect(details.variant).toBe('on');
|
|
119
|
+
expect(details.flagMetadata.config).toBe('{"color":"blue"}');
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('Number evaluations', () => {
|
|
124
|
+
test('should correctly evaluate a number flag', async () => {
|
|
125
|
+
const result = await client.getNumberValue('qc_team', 0);
|
|
126
|
+
expect(result).toBe(20);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('should return details for number flag evaluation', async () => {
|
|
130
|
+
const details = await client.getNumberDetails('qc_team', 0);
|
|
131
|
+
expect(details.value).toBe(20);
|
|
132
|
+
expect(details.flagKey).toBe('qc_team');
|
|
133
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
134
|
+
expect(details.variant).toBe('20');
|
|
135
|
+
expect(details.flagMetadata.config).toBe('{"color":"red"}');
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('Object evaluations', () => {
|
|
140
|
+
test('should correctly evaluate an object flag', async () => {
|
|
141
|
+
await OpenFeature.setContext({targetingKey: 'nicolas@split.io', trafficType: 'account'});
|
|
142
|
+
const result = await client.getObjectValue('whitelist', {ke:true});
|
|
143
|
+
expect(result).toEqual({ allowed: true });
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('should return details for object flag evaluation', async () => {
|
|
147
|
+
const details = await client.getObjectDetails('whitelist', {});
|
|
148
|
+
expect(details.value).toEqual({ allowed: true });
|
|
149
|
+
expect(details.flagKey).toBe('whitelist');
|
|
150
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
151
|
+
expect(details.variant).toBe('{"allowed":true}');
|
|
152
|
+
expect(details.flagMetadata.config).toBe('{"color":"green"}');
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/* eslint-disable jest/no-conditional-expect */
|
|
2
|
+
import { OpenFeature } from '@openfeature/web-sdk';
|
|
3
|
+
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
4
|
+
|
|
5
|
+
describe('OpenFeature Split Provider - Mock Integration Tests', () => {
|
|
6
|
+
let client;
|
|
7
|
+
let mockSplitClient;
|
|
8
|
+
let provider;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
// Create a fully mocked Split client that returns predictable values
|
|
12
|
+
mockSplitClient = {
|
|
13
|
+
// Mock Split client methods
|
|
14
|
+
ready: jest.fn(() => true),
|
|
15
|
+
on: jest.fn((event, callback) => {
|
|
16
|
+
if (event === 'SDK_READY') {
|
|
17
|
+
// Immediately call the callback to simulate ready state
|
|
18
|
+
callback();
|
|
19
|
+
}
|
|
20
|
+
return { id: 'mock-listener' };
|
|
21
|
+
}),
|
|
22
|
+
Event: { SDK_READY: 'SDK_READY' },
|
|
23
|
+
__getStatus: () => ({isReady: true}),
|
|
24
|
+
|
|
25
|
+
// Mock treatment evaluation methods
|
|
26
|
+
getTreatmentWithConfig: jest.fn((splitName) => {
|
|
27
|
+
if (splitName === 'my_feature') return { treatment: 'on', config: '{"desc": "this is a test"}' };
|
|
28
|
+
if (splitName === 'some_other_feature') return { treatment: 'off' };
|
|
29
|
+
if (splitName === 'int_feature') return { treatment: '32' };
|
|
30
|
+
if (splitName === 'obj_feature') return { treatment: '{"key": "value"}' };
|
|
31
|
+
return { treatment: 'control' };
|
|
32
|
+
}),
|
|
33
|
+
|
|
34
|
+
// Mock for cleanup
|
|
35
|
+
destroy: jest.fn(() => Promise.resolve())
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const mockSplitFactory = {
|
|
39
|
+
client: () => mockSplitClient,
|
|
40
|
+
destroy: () => {}
|
|
41
|
+
};
|
|
42
|
+
provider = new OpenFeatureSplitProvider(mockSplitFactory);
|
|
43
|
+
|
|
44
|
+
// Register with OpenFeature
|
|
45
|
+
OpenFeature.setProviderAndWait(provider);
|
|
46
|
+
OpenFeature.setContext({ targetingKey: 'user1' });
|
|
47
|
+
|
|
48
|
+
// Get the client
|
|
49
|
+
client = OpenFeature.getClient('mock-test');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(async () => {
|
|
53
|
+
await mockSplitClient.destroy();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('boolean evaluation should work', async () => {
|
|
57
|
+
const result = await client.getBooleanValue('my_feature', false);
|
|
58
|
+
expect(result).toBe(true);
|
|
59
|
+
expect(mockSplitClient.getTreatmentWithConfig).toHaveBeenCalledWith('my_feature', {});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('boolean evaluation should handle off value', async () => {
|
|
63
|
+
const result = await client.getBooleanValue('some_other_feature', true);
|
|
64
|
+
expect(result).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('string evaluation should work', async () => {
|
|
68
|
+
const result = await client.getStringValue('some_other_feature', 'default');
|
|
69
|
+
expect(result).toBe('off');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('number evaluation should work', async () => {
|
|
73
|
+
const result = await client.getNumberValue('int_feature', 0);
|
|
74
|
+
expect(result).toBe(32);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('object evaluation should work', async () => {
|
|
78
|
+
const result = await client.getObjectValue('obj_feature', {});
|
|
79
|
+
expect(result).toEqual({ key: 'value' });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('boolean details should include metadata', async () => {
|
|
83
|
+
const details = await client.getBooleanDetails('my_feature', false);
|
|
84
|
+
expect(details.value).toBe(true);
|
|
85
|
+
expect(details.variant).toBe('on');
|
|
86
|
+
expect(details.flagKey).toBe('my_feature');
|
|
87
|
+
expect(details.flagMetadata.config).toBe('{"desc": "this is a test"}');
|
|
88
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('control treatment should be handled correctly', async () => {
|
|
92
|
+
try {
|
|
93
|
+
provider.resolveBooleanEvaluation('non_existent_feature', false, { targetingKey: 'user1' });
|
|
94
|
+
} catch (error) {
|
|
95
|
+
expect(error.name).toBe('FlagNotFoundError');
|
|
96
|
+
expect(error.message).toContain('control');
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { OpenFeature } from '@openfeature/web-sdk';
|
|
2
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
3
|
+
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
4
|
+
|
|
5
|
+
describe('OpenFeature Split Provider - Working Integration Test', () => {
|
|
6
|
+
let client;
|
|
7
|
+
let provider;
|
|
8
|
+
let splitClient;
|
|
9
|
+
|
|
10
|
+
// Properly define the split treatments for localhost mode
|
|
11
|
+
const localFeatures = {
|
|
12
|
+
// Define features with proper structure
|
|
13
|
+
'my_feature': {
|
|
14
|
+
treatment: 'on',
|
|
15
|
+
config: '{"desc": "this is a test"}'
|
|
16
|
+
},
|
|
17
|
+
'some_other_feature': 'off',
|
|
18
|
+
'int_feature': '32',
|
|
19
|
+
'obj_feature': '{"key": "value"}'
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
// Create client
|
|
24
|
+
const splitFactory = SplitFactory({
|
|
25
|
+
core: {
|
|
26
|
+
authorizationKey: 'localhost'
|
|
27
|
+
},
|
|
28
|
+
features: localFeatures
|
|
29
|
+
});
|
|
30
|
+
splitClient = splitFactory.client();
|
|
31
|
+
|
|
32
|
+
provider = new OpenFeatureSplitProvider(splitFactory);
|
|
33
|
+
|
|
34
|
+
OpenFeature.setProvider(provider);
|
|
35
|
+
await OpenFeature.setContext({targetingKey: 'user1'});
|
|
36
|
+
client = OpenFeature.getClient('test');
|
|
37
|
+
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(async () => {
|
|
41
|
+
await splitClient.destroy();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('boolean treatment evaluations', async () => {
|
|
45
|
+
|
|
46
|
+
// Test the boolean value evaluation
|
|
47
|
+
const result = await client.getBooleanValue('my_feature', false);
|
|
48
|
+
expect(result).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('boolean treatment details evaluations', async () => {
|
|
52
|
+
|
|
53
|
+
// Test the boolean value evaluation
|
|
54
|
+
const result = await client.getBooleanDetails('my_feature', false);
|
|
55
|
+
expect(result.value).toBe(true);
|
|
56
|
+
expect(result.flagMetadata.config).toBe('{"desc": "this is a test"}');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Add a test for string treatment
|
|
60
|
+
test('string treatment evaluations', async () => {
|
|
61
|
+
const result = await client.getStringValue('some_other_feature', 'default');
|
|
62
|
+
expect(result).toBe('off');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Add a test for number treatment
|
|
66
|
+
test('number treatment evaluations', async () => {
|
|
67
|
+
const result = await client.getNumberValue('int_feature', 0);
|
|
68
|
+
expect(result).toBe(32);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Add a test for object treatment
|
|
72
|
+
test('object treatment evaluations', async () => {
|
|
73
|
+
const result = await client.getObjectValue('obj_feature', {});
|
|
74
|
+
expect(result).toEqual({ key: 'value' });
|
|
75
|
+
});
|
|
76
|
+
});
|