-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathclient.spec.ts
893 lines (783 loc) · 31.4 KB
/
client.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
import { GeneralError } from '@openfeature/core';
import type {
Client,
EvaluationContext,
EvaluationDetails,
Hook,
JsonArray,
JsonObject,
JsonValue,
Provider,
ResolutionDetails,
TransactionContext,
TransactionContextPropagator,
} from '../src';
import {
ErrorCode,
FlagNotFoundError,
OpenFeature,
ProviderFatalError,
ProviderStatus,
StandardResolutionReasons,
} from '../src';
import { OpenFeatureClient } from '../src/client/internal/open-feature-client';
import { isDeepStrictEqual } from 'node:util';
import type { HookContext } from '@openfeature/core';
import type { TrackingEventDetails } from '@openfeature/core';
const BOOLEAN_VALUE = true;
const STRING_VALUE = 'val';
const NUMBER_VALUE = 2048;
const ARRAY_VALUE: JsonValue[] = [];
const INNER_KEY = 'inner';
const INNER_NULL_KEY = 'nullKey';
const INNER_BOOLEAN_KEY = 'booleanKey';
const INNER_STRING_KEY = 'stringKey';
const INNER_NUMBER_KEY = 'numberKey';
const INNER_ARRAY_KEY = 'arrayKey';
const OBJECT_VALUE: JsonValue = {
[INNER_KEY]: {
[INNER_NULL_KEY]: null,
[INNER_BOOLEAN_KEY]: BOOLEAN_VALUE,
[INNER_STRING_KEY]: STRING_VALUE,
[INNER_NUMBER_KEY]: NUMBER_VALUE,
[INNER_ARRAY_KEY]: ARRAY_VALUE,
},
};
const DATETIME_VALUE = new Date(2022, 5, 13, 18, 20, 0);
const BOOLEAN_VARIANT = `${BOOLEAN_VALUE}`;
const STRING_VARIANT = `${STRING_VALUE}-variant`;
const NUMBER_VARIANT = NUMBER_VALUE.toString();
const OBJECT_VARIANT = 'json';
const REASON = 'mocked-value';
// a mock provider with some jest spies
const MOCK_PROVIDER: Provider = {
metadata: {
name: 'mock',
},
track: jest.fn((): void => {
return;
}),
resolveBooleanEvaluation: jest.fn((): Promise<ResolutionDetails<boolean>> => {
return Promise.resolve({
value: BOOLEAN_VALUE,
variant: BOOLEAN_VARIANT,
reason: REASON,
});
}),
resolveStringEvaluation: jest.fn(<U extends string>(): Promise<ResolutionDetails<U>> => {
return Promise.resolve({
value: STRING_VALUE,
variant: STRING_VARIANT,
reason: REASON,
}) as Promise<ResolutionDetails<U>>;
}) as <U>() => Promise<ResolutionDetails<U>>,
resolveNumberEvaluation: jest.fn((): Promise<ResolutionDetails<number>> => {
return Promise.resolve({
value: NUMBER_VALUE,
variant: NUMBER_VARIANT,
reason: REASON,
});
}),
resolveObjectEvaluation: jest.fn(<U extends JsonValue>(): Promise<ResolutionDetails<U>> => {
const details = Promise.resolve<ResolutionDetails<U>>({
value: OBJECT_VALUE as U,
variant: OBJECT_VARIANT,
reason: REASON,
});
return details as Promise<ResolutionDetails<U>>;
}) as <U>() => Promise<ResolutionDetails<U>>,
};
describe('OpenFeatureClient', () => {
beforeEach(() => {
OpenFeature.setProvider(MOCK_PROVIDER);
});
afterEach(async () => {
await OpenFeature.clearProviders();
jest.clearAllMocks();
});
describe('Requirement 1.1.8', () => {
class mockAsyncProvider implements Provider {
metadata = {
name: 'mock-async',
};
status = ProviderStatus.NOT_READY;
readonly runsOn = 'server';
constructor(private readonly throwInInit: boolean) {}
async initialize(): Promise<void> {
if (this.throwInInit) {
try {
throw new Error('provider failed to initialize');
} catch (err) {
this.status = ProviderStatus.ERROR;
throw err;
}
}
this.status = ProviderStatus.READY;
return;
}
resolveBooleanEvaluation(): Promise<ResolutionDetails<boolean>> {
throw new Error('Method not implemented.');
}
resolveStringEvaluation(): Promise<ResolutionDetails<string>> {
throw new Error('Method not implemented.');
}
resolveNumberEvaluation(): Promise<ResolutionDetails<number>> {
throw new Error('Method not implemented.');
}
resolveObjectEvaluation<T extends JsonValue>(): Promise<ResolutionDetails<T>> {
throw new Error('Method not implemented.');
}
}
it('should wait for the provider to successfully initialize', async () => {
const spy = jest.spyOn(mockAsyncProvider.prototype, 'initialize');
const provider = new mockAsyncProvider(false);
expect(provider.status).toBe(ProviderStatus.NOT_READY);
await OpenFeature.setProviderAndWait(provider);
expect(provider.status).toBe(ProviderStatus.READY);
expect(spy).toHaveBeenCalled();
});
it('should wait for the provider to fail during initialization', async () => {
const spy = jest.spyOn(mockAsyncProvider.prototype, 'initialize');
const provider = new mockAsyncProvider(true);
expect(provider.status).toBe(ProviderStatus.NOT_READY);
await expect(OpenFeature.setProviderAndWait(provider)).rejects.toThrow();
expect(provider.status).toBe(ProviderStatus.ERROR);
expect(spy).toHaveBeenCalled();
});
});
describe('Requirement 1.2.1', () => {
it('should allow addition of hooks', () => {
expect(OpenFeatureClient.prototype.addHooks).toBeDefined();
});
});
describe('Requirement 1.2.1', () => {
const domain = 'my-domain';
const client = OpenFeature.getClient(domain);
it('should have metadata accessor with name for backwards compatibility', () => {
expect(client.metadata.name).toEqual(domain);
});
it('should have metadata accessor with domain', () => {
expect(client.metadata.domain).toEqual(domain);
});
});
describe('Requirement 1.3.1, 1.3.2.1', () => {
let client: Client;
beforeEach(() => {
client = OpenFeature.getClient();
});
describe('flag evaluation', () => {
describe('getBooleanValue', () => {
it('should return boolean, and call boolean resolver', async () => {
const booleanFlag = 'my-boolean-flag';
const defaultBooleanValue = false;
const value = await client.getBooleanValue(booleanFlag, defaultBooleanValue);
expect(value).toEqual(BOOLEAN_VALUE);
expect(MOCK_PROVIDER.resolveBooleanEvaluation).toHaveBeenCalledWith(booleanFlag, defaultBooleanValue, {}, {});
});
});
describe('getStringValue', () => {
describe('with no generic arg (as string)', () => {
it('should return string, and call string resolver', async () => {
const stringFlag = 'my-string-flag';
const defaultStringValue = 'default-value';
const value: string = await client.getStringValue(stringFlag, defaultStringValue);
expect(value).toEqual(STRING_VALUE);
expect(MOCK_PROVIDER.resolveStringEvaluation).toHaveBeenCalledWith(stringFlag, defaultStringValue, {}, {});
});
});
describe('with generic arg', () => {
it('should return T, and call string resolver', async () => {
const stringFlag = 'my-string-flag';
type MyRestrictedString = 'val' | 'other';
const defaultStringValue = 'other';
const value: MyRestrictedString = await client.getStringValue<MyRestrictedString>(
stringFlag,
defaultStringValue,
);
expect(value).toEqual(STRING_VALUE);
expect(MOCK_PROVIDER.resolveStringEvaluation).toHaveBeenCalledWith(stringFlag, defaultStringValue, {}, {});
});
});
});
describe('getNumberValue', () => {
describe('with no generic arg (as number)', () => {
it('should return number, and call number resolver', async () => {
const numberFlag = 'my-number-flag';
const defaultNumberValue = 1970;
const value: number = await client.getNumberValue(numberFlag, defaultNumberValue);
expect(value).toEqual(NUMBER_VALUE);
expect(MOCK_PROVIDER.resolveNumberEvaluation).toHaveBeenCalledWith(numberFlag, defaultNumberValue, {}, {});
});
});
describe('with generic arg', () => {
it('should return T, and call number resolver', async () => {
const numberFlag = 'my-number-flag';
type MyRestrictedNumber = 4096 | 2048;
const defaultNumberValue = 4096;
const value: MyRestrictedNumber = await client.getNumberValue<MyRestrictedNumber>(
numberFlag,
defaultNumberValue,
);
expect(value).toEqual(NUMBER_VALUE);
expect(MOCK_PROVIDER.resolveNumberEvaluation).toHaveBeenCalledWith(numberFlag, defaultNumberValue, {}, {});
});
});
});
describe('getObjectValue', () => {
describe('with no generic arg (as JsonValue)', () => {
it('should return JsonValue, and call object resolver', async () => {
const objectFlag = 'my-object-flag';
const defaultObjectFlag = {};
const value: JsonValue = await client.getObjectValue(objectFlag, defaultObjectFlag);
// compare the object
expect(value).toEqual(OBJECT_VALUE);
// explore the object - type assertions required for safety.
const jsonObject: JsonObject = (value as JsonObject)[INNER_KEY] as JsonObject;
const nullValue = jsonObject?.[INNER_NULL_KEY] as null;
const booleanValue = jsonObject?.[INNER_BOOLEAN_KEY] as boolean;
const stringValue = jsonObject?.[INNER_STRING_KEY] as string;
const numberValue = jsonObject?.[INNER_NUMBER_KEY] as number;
const arrayValue = jsonObject?.[INNER_ARRAY_KEY] as JsonArray;
expect(nullValue).toEqual(null);
expect(booleanValue).toEqual(BOOLEAN_VALUE);
expect(stringValue).toEqual(STRING_VALUE);
expect(numberValue).toEqual(NUMBER_VALUE);
expect(arrayValue).toEqual(ARRAY_VALUE);
});
});
describe('with generic arg', () => {
it('should return T, and call object resolver', async () => {
const objectFlag = 'my-object-flag';
type MyType = {
inner: {
booleanKey: boolean;
};
};
const defaultMyTypeFlag: MyType = {
inner: {
booleanKey: false,
},
};
const value: MyType = await client.getObjectValue<MyType>(objectFlag, defaultMyTypeFlag);
const innerBooleanValue: boolean = value.inner.booleanKey;
expect(innerBooleanValue).toBeTruthy();
});
});
});
});
});
describe('Requirement 1.4.1', () => {
let client: Client;
beforeEach(() => {
client = OpenFeature.getClient();
});
describe('detailed flag evaluation', () => {
describe('getBooleanDetails', () => {
it('should return boolean details, and call boolean resolver', async () => {
const booleanFlag = 'my-boolean-flag';
const defaultBooleanValue = false;
const booleanDetails = await client.getBooleanDetails(booleanFlag, defaultBooleanValue);
expect(booleanDetails.value).toEqual(BOOLEAN_VALUE);
expect(booleanDetails.variant).toEqual(BOOLEAN_VARIANT);
expect(MOCK_PROVIDER.resolveBooleanEvaluation).toHaveBeenCalledWith(booleanFlag, defaultBooleanValue, {}, {});
});
});
describe('getStringDetails', () => {
it('should return string details, and call string resolver', async () => {
const stringFlag = 'my-string-flag';
const defaultStringValue = 'default-value';
const stringDetails = await client.getStringDetails(stringFlag, defaultStringValue);
expect(stringDetails.value).toEqual(STRING_VALUE);
expect(stringDetails.variant).toEqual(STRING_VARIANT);
expect(MOCK_PROVIDER.resolveStringEvaluation).toHaveBeenCalledWith(stringFlag, defaultStringValue, {}, {});
});
});
describe('getNumberDetails', () => {
it('should return number details, and call number resolver', async () => {
const numberFlag = 'my-number-flag';
const defaultNumberValue = 1970;
const numberDetails = await client.getNumberDetails(numberFlag, defaultNumberValue);
expect(numberDetails.value).toEqual(NUMBER_VALUE);
expect(numberDetails.variant).toEqual(NUMBER_VARIANT);
expect(MOCK_PROVIDER.resolveNumberEvaluation).toHaveBeenCalledWith(numberFlag, defaultNumberValue, {}, {});
});
});
describe('getObjectDetails', () => {
it('should return object details, and call object resolver', async () => {
const objectFlag = 'my-object-flag';
const defaultObjectFlag = {};
const objectDetails = await client.getObjectDetails(objectFlag, defaultObjectFlag);
expect(objectDetails.value).toEqual(OBJECT_VALUE);
expect(objectDetails.variant).toEqual(OBJECT_VARIANT);
expect(MOCK_PROVIDER.resolveObjectEvaluation).toHaveBeenCalledWith(objectFlag, defaultObjectFlag, {}, {});
});
});
});
});
describe('Requirement 1.4.3.1', () => {
describe('generic support', () => {
it('should support generics', async () => {
// No generic information exists at runtime, but this test has some value in ensuring the generic args still exist in the typings.
const client = OpenFeature.getClient();
const details: ResolutionDetails<JsonValue> = await client.getObjectDetails('flag', { key: 'value' });
expect(details).toBeDefined();
});
});
});
describe('Evaluation details structure', () => {
const flagKey = 'number-details';
const defaultValue = 1970;
let details: EvaluationDetails<number>;
describe('Normal execution', () => {
beforeEach(async () => {
const client = OpenFeature.getClient();
details = await client.getNumberDetails(flagKey, defaultValue);
expect(details).toBeDefined();
});
describe('Requirement 1.4.2, 1.4.3', () => {
it('should contain flag value', () => {
expect(details.value).toEqual(NUMBER_VALUE);
});
});
describe('Requirement 1.4.4', () => {
it('should contain flag key', () => {
expect(details.flagKey).toEqual(flagKey);
});
});
describe('Requirement 1.4.5', () => {
it('should contain flag variant', () => {
expect(details.variant).toEqual(NUMBER_VARIANT);
});
});
describe('Requirement 1.4.6', () => {
it('should contain reason', () => {
expect(details.reason).toEqual(REASON);
});
});
});
describe('Abnormal execution', () => {
const NON_OPEN_FEATURE_ERROR_MESSAGE = 'A null dereference or something, I dunno.';
const OPEN_FEATURE_ERROR_MESSAGE = "This ain't the flag you're looking for.";
let nonOpenFeatureErrorDetails: EvaluationDetails<number>;
let openFeatureErrorDetails: EvaluationDetails<string>;
let client: Client;
const errorProvider = {
metadata: {
name: 'error-mock',
},
resolveNumberEvaluation: jest.fn((): Promise<ResolutionDetails<number>> => {
throw new Error(NON_OPEN_FEATURE_ERROR_MESSAGE); // throw a non-open-feature error
}),
resolveStringEvaluation: jest.fn((): Promise<ResolutionDetails<string>> => {
throw new FlagNotFoundError(OPEN_FEATURE_ERROR_MESSAGE); // throw an open-feature error
}),
} as unknown as Provider;
const defaultNumberValue = 123;
const defaultStringValue = 'hey!';
beforeEach(async () => {
OpenFeature.setProvider(errorProvider);
client = OpenFeature.getClient();
nonOpenFeatureErrorDetails = await client.getNumberDetails('some-flag', defaultNumberValue);
openFeatureErrorDetails = await client.getStringDetails('some-flag', defaultStringValue);
});
describe('Requirement 1.4.7', () => {
describe('OpenFeatureError', () => {
it('should contain error code', () => {
expect(openFeatureErrorDetails.errorCode).toBeTruthy();
expect(openFeatureErrorDetails.errorCode).toEqual(ErrorCode.FLAG_NOT_FOUND); // should get code from thrown OpenFeatureError
});
});
describe('Non-OpenFeatureError', () => {
it('should contain error code', () => {
expect(nonOpenFeatureErrorDetails.errorCode).toBeTruthy();
expect(nonOpenFeatureErrorDetails.errorCode).toEqual(ErrorCode.GENERAL); // should fall back to GENERAL
});
});
});
describe('Requirement 1.4.8', () => {
it('should contain error reason', () => {
expect(nonOpenFeatureErrorDetails.reason).toEqual(StandardResolutionReasons.ERROR);
expect(openFeatureErrorDetails.reason).toEqual(StandardResolutionReasons.ERROR);
});
});
describe('Requirement 1.4.9', () => {
it('must not throw, must return default', async () => {
nonOpenFeatureErrorDetails = await client.getNumberDetails('some-flag', defaultNumberValue);
expect(nonOpenFeatureErrorDetails).toBeTruthy();
expect(nonOpenFeatureErrorDetails.value).toEqual(defaultNumberValue);
});
});
describe('Requirement 1.4.12', () => {
describe('OpenFeatureError', () => {
it('should contain "error" message', () => {
expect(openFeatureErrorDetails.errorMessage).toEqual(OPEN_FEATURE_ERROR_MESSAGE);
});
});
describe('Non-OpenFeatureError', () => {
it('should contain "error" message', () => {
expect(nonOpenFeatureErrorDetails.errorMessage).toEqual(NON_OPEN_FEATURE_ERROR_MESSAGE);
});
});
});
});
});
describe('Requirement 1.4.13, Requirement 1.4.14', () => {
it('should return immutable `flag metadata` as defined by the provider', async () => {
const flagMetadata = {
url: 'https://test.dev',
version: '1',
};
const flagMetadataProvider = {
metadata: {
name: 'flag-metadata',
},
resolveBooleanEvaluation: jest.fn((): Promise<ResolutionDetails<boolean>> => {
return Promise.resolve({
value: true,
flagMetadata,
});
}),
} as unknown as Provider;
OpenFeature.setProvider(flagMetadataProvider);
const client = OpenFeature.getClient();
const response = await client.getBooleanDetails('some-flag', false);
expect(response.flagMetadata).toBe(flagMetadata);
expect(Object.isFrozen(response.flagMetadata)).toBeTruthy();
});
it('should return empty `flag metadata` because it was not set by the provider', async () => {
// The mock provider doesn't contain flag metadata
OpenFeature.setProvider(MOCK_PROVIDER);
const client = OpenFeature.getClient();
const response = await client.getBooleanDetails('some-flag', false);
expect(response.flagMetadata).toEqual({});
});
});
describe('Requirement 1.6.1', () => {
describe('Provider', () => {
const nonTransformingProvider = {
metadata: {
name: 'non-transforming',
},
resolveBooleanEvaluation: jest.fn((): Promise<ResolutionDetails<boolean>> => {
return Promise.resolve({
value: true,
});
}),
} as unknown as Provider;
it('should pass context to resolver', async () => {
const flagKey = 'some-other-flag';
const defaultValue = false;
const context = { transformed: false };
OpenFeature.setProvider(nonTransformingProvider);
const client = OpenFeature.getClient();
await client.getBooleanValue(flagKey, defaultValue, context);
// expect context was passed to resolver.
expect(nonTransformingProvider.resolveBooleanEvaluation).toHaveBeenCalledWith(
flagKey,
defaultValue,
expect.objectContaining({ transformed: false }),
{},
);
});
});
});
describe('Requirement 1.7.1, 1.7.3', () => {
const initProvider = {
metadata: {
name: 'initProvider',
},
initialize: () => {
return Promise.resolve();
},
} as unknown as Provider;
it('status must be READY if init resolves', async () => {
await OpenFeature.setProviderAndWait('1.7.1, 1.7.3', initProvider);
const client = OpenFeature.getClient('1.7.1, 1.7.3');
expect(client.providerStatus).toEqual(ProviderStatus.READY);
});
});
describe('Requirement 1.7.4', () => {
const errorProvider = {
metadata: {
name: 'errorProvider',
},
initialize: async () => {
return Promise.reject(new GeneralError());
},
} as unknown as Provider;
it('status must be ERROR if init rejects', async () => {
await expect(OpenFeature.setProviderAndWait('1.7.4', errorProvider)).rejects.toThrow();
const client = OpenFeature.getClient('1.7.4');
expect(client.providerStatus).toEqual(ProviderStatus.ERROR);
});
});
describe('Requirement 1.7.5, 1.7.6, 1.7.8', () => {
const fatalProvider = {
metadata: {
name: 'fatalProvider',
},
initialize: () => {
return Promise.reject(new ProviderFatalError());
},
} as unknown as Provider;
it('must short circuit and return PROVIDER_FATAL code if provider FATAL', async () => {
await expect(OpenFeature.setProviderAndWait('1.7.5, 1.7.6, 1.7.8', fatalProvider)).rejects.toThrow();
const client = OpenFeature.getClient('1.7.5, 1.7.6, 1.7.8');
expect(client.providerStatus).toEqual(ProviderStatus.FATAL);
const defaultVal = 'default';
const details = await client.getStringDetails('some-flag', defaultVal);
expect(details.value).toEqual(defaultVal);
expect(details.errorCode).toEqual(ErrorCode.PROVIDER_FATAL);
});
});
describe('Requirement 1.7.7', () => {
const neverReadyProvider = {
metadata: {
name: 'fatalProvider',
},
initialize: () => {
return new Promise(() => {
return; // promise never resolves
});
},
} as unknown as Provider;
it('must short circuit and return PROVIDER_NOT_READY code if provider NOT_READY', async () => {
OpenFeature.setProviderAndWait('1.7.7', neverReadyProvider).catch(() => {
// do nothing
});
const defaultVal = 'default';
const client = OpenFeature.getClient('1.7.7');
expect(client.providerStatus).toEqual(ProviderStatus.NOT_READY);
const details = await client.getStringDetails('some-flag', defaultVal);
expect(details.value).toEqual(defaultVal);
expect(details.errorCode).toEqual(ErrorCode.PROVIDER_NOT_READY);
});
});
describe('Evaluation Context', () => {
const provider = {
metadata: {
name: 'evaluation-context',
},
resolveBooleanEvaluation: jest.fn((): Promise<ResolutionDetails<boolean>> => {
return Promise.resolve({
value: true,
});
}),
} as unknown as Provider;
describe('3.1.1', () => {
const TARGETING_KEY = 'abc123';
it('context define targeting key', async () => {
const flagKey = 'some-other-flag';
const defaultValue = false;
const context: EvaluationContext = {
targetingKey: TARGETING_KEY,
};
OpenFeature.setProvider(provider);
const client = OpenFeature.getClient();
await client.getBooleanValue(flagKey, defaultValue, context);
expect(provider.resolveBooleanEvaluation).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining({
targetingKey: TARGETING_KEY,
}),
expect.anything(),
);
});
});
describe('3.1.2', () => {
it('should support boolean | string | number | datetime | structure', async () => {
const flagKey = 'some-other-flag';
const defaultValue = false;
const context: EvaluationContext = {
booleanField: BOOLEAN_VALUE,
stringField: STRING_VALUE,
numberField: NUMBER_VALUE,
datetimeField: DATETIME_VALUE,
structureField: OBJECT_VALUE,
};
OpenFeature.setProvider(provider);
const client = OpenFeature.getClient();
await client.getBooleanValue(flagKey, defaultValue, context);
expect(provider.resolveBooleanEvaluation).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining({
...context,
}),
expect.anything(),
);
});
});
describe('3.2.1, 3.2.2', () => {
it('Evaluation context MUST be merged in the order: API (global; lowest precedence) -> transaction context -> client -> invocation -> before hooks (highest precedence), with duplicate values being overwritten.', async () => {
const flagKey = 'some-other-flag';
const defaultValue = false;
const globalContext: EvaluationContext = {
globalContextValue: 'abc',
globalContextValueToOverwrite: 'xxx', // should be overwritten
};
const transactionContext: TransactionContext = {
transactionContextValue: 'def',
transactionContextValueToOverwrite: 'xxx', // should be overwritten
globalContextValueToOverwrite: '123',
};
const clientContext: EvaluationContext = {
clientContextValue: 'ghi',
clientContextValueToOverwrite: 'xxx', // should be overwritten
transactionContextValueToOverwrite: '456',
};
const invocationContext: EvaluationContext = {
invocationContextValue: 'jkl',
invocationContextValueToOverwrite: 'xxx', // should be overwritten
clientContextValueToOverwrite: '789',
};
const beforeHookContext: EvaluationContext = {
invocationContextValueToOverwrite: '012',
beforeHookContextValue: 'mno',
};
// Set Global Context
OpenFeature.setProvider(provider).setContext(globalContext);
// Set Transaction Context
class LocalTransactionContextPropagator implements TransactionContextPropagator {
private context: TransactionContext = {};
getTransactionContext(): EvaluationContext {
return this.context;
}
setTransactionContext<TArgs extends unknown[], R>(
transactionContext: TransactionContext,
callback: (...args: TArgs) => R,
...args: TArgs
): void {
this.context = transactionContext;
callback(...args);
}
}
OpenFeature.setTransactionContextPropagator(new LocalTransactionContextPropagator());
OpenFeature.setTransactionContext(transactionContext, jest.fn());
// Set Client Context
const client = OpenFeature.getClient('contextual', 'test', clientContext);
// Set Hook Context
const hook = {
before: jest.fn((hookContext: HookContext) => {
// we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability
if (
isDeepStrictEqual(hookContext.context, {
...globalContext,
...transactionContext,
...clientContext,
...invocationContext,
// before hook context should be missing here (and not overridden)
})
) {
return beforeHookContext;
}
}),
after: jest.fn((hookContext: HookContext) => {
// we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability
if (
isDeepStrictEqual(hookContext.context, {
...globalContext,
...transactionContext,
...clientContext,
...invocationContext,
...beforeHookContext,
})
) {
return beforeHookContext;
}
}),
} as unknown as Hook;
await client.getBooleanValue(flagKey, defaultValue, invocationContext, { hooks: [hook] });
expect(provider.resolveBooleanEvaluation).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
// expect merged in the correct order...
expect.objectContaining({
...globalContext,
...transactionContext,
...clientContext,
...invocationContext,
...beforeHookContext,
}),
expect.anything(),
);
});
});
describe('client evaluation context', () => {
it('can be mutated', async () => {
const KEY = 'key';
const VAL = 'val';
const client = OpenFeature.getClient();
client.setContext({ [KEY]: VAL });
expect(client.getContext()[KEY]).toEqual(VAL);
});
});
});
it('should be chainable', async () => {
const client = OpenFeature.getClient();
expect(await client.addHooks().clearHooks().setContext({}).setLogger(console).getBooleanValue('test', true)).toBe(
true,
);
});
describe('tracking', () => {
describe('Requirement 2.7.1, Requirement 6.1.2.1', () => {
const eventName = 'test-tracking-event';
const trackingValue = 1234;
const trackingDetails: TrackingEventDetails = {
value: trackingValue,
};
const globalContextKey = 'globalKey';
const clientContextKey = 'clientKey';
const invocationContextKey = 'invocationKey';
const globalContextValue = 'globalValue';
const clientContextValue = 'clientValue';
const invocationContextValue = 'invocationValue';
it('should no-op and not throw if tracking not defined on provider', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER, track: undefined });
const client = OpenFeature.getClient();
expect(() => {
client.track(eventName, {}, trackingDetails);
}).not.toThrow();
});
it('should no-op and not throw if provider throws', async () => {
await OpenFeature.setProviderAndWait({
...MOCK_PROVIDER,
track: () => {
throw new Error('fake error');
},
});
const client = OpenFeature.getClient();
expect(() => {
client.track(eventName, {}, trackingDetails);
}).not.toThrow();
});
it('provide empty tracking details to provider if not supplied in call', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
const client = OpenFeature.getClient();
client.track(eventName);
expect(MOCK_PROVIDER.track).toHaveBeenCalledWith(
eventName,
expect.any(Object),
expect.any(Object),
);
});
it('should call provider with correct context', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
OpenFeature.setContext({ [globalContextKey]: globalContextValue });
const client = OpenFeature.getClient();
client.setContext({ [clientContextKey]: clientContextValue });
client.track(eventName, { [invocationContextKey]: invocationContextValue }, trackingDetails);
expect(MOCK_PROVIDER.track).toHaveBeenCalledWith(
eventName,
expect.objectContaining({
[globalContextKey]: globalContextValue,
[clientContextKey]: clientContextValue,
[invocationContextKey]: invocationContextValue,
}),
expect.objectContaining({ value: trackingValue }),
);
});
});
});
});