@@ -3,57 +3,86 @@ import { DynamicTargetedFeatureFlag } from '../../../src/featureFlag/DynamicFeat
33import { FeatureFlagSupplier } from '../../../src/featureFlag/FeatureFlagSupplier' ;
44
55describe ( 'FeatureFlagSupplier' , ( ) => {
6- afterEach ( ( ) => {
7- vi . restoreAllMocks ( ) ;
8- } ) ;
9-
10- it ( 'should initialize with targeted feature flags' , ( ) => {
11- const configSupplier = vi . fn ( ( ) => ( {
6+ const configSupplier = ( ) => {
7+ return {
128 version : 1 ,
139 description : 'test' ,
1410 features : {
15- EnhancedDryRun : { enabled : true } ,
11+ EnhancedDryRun : { enabled : true , fleetPercentage : 100 , allowlistedRegions : [ 'us-east-1' ] } ,
12+ Constants : { enabled : false } ,
1613 } ,
17- } ) ) ;
14+ } ;
15+ } ;
16+
17+ const throwError = ( ) => {
18+ throw new Error ( 'Error fallback' ) ;
19+ } ;
20+
21+ afterEach ( ( ) => {
22+ vi . restoreAllMocks ( ) ;
23+ } ) ;
1824
19- const supplier = new FeatureFlagSupplier ( configSupplier ) ;
25+ it ( 'should initialize with feature flags' , ( ) => {
26+ const supplier = new FeatureFlagSupplier ( configSupplier , throwError ) ;
27+
28+ expect ( [ ...supplier . featureFlags . keys ( ) ] ) . toEqual ( [ 'Constants' ] ) ;
29+ expect ( supplier . featureFlags . get ( 'Constants' ) ?. isEnabled ( ) ) . toBe ( false ) ;
30+
31+ expect ( [ ...supplier . targetedFeatureFlags . keys ( ) ] ) . toEqual ( [ 'EnhancedDryRun' ] ) ;
32+ expect ( supplier . targetedFeatureFlags . get ( 'EnhancedDryRun' ) ?. isEnabled ( 'us-east-1' ) ) . toBe ( true ) ;
33+ expect ( supplier . targetedFeatureFlags . get ( 'EnhancedDryRun' ) ?. isEnabled ( 'us-east-2' ) ) . toBe ( false ) ;
2034
21- expect ( supplier . featureFlags . size ) . toBe ( 1 ) ;
22- expect ( supplier . targetedFeatureFlags . size ) . toBe ( 1 ) ;
23- expect ( supplier . targetedFeatureFlags . has ( 'EnhancedDryRun' ) ) . toBe ( true ) ;
2435 supplier . close ( ) ;
2536 } ) ;
2637
2738 it ( 'should close all dynamic feature flags' , ( ) => {
28- const configSupplier = vi . fn ( ( ) => ( {
29- version : 1 ,
30- description : 'test' ,
31- features : { } ,
32- } ) ) ;
33-
34- const supplier = new FeatureFlagSupplier ( configSupplier ) ;
39+ const supplier = new FeatureFlagSupplier (
40+ ( ) => {
41+ return { version : 1 , description : 'test' , features : { } } ;
42+ } ,
43+ ( ) => { } ,
44+ ) ;
3545 const closeSpy = vi . spyOn ( DynamicTargetedFeatureFlag . prototype , 'close' ) ;
3646
3747 supplier . close ( ) ;
3848
3949 expect ( closeSpy ) . toHaveBeenCalled ( ) ;
4050 } ) ;
4151
42- it ( 'should handle invalid config gracefully ' , ( ) => {
43- const configSupplier = vi . fn ( ( ) => 'invalid' ) ;
52+ it ( 'should handle invalid config and fallback to default ' , ( ) => {
53+ const supplier = new FeatureFlagSupplier ( ( ) => 'invalid' , configSupplier ) ;
4454
45- const supplier = new FeatureFlagSupplier ( configSupplier ) ;
55+ expect ( [ ...supplier . featureFlags . keys ( ) ] ) . toEqual ( [ 'Constants' ] ) ;
56+ expect ( [ ...supplier . targetedFeatureFlags . keys ( ) ] ) . toEqual ( [ 'EnhancedDryRun' ] ) ;
4657
47- expect ( supplier . targetedFeatureFlags . size ) . toBe ( 1 ) ;
4858 supplier . close ( ) ;
4959 } ) ;
5060
5161 it ( 'should handle undefined config' , ( ) => {
52- const configSupplier = vi . fn ( ( ) => undefined ) ;
62+ const supplier = new FeatureFlagSupplier ( ( ) => undefined , configSupplier ) ;
5363
54- const supplier = new FeatureFlagSupplier ( configSupplier ) ;
64+ expect ( [ ...supplier . featureFlags . keys ( ) ] ) . toEqual ( [ 'Constants' ] ) ;
65+ expect ( [ ...supplier . targetedFeatureFlags . keys ( ) ] ) . toEqual ( [ 'EnhancedDryRun' ] ) ;
5566
56- expect ( supplier . targetedFeatureFlags . size ) . toBe ( 1 ) ;
5767 supplier . close ( ) ;
5868 } ) ;
69+
70+ it ( 'throws if both suppliers fail' , ( ) => {
71+ expect (
72+ ( ) =>
73+ new FeatureFlagSupplier (
74+ ( ) => undefined ,
75+ ( ) => undefined ,
76+ ) ,
77+ ) . toThrow (
78+ '[\n' +
79+ ' {\n' +
80+ ' "expected": "object",\n' +
81+ ' "code": "invalid_type",\n' +
82+ ' "path": [],\n' +
83+ ' "message": "Invalid input: expected object, received undefined"\n' +
84+ ' }\n' +
85+ ']' ,
86+ ) ;
87+ } ) ;
5988} ) ;
0 commit comments