Skip to content

Commit df4872c

Browse files
committed
fix test
1 parent a479380 commit df4872c

File tree

7 files changed

+351
-208
lines changed

7 files changed

+351
-208
lines changed

lib/core/decision_service/cmab/cmab_service.spec.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('DefaultCmabService', () => {
6868
});
6969

7070
const ruleId = '1234';
71-
const variation = await cmabService.getDecision(projectConfig, userContext, ruleId, []);
71+
const variation = await cmabService.getDecision(projectConfig, userContext, ruleId, {});
7272

7373
expect(variation.variationId).toEqual('123');
7474
expect(uuidValidate(variation.cmabUuid)).toBe(true);
@@ -101,8 +101,8 @@ describe('DefaultCmabService', () => {
101101
gender: 'male'
102102
});
103103

104-
await cmabService.getDecision(projectConfig, userContext, '1234', []);
105-
await cmabService.getDecision(projectConfig, userContext, '5678', []);
104+
await cmabService.getDecision(projectConfig, userContext, '1234', {});
105+
await cmabService.getDecision(projectConfig, userContext, '5678', {});
106106

107107
expect(mockCmabClient.fetchDecision).toHaveBeenCalledTimes(2);
108108
expect(mockCmabClient.fetchDecision.mock.calls[0][2]).toEqual({
@@ -136,7 +136,7 @@ describe('DefaultCmabService', () => {
136136
gender: 'male'
137137
});
138138

139-
const variation11 = await cmabService.getDecision(projectConfig, userContext11, '1234', []);
139+
const variation11 = await cmabService.getDecision(projectConfig, userContext11, '1234', {});
140140

141141
const userContext12 = mockUserContext('user123', {
142142
country: 'US',
@@ -145,7 +145,7 @@ describe('DefaultCmabService', () => {
145145
gender: 'female'
146146
});
147147

148-
const variation12 = await cmabService.getDecision(projectConfig, userContext12, '1234', []);
148+
const variation12 = await cmabService.getDecision(projectConfig, userContext12, '1234', {});
149149
expect(variation11.variationId).toEqual('123');
150150
expect(variation12.variationId).toEqual('123');
151151
expect(variation11.cmabUuid).toEqual(variation12.cmabUuid);
@@ -157,14 +157,14 @@ describe('DefaultCmabService', () => {
157157
age: '30',
158158
});
159159

160-
const variation21 = await cmabService.getDecision(projectConfig, userContext21, '5678', []);
160+
const variation21 = await cmabService.getDecision(projectConfig, userContext21, '5678', {});
161161

162162
const userContext22 = mockUserContext('user456', {
163163
country: 'BD',
164164
age: '35',
165165
});
166166

167-
const variation22 = await cmabService.getDecision(projectConfig, userContext22, '5678', []);
167+
const variation22 = await cmabService.getDecision(projectConfig, userContext22, '5678', {});
168168
expect(variation21.variationId).toEqual('456');
169169
expect(variation22.variationId).toEqual('456');
170170
expect(variation21.cmabUuid).toEqual(variation22.cmabUuid);
@@ -192,7 +192,7 @@ describe('DefaultCmabService', () => {
192192
gender: 'male'
193193
});
194194

195-
const variation11 = await cmabService.getDecision(projectConfig, userContext11, '1234', []);
195+
const variation11 = await cmabService.getDecision(projectConfig, userContext11, '1234', {});
196196

197197
const userContext12 = mockUserContext('user123', {
198198
gender: 'female',
@@ -201,7 +201,7 @@ describe('DefaultCmabService', () => {
201201
age: '25',
202202
});
203203

204-
const variation12 = await cmabService.getDecision(projectConfig, userContext12, '1234', []);
204+
const variation12 = await cmabService.getDecision(projectConfig, userContext12, '1234', {});
205205
expect(variation11.variationId).toEqual('123');
206206
expect(variation12.variationId).toEqual('123');
207207
expect(variation11.cmabUuid).toEqual(variation12.cmabUuid);
@@ -227,9 +227,9 @@ describe('DefaultCmabService', () => {
227227
age: '25',
228228
});
229229

230-
const variation1 = await cmabService.getDecision(projectConfig, userContext, '1234', []);
230+
const variation1 = await cmabService.getDecision(projectConfig, userContext, '1234', {});
231231

232-
const variation2 = await cmabService.getDecision(projectConfig, userContext, '5678', []);
232+
const variation2 = await cmabService.getDecision(projectConfig, userContext, '5678', {});
233233

234234
expect(variation1.variationId).toEqual('123');
235235
expect(variation2.variationId).toEqual('456');
@@ -260,9 +260,9 @@ describe('DefaultCmabService', () => {
260260
age: '25',
261261
});
262262

263-
const variation1 = await cmabService.getDecision(projectConfig, userContext1, '1234', []);
263+
const variation1 = await cmabService.getDecision(projectConfig, userContext1, '1234', {});
264264

265-
const variation2 = await cmabService.getDecision(projectConfig, userContext2, '1234', []);
265+
const variation2 = await cmabService.getDecision(projectConfig, userContext2, '1234', {});
266266
expect(variation1.variationId).toEqual('123');
267267
expect(variation2.variationId).toEqual('456');
268268
expect(variation1.cmabUuid).not.toEqual(variation2.cmabUuid);
@@ -289,7 +289,7 @@ describe('DefaultCmabService', () => {
289289
gender: 'male'
290290
});
291291

292-
const variation1 = await cmabService.getDecision(projectConfig, userContext1, '1234', []);
292+
const variation1 = await cmabService.getDecision(projectConfig, userContext1, '1234', {});
293293

294294
const userContext2 = mockUserContext('user123', {
295295
country: 'US',
@@ -298,7 +298,7 @@ describe('DefaultCmabService', () => {
298298
gender: 'male'
299299
});
300300

301-
const variation2 = await cmabService.getDecision(projectConfig, userContext2, '1234', []);
301+
const variation2 = await cmabService.getDecision(projectConfig, userContext2, '1234', {});
302302
expect(variation1.variationId).toEqual('123');
303303
expect(variation2.variationId).toEqual('456');
304304
expect(variation1.cmabUuid).not.toEqual(variation2.cmabUuid);
@@ -325,13 +325,13 @@ describe('DefaultCmabService', () => {
325325
gender: 'male'
326326
});
327327

328-
const variation1 = await cmabService.getDecision(projectConfig, userContext, '1234', []);
328+
const variation1 = await cmabService.getDecision(projectConfig, userContext, '1234', {});
329329

330-
const variation2 = await cmabService.getDecision(projectConfig, userContext, '1234', [
331-
OptimizelyDecideOption.IGNORE_CMAB_CACHE,
332-
]);
330+
const variation2 = await cmabService.getDecision(projectConfig, userContext, '1234', {
331+
[OptimizelyDecideOption.IGNORE_CMAB_CACHE]: true,
332+
});
333333

334-
const variation3 = await cmabService.getDecision(projectConfig, userContext, '1234', []);
334+
const variation3 = await cmabService.getDecision(projectConfig, userContext, '1234', {});
335335

336336
expect(variation1.variationId).toEqual('123');
337337
expect(variation2.variationId).toEqual('456');
@@ -367,18 +367,19 @@ describe('DefaultCmabService', () => {
367367
age: '50'
368368
});
369369

370-
const variation1 = await cmabService.getDecision(projectConfig, userContext1, '1234', []);
370+
const variation1 = await cmabService.getDecision(projectConfig, userContext1, '1234', {});
371371
expect(variation1.variationId).toEqual('123');
372372

373-
const variation2 = await cmabService.getDecision(projectConfig, userContext2, '1234', []);
373+
const variation2 = await cmabService.getDecision(projectConfig, userContext2, '1234', {});
374374
expect(variation2.variationId).toEqual('456');
375375

376-
const variation3 = await cmabService.getDecision(projectConfig, userContext1, '1234', [
377-
OptimizelyDecideOption.RESET_CMAB_CACHE,
378-
]);
376+
const variation3 = await cmabService.getDecision(projectConfig, userContext1, '1234', {
377+
[OptimizelyDecideOption.RESET_CMAB_CACHE]: true,
378+
});
379+
379380
expect(variation3.variationId).toEqual('789');
380381

381-
const variation4 = await cmabService.getDecision(projectConfig, userContext2, '1234', []);
382+
const variation4 = await cmabService.getDecision(projectConfig, userContext2, '1234', {});
382383
expect(variation4.variationId).toEqual('101112');
383384
});
384385

@@ -401,13 +402,13 @@ describe('DefaultCmabService', () => {
401402
gender: 'male'
402403
});
403404

404-
const variation1 = await cmabService.getDecision(projectConfig, userContext, '1234', []);
405+
const variation1 = await cmabService.getDecision(projectConfig, userContext, '1234', {});
405406

406-
const variation2 = await cmabService.getDecision(projectConfig, userContext, '1234', [
407-
OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE,
408-
]);
407+
const variation2 = await cmabService.getDecision(projectConfig, userContext, '1234', {
408+
[OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE]: true,
409+
});
409410

410-
const variation3 = await cmabService.getDecision(projectConfig, userContext, '1234', []);
411+
const variation3 = await cmabService.getDecision(projectConfig, userContext, '1234', {});
411412

412413
expect(variation1.variationId).toEqual('123');
413414
expect(variation2.variationId).toEqual('456');

lib/core/decision_service/cmab/cmab_service.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616

1717
import { LoggerFacade } from "../../../logging/logger";
18-
import OptimizelyUserContext from "../../../optimizely_user_context"
18+
// import OptimizelyUserContext from "../../../optimizely_user_context"
19+
import { IOptimizelyUserContext } from "../../../optimizely_user_context";
1920
import { ProjectConfig } from "../../../project_config/project_config"
2021
import { OptimizelyDecideOption, UserAttributes } from "../../../shared_types"
2122
import { Cache } from "../../../utils/cache/cache";
2223
import { CmabClient } from "./cmab_client";
2324
import { v4 as uuidV4 } from 'uuid';
2425
import murmurhash from "murmurhash";
25-
import { a } from "vitest/dist/chunks/suite.CcK46U-P";
26+
import { DecideOptionsMap } from "..";
2627

2728
export type CmabDecision = {
2829
variationId: string,
@@ -32,16 +33,16 @@ export type CmabDecision = {
3233
export interface CmabService {
3334
/**
3435
* Get variation id for the user
35-
* @param {OptimizelyUserContext} userContext
36+
* @param {IOptimizelyUserContext} userContext
3637
* @param {string} ruleId
3738
* @param {OptimizelyDecideOption[]} options
3839
* @return {Promise<CmabDecision>}
3940
*/
4041
getDecision(
4142
projectConfig: ProjectConfig,
42-
userContext: OptimizelyUserContext,
43+
userContext: IOptimizelyUserContext,
4344
ruleId: string,
44-
options: OptimizelyDecideOption[]
45+
options: DecideOptionsMap,
4546
): Promise<CmabDecision>
4647
}
4748

@@ -70,23 +71,23 @@ export class DefaultCmabService implements CmabService {
7071

7172
async getDecision(
7273
projectConfig: ProjectConfig,
73-
userContext: OptimizelyUserContext,
74+
userContext: IOptimizelyUserContext,
7475
ruleId: string,
75-
options: OptimizelyDecideOption[]
76+
options: DecideOptionsMap,
7677
): Promise<CmabDecision> {
7778
const filteredAttributes = this.filterAttributes(projectConfig, userContext, ruleId);
7879

79-
if (options.includes(OptimizelyDecideOption.IGNORE_CMAB_CACHE)) {
80+
if (options[OptimizelyDecideOption.IGNORE_CMAB_CACHE]) {
8081
return this.fetchDecision(ruleId, userContext.getUserId(), filteredAttributes);
8182
}
8283

83-
if (options.includes(OptimizelyDecideOption.RESET_CMAB_CACHE)) {
84+
if (options[OptimizelyDecideOption.RESET_CMAB_CACHE]) {
8485
this.cmabCache.clear();
8586
}
8687

8788
const cacheKey = this.getCacheKey(userContext.getUserId(), ruleId);
8889

89-
if (options.includes(OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE)) {
90+
if (options[OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE]) {
9091
this.cmabCache.remove(cacheKey);
9192
}
9293

@@ -125,7 +126,7 @@ export class DefaultCmabService implements CmabService {
125126

126127
private filterAttributes(
127128
projectConfig: ProjectConfig,
128-
userContext: OptimizelyUserContext,
129+
userContext: IOptimizelyUserContext,
129130
ruleId: string
130131
): UserAttributes {
131132
const filteredAttributes: UserAttributes = {};

0 commit comments

Comments
 (0)