4
4
import * as td from 'testdouble' ;
5
5
import mock from 'xhr-mock' ;
6
6
7
+ import { getInstance , init } from '..' ;
7
8
import {
8
9
IAssignmentTestCase ,
9
10
readAssignmentTestData ,
10
11
readMockRacResponse ,
11
- } from '../test/testHelpers' ;
12
+ } from '../../test/testHelpers' ;
13
+ import { IAssignmentLogger } from '../assignment-logger' ;
14
+ import { MAX_EVENT_QUEUE_SIZE } from '../constants' ;
15
+ import { OperatorType } from '../dto/rule-dto' ;
16
+ import { EppoLocalStorage } from '../local-storage' ;
17
+ import { EppoSessionStorage } from '../session-storage' ;
12
18
13
- import { IAssignmentLogger } from './assignment-logger' ;
14
- import { MAX_EVENT_QUEUE_SIZE } from './constants' ;
15
19
import EppoClient from './eppo-client' ;
16
- import { EppoLocalStorage } from './local-storage' ;
17
- import { OperatorType } from './rule' ;
18
- import { EppoSessionStorage } from './session-storage' ;
19
-
20
- import { getInstance , init } from '.' ;
21
20
22
21
describe ( 'EppoClient E2E test' , ( ) => {
23
22
const sessionOverrideSubject = 'subject-14' ;
@@ -27,16 +26,14 @@ describe('EppoClient E2E test', () => {
27
26
window . localStorage . clear ( ) ;
28
27
window . sessionStorage . clear ( ) ;
29
28
mock . setup ( ) ;
30
- mock . get ( / r a n d o m i z e d _ a s s i g n m e n t \/ c o n f i g * / , ( _req , res ) => {
29
+ mock . get ( / r a n d o m i z e d _ a s s i g n m e n t \/ v 2 \/ c o n f i g * / , ( _req , res ) => {
31
30
const rac = readMockRacResponse ( ) ;
32
31
return res . status ( 200 ) . body ( JSON . stringify ( rac ) ) ;
33
32
} ) ;
34
33
const preloadedConfig = {
35
34
name : preloadedConfigExperiment ,
36
- percentExposure : 1 ,
37
35
enabled : true ,
38
36
subjectShards : 100 ,
39
- variations : mockVariations ,
40
37
overrides : {
41
38
'5160f8b1a59fb002f8535257206cb824' : 'preloaded-config-variation' ,
42
39
} ,
@@ -56,50 +53,61 @@ describe('EppoClient E2E test', () => {
56
53
mock . teardown ( ) ;
57
54
} ) ;
58
55
59
- const mockVariations = [
60
- {
61
- name : 'control' ,
62
- shardRange : {
63
- start : 0 ,
64
- end : 34 ,
65
- } ,
66
- } ,
67
- {
68
- name : 'variant-1' ,
69
- shardRange : {
70
- start : 34 ,
71
- end : 67 ,
56
+ const experimentName = 'mock-experiment' ;
57
+
58
+ const mockExperimentConfig = {
59
+ name : experimentName ,
60
+ enabled : true ,
61
+ subjectShards : 100 ,
62
+ overrides : { } ,
63
+ rules : [
64
+ {
65
+ allocationKey : 'allocation1' ,
66
+ conditions : [ ] ,
72
67
} ,
73
- } ,
74
- {
75
- name : 'variant-2' ,
76
- shardRange : {
77
- start : 67 ,
78
- end : 100 ,
68
+ ] ,
69
+ allocations : {
70
+ allocation1 : {
71
+ percentExposure : 1 ,
72
+ variations : [
73
+ {
74
+ name : 'control' ,
75
+ value : 'control' ,
76
+ shardRange : {
77
+ start : 0 ,
78
+ end : 34 ,
79
+ } ,
80
+ } ,
81
+ {
82
+ name : 'variant-1' ,
83
+ value : 'variant-1' ,
84
+ shardRange : {
85
+ start : 34 ,
86
+ end : 67 ,
87
+ } ,
88
+ } ,
89
+ {
90
+ name : 'variant-2' ,
91
+ value : 'variant-2' ,
92
+ shardRange : {
93
+ start : 67 ,
94
+ end : 100 ,
95
+ } ,
96
+ } ,
97
+ ] ,
79
98
} ,
80
99
} ,
81
- ] ;
100
+ } ;
82
101
83
102
describe ( 'setLogger' , ( ) => {
84
- const experiment = 'exp-111' ;
85
103
beforeAll ( ( ) => {
86
- window . localStorage . setItem (
87
- experiment ,
88
- JSON . stringify ( {
89
- name : experiment ,
90
- percentExposure : 1 ,
91
- enabled : true ,
92
- subjectShards : 100 ,
93
- variations : mockVariations ,
94
- overrides : { } ,
95
- } ) ,
96
- ) ;
104
+ window . localStorage . setItem ( experimentName , JSON . stringify ( mockExperimentConfig ) ) ;
97
105
} ) ;
98
106
99
107
it ( 'Invokes logger for queued events' , ( ) => {
100
108
const mockLogger = td . object < IAssignmentLogger > ( ) ;
101
109
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
102
- client . getAssignment ( 'subject-to-be-logged' , experiment ) ;
110
+ client . getAssignment ( 'subject-to-be-logged' , experimentName ) ;
103
111
client . setLogger ( mockLogger ) ;
104
112
expect ( td . explain ( mockLogger . logAssignment ) . callCount ) . toEqual ( 1 ) ;
105
113
expect ( td . explain ( mockLogger . logAssignment ) . calls [ 0 ] . args [ 0 ] . subject ) . toEqual (
@@ -110,7 +118,7 @@ describe('EppoClient E2E test', () => {
110
118
it ( 'Does not log same queued event twice' , ( ) => {
111
119
const mockLogger = td . object < IAssignmentLogger > ( ) ;
112
120
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
113
- client . getAssignment ( 'subject-to-be-logged' , experiment ) ;
121
+ client . getAssignment ( 'subject-to-be-logged' , experimentName ) ;
114
122
client . setLogger ( mockLogger ) ;
115
123
expect ( td . explain ( mockLogger . logAssignment ) . callCount ) . toEqual ( 1 ) ;
116
124
client . setLogger ( mockLogger ) ;
@@ -121,7 +129,7 @@ describe('EppoClient E2E test', () => {
121
129
const mockLogger = td . object < IAssignmentLogger > ( ) ;
122
130
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
123
131
for ( let i = 0 ; i < MAX_EVENT_QUEUE_SIZE + 100 ; i ++ ) {
124
- client . getAssignment ( `subject-to-be-logged-${ i } ` , experiment ) ;
132
+ client . getAssignment ( `subject-to-be-logged-${ i } ` , experimentName ) ;
125
133
}
126
134
client . setLogger ( mockLogger ) ;
127
135
expect ( td . explain ( mockLogger . logAssignment ) . callCount ) . toEqual ( MAX_EVENT_QUEUE_SIZE ) ;
@@ -153,103 +161,67 @@ describe('EppoClient E2E test', () => {
153
161
} ) ;
154
162
155
163
it ( 'returns subject from overrides when enabled is true' , ( ) => {
156
- const experiment = 'experiment_5' ;
157
164
window . localStorage . setItem (
158
- experiment ,
165
+ experimentName ,
159
166
JSON . stringify ( {
160
- name : experiment ,
161
- percentExposure : 1 ,
162
- enabled : true ,
163
- subjectShards : 100 ,
164
- variations : mockVariations ,
167
+ ...mockExperimentConfig ,
165
168
overrides : {
166
- a90ea45116d251a43da56e03d3dd7275 : 'variant-2 ' ,
169
+ '1b50f33aef8f681a13f623963da967ed' : 'control ' ,
167
170
} ,
168
171
} ) ,
169
172
) ;
170
173
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
171
- const assignment = client . getAssignment ( 'subject-1 ' , experiment ) ;
172
- expect ( assignment ) . toEqual ( 'variant-2 ' ) ;
174
+ const assignment = client . getAssignment ( 'subject-10 ' , experimentName ) ;
175
+ expect ( assignment ) . toEqual ( 'control ' ) ;
173
176
} ) ;
174
177
175
178
it ( 'returns subject from overrides when enabled is false' , ( ) => {
176
- const experiment = 'experiment_5' ;
177
179
window . localStorage . setItem (
178
- experiment ,
180
+ experimentName ,
179
181
JSON . stringify ( {
180
- name : experiment ,
181
- percentExposure : 0 ,
182
+ ...mockExperimentConfig ,
182
183
enabled : false ,
183
- subjectShards : 100 ,
184
- variations : mockVariations ,
185
184
overrides : {
186
- a90ea45116d251a43da56e03d3dd7275 : 'variant-2 ' ,
185
+ '1b50f33aef8f681a13f623963da967ed' : 'control ' ,
187
186
} ,
188
187
} ) ,
189
188
) ;
190
189
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
191
- const assignment = client . getAssignment ( 'subject-1 ' , experiment ) ;
192
- expect ( assignment ) . toEqual ( 'variant-2 ' ) ;
190
+ const assignment = client . getAssignment ( 'subject-10 ' , experimentName ) ;
191
+ expect ( assignment ) . toEqual ( 'control ' ) ;
193
192
} ) ;
194
193
195
194
it ( 'logs variation assignment' , ( ) => {
196
195
const mockLogger = td . object < IAssignmentLogger > ( ) ;
197
- const experiment = 'experiment_5' ;
198
- window . localStorage . setItem (
199
- experiment ,
200
- JSON . stringify ( {
201
- name : experiment ,
202
- percentExposure : 1 ,
203
- enabled : true ,
204
- subjectShards : 100 ,
205
- variations : mockVariations ,
206
- overrides : { } ,
207
- } ) ,
208
- ) ;
196
+ window . localStorage . setItem ( experimentName , JSON . stringify ( mockExperimentConfig ) ) ;
209
197
const subjectAttributes = { foo : 3 } ;
210
198
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
211
199
client . setLogger ( mockLogger ) ;
212
- const assignment = client . getAssignment ( 'subject-1 ' , experiment , subjectAttributes ) ;
200
+ const assignment = client . getAssignment ( 'subject-10 ' , experimentName , subjectAttributes ) ;
213
201
expect ( assignment ) . toEqual ( 'control' ) ;
214
202
expect ( td . explain ( mockLogger . logAssignment ) . callCount ) . toEqual ( 1 ) ;
215
- expect ( td . explain ( mockLogger . logAssignment ) . calls [ 0 ] . args [ 0 ] . subject ) . toEqual ( 'subject-1 ' ) ;
203
+ expect ( td . explain ( mockLogger . logAssignment ) . calls [ 0 ] . args [ 0 ] . subject ) . toEqual ( 'subject-10 ' ) ;
216
204
} ) ;
217
205
218
206
it ( 'handles logging exception' , ( ) => {
219
207
const mockLogger = td . object < IAssignmentLogger > ( ) ;
220
- const experiment = 'experiment_5' ;
221
208
td . when ( mockLogger . logAssignment ( td . matchers . anything ( ) ) ) . thenThrow ( new Error ( 'logging error' ) ) ;
222
- window . localStorage . setItem (
223
- experiment ,
224
- JSON . stringify ( {
225
- name : experiment ,
226
- percentExposure : 1 ,
227
- enabled : true ,
228
- subjectShards : 100 ,
229
- variations : mockVariations ,
230
- overrides : { } ,
231
- } ) ,
232
- ) ;
209
+ window . localStorage . setItem ( experimentName , JSON . stringify ( mockExperimentConfig ) ) ;
233
210
const subjectAttributes = { foo : 3 } ;
234
211
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
235
212
client . setLogger ( mockLogger ) ;
236
- const assignment = client . getAssignment ( 'subject-1 ' , experiment , subjectAttributes ) ;
213
+ const assignment = client . getAssignment ( 'subject-10 ' , experimentName , subjectAttributes ) ;
237
214
expect ( assignment ) . toEqual ( 'control' ) ;
238
215
} ) ;
239
216
240
217
it ( 'only returns variation if subject matches rules' , ( ) => {
241
- const experiment = 'experiment_5' ;
242
218
window . localStorage . setItem (
243
- experiment ,
219
+ experimentName ,
244
220
JSON . stringify ( {
245
- name : experiment ,
246
- percentExposure : 1 ,
247
- enabled : true ,
248
- subjectShards : 100 ,
249
- variations : mockVariations ,
250
- overrides : { } ,
221
+ ...mockExperimentConfig ,
251
222
rules : [
252
223
{
224
+ allocationKey : 'allocation1' ,
253
225
conditions : [
254
226
{
255
227
operator : OperatorType . GT ,
@@ -262,11 +234,11 @@ describe('EppoClient E2E test', () => {
262
234
} ) ,
263
235
) ;
264
236
const client = new EppoClient ( new EppoLocalStorage ( ) , new EppoSessionStorage ( ) ) ;
265
- let assignment = client . getAssignment ( 'subject-1 ' , experiment , { appVersion : 9 } ) ;
237
+ let assignment = client . getAssignment ( 'subject-10 ' , experimentName , { appVersion : 9 } ) ;
266
238
expect ( assignment ) . toEqual ( null ) ;
267
- assignment = client . getAssignment ( 'subject-1 ' , experiment ) ;
239
+ assignment = client . getAssignment ( 'subject-10 ' , experimentName ) ;
268
240
expect ( assignment ) . toEqual ( null ) ;
269
- assignment = client . getAssignment ( 'subject-1 ' , experiment , { appVersion : 11 } ) ;
241
+ assignment = client . getAssignment ( 'subject-10 ' , experimentName , { appVersion : 11 } ) ;
270
242
expect ( assignment ) . toEqual ( 'control' ) ;
271
243
} ) ;
272
244
0 commit comments