Skip to content

Commit 5557a04

Browse files
committed
fix
1 parent 5a18cc3 commit 5557a04

File tree

3 files changed

+90
-34
lines changed

3 files changed

+90
-34
lines changed

lib/core/audience_evaluator/index.tests.js

Lines changed: 88 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { sprintf } from '../../utils/fns';
2121
import AudienceEvaluator, { createAudienceEvaluator } from './index';
2222
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
2323
import * as customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator';
24+
import { AUDIENCE_EVALUATION_RESULT, EVALUATING_AUDIENCE } from '../../log_messages';
25+
// import { getEvaluator } from '../custom_attribute_condition_evaluator';
2426

2527
var buildLogMessageFromArgs = args => sprintf(args[1], ...args.splice(2));
2628
var mockLogger = {
@@ -181,7 +183,6 @@ describe('lib/core/audience_evaluator', function() {
181183

182184
beforeEach(function() {
183185
sandbox.stub(conditionTreeEvaluator, 'evaluate');
184-
sandbox.stub(customAttributeConditionEvaluator, 'evaluate');
185186
});
186187

187188
afterEach(function() {
@@ -210,26 +211,40 @@ describe('lib/core/audience_evaluator', function() {
210211
conditionTreeEvaluator.evaluate.callsFake(function(conditions, leafEvaluator) {
211212
return leafEvaluator(conditions[1]);
212213
});
213-
customAttributeConditionEvaluator.evaluate.returns(false);
214+
215+
const mockCustomAttributeConditionEvaluator = sinon.stub().returns(false);
216+
217+
sinon.stub(customAttributeConditionEvaluator, 'getEvaluator').returns({
218+
evaluate: mockCustomAttributeConditionEvaluator,
219+
});
220+
221+
const audienceEvaluator = createAudienceEvaluator();
222+
214223
var userAttributes = { device_model: 'android' };
215224
var user = getMockUserContext(userAttributes);
216225
var result = audienceEvaluator.evaluate(['or', '1'], audiencesById, user);
217-
sinon.assert.calledOnce(customAttributeConditionEvaluator.evaluate);
226+
sinon.assert.calledOnce(mockCustomAttributeConditionEvaluator);
218227
sinon.assert.calledWithExactly(
219-
customAttributeConditionEvaluator.evaluate,
228+
mockCustomAttributeConditionEvaluator,
220229
iphoneUserAudience.conditions[1],
221230
user,
222231
);
223232
assert.isFalse(result);
233+
234+
customAttributeConditionEvaluator.getEvaluator.restore();
224235
});
225236
});
226237

227238
describe('Audience evaluation logging', function() {
228239
var sandbox = sinon.sandbox.create();
240+
var mockCustomAttributeConditionEvaluator;
229241

230242
beforeEach(function() {
243+
mockCustomAttributeConditionEvaluator = sinon.stub();
231244
sandbox.stub(conditionTreeEvaluator, 'evaluate');
232-
sandbox.stub(customAttributeConditionEvaluator, 'evaluate');
245+
sandbox.stub(customAttributeConditionEvaluator, 'getEvaluator').returns({
246+
evaluate: mockCustomAttributeConditionEvaluator,
247+
});
233248
});
234249

235250
afterEach(function() {
@@ -240,69 +255,110 @@ describe('lib/core/audience_evaluator', function() {
240255
conditionTreeEvaluator.evaluate.callsFake(function(conditions, leafEvaluator) {
241256
return leafEvaluator(conditions[1]);
242257
});
243-
customAttributeConditionEvaluator.evaluate.returns(null);
258+
259+
mockCustomAttributeConditionEvaluator.returns(null);
244260
var userAttributes = { device_model: 5.5 };
245261
var user = getMockUserContext(userAttributes);
262+
263+
const audienceEvaluator = createAudienceEvaluator({}, mockLogger);
264+
246265
var result = audienceEvaluator.evaluate(['or', '1'], audiencesById, user);
247-
sinon.assert.calledOnce(customAttributeConditionEvaluator.evaluate);
266+
267+
sinon.assert.calledOnce(mockCustomAttributeConditionEvaluator);
248268
sinon.assert.calledWithExactly(
249-
customAttributeConditionEvaluator.evaluate,
269+
mockCustomAttributeConditionEvaluator,
250270
iphoneUserAudience.conditions[1],
251271
user
252272
);
253273
assert.isFalse(result);
254-
assert.strictEqual(2, mockLogger.log.callCount);
255-
assert.strictEqual(
256-
buildLogMessageFromArgs(mockLogger.log.args[0]),
257-
'AUDIENCE_EVALUATOR: Starting to evaluate audience "1" with conditions: ["and",{"name":"device_model","value":"iphone","type":"custom_attribute"}].'
258-
);
259-
assert.strictEqual(buildLogMessageFromArgs(mockLogger.log.args[1]), 'AUDIENCE_EVALUATOR: Audience "1" evaluated to UNKNOWN.');
274+
assert.strictEqual(2, mockLogger.debug.callCount);
275+
276+
sinon.assert.calledWithExactly(
277+
mockLogger.debug,
278+
EVALUATING_AUDIENCE,
279+
'1',
280+
JSON.stringify(['and', iphoneUserAudience.conditions[1]])
281+
)
282+
283+
sinon.assert.calledWithExactly(
284+
mockLogger.debug,
285+
AUDIENCE_EVALUATION_RESULT,
286+
'1',
287+
'UNKNOWN'
288+
)
260289
});
261290

262291
it('logs correctly when conditionTreeEvaluator.evaluate returns true', function() {
263292
conditionTreeEvaluator.evaluate.callsFake(function(conditions, leafEvaluator) {
264293
return leafEvaluator(conditions[1]);
265294
});
266-
customAttributeConditionEvaluator.evaluate.returns(true);
295+
296+
mockCustomAttributeConditionEvaluator.returns(true);
297+
267298
var userAttributes = { device_model: 'iphone' };
268299
var user = getMockUserContext(userAttributes);
300+
301+
const audienceEvaluator = createAudienceEvaluator({}, mockLogger);
302+
269303
var result = audienceEvaluator.evaluate(['or', '1'], audiencesById, user);
270-
sinon.assert.calledOnce(customAttributeConditionEvaluator.evaluate);
304+
sinon.assert.calledOnce(mockCustomAttributeConditionEvaluator);
271305
sinon.assert.calledWithExactly(
272-
customAttributeConditionEvaluator.evaluate,
306+
mockCustomAttributeConditionEvaluator,
273307
iphoneUserAudience.conditions[1],
274308
user,
275309
);
276310
assert.isTrue(result);
277-
assert.strictEqual(2, mockLogger.log.callCount);
278-
assert.strictEqual(
279-
buildLogMessageFromArgs(mockLogger.log.args[0]),
280-
'AUDIENCE_EVALUATOR: Starting to evaluate audience "1" with conditions: ["and",{"name":"device_model","value":"iphone","type":"custom_attribute"}].'
281-
);
282-
assert.strictEqual(buildLogMessageFromArgs(mockLogger.log.args[1]), 'AUDIENCE_EVALUATOR: Audience "1" evaluated to TRUE.');
311+
assert.strictEqual(2, mockLogger.debug.callCount);
312+
sinon.assert.calledWithExactly(
313+
mockLogger.debug,
314+
EVALUATING_AUDIENCE,
315+
'1',
316+
JSON.stringify(['and', iphoneUserAudience.conditions[1]])
317+
)
318+
319+
sinon.assert.calledWithExactly(
320+
mockLogger.debug,
321+
AUDIENCE_EVALUATION_RESULT,
322+
'1',
323+
'TRUE'
324+
)
283325
});
284326

285327
it('logs correctly when conditionTreeEvaluator.evaluate returns false', function() {
286328
conditionTreeEvaluator.evaluate.callsFake(function(conditions, leafEvaluator) {
287329
return leafEvaluator(conditions[1]);
288330
});
289-
customAttributeConditionEvaluator.evaluate.returns(false);
331+
332+
mockCustomAttributeConditionEvaluator.returns(false);
333+
290334
var userAttributes = { device_model: 'android' };
291335
var user = getMockUserContext(userAttributes);
336+
337+
const audienceEvaluator = createAudienceEvaluator({}, mockLogger);
338+
292339
var result = audienceEvaluator.evaluate(['or', '1'], audiencesById, user);
293-
sinon.assert.calledOnce(customAttributeConditionEvaluator.evaluate);
340+
sinon.assert.calledOnce(mockCustomAttributeConditionEvaluator);
294341
sinon.assert.calledWithExactly(
295-
customAttributeConditionEvaluator.evaluate,
342+
mockCustomAttributeConditionEvaluator,
296343
iphoneUserAudience.conditions[1],
297344
user,
298345
);
299346
assert.isFalse(result);
300-
assert.strictEqual(2, mockLogger.log.callCount);
301-
assert.strictEqual(
302-
buildLogMessageFromArgs(mockLogger.log.args[0]),
303-
'AUDIENCE_EVALUATOR: Starting to evaluate audience "1" with conditions: ["and",{"name":"device_model","value":"iphone","type":"custom_attribute"}].'
304-
);
305-
assert.strictEqual(buildLogMessageFromArgs(mockLogger.log.args[1]), 'AUDIENCE_EVALUATOR: Audience "1" evaluated to FALSE.');
347+
assert.strictEqual(2, mockLogger.debug.callCount);
348+
349+
sinon.assert.calledWithExactly(
350+
mockLogger.debug,
351+
EVALUATING_AUDIENCE,
352+
'1',
353+
JSON.stringify(['and', iphoneUserAudience.conditions[1]])
354+
)
355+
356+
sinon.assert.calledWithExactly(
357+
mockLogger.debug,
358+
AUDIENCE_EVALUATION_RESULT,
359+
'1',
360+
'FALSE'
361+
)
306362
});
307363
});
308364
});

lib/log_messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const VARIATION_REMOVED_FOR_USER = 'Variation mapped to experiment %s has
9292
export const VALID_BUCKETING_ID = 'BucketingId is valid: "%s"';
9393
export const EVALUATING_AUDIENCE = 'Starting to evaluate audience "%s" with conditions: %s.';
9494
export const EVALUATING_AUDIENCES_COMBINED = 'Evaluating audiences for %s "%s": %s.';
95-
export const AUDIENCE_EVALUATION_RESULT = '%s: Audience "%s" evaluated to %s.';
95+
export const AUDIENCE_EVALUATION_RESULT = 'Audience "%s" evaluated to %s.';
9696
export const AUDIENCE_EVALUATION_RESULT_COMBINED = 'Audiences for %s %s collectively evaluated to %s.';
9797
export const MISSING_ATTRIBUTE_VALUE =
9898
'Audience condition %s evaluated to UNKNOWN because no value was passed for user attribute "%s".';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"clean:win": "(if exist dist rd /s/q dist)",
7575
"lint": "tsc --noEmit && eslint 'lib/**/*.js' 'lib/**/*.ts'",
7676
"test-vitest": "tsc --noEmit --p tsconfig.spec.json && vitest run",
77-
"test-mocha": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/**/*.tests.ts' 'lib/**/optimizely/index.tests.js'",
77+
"test-mocha": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/**/*.tests.ts' 'lib/**/*.tests.js'",
7878
"test": "npm run test-mocha && npm run test-vitest",
7979
"posttest": "npm run lint",
8080
"test-ci": "npm run test-xbrowser && npm run test-umdbrowser",

0 commit comments

Comments
 (0)