From 69b2453fe1b11e85a18213c631d4e3dc6cdedd27 Mon Sep 17 00:00:00 2001 From: Farhan Anjum Date: Fri, 10 Jan 2025 01:53:44 +0600 Subject: [PATCH] [FSSDK-10765] enhancement: Implement UPS request batching for decideForKeys (#353) * user profile tracker created * lib/optimizely.rb -> Added user_profile_tracker require lib/optimizely.rb -> Updated decide_for_keys method lib/optimizely.rb -> Enhanced decision-making logic lib/optimizely.rb -> Integrated UserProfileTracker usage lib/optimizely.rb -> Refined decision reasons handling lib/optimizely/user_profile_tracker.rb -> New user profile tracker class * Implementation complete. Unit Tests are failing. * lib/optimizely.rb -> Made optional parameter explicit lib/optimizely/decision_service.rb -> Added user profile tracker usage lib/optimizely/decision_service.rb -> Clarified handling of user profiles lib/optimizely/user_profile_tracker.rb -> Fixed user ID reference in error spec/decision_service_spec.rb -> Adjusted tests for user profile tracker * lib/optimizely/decision_service.rb -> Simplified decision logging lib/optimizely/user_profile_tracker.rb -> Improved user profile lookup handling spec/project_spec.rb -> Updated mocks for decision service calls * lib/optimizely/decision_service.rb -> Removed user profile tracker instantiation. lib/optimizely/user_profile_tracker.rb -> Improved error logging message. spec/decision_service_spec.rb -> Refactored user profile tracking in tests. spec/project_spec.rb -> Updated decision service method stubs. spec/user_profile_tracker.rb -> Updated lookup, update and save tests for user_profile_tracker * spec/user_profile_tracker_spec.rb -> Updated error messages in tests. * spec/user_profile_tracker_spec.rb -> linting fix * linting fixes * Update README.md * Update README.md * Trigger checks * Trigger checks * Trigger checks * Trigger checks * lib/optimizely/user_profile_tracker.rb -> Added user profile init check. * lib/optimizely/decision_service.rb -> Updated user profile tracker initialization. * lib/optimizely/decision_service.rb -> Update user profile save method --------- Co-authored-by: Matjaz Pirnovar --- lib/optimizely.rb | 174 +++++++++++++++++-------- lib/optimizely/decision_service.rb | 70 ++++++---- lib/optimizely/helpers/validator.rb | 4 +- lib/optimizely/optimizely_factory.rb | 1 - lib/optimizely/user_profile_tracker.rb | 64 +++++++++ spec/decision_service_spec.rb | 173 +++++++----------------- spec/project_spec.rb | 37 ++++-- spec/user_profile_tracker_spec.rb | 101 ++++++++++++++ 8 files changed, 402 insertions(+), 222 deletions(-) create mode 100644 lib/optimizely/user_profile_tracker.rb create mode 100644 spec/user_profile_tracker_spec.rb diff --git a/lib/optimizely.rb b/lib/optimizely.rb index 1dbd54c4..7c5571b3 100644 --- a/lib/optimizely.rb +++ b/lib/optimizely.rb @@ -42,6 +42,7 @@ require_relative 'optimizely/odp/lru_cache' require_relative 'optimizely/odp/odp_manager' require_relative 'optimizely/helpers/sdk_settings' +require_relative 'optimizely/user_profile_tracker' module Optimizely class Project @@ -172,65 +173,18 @@ def create_user_context(user_id, attributes = nil) OptimizelyUserContext.new(self, user_id, attributes) end - def decide(user_context, key, decide_options = []) - # raising on user context as it is internal and not provided directly by the user. - raise if user_context.class != OptimizelyUserContext - - reasons = [] - - # check if SDK is ready - unless is_valid - @logger.log(Logger::ERROR, InvalidProjectConfigError.new('decide').message) - reasons.push(OptimizelyDecisionMessage::SDK_NOT_READY) - return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) - end - - # validate that key is a string - unless key.is_a?(String) - @logger.log(Logger::ERROR, 'Provided key is invalid') - reasons.push(format(OptimizelyDecisionMessage::FLAG_KEY_INVALID, key)) - return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) - end - - # validate that key maps to a feature flag - config = project_config - feature_flag = config.get_feature_flag_from_key(key) - unless feature_flag - @logger.log(Logger::ERROR, "No feature flag was found for key '#{key}'.") - reasons.push(format(OptimizelyDecisionMessage::FLAG_KEY_INVALID, key)) - return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) - end - - # merge decide_options and default_decide_options - if decide_options.is_a? Array - decide_options += @default_decide_options - else - @logger.log(Logger::DEBUG, 'Provided decide options is not an array. Using default decide options.') - decide_options = @default_decide_options - end - + def create_optimizely_decision(user_context, flag_key, decision, reasons, decide_options, config) # Create Optimizely Decision Result. user_id = user_context.user_id attributes = user_context.user_attributes variation_key = nil feature_enabled = false rule_key = nil - flag_key = key all_variables = {} decision_event_dispatched = false + feature_flag = config.get_feature_flag_from_key(flag_key) experiment = nil decision_source = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] - context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(key, nil) - variation, reasons_received = @decision_service.validated_forced_decision(config, context, user_context) - reasons.push(*reasons_received) - - if variation - decision = Optimizely::DecisionService::Decision.new(nil, variation, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST']) - else - decision, reasons_received = @decision_service.get_variation_for_feature(config, feature_flag, user_context, decide_options) - reasons.push(*reasons_received) - end - # Send impression event if Decision came from a feature test and decide options doesn't include disableDecisionEvent if decision.is_a?(Optimizely::DecisionService::Decision) experiment = decision.experiment @@ -249,7 +203,7 @@ def decide(user_context, key, decide_options = []) # Generate all variables map if decide options doesn't include excludeVariables unless decide_options.include? OptimizelyDecideOption::EXCLUDE_VARIABLES feature_flag['variables'].each do |variable| - variable_value = get_feature_variable_for_variation(key, feature_enabled, variation, variable, user_id) + variable_value = get_feature_variable_for_variation(flag_key, feature_enabled, variation, variable, user_id) all_variables[variable['key']] = Helpers::VariableType.cast_value_to_type(variable_value, variable['type'], @logger) end end @@ -281,6 +235,47 @@ def decide(user_context, key, decide_options = []) ) end + def decide(user_context, key, decide_options = []) + # raising on user context as it is internal and not provided directly by the user. + raise if user_context.class != OptimizelyUserContext + + reasons = [] + + # check if SDK is ready + unless is_valid + @logger.log(Logger::ERROR, InvalidProjectConfigError.new('decide').message) + reasons.push(OptimizelyDecisionMessage::SDK_NOT_READY) + return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) + end + + # validate that key is a string + unless key.is_a?(String) + @logger.log(Logger::ERROR, 'Provided key is invalid') + reasons.push(format(OptimizelyDecisionMessage::FLAG_KEY_INVALID, key)) + return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) + end + + # validate that key maps to a feature flag + config = project_config + feature_flag = config.get_feature_flag_from_key(key) + unless feature_flag + @logger.log(Logger::ERROR, "No feature flag was found for key '#{key}'.") + reasons.push(format(OptimizelyDecisionMessage::FLAG_KEY_INVALID, key)) + return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) + end + + # merge decide_options and default_decide_options + if decide_options.is_a? Array + decide_options += @default_decide_options + else + @logger.log(Logger::DEBUG, 'Provided decide options is not an array. Using default decide options.') + decide_options = @default_decide_options + end + + decide_options.delete(OptimizelyDecideOption::ENABLED_FLAGS_ONLY) if decide_options.include?(OptimizelyDecideOption::ENABLED_FLAGS_ONLY) + decide_for_keys(user_context, [key], decide_options, true)[key] + end + def decide_all(user_context, decide_options = []) # raising on user context as it is internal and not provided directly by the user. raise if user_context.class != OptimizelyUserContext @@ -298,7 +293,7 @@ def decide_all(user_context, decide_options = []) decide_for_keys(user_context, keys, decide_options) end - def decide_for_keys(user_context, keys, decide_options = []) + def decide_for_keys(user_context, keys, decide_options = [], ignore_default_options = false) # rubocop:disable Style/OptionalBooleanParameter # raising on user context as it is internal and not provided directly by the user. raise if user_context.class != OptimizelyUserContext @@ -308,13 +303,79 @@ def decide_for_keys(user_context, keys, decide_options = []) return {} end - enabled_flags_only = (!decide_options.nil? && (decide_options.include? OptimizelyDecideOption::ENABLED_FLAGS_ONLY)) || (@default_decide_options.include? OptimizelyDecideOption::ENABLED_FLAGS_ONLY) + # merge decide_options and default_decide_options + unless ignore_default_options + if decide_options.is_a?(Array) + decide_options += @default_decide_options + else + @logger.log(Logger::DEBUG, 'Provided decide options is not an array. Using default decide options.') + decide_options = @default_decide_options + end + end + + # enabled_flags_only = (!decide_options.nil? && (decide_options.include? OptimizelyDecideOption::ENABLED_FLAGS_ONLY)) || (@default_decide_options.include? OptimizelyDecideOption::ENABLED_FLAGS_ONLY) decisions = {} + valid_keys = [] + decision_reasons_dict = {} + config = project_config + return decisions unless config + + flags_without_forced_decision = [] + flag_decisions = {} + keys.each do |key| - decision = decide(user_context, key, decide_options) - decisions[key] = decision unless enabled_flags_only && !decision.enabled + # Retrieve the feature flag from the project's feature flag key map + feature_flag = config.feature_flag_key_map[key] + + # If the feature flag is nil, create a default OptimizelyDecision and move to the next key + if feature_flag.nil? + decisions[key] = OptimizelyDecision.new(nil, false, nil, nil, key, user_context, []) + next + end + valid_keys.push(key) + decision_reasons = [] + decision_reasons_dict[key] = decision_reasons + + config = project_config + context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(key, nil) + variation, reasons_received = @decision_service.validated_forced_decision(config, context, user_context) + decision_reasons_dict[key].push(*reasons_received) + if variation + decision = Optimizely::DecisionService::Decision.new(nil, variation, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST']) + flag_decisions[key] = decision + else + flags_without_forced_decision.push(feature_flag) + end end + decision_list = @decision_service.get_variations_for_feature_list(config, flags_without_forced_decision, user_context, decide_options) + + flags_without_forced_decision.each_with_index do |flag, i| + decision = decision_list[i][0] + reasons = decision_list[i][1] + flag_key = flag['key'] + flag_decisions[flag_key] = decision + decision_reasons_dict[flag_key] ||= [] + decision_reasons_dict[flag_key].push(*reasons) + end + valid_keys.each do |key| + flag_decision = flag_decisions[key] + decision_reasons = decision_reasons_dict[key] + optimizely_decision = create_optimizely_decision( + user_context, + key, + flag_decision, + decision_reasons, + decide_options, + config + ) + + enabled_flags_only_missing = !decide_options.include?(OptimizelyDecideOption::ENABLED_FLAGS_ONLY) + is_enabled = optimizely_decision.enabled + + decisions[key] = optimizely_decision if enabled_flags_only_missing || is_enabled + end + decisions end @@ -959,7 +1020,10 @@ def get_variation_with_config(experiment_key, user_id, attributes, config) return nil unless user_inputs_valid?(attributes) user_context = OptimizelyUserContext.new(self, user_id, attributes, identify: false) - variation_id, = @decision_service.get_variation(config, experiment_id, user_context) + user_profile_tracker = UserProfileTracker.new(user_id, @user_profile_service, @logger) + user_profile_tracker.load_user_profile + variation_id, = @decision_service.get_variation(config, experiment_id, user_context, user_profile_tracker) + user_profile_tracker.save_user_profile variation = config.get_variation_from_id(experiment_key, variation_id) unless variation_id.nil? variation_key = variation['key'] if variation decision_notification_type = if config.feature_experiment?(experiment_id) diff --git a/lib/optimizely/decision_service.rb b/lib/optimizely/decision_service.rb index 3dbbf1d0..3303907d 100644 --- a/lib/optimizely/decision_service.rb +++ b/lib/optimizely/decision_service.rb @@ -52,17 +52,20 @@ def initialize(logger, user_profile_service = nil) @forced_variation_map = {} end - def get_variation(project_config, experiment_id, user_context, decide_options = []) + def get_variation(project_config, experiment_id, user_context, user_profile_tracker = nil, decide_options = [], reasons = []) # Determines variation into which user will be bucketed. # # project_config - project_config - Instance of ProjectConfig # experiment_id - Experiment for which visitor variation needs to be determined # user_context - Optimizely user context instance + # user_profile_tracker: Tracker for reading and updating user profile of the user. + # reasons: Decision reasons. # # Returns variation ID where visitor will be bucketed # (nil if experiment is inactive or user does not meet audience conditions) - + user_profile_tracker = UserProfileTracker.new(user_context.user_id, @user_profile_service, @logger) unless user_profile_tracker.is_a?(Optimizely::UserProfileTracker) decide_reasons = [] + decide_reasons.push(*reasons) user_id = user_context.user_id attributes = user_context.user_attributes # By default, the bucketing ID should be the user ID @@ -92,10 +95,8 @@ def get_variation(project_config, experiment_id, user_context, decide_options = 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, reasons_received = get_user_profile(user_id) - decide_reasons.push(*reasons_received) - saved_variation_id, reasons_received = get_saved_variation_id(project_config, experiment_id, user_profile) + unless should_ignore_user_profile_service && 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 message = "Returning previously activated variation ID #{saved_variation_id} of experiment '#{experiment_key}' for user '#{user_id}' from user profile." @@ -131,7 +132,7 @@ def get_variation(project_config, experiment_id, user_context, decide_options = decide_reasons.push(message) # Persist bucketing decision - save_user_profile(user_profile, experiment_id, variation_id) unless should_ignore_user_profile_service + user_profile_tracker.update_user_profile(experiment_id, variation_id) unless should_ignore_user_profile_service && user_profile_tracker [variation_id, decide_reasons] end @@ -143,21 +144,46 @@ def get_variation_for_feature(project_config, feature_flag, user_context, decide # user_context - Optimizely user context instance # # Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature) + get_variations_for_feature_list(project_config, [feature_flag], user_context, decide_options).first + end - decide_reasons = [] - - # check if the feature is being experiment on and whether the user is bucketed into the experiment - decision, reasons_received = get_variation_for_feature_experiment(project_config, feature_flag, user_context, decide_options) - decide_reasons.push(*reasons_received) - return decision, decide_reasons unless decision.nil? - - decision, reasons_received = get_variation_for_feature_rollout(project_config, feature_flag, user_context) - decide_reasons.push(*reasons_received) - - [decision, decide_reasons] + def get_variations_for_feature_list(project_config, feature_flags, user_context, decide_options = []) + # Returns the list of experiment/variation the user is bucketed in for the given list of features. + # + # Args: + # project_config: Instance of ProjectConfig. + # feature_flags: Array of features for which we are determining if it is enabled or not for the given user. + # user_context: User context for user. + # decide_options: Decide options. + # + # Returns: + # Array of Decision struct. + ignore_ups = decide_options.include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE + user_profile_tracker = nil + unless ignore_ups && @user_profile_service + user_profile_tracker = UserProfileTracker.new(user_context.user_id, @user_profile_service, @logger) + user_profile_tracker.load_user_profile + end + decisions = [] + feature_flags.each do |feature_flag| + decide_reasons = [] + # check if the feature is being experiment on and whether the user is bucketed into the experiment + decision, reasons_received = get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options) + decide_reasons.push(*reasons_received) + if decision + decisions << [decision, decide_reasons] + else + # Proceed to rollout if the decision is nil + rollout_decision, reasons_received = get_variation_for_feature_rollout(project_config, feature_flag, user_context) + decide_reasons.push(*reasons_received) + decisions << [rollout_decision, decide_reasons] + end + end + user_profile_tracker&.save_user_profile + decisions end - def get_variation_for_feature_experiment(project_config, feature_flag, user_context, decide_options = []) + def get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options = []) # Gets the variation the user is bucketed into for the feature flag's experiment. # # project_config - project_config - Instance of ProjectConfig @@ -187,7 +213,7 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_cont end experiment_id = experiment['id'] - variation_id, reasons_received = get_variation_from_experiment_rule(project_config, feature_flag_key, experiment, user_context, decide_options) + variation_id, reasons_received = get_variation_from_experiment_rule(project_config, feature_flag_key, experiment, user_context, user_profile_tracker, decide_options) decide_reasons.push(*reasons_received) next unless variation_id @@ -252,7 +278,7 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context [nil, decide_reasons] end - def get_variation_from_experiment_rule(project_config, flag_key, rule, user, options = []) + def get_variation_from_experiment_rule(project_config, flag_key, rule, user, user_profile_tracker, options = []) # Determine which variation the user is in for a given rollout. # Returns the variation from experiment rules. # @@ -270,7 +296,7 @@ def get_variation_from_experiment_rule(project_config, flag_key, rule, user, opt return [variation['id'], reasons] if variation - variation_id, response_reasons = get_variation(project_config, rule['id'], user, options) + variation_id, response_reasons = get_variation(project_config, rule['id'], user, user_profile_tracker, options) reasons.push(*response_reasons) [variation_id, reasons] diff --git a/lib/optimizely/helpers/validator.rb b/lib/optimizely/helpers/validator.rb index d3baa447..4d975483 100644 --- a/lib/optimizely/helpers/validator.rb +++ b/lib/optimizely/helpers/validator.rb @@ -122,11 +122,11 @@ def inputs_valid?(variables, logger = NoOpLogger.new, level = Logger::ERROR) return false unless variables.respond_to?(:each) && !variables.empty? - is_valid = true + is_valid = true # rubocop:disable Lint/UselessAssignment if variables.include? :user_id # Empty str is a valid user ID. unless variables[:user_id].is_a?(String) - is_valid = false + is_valid = false # rubocop:disable Lint/UselessAssignment logger.log(level, "#{Constants::INPUT_VARIABLES['USER_ID']} is invalid") end variables.delete :user_id diff --git a/lib/optimizely/optimizely_factory.rb b/lib/optimizely/optimizely_factory.rb index 04c7ecdd..717e43d9 100644 --- a/lib/optimizely/optimizely_factory.rb +++ b/lib/optimizely/optimizely_factory.rb @@ -142,7 +142,6 @@ def self.custom_instance( # rubocop:disable Metrics/ParameterLists notification_center = nil, settings = nil ) - error_handler ||= NoOpErrorHandler.new logger ||= NoOpLogger.new notification_center = notification_center.is_a?(Optimizely::NotificationCenter) ? notification_center : NotificationCenter.new(logger, error_handler) diff --git a/lib/optimizely/user_profile_tracker.rb b/lib/optimizely/user_profile_tracker.rb new file mode 100644 index 00000000..082576b0 --- /dev/null +++ b/lib/optimizely/user_profile_tracker.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require_relative 'logger' + +module Optimizely + class UserProfileTracker + attr_reader :user_profile + + def initialize(user_id, user_profile_service = nil, logger = nil) + @user_id = user_id + @user_profile_service = user_profile_service + @logger = logger || NoOpLogger.new + @profile_updated = false + @user_profile = { + user_id: user_id, + experiment_bucket_map: {} + } + end + + def load_user_profile(reasons = [], error_handler = nil) + return if reasons.nil? + + begin + @user_profile = @user_profile_service.lookup(@user_id) if @user_profile_service + if @user_profile.nil? + @user_profile = { + user_id: @user_id, + experiment_bucket_map: {} + } + end + rescue => e + message = "Error while looking up user profile for user ID '#{@user_id}': #{e}." + reasons << message + @logger.log(Logger::ERROR, message) + error_handler&.handle_error(e) + end + end + + def update_user_profile(experiment_id, variation_id) + user_id = @user_profile[:user_id] + begin + @user_profile[:experiment_bucket_map][experiment_id] = { + variation_id: variation_id + } + @profile_updated = true + @logger.log(Logger::INFO, "Updated variation ID #{variation_id} of experiment ID #{experiment_id} for user '#{user_id}'.") + rescue => e + @logger.log(Logger::ERROR, "Error while updating user profile for user ID '#{user_id}': #{e}.") + end + end + + def save_user_profile(error_handler = nil) + return unless @profile_updated && @user_profile_service + + begin + @user_profile_service.save(@user_profile) + @logger.log(Logger::INFO, "Saved user profile for user '#{@user_profile[:user_id]}'.") + rescue => e + @logger.log(Logger::ERROR, "Failed to save user profile for user '#{@user_profile[:user_id]}': #{e}.") + error_handler&.handle_error(e) + end + end + end +end diff --git a/spec/decision_service_spec.rb b/spec/decision_service_spec.rb index 10f58792..af22b18b 100644 --- a/spec/decision_service_spec.rb +++ b/spec/decision_service_spec.rb @@ -73,7 +73,8 @@ it 'should return the correct variation ID for a given user ID and key of a running experiment' do user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq('111128') expect(reasons).to eq([ @@ -90,7 +91,8 @@ it 'should return nil when user ID is not bucketed' do allow(decision_service.bucketer).to receive(:bucket).and_return(nil) user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq(nil) expect(reasons).to eq([ "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", @@ -189,7 +191,8 @@ it 'should return nil if the user does not meet the audience conditions for a given experiment' do user_attributes = {'browser_type' => 'chrome'} user_context = project_instance.create_user_context('test_user', user_attributes) - variation_received, reasons = decision_service.get_variation(config, '122227', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation(config, '122227', user_context, user_profile_tracker) expect(variation_received).to eq(nil) expect(reasons).to eq([ "Starting to evaluate audience '11154' with conditions: [\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"firefox\"}]]].", @@ -240,7 +243,8 @@ it 'should bucket normally if user is whitelisted into a forced variation that is not in the datafile' do user_context = project_instance.create_user_context('forced_user_with_invalid_variation') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq('111128') expect(reasons).to eq([ "User 'forced_user_with_invalid_variation' is whitelisted into variation 'invalid_variation', which is not in the datafile.", @@ -259,50 +263,14 @@ end describe 'when a UserProfile service is provided' do - it 'should look up the UserProfile, bucket normally, and save the result if no saved profile is found' do - expected_user_profile = { - user_id: 'test_user', - experiment_bucket_map: { - '111127' => { - variation_id: '111128' - } - } - } - expect(spy_user_profile_service).to receive(:lookup).once.and_return(nil) - - user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) - expect(variation_received).to eq('111128') - expect(reasons).to eq([ - "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) - - # bucketing should have occurred - expect(decision_service.bucketer).to have_received(:bucket).once - # bucketing decision should have been saved - expect(spy_user_profile_service).to have_received(:save).once.with(expected_user_profile) - expect(spy_logger).to have_received(:log).once - .with(Logger::INFO, "Saved variation ID 111128 of experiment ID 111127 for user 'test_user'.") - end - - it 'should look up the UserProfile, bucket normally (using Bucketing ID attribute), and save the result if no saved profile is found' do - expected_user_profile = { - user_id: 'test_user', - experiment_bucket_map: { - '111127' => { - variation_id: '111129' - } - } - } + it 'bucket normally (using Bucketing ID attribute)' do user_attributes = { 'browser_type' => 'firefox', Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BUCKETING_ID'] => 'pid' } - expect(spy_user_profile_service).to receive(:lookup).once.and_return(nil) - user_context = project_instance.create_user_context('test_user', user_attributes) - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id, spy_user_profile_service, spy_logger) + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq('111129') expect(reasons).to eq([ "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", @@ -311,13 +279,9 @@ # bucketing should have occurred expect(decision_service.bucketer).to have_received(:bucket).once - # bucketing decision should have been saved - expect(spy_user_profile_service).to have_received(:save).once.with(expected_user_profile) - expect(spy_logger).to have_received(:log).once - .with(Logger::INFO, "Saved variation ID 111129 of experiment ID 111127 for user 'test_user'.") end - it 'should look up the user profile and skip normal bucketing if a profile with a saved decision is found' do + it 'skip normal bucketing if a profile with a saved decision is found' do saved_user_profile = { user_id: 'test_user', experiment_bucket_map: { @@ -330,7 +294,9 @@ .with('test_user').once.and_return(saved_user_profile) user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id, spy_user_profile_service, spy_logger) + user_profile_tracker.load_user_profile + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq('111129') expect(reasons).to eq([ "Returning previously activated variation ID 111129 of experiment 'test_experiment' for user 'test_user' from user profile." @@ -346,7 +312,7 @@ expect(spy_user_profile_service).not_to have_received(:save) end - it 'should look up the user profile and bucket normally if a profile without a saved decision is found' do + it 'bucket normally if a profile without a saved decision is found' do saved_user_profile = { user_id: 'test_user', experiment_bucket_map: { @@ -360,7 +326,9 @@ .once.with('test_user').and_return(saved_user_profile) user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id, spy_user_profile_service, spy_logger) + user_profile_tracker.load_user_profile + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq('111128') expect(reasons).to eq([ "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", @@ -369,20 +337,6 @@ # bucketing should have occurred expect(decision_service.bucketer).to have_received(:bucket).once - - # user profile should have been updated with bucketing decision - expected_user_profile = { - user_id: 'test_user', - experiment_bucket_map: { - '111127' => { - variation_id: '111128' - }, - '122227' => { - variation_id: '122228' - } - } - } - expect(spy_user_profile_service).to have_received(:save).once.with(expected_user_profile) end it 'should bucket normally if the user profile contains a variation ID not in the datafile' do @@ -399,7 +353,9 @@ .once.with('test_user').and_return(saved_user_profile) user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id, spy_user_profile_service, spy_logger) + user_profile_tracker.load_user_profile + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) expect(variation_received).to eq('111128') expect(reasons).to eq([ "User 'test_user' was previously bucketed into variation ID '111111' for experiment '111127', but no matching variation was found. Re-bucketing user.", @@ -409,27 +365,18 @@ # bucketing should have occurred expect(decision_service.bucketer).to have_received(:bucket).once - - # user profile should have been updated with bucketing decision - expected_user_profile = { - user_id: 'test_user', - experiment_bucket_map: { - '111127' => { - variation_id: '111128' - } - } - } - expect(spy_user_profile_service).to have_received(:save).with(expected_user_profile) end - it 'should bucket normally if the user profile service throws an error during lookup' do + it 'should bucket normally if the user profile tracker throws an error during lookup' do expect(spy_user_profile_service).to receive(:lookup).once.with('test_user').and_throw(:LookupError) user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id, spy_user_profile_service, spy_logger) + user_profile_tracker.load_user_profile + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + user_profile_tracker.save_user_profile expect(variation_received).to eq('111128') expect(reasons).to eq([ - "Error while looking up user profile for user ID 'test_user': uncaught throw :LookupError.", "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", "User 'test_user' is in variation 'control' of experiment '111127'." ]) @@ -440,46 +387,15 @@ expect(decision_service.bucketer).to have_received(:bucket).once end - it 'should log an error if the user profile service throws an error during save' do - expect(spy_user_profile_service).to receive(:save).once.and_throw(:SaveError) - - user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) - expect(variation_received).to eq('111128') - expect(reasons).to eq([ - "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) - - expect(spy_logger).to have_received(:log).once - .with(Logger::ERROR, "Error while saving user profile for user ID 'test_user': uncaught throw :SaveError.") - end - describe 'IGNORE_USER_PROFILE_SERVICE decide option' do it 'should ignore user profile service if this option is set' do allow(spy_user_profile_service).to receive(:lookup) .with('test_user').once.and_return(nil) user_context = project_instance.create_user_context('test_user', nil) - variation_received, reasons = decision_service.get_variation(config, '111127', user_context, [Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE]) - expect(variation_received).to eq('111128') - expect(reasons).to eq([ - "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) - - expect(decision_service.bucketer).to have_received(:bucket) - expect(Optimizely::Audience).to have_received(:user_meets_audience_conditions?) - expect(spy_user_profile_service).not_to have_received(:lookup) - expect(spy_user_profile_service).not_to have_received(:save) - end - - it 'should not ignore user profile service if this option is not set' do - allow(spy_user_profile_service).to receive(:lookup) - .with('test_user').once.and_return(nil) - - user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id, spy_user_profile_service, spy_logger) + user_profile_tracker.load_user_profile + variation_received, reasons = decision_service.get_variation(config, '111127', user_context, user_profile_tracker, [Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE]) expect(variation_received).to eq('111128') expect(reasons).to eq([ "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", @@ -488,8 +404,6 @@ expect(decision_service.bucketer).to have_received(:bucket) expect(Optimizely::Audience).to have_received(:user_meets_audience_conditions?) - expect(spy_user_profile_service).to have_received(:lookup) - expect(spy_user_profile_service).to have_received(:save) end end end @@ -499,11 +413,11 @@ config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON project_instance = Optimizely::Project.new(datafile: config_body_json) user_context = project_instance.create_user_context('user_1', {}) - + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) describe 'when the feature flag\'s experiment ids array is empty' do it 'should return nil and log a message' do feature_flag = config.feature_flag_key_map['empty_feature'] - variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context) + variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) expect(variation_received).to eq(nil) expect(reasons).to eq(["The feature flag 'empty_feature' is not used in any experiments."]) @@ -517,7 +431,8 @@ feature_flag = config.feature_flag_key_map['boolean_feature'].dup # any string that is not an experiment id in the data file feature_flag['experimentIds'] = ['1333333337'] - variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) expect(variation_received).to eq(nil) expect(reasons).to eq(["Feature flag experiment with ID '1333333337' is not in the datafile."]) expect(spy_logger).to have_received(:log).once @@ -526,19 +441,19 @@ end describe 'when the feature flag is associated with a non-mutex experiment' do + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) describe 'and the user is not bucketed into the feature flag\'s experiments' do before(:each) do multivariate_experiment = config.experiment_key_map['test_experiment_multivariate'] - # make sure the user is not bucketed into the feature experiment allow(decision_service).to receive(:get_variation) - .with(config, multivariate_experiment['id'], user_context, []) + .with(config, multivariate_experiment['id'], user_context, user_profile_tracker, []) .and_return([nil, nil]) end it 'should return nil and log a message' do feature_flag = config.feature_flag_key_map['multi_variate_feature'] - variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, []) + variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker, []) expect(variation_received).to eq(nil) expect(reasons).to eq(["The user 'user_1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'."]) @@ -560,8 +475,8 @@ config.variation_id_map['test_experiment_multivariate']['122231'], Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - - variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) expect(variation_received).to eq(expected_decision) expect(reasons).to eq([]) end @@ -586,27 +501,29 @@ it 'should return the variation the user is bucketed into' do feature_flag = config.feature_flag_key_map['mutex_group_feature'] - variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context) + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) + variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) expect(variation_received).to eq(expected_decision) expect(reasons).to eq([]) end end describe 'and the user is not bucketed into any of the mutex experiments' do + user_profile_tracker = Optimizely::UserProfileTracker.new(user_context.user_id) before(:each) do mutex_exp = config.experiment_key_map['group1_exp1'] mutex_exp2 = config.experiment_key_map['group1_exp2'] allow(decision_service).to receive(:get_variation) - .with(config, mutex_exp['id'], user_context, []) + .with(config, mutex_exp['id'], user_context, user_profile_tracker, []) .and_return([nil, nil]) allow(decision_service).to receive(:get_variation) - .with(config, mutex_exp2['id'], user_context, []) + .with(config, mutex_exp2['id'], user_context, user_profile_tracker, []) .and_return([nil, nil]) end it 'should return nil and log a message' do feature_flag = config.feature_flag_key_map['mutex_group_feature'] - variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context) + variation_received, reasons = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) expect(variation_received).to eq(nil) expect(reasons).to eq(["The user 'user_1' is not bucketed into any of the experiments on the feature 'mutex_group_feature'."]) diff --git a/spec/project_spec.rb b/spec/project_spec.rb index 2c1aeaca..7c02f765 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -3766,7 +3766,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_list_to_be_returned = [] + decision_list_to_be_returned << [decision_to_return, []] + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_be_returned) user_context = project_instance.create_user_context('user1') decision = project_instance.decide(user_context, 'multi_variate_feature') expect(decision.as_json).to include( @@ -3807,7 +3809,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_list_to_be_returned = [] + decision_list_to_be_returned << [decision_to_return, []] + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_be_returned) user_context = project_instance.create_user_context('user1') decision = project_instance.decide(user_context, 'multi_variate_feature') @@ -3888,7 +3892,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_list_to_return = [[decision_to_return, []]] + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_return) user_context = project_instance.create_user_context('user1') decision = project_instance.decide(user_context, 'multi_variate_feature') expect(decision.as_json).to include( @@ -4055,8 +4060,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + decision_list_to_be_returned = [[decision_to_return, []]] allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_be_returned) user_context = project_instance.create_user_context('user1') decision = project_instance.decide(user_context, 'multi_variate_feature', [Optimizely::Decide::OptimizelyDecideOption::EXCLUDE_VARIABLES]) expect(decision.as_json).to include( @@ -4078,8 +4084,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + decision_list_to_return = [[decision_to_return, []]] allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_return) user_context = project_instance.create_user_context('user1') decision = project_instance.decide(user_context, 'multi_variate_feature') expect(decision.as_json).to include( @@ -4096,8 +4103,6 @@ def callback(_args); end describe 'INCLUDE_REASONS' do it 'should include reasons when the option is set' do - expect(project_instance.notification_center).to receive(:send_notifications) - .once.with(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args) expect(project_instance.notification_center).to receive(:send_notifications) .once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -4119,6 +4124,8 @@ def callback(_args); end ], decision_event_dispatched: true ) + expect(project_instance.notification_center).to receive(:send_notifications) + .once.with(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) user_context = project_instance.create_user_context('user1') decision = project_instance.decide(user_context, 'multi_variate_feature', [Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS]) @@ -4180,23 +4187,23 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + decision_list_to_return = [[decision_to_return, []]] allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_return) user_context = project_instance.create_user_context('user1') - expect(project_instance.decision_service).to receive(:get_variation_for_feature) + expect(project_instance.decision_service).to receive(:get_variations_for_feature_list) .with(anything, anything, anything, []).once project_instance.decide(user_context, 'multi_variate_feature') - expect(project_instance.decision_service).to receive(:get_variation_for_feature) + expect(project_instance.decision_service).to receive(:get_variations_for_feature_list) .with(anything, anything, anything, [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT]).once project_instance.decide(user_context, 'multi_variate_feature', [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT]) - expect(project_instance.decision_service).to receive(:get_variation_for_feature) + expect(project_instance.decision_service).to receive(:get_variations_for_feature_list) .with(anything, anything, anything, [ Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT, Optimizely::Decide::OptimizelyDecideOption::EXCLUDE_VARIABLES, - Optimizely::Decide::OptimizelyDecideOption::ENABLED_FLAGS_ONLY, Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE, Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS, Optimizely::Decide::OptimizelyDecideOption::EXCLUDE_VARIABLES @@ -4400,8 +4407,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + decision_list_to_return = [[decision_to_return, []]] allow(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) - allow(custom_project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(custom_project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_return) user_context = custom_project_instance.create_user_context('user1') decision = custom_project_instance.decide(user_context, 'multi_variate_feature') expect(decision.as_json).to include( @@ -4428,8 +4436,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + decision_list_to_return = [[decision_to_return, []]] allow(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) - allow(custom_project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(custom_project_instance.decision_service).to receive(:get_variations_for_feature_list).and_return(decision_list_to_return) user_context = custom_project_instance.create_user_context('user1') decision = custom_project_instance.decide(user_context, 'multi_variate_feature') expect(decision.as_json).to include( diff --git a/spec/user_profile_tracker_spec.rb b/spec/user_profile_tracker_spec.rb new file mode 100644 index 00000000..85515bb1 --- /dev/null +++ b/spec/user_profile_tracker_spec.rb @@ -0,0 +1,101 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rspec' + +RSpec.describe Optimizely::UserProfileTracker do + let(:user_id) { 'test_user' } + let(:mock_user_profile_service) { instance_double('UserProfileService') } + let(:mock_logger) { instance_double('Logger') } + let(:user_profile_tracker) { described_class.new(user_id, mock_user_profile_service, mock_logger) } + + describe '#initialize' do + it 'initializes with a user ID and default values' do + tracker = described_class.new(user_id) + expect(tracker.user_profile[:user_id]).to eq(user_id) + expect(tracker.user_profile[:experiment_bucket_map]).to eq({}) + end + + it 'accepts a user profile service and logger' do + expect(user_profile_tracker.instance_variable_get(:@user_profile_service)).to eq(mock_user_profile_service) + expect(user_profile_tracker.instance_variable_get(:@logger)).to eq(mock_logger) + end + end + + describe '#load_user_profile' do + it 'loads the user profile from the service if provided' do + expected_profile = { + user_id: user_id, + experiment_bucket_map: {'111127' => {variation_id: '111128'}} + } + allow(mock_user_profile_service).to receive(:lookup).with(user_id).and_return(expected_profile) + user_profile_tracker.load_user_profile + expect(user_profile_tracker.user_profile).to eq(expected_profile) + end + + it 'handles errors during lookup and logs them' do + allow(mock_user_profile_service).to receive(:lookup).with(user_id).and_raise(StandardError.new('lookup error')) + allow(mock_logger).to receive(:log) + + reasons = [] + user_profile_tracker.load_user_profile(reasons) + expect(reasons).to include("Error while looking up user profile for user ID 'test_user': lookup error.") + expect(mock_logger).to have_received(:log).with(Logger::ERROR, "Error while looking up user profile for user ID 'test_user': lookup error.") + end + + it 'does nothing if reasons array is nil' do + expect(mock_user_profile_service).not_to receive(:lookup) + user_profile_tracker.load_user_profile(nil) + end + end + + describe '#update_user_profile' do + let(:experiment_id) { '111127' } + let(:variation_id) { '111128' } + + before do + allow(mock_logger).to receive(:log) + end + + it 'updates the experiment bucket map with the given experiment and variation IDs' do + user_profile_tracker.update_user_profile(experiment_id, variation_id) + + # Verify the experiment and variation were added + expect(user_profile_tracker.user_profile[:experiment_bucket_map][experiment_id][:variation_id]).to eq(variation_id) + # Verify the profile_updated flag was set + expect(user_profile_tracker.instance_variable_get(:@profile_updated)).to eq(true) + # Verify a log message was recorded + expect(mock_logger).to have_received(:log).with(Logger::INFO, "Updated variation ID #{variation_id} of experiment ID #{experiment_id} for user 'test_user'.") + end + end + + describe '#save_user_profile' do + it 'saves the user profile if updates were made and service is available' do + allow(mock_user_profile_service).to receive(:save) + allow(mock_logger).to receive(:log) + + user_profile_tracker.update_user_profile('111127', '111128') + user_profile_tracker.save_user_profile + + expect(mock_user_profile_service).to have_received(:save).with(user_profile_tracker.user_profile) + expect(mock_logger).to have_received(:log).with(Logger::INFO, "Saved user profile for user 'test_user'.") + end + + it 'does not save the user profile if no updates were made' do + allow(mock_user_profile_service).to receive(:save) + + user_profile_tracker.save_user_profile + expect(mock_user_profile_service).not_to have_received(:save) + end + + it 'handles errors during save and logs them' do + allow(mock_user_profile_service).to receive(:save).and_raise(StandardError.new('save error')) + allow(mock_logger).to receive(:log) + + user_profile_tracker.update_user_profile('111127', '111128') + user_profile_tracker.save_user_profile + + expect(mock_logger).to have_received(:log).with(Logger::ERROR, "Failed to save user profile for user 'test_user': save error.") + end + end +end