11import { SSMClient , SSMClientConfig } from '@aws-sdk/client-ssm' ;
22import { AwsSsmProvider } from './aws-ssm-provider' ;
3+ import { StandardResolutionReasons } from '@openfeature/core' ;
4+ import { Cache } from './cache' ;
35
46const MOCK_SSM_CLIENT_CONFIG : SSMClientConfig = {
57 region : 'us-east-1' ,
@@ -9,8 +11,186 @@ const MOCK_SSM_CLIENT_CONFIG: SSMClientConfig = {
911 } ,
1012} ;
1113
14+ const provider : AwsSsmProvider = new AwsSsmProvider ( {
15+ ssmClientConfig : MOCK_SSM_CLIENT_CONFIG ,
16+ cacheOpts : {
17+ enabled : true ,
18+ ttl : 1000 ,
19+ size : 100 ,
20+ } ,
21+ } ) ;
22+
1223describe ( AwsSsmProvider . name , ( ) => {
13- it ( 'should pass' , ( ) => {
14- expect ( true ) ;
24+ describe ( AwsSsmProvider . prototype . resolveBooleanEvaluation . name , ( ) => {
25+ beforeEach ( ( ) => {
26+ jest . clearAllMocks ( ) ;
27+ } ) ;
28+ describe ( 'when flag is cached' , ( ) => {
29+ afterAll ( ( ) => {
30+ provider . cache . clear ( ) ;
31+ } ) ;
32+ it ( 'should return cached value' , async ( ) => {
33+ provider . cache . set ( 'test' , {
34+ value : true ,
35+ reason : StandardResolutionReasons . STATIC ,
36+ } ) ;
37+ await expect ( provider . resolveBooleanEvaluation ( 'test' , false , { } ) ) . resolves . toEqual ( {
38+ value : true ,
39+ reason : StandardResolutionReasons . CACHED ,
40+ } ) ;
41+ } ) ;
42+ } ) ;
43+ describe ( 'when flag is not cached' , ( ) => {
44+ describe ( 'when getBooleanValue rejects' , ( ) => {
45+ it ( 'should return default value' , async ( ) => {
46+ jest . spyOn ( provider . service , 'getBooleanValue' ) . mockRejectedValue ( new Error ( ) ) ;
47+ await expect ( provider . resolveBooleanEvaluation ( 'test' , false , { } ) ) . resolves . toEqual ( {
48+ value : false ,
49+ reason : StandardResolutionReasons . DEFAULT ,
50+ } ) ;
51+ } ) ;
52+ } ) ;
53+ describe ( 'when getBooleanValue resolves' , ( ) => {
54+ it ( 'should resolve with expected value' , async ( ) => {
55+ jest . spyOn ( provider . service , 'getBooleanValue' ) . mockResolvedValue ( {
56+ value : true ,
57+ reason : StandardResolutionReasons . STATIC ,
58+ } ) ;
59+ await expect ( provider . resolveBooleanEvaluation ( 'test' , false , { } ) ) . resolves . toEqual ( {
60+ value : true ,
61+ reason : StandardResolutionReasons . STATIC ,
62+ } ) ;
63+ } ) ;
64+ } ) ;
65+ } ) ;
66+ } ) ;
67+ describe ( AwsSsmProvider . prototype . resolveStringEvaluation . name , ( ) => {
68+ beforeEach ( ( ) => {
69+ jest . clearAllMocks ( ) ;
70+ } ) ;
71+ describe ( 'when flag is cached' , ( ) => {
72+ afterAll ( ( ) => {
73+ provider . cache . clear ( ) ;
74+ } ) ;
75+ it ( 'should return cached value' , async ( ) => {
76+ provider . cache . set ( 'test' , {
77+ value : 'somestring' ,
78+ reason : StandardResolutionReasons . STATIC ,
79+ } ) ;
80+ await expect ( provider . resolveStringEvaluation ( 'test' , 'default' , { } ) ) . resolves . toEqual ( {
81+ value : 'somestring' ,
82+ reason : StandardResolutionReasons . CACHED ,
83+ } ) ;
84+ } ) ;
85+ } ) ;
86+ describe ( 'when flag is not cached' , ( ) => {
87+ describe ( 'when getStringValue rejects' , ( ) => {
88+ it ( 'should return default value' , async ( ) => {
89+ jest . spyOn ( provider . service , 'getStringValue' ) . mockRejectedValue ( new Error ( ) ) ;
90+ await expect ( provider . resolveStringEvaluation ( 'test' , 'default' , { } ) ) . resolves . toEqual ( {
91+ value : 'default' ,
92+ reason : StandardResolutionReasons . DEFAULT ,
93+ } ) ;
94+ } ) ;
95+ } ) ;
96+ describe ( 'when getStringValue resolves' , ( ) => {
97+ it ( 'should resolve with expected value' , async ( ) => {
98+ jest . spyOn ( provider . service , 'getStringValue' ) . mockResolvedValue ( {
99+ value : 'somestring' ,
100+ reason : StandardResolutionReasons . STATIC ,
101+ } ) ;
102+ await expect ( provider . resolveStringEvaluation ( 'test' , 'default' , { } ) ) . resolves . toEqual ( {
103+ value : 'somestring' ,
104+ reason : StandardResolutionReasons . STATIC ,
105+ } ) ;
106+ } ) ;
107+ } ) ;
108+ } ) ;
109+ } ) ;
110+ describe ( AwsSsmProvider . prototype . resolveNumberEvaluation . name , ( ) => {
111+ beforeEach ( ( ) => {
112+ jest . clearAllMocks ( ) ;
113+ } ) ;
114+ describe ( 'when flag is cached' , ( ) => {
115+ afterAll ( ( ) => {
116+ provider . cache . clear ( ) ;
117+ } ) ;
118+ it ( 'should return cached value' , async ( ) => {
119+ provider . cache . set ( 'test' , {
120+ value : 489 ,
121+ reason : StandardResolutionReasons . STATIC ,
122+ } ) ;
123+ await expect ( provider . resolveNumberEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
124+ value : 489 ,
125+ reason : StandardResolutionReasons . CACHED ,
126+ } ) ;
127+ } ) ;
128+ } ) ;
129+ describe ( 'when flag is not cached' , ( ) => {
130+ describe ( 'when getNumberValue rejects' , ( ) => {
131+ it ( 'should return default value' , async ( ) => {
132+ jest . spyOn ( provider . service , 'getNumberValue' ) . mockRejectedValue ( new Error ( ) ) ;
133+ await expect ( provider . resolveNumberEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
134+ value : - 1 ,
135+ reason : StandardResolutionReasons . DEFAULT ,
136+ } ) ;
137+ } ) ;
138+ } ) ;
139+ describe ( 'when getNumberValue resolves' , ( ) => {
140+ it ( 'should resolve with expected value' , async ( ) => {
141+ jest . spyOn ( provider . service , 'getNumberValue' ) . mockResolvedValue ( {
142+ value : 489 ,
143+ reason : StandardResolutionReasons . STATIC ,
144+ } ) ;
145+ await expect ( provider . resolveNumberEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
146+ value : 489 ,
147+ reason : StandardResolutionReasons . STATIC ,
148+ } ) ;
149+ } ) ;
150+ } ) ;
151+ } ) ;
152+ } ) ;
153+ describe ( AwsSsmProvider . prototype . resolveObjectEvaluation . name , ( ) => {
154+ beforeEach ( ( ) => {
155+ jest . clearAllMocks ( ) ;
156+ } ) ;
157+ describe ( 'when flag is cached' , ( ) => {
158+ afterAll ( ( ) => {
159+ provider . cache . clear ( ) ;
160+ } ) ;
161+ it ( 'should return cached value' , async ( ) => {
162+ provider . cache . set ( 'test' , {
163+ value : { default : false } ,
164+ reason : StandardResolutionReasons . STATIC ,
165+ } ) ;
166+ await expect ( provider . resolveObjectEvaluation ( 'test' , { default : true } , { } ) ) . resolves . toEqual ( {
167+ value : { default : false } ,
168+ reason : StandardResolutionReasons . CACHED ,
169+ } ) ;
170+ } ) ;
171+ } ) ;
172+ describe ( 'when flag is not cached' , ( ) => {
173+ describe ( 'when getObjectValue rejects' , ( ) => {
174+ it ( 'should return default value' , async ( ) => {
175+ jest . spyOn ( provider . service , 'getObjectValue' ) . mockRejectedValue ( new Error ( ) ) ;
176+ await expect ( provider . resolveObjectEvaluation ( 'test' , { default : true } , { } ) ) . resolves . toEqual ( {
177+ value : { default : true } ,
178+ reason : StandardResolutionReasons . DEFAULT ,
179+ } ) ;
180+ } ) ;
181+ } ) ;
182+ describe ( 'when getObjectValue resolves' , ( ) => {
183+ it ( 'should resolve with expected value' , async ( ) => {
184+ jest . spyOn ( provider . service , 'getObjectValue' ) . mockResolvedValue ( {
185+ value : { default : true } ,
186+ reason : StandardResolutionReasons . STATIC ,
187+ } ) ;
188+ await expect ( provider . resolveObjectEvaluation ( 'test' , - 1 , { } ) ) . resolves . toEqual ( {
189+ value : { default : true } ,
190+ reason : StandardResolutionReasons . STATIC ,
191+ } ) ;
192+ } ) ;
193+ } ) ;
194+ } ) ;
15195 } ) ;
16196} ) ;
0 commit comments