forked from open-feature/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.spec.ts
365 lines (303 loc) · 12.4 KB
/
evaluation.spec.ts
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
360
361
362
363
364
365
import { defineFeature, loadFeature } from 'jest-cucumber';
import {
JsonValue,
JsonObject,
EvaluationDetails,
EvaluationContext,
ResolutionDetails,
StandardResolutionReasons,
ProviderEvents,
} from '@openfeature/core';
import { OpenFeature, InMemoryProvider } from '../../src';
import flagConfiguration from './flags-config';
// load the feature file.
const feature = loadFeature('packages/server/e2e/features/evaluation.feature');
// get a client (flagd provider registered in setup)
const client = OpenFeature.getClient();
const givenAnOpenfeatureClientIsRegisteredWithCacheDisabled = (
given: (stepMatcher: string, stepDefinitionCallback: () => void) => void
) => {
OpenFeature.setProvider(
new InMemoryProvider(flagConfiguration),
);
given('a provider is registered with cache disabled', () => undefined);
};
defineFeature(feature, (test) => {
beforeAll((done) => {
client.addHandler(ProviderEvents.Ready, async () => {
done();
});
});
afterAll(async () => {
await OpenFeature.close();
});
test('Resolves boolean value', ({ given, when, then }) => {
let value: boolean;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a boolean flag with key "(.*)" is evaluated with default value "(.*)"$/,
async (key: string, defaultValue: string) => {
flagKey = key;
value = await client.getBooleanValue(flagKey, defaultValue === 'true');
}
);
then(/^the resolved boolean value should be "(.*)"$/, (expectedValue: string) => {
expect(value).toEqual(expectedValue === 'true');
});
});
test('Resolves string value', ({ given, when, then }) => {
let value: string;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a string flag with key "(.*)" is evaluated with default value "(.*)"$/,
async (key: string, defaultValue: string) => {
flagKey = key;
value = await client.getStringValue(flagKey, defaultValue);
}
);
then(/^the resolved string value should be "(.*)"$/, (expectedValue: string) => {
expect(value).toEqual(expectedValue);
});
});
test('Resolves integer value', ({ given, when, then }) => {
let value: number;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^an integer flag with key "(.*)" is evaluated with default value (\d+)$/,
async (key: string, defaultValue: string) => {
flagKey = key;
value = await client.getNumberValue(flagKey, Number.parseInt(defaultValue));
}
);
then(/^the resolved integer value should be (\d+)$/, (expectedValue: string) => {
expect(value).toEqual(Number.parseInt(expectedValue));
});
});
test('Resolves float value', ({ given, when, then }) => {
let value: number;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a float flag with key "(.*)" is evaluated with default value (\d+\.?\d*)$/,
async (key: string, defaultValue: string) => {
flagKey = key;
value = await client.getNumberValue(flagKey, Number.parseFloat(defaultValue));
}
);
then(/^the resolved float value should be (\d+\.?\d*)$/, (expectedValue: string) => {
expect(value).toEqual(Number.parseFloat(expectedValue));
});
});
test('Resolves object value', ({ given, when, then }) => {
let value: JsonValue;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(/^an object flag with key "(.*)" is evaluated with a null default value$/, async (key: string) => {
flagKey = key;
value = await client.getObjectValue(flagKey, {});
});
then(
/^the resolved object value should be contain fields "(.*)", "(.*)", and "(.*)", with values "(.*)", "(.*)" and (\d+), respectively$/,
(field1: string, field2: string, field3: string, boolValue: string, stringValue: string, intValue: string) => {
const jsonObject = value as JsonObject;
expect(jsonObject[field1]).toEqual(boolValue === 'true');
expect(jsonObject[field2]).toEqual(stringValue);
expect(jsonObject[field3]).toEqual(Number.parseInt(intValue));
}
);
});
test('Resolves boolean details', ({ given, when, then }) => {
let details: EvaluationDetails<boolean>;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a boolean flag with key "(.*)" is evaluated with details and default value "(.*)"$/,
async (key: string, defaultValue: string) => {
flagKey = key;
details = await client.getBooleanDetails(flagKey, defaultValue === 'true');
}
);
then(
/^the resolved boolean details value should be "(.*)", the variant should be "(.*)", and the reason should be "(.*)"$/,
(expectedValue: string, expectedVariant: string, expectedReason: string) => {
expect(details.value).toEqual(expectedValue === 'true');
expect(details.variant).toEqual(expectedVariant);
expect(details.reason).toEqual(expectedReason);
}
);
});
test('Resolves string details', ({ given, when, then }) => {
let details: EvaluationDetails<string>;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a string flag with key "(.*)" is evaluated with details and default value "(.*)"$/,
async (key: string, defaultValue: string) => {
flagKey = key;
details = await client.getStringDetails(flagKey, defaultValue);
}
);
then(
/^the resolved string details value should be "(.*)", the variant should be "(.*)", and the reason should be "(.*)"$/,
(expectedValue: string, expectedVariant: string, expectedReason: string) => {
expect(details.value).toEqual(expectedValue);
expect(details.variant).toEqual(expectedVariant);
expect(details.reason).toEqual(expectedReason);
}
);
});
test('Resolves integer details', ({ given, when, then }) => {
let details: EvaluationDetails<number>;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^an integer flag with key "(.*)" is evaluated with details and default value (\d+)$/,
async (key: string, defaultValue: string) => {
flagKey = key;
details = await client.getNumberDetails(flagKey, Number.parseInt(defaultValue));
}
);
then(
/^the resolved integer details value should be (\d+), the variant should be "(.*)", and the reason should be "(.*)"$/,
(expectedValue: string, expectedVariant: string, expectedReason: string) => {
expect(details.value).toEqual(Number.parseInt(expectedValue));
expect(details.variant).toEqual(expectedVariant);
expect(details.reason).toEqual(expectedReason);
}
);
});
test('Resolves float details', ({ given, when, then }) => {
let details: EvaluationDetails<number>;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a float flag with key "(.*)" is evaluated with details and default value (\d+\.?\d*)$/,
async (key: string, defaultValue: string) => {
flagKey = key;
details = await client.getNumberDetails(flagKey, Number.parseFloat(defaultValue));
}
);
then(
/^the resolved float details value should be (\d+\.?\d*), the variant should be "(.*)", and the reason should be "(.*)"$/,
(expectedValue: string, expectedVariant: string, expectedReason: string) => {
expect(details.value).toEqual(Number.parseFloat(expectedValue));
expect(details.variant).toEqual(expectedVariant);
expect(details.reason).toEqual(expectedReason);
}
);
});
test('Resolves object details', ({ given, when, then, and }) => {
let details: EvaluationDetails<JsonValue>; // update this after merge
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(/^an object flag with key "(.*)" is evaluated with details and a null default value$/, async (key: string) => {
flagKey = key;
details = await client.getObjectDetails(flagKey, {}); // update this after merge
});
then(
/^the resolved object details value should be contain fields "(.*)", "(.*)", and "(.*)", with values "(.*)", "(.*)" and (\d+), respectively$/,
(field1: string, field2: string, field3: string, boolValue: string, stringValue: string, intValue: string) => {
const jsonObject = details.value as JsonObject;
expect(jsonObject[field1]).toEqual(boolValue === 'true');
expect(jsonObject[field2]).toEqual(stringValue);
expect(jsonObject[field3]).toEqual(Number.parseInt(intValue));
}
);
and(
/^the variant should be "(.*)", and the reason should be "(.*)"$/,
(expectedVariant: string, expectedReason: string) => {
expect(details.variant).toEqual(expectedVariant);
expect(details.reason).toEqual(expectedReason);
}
);
});
test('Resolves based on context', ({ given, when, and, then }) => {
const context: EvaluationContext = {};
let value: string;
let flagKey: string;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^context contains keys "(.*)", "(.*)", "(.*)", "(.*)" with values "(.*)", "(.*)", (\d+), "(.*)"$/,
(
stringField1: string,
stringField2: string,
intField: string,
boolField: string,
stringValue1: string,
stringValue2: string,
intValue: string,
boolValue: string
) => {
context[stringField1] = stringValue1;
context[stringField2] = stringValue2;
context[intField] = Number.parseInt(intValue);
context[boolField] = boolValue === 'true';
}
);
and(
/^a flag with key "(.*)" is evaluated with default value "(.*)"$/,
async (key: string, defaultValue: string) => {
flagKey = key;
value = await client.getStringValue(flagKey, defaultValue, context);
}
);
then(/^the resolved string response should be "(.*)"$/, (expectedValue: string) => {
expect(value).toEqual(expectedValue);
});
and(/^the resolved flag value is "(.*)" when the context is empty$/, async (expectedValue) => {
const emptyContextValue = await client.getStringValue(flagKey, 'nope', {});
expect(emptyContextValue).toEqual(expectedValue);
});
});
test('Flag not found', ({ given, when, then, and }) => {
let flagKey: string;
let fallbackValue: string;
let details: ResolutionDetails<string>;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a non-existent string flag with key "(.*)" is evaluated with details and a default value "(.*)"$/,
async (key: string, defaultValue: string) => {
flagKey = key;
fallbackValue = defaultValue;
details = await client.getStringDetails(flagKey, defaultValue);
}
);
then(/^the default string value should be returned$/, () => {
expect(details.value).toEqual(fallbackValue);
});
and(
/^the reason should indicate an error and the error code should indicate a missing flag with "(.*)"$/,
(errorCode: string) => {
expect(details.reason).toEqual(StandardResolutionReasons.ERROR);
expect(details.errorCode).toEqual(errorCode);
}
);
});
test('Type error', ({ given, when, then, and }) => {
let flagKey: string;
let fallbackValue: number;
let details: ResolutionDetails<number>;
givenAnOpenfeatureClientIsRegisteredWithCacheDisabled(given);
when(
/^a string flag with key "(.*)" is evaluated as an integer, with details and a default value (\d+)$/,
async (key: string, defaultValue: string) => {
flagKey = key;
fallbackValue = Number.parseInt(defaultValue);
details = await client.getNumberDetails(flagKey, Number.parseInt(defaultValue));
}
);
then(/^the default integer value should be returned$/, () => {
expect(details.value).toEqual(fallbackValue);
});
and(
/^the reason should indicate an error and the error code should indicate a type mismatch with "(.*)"$/,
(errorCode: string) => {
expect(details.reason).toEqual(StandardResolutionReasons.ERROR);
expect(details.errorCode).toEqual(errorCode);
}
);
});
});