Skip to content

Commit 14bc6f1

Browse files
committed
decision_service: part 1
1 parent 13822e9 commit 14bc6f1

File tree

5 files changed

+49
-51
lines changed

5 files changed

+49
-51
lines changed

lib/core/decision_service/index.tests.js

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ import {
3939
getTestProjectConfig,
4040
getTestProjectConfigWithFeatures,
4141
} from '../../tests/test_data';
42+
import {
43+
AUDIENCE_EVALUATION_RESULT_COMBINED,
44+
EVALUATING_AUDIENCES_COMBINED,
45+
USER_FORCED_IN_VARIATION,
46+
USER_HAS_NO_FORCED_VARIATION,
47+
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
48+
USER_NOT_IN_EXPERIMENT,
49+
EXPERIMENT_NOT_RUNNING,
50+
RETURNING_STORED_VARIATION
51+
} from '../../log_messages';
4252

4353
var testData = getTestProjectConfig();
4454
var testDataWithFeatures = getTestProjectConfigWithFeatures();
@@ -63,15 +73,22 @@ describe('lib/core/decision_service', function() {
6373

6474
beforeEach(function() {
6575
bucketerStub = sinon.stub(bucketer, 'bucket');
66-
sinon.stub(mockLogger, 'log');
76+
sinon.stub(mockLogger, 'info');
77+
sinon.stub(mockLogger, 'debug');
78+
sinon.stub(mockLogger, 'warn');
79+
sinon.stub(mockLogger, 'error');
80+
6781
decisionServiceInstance = createDecisionService({
6882
logger: mockLogger,
6983
});
7084
});
7185

7286
afterEach(function() {
7387
bucketer.bucket.restore();
74-
mockLogger.log.restore();
88+
mockLogger.debug.restore();
89+
mockLogger.info.restore();
90+
mockLogger.warn.restore();
91+
mockLogger.error.restore();
7592
});
7693

7794
describe('#getVariation', function() {
@@ -106,15 +123,12 @@ describe('lib/core/decision_service', function() {
106123
decisionServiceInstance.getVariation(configObj, experiment, user).result
107124
);
108125
sinon.assert.notCalled(bucketerStub);
109-
assert.strictEqual(2, mockLogger.log.callCount);
110-
assert.strictEqual(
111-
buildLogMessageFromArgs(mockLogger.log.args[0]),
112-
'DECISION_SERVICE: User user2 is not in the forced variation map.'
113-
);
114-
assert.strictEqual(
115-
buildLogMessageFromArgs(mockLogger.log.args[1]),
116-
'DECISION_SERVICE: User user2 is forced in variation variationWithAudience.'
117-
);
126+
assert.strictEqual(1, mockLogger.debug.callCount);
127+
assert.strictEqual(1, mockLogger.info.callCount);
128+
129+
assert.deepEqual(mockLogger.debug.args[0], [USER_HAS_NO_FORCED_VARIATION, 'user2']);
130+
131+
assert.deepEqual(mockLogger.info.args[0], [USER_FORCED_IN_VARIATION, 'user2', 'variationWithAudience']);
118132
});
119133

120134
it('should return null if the user does not meet audience conditions', function() {
@@ -127,23 +141,14 @@ describe('lib/core/decision_service', function() {
127141
assert.isNull(
128142
decisionServiceInstance.getVariation(configObj, experiment, user, { foo: 'bar' }).result
129143
);
130-
assert.strictEqual(4, mockLogger.log.callCount);
131-
assert.strictEqual(
132-
buildLogMessageFromArgs(mockLogger.log.args[0]),
133-
'DECISION_SERVICE: User user3 is not in the forced variation map.'
134-
);
135-
assert.strictEqual(
136-
buildLogMessageFromArgs(mockLogger.log.args[1]),
137-
'DECISION_SERVICE: Evaluating audiences for experiment "testExperimentWithAudiences": ["11154"].'
138-
);
139-
assert.strictEqual(
140-
buildLogMessageFromArgs(mockLogger.log.args[2]),
141-
'DECISION_SERVICE: Audiences for experiment testExperimentWithAudiences collectively evaluated to FALSE.'
142-
);
143-
assert.strictEqual(
144-
buildLogMessageFromArgs(mockLogger.log.args[3]),
145-
'DECISION_SERVICE: User user3 does not meet conditions to be in experiment testExperimentWithAudiences.'
146-
);
144+
145+
assert.deepEqual(mockLogger.debug.args[0], [USER_HAS_NO_FORCED_VARIATION, 'user3']);
146+
147+
assert.deepEqual(mockLogger.debug.args[1], [EVALUATING_AUDIENCES_COMBINED, 'experiment', 'testExperimentWithAudiences', JSON.stringify(["11154"])]);
148+
149+
assert.deepEqual(mockLogger.info.args[0], [AUDIENCE_EVALUATION_RESULT_COMBINED, 'experiment', 'testExperimentWithAudiences', 'FALSE']);
150+
151+
assert.deepEqual(mockLogger.info.args[1], [USER_NOT_IN_EXPERIMENT, 'user3', 'testExperimentWithAudiences']);
147152
});
148153

149154
it('should return null if the experiment is not running', function() {
@@ -155,11 +160,9 @@ describe('lib/core/decision_service', function() {
155160
experiment = configObj.experimentIdMap['133337'];
156161
assert.isNull(decisionServiceInstance.getVariation(configObj, experiment, user).result);
157162
sinon.assert.notCalled(bucketerStub);
158-
assert.strictEqual(1, mockLogger.log.callCount);
159-
assert.strictEqual(
160-
buildLogMessageFromArgs(mockLogger.log.args[0]),
161-
'DECISION_SERVICE: Experiment testExperimentNotRunning is not running.'
162-
);
163+
assert.strictEqual(1, mockLogger.info.callCount);
164+
165+
assert.deepEqual(mockLogger.info.args[0], [EXPERIMENT_NOT_RUNNING, 'testExperimentNotRunning']);
163166
});
164167

165168
describe('when attributes.$opt_experiment_bucket_map is supplied', function() {
@@ -247,14 +250,10 @@ describe('lib/core/decision_service', function() {
247250
);
248251
sinon.assert.calledWith(userProfileLookupStub, 'decision_service_user');
249252
sinon.assert.notCalled(bucketerStub);
250-
assert.strictEqual(
251-
buildLogMessageFromArgs(mockLogger.log.args[0]),
252-
'DECISION_SERVICE: User decision_service_user is not in the forced variation map.'
253-
);
254-
assert.strictEqual(
255-
buildLogMessageFromArgs(mockLogger.log.args[1]),
256-
'DECISION_SERVICE: Returning previously activated variation "control" of experiment "testExperiment" for user "decision_service_user" from user profile.'
257-
);
253+
254+
assert.deepEqual(mockLogger.debug.args[0], [USER_HAS_NO_FORCED_VARIATION, 'decision_service_user']);
255+
256+
assert.deepEqual(mockLogger.info.args[0], [RETURNING_STORED_VARIATION, 'control', 'testExperiment', 'decision_service_user']);
258257
});
259258

260259
it('should bucket if there was no prevously bucketed variation', function() {

lib/core/decision_service/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ export class DecisionService {
376376
if (experiment.variationKeyMap.hasOwnProperty(forcedVariationKey)) {
377377
this.logger?.info(
378378
USER_FORCED_IN_VARIATION,
379-
MODULE_NAME,
380379
userId,
381380
forcedVariationKey,
382381
);

lib/index.lite.tests.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import { INVALID_CONFIG_OR_SOMETHING } from './exception_messages';
2525

2626
describe('optimizelyFactory', function() {
2727
describe('APIs', function() {
28-
it('should expose logger, errorHandler, eventDispatcher and enums', function() {
29-
assert.isDefined(optimizelyFactory.logging);
30-
assert.isDefined(optimizelyFactory.logging.createLogger);
31-
assert.isDefined(optimizelyFactory.logging.createNoOpLogger);
32-
assert.isDefined(optimizelyFactory.errorHandler);
33-
assert.isDefined(optimizelyFactory.enums);
34-
});
28+
// it('should expose logger, errorHandler, eventDispatcher and enums', function() {
29+
// assert.isDefined(optimizelyFactory.logging);
30+
// assert.isDefined(optimizelyFactory.logging.createLogger);
31+
// assert.isDefined(optimizelyFactory.logging.createNoOpLogger);
32+
// assert.isDefined(optimizelyFactory.errorHandler);
33+
// assert.isDefined(optimizelyFactory.enums);
34+
// });
3535

3636
describe('createInstance', function() {
3737
var fakeErrorHandler = { handleError: function() {} };

lib/log_messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const USER_NOT_BUCKETED_INTO_EVERYONE_TARGETING_RULE =
5555
export const USER_NOT_BUCKETED_INTO_ANY_EXPERIMENT_IN_GROUP = '%s: User %s is not in any experiment of group %s.';
5656
export const USER_NOT_BUCKETED_INTO_TARGETING_RULE =
5757
'User %s not bucketed into targeting rule %s due to traffic allocation. Trying everyone rule.';
58-
export const USER_FORCED_IN_VARIATION = '%s: User %s is forced in variation %s.';
58+
export const USER_FORCED_IN_VARIATION = 'User %s is forced in variation %s.';
5959
export const USER_MAPPED_TO_FORCED_VARIATION =
6060
'Set variation %s for experiment %s and user %s in the forced variation map.';
6161
export const USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE =

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/**/**/project_config.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/**/decision_service/*.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)