diff --git a/packages/optimizely-sdk/lib/core/decision_service/index.js b/packages/optimizely-sdk/lib/core/decision_service/index.js index be26bb2f0..64fc0bbe9 100644 --- a/packages/optimizely-sdk/lib/core/decision_service/index.js +++ b/packages/optimizely-sdk/lib/core/decision_service/index.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2017-2018, Optimizely, Inc. and contributors * + * Copyright 2017-2019, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -315,7 +315,7 @@ DecisionService.prototype._getVariationForFeatureExperiment = function(feature, var group = this.configObj.groupIdMap[feature.groupId]; if (group) { experiment = this._getExperimentInGroup(group, userId); - if (experiment) { + if (experiment && feature.experimentIds.indexOf(experiment.id) !== -1) { variationKey = this.getVariation(experiment.key, userId, attributes); } } diff --git a/packages/optimizely-sdk/lib/core/decision_service/index.tests.js b/packages/optimizely-sdk/lib/core/decision_service/index.tests.js index 2bb4b74c9..356afca4f 100644 --- a/packages/optimizely-sdk/lib/core/decision_service/index.tests.js +++ b/packages/optimizely-sdk/lib/core/decision_service/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2017-2018, Optimizely, Inc. and contributors * + * Copyright 2017-2019, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -889,6 +889,18 @@ describe('lib/core/decision_service', function() { assert.deepEqual(decision, expectedDecision); sinon.assert.calledWithExactly(mockLogger.log, LOG_LEVEL.DEBUG, 'DECISION_SERVICE: User user1 is not in any experiment on the feature feature_with_group.'); }); + + it('returns null decision for group experiment not referenced by the feature', function() { + var noTrafficExpFeature = configObj.featureKeyMap.feature_exp_no_traffic; + var decision = decisionServiceInstance.getVariationForFeature(noTrafficExpFeature, 'user1'); + var expectedDecision = { + experiment: null, + variation: null, + decisionSource: null, + }; + assert.deepEqual(decision, expectedDecision); + sinon.assert.calledWithExactly(mockLogger.log, LOG_LEVEL.DEBUG, 'DECISION_SERVICE: User user1 is not in any experiment on the feature feature_exp_no_traffic.'); + }); }); describe('user not bucketed into the group', function() { diff --git a/packages/optimizely-sdk/lib/optimizely/index.tests.js b/packages/optimizely-sdk/lib/optimizely/index.tests.js index d79e49585..f7d4e2f57 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.tests.js +++ b/packages/optimizely-sdk/lib/optimizely/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2016-2018, Optimizely, Inc. and contributors * + * Copyright 2016-2019, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -2991,7 +2991,7 @@ describe('lib/optimizely', function() { assert.strictEqual(result.length, 2); assert.isAbove(result.indexOf('test_feature'), -1); assert.isAbove(result.indexOf('test_feature_for_experiment'), -1); - sinon.assert.callCount(optlyInstance.isFeatureEnabled, 6); + sinon.assert.callCount(optlyInstance.isFeatureEnabled, 7); sinon.assert.calledWithExactly( optlyInstance.isFeatureEnabled, 'test_feature', @@ -3028,6 +3028,12 @@ describe('lib/optimizely', function() { 'user1', attributes ); + sinon.assert.calledWithExactly( + optlyInstance.isFeatureEnabled, + 'feature_exp_no_traffic', + 'user1', + attributes + ); }); }); diff --git a/packages/optimizely-sdk/lib/tests/test_data.js b/packages/optimizely-sdk/lib/tests/test_data.js index 79f7eaaa7..bfac5e239 100644 --- a/packages/optimizely-sdk/lib/tests/test_data.js +++ b/packages/optimizely-sdk/lib/tests/test_data.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2018, Optimizely + * Copyright 2016-2019, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -429,6 +429,15 @@ var configWithFeatures = { 'id': '599110', 'experimentIds': [], 'variables': [] + }, + { + 'rolloutId': '', + 'key': 'feature_exp_no_traffic', + 'id': '4482920079', + 'experimentIds': [ + '12115595439' + ], + 'variables': [] } ], 'experiments': [ @@ -637,6 +646,74 @@ var configWithFeatures = { 'entityId': '599082' } ] + }, + { + 'policy': 'random', + 'id': '595025', + 'experiments': [ + { + 'trafficAllocation': [ + { + 'endOfRange': 10000, + 'entityId': '12098126627' + } + ], + 'layerId': '595005', + 'forcedVariations': {}, + 'audienceIds': [], + 'variations': [ + { + 'key': 'all_traffic_variation', + 'id': '12098126627', + 'variables': [] + }, + { + 'key': 'no_traffic_variation', + 'id': '12098126628', + 'variables': [] + } + ], + 'status': 'Running', + 'key': 'all_traffic_experiment', + 'id': '12198292375' + }, + { + 'trafficAllocation': [ + { + 'endOfRange': 5000, + 'entityId': '12098126629' + }, + { + 'endOfRange': 10000, + 'entityId': '12098126630' + } + ], + 'layerId': '12187694826', + 'forcedVariations': {}, + 'audienceIds': [], + 'variations': [ + { + 'key': 'variation_5000', + 'id': '12098126629', + 'variables': [] + }, + { + 'key': 'variation_10000', + 'id': '12098126630', + 'variables': [] + } + ], + 'status': 'Running', + 'key': 'no_traffic_experiment', + 'id': '12115595439' + } + ], + 'trafficAllocation': [ + { + 'endOfRange': 10000, + 'entityId': '12198292375' + } + ] } ], 'attributes': [ @@ -1333,6 +1410,10 @@ var datafileWithFeaturesExpectedData = { }, 599080: {}, 599081: {}, + 12098126627: {}, + 12098126628: {}, + 12098126629: {}, + 12098126630: {}, }, featureKeyMap: { @@ -1547,6 +1628,15 @@ var datafileWithFeaturesExpectedData = { 'variables': [], variableKeyMap: {}, }, + feature_exp_no_traffic: { + 'rolloutId': '', + 'key': 'feature_exp_no_traffic', + 'id': '4482920079', + 'experimentIds': ['12115595439'], + 'variables': [], + variableKeyMap: {}, + groupId: '595025', + }, }, };