-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheppo-client-with-bandits.spec.ts
736 lines (649 loc) · 27.5 KB
/
eppo-client-with-bandits.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import * as base64 from 'js-base64';
import {
readMockUFCResponse,
MOCK_BANDIT_MODELS_RESPONSE_FILE,
MOCK_FLAGS_WITH_BANDITS_RESPONSE_FILE,
testCasesByFileName,
BanditTestCase,
BANDIT_TEST_DATA_DIR,
readMockConfigurationWireResponse,
SHARED_BOOTSTRAP_BANDIT_FLAGS_FILE,
} from '../../test/testHelpers';
import ApiEndpoints from '../api-endpoints';
import { IAssignmentEvent, IAssignmentLogger } from '../assignment-logger';
import { BanditEvaluation, BanditEvaluator } from '../bandit-evaluator';
import { IBanditEvent, IBanditLogger } from '../bandit-logger';
import ConfigurationRequestor from '../configuration-requestor';
import { ConfigurationManager } from '../configuration-store/configuration-manager';
import { MemoryOnlyConfigurationStore } from '../configuration-store/memory.store';
import {
IConfigurationWire,
IPrecomputedConfiguration,
IObfuscatedPrecomputedConfigurationResponse,
ConfigurationWireV1, configurationFromString,
} from '../configuration-wire/configuration-wire-types';
import { Evaluator, FlagEvaluation } from '../evaluator';
import {
AllocationEvaluationCode,
IFlagEvaluationDetails,
} from '../flag-evaluation-details-builder';
import FetchHttpClient from '../http-client';
import { BanditVariation, BanditParameters, Flag } from '../interfaces';
import { attributeEncodeBase64 } from '../obfuscation';
import { Attributes, BanditActions, ContextAttributes } from '../types';
import EppoClient, { IAssignmentDetails } from './eppo-client';
describe('EppoClient Bandits E2E test', () => {
const flagStore = new MemoryOnlyConfigurationStore<Flag>();
const banditVariationStore = new MemoryOnlyConfigurationStore<BanditVariation[]>();
const banditModelStore = new MemoryOnlyConfigurationStore<BanditParameters>();
let client: EppoClient;
const mockLogAssignment = jest.fn();
const mockLogBanditAction = jest.fn();
beforeAll(async () => {
// Mock out fetch to return the bandit flag configuration and model parameters
global.fetch = jest.fn((url: string) => {
const responseFile = url.includes('bandits')
? MOCK_BANDIT_MODELS_RESPONSE_FILE
: MOCK_FLAGS_WITH_BANDITS_RESPONSE_FILE;
const response = readMockUFCResponse(responseFile);
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(response),
});
}) as jest.Mock;
// Initialize a configuration requestor
const apiEndpoints = new ApiEndpoints({
baseUrl: 'http://127.0.0.1:4000',
queryParams: {
apiKey: 'dummy',
sdkName: 'js-client-sdk-common',
sdkVersion: '1.0.0',
},
});
const httpClient = new FetchHttpClient(apiEndpoints, 1000);
const configManager = new ConfigurationManager(
flagStore,
banditVariationStore,
banditModelStore,
);
const configurationRequestor = new ConfigurationRequestor(httpClient, configManager, true);
await configurationRequestor.fetchAndStoreConfigurations();
});
beforeEach(() => {
client = new EppoClient({
flagConfigurationStore: flagStore,
banditVariationConfigurationStore: banditVariationStore,
banditModelConfigurationStore: banditModelStore,
isObfuscated: false,
});
client.setIsGracefulFailureMode(false);
client.setAssignmentLogger({ logAssignment: mockLogAssignment });
client.setBanditLogger({ logBanditAction: mockLogBanditAction });
jest.clearAllMocks();
});
afterAll(() => {
jest.restoreAllMocks();
});
describe('Shared test cases', () => {
const testCases = testCasesByFileName<BanditTestCase>(BANDIT_TEST_DATA_DIR);
function testBanditCaseAgainstClient(client: EppoClient, testCase: BanditTestCase) {
const { flag: flagKey, defaultValue, subjects } = testCase;
let numAssignmentsChecked = 0;
subjects.forEach((subject) => {
// test files have actions as an array, so we convert them to a map as expected by the client
const actions: Record<string, ContextAttributes> = {};
subject.actions.forEach((action) => {
actions[action.actionKey] = {
numericAttributes: action.numericAttributes,
categoricalAttributes: action.categoricalAttributes,
};
});
// get the bandit assignment for the test case
const banditAssignment = client.getBanditAction(
flagKey,
subject.subjectKey,
subject.subjectAttributes,
actions,
defaultValue,
);
// Do this check in addition to assertions to provide helpful information on exactly which
// evaluation failed to produce an expected result
if (
banditAssignment.variation !== subject.assignment.variation ||
banditAssignment.action !== subject.assignment.action
) {
console.error(`Unexpected result for flag ${flagKey} and subject ${subject.subjectKey}`);
}
expect(banditAssignment.variation).toBe(subject.assignment.variation);
expect(banditAssignment.action).toBe(subject.assignment.action);
numAssignmentsChecked += 1;
});
// Ensure that this test case correctly checked some test assignments
expect(numAssignmentsChecked).toBeGreaterThan(0);
}
describe('bootstrapped client', () => {
const banditFlagsConfig = configurationFromString(
readMockConfigurationWireResponse(SHARED_BOOTSTRAP_BANDIT_FLAGS_FILE),
);
let client: EppoClient;
beforeAll(async () => {
client = new EppoClient({
flagConfigurationStore: new MemoryOnlyConfigurationStore(),
banditVariationConfigurationStore: new MemoryOnlyConfigurationStore(),
banditModelConfigurationStore: new MemoryOnlyConfigurationStore(),
});
client.setIsGracefulFailureMode(false);
// Bootstrap using the bandit flag config.
await client.bootstrap(banditFlagsConfig);
});
it.each(Object.keys(testCases))('Shared bandit test case - %s', async (fileName: string) => {
testBanditCaseAgainstClient(client, testCases[fileName]);
});
});
describe('traditional client', () => {
it.each(Object.keys(testCases))('Shared bandit test case - %s', async (fileName: string) => {
testBanditCaseAgainstClient(client, testCases[fileName]);
});
});
});
describe('Client-specific tests', () => {
const testStart = Date.now();
const flagKey = 'banner_bandit_flag'; // piggyback off a shared test data flag
const subjectKey = 'bob';
const subjectAttributes: Attributes = { age: 25, country: 'USA', gender_identity: 'female' };
const actions: Record<string, Attributes> = {
nike: { brand_affinity: 1.5, loyalty_tier: 'silver' },
adidas: { brand_affinity: -1.0, loyalty_tier: 'bronze' },
reebok: { brand_affinity: 0.5, loyalty_tier: 'gold' },
};
it('Passes the correct information to the logger', () => {
const banditAssignment = client.getBanditAction(
flagKey,
subjectKey,
subjectAttributes,
actions,
'control',
);
expect(banditAssignment.variation).toBe('banner_bandit');
expect(banditAssignment.action).toBe('adidas');
expect(mockLogAssignment).toHaveBeenCalledTimes(1);
const assignmentEvent: IAssignmentEvent = mockLogAssignment.mock.calls[0][0];
expect(new Date(assignmentEvent.timestamp).getTime()).toBeGreaterThanOrEqual(testStart);
expect(assignmentEvent.featureFlag).toBe(flagKey);
expect(assignmentEvent.allocation).toBe('training');
expect(assignmentEvent.experiment).toBe('banner_bandit_flag-training');
expect(assignmentEvent.variation).toBe('banner_bandit');
expect(assignmentEvent.subject).toBe(subjectKey);
expect(assignmentEvent.subjectAttributes).toStrictEqual(subjectAttributes);
expect(assignmentEvent.metaData?.obfuscated).toBe(false);
expect(mockLogBanditAction).toHaveBeenCalledTimes(1);
const banditEvent: IBanditEvent = mockLogBanditAction.mock.calls[0][0];
expect(new Date(banditEvent.timestamp).getTime()).toBeGreaterThanOrEqual(testStart);
expect(banditEvent.featureFlag).toBe(flagKey);
expect(banditEvent.bandit).toBe('banner_bandit');
expect(banditEvent.subject).toBe(subjectKey);
expect(banditEvent.action).toBe('adidas');
expect(banditEvent.actionProbability).toBeCloseTo(0.099);
expect(banditEvent.optimalityGap).toBe(7.1);
expect(banditEvent.modelVersion).toBe('123');
expect(banditEvent.subjectNumericAttributes).toStrictEqual({ age: 25 });
expect(banditEvent.subjectCategoricalAttributes).toStrictEqual({
country: 'USA',
gender_identity: 'female',
});
expect(banditEvent.actionNumericAttributes).toStrictEqual({ brand_affinity: -1 });
expect(banditEvent.actionCategoricalAttributes).toStrictEqual({ loyalty_tier: 'bronze' });
expect(banditEvent.metaData?.obfuscated).toBe(false);
expect(banditEvent.evaluationDetails?.configFetchedAt).toBeTruthy();
expect(typeof banditEvent.evaluationDetails?.configFetchedAt).toBe('string');
const expectedEvaluationDetails: IFlagEvaluationDetails = {
configFetchedAt: expect.any(String),
configPublishedAt: '2024-04-17T19:40:53.716Z',
environmentName: 'Test',
flagEvaluationCode: 'MATCH',
flagEvaluationDescription:
'bob belongs to the range of traffic assigned to "banner_bandit" defined in allocation "training".',
matchedAllocation: {
allocationEvaluationCode: AllocationEvaluationCode.MATCH,
key: 'training',
orderPosition: 2,
},
matchedRule: null,
unevaluatedAllocations: [],
unmatchedAllocations: [
{
allocationEvaluationCode: AllocationEvaluationCode.TRAFFIC_EXPOSURE_MISS,
key: 'analysis',
orderPosition: 1,
},
],
variationKey: 'banner_bandit',
variationValue: 'banner_bandit',
banditKey: 'banner_bandit',
banditAction: 'adidas',
};
expect(banditEvent.evaluationDetails).toEqual(expectedEvaluationDetails);
});
it('Flushed queued logging events when a logger is set', () => {
client.useLRUInMemoryAssignmentCache(5);
client.useExpiringInMemoryBanditAssignmentCache(5);
client.setAssignmentLogger(null as unknown as IAssignmentLogger);
client.setBanditLogger(null as unknown as IBanditLogger);
const banditAssignment = client.getBanditAction(
flagKey,
subjectKey,
subjectAttributes,
actions,
'control',
);
expect(banditAssignment.variation).toBe('banner_bandit');
expect(banditAssignment.action).toBe('adidas');
expect(mockLogAssignment).not.toHaveBeenCalled();
expect(mockLogBanditAction).not.toHaveBeenCalled();
const repeatAssignment = client.getBanditAction(
flagKey,
subjectKey,
subjectAttributes,
actions,
'control',
);
expect(repeatAssignment.variation).toBe('banner_bandit');
expect(repeatAssignment.action).toBe('adidas');
expect(mockLogAssignment).not.toHaveBeenCalled();
expect(mockLogBanditAction).not.toHaveBeenCalled();
client.setAssignmentLogger({ logAssignment: mockLogAssignment });
client.setBanditLogger({ logBanditAction: mockLogBanditAction });
expect(mockLogAssignment).toHaveBeenCalledTimes(1);
const assignmentEvent: IAssignmentEvent = mockLogAssignment.mock.calls[0][0];
expect(assignmentEvent.variation).toBe('banner_bandit');
expect(mockLogBanditAction).toHaveBeenCalledTimes(1);
const banditEvent: IBanditEvent = mockLogBanditAction.mock.calls[0][0];
expect(new Date(banditEvent.timestamp).getTime()).toBeGreaterThanOrEqual(testStart);
expect(banditEvent.action).toBe('adidas');
});
it('Logs assignment but not bandit action if no actions provided', () => {
const banditAssignment = client.getBanditAction(
'banner_bandit_flag',
'eve',
{},
{},
'control',
);
expect(banditAssignment.variation).toBe('banner_bandit');
expect(banditAssignment.action).toBeNull();
expect(mockLogAssignment).toHaveBeenCalledTimes(1);
expect(mockLogBanditAction).not.toHaveBeenCalled();
});
describe('Bandit evaluation errors', () => {
const testStart = Date.now();
beforeEach(() => {
jest
.spyOn(
(client as unknown as { banditEvaluator: BanditEvaluator }).banditEvaluator,
'evaluateBandit',
)
.mockImplementation(() => {
throw new Error('Intentional Error For Test');
});
});
it('Returns null action when graceful mode is on', () => {
client.setIsGracefulFailureMode(true);
const banditAssignment = client.getBanditActionDetails(
flagKey,
subjectKey,
subjectAttributes,
actions,
'control',
);
expect(banditAssignment.variation).toBe('banner_bandit');
expect(banditAssignment.action).toBeNull();
expect(
Date.parse(banditAssignment.evaluationDetails.configFetchedAt),
).toBeGreaterThanOrEqual(testStart);
const expectedEvaluationDetails: IFlagEvaluationDetails = {
configFetchedAt: expect.any(String),
configPublishedAt: '2024-04-17T19:40:53.716Z',
environmentName: 'Test',
flagEvaluationCode: 'BANDIT_ERROR',
flagEvaluationDescription: 'Error evaluating bandit action: Intentional Error For Test',
matchedAllocation: {
allocationEvaluationCode: AllocationEvaluationCode.MATCH,
key: 'training',
orderPosition: 2,
},
matchedRule: null,
unevaluatedAllocations: [],
unmatchedAllocations: [
{
allocationEvaluationCode: AllocationEvaluationCode.TRAFFIC_EXPOSURE_MISS,
key: 'analysis',
orderPosition: 1,
},
],
variationKey: 'banner_bandit',
variationValue: 'banner_bandit',
banditKey: 'banner_bandit',
banditAction: null,
};
expect(banditAssignment.evaluationDetails).toEqual(expectedEvaluationDetails);
});
it('Throws the error when graceful mode is off', () => {
client.setIsGracefulFailureMode(false); // Note: this is superfluous to beforeEach(), but done for clarity
expect(() =>
client.getBanditAction(flagKey, subjectKey, subjectAttributes, actions, 'control'),
).toThrow();
});
});
describe('Flexible arguments for attributes', () => {
// Note these mirror the test cases in test-case-banner-bandit.dynamic-typing.json
it('Can take non-contextual subject attributes', async () => {
// mirror test case in test-case-banner-bandit.dynamic-typing.json
const actions: Record<string, ContextAttributes> = {
nike: {
numericAttributes: { brand_affinity: -5 },
categoricalAttributes: { loyalty_tier: 'silver' },
},
adidas: {
numericAttributes: { brand_affinity: 1.0 },
categoricalAttributes: { loyalty_tier: 'bronze' },
},
reebok: {
numericAttributes: { brand_affinity: 20 },
categoricalAttributes: { loyalty_tier: 'gold' },
},
};
const subjectAttributesWithAreaCode: Attributes = {
age: 25,
mistake: 'oops',
country: 'USA',
gender_identity: 'female',
area_code: '303', // categorical area code
};
let banditAssignment = client.getBanditAction(
flagKey,
'henry',
subjectAttributesWithAreaCode,
actions,
'default',
);
expect(banditAssignment.action).toBe('adidas');
expect(banditAssignment.variation).toBe('banner_bandit');
// changing area code to a number should result in a different evaluation
subjectAttributesWithAreaCode.area_code = 303;
banditAssignment = client.getBanditAction(
flagKey,
'henry',
subjectAttributesWithAreaCode,
actions,
'default',
);
expect(banditAssignment.action).toBe('reebok');
expect(banditAssignment.variation).toBe('banner_bandit');
});
it('Can take non-contextual action attributes', async () => {
const actionsWithNonContextualAttributes: Record<string, Attributes> = {
nike: { brand_affinity: -15, loyalty_tier: 'silver', zip: '81427' },
adidas: { brand_affinity: 0.0, loyalty_tier: 'bronze' },
reebok: { brand_affinity: 15, loyalty_tier: 'gold' },
};
let banditAssignment = client.getBanditAction(
flagKey,
'imogene',
subjectAttributes,
actionsWithNonContextualAttributes,
'default',
);
expect(banditAssignment.action).toBe('nike');
expect(banditAssignment.variation).toBe('banner_bandit');
// changing zip code to a number should result in a different evaluation
actionsWithNonContextualAttributes.nike.zip = 81427;
banditAssignment = client.getBanditAction(
flagKey,
'imogene',
subjectAttributes,
actionsWithNonContextualAttributes,
'default',
);
expect(banditAssignment.action).toBe('adidas');
expect(banditAssignment.variation).toBe('banner_bandit');
});
it('Can take actions without any context', async () => {
const actionNamesOnly = ['nike', 'adidas', 'reebok'];
let banditAssignment = client.getBanditAction(
flagKey,
'imogene',
subjectAttributes,
actionNamesOnly,
'default',
);
expect(banditAssignment.action).toBe('nike');
expect(banditAssignment.variation).toBe('banner_bandit');
expect(mockLogBanditAction).toHaveBeenCalledTimes(1);
expect(mockLogBanditAction.mock.calls[0][0].actionProbability).toBeCloseTo(0.256);
// Duplicates should be ignored and not change anything
actionNamesOnly.push('nike');
banditAssignment = client.getBanditAction(
flagKey,
'imogene',
subjectAttributes,
actionNamesOnly,
'default',
);
expect(banditAssignment.action).toBe('nike');
expect(banditAssignment.variation).toBe('banner_bandit');
expect(mockLogBanditAction).toHaveBeenCalledTimes(2);
expect(mockLogBanditAction.mock.calls[1][0].actionProbability).toBeCloseTo(0.256);
});
});
describe('Best bandit action', () => {
it('Selects the highest scoring action', async () => {
const actions: Record<string, ContextAttributes> = {
nike: {
numericAttributes: { brand_affinity: 1.0 },
categoricalAttributes: {},
},
adidas: {
numericAttributes: { brand_affinity: 1.0 },
categoricalAttributes: {},
},
reebok: {
numericAttributes: { brand_affinity: 2.0 },
categoricalAttributes: {},
},
};
const subjectAttributesWithAreaCode: Attributes = {
age: 25,
mistake: 'oops',
country: 'USA',
gender_identity: 'female',
area_code: '303', // categorical area code
};
const banditAssignment = client.getBestAction(
flagKey,
subjectAttributesWithAreaCode,
actions,
'default',
);
expect(banditAssignment).toBe('adidas');
});
});
describe('Assignment logging deduplication', () => {
let mockEvaluateFlag: jest.SpyInstance;
let mockEvaluateBandit: jest.SpyInstance;
// The below two variables allow easily changing what the mock evaluation functions return throughout the test
let variationToReturn: string;
let actionToReturn: string | null;
// Convenience method for repeatedly making the exact same assignment call
function requestClientBanditAction(): Omit<IAssignmentDetails<string>, 'evaluationDetails'> {
return client.getBanditAction(
flagKey,
subjectKey,
subjectAttributes,
['toyota', 'honda'],
'control',
);
}
beforeAll(() => {
mockEvaluateFlag = jest
.spyOn(Evaluator.prototype, 'evaluateFlag')
.mockImplementation(() => {
return {
flagKey,
subjectKey,
subjectAttributes,
allocationKey: 'mock-allocation',
variation: { key: variationToReturn, value: variationToReturn },
extraLogging: {},
doLog: true,
flagEvaluationDetails: {
flagEvaluationCode: 'MATCH',
flagEvaluationDescription: 'Mocked evaluation',
},
} as FlagEvaluation;
});
mockEvaluateBandit = jest
.spyOn(BanditEvaluator.prototype, 'evaluateBandit')
.mockImplementation(() => {
return {
flagKey,
subjectKey,
subjectAttributes: { numericAttributes: {}, categoricalAttributes: {} },
actionKey: actionToReturn,
actionAttributes: { numericAttributes: {}, categoricalAttributes: {} },
actionScore: 10,
actionWeight: 0.5,
gamma: 1.0,
optimalityGap: 5,
} as BanditEvaluation;
});
});
beforeEach(() => {
client.useNonExpiringInMemoryAssignmentCache();
client.useNonExpiringInMemoryBanditAssignmentCache();
});
afterEach(() => {
client.disableAssignmentCache();
client.disableBanditAssignmentCache();
});
afterAll(() => {
mockEvaluateFlag.mockClear();
mockEvaluateBandit.mockClear();
jest.restoreAllMocks();
});
it('handles bandit actions appropriately', async () => {
// First assign to non-bandit variation
variationToReturn = 'non-bandit';
actionToReturn = null;
const firstNonBanditAssignment = requestClientBanditAction();
expect(firstNonBanditAssignment.variation).toBe('non-bandit');
expect(firstNonBanditAssignment.action).toBeNull();
expect(mockLogAssignment).toHaveBeenCalledTimes(1); // new variation assignment
expect(mockLogBanditAction).not.toHaveBeenCalled(); // no bandit assignment
// Assign bandit action
variationToReturn = 'banner_bandit';
actionToReturn = 'toyota';
const firstBanditAssignment = requestClientBanditAction();
expect(firstBanditAssignment.variation).toBe('banner_bandit');
expect(firstBanditAssignment.action).toBe('toyota');
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // new variation assignment
expect(mockLogBanditAction).toHaveBeenCalledTimes(1); // new bandit assignment
// Repeat bandit action assignment
variationToReturn = 'banner_bandit';
actionToReturn = 'toyota';
const secondBanditAssignment = requestClientBanditAction();
expect(secondBanditAssignment.variation).toBe('banner_bandit');
expect(secondBanditAssignment.action).toBe('toyota');
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // repeat variation assignment
expect(mockLogBanditAction).toHaveBeenCalledTimes(1); // repeat bandit assignment
// New bandit action assignment
variationToReturn = 'banner_bandit';
actionToReturn = 'honda';
const thirdBanditAssignment = requestClientBanditAction();
expect(thirdBanditAssignment.variation).toBe('banner_bandit');
expect(thirdBanditAssignment.action).toBe('honda');
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // repeat variation assignment
expect(mockLogBanditAction).toHaveBeenCalledTimes(2); // new bandit assignment
// Flip-flop to an earlier action assignment
variationToReturn = 'banner_bandit';
actionToReturn = 'toyota';
const fourthBanditAssignment = requestClientBanditAction();
expect(fourthBanditAssignment.variation).toBe('banner_bandit');
expect(fourthBanditAssignment.action).toBe('toyota');
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // repeat variation assignment
expect(mockLogBanditAction).toHaveBeenCalledTimes(3); // "new" bandit assignment
// Flip-flop back to non-bandit assignment
variationToReturn = 'non-bandit';
actionToReturn = null;
const secondNonBanditAssignment = requestClientBanditAction();
expect(secondNonBanditAssignment.variation).toBe('non-bandit');
expect(secondNonBanditAssignment.action).toBeNull();
expect(mockLogAssignment).toHaveBeenCalledTimes(3); // "new" variation assignment
expect(mockLogBanditAction).toHaveBeenCalledTimes(3); // no bandit assignment
});
});
});
describe('precomputed bandits', () => {
const bob = 'bob';
const bobInfo: ContextAttributes = {
numericAttributes: { age: 30, account_age: 10 },
categoricalAttributes: { country: 'UK', gender_identity: 'male' },
};
const bobActions: Record<string, BanditActions> = {
banner_bandit_flag: {
nike: {
numericAttributes: { brand_affinity: -2.5 },
categoricalAttributes: { loyalty_tier: 'bronze' },
},
adidas: {
numericAttributes: { brand_affinity: -2.5 },
categoricalAttributes: { loyalty_tier: 'bronze' },
},
reebok: {
numericAttributes: { brand_affinity: -2.5 },
categoricalAttributes: { loyalty_tier: 'bronze' },
},
},
};
function getPrecomputedResults(
client: EppoClient,
subjectKey: string,
subjectAttributes: ContextAttributes,
banditActions: Record<string, BanditActions>,
): IPrecomputedConfiguration {
const salt = base64.fromUint8Array(new Uint8Array([101, 112, 112, 111]));
const precomputedResults = client.getPrecomputedConfiguration(
subjectKey,
subjectAttributes,
banditActions,
salt,
);
const { precomputed } = JSON.parse(precomputedResults) as IConfigurationWire;
if (!precomputed) {
fail('precomputed result was not parsed');
}
return precomputed;
}
describe('obfuscated results', () => {
it('obfuscates precomputed bandits', () => {
const bannerBanditFlagMd5 = '3ac89e06235484aa6f2aec8c33109a02';
const brandAffinityB64 = 'YnJhbmRfYWZmaW5pdHk=';
const loyaltyTierB64 = 'bG95YWx0eV90aWVy';
const bronzeB64 = 'YnJvbnpl';
const adidasB64 = 'YWRpZGFz';
const modelB64 = 'MTIz'; // 123
const precomputed = getPrecomputedResults(client, bob, bobInfo, bobActions);
const response = JSON.parse(
precomputed.response,
) as IObfuscatedPrecomputedConfigurationResponse;
const numericAttrs = response.bandits[bannerBanditFlagMd5]['actionNumericAttributes'];
const categoricalAttrs =
response.bandits[bannerBanditFlagMd5]['actionCategoricalAttributes'];
expect(response.bandits[bannerBanditFlagMd5]['action']).toEqual(adidasB64);
expect(response.bandits[bannerBanditFlagMd5]['modelVersion']).toEqual(modelB64);
expect(categoricalAttrs[loyaltyTierB64]).toEqual(bronzeB64);
expect(numericAttrs[brandAffinityB64]).toEqual(attributeEncodeBase64(-2.5));
});
});
});
});