1
- import { EvaluationContext , OpenFeature , ProviderEvents } from '@openfeature/server-sdk' ;
1
+ import { EvaluationContext , EvaluationDetails , OpenFeature , ProviderEvents } from '@openfeature/server-sdk' ;
2
2
import { defineFeature , loadFeature } from 'jest-cucumber' ;
3
3
import { StepsDefinitionCallbackFunction } from 'jest-cucumber/dist/src/feature-definition-creation' ;
4
+ import { E2E_CLIENT_NAME } from '../constants' ;
4
5
5
6
// load the feature file.
6
7
const feature = loadFeature ( 'features/flagd-json-evaluator.feature' ) ;
7
8
8
9
// get a client (flagd provider registered in setup)
9
- const client = OpenFeature . getClient ( 'e2e' ) ;
10
+ const client = OpenFeature . getClient ( E2E_CLIENT_NAME ) ;
10
11
11
12
const aFlagProviderIsSet = ( given : ( stepMatcher : string , stepDefinitionCallback : ( ) => void ) => void ) => {
12
13
given ( 'a flagd provider is set' , ( ) => undefined ) ;
@@ -56,10 +57,12 @@ defineFeature(feature, (test) => {
56
57
defaultValue = defaultVal ;
57
58
} ) ;
58
59
and (
59
- / ^ a c o n t e x t c o n t a i n i n g a n e s t e d p r o p e r t y w i t h o u t e r k e y " ( .* ) " a n d i n n e r k e y " ( .* ) " , w i t h v a l u e " ( .* ) " $ / ,
60
+ / ^ a c o n t e x t c o n t a i n i n g a n e s t e d p r o p e r t y w i t h o u t e r k e y " ( .* ) " a n d i n n e r k e y " ( .* ) " , w i t h v a l u e ( .* ) $ / ,
60
61
( outerKey : string , innerKey : string , value : string ) => {
62
+ // we have to support string and non-string params in this test (we test invalid context value 3)
63
+ const valueNoQuotes = value . replaceAll ( '"' , '' ) ;
61
64
evaluationContext [ outerKey ] = {
62
- [ innerKey ] : value ,
65
+ [ innerKey ] : parseInt ( valueNoQuotes ) || valueNoQuotes ,
63
66
} ;
64
67
} ,
65
68
) ;
@@ -71,7 +74,70 @@ defineFeature(feature, (test) => {
71
74
72
75
test ( 'Substring operators' , evaluateStringFlagWithContext ) ;
73
76
74
- test ( 'Semantic version operator numeric comparision ' , evaluateStringFlagWithContext ) ;
77
+ test ( 'Semantic version operator numeric comparison ' , evaluateStringFlagWithContext ) ;
75
78
76
- test ( 'Semantic version operator semantic comparision' , evaluateStringFlagWithContext ) ;
79
+ test ( 'Semantic version operator semantic comparison' , evaluateStringFlagWithContext ) ;
80
+
81
+ test ( 'Time-based operations' , ( { given, when, and, then } ) => {
82
+ let flagKey : string ;
83
+ let defaultValue : number ;
84
+ const evaluationContext : EvaluationContext = { } ;
85
+
86
+ aFlagProviderIsSet ( given ) ;
87
+
88
+ when ( / ^ a n i n t e g e r f l a g w i t h k e y " ( .* ) " i s e v a l u a t e d w i t h d e f a u l t v a l u e ( \d + ) $ / , ( key , defaultVal ) => {
89
+ flagKey = key ;
90
+ defaultValue = defaultVal ;
91
+ } ) ;
92
+
93
+ and ( / ^ a c o n t e x t c o n t a i n i n g a k e y " ( .* ) " , w i t h v a l u e ( .* ) $ / , ( key , value ) => {
94
+ evaluationContext [ key ] = value ;
95
+ } ) ;
96
+ then ( / ^ t h e r e t u r n e d v a l u e s h o u l d b e ( .* ) $ / , async ( expectedValue ) => {
97
+ const value = await client . getNumberValue ( flagKey , defaultValue , evaluationContext ) ;
98
+ expect ( value ) . toEqual ( parseInt ( expectedValue ) ) ;
99
+ } ) ;
100
+ } ) ;
101
+
102
+ test ( 'Targeting by targeting key' , ( { given, when, and, then } ) => {
103
+ let flagKey : string ;
104
+ let defaultValue : string ;
105
+ let details : EvaluationDetails < string > ;
106
+
107
+ aFlagProviderIsSet ( given ) ;
108
+
109
+ when ( / ^ a s t r i n g f l a g w i t h k e y " ( .* ) " i s e v a l u a t e d w i t h d e f a u l t v a l u e " ( .* ) " $ / , ( key , defaultVal ) => {
110
+ flagKey = key ;
111
+ defaultValue = defaultVal ;
112
+ } ) ;
113
+
114
+ and ( / ^ a c o n t e x t c o n t a i n i n g a t a r g e t i n g k e y w i t h v a l u e " ( .* ) " $ / , async ( targetingKeyValue ) => {
115
+ details = await client . getStringDetails ( flagKey , defaultValue , { targetingKey : targetingKeyValue } ) ;
116
+ } ) ;
117
+
118
+ then ( / ^ t h e r e t u r n e d v a l u e s h o u l d b e " ( .* ) " $ / , ( expectedValue ) => {
119
+ expect ( details . value ) . toEqual ( expectedValue ) ;
120
+ } ) ;
121
+
122
+ then ( / ^ t h e r e t u r n e d r e a s o n s h o u l d b e " ( .* ) " $ / , ( expectedReason ) => {
123
+ expect ( details . reason ) . toEqual ( expectedReason ) ;
124
+ } ) ;
125
+ } ) ;
126
+
127
+ test ( 'Errors and edge cases' , ( { given, when, then } ) => {
128
+ let flagKey : string ;
129
+ let defaultValue : number ;
130
+
131
+ aFlagProviderIsSet ( given ) ;
132
+
133
+ when ( / ^ a n i n t e g e r f l a g w i t h k e y " ( .* ) " i s e v a l u a t e d w i t h d e f a u l t v a l u e ( .* ) $ / , ( key , defaultVal ) => {
134
+ flagKey = key ;
135
+ defaultValue = parseInt ( defaultVal ) ;
136
+ } ) ;
137
+
138
+ then ( / ^ t h e r e t u r n e d v a l u e s h o u l d b e ( .* ) $ / , async ( expectedValue ) => {
139
+ const value = await client . getNumberValue ( flagKey , defaultValue ) ;
140
+ expect ( value ) . toEqual ( parseInt ( expectedValue ) ) ;
141
+ } ) ;
142
+ } ) ;
77
143
} ) ;
0 commit comments