diff --git a/lib/optimizely/decision_service.rb b/lib/optimizely/decision_service.rb index 051a8b6..23db175 100644 --- a/lib/optimizely/decision_service.rb +++ b/lib/optimizely/decision_service.rb @@ -100,7 +100,9 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac should_ignore_user_profile_service = decide_options.include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE # Check for saved bucketing decisions if decide_options do not include ignoreUserProfileService - unless should_ignore_user_profile_service && user_profile_tracker + # Skip UPS for CMAB experiments as they require dynamic decisions + is_cmab_experiment = experiment.key?('cmab') + unless should_ignore_user_profile_service || is_cmab_experiment || !user_profile_tracker saved_variation_id, reasons_received = get_saved_variation_id(project_config, experiment_id, user_profile_tracker.user_profile) decide_reasons.push(*reasons_received) if saved_variation_id @@ -111,6 +113,13 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac end end + # Add decision reason when UPS is skipped for CMAB + if is_cmab_experiment && !should_ignore_user_profile_service && user_profile_tracker + message = 'User Profile Service is not used for CMAB experiments.' + @logger.log(Logger::INFO, message) + decide_reasons.push(message) + end + # Check audience conditions user_meets_audience_conditions, reasons_received = Audience.user_meets_audience_conditions?(project_config, experiment, user_context, @logger) decide_reasons.push(*reasons_received) @@ -155,7 +164,8 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac decide_reasons.push(message) if message # Persist bucketing decision - user_profile_tracker.update_user_profile(experiment_id, variation_id) unless should_ignore_user_profile_service && user_profile_tracker + # Skip UPS for CMAB experiments as they require dynamic decisions + user_profile_tracker.update_user_profile(experiment_id, variation_id) unless should_ignore_user_profile_service || is_cmab_experiment || !user_profile_tracker VariationResult.new(cmab_uuid, false, decide_reasons, variation_id) end diff --git a/spec/decision_service_spec.rb b/spec/decision_service_spec.rb index 30ad7d2..610c23c 100644 --- a/spec/decision_service_spec.rb +++ b/spec/decision_service_spec.rb @@ -1166,5 +1166,74 @@ expect(spy_cmab_service).not_to have_received(:get_decision) end end + + describe 'when user profile service is enabled' do + it 'should skip user profile service lookup and save for CMAB experiments' do + # Create a CMAB experiment configuration + cmab_experiment = { + 'id' => '111150', + 'key' => 'cmab_experiment', + 'status' => 'Running', + 'layerId' => '111150', + 'audienceIds' => [], + 'forcedVariations' => {}, + 'variations' => [ + {'id' => '111151', 'key' => 'variation_1'}, + {'id' => '111152', 'key' => 'variation_2'} + ], + 'trafficAllocation' => [ + {'entityId' => '111151', 'endOfRange' => 5000}, + {'entityId' => '111152', 'endOfRange' => 10_000} + ], + 'cmab' => {'trafficAllocation' => 5000} + } + user_context = project_instance.create_user_context('test_user', {}) + + # Create user profile tracker + user_profile_service = Optimizely::UserProfileService.new + user_profile_tracker = Optimizely::UserProfileTracker.new('test_user', user_profile_service, spy_logger) + + # Mock experiment lookup to return our CMAB experiment + allow(config).to receive(:get_experiment_from_id).with('111150').and_return(cmab_experiment) + allow(config).to receive(:experiment_running?).with(cmab_experiment).and_return(true) + + # Mock audience evaluation to pass + allow(Optimizely::Audience).to receive(:user_meets_audience_conditions?).and_return([true, []]) + + # Mock bucketer to return a valid entity ID (user is in traffic allocation) + allow(decision_service.bucketer).to receive(:bucket_to_entity_id) + .with(config, cmab_experiment, 'test_user', 'test_user') + .and_return(['$', []]) + + # Mock CMAB service to return a decision + allow(spy_cmab_service).to receive(:get_decision) + .with(config, user_context, '111150', []) + .and_return(Optimizely::CmabDecision.new(variation_id: '111151', cmab_uuid: 'test-cmab-uuid-123')) + + # Mock variation lookup + allow(config).to receive(:get_variation_from_id_by_experiment_id) + .with('111150', '111151') + .and_return({'id' => '111151', 'key' => 'variation_1'}) + + # Spy on user profile tracker methods + allow(user_profile_tracker).to receive(:update_user_profile).and_call_original + allow(decision_service).to receive(:get_saved_variation_id).and_call_original + + variation_result = decision_service.get_variation(config, '111150', user_context, user_profile_tracker) + + expect(variation_result.variation_id).to eq('111151') + expect(variation_result.cmab_uuid).to eq('test-cmab-uuid-123') + expect(variation_result.error).to eq(false) + + # Verify UPS lookup was NOT called for CMAB + expect(decision_service).not_to have_received(:get_saved_variation_id) + + # Verify UPS save was NOT called for CMAB + expect(user_profile_tracker).not_to have_received(:update_user_profile) + + # Verify the UPS exclusion message is in reasons + expect(variation_result.reasons).to include('User Profile Service is not used for CMAB experiments.') + end + end end end