Skip to content

Commit 5c8a09e

Browse files
committed
decision: 3
1 parent e70b61f commit 5c8a09e

File tree

1 file changed

+74
-82
lines changed

1 file changed

+74
-82
lines changed

lib/core/decision_service/index.tests.js

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ import {
4747
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
4848
USER_NOT_IN_EXPERIMENT,
4949
EXPERIMENT_NOT_RUNNING,
50-
RETURNING_STORED_VARIATION
50+
RETURNING_STORED_VARIATION,
51+
FEATURE_HAS_NO_EXPERIMENTS,
52+
NO_ROLLOUT_EXISTS,
53+
USER_BUCKETED_INTO_TARGETING_RULE,
54+
USER_IN_ROLLOUT,
55+
USER_MEETS_CONDITIONS_FOR_TARGETING_RULE,
56+
USER_NOT_BUCKETED_INTO_TARGETING_RULE,
57+
USER_NOT_IN_ROLLOUT,
58+
VALID_BUCKETING_ID
5159
} from '../../log_messages';
5260
import { mock } from 'node:test';
61+
import { BUCKETING_ID_NOT_STRING } from '../../error_messages';
5362

5463
var testData = getTestProjectConfig();
5564
var testDataWithFeatures = getTestProjectConfigWithFeatures();
@@ -1209,15 +1218,22 @@ describe('lib/core/decision_service', function() {
12091218
};
12101219

12111220
beforeEach(function() {
1212-
sinon.stub(mockLogger, 'log');
1221+
sinon.stub(mockLogger, 'debug');
1222+
sinon.stub(mockLogger, 'info');
1223+
sinon.stub(mockLogger, 'warn');
1224+
sinon.stub(mockLogger, 'error');
1225+
12131226
configObj = projectConfig.createProjectConfig(cloneDeep(testData));
12141227
decisionService = createDecisionService({
12151228
logger: mockLogger,
12161229
});
12171230
});
12181231

12191232
afterEach(function() {
1220-
mockLogger.log.restore();
1233+
mockLogger.debug.restore();
1234+
mockLogger.info.restore();
1235+
mockLogger.warn.restore();
1236+
mockLogger.error.restore();
12211237
});
12221238

12231239
it('should return userId if bucketingId is not defined in user attributes', function() {
@@ -1227,17 +1243,13 @@ describe('lib/core/decision_service', function() {
12271243

12281244
it('should log warning in case of invalid bucketingId', function() {
12291245
assert.strictEqual(userId, decisionService.getBucketingId(userId, userAttributesWithInvalidBucketingId));
1230-
assert.strictEqual(1, mockLogger.log.callCount);
1231-
assert.strictEqual(
1232-
buildLogMessageFromArgs(mockLogger.log.args[0]),
1233-
'DECISION_SERVICE: BucketingID attribute is not a string. Defaulted to userId'
1234-
);
1246+
assert.deepEqual(mockLogger.warn.args[0], [BUCKETING_ID_NOT_STRING]);
12351247
});
12361248

12371249
it('should return correct bucketingId when provided in attributes', function() {
12381250
assert.strictEqual('123456789', decisionService.getBucketingId(userId, userAttributesWithBucketingId));
1239-
assert.strictEqual(1, mockLogger.log.callCount);
1240-
assert.strictEqual(buildLogMessageFromArgs(mockLogger.log.args[0]), 'DECISION_SERVICE: BucketingId is valid: "123456789"');
1251+
assert.strictEqual(1, mockLogger.debug.callCount);
1252+
assert.deepEqual(mockLogger.debug.args[0], [VALID_BUCKETING_ID, '123456789']);
12411253
});
12421254
});
12431255

@@ -1538,10 +1550,8 @@ describe('lib/core/decision_service', function() {
15381550
decisionSource: DECISION_SOURCES.ROLLOUT,
15391551
};
15401552
assert.deepEqual(decision, expectedDecision);
1541-
assert.strictEqual(
1542-
buildLogMessageFromArgs(mockLogger.log.lastCall.args),
1543-
'DECISION_SERVICE: User user1 is not in rollout of feature test_feature_for_experiment.'
1544-
);
1553+
1554+
assert.deepEqual(mockLogger.debug.lastCall.args, [USER_NOT_IN_ROLLOUT, 'user1', 'test_feature_for_experiment']);
15451555
});
15461556
});
15471557
});
@@ -1634,10 +1644,8 @@ describe('lib/core/decision_service', function() {
16341644
decisionSource: DECISION_SOURCES.ROLLOUT,
16351645
};
16361646
assert.deepEqual(decision, expectedDecision);
1637-
assert.strictEqual(
1638-
buildLogMessageFromArgs(mockLogger.log.lastCall.args),
1639-
'DECISION_SERVICE: User user1 is not in rollout of feature feature_with_group.'
1640-
);
1647+
1648+
assert.deepEqual(mockLogger.debug.lastCall.args, [USER_NOT_IN_ROLLOUT, 'user1', 'feature_with_group']);
16411649
});
16421650

