@@ -2,202 +2,216 @@ import type { SSMClientConfig } from '@aws-sdk/client-ssm';
22import { AwsSsmProvider } from './aws-ssm-provider' ;
33import { ErrorCode , StandardResolutionReasons } from '@openfeature/core' ;
44
5- const MOCK_SSM_CLIENT_CONFIG : SSMClientConfig = {
6- region : 'us-east-1' ,
7- credentials : {
8- accessKeyId : 'accessKeyId' ,
9- secretAccessKey : 'secretAccessKey' ,
10- } ,
11- } ;
12-
13- const provider : AwsSsmProvider = new AwsSsmProvider ( {
14- ssmClientConfig : MOCK_SSM_CLIENT_CONFIG ,
15- cacheOpts : {
16- enabled : true ,
17- ttl : 1000 ,
18- size : 100 ,
19- } ,
20- } ) ;
5+ describe ( "aws-ssm-provider.ts - AwsSsmProvider" , ( ) => {
6+
7+ let provider : AwsSsmProvider ;
8+ let getBooleanValueSpy : jest . SpyInstance ;
9+ let getStringValueSpy : jest . SpyInstance ;
10+ let getNumberValueSpy : jest . SpyInstance ;
11+ let getObjectValueSpy : jest . SpyInstance ;
2112
22- describe ( AwsSsmProvider . name , ( ) => {
23- describe ( AwsSsmProvider . prototype . resolveBooleanEvaluation . name , ( ) => {
24- beforeEach ( ( ) => {
25- jest . clearAllMocks ( ) ;
13+ const mockSsmClientConfig : SSMClientConfig = {
14+ region : 'us-east-1' ,
15+ credentials : {
16+ accessKeyId : 'accessKeyId' ,
17+ secretAccessKey : 'secretAccessKey' ,
18+ } ,
19+ } ;
20+
21+ beforeAll ( ( ) => {
22+ provider = new AwsSsmProvider ( {
23+ ssmClientConfig : mockSsmClientConfig ,
24+ cacheOpts : {
25+ enabled : true ,
26+ ttl : 1000 ,
27+ size : 100 ,
28+ } ,
2629 } ) ;
27- describe ( 'when flag is cached' , ( ) => {
28- afterAll ( ( ) => {
29- provider . cache . clear ( ) ;
30- } ) ;
31- it ( 'should return cached value' , async ( ) => {
32- provider . cache . set ( 'test' , {
33- value : true ,
34- reason : StandardResolutionReasons . STATIC ,
35- } ) ;
36- await expect ( provider . resolveBooleanEvaluation ( 'test' , false , { } ) ) . resolves . toEqual ( {
37- value : true ,
38- reason : StandardResolutionReasons . CACHED ,
39- } ) ;
30+
31+ getBooleanValueSpy = jest . spyOn ( provider . service , 'getBooleanValue' ) ;
32+ getStringValueSpy = jest . spyOn ( provider . service , 'getStringValue' ) ;
33+ getNumberValueSpy = jest . spyOn ( provider . service , 'getNumberValue' ) ;
34+ getObjectValueSpy = jest . spyOn ( provider . service , 'getObjectValue' ) ;
35+ } ) ;
36+
37+ describe ( "resolveBooleanEvaluation" , ( ) => {
38+
39+ it ( "should return cached value when flag is cached" , async ( ) => {
40+ provider . cache . set ( 'test' , {
41+ value : true ,
42+ reason : StandardResolutionReasons . STATIC ,
43+ } ) ;
44+
45+ const result = await provider . resolveBooleanEvaluation ( 'test' , false , { } ) ;
46+
47+ expect ( result ) . toEqual ( {
48+ value : true ,
49+ reason : StandardResolutionReasons . CACHED ,
50+ } ) ;
51+ } ) ;
52+
53+ it ( "should return default value when getBooleanValue rejects" , async ( ) => {
54+ getBooleanValueSpy . mockRejectedValue ( new Error ( ) ) ;
55+
56+ const result = await provider . resolveBooleanEvaluation ( 'test-error' , false , { } ) ;
57+
58+ expect ( result ) . toEqual ( {
59+ value : false ,
60+ reason : StandardResolutionReasons . ERROR ,
61+ errorMessage : 'An unknown error occurred' ,
62+ errorCode : ErrorCode . GENERAL ,
4063 } ) ;
4164 } ) ;
42- describe ( 'when flag is not cached' , ( ) => {
43- describe ( 'when getBooleanValue rejects' , ( ) => {
44- it ( 'should return default value' , async ( ) => {
45- jest . spyOn ( provider . service , 'getBooleanValue' ) . mockRejectedValue ( new Error ( ) ) ;
46- await expect ( provider . resolveBooleanEvaluation ( 'test' , false , { } ) ) . resolves . toEqual ( {
47- value : false ,
48- reason : StandardResolutionReasons . ERROR ,
49- errorMessage : 'An unknown error occurred' ,
50- errorCode : ErrorCode . GENERAL ,
51- } ) ;
52- } ) ;
53- } ) ;
54- describe ( 'when getBooleanValue resolves' , ( ) => {
55- it ( 'should resolve with expected value' , async ( ) => {
56- jest . spyOn ( provider . service , 'getBooleanValue' ) . mockResolvedValue ( {
57- value : true ,
58- reason : StandardResolutionReasons . STATIC ,
59- } ) ;
60- await expect ( provider . resolveBooleanEvaluation ( 'test' , false , { } ) ) . resolves . toEqual ( {
61- value : true ,
62- reason : StandardResolutionReasons . STATIC ,
63- } ) ;
64- } ) ;
65+
66+ it ( "should resolve with expected value when getBooleanValue resolves" , async ( ) => {
67+ getBooleanValueSpy . mockResolvedValue ( {
68+ value : true ,
69+ reason : StandardResolutionReasons . STATIC ,
70+ } ) ;
71+
72+ const result = await provider . resolveBooleanEvaluation ( 'test-success' , false , { } ) ;
73+
74+ expect ( result ) . toEqual ( {
75+ value : true ,
76+ reason : StandardResolutionReasons . STATIC ,
6577 } ) ;
6678 } ) ;
6779 } ) ;
68- describe ( AwsSsmProvider . prototype . resolveStringEvaluation . name , ( ) => {
69- beforeEach ( ( ) => {
70- jest . clearAllMocks ( ) ;
80+
81+ describe ( "resolveStringEvaluation" , ( ) => {
82+
83+ it ( "should return cached value when flag is cached" , async ( ) => {
84+ provider . cache . set ( 'test-string' , {
85+ value : 'somestring' ,
86+ reason : StandardResolutionReasons . STATIC ,
87+ } ) ;
88+
89+ const result = await provider . resolveStringEvaluation ( 'test-string' , 'default' , { } ) ;
90+
91+ expect ( result ) . toEqual ( {
92+ value : 'somestring' ,
93+ reason : StandardResolutionReasons . CACHED ,
94+ } ) ;
7195 } ) ;
72- describe ( 'when flag is cached' , ( ) => {
73- afterAll ( ( ) => {
74- provider . cache . clear ( ) ;
75- } ) ;
76- it ( 'should return cached value' , async ( ) => {
77- provider . cache . set ( 'test' , {
78- value : 'somestring' ,
79- reason : StandardResolutionReasons . STATIC ,
80- } ) ;
81- await expect ( provider . resolveStringEvaluation ( 'test' , 'default' , { } ) ) . resolves . toEqual ( {
82- value : 'somestring' ,
83- reason : StandardResolutionReasons . CACHED ,
84- } ) ;
96+
97+ it ( "should return default value when getStringValue rejects" , async ( ) => {
98+ getStringValueSpy . mockRejectedValue ( new Error ( ) ) ;
99+
100+ const result = await provider . resolveStringEvaluation ( 'test-string-error' , 'default' , { } ) ;
101+
102+ expect ( result ) . toEqual ( {
103+ value : 'default' ,
104+ reason : StandardResolutionReasons . ERROR ,
105+ errorMessage : 'An unknown error occurred' ,
106+ errorCode : ErrorCode . GENERAL ,
85107 } ) ;
86108 } ) ;
87- describe ( 'when flag is not cached' , ( ) => {
88- describe ( 'when getStringValue rejects' , ( ) => {
89- it ( 'should return default value' , async ( ) => {
90- jest . spyOn ( provider . service , 'getStringValue' ) . mockRejectedValue ( new Error ( ) ) ;
91- await expect ( provider . resolveStringEvaluation ( 'test' , 'default' , { } ) ) . resolves . toEqual ( {
92- value : 'default' ,
93- reason : StandardResolutionReasons . ERROR ,
94- errorMessage : 'An unknown error occurred' ,
95- errorCode : ErrorCode . GENERAL ,
96- } ) ;
97- } ) ;
98- } ) ;
99- describe ( 'when getStringValue resolves' , ( ) => {
100- it ( 'should resolve with expected value' , async ( ) => {
101- jest . spyOn ( provider . service , 'getStringValue' ) . mockResolvedValue ( {
102- value : 'somestring' ,
103- reason : StandardResolutionReasons . STATIC ,
104- } ) ;
105- await expect ( provider . resolveStringEvaluation ( 'test' , 'default' , { } ) ) . resolves . toEqual ( {
106- value : 'somestring' ,
107- reason : StandardResolutionReasons . STATIC ,
108- } ) ;
109- } ) ;
109+
110+ it ( "should resolve with expected value when getStringValue resolves" , async ( ) => {
111+ getStringValueSpy . mockResolvedValue ( {
112+ value : 'somestring' ,
113+ reason : StandardResolutionReasons . STATIC ,
114+ } ) ;
115+
116+ const result = await provider . resolveStringEvaluation ( 'test-string-success' , 'default' , { } ) ;
117+
118+ expect ( result ) . toEqual ( {
119+ value : 'somestring' ,
120+ reason : StandardResolutionReasons . STATIC ,
110121 } ) ;
111122 } ) ;
112123 } ) ;
113- describe ( AwsSsmProvider . prototype . resolveNumberEvaluation . name , ( ) => {
114- beforeEach ( ( ) => {
115- jest . clearAllMocks ( ) ;
124+
125+ describe ( "resolveNumberEvaluation" , ( ) => {
126+
127+ it ( "should return cached value when flag is cached" , async ( ) => {
128+ provider . cache . set ( 'test-number' , {
129+ value : 489 ,
130+ reason : StandardResolutionReasons . STATIC ,
131+ } ) ;
132+
133+ const result = await provider . resolveNumberEvaluation ( 'test-number' , - 1 , { } ) ;
134+
135+ expect ( result ) . toEqual ( {
136+ value : 489 ,
137+ reason : StandardResolutionReasons . CACHED ,
138+ } ) ;
116139 } ) ;
117- describe ( 'when flag is cached' , ( ) => {
118- afterAll ( ( ) => {
119- provider . cache . clear ( ) ;
120- } ) ;
121- it ( 'should return cached value' , async ( ) => {
122- provider . cache . set ( 'test' , {
123- value : 489 ,
124- reason : StandardResolutionReasons . STATIC ,
125- } ) ;
126- await expect ( provider . resolveNumberEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
127- value : 489 ,
128- reason : StandardResolutionReasons . CACHED ,
129- } ) ;
140+
141+ it ( "should return default value when getNumberValue rejects" , async ( ) => {
142+ getNumberValueSpy . mockRejectedValue ( new Error ( ) ) ;
143+
144+ const result = await provider . resolveNumberEvaluation ( 'test-number-error' , - 1 , { } ) ;
145+
146+ expect ( result ) . toEqual ( {
147+ value : - 1 ,
148+ reason : StandardResolutionReasons . ERROR ,
149+ errorMessage : 'An unknown error occurred' ,
150+ errorCode : ErrorCode . GENERAL ,
130151 } ) ;
131152 } ) ;
132- describe ( 'when flag is not cached' , ( ) => {
133- describe ( 'when getNumberValue rejects' , ( ) => {
134- it ( 'should return default value' , async ( ) => {
135- jest . spyOn ( provider . service , 'getNumberValue' ) . mockRejectedValue ( new Error ( ) ) ;
136- await expect ( provider . resolveNumberEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
137- value : - 1 ,
138- reason : StandardResolutionReasons . ERROR ,
139- errorMessage : 'An unknown error occurred' ,
140- errorCode : ErrorCode . GENERAL ,
141- } ) ;
142- } ) ;
143- } ) ;
144- describe ( 'when getNumberValue resolves' , ( ) => {
145- it ( 'should resolve with expected value' , async ( ) => {
146- jest . spyOn ( provider . service , 'getNumberValue' ) . mockResolvedValue ( {
147- value : 489 ,
148- reason : StandardResolutionReasons . STATIC ,
149- } ) ;
150- await expect ( provider . resolveNumberEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
151- value : 489 ,
152- reason : StandardResolutionReasons . STATIC ,
153- } ) ;
154- } ) ;
153+
154+ it ( "should resolve with expected value when getNumberValue resolves" , async ( ) => {
155+ getNumberValueSpy . mockResolvedValue ( {
156+ value : 489 ,
157+ reason : StandardResolutionReasons . STATIC ,
158+ } ) ;
159+
160+ const result = await provider . resolveNumberEvaluation ( 'test-number-success' , - 1 , { } ) ;
161+
162+ expect ( result ) . toEqual ( {
163+ value : 489 ,
164+ reason : StandardResolutionReasons . STATIC ,
155165 } ) ;
156166 } ) ;
157167 } ) ;
158- describe ( AwsSsmProvider . prototype . resolveObjectEvaluation . name , ( ) => {
159- beforeEach ( ( ) => {
160- jest . clearAllMocks ( ) ;
168+
169+ describe ( "resolveObjectEvaluation" , ( ) => {
170+
171+ it ( "should return cached value when flag is cached" , async ( ) => {
172+ provider . cache . set ( 'test-object' , {
173+ value : { default : false } ,
174+ reason : StandardResolutionReasons . STATIC ,
175+ } ) ;
176+
177+ const result = await provider . resolveObjectEvaluation ( 'test-object' , { default : true } , { } ) ;
178+
179+ expect ( result ) . toEqual ( {
180+ value : { default : false } ,
181+ reason : StandardResolutionReasons . CACHED ,
182+ } ) ;
161183 } ) ;
162- describe ( 'when flag is cached' , ( ) => {
163- afterAll ( ( ) => {
164- provider . cache . clear ( ) ;
165- } ) ;
166- it ( 'should return cached value' , async ( ) => {
167- provider . cache . set ( 'test' , {
168- value : { default : false } ,
169- reason : StandardResolutionReasons . STATIC ,
170- } ) ;
171- await expect ( provider . resolveObjectEvaluation ( 'test' , { default : true } , { } ) ) . resolves . toEqual ( {
172- value : { default : false } ,
173- reason : StandardResolutionReasons . CACHED ,
174- } ) ;
184+
185+ it ( "should return default value when getObjectValue rejects" , async ( ) => {
186+ getObjectValueSpy . mockRejectedValue ( new Error ( ) ) ;
187+
188+ const result = await provider . resolveObjectEvaluation ( 'test-object-error' , { default : true } , { } ) ;
189+
190+ expect ( result ) . toEqual ( {
191+ value : { default : true } ,
192+ reason : StandardResolutionReasons . ERROR ,
193+ errorMessage : 'An unknown error occurred' ,
194+ errorCode : ErrorCode . GENERAL ,
175195 } ) ;
176196 } ) ;
177- describe ( 'when flag is not cached' , ( ) => {
178- describe ( 'when getObjectValue rejects' , ( ) => {
179- it ( 'should return default value' , async ( ) => {
180- jest . spyOn ( provider . service , 'getObjectValue' ) . mockRejectedValue ( new Error ( ) ) ;
181- await expect ( provider . resolveObjectEvaluation ( 'test' , { default : true } , { } ) ) . resolves . toEqual ( {
182- value : { default : true } ,
183- reason : StandardResolutionReasons . ERROR ,
184- errorMessage : 'An unknown error occurred' ,
185- errorCode : ErrorCode . GENERAL ,
186- } ) ;
187- } ) ;
188- } ) ;
189- describe ( 'when getObjectValue resolves' , ( ) => {
190- it ( 'should resolve with expected value' , async ( ) => {
191- jest . spyOn ( provider . service , 'getObjectValue' ) . mockResolvedValue ( {
192- value : { default : true } ,
193- reason : StandardResolutionReasons . STATIC ,
194- } ) ;
195- await expect ( provider . resolveObjectEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
196- value : { default : true } ,
197- reason : StandardResolutionReasons . STATIC ,
198- } ) ;
199- } ) ;
197+
198+ it ( "should resolve with expected value when getObjectValue resolves" , async ( ) => {
199+ getObjectValueSpy . mockResolvedValue ( {
200+ value : { default : true } ,
201+ reason : StandardResolutionReasons . STATIC ,
202+ } ) ;
203+
204+ const result = await provider . resolveObjectEvaluation ( 'test-object-success' , - 1 , { } ) ;
205+
206+ expect ( result ) . toEqual ( {
207+ value : { default : true } ,
208+ reason : StandardResolutionReasons . STATIC ,
200209 } ) ;
201210 } ) ;
202211 } ) ;
212+
213+ afterEach ( ( ) => {
214+ provider . cache . clear ( ) ;
215+ jest . clearAllMocks ( ) ;
216+ } ) ;
203217} ) ;
0 commit comments