4
4
import * as td from 'testdouble' ;
5
5
import mock from 'xhr-mock' ;
6
6
7
- import { IAssignmentTestCase , readAssignmentTestData } from '../test/testHelpers' ;
7
+ import {
8
+ IAssignmentTestCase ,
9
+ readAssignmentTestData ,
10
+ readMockRacResponse ,
11
+ } from '../test/testHelpers' ;
8
12
9
13
import { IAssignmentLogger } from './assignment-logger' ;
10
14
import { MAX_EVENT_QUEUE_SIZE } from './constants' ;
11
15
import EppoClient from './eppo-client' ;
12
- import { IExperimentConfiguration } from './experiment/experiment-configuration' ;
13
- import { IVariation } from './experiment/variation' ;
14
16
import { EppoLocalStorage } from './local-storage' ;
15
17
import { OperatorType } from './rule' ;
16
18
import { EppoSessionStorage } from './session-storage' ;
@@ -26,20 +28,8 @@ describe('EppoClient E2E test', () => {
26
28
window . sessionStorage . clear ( ) ;
27
29
mock . setup ( ) ;
28
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
- const testCases : IAssignmentTestCase [ ] = readAssignmentTestData ( ) ;
30
- const assignmentConfig : Record < string , IExperimentConfiguration > = { } ;
31
- testCases . forEach ( ( { experiment, percentExposure, variations } ) => {
32
- assignmentConfig [ experiment ] = {
33
- name : experiment ,
34
- percentExposure,
35
- enabled : true ,
36
- subjectShards : 10000 ,
37
- variations,
38
- overrides : { } ,
39
- rules : [ ] ,
40
- } ;
41
- } ) ;
42
- return res . status ( 200 ) . body ( JSON . stringify ( { experiments : assignmentConfig } ) ) ;
31
+ const rac = readMockRacResponse ( ) ;
32
+ return res . status ( 200 ) . body ( JSON . stringify ( rac ) ) ;
43
33
} ) ;
44
34
const preloadedConfig = {
45
35
name : preloadedConfigExperiment ,
@@ -142,22 +132,16 @@ describe('EppoClient E2E test', () => {
142
132
it . each ( readAssignmentTestData ( ) ) (
143
133
'test variation assignment splits' ,
144
134
async ( {
145
- variations,
146
135
experiment,
147
- percentExposure,
148
136
subjects,
137
+ subjectsWithAttributes,
149
138
expectedAssignments,
150
139
} : IAssignmentTestCase ) => {
151
140
console . log ( `---- Test Case for ${ experiment } Experiment ----` ) ;
152
- const assignments = getAssignments ( subjects , experiment ) ;
153
- // verify the assingments don't change across test runs (deterministic)
141
+ const assignments = subjectsWithAttributes
142
+ ? getAssignmentsWithSubjectAttributes ( subjectsWithAttributes , experiment )
143
+ : getAssignments ( subjects , experiment ) ;
154
144
expect ( assignments ) . toEqual ( expectedAssignments ) ;
155
- const expectedVariationSplitPercentage = percentExposure / variations . length ;
156
- const unassignedCount = assignments . filter ( ( assignment ) => assignment == null ) . length ;
157
- expectToBeCloseToPercentage ( unassignedCount / assignments . length , 1 - percentExposure ) ;
158
- variations . forEach ( ( variation ) => {
159
- validateAssignmentCounts ( assignments , expectedVariationSplitPercentage , variation ) ;
160
- } ) ;
161
145
} ,
162
146
) ;
163
147
} ) ;
@@ -286,30 +270,24 @@ describe('EppoClient E2E test', () => {
286
270
expect ( assignment ) . toEqual ( 'control' ) ;
287
271
} ) ;
288
272
289
- function validateAssignmentCounts (
290
- assignments : string [ ] ,
291
- expectedPercentage : number ,
292
- variation : IVariation ,
293
- ) {
294
- const assignedCount = assignments . filter ( ( assignment ) => assignment === variation . name ) . length ;
295
- console . log (
296
- `Expect variation ${ variation . name } percentage of ${
297
- assignedCount / assignments . length
298
- } to be close to ${ expectedPercentage } `,
299
- ) ;
300
- expectToBeCloseToPercentage ( assignedCount / assignments . length , expectedPercentage ) ;
301
- }
302
-
303
- // expect assignment count to be within 5 percentage points of the expected count (because the hash output is random)
304
- function expectToBeCloseToPercentage ( percentage : number , expectedPercentage : number ) {
305
- expect ( percentage ) . toBeGreaterThanOrEqual ( expectedPercentage - 0.05 ) ;
306
- expect ( percentage ) . toBeLessThanOrEqual ( expectedPercentage + 0.05 ) ;
307
- }
308
-
309
273
function getAssignments ( subjects : string [ ] , experiment : string ) : string [ ] {
310
274
const client = getInstance ( ) ;
311
275
return subjects . map ( ( subjectKey ) => {
312
276
return client . getAssignment ( subjectKey , experiment ) ;
313
277
} ) ;
314
278
}
279
+
280
+ function getAssignmentsWithSubjectAttributes (
281
+ subjectsWithAttributes : {
282
+ subjectKey : string ;
283
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
284
+ subjectAttributes : Record < string , any > ;
285
+ } [ ] ,
286
+ experiment : string ,
287
+ ) : string [ ] {
288
+ const client = getInstance ( ) ;
289
+ return subjectsWithAttributes . map ( ( subject ) => {
290
+ return client . getAssignment ( subject . subjectKey , experiment , subject . subjectAttributes ) ;
291
+ } ) ;
292
+ }
315
293
} ) ;
0 commit comments