@@ -21,87 +21,155 @@ const namespace = 'LD_KV';
21
21
const rootEnvKey = `LD-Env-${ clientSideID } ` ;
22
22
23
23
describe ( 'init' , ( ) => {
24
- let kv : KVNamespace ;
25
- let ldClient : LDClient ;
26
-
27
- beforeAll ( async ( ) => {
28
- kv = ( await mf . getKVNamespace ( namespace ) ) as unknown as KVNamespace ;
29
- await kv . put ( rootEnvKey , JSON . stringify ( allFlagsSegments ) ) ;
30
- ldClient = init ( clientSideID , kv ) ;
31
- await ldClient . waitForInitialization ( ) ;
32
- } ) ;
33
-
34
- afterAll ( ( ) => {
35
- ldClient . close ( ) ;
36
- } ) ;
24
+ describe ( 'without caching' , ( ) => {
25
+ let kv : KVNamespace ;
26
+ let ldClient : LDClient ;
27
+
28
+ beforeAll ( async ( ) => {
29
+ kv = ( await mf . getKVNamespace ( namespace ) ) as unknown as KVNamespace ;
30
+ await kv . put ( rootEnvKey , JSON . stringify ( allFlagsSegments ) ) ;
31
+ ldClient = init ( clientSideID , kv ) ;
32
+ await ldClient . waitForInitialization ( ) ;
33
+ } ) ;
37
34
38
- describe ( 'flags' , ( ) => {
39
- test ( 'variation default' , async ( ) => {
40
- const value = await ldClient . variation ( flagKey1 , context , false ) ;
41
- expect ( value ) . toBeTruthy ( ) ;
35
+ afterAll ( ( ) => {
36
+ ldClient . close ( ) ;
42
37
} ) ;
43
38
44
- test ( 'variation default rollout' , async ( ) => {
45
- const contextWithEmail = { ...
context , email :
'[email protected] ' } ;
46
- const value = await ldClient . variation ( flagKey2 , contextWithEmail , false ) ;
47
- const detail = await ldClient . variationDetail ( flagKey2 , contextWithEmail , false ) ;
39
+ describe ( 'flags' , ( ) => {
40
+ it ( 'variation default' , async ( ) => {
41
+ const value = await ldClient . variation ( flagKey1 , context , false ) ;
42
+ expect ( value ) . toBeTruthy ( ) ;
43
+ } ) ;
48
44
49
- expect ( detail ) . toEqual ( { reason : { kind : 'FALLTHROUGH' } , value : true , variationIndex : 0 } ) ;
50
- expect ( value ) . toBeTruthy ( ) ;
51
- } ) ;
45
+ it ( 'variation default rollout' , async ( ) => {
46
+ const contextWithEmail = { ...
context , email :
'[email protected] ' } ;
47
+ const value = await ldClient . variation ( flagKey2 , contextWithEmail , false ) ;
48
+ const detail = await ldClient . variationDetail ( flagKey2 , contextWithEmail , false ) ;
52
49
53
- test ( 'rule match' , async ( ) => {
54
- const contextWithEmail = { ...
context , email :
'[email protected] ' } ;
55
- const value = await ldClient . variation ( flagKey1 , contextWithEmail , false ) ;
56
- const detail = await ldClient . variationDetail ( flagKey1 , contextWithEmail , false ) ;
50
+ expect ( detail ) . toEqual ( { reason : { kind : 'FALLTHROUGH' } , value : true , variationIndex : 0 } ) ;
51
+ expect ( value ) . toBeTruthy ( ) ;
52
+ } ) ;
57
53
58
- expect ( detail ) . toEqual ( {
59
- reason : { kind : 'RULE_MATCH' , ruleId : 'rule1' , ruleIndex : 0 } ,
60
- value : false ,
61
- variationIndex : 1 ,
54
+ it ( 'rule match' , async ( ) => {
55
+ const contextWithEmail = { ...
context , email :
'[email protected] ' } ;
56
+ const value = await ldClient . variation ( flagKey1 , contextWithEmail , false ) ;
57
+ const detail = await ldClient . variationDetail ( flagKey1 , contextWithEmail , false ) ;
58
+
59
+ expect ( detail ) . toEqual ( {
60
+ reason : { kind : 'RULE_MATCH' , ruleId : 'rule1' , ruleIndex : 0 } ,
61
+ value : false ,
62
+ variationIndex : 1 ,
63
+ } ) ;
64
+ expect ( value ) . toBeFalsy ( ) ;
62
65
} ) ;
63
- expect ( value ) . toBeFalsy ( ) ;
64
- } ) ;
65
66
66
- test ( 'fallthrough' , async ( ) => {
67
- const contextWithEmail = { ...
context , email :
'[email protected] ' } ;
68
- const value = await ldClient . variation ( flagKey1 , contextWithEmail , false ) ;
69
- const detail = await ldClient . variationDetail ( flagKey1 , contextWithEmail , false ) ;
67
+ it ( 'fallthrough' , async ( ) => {
68
+ const contextWithEmail = { ...
context , email :
'[email protected] ' } ;
69
+ const value = await ldClient . variation ( flagKey1 , contextWithEmail , false ) ;
70
+ const detail = await ldClient . variationDetail ( flagKey1 , contextWithEmail , false ) ;
70
71
71
- expect ( detail ) . toEqual ( { reason : { kind : 'FALLTHROUGH' } , value : true , variationIndex : 0 } ) ;
72
- expect ( value ) . toBeTruthy ( ) ;
72
+ expect ( detail ) . toEqual ( { reason : { kind : 'FALLTHROUGH' } , value : true , variationIndex : 0 } ) ;
73
+ expect ( value ) . toBeTruthy ( ) ;
74
+ } ) ;
75
+
76
+ it ( 'allFlags fallthrough' , async ( ) => {
77
+ const allFlags = await ldClient . allFlagsState ( context ) ;
78
+
79
+ expect ( allFlags ) . toBeDefined ( ) ;
80
+ expect ( allFlags . toJSON ( ) ) . toEqual ( {
81
+ $flagsState : {
82
+ testFlag1 : { debugEventsUntilDate : 2000 , variation : 0 , version : 2 } ,
83
+ testFlag2 : { debugEventsUntilDate : 2000 , variation : 0 , version : 2 } ,
84
+ testFlag3 : { debugEventsUntilDate : 2000 , variation : 0 , version : 2 } ,
85
+ } ,
86
+ $valid : true ,
87
+ testFlag1 : true ,
88
+ testFlag2 : true ,
89
+ testFlag3 : true ,
90
+ } ) ;
91
+ } ) ;
73
92
} ) ;
74
93
75
- test ( 'allFlags fallthrough' , async ( ) => {
76
- const allFlags = await ldClient . allFlagsState ( context ) ;
77
-
78
- expect ( allFlags ) . toBeDefined ( ) ;
79
- expect ( allFlags . toJSON ( ) ) . toEqual ( {
80
- $flagsState : {
81
- testFlag1 : { debugEventsUntilDate : 2000 , variation : 0 , version : 2 } ,
82
- testFlag2 : { debugEventsUntilDate : 2000 , variation : 0 , version : 2 } ,
83
- testFlag3 : { debugEventsUntilDate : 2000 , variation : 0 , version : 2 } ,
84
- } ,
85
- $valid : true ,
86
- testFlag1 : true ,
87
- testFlag2 : true ,
88
- testFlag3 : true ,
94
+ describe ( 'segments' , ( ) => {
95
+ it ( 'segment by country' , async ( ) => {
96
+ const contextWithCountry = { ...context , country : 'australia' } ;
97
+ const value = await ldClient . variation ( flagKey3 , contextWithCountry , false ) ;
98
+ const detail = await ldClient . variationDetail ( flagKey3 , contextWithCountry , false ) ;
99
+
100
+ expect ( detail ) . toEqual ( {
101
+ reason : { kind : 'RULE_MATCH' , ruleId : 'rule1' , ruleIndex : 0 } ,
102
+ value : false ,
103
+ variationIndex : 1 ,
104
+ } ) ;
105
+ expect ( value ) . toBeFalsy ( ) ;
89
106
} ) ;
90
107
} ) ;
91
108
} ) ;
92
109
93
- describe ( 'segments ' , ( ) => {
94
- test ( 'segment by country ', async ( ) => {
95
- const contextWithCountry = { ... context , country : 'australia' } ;
96
- const value = await ldClient . variation ( flagKey3 , contextWithCountry , false ) ;
97
- const detail = await ldClient . variationDetail ( flagKey3 , contextWithCountry , false ) ;
110
+ describe ( 'with caching ' , ( ) => {
111
+ it ( 'will cache across multiple variation calls ', async ( ) => {
112
+ const kv = ( await mf . getKVNamespace ( namespace ) ) as unknown as KVNamespace ;
113
+ await kv . put ( rootEnvKey , JSON . stringify ( allFlagsSegments ) ) ;
114
+ const ldClient = init ( clientSideID , kv , { cache : { ttl : 60 , checkInterval : 600 } } ) ;
98
115
99
- expect ( detail ) . toEqual ( {
100
- reason : { kind : 'RULE_MATCH' , ruleId : 'rule1' , ruleIndex : 0 } ,
101
- value : false ,
102
- variationIndex : 1 ,
103
- } ) ;
104
- expect ( value ) . toBeFalsy ( ) ;
116
+ await ldClient . waitForInitialization ( ) ;
117
+ const spy = jest . spyOn ( kv , 'get' ) ;
118
+ await ldClient . variation ( flagKey1 , context , false ) ;
119
+ await ldClient . variation ( flagKey2 , context , false ) ;
120
+ ldClient . close ( ) ;
121
+
122
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
123
+ } ) ;
124
+
125
+ it ( 'will cache across multiple allFlags calls' , async ( ) => {
126
+ const kv = ( await mf . getKVNamespace ( namespace ) ) as unknown as KVNamespace ;
127
+ await kv . put ( rootEnvKey , JSON . stringify ( allFlagsSegments ) ) ;
128
+ const ldClient = init ( clientSideID , kv , { cache : { ttl : 60 , checkInterval : 600 } } ) ;
129
+
130
+ await ldClient . waitForInitialization ( ) ;
131
+ const spy = jest . spyOn ( kv , 'get' ) ;
132
+ await ldClient . allFlagsState ( context ) ;
133
+ await ldClient . allFlagsState ( context ) ;
134
+ ldClient . close ( ) ;
135
+
136
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
137
+ } ) ;
138
+
139
+ it ( 'will cache between allFlags and variation' , async ( ) => {
140
+ const kv = ( await mf . getKVNamespace ( namespace ) ) as unknown as KVNamespace ;
141
+ await kv . put ( rootEnvKey , JSON . stringify ( allFlagsSegments ) ) ;
142
+ const ldClient = init ( clientSideID , kv , { cache : { ttl : 60 , checkInterval : 600 } } ) ;
143
+
144
+ await ldClient . waitForInitialization ( ) ;
145
+ const spy = jest . spyOn ( kv , 'get' ) ;
146
+ await ldClient . variation ( flagKey1 , context , false ) ;
147
+ await ldClient . allFlagsState ( context ) ;
148
+ ldClient . close ( ) ;
149
+
150
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
151
+ } ) ;
152
+
153
+ it ( 'will eventually expire' , async ( ) => {
154
+ jest . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => 0 ) ;
155
+
156
+ const kv = ( await mf . getKVNamespace ( namespace ) ) as unknown as KVNamespace ;
157
+ await kv . put ( rootEnvKey , JSON . stringify ( allFlagsSegments ) ) ;
158
+ const ldClient = init ( clientSideID , kv , { cache : { ttl : 60 , checkInterval : 600 } } ) ;
159
+
160
+ await ldClient . waitForInitialization ( ) ;
161
+ const spy = jest . spyOn ( kv , 'get' ) ;
162
+ await ldClient . variation ( flagKey1 , context , false ) ;
163
+ await ldClient . variation ( flagKey2 , context , false ) ;
164
+
165
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
166
+
167
+ jest . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => 60 * 1000 + 1 ) ;
168
+
169
+ await ldClient . variation ( flagKey2 , context , false ) ;
170
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
171
+
172
+ ldClient . close ( ) ;
105
173
} ) ;
106
174
} ) ;
107
175
} ) ;
0 commit comments