16431651
it('returns null decision for group experiment not referenced by the feature', function() {
@@ -1650,10 +1658,9 @@ describe('lib/core/decision_service', function() {
16501658
};
16511659
assert.deepEqual(decision, expectedDecision);
16521660
sinon.assert.calledWithExactly(
1653-
mockLogger.log,
1654-
LOG_LEVEL.DEBUG,
1655-
'%s: There is no rollout of feature %s.',
1656-
'DECISION_SERVICE', 'feature_exp_no_traffic'
1661+
mockLogger.debug,
1662+
NO_ROLLOUT_EXISTS,
1663+
'feature_exp_no_traffic'
16571664
);
16581665
});
16591666
});
@@ -1784,23 +1791,20 @@ describe('lib/core/decision_service', function() {
17841791
};
17851792
assert.deepEqual(decision, expectedDecision);
17861793
sinon.assert.calledWithExactly(
1787-
mockLogger.log,
1788-
LOG_LEVEL.DEBUG,
1789-
'%s: User %s meets conditions for targeting rule %s.',
1790-
'DECISION_SERVICE', 'user1', 1
1794+
mockLogger.debug,
1795+
USER_MEETS_CONDITIONS_FOR_TARGETING_RULE,
1796+
'user1', 1
17911797
);
17921798
sinon.assert.calledWithExactly(
1793-
mockLogger.log,
1794-
LOG_LEVEL.DEBUG,
1795-
'%s: User %s bucketed into targeting rule %s.',
1796-
'DECISION_SERVICE', 'user1', 1
1799+
mockLogger.debug,
1800+
USER_BUCKETED_INTO_TARGETING_RULE,
1801+
'user1', 1
17971802

17981803
);
17991804
sinon.assert.calledWithExactly(
1800-
mockLogger.log,
1801-
LOG_LEVEL.DEBUG,
1802-
'%s: User %s is in rollout of feature %s.',
1803-
'DECISION_SERVICE', 'user1', 'test_feature'
1805+
mockLogger.debug,
1806+
USER_IN_ROLLOUT,
1807+
'user1', 'test_feature'
18041808
);
18051809
});
18061810
});
@@ -1922,22 +1926,19 @@ describe('lib/core/decision_service', function() {
19221926
};
19231927
assert.deepEqual(decision, expectedDecision);
19241928
sinon.assert.calledWithExactly(
1925-
mockLogger.log,
1926-
LOG_LEVEL.DEBUG,
1927-
'%s: User %s does not meet conditions for targeting rule %s.',
1928-
'DECISION_SERVICE', 'user1', 1
1929+
mockLogger.debug,
1930+
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
1931+
'user1', 1
19291932
);
19301933
sinon.assert.calledWithExactly(
1931-
mockLogger.log,
1932-
LOG_LEVEL.DEBUG,
1933-
'%s: User %s bucketed into targeting rule %s.',
1934-
'DECISION_SERVICE', 'user1', 'Everyone Else'
1934+
mockLogger.debug,
1935+
USER_BUCKETED_INTO_TARGETING_RULE,
1936+
'user1', 'Everyone Else'
19351937
);
19361938
sinon.assert.calledWithExactly(
1937-
mockLogger.log,
1938-
LOG_LEVEL.DEBUG,
1939-
'%s: User %s is in rollout of feature %s.',
1940-
'DECISION_SERVICE', 'user1', 'test_feature'
1939+
mockLogger.debug,
1940+
USER_IN_ROLLOUT,
1941+
'user1', 'test_feature'
19411942
);
19421943
});
19431944
});
@@ -1965,16 +1966,14 @@ describe('lib/core/decision_service', function() {
19651966
};
19661967
assert.deepEqual(decision, expectedDecision);
19671968
sinon.assert.calledWithExactly(
1968-
mockLogger.log,
1969-
LOG_LEVEL.DEBUG,
1970-
'%s: User %s does not meet conditions for targeting rule %s.',
1971-
'DECISION_SERVICE', 'user1', 1
1969+
mockLogger.debug,
1970+
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
1971+
'user1', 1
19721972
);
19731973
sinon.assert.calledWithExactly(
1974-
mockLogger.log,
1975-
LOG_LEVEL.DEBUG,
1976-
'%s: User %s is not in rollout of feature %s.',
1977-
'DECISION_SERVICE', 'user1', 'test_feature'
1974+
mockLogger.debug,
1975+
USER_NOT_IN_ROLLOUT,
1976+
'user1', 'test_feature'
19781977
);
19791978
});
19801979
});
@@ -2107,22 +2106,19 @@ describe('lib/core/decision_service', function() {
21072106
};
21082107
assert.deepEqual(decision, expectedDecision);
21092108
sinon.assert.calledWithExactly(
2110-
mockLogger.log,
2111-
LOG_LEVEL.DEBUG,
2112-
'%s: User %s meets conditions for targeting rule %s.',
2113-
'DECISION_SERVICE', 'user1', 1
2109+
mockLogger.debug,
2110+
USER_MEETS_CONDITIONS_FOR_TARGETING_RULE,
2111+
'user1', 1
21142112
);
21152113
sinon.assert.calledWithExactly(
2116-
mockLogger.log,
2117-
LOG_LEVEL.DEBUG,
2118-
'%s User %s not bucketed into targeting rule %s due to traffic allocation. Trying everyone rule.',
2119-
'DECISION_SERVICE', 'user1', 1
2114+
mockLogger.debug,
2115+
USER_NOT_BUCKETED_INTO_TARGETING_RULE,
2116+
'user1', 1
21202117
);
21212118
sinon.assert.calledWithExactly(
2122-
mockLogger.log,
2123-
LOG_LEVEL.DEBUG,
2124-
'%s: User %s bucketed into targeting rule %s.',
2125-
'DECISION_SERVICE', 'user1', 'Everyone Else'
2119+
mockLogger.debug,
2120+
USER_BUCKETED_INTO_TARGETING_RULE,
2121+
'user1', 'Everyone Else'
21262122
);
21272123
});
21282124
});
@@ -2226,16 +2222,14 @@ describe('lib/core/decision_service', function() {
22262222
};
22272223
assert.deepEqual(decision, expectedDecision);
22282224
sinon.assert.calledWithExactly(
2229-
mockLogger.log,
2230-
LOG_LEVEL.DEBUG,
2231-
'%s: User %s bucketed into targeting rule %s.',
2232-
'DECISION_SERVICE', 'user1', 'Everyone Else'
2225+
mockLogger.debug,
2226+
USER_BUCKETED_INTO_TARGETING_RULE,
2227+
'user1', 'Everyone Else'
22332228
);
22342229
sinon.assert.calledWithExactly(
2235-
mockLogger.log,
2236-
LOG_LEVEL.DEBUG,
2237-
'%s: User %s is in rollout of feature %s.',
2238-
'DECISION_SERVICE', 'user1', 'shared_feature'
2230+
mockLogger.debug,
2231+
USER_IN_ROLLOUT,
2232+
'user1', 'shared_feature'
22392233
);
22402234
});
22412235
});
@@ -2260,16 +2254,14 @@ describe('lib/core/decision_service', function() {
22602254
};
22612255
assert.deepEqual(decision, expectedDecision);
22622256
sinon.assert.calledWithExactly(
2263-
mockLogger.log,
2264-
LOG_LEVEL.DEBUG,
2265-
'%s: Feature %s is not attached to any experiments.',
2266-
'DECISION_SERVICE', 'unused_flag'
2257+
mockLogger.debug,
2258+
FEATURE_HAS_NO_EXPERIMENTS,
2259+
'unused_flag'
22672260
);
22682261
sinon.assert.calledWithExactly(
2269-
mockLogger.log,
2270-
LOG_LEVEL.DEBUG,
2271-
'%s: There is no rollout of feature %s.',
2272-
'DECISION_SERVICE', 'unused_flag'
2262+
mockLogger.debug,
2263+
NO_ROLLOUT_EXISTS,
2264+
'unused_flag'
22732265
);
22742266
});
22752267
});

0 commit comments

Comments
 (0)