diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 98f73108..971eea92 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -33,6 +33,13 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Install rubocop 1.78.0 for Ruby 3.0.0 + if: matrix.ruby == '3.0.0' + run: | + echo "Installing rubocop 1.78.0 for Ruby 3.0.0" + bundle add rubocop --version 1.78.0 || true + bundle install + bundle exec rubocop -A Gemfile || true - name: Run linting run: | bundle exec rubocop diff --git a/lib/optimizely.rb b/lib/optimizely.rb index 7c5571b3..aa50ce4e 100644 --- a/lib/optimizely.rb +++ b/lib/optimizely.rb @@ -43,11 +43,17 @@ require_relative 'optimizely/odp/odp_manager' require_relative 'optimizely/helpers/sdk_settings' require_relative 'optimizely/user_profile_tracker' +require_relative 'optimizely/cmab/cmab_client' +require_relative 'optimizely/cmab/cmab_service' module Optimizely class Project include Optimizely::Decide + # CMAB Constants + DEFAULT_CMAB_CACHE_TIMEOUT = (30 * 60 * 1000) + DEFAULT_CMAB_CACHE_SIZE = 1000 + attr_reader :notification_center # @api no-doc attr_reader :config_manager, :decision_service, :error_handler, :event_dispatcher, @@ -131,7 +137,20 @@ def initialize( setup_odp!(@config_manager.sdk_key) - @decision_service = DecisionService.new(@logger, @user_profile_service) + # Initialize CMAB components + @cmab_client = DefaultCmabClient.new( + nil, + CmabRetryConfig.new, + @logger + ) + @cmab_cache = LRUCache.new(DEFAULT_CMAB_CACHE_SIZE, DEFAULT_CMAB_CACHE_TIMEOUT) + @cmab_service = DefaultCmabService.new( + @cmab_cache, + @cmab_client, + @logger + ) + + @decision_service = DecisionService.new(@logger, @cmab_service, @user_profile_service) @event_processor = if event_processor.respond_to?(:process) event_processor @@ -185,18 +204,23 @@ def create_optimizely_decision(user_context, flag_key, decision, reasons, decide feature_flag = config.get_feature_flag_from_key(flag_key) experiment = nil decision_source = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] + experiment_id = nil + variation_id = nil + # 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 rule_key = experiment ? experiment['key'] : nil - variation = decision['variation'] + experiment_id = experiment ? experiment['id'] : nil + variation = decision.variation variation_key = variation ? variation['key'] : nil + variation_id = variation ? variation['id'] : nil feature_enabled = variation ? variation['featureEnabled'] : false decision_source = decision.source end if !decide_options.include?(OptimizelyDecideOption::DISABLE_DECISION_EVENT) && (decision_source == Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] || config.send_flag_decisions) - send_impression(config, experiment, variation_key || '', flag_key, rule_key || '', feature_enabled, decision_source, user_id, attributes) + send_impression(config, experiment, variation_key || '', flag_key, rule_key || '', feature_enabled, decision_source, user_id, attributes, decision&.cmab_uuid) decision_event_dispatched = true end @@ -214,14 +238,16 @@ def create_optimizely_decision(user_context, flag_key, decision, reasons, decide @notification_center.send_notifications( NotificationCenter::NOTIFICATION_TYPES[:DECISION], Helpers::Constants::DECISION_NOTIFICATION_TYPES['FLAG'], - user_id, (attributes || {}), + user_id, attributes || {}, flag_key: flag_key, enabled: feature_enabled, variables: all_variables, variation_key: variation_key, rule_key: rule_key, reasons: should_include_reasons ? reasons : [], - decision_event_dispatched: decision_event_dispatched + decision_event_dispatched: decision_event_dispatched, + experiment_id: experiment_id, + variation_id: variation_id ) OptimizelyDecision.new( @@ -330,7 +356,7 @@ def decide_for_keys(user_context, keys, decide_options = [], ignore_default_opti # 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, []) + decisions[key] = OptimizelyDecision.new(variation_key: nil, enabled: false, variables: nil, rule_key: nil, flag_key: key, user_context: user_context, reasons: []) next end valid_keys.push(key) @@ -351,9 +377,17 @@ def decide_for_keys(user_context, keys, decide_options = [], ignore_default_opti 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] + decision = decision_list[i].decision + reasons = decision_list[i].reasons + error = decision_list[i].error flag_key = flag['key'] + # store error decision against key and remove key from valid keys + if error + optimizely_decision = OptimizelyDecision.new_error_decision(flag_key, user_context, reasons) + decisions[flag_key] = optimizely_decision + valid_keys.delete(flag_key) if valid_keys.include?(flag_key) + next + end flag_decisions[flag_key] = decision decision_reasons_dict[flag_key] ||= [] decision_reasons_dict[flag_key].push(*reasons) @@ -592,8 +626,8 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil) end user_context = OptimizelyUserContext.new(self, user_id, attributes, identify: false) - decision, = @decision_service.get_variation_for_feature(config, feature_flag, user_context) - + decision_result = @decision_service.get_variation_for_feature(config, feature_flag, user_context) + decision = decision_result.decision feature_enabled = false source_string = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] if decision.is_a?(Optimizely::DecisionService::Decision) @@ -625,7 +659,7 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil) @notification_center.send_notifications( NotificationCenter::NOTIFICATION_TYPES[:DECISION], Helpers::Constants::DECISION_NOTIFICATION_TYPES['FEATURE'], - user_id, (attributes || {}), + user_id, attributes || {}, feature_key: feature_flag_key, feature_enabled: feature_enabled, source: source_string, @@ -832,7 +866,8 @@ def get_all_feature_variables(feature_flag_key, user_id, attributes = nil) end user_context = OptimizelyUserContext.new(self, user_id, attributes, identify: false) - decision, = @decision_service.get_variation_for_feature(config, feature_flag, user_context) + decision_result = @decision_service.get_variation_for_feature(config, feature_flag, user_context) + decision = decision_result.decision variation = decision ? decision['variation'] : nil feature_enabled = variation ? variation['featureEnabled'] : false all_variables = {} @@ -853,7 +888,7 @@ def get_all_feature_variables(feature_flag_key, user_id, attributes = nil) @notification_center.send_notifications( NotificationCenter::NOTIFICATION_TYPES[:DECISION], - Helpers::Constants::DECISION_NOTIFICATION_TYPES['ALL_FEATURE_VARIABLES'], user_id, (attributes || {}), + Helpers::Constants::DECISION_NOTIFICATION_TYPES['ALL_FEATURE_VARIABLES'], user_id, attributes || {}, feature_key: feature_flag_key, feature_enabled: feature_enabled, source: source_string, @@ -1022,7 +1057,8 @@ def get_variation_with_config(experiment_key, user_id, attributes, config) user_context = OptimizelyUserContext.new(self, user_id, attributes, identify: false) 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) + variation_result = @decision_service.get_variation(config, experiment_id, user_context, user_profile_tracker) + variation_id = variation_result.variation_id 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 @@ -1033,7 +1069,7 @@ def get_variation_with_config(experiment_key, user_id, attributes, config) end @notification_center.send_notifications( NotificationCenter::NOTIFICATION_TYPES[:DECISION], - decision_notification_type, user_id, (attributes || {}), + decision_notification_type, user_id, attributes || {}, experiment_key: experiment_key, variation_key: variation_key ) @@ -1090,7 +1126,8 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type, end user_context = OptimizelyUserContext.new(self, user_id, attributes, identify: false) - decision, = @decision_service.get_variation_for_feature(config, feature_flag, user_context) + decision_result = @decision_service.get_variation_for_feature(config, feature_flag, user_context) + decision = decision_result.decision variation = decision ? decision['variation'] : nil feature_enabled = variation ? variation['featureEnabled'] : false @@ -1108,7 +1145,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type, @notification_center.send_notifications( NotificationCenter::NOTIFICATION_TYPES[:DECISION], - Helpers::Constants::DECISION_NOTIFICATION_TYPES['FEATURE_VARIABLE'], user_id, (attributes || {}), + Helpers::Constants::DECISION_NOTIFICATION_TYPES['FEATURE_VARIABLE'], user_id, attributes || {}, feature_key: feature_flag_key, feature_enabled: feature_enabled, source: source_string, @@ -1208,7 +1245,7 @@ def validate_instantiation_options raise InvalidInputError, 'event_dispatcher' end - def send_impression(config, experiment, variation_key, flag_key, rule_key, enabled, rule_type, user_id, attributes = nil) + def send_impression(config, experiment, variation_key, flag_key, rule_key, enabled, rule_type, user_id, attributes = nil, cmab_uuid = nil) if experiment.nil? experiment = { 'id' => '', @@ -1240,6 +1277,7 @@ def send_impression(config, experiment, variation_key, flag_key, rule_key, enabl variation_key: variation_key, enabled: enabled } + metadata[:cmab_uuid] = cmab_uuid unless cmab_uuid.nil? user_event = UserEventFactory.create_impression_event(config, experiment, variation_id, metadata, user_id, attributes) @event_processor.process(user_event) diff --git a/lib/optimizely/audience.rb b/lib/optimizely/audience.rb index 3e919ad8..6ea523e4 100644 --- a/lib/optimizely/audience.rb +++ b/lib/optimizely/audience.rb @@ -72,6 +72,20 @@ def user_meets_audience_conditions?(config, experiment, user_context, logger, lo decide_reasons.push(message) audience_conditions = JSON.parse(audience_conditions) if audience_conditions.is_a?(String) + # Convert all symbol keys to string keys in the parsed conditions + stringify_keys = lambda do |obj| + case obj + when Hash + obj.transform_keys(&:to_s).transform_values { |v| stringify_keys.call(v) } + when Array + obj.map { |item| stringify_keys.call(item) } + else + obj + end + end + + audience_conditions = stringify_keys.call(audience_conditions) + result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_user_conditions) result_str = result.nil? ? 'UNKNOWN' : result.to_s.upcase message = format(logs_hash['AUDIENCE_EVALUATION_RESULT'], audience_id, result_str) diff --git a/lib/optimizely/bucketer.rb b/lib/optimizely/bucketer.rb index 15f711cb..d62e088d 100644 --- a/lib/optimizely/bucketer.rb +++ b/lib/optimizely/bucketer.rb @@ -44,6 +44,25 @@ def bucket(project_config, experiment, bucketing_id, user_id) # user_id - String ID for user. # # Returns variation in which visitor with ID user_id has been placed. Nil if no variation. + + variation_id, decide_reasons = bucket_to_entity_id(project_config, experiment, bucketing_id, user_id) + if variation_id && variation_id != '' + experiment_id = experiment['id'] + variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) + return variation, decide_reasons + end + + # Handle the case when the traffic range is empty due to sticky bucketing + if variation_id == '' + message = 'Bucketed into an empty traffic range. Returning nil.' + @logger.log(Logger::DEBUG, message) + decide_reasons.push(message) + end + + [nil, decide_reasons] + end + + def bucket_to_entity_id(project_config, experiment, bucketing_id, user_id) return nil, [] if experiment.nil? decide_reasons = [] @@ -84,22 +103,18 @@ def bucket(project_config, experiment, bucketing_id, user_id) end traffic_allocations = experiment['trafficAllocation'] + if experiment['cmab'] + traffic_allocations = [ + { + 'entityId' => '$', + 'endOfRange' => experiment['cmab']['trafficAllocation'] + } + ] + end variation_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations) decide_reasons.push(*find_bucket_reasons) - if variation_id && variation_id != '' - variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) - return variation, decide_reasons - end - - # Handle the case when the traffic range is empty due to sticky bucketing - if variation_id == '' - message = 'Bucketed into an empty traffic range. Returning nil.' - @logger.log(Logger::DEBUG, message) - decide_reasons.push(message) - end - - [nil, decide_reasons] + [variation_id, decide_reasons] end def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations) diff --git a/lib/optimizely/cmab/cmab_client.rb b/lib/optimizely/cmab/cmab_client.rb new file mode 100644 index 00000000..f9a21cff --- /dev/null +++ b/lib/optimizely/cmab/cmab_client.rb @@ -0,0 +1,223 @@ +# frozen_string_literal: true + +# +# Copyright 2025 Optimizely and contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +require 'optimizely/helpers/http_utils' +require 'optimizely/helpers/constants' + +module Optimizely + # Default constants for CMAB requests + DEFAULT_MAX_RETRIES = 3 + DEFAULT_INITIAL_BACKOFF = 0.1 # in seconds (100 ms) + DEFAULT_MAX_BACKOFF = 10 # in seconds + DEFAULT_BACKOFF_MULTIPLIER = 2.0 + MAX_WAIT_TIME = 10 + + class CmabRetryConfig + # Configuration for retrying CMAB requests. + # Contains parameters for maximum retries, backoff intervals, and multipliers. + attr_reader :max_retries, :initial_backoff, :max_backoff, :backoff_multiplier + + def initialize(max_retries: DEFAULT_MAX_RETRIES, initial_backoff: DEFAULT_INITIAL_BACKOFF, max_backoff: DEFAULT_MAX_BACKOFF, backoff_multiplier: DEFAULT_BACKOFF_MULTIPLIER) + @max_retries = max_retries + @initial_backoff = initial_backoff + @max_backoff = max_backoff + @backoff_multiplier = backoff_multiplier + end + end + + class DefaultCmabClient + # Client for interacting with the CMAB service. + # Provides methods to fetch decisions with optional retry logic. + + def initialize(http_client = nil, retry_config = nil, logger = nil) + # Initialize the CMAB client. + # Args: + # http_client: HTTP client for making requests. + # retry_config: Configuration for retry settings. + # logger: Logger for logging errors and info. + @http_client = http_client || DefaultHttpClient.new + @retry_config = retry_config || CmabRetryConfig.new + @logger = logger || NoOpLogger.new + end + + def fetch_decision(rule_id, user_id, attributes, cmab_uuid, timeout: MAX_WAIT_TIME) + # Fetches a decision from the CMAB service. + # Args: + # rule_id: The rule ID for the experiment. + # user_id: The user ID for the request. + # attributes: User attributes for the request. + # cmab_uuid: Unique identifier for the CMAB request. + # timeout: Maximum wait time for the request to respond in seconds. (default is 10 seconds). + # Returns: + # The variation ID. + url = "https://prediction.cmab.optimizely.com/predict/#{rule_id}" + cmab_attributes = attributes.map { |key, value| {'id' => key.to_s, 'value' => value, 'type' => 'custom_attribute'} } + + request_body = { + instances: [{ + visitorId: user_id, + experimentId: rule_id, + attributes: cmab_attributes, + cmabUUID: cmab_uuid + }] + } + + if @retry_config && @retry_config.max_retries.to_i.positive? + _do_fetch_with_retry(url, request_body, @retry_config, timeout) + else + _do_fetch(url, request_body, timeout) + end + end + + def _do_fetch(url, request_body, timeout) + # Perform a single fetch request to the CMAB prediction service. + + # Args: + # url: The endpoint URL. + # request_body: The request payload. + # timeout: Maximum wait time for the request to respond in seconds. + # Returns: + # The variation ID from the response. + + headers = {'Content-Type' => 'application/json'} + begin + response = @http_client.post(url, json: request_body, headers: headers, timeout: timeout.to_i) + rescue StandardError => e + error_message = Optimizely::Helpers::Constants::CMAB_FETCH_FAILED % e.message + @logger.log(Logger::ERROR, error_message) + raise CmabFetchError, error_message + end + + unless (200..299).include?(response.status_code) + error_message = Optimizely::Helpers::Constants::CMAB_FETCH_FAILED % response.status_code + @logger.log(Logger::ERROR, error_message) + raise CmabFetchError, error_message + end + + begin + body = response.json + rescue JSON::ParserError, Optimizely::CmabInvalidResponseError + error_message = Optimizely::Helpers::Constants::INVALID_CMAB_FETCH_RESPONSE + @logger.log(Logger::ERROR, error_message) + raise CmabInvalidResponseError, error_message + end + + unless validate_response(body) + error_message = Optimizely::Helpers::Constants::INVALID_CMAB_FETCH_RESPONSE + @logger.log(Logger::ERROR, error_message) + raise CmabInvalidResponseError, error_message + end + + body['predictions'][0]['variation_id'] + end + + def validate_response(body) + # Validate the response structure from the CMAB service. + # Args: + # body: The JSON response body to validate. + # Returns: + # true if valid, false otherwise. + + body.is_a?(Hash) && + body.key?('predictions') && + body['predictions'].is_a?(Array) && + !body['predictions'].empty? && + body['predictions'][0].is_a?(Hash) && + body['predictions'][0].key?('variation_id') + end + + def _do_fetch_with_retry(url, request_body, retry_config, timeout) + # Perform a fetch request with retry logic. + # Args: + # url: The endpoint URL. + # request_body: The request payload. + # retry_config: Configuration for retry settings. + # timeout: Maximum wait time for the request to respond in seconds. + # Returns: + # The variation ID from the response. + + backoff = retry_config.initial_backoff + + (0..retry_config.max_retries).each do |attempt| + variation_id = _do_fetch(url, request_body, timeout) + return variation_id + rescue StandardError => e + if attempt < retry_config.max_retries + @logger.log(Logger::INFO, "Retrying CMAB request (attempt #{attempt + 1}) after #{backoff} seconds...") + Kernel.sleep(backoff) + + backoff = [ + backoff * retry_config.backoff_multiplier, + retry_config.max_backoff + ].min + else + @logger.log(Logger::ERROR, "Max retries exceeded for CMAB request: #{e.message}") + raise Optimizely::CmabFetchError, "CMAB decision fetch failed (#{e.message})." + end + end + end + end + + class DefaultHttpClient + # Default HTTP client for making requests. + # Uses Optimizely::Helpers::HttpUtils to make requests. + + def post(url, json: nil, headers: {}, timeout: nil) + # Makes a POST request to the specified URL with JSON body and headers. + # Args: + # url: The endpoint URL. + # json: The JSON payload to send in the request body. + # headers: Additional headers for the request. + # timeout: Maximum wait time for the request to respond in seconds. + # Returns: + # The response object. + + response = Optimizely::Helpers::HttpUtils.make_request(url, :post, json.to_json, headers, timeout) + + HttpResponseAdapter.new(response) + end + + class HttpResponseAdapter + # Adapter for HTTP response to provide a consistent interface. + # Args: + # response: The raw HTTP response object. + + def initialize(response) + @response = response + end + + def status_code + @response.code.to_i + end + + def json + JSON.parse(@response.body) + rescue JSON::ParserError + raise Optimizely::CmabInvalidResponseError, Optimizely::Helpers::Constants::INVALID_CMAB_FETCH_RESPONSE + end + + def body + @response.body + end + end + end + + class NoOpLogger + # A no-operation logger that does nothing. + def log(_level, _message); end + end +end diff --git a/lib/optimizely/cmab/cmab_service.rb b/lib/optimizely/cmab/cmab_service.rb new file mode 100644 index 00000000..ceed3066 --- /dev/null +++ b/lib/optimizely/cmab/cmab_service.rb @@ -0,0 +1,156 @@ +# frozen_string_literal: true + +# +# Copyright 2025 Optimizely and contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +require 'optimizely/odp/lru_cache' +require 'optimizely/decide/optimizely_decide_option' +require 'digest' +require 'json' +require 'securerandom' + +module Optimizely + CmabDecision = Struct.new(:variation_id, :cmab_uuid, keyword_init: true) + CmabCacheValue = Struct.new(:attributes_hash, :variation_id, :cmab_uuid, keyword_init: true) + + # Default CMAB service implementation + class DefaultCmabService + # Initializes a new instance of the CmabService. + # + # @param cmab_cache [LRUCache] The cache object used for storing CMAB data. Must be an instance of LRUCache. + # @param cmab_client [DefaultCmabClient] The client used to interact with the CMAB service. Must be an instance of DefaultCmabClient. + # @param logger [Logger, nil] Optional logger for logging messages. Defaults to nil. + # + # @raise [ArgumentError] If cmab_cache is not an instance of LRUCache. + # @raise [ArgumentError] If cmab_client is not an instance of DefaultCmabClient. + def initialize(cmab_cache, cmab_client, logger = nil) + @cmab_cache = cmab_cache + @cmab_client = cmab_client + @logger = logger + end + + def get_decision(project_config, user_context, rule_id, options) + # Retrieves a decision for a given user and rule, utilizing a cache for efficiency. + # + # This method filters user attributes, checks for various cache-related options, + # and either fetches a fresh decision or returns a cached one if appropriate. + # It supports options to ignore the cache, reset the cache, or invalidate a specific user's cache entry. + # + # @param project_config [Object] The project configuration object. + # @param user_context [Object] The user context containing user_id and attributes. + # @param rule_id [String] The identifier for the decision rule. + # @param options [Array, nil] Optional flags to control cache behavior. Supported options: + # - OptimizelyDecideOption::IGNORE_CMAB_CACHE: Bypass cache and fetch a new decision. + # - OptimizelyDecideOption::RESET_CMAB_CACHE: Reset the entire cache. + # - OptimizelyDecideOption::INVALIDATE_USER_CMAB_CACHE: Invalidate cache for the specific user and rule. + # + # @return [CmabDecision] The decision object containing variation_id and cmab_uuid. + + filtered_attributes = filter_attributes(project_config, user_context, rule_id) + + return fetch_decision(rule_id, user_context.user_id, filtered_attributes) if options&.include?(Decide::OptimizelyDecideOption::IGNORE_CMAB_CACHE) + + @cmab_cache.reset if options&.include?(Decide::OptimizelyDecideOption::RESET_CMAB_CACHE) + + cache_key = get_cache_key(user_context.user_id, rule_id) + + @cmab_cache.remove(cache_key) if options&.include?(Decide::OptimizelyDecideOption::INVALIDATE_USER_CMAB_CACHE) + cached_value = @cmab_cache.lookup(cache_key) + attributes_hash = hash_attributes(filtered_attributes) + + if cached_value + return CmabDecision.new(variation_id: cached_value.variation_id, cmab_uuid: cached_value.cmab_uuid) if cached_value.attributes_hash == attributes_hash + + @cmab_cache.remove(cache_key) + end + cmab_decision = fetch_decision(rule_id, user_context.user_id, filtered_attributes) + @cmab_cache.save(cache_key, + CmabCacheValue.new( + attributes_hash: attributes_hash, + variation_id: cmab_decision.variation_id, + cmab_uuid: cmab_decision.cmab_uuid + )) + cmab_decision + end + + private + + def fetch_decision(rule_id, user_id, attributes) + # Fetches a decision for a given rule and user, along with user attributes. + # + # Generates a unique UUID for the decision request, then delegates to the CMAB client + # to fetch the variation ID. Returns a CmabDecision object containing the variation ID + # and the generated UUID. + # + # @param rule_id [String] The identifier for the rule to evaluate. + # @param user_id [String] The identifier for the user. + # @param attributes [Hash] A hash of user attributes to be used in decision making. + # @return [CmabDecision] The decision object containing the variation ID and UUID. + cmab_uuid = SecureRandom.uuid + variation_id = @cmab_client.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + CmabDecision.new(variation_id: variation_id, cmab_uuid: cmab_uuid) + end + + def filter_attributes(project_config, user_context, rule_id) + # Filters the user attributes based on the CMAB attribute IDs defined in the experiment. + # + # @param project_config [Object] The project configuration object containing experiment and attribute mappings. + # @param user_context [Object] The user context object containing user attributes. + # @param rule_id [String] The ID of the experiment (rule) to filter attributes for. + # @return [Hash] A hash of filtered user attributes whose keys match the CMAB attribute IDs for the given experiment. + user_attributes = user_context.user_attributes + filtered_user_attributes = {} + + experiment = project_config.experiment_id_map[rule_id] + return filtered_user_attributes if experiment.nil? || experiment['cmab'].nil? + + cmab_attribute_ids = experiment['cmab']['attributeIds'] + cmab_attribute_ids.each do |attribute_id| + attribute = project_config.attribute_id_map[attribute_id] + next unless attribute + + attribute_key = attribute['key'] + filtered_user_attributes[attribute_key] = user_attributes[attribute_key] if user_attributes.key?(attribute_key) + end + + filtered_user_attributes + end + + def get_cache_key(user_id, rule_id) + # Generates a cache key string based on the provided user ID and rule ID. + # + # The cache key is constructed in the format: "--", + # where is the length of the user_id string. + # + # @param user_id [String] The unique identifier for the user. + # @param rule_id [String] The unique identifier for the rule. + # @return [String] The generated cache key. + "#{user_id.length}-#{user_id}-#{rule_id}" + end + + def hash_attributes(attributes) + # Generates an MD5 hash for a given attributes hash. + # + # The method sorts the attributes by key, serializes them to a JSON string, + # and then computes the MD5 hash of the resulting string. This ensures that + # the hash is consistent regardless of the original key order in the input hash. + # + # @param attributes [Hash] The attributes to be hashed. + # @return [String] The MD5 hash of the sorted and serialized attributes. + sorted_attrs = JSON.generate(attributes.sort.to_h) + Digest::MD5.hexdigest(sorted_attrs) + end + end +end diff --git a/lib/optimizely/config/datafile_project_config.rb b/lib/optimizely/config/datafile_project_config.rb index 25357133..8b7b00ba 100644 --- a/lib/optimizely/config/datafile_project_config.rb +++ b/lib/optimizely/config/datafile_project_config.rb @@ -27,12 +27,13 @@ class DatafileProjectConfig < ProjectConfig attr_reader :datafile, :account_id, :attributes, :audiences, :typed_audiences, :events, :experiments, :feature_flags, :groups, :project_id, :bot_filtering, :revision, :sdk_key, :environment_key, :rollouts, :version, :send_flag_decisions, - :attribute_key_map, :audience_id_map, :event_key_map, :experiment_feature_map, + :attribute_key_map, :attribute_id_to_key_map, :attribute_id_map, + :audience_id_map, :event_key_map, :experiment_feature_map, :experiment_id_map, :experiment_key_map, :feature_flag_key_map, :feature_variable_key_map, :group_id_map, :rollout_id_map, :rollout_experiment_id_map, :variation_id_map, :variation_id_to_variable_usage_map, :variation_key_map, :variation_id_map_by_experiment_id, :variation_key_map_by_experiment_id, :flag_variation_map, :integration_key_map, :integrations, - :public_key_for_odp, :host_for_odp, :all_segments + :public_key_for_odp, :host_for_odp, :all_segments, :region # Boolean - denotes if Optimizely should remove the last block of visitors' IP address before storing event data attr_reader :anonymize_ip @@ -68,6 +69,10 @@ def initialize(datafile, logger, error_handler) @rollouts = config.fetch('rollouts', []) @send_flag_decisions = config.fetch('sendFlagDecisions', false) @integrations = config.fetch('integrations', []) + @region = config.fetch('region', 'US') + + # Default to US region if not specified + @region = 'US' if @region.nil? || @region.empty? # Json type is represented in datafile as a subtype of string for the sake of backwards compatibility. # Converting it to a first-class json type while creating Project Config @@ -82,6 +87,11 @@ def initialize(datafile, logger, error_handler) # Utility maps for quick lookup @attribute_key_map = generate_key_map(@attributes, 'key') + @attribute_id_map = generate_key_map(@attributes, 'id') + @attribute_id_to_key_map = {} + @attributes.each do |attribute| + @attribute_id_to_key_map[attribute['id']] = attribute['key'] + end @event_key_map = generate_key_map(@events, 'key') @group_id_map = generate_key_map(@groups, 'id') @group_id_map.each do |key, group| @@ -440,6 +450,40 @@ def get_attribute_id(attribute_key) nil end + def get_attribute_by_key(attribute_key) + # Get attribute for the provided attribute key. + # + # Args: + # Attribute key for which attribute is to be fetched. + # + # Returns: + # Attribute corresponding to the provided attribute key. + attribute = @attribute_key_map[attribute_key] + return attribute if attribute + + invalid_attribute_error = InvalidAttributeError.new(attribute_key) + @logger.log Logger::ERROR, invalid_attribute_error.message + @error_handler.handle_error invalid_attribute_error + nil + end + + def get_attribute_key_by_id(attribute_id) + # Get attribute key for the provided attribute ID. + # + # Args: + # Attribute ID for which attribute is to be fetched. + # + # Returns: + # Attribute key corresponding to the provided attribute ID. + attribute = @attribute_id_to_key_map[attribute_id] + return attribute if attribute + + invalid_attribute_error = InvalidAttributeError.new(attribute_id) + @logger.log Logger::ERROR, invalid_attribute_error.message + @error_handler.handle_error invalid_attribute_error + nil + end + def variation_id_exists?(experiment_id, variation_id) # Determines if a given experiment ID / variation ID pair exists in the datafile # diff --git a/lib/optimizely/decide/optimizely_decide_option.rb b/lib/optimizely/decide/optimizely_decide_option.rb index f89dcd51..1b6781c2 100644 --- a/lib/optimizely/decide/optimizely_decide_option.rb +++ b/lib/optimizely/decide/optimizely_decide_option.rb @@ -23,6 +23,9 @@ module OptimizelyDecideOption IGNORE_USER_PROFILE_SERVICE = 'IGNORE_USER_PROFILE_SERVICE' INCLUDE_REASONS = 'INCLUDE_REASONS' EXCLUDE_VARIABLES = 'EXCLUDE_VARIABLES' + IGNORE_CMAB_CACHE = 'IGNORE_CMAB_CACHE' + RESET_CMAB_CACHE = 'RESET_CMAB_CACHE' + INVALIDATE_USER_CMAB_CACHE = 'INVALIDATE_USER_CMAB_CACHE' end end end diff --git a/lib/optimizely/decide/optimizely_decision.rb b/lib/optimizely/decide/optimizely_decision.rb index 06b109b3..ea1964d3 100644 --- a/lib/optimizely/decide/optimizely_decision.rb +++ b/lib/optimizely/decide/optimizely_decision.rb @@ -55,6 +55,25 @@ def as_json def to_json(*args) as_json.to_json(*args) end + + # Create a new OptimizelyDecision representing an error state. + # + # @param key [String] The flag key + # @param user [OptimizelyUserContext] The user context + # @param reasons [Array] List of reasons explaining the error + # + # @return [OptimizelyDecision] OptimizelyDecision with error state values + def self.new_error_decision(key, user, reasons = []) + new( + variation_key: nil, + enabled: false, + variables: {}, + rule_key: nil, + flag_key: key, + user_context: user, + reasons: reasons + ) + end end end end diff --git a/lib/optimizely/decision_service.rb b/lib/optimizely/decision_service.rb index 3303907d..a97bf4d6 100644 --- a/lib/optimizely/decision_service.rb +++ b/lib/optimizely/decision_service.rb @@ -29,7 +29,8 @@ class DecisionService # 3. Check whitelisting # 4. Check user profile service for past bucketing decisions (sticky bucketing) # 5. Check audience targeting - # 6. Use Murmurhash3 to bucket the user + # 6. Check cmab service + # 7. Use Murmurhash3 to bucket the user attr_reader :bucketer @@ -37,7 +38,10 @@ class DecisionService # This contains all the forced variations set by the user by calling setForcedVariation. attr_reader :forced_variation_map - Decision = Struct.new(:experiment, :variation, :source) + Decision = Struct.new(:experiment, :variation, :source, :cmab_uuid) + CmabDecisionResult = Struct.new(:error, :result, :reasons) + VariationResult = Struct.new(:cmab_uuid, :error, :reasons, :variation_id) + DecisionResult = Struct.new(:decision, :error, :reasons) DECISION_SOURCES = { 'EXPERIMENT' => 'experiment', @@ -45,11 +49,12 @@ class DecisionService 'ROLLOUT' => 'rollout' }.freeze - def initialize(logger, user_profile_service = nil) + def initialize(logger, cmab_service, user_profile_service = nil) @logger = logger @user_profile_service = user_profile_service @bucketer = Bucketer.new(logger) @forced_variation_map = {} + @cmab_service = cmab_service end def get_variation(project_config, experiment_id, user_context, user_profile_tracker = nil, decide_options = [], reasons = []) @@ -61,8 +66,7 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac # 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) + # Returns VariationResult struct 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) @@ -73,25 +77,25 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac decide_reasons.push(*bucketing_id_reasons) # Check to make sure experiment is active experiment = project_config.get_experiment_from_id(experiment_id) - return nil, decide_reasons if experiment.nil? + return VariationResult.new(nil, false, decide_reasons, nil) if experiment.nil? experiment_key = experiment['key'] unless project_config.experiment_running?(experiment) message = "Experiment '#{experiment_key}' is not running." @logger.log(Logger::INFO, message) decide_reasons.push(message) - return nil, decide_reasons + return VariationResult.new(nil, false, decide_reasons, nil) end # Check if a forced variation is set for the user forced_variation, reasons_received = get_forced_variation(project_config, experiment['key'], user_id) decide_reasons.push(*reasons_received) - return forced_variation['id'], decide_reasons if forced_variation + return VariationResult.new(nil, false, decide_reasons, forced_variation['id']) if forced_variation # Check if user is in a white-listed variation whitelisted_variation_id, reasons_received = get_whitelisted_variation_id(project_config, experiment_id, user_id) decide_reasons.push(*reasons_received) - return whitelisted_variation_id, decide_reasons if whitelisted_variation_id + return VariationResult.new(nil, false, decide_reasons, whitelisted_variation_id) if whitelisted_variation_id 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 @@ -102,7 +106,7 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac message = "Returning previously activated variation ID #{saved_variation_id} of experiment '#{experiment_key}' for user '#{user_id}' from user profile." @logger.log(Logger::INFO, message) decide_reasons.push(message) - return saved_variation_id, decide_reasons + return VariationResult.new(nil, false, decide_reasons, saved_variation_id) end end @@ -113,27 +117,45 @@ def get_variation(project_config, experiment_id, user_context, user_profile_trac message = "User '#{user_id}' does not meet the conditions to be in experiment '#{experiment_key}'." @logger.log(Logger::INFO, message) decide_reasons.push(message) - return nil, decide_reasons + return VariationResult.new(nil, false, decide_reasons, nil) end - # Bucket normally - variation, bucket_reasons = @bucketer.bucket(project_config, experiment, bucketing_id, user_id) - decide_reasons.push(*bucket_reasons) - variation_id = variation ? variation['id'] : nil + # Check if this is a CMAB experiment + # If so, handle CMAB-specific traffic allocation and decision logic. + # Otherwise, proceed with standard bucketing logic for non-CMAB experiments. + if experiment.key?('cmab') + cmab_decision_result = get_decision_for_cmab_experiment(project_config, experiment, user_context, bucketing_id, decide_options) + decide_reasons.push(*cmab_decision_result.reasons) + if cmab_decision_result.error + # CMAB decision failed, return error + return VariationResult.new(nil, true, decide_reasons, nil) + end - message = '' - if variation_id - variation_key = variation['key'] - message = "User '#{user_id}' is in variation '#{variation_key}' of experiment '#{experiment_id}'." + cmab_decision = cmab_decision_result.result + variation_id = cmab_decision&.variation_id + cmab_uuid = cmab_decision&.cmab_uuid + variation = variation_id ? project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) : nil else - message = "User '#{user_id}' is in no variation." + # Bucket normally + variation, bucket_reasons = @bucketer.bucket(project_config, experiment, bucketing_id, user_id) + decide_reasons.push(*bucket_reasons) + variation_id = variation ? variation['id'] : nil + cmab_uuid = nil end + + variation_key = variation['key'] if variation + message = if variation_id + "User '#{user_id}' is in variation '#{variation_key}' of experiment '#{experiment_id}'." + else + "User '#{user_id}' is in no variation." + end + @logger.log(Logger::INFO, message) - decide_reasons.push(message) + 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 - [variation_id, decide_reasons] + VariationResult.new(cmab_uuid, false, decide_reasons, variation_id) end def get_variation_for_feature(project_config, feature_flag, user_context, decide_options = []) @@ -143,7 +165,7 @@ def get_variation_for_feature(project_config, feature_flag, user_context, decide # feature_flag - The feature flag the user wants to access # user_context - Optimizely user context instance # - # Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature) + # Returns DecisionResult struct. get_variations_for_feature_list(project_config, [feature_flag], user_context, decide_options).first end @@ -157,7 +179,7 @@ def get_variations_for_feature_list(project_config, feature_flags, user_context, # decide_options: Decide options. # # Returns: - # Array of Decision struct. + # Array of DecisionResult struct. ignore_ups = decide_options.include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE user_profile_tracker = nil unless ignore_ups && @user_profile_service @@ -166,18 +188,15 @@ def get_variations_for_feature_list(project_config, feature_flags, user_context, 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] + decision_result = get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options) + # Only process rollout if no experiment decision was found and no error + if decision_result.decision.nil? && !decision_result.error + decision_result_rollout = get_variation_for_feature_rollout(project_config, feature_flag, user_context) unless decision_result.decision + decision_result.decision = decision_result_rollout.decision + decision_result.reasons.push(*decision_result_rollout.reasons) end + decisions << decision_result end user_profile_tracker&.save_user_profile decisions @@ -190,8 +209,8 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_cont # feature_flag - The feature flag the user wants to access # user_context - Optimizely user context instance # - # Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature) - # or nil if the user is not bucketed into any of the experiments on the feature + # Returns a DecisionResult containing the decision (or nil if not bucketed), + # an error flag, and an array of decision reasons. decide_reasons = [] user_id = user_context.user_id feature_flag_key = feature_flag['key'] @@ -199,7 +218,7 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_cont message = "The feature flag '#{feature_flag_key}' is not used in any experiments." @logger.log(Logger::DEBUG, message) decide_reasons.push(message) - return nil, decide_reasons + return DecisionResult.new(nil, false, decide_reasons) end # Evaluate each experiment and return the first bucketed experiment variation @@ -209,26 +228,34 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_cont message = "Feature flag experiment with ID '#{experiment_id}' is not in the datafile." @logger.log(Logger::DEBUG, message) decide_reasons.push(message) - return nil, decide_reasons + return DecisionResult.new(nil, false, decide_reasons) end experiment_id = experiment['id'] - variation_id, reasons_received = get_variation_from_experiment_rule(project_config, feature_flag_key, experiment, user_context, user_profile_tracker, decide_options) + variation_result = get_variation_from_experiment_rule(project_config, feature_flag_key, experiment, user_context, user_profile_tracker, decide_options) + error = variation_result.error + reasons_received = variation_result.reasons + variation_id = variation_result.variation_id + cmab_uuid = variation_result.cmab_uuid decide_reasons.push(*reasons_received) + # If there's an error, return immediately instead of falling back to next experiment + return DecisionResult.new(nil, error, decide_reasons) if error + next unless variation_id variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) variation = project_config.get_variation_from_flag(feature_flag['key'], variation_id, 'id') if variation.nil? - return Decision.new(experiment, variation, DECISION_SOURCES['FEATURE_TEST']), decide_reasons + decision = Decision.new(experiment, variation, DECISION_SOURCES['FEATURE_TEST'], cmab_uuid) + return DecisionResult.new(decision, error, decide_reasons) end message = "The user '#{user_id}' is not bucketed into any of the experiments on the feature '#{feature_flag_key}'." @logger.log(Logger::INFO, message) decide_reasons.push(message) - [nil, decide_reasons] + DecisionResult.new(nil, false, decide_reasons) end def get_variation_for_feature_rollout(project_config, feature_flag, user_context) @@ -239,7 +266,8 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context # feature_flag - The feature flag the user wants to access # user_context - Optimizely user context instance # - # Returns the Decision struct or nil if not bucketed into any of the targeting rules + # Returns a DecisionResult containing the decision (or nil if not bucketed), + # an error flag, and an array of decision reasons. decide_reasons = [] rollout_id = feature_flag['rolloutId'] @@ -248,7 +276,7 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context message = "Feature flag '#{feature_flag_key}' is not used in a rollout." @logger.log(Logger::DEBUG, message) decide_reasons.push(message) - return nil, decide_reasons + return DecisionResult.new(nil, false, decide_reasons) end rollout = project_config.get_rollout_from_id(rollout_id) @@ -256,10 +284,10 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context message = "Rollout with ID '#{rollout_id}' is not in the datafile '#{feature_flag['key']}'" @logger.log(Logger::DEBUG, message) decide_reasons.push(message) - return nil, decide_reasons + return DecisionResult.new(nil, false, decide_reasons) end - return nil, decide_reasons if rollout['experiments'].empty? + return DecisionResult.new(nil, false, decide_reasons) if rollout['experiments'].empty? index = 0 rollout_rules = rollout['experiments'] @@ -268,14 +296,14 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context decide_reasons.push(*reasons_received) if variation rule = rollout_rules[index] - feature_decision = Decision.new(rule, variation, DECISION_SOURCES['ROLLOUT']) - return [feature_decision, decide_reasons] + feature_decision = Decision.new(rule, variation, DECISION_SOURCES['ROLLOUT'], nil) + return DecisionResult.new(feature_decision, false, decide_reasons) end index = skip_to_everyone_else ? (rollout_rules.length - 1) : (index + 1) end - [nil, decide_reasons] + DecisionResult.new(nil, false, decide_reasons) end def get_variation_from_experiment_rule(project_config, flag_key, rule, user, user_profile_tracker, options = []) @@ -293,13 +321,11 @@ def get_variation_from_experiment_rule(project_config, flag_key, rule, user, use context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(flag_key, rule['key']) variation, forced_reasons = validated_forced_decision(project_config, context, user) reasons.push(*forced_reasons) + return VariationResult.new(nil, false, reasons, variation['id']) if variation - return [variation['id'], reasons] if variation - - variation_id, response_reasons = get_variation(project_config, rule['id'], user, user_profile_tracker, options) - reasons.push(*response_reasons) - - [variation_id, reasons] + variation_result = get_variation(project_config, rule['id'], user, user_profile_tracker, options) + variation_result.reasons = reasons + variation_result.reasons + variation_result end def get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index, user_context) @@ -467,6 +493,50 @@ def validated_forced_decision(project_config, context, user_context) private + def get_decision_for_cmab_experiment(project_config, experiment, user_context, bucketing_id, decide_options = []) + # Determines the CMAB (Contextual Multi-Armed Bandit) decision for a given experiment and user context. + # + # This method first checks if the user is bucketed into the CMAB experiment based on traffic allocation. + # If the user is not bucketed, it returns a CmabDecisionResult indicating exclusion. + # If the user is bucketed, it attempts to fetch a CMAB decision using the CMAB service. + # In case of errors during CMAB decision retrieval, it logs the error and returns a result indicating failure. + # + # @param project_config [ProjectConfig] The current project configuration. + # @param experiment [Hash] The experiment configuration hash. + # @param user_context [OptimizelyUserContext] The user context object containing user information. + # @param bucketing_id [String] The bucketing ID used for traffic allocation. + # @param decide_options [Array] Optional array of decision options. + # + # @return [CmabDecisionResult] The result of the CMAB decision process, including decision error status, decision data, and reasons. + decide_reasons = [] + user_id = user_context.user_id + + # Check if user is in CMAB traffic allocation + bucketed_entity_id, bucket_reasons = @bucketer.bucket_to_entity_id( + project_config, experiment, bucketing_id, user_id + ) + decide_reasons.push(*bucket_reasons) + unless bucketed_entity_id + message = "User \"#{user_context.user_id}\" not in CMAB experiment \"#{experiment['key']}\" due to traffic allocation." + @logger.log(Logger::INFO, message) + decide_reasons.push(message) + return CmabDecisionResult.new(false, nil, decide_reasons) + end + + # User is in CMAB allocation, proceed to CMAB decision + begin + cmab_decision = @cmab_service.get_decision( + project_config, user_context, experiment['id'], decide_options + ) + CmabDecisionResult.new(false, cmab_decision, decide_reasons) + rescue StandardError => e + error_message = "Failed to fetch CMAB data for experiment #{experiment['key']}." + decide_reasons.push(error_message) + @logger&.log(Logger::ERROR, "#{error_message} #{e}") + CmabDecisionResult.new(true, nil, decide_reasons) + end + end + def get_whitelisted_variation_id(project_config, experiment_id, user_id) # Determine if a user is whitelisted into a variation for the given experiment and return the ID of that variation # diff --git a/lib/optimizely/event/entity/event_context.rb b/lib/optimizely/event/entity/event_context.rb index 65f8f18e..f26b30a2 100644 --- a/lib/optimizely/event/entity/event_context.rb +++ b/lib/optimizely/event/entity/event_context.rb @@ -26,7 +26,8 @@ def initialize( anonymize_ip:, revision:, client_name:, - client_version: + client_version:, + region: ) @account_id = account_id @project_id = project_id @@ -34,6 +35,7 @@ def initialize( @revision = revision @client_name = client_name @client_version = client_version + @region = region end def as_json @@ -43,7 +45,8 @@ def as_json anonymize_ip: @anonymize_ip, revision: @revision, client_name: @client_name, - client_version: @client_version + client_version: @client_version, + region: @region } end end diff --git a/lib/optimizely/event/event_factory.rb b/lib/optimizely/event/event_factory.rb index 9ac8a937..b1afa103 100644 --- a/lib/optimizely/event/event_factory.rb +++ b/lib/optimizely/event/event_factory.rb @@ -28,7 +28,10 @@ class EventFactory # EventFactory builds LogEvent objects from a given user_event. class << self CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom' - ENDPOINT = 'https://logx.optimizely.com/v1/events' + ENDPOINTS = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + }.freeze POST_HEADERS = {'Content-Type' => 'application/json'}.freeze ACTIVATE_EVENT_KEY = 'campaign_activated' @@ -67,7 +70,10 @@ def create_log_event(user_events, logger) builder.with_visitors(visitors) event_batch = builder.build - Event.new(:post, ENDPOINT, event_batch.as_json, POST_HEADERS) + + endpoint = ENDPOINTS[user_context[:region].to_s.upcase.to_sym] || ENDPOINTS[:US] + + Event.new(:post, endpoint, event_batch.as_json, POST_HEADERS) end def build_attribute_list(user_attributes, project_config) diff --git a/lib/optimizely/event/user_event_factory.rb b/lib/optimizely/event/user_event_factory.rb index f7852341..872a70b8 100644 --- a/lib/optimizely/event/user_event_factory.rb +++ b/lib/optimizely/event/user_event_factory.rb @@ -33,6 +33,7 @@ def self.create_impression_event(project_config, experiment, variation_id, metad # # Returns Event encapsulating the impression event. event_context = Optimizely::EventContext.new( + region: project_config.region, account_id: project_config.account_id, project_id: project_config.project_id, anonymize_ip: project_config.anonymize_ip, @@ -67,6 +68,7 @@ def self.create_conversion_event(project_config, event, user_id, user_attributes # Returns Event encapsulating the conversion event. event_context = Optimizely::EventContext.new( + region: project_config.region, account_id: project_config.account_id, project_id: project_config.project_id, anonymize_ip: project_config.anonymize_ip, diff --git a/lib/optimizely/event_builder.rb b/lib/optimizely/event_builder.rb index 4c743cc3..a5ee82a9 100644 --- a/lib/optimizely/event_builder.rb +++ b/lib/optimizely/event_builder.rb @@ -101,13 +101,17 @@ def get_common_params(project_config, user_id, attributes) revision: project_config.revision, client_name: CLIENT_ENGINE, enrich_decisions: true, - client_version: VERSION + client_version: VERSION, + region: project_config.region || 'US' } end end class EventBuilder < BaseEventBuilder - ENDPOINT = 'https://logx.optimizely.com/v1/events' + ENDPOINTS = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + }.freeze POST_HEADERS = {'Content-Type' => 'application/json'}.freeze ACTIVATE_EVENT_KEY = 'campaign_activated' @@ -122,11 +126,14 @@ def create_impression_event(project_config, experiment, variation_id, user_id, a # # Returns +Event+ encapsulating the impression event. + region = project_config.region || 'US' event_params = get_common_params(project_config, user_id, attributes) impression_params = get_impression_params(project_config, experiment, variation_id) event_params[:visitors][0][:snapshots].push(impression_params) - Event.new(:post, ENDPOINT, event_params, POST_HEADERS) + endpoint = ENDPOINTS[region.to_s.upcase.to_sym] + + Event.new(:post, endpoint, event_params, POST_HEADERS) end def create_conversion_event(project_config, event, user_id, attributes, event_tags) @@ -140,11 +147,14 @@ def create_conversion_event(project_config, event, user_id, attributes, event_ta # # Returns +Event+ encapsulating the conversion event. + region = project_config.region || 'US' event_params = get_common_params(project_config, user_id, attributes) conversion_params = get_conversion_params(event, event_tags) event_params[:visitors][0][:snapshots] = [conversion_params] - Event.new(:post, ENDPOINT, event_params, POST_HEADERS) + endpoint = ENDPOINTS[region.to_s.upcase.to_sym] + + Event.new(:post, endpoint, event_params, POST_HEADERS) end private diff --git a/lib/optimizely/exceptions.rb b/lib/optimizely/exceptions.rb index 5d608b2f..073433af 100644 --- a/lib/optimizely/exceptions.rb +++ b/lib/optimizely/exceptions.rb @@ -190,4 +190,28 @@ def initialize(msg = 'Provided semantic version is invalid.') super end end + + class CmabError < Error + # Base exception for CMAB errors + + def initialize(msg = 'CMAB error occurred.') + super + end + end + + class CmabFetchError < CmabError + # Exception raised when CMAB fetch fails + + def initialize(msg = 'CMAB decision fetch failed with status:') + super + end + end + + class CmabInvalidResponseError < CmabError + # Exception raised when CMAB fetch returns an invalid response + + def initialize(msg = 'Invalid CMAB fetch response') + super + end + end end diff --git a/lib/optimizely/helpers/constants.rb b/lib/optimizely/helpers/constants.rb index 02b815ae..af3e5a08 100644 --- a/lib/optimizely/helpers/constants.rb +++ b/lib/optimizely/helpers/constants.rb @@ -201,6 +201,9 @@ module Constants }, 'forcedVariations' => { 'type' => 'object' + }, + 'cmab' => { + 'type' => 'object' } }, 'required' => %w[ @@ -303,6 +306,18 @@ module Constants }, 'required' => %w[key] } + }, + 'cmab' => { + 'type' => 'object', + 'properties' => { + 'attributeIds' => { + 'type' => 'array', + 'items' => {'type' => 'string'} + }, + 'trafficAllocation' => { + 'type' => 'integer' + } + } } }, 'required' => %w[ @@ -454,6 +469,9 @@ module Constants 'IF_MODIFIED_SINCE' => 'If-Modified-Since', 'LAST_MODIFIED' => 'Last-Modified' }.freeze + + CMAB_FETCH_FAILED = 'CMAB decision fetch failed (%s).' + INVALID_CMAB_FETCH_RESPONSE = 'Invalid CMAB fetch response' end end end diff --git a/lib/optimizely/helpers/validator.rb b/lib/optimizely/helpers/validator.rb index 4d975483..d3baa447 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 # rubocop:disable Lint/UselessAssignment + is_valid = true if variables.include? :user_id # Empty str is a valid user ID. unless variables[:user_id].is_a?(String) - is_valid = false # rubocop:disable Lint/UselessAssignment + is_valid = false logger.log(level, "#{Constants::INPUT_VARIABLES['USER_ID']} is invalid") end variables.delete :user_id diff --git a/lib/optimizely/odp/lru_cache.rb b/lib/optimizely/odp/lru_cache.rb index 8ce61549..6d4c9af3 100644 --- a/lib/optimizely/odp/lru_cache.rb +++ b/lib/optimizely/odp/lru_cache.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # -# Copyright 2022, Optimizely and contributors +# Copyright 2022-2025, Optimizely and contributors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -91,6 +91,19 @@ def peek(key) @cache_mutex.synchronize { @map[key]&.value } end + + # Remove the element associated with the provided key from the cache + # + # @param key - The key to remove + + def remove(key) + return if @capacity <= 0 + + @cache_mutex.synchronize do + @map.delete(key) + end + nil + end end class CacheElement diff --git a/lib/optimizely/project_config.rb b/lib/optimizely/project_config.rb index b0d43aa3..b5094b62 100644 --- a/lib/optimizely/project_config.rb +++ b/lib/optimizely/project_config.rb @@ -62,6 +62,8 @@ def host_for_odp; end def all_segments; end + def region; end + def experiment_running?(experiment); end def get_experiment_from_key(experiment_key); end @@ -86,6 +88,10 @@ def get_whitelisted_variations(experiment_id); end def get_attribute_id(attribute_key); end + def get_attribute_by_key(attribute_key); end + + def get_attribute_key_by_id(attribute_id); end + def variation_id_exists?(experiment_id, variation_id); end def get_feature_flag_from_key(feature_flag_key); end diff --git a/spec/cmab/cmab_client_spec.rb b/spec/cmab/cmab_client_spec.rb new file mode 100644 index 00000000..daa9ccd4 --- /dev/null +++ b/spec/cmab/cmab_client_spec.rb @@ -0,0 +1,198 @@ +# frozen_string_literal: true + +# +# Copyright 2025 Optimizely and contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +require 'spec_helper' +require 'optimizely/logger' +require 'optimizely/cmab/cmab_client' +require 'webmock/rspec' + +describe Optimizely::DefaultCmabClient do + let(:spy_logger) { spy('logger') } + let(:retry_config) { Optimizely::CmabRetryConfig.new(max_retries: 3, initial_backoff: 0.01, max_backoff: 1, backoff_multiplier: 2) } + let(:rule_id) { 'test_rule' } + let(:user_id) { 'user123' } + let(:attributes) { {'attr1': 'value1', 'attr2': 'value2'} } + let(:cmab_uuid) { 'uuid-1234' } + let(:expected_url) { "https://prediction.cmab.optimizely.com/predict/#{rule_id}" } + let(:expected_body_for_webmock) do + { + instances: [{ + visitorId: user_id, + experimentId: rule_id, + attributes: [ + {'id' => 'attr1', 'value' => 'value1', 'type' => 'custom_attribute'}, + {'id' => 'attr2', 'value' => 'value2', 'type' => 'custom_attribute'} + ], + cmabUUID: cmab_uuid + }] + }.to_json + end + let(:expected_headers) { {'Content-Type' => 'application/json'} } + + before do + allow(Kernel).to receive(:sleep) + WebMock.disable_net_connect! + end + + after do + RSpec::Mocks.space.proxy_for(spy_logger).reset + WebMock.reset! + WebMock.allow_net_connect! + end + + context 'when client is configured without retries' do + let(:client) { described_class.new(nil, Optimizely::CmabRetryConfig.new(max_retries: 0), spy_logger) } + + it 'should return the variation id on success' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return(status: 200, body: {'predictions' => [{'variation_id' => 'abc123'}]}.to_json, headers: {'Content-Type' => 'application/json'}) + + result = client.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + + expect(result).to eq('abc123') + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).once + expect(Kernel).not_to have_received(:sleep) + end + + it 'should raise error on http client exception' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_raise(StandardError.new('Connection error')) + + expect do + client.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + end.to raise_error(Optimizely::CmabFetchError, /Connection error/) + + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).once + expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Connection error')) + expect(Kernel).not_to have_received(:sleep) + end + + it 'should raise error on non success status' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return(status: 500) + + expect do + client.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + end.to raise_error(Optimizely::CmabFetchError, /500/) + + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).once + expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('500')) + expect(Kernel).not_to have_received(:sleep) + end + + it 'should raise error on invalid json response' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return(status: 200, body: 'this is not json', headers: {'Content-Type' => 'text/plain'}) + + expect do + client.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + end.to raise_error(Optimizely::CmabInvalidResponseError, /Invalid CMAB fetch response/) + + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).once + expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Invalid CMAB fetch response')) + expect(Kernel).not_to have_received(:sleep) + end + + it 'should raise error on invalid response structure' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return(status: 200, body: {'no_predictions' => []}.to_json, headers: {'Content-Type' => 'application/json'}) + + expect do + client.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + end.to raise_error(Optimizely::CmabInvalidResponseError, /Invalid CMAB fetch response/) + + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).once + expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Invalid CMAB fetch response')) + expect(Kernel).not_to have_received(:sleep) + end + end + + context 'when client is configured with retries' do + let(:client_with_retry) { described_class.new(nil, retry_config, spy_logger) } + + it 'should return the variation id on first try' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return(status: 200, body: {'predictions' => [{'variation_id' => 'abc123'}]}.to_json, headers: {'Content-Type' => 'application/json'}) + + result = client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + + expect(result).to eq('abc123') + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).once + expect(Kernel).not_to have_received(:sleep) + end + + it 'should return the variation id on third try' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return({status: 500}, + {status: 500}, + {status: 200, body: {'predictions' => [{'variation_id' => 'xyz456'}]}.to_json, headers: {'Content-Type' => 'application/json'}}) + + result = client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + + expect(result).to eq('xyz456') + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).times(3) + + expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...').once + expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...').once + expect(spy_logger).not_to have_received(:log).with(Logger::INFO, a_string_including('Retrying CMAB request (attempt 3)')) + + expect(Kernel).to have_received(:sleep).with(0.01).once + expect(Kernel).to have_received(:sleep).with(0.02).once + expect(Kernel).not_to have_received(:sleep).with(0.04) + end + + it 'should exhaust all retry attempts' do + WebMock.stub_request(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers) + .to_return({status: 500}, + {status: 500}, + {status: 500}, + {status: 500}) + + expect do + client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid) + end.to raise_error(Optimizely::CmabFetchError) + + expect(WebMock).to have_requested(:post, expected_url) + .with(body: expected_body_for_webmock, headers: expected_headers).times(4) + + expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...').once + expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...').once + expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 3) after 0.04 seconds...').once + + expect(Kernel).to have_received(:sleep).with(0.01).once + expect(Kernel).to have_received(:sleep).with(0.02).once + expect(Kernel).to have_received(:sleep).with(0.04).once + + expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Max retries exceeded for CMAB request')) + end + end +end diff --git a/spec/cmab/cmab_service_spec.rb b/spec/cmab/cmab_service_spec.rb new file mode 100644 index 00000000..de94d39b --- /dev/null +++ b/spec/cmab/cmab_service_spec.rb @@ -0,0 +1,233 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'optimizely/cmab/cmab_service' +require 'optimizely/odp/lru_cache' +require 'optimizely/cmab/cmab_client' +require 'optimizely/decide/optimizely_decide_option' + +describe Optimizely::DefaultCmabService do + let(:mock_cmab_cache) { instance_double(Optimizely::LRUCache) } + let(:mock_cmab_client) { instance_double(Optimizely::DefaultCmabClient) } + let(:mock_logger) { double('logger') } + let(:cmab_service) { described_class.new(mock_cmab_cache, mock_cmab_client, mock_logger) } + + let(:mock_project_config) { double('project_config') } + let(:mock_user_context) { double('user_context') } + let(:user_id) { 'user123' } + let(:rule_id) { 'exp1' } + let(:user_attributes) { {'age' => 25, 'location' => 'USA'} } + + let(:mock_experiment) { {'cmab' => {'attributeIds' => %w[66 77]}} } + let(:mock_attr1) { {'key' => 'age'} } + let(:mock_attr2) { {'key' => 'location'} } + + before do + allow(mock_user_context).to receive(:user_id).and_return(user_id) + allow(mock_user_context).to receive(:user_attributes).and_return(user_attributes) + + allow(mock_project_config).to receive(:experiment_id_map).and_return({rule_id => mock_experiment}) + allow(mock_project_config).to receive(:attribute_id_map).and_return({ + '66' => mock_attr1, + '77' => mock_attr2 + }) + end + + describe '#get_decision' do + it 'returns decision from cache when valid' do + expected_key = cmab_service.send(:get_cache_key, user_id, rule_id) + expected_attributes = {'age' => 25, 'location' => 'USA'} + expected_hash = cmab_service.send(:hash_attributes, expected_attributes) + + cached_value = Optimizely::CmabCacheValue.new( + attributes_hash: expected_hash, + variation_id: 'varA', + cmab_uuid: 'uuid-123' + ) + + allow(mock_cmab_cache).to receive(:lookup).with(expected_key).and_return(cached_value) + + decision = cmab_service.get_decision(mock_project_config, mock_user_context, rule_id, []) + + expect(mock_cmab_cache).to have_received(:lookup).with(expected_key) + expect(decision.variation_id).to eq('varA') + expect(decision.cmab_uuid).to eq('uuid-123') + end + + it 'ignores cache when option given' do + allow(mock_cmab_client).to receive(:fetch_decision).and_return('varB') + expected_attributes = {'age' => 25, 'location' => 'USA'} + + decision = cmab_service.get_decision( + mock_project_config, + mock_user_context, + rule_id, + [Optimizely::Decide::OptimizelyDecideOption::IGNORE_CMAB_CACHE] + ) + + expect(decision.variation_id).to eq('varB') + expect(decision.cmab_uuid).to be_a(String) + expect(mock_cmab_client).to have_received(:fetch_decision).with( + rule_id, + user_id, + expected_attributes, + decision.cmab_uuid + ) + end + + it 'invalidates user cache when option given' do + allow(mock_cmab_client).to receive(:fetch_decision).and_return('varC') + allow(mock_cmab_cache).to receive(:lookup).and_return(nil) + allow(mock_cmab_cache).to receive(:remove) + allow(mock_cmab_cache).to receive(:save) + + cmab_service.get_decision( + mock_project_config, + mock_user_context, + rule_id, + [Optimizely::Decide::OptimizelyDecideOption::INVALIDATE_USER_CMAB_CACHE] + ) + + key = cmab_service.send(:get_cache_key, user_id, rule_id) + expect(mock_cmab_cache).to have_received(:remove).with(key) + end + + it 'resets cache when option given' do + allow(mock_cmab_client).to receive(:fetch_decision).and_return('varD') + allow(mock_cmab_cache).to receive(:reset) + allow(mock_cmab_cache).to receive(:lookup).and_return(nil) + allow(mock_cmab_cache).to receive(:save) + + decision = cmab_service.get_decision( + mock_project_config, + mock_user_context, + rule_id, + [Optimizely::Decide::OptimizelyDecideOption::RESET_CMAB_CACHE] + ) + + expect(mock_cmab_cache).to have_received(:reset) + expect(decision.variation_id).to eq('varD') + expect(decision.cmab_uuid).to be_a(String) + end + + it 'fetches new decision when hash changes' do + old_cached_value = Optimizely::CmabCacheValue.new( + attributes_hash: 'old_hash', + variation_id: 'varA', + cmab_uuid: 'uuid-123' + ) + + allow(mock_cmab_cache).to receive(:lookup).and_return(old_cached_value) + allow(mock_cmab_cache).to receive(:remove) + allow(mock_cmab_cache).to receive(:save) + allow(mock_cmab_client).to receive(:fetch_decision).and_return('varE') + + expected_attributes = {'age' => 25, 'location' => 'USA'} + cmab_service.send(:hash_attributes, expected_attributes) + expected_key = cmab_service.send(:get_cache_key, user_id, rule_id) + + decision = cmab_service.get_decision(mock_project_config, mock_user_context, rule_id, []) + + expect(mock_cmab_cache).to have_received(:remove).with(expected_key) + expect(mock_cmab_cache).to have_received(:save).with( + expected_key, + an_instance_of(Optimizely::CmabCacheValue) + ) + expect(decision.variation_id).to eq('varE') + expect(mock_cmab_client).to have_received(:fetch_decision).with( + rule_id, + user_id, + expected_attributes, + decision.cmab_uuid + ) + end + + it 'only passes cmab attributes to client' do + allow(mock_user_context).to receive(:user_attributes).and_return({ + 'age' => 25, + 'location' => 'USA', + 'extra_attr' => 'value', + 'another_extra' => 123 + }) + allow(mock_cmab_client).to receive(:fetch_decision).and_return('varF') + + decision = cmab_service.get_decision( + mock_project_config, + mock_user_context, + rule_id, + [Optimizely::Decide::OptimizelyDecideOption::IGNORE_CMAB_CACHE] + ) + + # Verify only age and location are passed + expect(mock_cmab_client).to have_received(:fetch_decision).with( + rule_id, + user_id, + {'age' => 25, 'location' => 'USA'}, + decision.cmab_uuid + ) + end + end + + describe '#filter_attributes' do + it 'returns correct subset of attributes' do + filtered = cmab_service.send(:filter_attributes, mock_project_config, mock_user_context, rule_id) + + expect(filtered['age']).to eq(25) + expect(filtered['location']).to eq('USA') + end + + it 'returns empty hash when no cmab config' do + allow(mock_project_config).to receive(:experiment_id_map).and_return({rule_id => {'cmab' => nil}}) + + filtered = cmab_service.send(:filter_attributes, mock_project_config, mock_user_context, rule_id) + + expect(filtered).to eq({}) + end + + it 'returns empty hash when experiment not found' do + allow(mock_project_config).to receive(:experiment_id_map).and_return({}) + + filtered = cmab_service.send(:filter_attributes, mock_project_config, mock_user_context, rule_id) + + expect(filtered).to eq({}) + end + end + + describe '#hash_attributes' do + it 'produces stable output regardless of key order' do + attrs1 = {'b' => 2, 'a' => 1} + attrs2 = {'a' => 1, 'b' => 2} + + hash1 = cmab_service.send(:hash_attributes, attrs1) + hash2 = cmab_service.send(:hash_attributes, attrs2) + + expect(hash1).to eq(hash2) + end + end + + describe '#get_cache_key' do + it 'generates correct cache key format' do + key = cmab_service.send(:get_cache_key, 'user123', 'exp1') + + expect(key).to eq('7-user123-exp1') + end + end + + describe '#fetch_decision' do + it 'generates uuid and calls client' do + allow(mock_cmab_client).to receive(:fetch_decision).and_return('varX') + attributes = {'age' => 25} + + decision = cmab_service.send(:fetch_decision, rule_id, user_id, attributes) + + expect(decision.variation_id).to eq('varX') + expect(decision.cmab_uuid).to match(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/) + expect(mock_cmab_client).to have_received(:fetch_decision).with( + rule_id, + user_id, + attributes, + decision.cmab_uuid + ) + end + end +end diff --git a/spec/config/datafile_project_config_spec.rb b/spec/config/datafile_project_config_spec.rb index e30d07e1..74c264b3 100644 --- a/spec/config/datafile_project_config_spec.rb +++ b/spec/config/datafile_project_config_spec.rb @@ -57,6 +57,7 @@ expect(project_config.sdk_key).to eq(config_body['sdkKey']) expect(project_config.environment_key).to eq(config_body['environmentKey']) expect(project_config.send_flag_decisions).to eq(config_body['sendFlagDecisions']) + expect(project_config.region).to eq(config_body['region']) expected_attribute_key_map = { 'browser_type' => config_body['attributes'][0], @@ -756,6 +757,23 @@ expect(project_config.rollout_experiment_id_map).to eq(expected_rollout_experiment_id_map) end + it 'should use US region when no region is specified in datafile' do + project_config = Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) + expect(project_config.region).to eq('US') + end + + it 'should parse region specified in datafile correctly' do + project_config_us = Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) + expect(project_config_us.region).to eq('US') + + config_body_eu = config_body.dup + config_body_eu['region'] = 'EU' + config_body_json = JSON.dump(config_body_eu) + project_config_eu = Optimizely::DatafileProjectConfig.new(config_body_json, logger, error_handler) + + expect(project_config_eu.region).to eq('EU') + end + it 'should initialize properties correctly upon creating project with typed audience dict' do project_config = Optimizely::DatafileProjectConfig.new(JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), logger, error_handler) config_body = OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES @@ -1078,6 +1096,67 @@ end end + describe '#test_cmab_field_population' do + it 'Should return CMAB details' do + config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY)) + config_dict['experiments'][0]['cmab'] = {'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000} + config_dict['experiments'][0]['trafficAllocation'] = [] + + config_json = JSON.dump(config_dict) + project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler) + + experiment = project_config.get_experiment_from_key('test_experiment') + expect(experiment['cmab']).to eq({'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000}) + + experiment2 = project_config.get_experiment_from_key('test_experiment_with_audience') + expect(experiment2['cmab']).to eq(nil) + end + it 'should return nil if cmab field is missing' do + config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY)) + config_dict['experiments'][0].delete('cmab') + config_json = JSON.dump(config_dict) + project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler) + experiment = project_config.get_experiment_from_key('test_experiment') + expect(experiment['cmab']).to eq(nil) + end + + it 'should handle empty cmab object' do + config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY)) + config_dict['experiments'][0]['cmab'] = {} + config_json = JSON.dump(config_dict) + project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler) + experiment = project_config.get_experiment_from_key('test_experiment') + expect(experiment['cmab']).to eq({}) + end + + it 'should handle cmab with only attributeIds' do + config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY)) + config_dict['experiments'][0]['cmab'] = {'attributeIds' => %w[808797688]} + config_json = JSON.dump(config_dict) + project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler) + experiment = project_config.get_experiment_from_key('test_experiment') + expect(experiment['cmab']).to eq({'attributeIds' => %w[808797688]}) + end + + it 'should handle cmab with only trafficAllocation' do + config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY)) + config_dict['experiments'][0]['cmab'] = {'trafficAllocation' => 1234} + config_json = JSON.dump(config_dict) + project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler) + experiment = project_config.get_experiment_from_key('test_experiment') + expect(experiment['cmab']).to eq({'trafficAllocation' => 1234}) + end + + it 'should not affect other experiments when cmab is set' do + config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY)) + config_dict['experiments'][0]['cmab'] = {'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000} + config_json = JSON.dump(config_dict) + project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler) + experiment2 = project_config.get_experiment_from_key('test_experiment_with_audience') + expect(experiment2['cmab']).to eq(nil) + end + end + describe '#feature_experiment' do let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) } diff --git a/spec/decision_service_spec.rb b/spec/decision_service_spec.rb index af22b18b..fe2cc881 100644 --- a/spec/decision_service_spec.rb +++ b/spec/decision_service_spec.rb @@ -26,8 +26,9 @@ let(:error_handler) { Optimizely::NoOpErrorHandler.new } let(:spy_logger) { spy('logger') } let(:spy_user_profile_service) { spy('user_profile_service') } + let(:spy_cmab_service) { spy('cmab_service') } let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, spy_logger, error_handler) } - let(:decision_service) { Optimizely::DecisionService.new(spy_logger, spy_user_profile_service) } + let(:decision_service) { Optimizely::DecisionService.new(spy_logger, spy_cmab_service, spy_user_profile_service) } let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) } let(:user_context) { project_instance.create_user_context('some-user', {}) } after(:example) { project_instance.close } @@ -46,9 +47,9 @@ it 'should return the correct variation ID for a given user for whom a variation has been forced' do decision_service.set_forced_variation(config, 'test_experiment', 'test_user', 'variation') 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('111129') - expect(reasons).to eq(["Variation 'variation' is mapped to experiment '111127' and user 'test_user' in the forced variation map"]) + variation_result = decision_service.get_variation(config, '111127', user_context) + expect(variation_result.variation_id).to eq('111129') + expect(variation_result.reasons).to eq(["Variation 'variation' is mapped to experiment '111127' and user 'test_user' in the forced variation map"]) # Setting forced variation should short circuit whitelist check, bucketing and audience evaluation expect(decision_service).not_to have_received(:get_whitelisted_variation_id) expect(decision_service.bucketer).not_to have_received(:bucket) @@ -62,9 +63,9 @@ } decision_service.set_forced_variation(config, 'test_experiment_with_audience', 'test_user', 'control_with_audience') user_context = project_instance.create_user_context('test_user', user_attributes) - variation_received, reasons = decision_service.get_variation(config, '122227', user_context) - expect(variation_received).to eq('122228') - expect(reasons).to eq(["Variation 'control_with_audience' is mapped to experiment '122227' and user 'test_user' in the forced variation map"]) + variation_result = decision_service.get_variation(config, '122227', user_context) + expect(variation_result.variation_id).to eq('122228') + expect(variation_result.reasons).to eq(["Variation 'control_with_audience' is mapped to experiment '122227' and user 'test_user' in the forced variation map"]) # Setting forced variation should short circuit whitelist check, bucketing and audience evaluation expect(decision_service).not_to have_received(:get_whitelisted_variation_id) expect(decision_service.bucketer).not_to have_received(:bucket) @@ -74,13 +75,13 @@ 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') 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') + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).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(variation_result.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::INFO, "User 'test_user' is in variation 'control' of experiment '111127'.") @@ -92,12 +93,12 @@ allow(decision_service.bucketer).to receive(:bucket).and_return(nil) user_context = project_instance.create_user_context('test_user') 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.", - "User 'test_user' is in no variation." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq(nil) + expect(variation_result.reasons).to eq([ + "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", + "User 'test_user' is in no variation." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'test_user' is in no variation.") @@ -105,20 +106,20 @@ it 'should return correct variation ID if user ID is in whitelisted Variations and variation is valid' do user_context = project_instance.create_user_context('forced_user1') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) - expect(variation_received).to eq('111128') - expect(reasons).to eq([ - "User 'forced_user1' is whitelisted into variation 'control' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.reasons).to eq([ + "User 'forced_user1' is whitelisted into variation 'control' of experiment '111127'." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'forced_user1' is whitelisted into variation 'control' of experiment '111127'.") user_context = project_instance.create_user_context('forced_user2') - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) - expect(variation_received).to eq('111129') - expect(reasons).to eq([ - "User 'forced_user2' is whitelisted into variation 'variation' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context) + expect(variation_result.variation_id).to eq('111129') + expect(variation_result.reasons).to eq([ + "User 'forced_user2' is whitelisted into variation 'variation' of experiment '111127'." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'forced_user2' is whitelisted into variation 'variation' of experiment '111127'.") @@ -135,20 +136,20 @@ } user_context = project_instance.create_user_context('forced_user1', user_attributes) - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) - expect(variation_received).to eq('111128') - expect(reasons).to eq([ - "User 'forced_user1' is whitelisted into variation 'control' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.reasons).to eq([ + "User 'forced_user1' is whitelisted into variation 'control' of experiment '111127'." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'forced_user1' is whitelisted into variation 'control' of experiment '111127'.") user_context = project_instance.create_user_context('forced_user2', user_attributes) - variation_received, reasons = decision_service.get_variation(config, '111127', user_context) - expect(variation_received).to eq('111129') - expect(reasons).to eq([ - "User 'forced_user2' is whitelisted into variation 'variation' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context) + expect(variation_result.variation_id).to eq('111129') + expect(variation_result.reasons).to eq([ + "User 'forced_user2' is whitelisted into variation 'variation' of experiment '111127'." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'forced_user2' is whitelisted into variation 'variation' of experiment '111127'.") @@ -161,11 +162,11 @@ it 'should return the correct variation ID for a user in a whitelisted variation (even when audience conditions do not match)' do user_attributes = {'browser_type' => 'wrong_browser'} user_context = project_instance.create_user_context('forced_audience_user', user_attributes) - variation_received, reasons = decision_service.get_variation(config, '122227', user_context) - expect(variation_received).to eq('122229') - expect(reasons).to eq([ - "User 'forced_audience_user' is whitelisted into variation 'variation_with_audience' of experiment '122227'." - ]) + variation_result = decision_service.get_variation(config, '122227', user_context) + expect(variation_result.variation_id).to eq('122229') + expect(variation_result.reasons).to eq([ + "User 'forced_audience_user' is whitelisted into variation 'variation_with_audience' of experiment '122227'." + ]) expect(spy_logger).to have_received(:log) .once.with( Logger::INFO, @@ -180,9 +181,9 @@ it 'should return nil if the experiment key is invalid' do user_context = project_instance.create_user_context('test_user', {}) - variation_received, reasons = decision_service.get_variation(config, 'totally_invalid_experiment', user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq([]) + variation_result = decision_service.get_variation(config, 'totally_invalid_experiment', user_context) + expect(variation_result.variation_id).to eq(nil) + expect(variation_result.reasons).to eq([]) expect(spy_logger).to have_received(:log) .once.with(Logger::ERROR, "Experiment id 'totally_invalid_experiment' is not in datafile.") @@ -192,14 +193,14 @@ user_attributes = {'browser_type' => 'chrome'} user_context = project_instance.create_user_context('test_user', user_attributes) 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\"}]]].", - "Audience '11154' evaluated to FALSE.", - "Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE.", - "User 'test_user' does not meet the conditions to be in experiment 'test_experiment_with_audience'." - ]) + variation_result = decision_service.get_variation(config, '122227', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq(nil) + expect(variation_result.reasons).to eq([ + "Starting to evaluate audience '11154' with conditions: [\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"firefox\"}]]].", + "Audience '11154' evaluated to FALSE.", + "Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE.", + "User 'test_user' does not meet the conditions to be in experiment 'test_experiment_with_audience'." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'test_user' does not meet the conditions to be in experiment 'test_experiment_with_audience'.") @@ -211,9 +212,9 @@ it 'should return nil if the given experiment is not running' do user_context = project_instance.create_user_context('test_user') - variation_received, reasons = decision_service.get_variation(config, '100027', user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq(["Experiment 'test_experiment_not_started' is not running."]) + variation_result = decision_service.get_variation(config, '100027', user_context) + expect(variation_result.variation_id).to eq(nil) + expect(variation_result.reasons).to eq(["Experiment 'test_experiment_not_started' is not running."]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "Experiment 'test_experiment_not_started' is not running.") @@ -227,11 +228,11 @@ it 'should respect forced variations within mutually exclusive grouped experiments' do user_context = project_instance.create_user_context('forced_group_user1') - variation_received, reasons = decision_service.get_variation(config, '133332', user_context) - expect(variation_received).to eq('130004') - expect(reasons).to eq([ - "User 'forced_group_user1' is whitelisted into variation 'g1_e2_v2' of experiment '133332'." - ]) + variation_result = decision_service.get_variation(config, '133332', user_context) + expect(variation_result.variation_id).to eq('130004') + expect(variation_result.reasons).to eq([ + "User 'forced_group_user1' is whitelisted into variation 'g1_e2_v2' of experiment '133332'." + ]) expect(spy_logger).to have_received(:log) .once.with(Logger::INFO, "User 'forced_group_user1' is whitelisted into variation 'g1_e2_v2' of experiment '133332'.") @@ -244,13 +245,13 @@ 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') 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.", - "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", - "User 'forced_user_with_invalid_variation' is in variation 'control' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.reasons).to eq([ + "User 'forced_user_with_invalid_variation' is whitelisted into variation 'invalid_variation', which is not in the datafile.", + "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", + "User 'forced_user_with_invalid_variation' is in variation 'control' of experiment '111127'." + ]) expect(spy_logger).to have_received(:log) .once.with( Logger::INFO, @@ -270,12 +271,12 @@ } user_context = project_instance.create_user_context('test_user', user_attributes) 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.", - "User 'test_user' is in variation 'variation' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq('111129') + expect(variation_result.reasons).to eq([ + "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", + "User 'test_user' is in variation 'variation' of experiment '111127'." + ]) # bucketing should have occurred expect(decision_service.bucketer).to have_received(:bucket).once @@ -296,11 +297,11 @@ user_context = project_instance.create_user_context('test_user') 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." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq('111129') + expect(variation_result.reasons).to eq([ + "Returning previously activated variation ID 111129 of experiment 'test_experiment' for user 'test_user' from user profile." + ]) expect(spy_logger).to have_received(:log).once .with(Logger::INFO, "Returning previously activated variation ID 111129 of experiment 'test_experiment' for user 'test_user' from user profile.") @@ -328,12 +329,12 @@ user_context = project_instance.create_user_context('test_user') 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.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.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 @@ -355,13 +356,13 @@ user_context = project_instance.create_user_context('test_user') 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.", - "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.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.", + "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 @@ -373,13 +374,13 @@ user_context = project_instance.create_user_context('test_user') 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) + variation_result = 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([ - "Audiences for experiment 'test_experiment' collectively evaluated to TRUE.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.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 looking up user profile for user ID 'test_user': uncaught throw :LookupError.") @@ -395,12 +396,12 @@ user_context = project_instance.create_user_context('test_user', nil) 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.", - "User 'test_user' is in variation 'control' of experiment '111127'." - ]) + variation_result = decision_service.get_variation(config, '111127', user_context, user_profile_tracker, [Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE]) + expect(variation_result.variation_id).to eq('111128') + expect(variation_result.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?) @@ -417,9 +418,9 @@ 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, user_profile_tracker) - expect(variation_received).to eq(nil) - expect(reasons).to eq(["The feature flag 'empty_feature' is not used in any experiments."]) + decision_result = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq(["The feature flag 'empty_feature' is not used in any experiments."]) expect(spy_logger).to have_received(:log).once .with(Logger::DEBUG, "The feature flag 'empty_feature' is not used in any experiments.") @@ -432,9 +433,9 @@ # any string that is not an experiment id in the data file feature_flag['experimentIds'] = ['1333333337'] 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."]) + decision_result = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq(["Feature flag experiment with ID '1333333337' is not in the datafile."]) expect(spy_logger).to have_received(:log).once .with(Logger::DEBUG, "Feature flag experiment with ID '1333333337' is not in the datafile.") end @@ -448,14 +449,14 @@ # 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, user_profile_tracker, []) - .and_return([nil, nil]) + .and_return(Optimizely::DecisionService::VariationResult.new(nil, false, [], 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, 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'."]) + decision_result = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker, []) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq(["The user 'user_1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'."]) expect(spy_logger).to have_received(:log).once .with(Logger::INFO, "The user 'user_1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'.") @@ -465,7 +466,7 @@ describe 'and the user is bucketed into a variation for the experiment on the feature flag' do before(:each) do # mock and return the first variation of the `test_experiment_multivariate` experiment, which is attached to the `multi_variate_feature` - allow(decision_service).to receive(:get_variation).and_return('122231') + allow(decision_service).to receive(:get_variation).and_return(Optimizely::DecisionService::VariationResult.new(nil, false, [], '122231')) end it 'should return the variation' do @@ -475,10 +476,15 @@ config.variation_id_map['test_experiment_multivariate']['122231'], Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + expected_decision_result = Optimizely::DecisionService::DecisionResult.new( + expected_decision, + false, + [] + ) 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([]) + decision_result = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) + expect(decision_result).to eq(expected_decision_result) + expect(decision_result.reasons).to eq([]) end end end @@ -496,15 +502,15 @@ Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) allow(decision_service).to receive(:get_variation) - .and_return(variation['id']) + .and_return(Optimizely::DecisionService::VariationResult.new(nil, false, [], variation['id'])) end it 'should return the variation the user is bucketed into' do feature_flag = config.feature_flag_key_map['mutex_group_feature'] 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([]) + decision_result = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) + expect(decision_result.decision).to eq(expected_decision) + expect(decision_result.reasons).to eq([]) end end @@ -515,17 +521,17 @@ mutex_exp2 = config.experiment_key_map['group1_exp2'] allow(decision_service).to receive(:get_variation) .with(config, mutex_exp['id'], user_context, user_profile_tracker, []) - .and_return([nil, nil]) + .and_return(Optimizely::DecisionService::VariationResult.new(nil, false, [], nil)) allow(decision_service).to receive(:get_variation) .with(config, mutex_exp2['id'], user_context, user_profile_tracker, []) - .and_return([nil, nil]) + .and_return(Optimizely::DecisionService::VariationResult.new(nil, false, [], 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, 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'."]) + decision_result = decision_service.get_variation_for_feature_experiment(config, feature_flag, user_context, user_profile_tracker) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq(["The user 'user_1' is not bucketed into any of the experiments on the feature 'mutex_group_feature'."]) expect(spy_logger).to have_received(:log).once .with(Logger::INFO, "The user 'user_1' is not bucketed into any of the experiments on the feature 'mutex_group_feature'.") @@ -543,9 +549,9 @@ describe 'when the feature flag is not associated with a rollout' do it 'should log a message and return nil' do feature_flag = config.feature_flag_key_map['boolean_feature'] - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq(["Feature flag '#{feature_flag['key']}' is not used in a rollout."]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq(["Feature flag '#{feature_flag['key']}' is not used in a rollout."]) expect(spy_logger).to have_received(:log).once .with(Logger::DEBUG, "Feature flag '#{feature_flag['key']}' is not used in a rollout.") end @@ -555,9 +561,9 @@ it 'should log a message and return nil' do feature_flag = config.feature_flag_key_map['boolean_feature'].dup feature_flag['rolloutId'] = 'invalid_rollout_id' - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq(["Rollout with ID 'invalid_rollout_id' is not in the datafile 'boolean_feature'"]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq(["Rollout with ID 'invalid_rollout_id' is not in the datafile 'boolean_feature'"]) expect(spy_logger).to have_received(:log).once .with(Logger::ERROR, "Rollout with ID 'invalid_rollout_id' is not in the datafile.") @@ -570,9 +576,9 @@ experimentless_rollout['experiments'] = [] allow(config).to receive(:get_rollout_from_id).and_return(experimentless_rollout) feature_flag = config.feature_flag_key_map['boolean_single_variable_feature'] - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq([]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq([]) end end @@ -587,10 +593,10 @@ allow(decision_service.bucketer).to receive(:bucket) .with(config, rollout_experiment, user_id, user_id) .and_return(variation) - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(expected_decision) - expect(reasons).to eq(["User 'user_1' meets the audience conditions for targeting rule '1'.", - "User 'user_1' is in the traffic group of targeting rule '1'."]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(expected_decision) + expect(decision_result.reasons).to eq(["User 'user_1' meets the audience conditions for targeting rule '1'.", + "User 'user_1' is in the traffic group of targeting rule '1'."]) end end @@ -609,13 +615,13 @@ .with(config, everyone_else_experiment, user_id, user_id) .and_return(nil) - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq([ - "User 'user_1' meets the audience conditions for targeting rule '1'.", - "User 'user_1' is not in the traffic group for targeting rule '1'.", - "User 'user_1' meets the audience conditions for targeting rule 'Everyone Else'." - ]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq([ + "User 'user_1' meets the audience conditions for targeting rule '1'.", + "User 'user_1' is not in the traffic group for targeting rule '1'.", + "User 'user_1' meets the audience conditions for targeting rule 'Everyone Else'." + ]) # make sure we only checked the audience for the first rule expect(Optimizely::Audience).to have_received(:user_meets_audience_conditions?).once @@ -640,14 +646,14 @@ .with(config, everyone_else_experiment, user_id, user_id) .and_return(variation) - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(expected_decision) - expect(reasons).to eq([ - "User 'user_1' meets the audience conditions for targeting rule '1'.", - "User 'user_1' is not in the traffic group for targeting rule '1'.", - "User 'user_1' meets the audience conditions for targeting rule 'Everyone Else'.", - "User 'user_1' is in the traffic group of targeting rule 'Everyone Else'." - ]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(expected_decision) + expect(decision_result.reasons).to eq([ + "User 'user_1' meets the audience conditions for targeting rule '1'.", + "User 'user_1' is not in the traffic group for targeting rule '1'.", + "User 'user_1' meets the audience conditions for targeting rule 'Everyone Else'.", + "User 'user_1' is in the traffic group of targeting rule 'Everyone Else'." + ]) # make sure we only checked the audience for the first rule expect(Optimizely::Audience).to have_received(:user_meets_audience_conditions?).once @@ -675,14 +681,14 @@ .with(config, everyone_else_experiment, user_id, user_id) .and_return(variation) - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(expected_decision) - expect(reasons).to eq([ - "User 'user_1' does not meet the conditions for targeting rule '1'.", - "User 'user_1' does not meet the conditions for targeting rule '2'.", - "User 'user_1' meets the audience conditions for targeting rule 'Everyone Else'.", - "User 'user_1' is in the traffic group of targeting rule 'Everyone Else'." - ]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(expected_decision) + expect(decision_result.reasons).to eq([ + "User 'user_1' does not meet the conditions for targeting rule '1'.", + "User 'user_1' does not meet the conditions for targeting rule '2'.", + "User 'user_1' meets the audience conditions for targeting rule 'Everyone Else'.", + "User 'user_1' is in the traffic group of targeting rule 'Everyone Else'." + ]) # verify we tried to bucket in all targeting rules and the everyone else rule expect(Optimizely::Audience).to have_received(:user_meets_audience_conditions?).exactly(3).times @@ -705,13 +711,13 @@ expect(decision_service.bucketer).not_to receive(:bucket) .with(config, everyone_else_experiment, user_id, user_id) - variation_received, reasons = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) - expect(variation_received).to eq(nil) - expect(reasons).to eq([ - "User 'user_1' does not meet the conditions for targeting rule '1'.", - "User 'user_1' does not meet the conditions for targeting rule '2'.", - "User 'user_1' does not meet the conditions for targeting rule 'Everyone Else'." - ]) + decision_result = decision_service.get_variation_for_feature_rollout(config, feature_flag, user_context) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq([ + "User 'user_1' does not meet the conditions for targeting rule '1'.", + "User 'user_1' does not meet the conditions for targeting rule '2'.", + "User 'user_1' does not meet the conditions for targeting rule 'Everyone Else'." + ]) # verify we tried to bucket in all targeting rules and the everyone else rule expect(Optimizely::Audience).to have_received(:user_meets_audience_conditions?).once @@ -745,11 +751,11 @@ 'experiment' => expected_experiment, 'variation' => expected_variation } - allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return([expected_decision, nil]) + allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return(Optimizely::DecisionService::DecisionResult.new(expected_decision, false, [])) - decision_received, reasons = decision_service.get_variation_for_feature(config, feature_flag, user_context) - expect(decision_received).to eq(expected_decision) - expect(reasons).to eq([]) + decision_result = decision_service.get_variation_for_feature(config, feature_flag, user_context) + expect(decision_result.decision).to eq(expected_decision) + expect(decision_result.reasons).to eq([]) end end @@ -764,24 +770,24 @@ variation, Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) - allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return([nil, nil]) - allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return([expected_decision, nil]) + allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) + allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return(Optimizely::DecisionService::DecisionResult.new(expected_decision, false, [])) - decision_received, reasons = decision_service.get_variation_for_feature(config, feature_flag, user_context) - expect(decision_received).to eq(expected_decision) - expect(reasons).to eq([]) + decision_result = decision_service.get_variation_for_feature(config, feature_flag, user_context) + expect(decision_result.decision).to eq(expected_decision) + expect(decision_result.reasons).to eq([]) end end describe 'and the user is not bucketed into the feature rollout' do it 'should log a message and return nil' do feature_flag = config.feature_flag_key_map['string_single_variable_feature'] - allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return([nil, nil]) - allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return([nil, nil]) + allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) + allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) - decision_received, reasons = decision_service.get_variation_for_feature(config, feature_flag, user_context) - expect(decision_received).to eq(nil) - expect(reasons).to eq([]) + decision_result = decision_service.get_variation_for_feature(config, feature_flag, user_context) + expect(decision_result.decision).to eq(nil) + expect(decision_result.reasons).to eq([]) end end end @@ -931,4 +937,234 @@ expect(reasons).to eq(["Variation 'control' is mapped to experiment '111127' and user 'test_user_2' in the forced variation map"]) end end + describe 'CMAB experiments' do + describe 'when user is in traffic allocation' do + it 'should return correct variation and CMAB UUID from CMAB service' 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', {}) + + # 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'}) + + variation_result = decision_service.get_variation(config, '111150', user_context) + + 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) + expect(variation_result.reasons).to include( + "User 'test_user' is in variation 'variation_1' of experiment '111150'." + ) + + # Verify CMAB service was called + expect(spy_cmab_service).to have_received(:get_decision).once + end + end + + describe 'when user is not in traffic allocation' do + it 'should return nil variation and log traffic allocation message' do + cmab_experiment = { + 'id' => '111150', + 'key' => 'cmab_experiment', + 'status' => 'Running', + 'layerId' => '111150', + 'audienceIds' => [], + 'forcedVariations' => {}, + 'variations' => [ + {'id' => '111151', 'key' => 'variation_1'} + ], + 'trafficAllocation' => [ + {'entityId' => '111151', 'endOfRange' => 10_000} + ], + 'cmab' => {'trafficAllocation' => 1000} + } + user_context = project_instance.create_user_context('test_user', {}) + + # 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, []]) + + variation_result = decision_service.get_variation(config, '111150', user_context) + + expect(variation_result.variation_id).to eq(nil) + expect(variation_result.cmab_uuid).to eq(nil) + expect(variation_result.error).to eq(false) + expect(variation_result.reasons).to include( + 'User "test_user" not in CMAB experiment "cmab_experiment" due to traffic allocation.' + ) + + # Verify CMAB service was not called since user is not in traffic allocation + expect(spy_cmab_service).not_to have_received(:get_decision) + end + end + + describe 'when CMAB service returns an error' do + it 'should return nil variation and include error in reasons' do + cmab_experiment = { + 'id' => '111150', + 'key' => 'cmab_experiment', + 'status' => 'Running', + 'layerId' => '111150', + 'audienceIds' => [], + 'forcedVariations' => {}, + 'variations' => [ + {'id' => '111151', 'key' => 'variation_1'} + ], + 'trafficAllocation' => [ + {'entityId' => '111151', 'endOfRange' => 10_000} + ], + 'cmab' => {'trafficAllocation' => 5000} + } + user_context = project_instance.create_user_context('test_user', {}) + + # 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 an error + allow(spy_cmab_service).to receive(:get_decision) + .with(config, user_context, '111150', []) + .and_raise(StandardError.new('CMAB service error')) + + variation_result = decision_service.get_variation(config, '111150', user_context) + + expect(variation_result.variation_id).to be_nil + expect(variation_result.cmab_uuid).to be_nil + expect(variation_result.error).to eq(true) + expect(variation_result.reasons).to include( + 'Failed to fetch CMAB data for experiment cmab_experiment.' + ) + + # Verify CMAB service was called but errored + expect(spy_cmab_service).to have_received(:get_decision).once + end + end + + describe 'when user has forced variation' do + it 'should return forced variation and skip CMAB service call' do + # Use a real experiment from the datafile and modify it to be a CMAB experiment + real_experiment = config.get_experiment_from_key('test_experiment') + cmab_experiment = real_experiment.dup + cmab_experiment['cmab'] = {'trafficAllocation' => 5000} + + user_context = project_instance.create_user_context('test_user', {}) + + # Set up forced variation first (using real experiment that exists in datafile) + decision_service.set_forced_variation(config, 'test_experiment', 'test_user', 'variation') + + # Mock the experiment to be a CMAB experiment after setting forced variation + allow(config).to receive(:get_experiment_from_id).with('111127').and_return(cmab_experiment) + allow(config).to receive(:experiment_running?).with(cmab_experiment).and_return(true) + + # Add spy for bucket_to_entity_id method + allow(decision_service.bucketer).to receive(:bucket_to_entity_id).and_call_original + + variation_result = decision_service.get_variation(config, '111127', user_context) + + expect(variation_result.variation_id).to eq('111129') + expect(variation_result.cmab_uuid).to be_nil + expect(variation_result.error).to eq(false) + expect(variation_result.reasons).to include( + "Variation 'variation' is mapped to experiment '111127' and user 'test_user' in the forced variation map" + ) + + # Verify CMAB service was not called since user has forced variation + expect(spy_cmab_service).not_to have_received(:get_decision) + # Verify bucketer was not called since forced variations short-circuit bucketing + expect(decision_service.bucketer).not_to have_received(:bucket_to_entity_id) + end + end + + describe 'when user has whitelisted variation' do + it 'should return whitelisted variation and skip CMAB service call' do + # Create a CMAB experiment with whitelisted users + cmab_experiment = { + 'id' => '111150', + 'key' => 'cmab_experiment', + 'status' => 'Running', + 'layerId' => '111150', + 'audienceIds' => [], + 'forcedVariations' => { + 'whitelisted_user' => '111151' # User is whitelisted to variation_1 + }, + '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('whitelisted_user', {}) + + # 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 the get_whitelisted_variation_id method directly + allow(decision_service).to receive(:get_whitelisted_variation_id) + .with(config, '111150', 'whitelisted_user') + .and_return(['111151', "User 'whitelisted_user' is whitelisted into variation 'variation_1' of experiment '111150'."]) + + variation_result = decision_service.get_variation(config, '111150', user_context) + + expect(variation_result.variation_id).to eq('111151') + expect(variation_result.cmab_uuid).to be_nil + expect(variation_result.error).to eq(false) + expect(variation_result.reasons).to include( + "User 'whitelisted_user' is whitelisted into variation 'variation_1' of experiment '111150'." + ) + # Verify CMAB service was not called since user is whitelisted + expect(spy_cmab_service).not_to have_received(:get_decision) + end + end + end end diff --git a/spec/event/event_factory_spec.rb b/spec/event/event_factory_spec.rb index b92661be..f9771e4f 100644 --- a/spec/event/event_factory_spec.rb +++ b/spec/event/event_factory_spec.rb @@ -34,7 +34,10 @@ allow(Time).to receive(:now).and_return(time_now) allow(SecureRandom).to receive(:uuid).and_return('a68cf1ad-0393-4e18-af87-efe8f01a7c9c') - @expected_endpoint = 'https://logx.optimizely.com/v1/events' + @expected_endpoints = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + } @expected_impression_params = { account_id: '12001', project_id: '111001', @@ -111,7 +114,33 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', nil) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) + expect(log_event.http_verb).to eq(:post) + end + + it 'should create valid Event when create_impression_event is called without attributes and with EU' do + experiment = project_config.get_experiment_from_key('test_experiment') + metadata = { + flag_key: '', + rule_key: 'test_experiment', + rule_type: 'experiment', + variation_key: '111128' + } + allow_any_instance_of(Optimizely::ImpressionEvent).to receive(:event_context).and_return( + { + account_id: '12001', + project_id: '111001', + client_version: Optimizely::VERSION, + revision: '42', + client_name: Optimizely::CLIENT_ENGINE, + anonymize_ip: false, + region: 'EU' + } + ) + impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', nil) + log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) + expect(log_event.params).to eq(@expected_impression_params) + expect(log_event.url).to eq(@expected_endpoints[:EU]) expect(log_event.http_verb).to eq(:post) end @@ -134,7 +163,7 @@ 'browser_type' => 'firefox') log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -184,7 +213,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -225,7 +254,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -248,7 +277,7 @@ 'browser_type' => false) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -270,7 +299,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', 'browser_type' => 0) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -286,7 +315,7 @@ invalid_attribute: 'sorry_not_sorry') log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -294,7 +323,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -309,7 +338,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', {'browser_type' => 'firefox'}, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -322,7 +351,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -335,7 +364,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -347,7 +376,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -359,7 +388,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -375,7 +404,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -390,7 +419,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -404,7 +433,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -418,7 +447,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -432,7 +461,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -447,7 +476,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -461,7 +490,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -487,7 +516,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', {'browser_type' => 'firefox'}, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -520,7 +549,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', user_attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -556,7 +585,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', user_attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -594,7 +623,7 @@ ) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -620,7 +649,45 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) + expect(log_event.http_verb).to eq(:post) + end + + it 'should create valid Event when create_conversion_event is called with Bucketing ID attribute and with EU' do + @expected_conversion_params[:visitors][0][:attributes].unshift( + { + entity_id: '111094', + key: 'browser_type', + type: 'custom', + value: 'firefox' + }, + entity_id: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BUCKETING_ID'], + key: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BUCKETING_ID'], + type: 'custom', + value: 'variation' + ) + + user_attributes = { + 'browser_type' => 'firefox', + '$opt_bucketing_id' => 'variation' + } + + allow_any_instance_of(Optimizely::ConversionEvent).to receive(:event_context).and_return( + { + account_id: '12001', + project_id: '111001', + client_version: Optimizely::VERSION, + revision: '42', + client_name: Optimizely::CLIENT_ENGINE, + anonymize_ip: false, + region: 'EU' + } + ) + + conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) + log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) + expect(log_event.params).to eq(@expected_conversion_params) + expect(log_event.url).to eq(@expected_endpoints[:EU]) expect(log_event.http_verb).to eq(:post) end @@ -642,7 +709,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -671,7 +738,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end end diff --git a/spec/event/user_event_factory_spec.rb b/spec/event/user_event_factory_spec.rb index f9876c23..f1ed533e 100644 --- a/spec/event/user_event_factory_spec.rb +++ b/spec/event/user_event_factory_spec.rb @@ -46,6 +46,7 @@ expect(impression_event.event_context[:project_id]).to eq(project_config.project_id) expect(impression_event.event_context[:revision]).to eq(project_config.revision) expect(impression_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(impression_event.event_context[:region]).to eq(project_config.region) expect(impression_event.bot_filtering).to eq(project_config.bot_filtering) expect(impression_event.experiment_id).to eq(experiment['id']) expect(impression_event.variation_id).to eq('111128') @@ -79,6 +80,7 @@ expect(impression_event.event_context[:project_id]).to eq(project_config.project_id) expect(impression_event.event_context[:revision]).to eq(project_config.revision) expect(impression_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(impression_event.event_context[:region]).to eq(project_config.region) expect(impression_event.bot_filtering).to eq(project_config.bot_filtering) expect(impression_event.experiment_id).to eq(experiment['id']) expect(impression_event.variation_id).to eq('111128') @@ -108,6 +110,7 @@ expect(conversion_event.event_context[:project_id]).to eq(project_config.project_id) expect(conversion_event.event_context[:revision]).to eq(project_config.revision) expect(conversion_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(conversion_event.event_context[:region]).to eq(project_config.region) expect(conversion_event.event['key']).to eq(event['key']) expect(conversion_event.bot_filtering).to eq(project_config.bot_filtering) expect(conversion_event.user_id).to eq('test_user') @@ -141,6 +144,7 @@ expect(conversion_event.event_context[:project_id]).to eq(project_config.project_id) expect(conversion_event.event_context[:revision]).to eq(project_config.revision) expect(conversion_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(conversion_event.event_context[:region]).to eq(project_config.region) expect(conversion_event.event['key']).to eq(event['key']) expect(conversion_event.bot_filtering).to eq(project_config.bot_filtering) expect(conversion_event.user_id).to eq('test_user') diff --git a/spec/event_builder_spec.rb b/spec/event_builder_spec.rb index 4201c579..62cf5ba1 100644 --- a/spec/event_builder_spec.rb +++ b/spec/event_builder_spec.rb @@ -39,7 +39,10 @@ allow(Time).to receive(:now).and_return(time_now) allow(SecureRandom).to receive(:uuid).and_return('a68cf1ad-0393-4e18-af87-efe8f01a7c9c') - @expected_endpoint = 'https://logx.optimizely.com/v1/events' + @expected_endpoints = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + } @expected_impression_params = { account_id: '12001', project_id: '111001', @@ -69,7 +72,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } @expected_conversion_params = { account_id: '12001', @@ -95,7 +99,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -103,7 +108,7 @@ experiment = config.get_experiment_from_key('test_experiment') impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', nil) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -119,7 +124,7 @@ impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', 'browser_type' => 'firefox') expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -161,7 +166,7 @@ } impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -194,7 +199,7 @@ } impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -210,7 +215,7 @@ impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', 'browser_type' => false) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -225,7 +230,7 @@ experiment = config.get_experiment_from_key('test_experiment') impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', 'browser_type' => 0) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -234,14 +239,14 @@ impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', invalid_attribute: 'sorry_not_sorry') expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end it 'should create a valid Event when create_conversion_event is called' do conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -255,7 +260,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', {'browser_type' => 'firefox'}, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -267,7 +272,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -279,7 +284,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -290,7 +295,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -301,7 +306,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -316,7 +321,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -330,7 +335,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -343,7 +348,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -356,7 +361,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -369,7 +374,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -383,7 +388,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -396,7 +401,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -421,7 +426,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', {'browser_type' => 'firefox'}, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -447,7 +452,7 @@ experiment = config.get_experiment_from_key('test_experiment') impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', user_attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -476,7 +481,7 @@ expect(config.send(:bot_filtering)).to eq(true) impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', user_attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -505,7 +510,7 @@ allow(config).to receive(:bot_filtering).and_return(false) impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', user_attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -530,7 +535,7 @@ } conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', user_attributes, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -551,7 +556,7 @@ } conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', user_attributes, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -579,7 +584,7 @@ allow(config).to receive(:bot_filtering).and_return(false) conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', user_attributes, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end end diff --git a/spec/odp/lru_cache_spec.rb b/spec/odp/lru_cache_spec.rb index 46363c8b..32db021f 100644 --- a/spec/odp/lru_cache_spec.rb +++ b/spec/odp/lru_cache_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # -# Copyright 2022, Optimizely and contributors +# Copyright 2022-2025, Optimizely and contributors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -149,4 +149,84 @@ cache.save('cow', 'crate') expect(cache.lookup('cow')).to eq 'crate' end + + it 'should remove existing key' do + cache = Optimizely::LRUCache.new(3, 1000) + + cache.save('1', 100) + cache.save('2', 200) + cache.save('3', 300) + + expect(cache.lookup('1')).to eq 100 + expect(cache.lookup('2')).to eq 200 + expect(cache.lookup('3')).to eq 300 + + cache.remove('2') + + expect(cache.lookup('1')).to eq 100 + expect(cache.lookup('2')).to be_nil + expect(cache.lookup('3')).to eq 300 + end + + it 'should handle removing non-existent key' do + cache = Optimizely::LRUCache.new(3, 1000) + cache.save('1', 100) + cache.save('2', 200) + + cache.remove('3') # Doesn't exist + + expect(cache.lookup('1')).to eq 100 + expect(cache.lookup('2')).to eq 200 + end + + it 'should handle removing from zero sized cache' do + cache = Optimizely::LRUCache.new(0, 1000) + cache.save('1', 100) + cache.remove('1') + + expect(cache.lookup('1')).to be_nil + end + + it 'should handle removing and adding back a key' do + cache = Optimizely::LRUCache.new(3, 1000) + cache.save('1', 100) + cache.save('2', 200) + cache.save('3', 300) + + cache.remove('2') + cache.save('2', 201) + + expect(cache.lookup('1')).to eq 100 + expect(cache.lookup('2')).to eq 201 + expect(cache.lookup('3')).to eq 300 + end + + it 'should handle thread safety' do + max_size = 100 + cache = Optimizely::LRUCache.new(max_size, 1000) + + (1..max_size).each do |i| + cache.save(i.to_s, i * 100) + end + + threads = [] + (1..(max_size / 2)).each do |i| + thread = Thread.new do + cache.remove(i.to_s) + end + threads << thread + end + + threads.each(&:join) + + (1..max_size).each do |i| + if i <= max_size / 2 + expect(cache.lookup(i.to_s)).to be_nil + else + expect(cache.lookup(i.to_s)).to eq(i * 100) + end + end + + expect(cache.instance_variable_get('@map').size).to eq(max_size / 2) + end end diff --git a/spec/optimizely_user_context_spec.rb b/spec/optimizely_user_context_spec.rb index c968c336..42d71065 100644 --- a/spec/optimizely_user_context_spec.rb +++ b/spec/optimizely_user_context_spec.rb @@ -251,7 +251,9 @@ variation_key: '3324490562', rule_key: nil, reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: '3324490562' ) user_context_obj = forced_decision_project_instance.create_user_context(user_id) context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, nil) @@ -347,7 +349,9 @@ variation_key: 'b', rule_key: 'exp_with_audience', reasons: ['Variation (b) is mapped to flag (feature_1), rule (exp_with_audience) and user (tester) in the forced decision map.'], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: '10390977673', + variation_id: '10416523121' ) user_context_obj = Optimizely::OptimizelyUserContext.new(forced_decision_project_instance, user_id, original_attributes) context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, 'exp_with_audience') @@ -464,7 +468,9 @@ variation_key: '3324490562', rule_key: nil, reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: '3324490562' ) user_context_obj = forced_decision_project_instance.create_user_context(user_id) context_with_flag = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, nil) @@ -550,6 +556,7 @@ decision = user_context_obj.decide(feature_key, [Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS]) expect(decision.variation_key).to eq('18257766532') expect(decision.rule_key).to eq('18322080788') + # puts decision.reasons expect(decision.reasons).to include('Invalid variation is mapped to flag (feature_1), rule (exp_with_audience) and user (tester) in the forced decision map.') # delivery-rule-to-decision diff --git a/spec/project_spec.rb b/spec/project_spec.rb index 7c02f765..9abbc39f 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -1681,7 +1681,7 @@ def callback(_args); end it 'should return false and send an impression when the user is not bucketed into any variation' do 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(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.is_feature_enabled('multi_variate_feature', 'test_user')).to be(false) @@ -1703,7 +1703,7 @@ def callback(_args); end ) 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_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) expect(project_instance.is_feature_enabled('boolean_single_variable_feature', 'test_user')).to be true @@ -1723,7 +1723,7 @@ def callback(_args); end Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) 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_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) expect(variation_to_return['featureEnabled']).to be false expect(project_instance.is_feature_enabled('boolean_single_variable_feature', 'test_user')).to be false @@ -1744,7 +1744,7 @@ def callback(_args); end Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) 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_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) expect(variation_to_return['featureEnabled']).to be true expect(project_instance.is_feature_enabled('boolean_single_variable_feature', 'test_user')).to be true @@ -1841,7 +1841,7 @@ def callback(_args); end Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], any_args ).ordered - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) expect(project_instance.is_feature_enabled('multi_variate_feature', 'test_user')).to be true @@ -1862,7 +1862,7 @@ def callback(_args); end Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) expect(variation_to_return['featureEnabled']).to be false - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) expect(project_instance.is_feature_enabled('multi_variate_feature', 'test_user')).to be false @@ -1888,7 +1888,7 @@ def callback(_args); end Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) # Activate listener expect(project_instance.notification_center).to receive(:send_notifications).once.with( @@ -1925,7 +1925,7 @@ def callback(_args); end Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args @@ -1959,7 +1959,7 @@ 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) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) # DECISION listener called when the user is in rollout with variation feature true. expect(variation_to_return['featureEnabled']).to be true @@ -1983,8 +1983,21 @@ def callback(_args); end end it 'should call decision listener when user is bucketed into rollout with featureEnabled property is false' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::Decision) + experiment_to_return = config_body['rollouts'][0]['experiments'][1] + variation_to_return = experiment_to_return['variations'][0] + decision_to_return = Optimizely::DecisionService::Decision.new( + experiment_to_return, + variation_to_return, + Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] + ) + # Ensure featureEnabled is false for this test + expect(variation_to_return['featureEnabled']).to be false + + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, [])) + expect(project_instance.notification_center).to receive(:send_notifications).once.with( + Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args + ).ordered # DECISION listener called when the user is in rollout with variation feature off. expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -1999,7 +2012,7 @@ def callback(_args); end end it 'call decision listener when the user is not bucketed into any experiment or rollout' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args ).ordered @@ -2079,7 +2092,7 @@ def callback(_args); end it 'should return only enabled feature flags keys' do # Sets all feature-flags keys with randomly assigned status features_keys = project_config.feature_flags.map do |item| - {key: (item['key']).to_s, value: [true, false].sample} # '[true, false].sample' generates random boolean + {key: item['key'].to_s, value: [true, false].sample} # '[true, false].sample' generates random boolean end enabled_features = features_keys.map { |x| x[:key] if x[:value] == true }.compact @@ -2113,26 +2126,42 @@ def callback(_args); end rollout_to_return = config_body['rollouts'][0]['experiments'][0] allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return( - Optimizely::DecisionService::Decision.new( - experiment_to_return, - experiment_to_return['variations'][0], - Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] + Optimizely::DecisionService::DecisionResult.new( + Optimizely::DecisionService::Decision.new( + experiment_to_return, + experiment_to_return['variations'][0], + Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] + ), false, [] ), - nil, - Optimizely::DecisionService::Decision.new( - rollout_to_return, - rollout_to_return['variations'][0], - Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] + Optimizely::DecisionService::DecisionResult.new( + nil, false, [] ), - Optimizely::DecisionService::Decision.new( - experiment_to_return, - experiment_to_return['variations'][1], - Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] + Optimizely::DecisionService::DecisionResult.new( + Optimizely::DecisionService::Decision.new( + rollout_to_return, + rollout_to_return['variations'][0], + Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] + ), false, [] + ), + Optimizely::DecisionService::DecisionResult.new( + Optimizely::DecisionService::Decision.new( + experiment_to_return, + experiment_to_return['variations'][1], + Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] + ), false, [] + ), + Optimizely::DecisionService::DecisionResult.new( + nil, false, [] + ), + Optimizely::DecisionService::DecisionResult.new( + nil, false, [] ), - nil, - nil, - nil, - nil + Optimizely::DecisionService::DecisionResult.new( + nil, false, [] + ), + Optimizely::DecisionService::DecisionResult.new( + nil, false, [] + ) ) expect(project_instance.notification_center).to receive(:send_notifications).exactly(10).times.with( @@ -2274,7 +2303,8 @@ def callback(_args); end 'experiment' => nil, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_string('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq('wingardium leviosa') @@ -2294,7 +2324,8 @@ def callback(_args); end 'experiment' => nil, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_string('boolean_single_variable_feature', 'boolean_variable', user_id, user_attributes)) .to eq(nil) @@ -2315,7 +2346,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_string('integer_single_variable_feature', 'integer_variable', user_id, user_attributes)) .to eq(nil) @@ -2334,7 +2366,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_string('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq('cta_1') @@ -2351,7 +2384,7 @@ def callback(_args); end describe 'when the feature flag is not enabled for the user' do it 'should return the default variable value' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.get_feature_variable_string('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq('wingardium leviosa') @@ -2424,7 +2457,8 @@ def callback(_args); end 'experiment' => nil, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -2496,7 +2530,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -2525,7 +2560,7 @@ def callback(_args); end describe 'when the feature flag is not enabled for the user' do it 'should return the default variable value' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -2608,7 +2643,8 @@ def callback(_args); end 'experiment' => nil, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_boolean('boolean_single_variable_feature', 'boolean_variable', user_id, user_attributes)) .to eq(true) @@ -2652,8 +2688,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_double('double_single_variable_feature', 'double_variable', user_id, user_attributes)) .to eq(42.42) @@ -2698,8 +2734,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable_integer('integer_single_variable_feature', 'integer_variable', user_id, user_attributes)) .to eq(42) @@ -2741,7 +2777,8 @@ def callback(_args); end Decision = Struct.new(:experiment, :variation, :source) # rubocop:disable Lint/ConstantDefinitionInBlock variation_to_return = project_config.rollout_id_map['166661']['experiments'][0]['variations'][0] decision_to_return = Decision.new({'key' => 'test-exp'}, variation_to_return, 'feature-test') - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decisiont_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decisiont_result_to_return) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -2814,7 +2851,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decisiont_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decisiont_result_to_return) allow(project_config).to receive(:variation_id_to_variable_usage_map).and_return(variation_id_to_variable_usage_map) expect(project_instance.notification_center).to receive(:send_notifications).once.with( @@ -2873,7 +2911,7 @@ def callback(_args); end describe 'when the feature flag is not enabled for the user' do it 'should return the default variable value' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -2976,7 +3014,8 @@ def callback(_args); end 'experiment' => nil, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq('wingardium leviosa') @@ -2996,7 +3035,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq('cta_1') @@ -3017,7 +3057,8 @@ def callback(_args); end 'experiment' => nil, 'variation' => variation_to_return } - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable('boolean_single_variable_feature', 'boolean_variable', user_id, user_attributes)) .to eq(true) @@ -3038,8 +3079,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable('double_single_variable_feature', 'double_variable', user_id, user_attributes)) .to eq(42.42) @@ -3060,8 +3101,8 @@ def callback(_args); end 'experiment' => experiment_to_return, 'variation' => variation_to_return } - - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) expect(project_instance.get_feature_variable('integer_single_variable_feature', 'integer_variable', user_id, user_attributes)) .to eq(42) @@ -3078,7 +3119,7 @@ def callback(_args); end describe 'when the feature flag is not enabled for the user' do it 'should return the default variable value' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.get_feature_variable('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq('wingardium leviosa') @@ -3243,8 +3284,8 @@ 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_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) # DECISION listener called when the user is in experiment with variation feature off. expect(project_instance.notification_center).to receive(:send_notifications).once.with( @@ -3287,8 +3328,8 @@ 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_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) # DECISION listener called when the user is in experiment with variation feature on. expect(project_instance.notification_center).to receive(:send_notifications).once.with( @@ -3325,8 +3366,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_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) # DECISION listener called when the user is in rollout with variation feature on. expect(variation_to_return['featureEnabled']).to be true @@ -3360,7 +3401,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_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_result_to_return) # DECISION listener called when the user is in rollout with variation feature on. expect(variation_to_return['featureEnabled']).to be false @@ -3392,7 +3434,7 @@ def callback(_args); end end it 'should call listener with default variable type and value, when user neither in experiment nor in rollout' do - allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil) + allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(Optimizely::DecisionService::DecisionResult.new(nil, false, [])) expect(project_instance.notification_center).to receive(:send_notifications).once.with( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], @@ -3758,7 +3800,9 @@ def callback(_args); end variation_key: 'Fred', rule_key: 'test_experiment_multivariate', reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: experiment_to_return['id'], + variation_id: variation_to_return['id'] ) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) decision_to_return = Optimizely::DecisionService::Decision.new( @@ -3766,8 +3810,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) decision_list_to_be_returned = [] - decision_list_to_be_returned << [decision_to_return, []] + decision_list_to_be_returned << decision_result_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') @@ -3801,7 +3846,9 @@ def callback(_args); end variation_key: 'Fred', rule_key: 'test_experiment_multivariate', reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: experiment_to_return['id'], + variation_id: variation_to_return['id'] ) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) decision_to_return = Optimizely::DecisionService::Decision.new( @@ -3809,8 +3856,9 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) decision_list_to_be_returned = [] - decision_list_to_be_returned << [decision_to_return, []] + decision_list_to_be_returned << decision_result_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') @@ -3883,7 +3931,9 @@ def callback(_args); end variation_key: 'Fred', rule_key: 'test_experiment_multivariate', reasons: [], - decision_event_dispatched: false + decision_event_dispatched: false, + experiment_id: experiment_to_return['id'], + variation_id: variation_to_return['id'] ) allow(project_config).to receive(:send_flag_decisions).and_return(false) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) @@ -3892,7 +3942,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] ) - decision_list_to_return = [[decision_to_return, []]] + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + decision_list_to_return = [decision_result_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') @@ -3921,7 +3972,9 @@ def callback(_args); end variation_key: nil, rule_key: nil, reasons: [], - decision_event_dispatched: false + decision_event_dispatched: false, + experiment_id: nil, + variation_id: nil ) allow(project_config).to receive(:send_flag_decisions).and_return(false) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) @@ -3958,7 +4011,9 @@ def callback(_args); end variation_key: nil, rule_key: nil, reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: nil ) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) decision_to_return = nil @@ -4060,7 +4115,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - decision_list_to_be_returned = [[decision_to_return, []]] + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + decision_list_to_be_returned = [decision_result_to_return] allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) 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') @@ -4084,7 +4140,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - decision_list_to_return = [[decision_to_return, []]] + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + decision_list_to_return = [decision_result_to_return] allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) 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') @@ -4122,7 +4179,9 @@ def callback(_args); end "The user 'user1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'.", "Feature flag 'multi_variate_feature' is not used in a rollout." ], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: nil ) expect(project_instance.notification_center).to receive(:send_notifications) .once.with(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args) @@ -4162,7 +4221,9 @@ def callback(_args); end variation_key: nil, rule_key: nil, reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: nil ) allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) user_context = project_instance.create_user_context('user1') @@ -4187,7 +4248,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - decision_list_to_return = [[decision_to_return, []]] + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + decision_list_to_return = [decision_result_to_return] allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) 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') @@ -4219,6 +4281,130 @@ def callback(_args); end ]) end end + describe 'when decision service fails with CMAB error' do + it 'should return error decision when CMAB decision service fails' do + # Add the HTTP stub to prevent real requests + stub_request(:post, 'https://logx.optimizely.com/v1/events') + .to_return(status: 200, body: '', headers: {}) + + feature_flag_key = 'boolean_single_variable_feature' + + # Mock the decision service to return an error result + error_decision_result = double('DecisionResult') + allow(error_decision_result).to receive(:decision).and_return(nil) + allow(error_decision_result).to receive(:error).and_return(true) + allow(error_decision_result).to receive(:reasons).and_return(['CMAB service failed to fetch decision']) + + # Mock get_variations_for_feature_list instead of get_variation_for_feature + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list) + .and_return([error_decision_result]) + + user_context = project_instance.create_user_context('test_user') + decision = user_context.decide(feature_flag_key) + + expect(decision.enabled).to eq(false) + expect(decision.variation_key).to be_nil + expect(decision.flag_key).to eq(feature_flag_key) + expect(decision.reasons).to include('CMAB service failed to fetch decision') + end + end + describe 'CMAB experiments' do + it 'should include CMAB UUID in dispatched event when decision service returns CMAB result' do + # Use an existing feature flag from the test config + feature_flag_key = 'boolean_single_variable_feature' + + # Get an existing experiment that actually exists in the datafile + # Looking at the test config, let's use experiment ID '122230' which exists + existing_experiment = project_config.get_experiment_from_id('122230') + + # Modify the existing experiment to be a CMAB experiment + cmab_experiment = existing_experiment.dup + cmab_experiment['trafficAllocation'] = [] # Empty for CMAB + cmab_experiment['cmab'] = {'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000} + + # Mock the config to return our modified CMAB experiment + allow(project_instance.config_manager.config).to receive(:get_experiment_from_id) + .with('122230') + .and_return(cmab_experiment) + + allow(project_instance.config_manager.config).to receive(:experiment_running?) + .with(cmab_experiment) + .and_return(true) + + # Get the feature flag and update it to reference our CMAB experiment + feature_flag = project_instance.config_manager.config.get_feature_flag_from_key(feature_flag_key) + feature_flag['experimentIds'] = ['122230'] + + # Use existing variations from the original experiment + variation_to_use = existing_experiment['variations'][0] + + # Create a decision with CMAB UUID + expected_cmab_uuid = 'uuid-cmab' + decision_with_cmab = Optimizely::DecisionService::Decision.new( + cmab_experiment, + variation_to_use, + Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'], + expected_cmab_uuid + ) + + decision_result_with_cmab = Optimizely::DecisionService::DecisionResult.new( + decision_with_cmab, + false, + [] + ) + + # Mock get_variations_for_feature_list to return CMAB result + allow(project_instance.decision_service).to receive(:get_variations_for_feature_list) + .and_return([decision_result_with_cmab]) + + # Set up time and UUID mocks for consistent event data + allow(Time).to receive(:now).and_return(time_now) + allow(SecureRandom).to receive(:uuid).and_return('a68cf1ad-0393-4e18-af87-efe8f01a7c9c') + + # Create array to capture dispatched events + dispatched_events = [] + allow(project_instance.event_dispatcher).to receive(:dispatch_event) do |event| + dispatched_events << event + end + + user_context = project_instance.create_user_context('test_user') + decision = user_context.decide(feature_flag_key) + + # Wait for batch processing thread to send event + sleep 0.1 until project_instance.event_processor.event_queue.empty? + + # Verify the decision contains expected information + expect(decision.enabled).to eq(true) + expect(decision.variation_key).to eq(variation_to_use['key']) + expect(decision.rule_key).to eq(existing_experiment['key']) + expect(decision.flag_key).to eq(feature_flag_key) + + # Verify an event was dispatched + expect(dispatched_events.length).to eq(1) + + dispatched_event = dispatched_events[0] + + # Remove the puts statement and verify the event structure and CMAB UUID + expect(dispatched_event.params).to have_key(:visitors) + expect(dispatched_event.params[:visitors].length).to be > 0 + expect(dispatched_event.params[:visitors][0]).to have_key(:snapshots) + expect(dispatched_event.params[:visitors][0][:snapshots].length).to be > 0 + expect(dispatched_event.params[:visitors][0][:snapshots][0]).to have_key(:decisions) + expect(dispatched_event.params[:visitors][0][:snapshots][0][:decisions].length).to be > 0 + + # Get the metadata and assert CMAB UUID + metadata = dispatched_event.params[:visitors][0][:snapshots][0][:decisions][0][:metadata] + expect(metadata).to have_key(:cmab_uuid) + expect(metadata[:cmab_uuid]).to eq(expected_cmab_uuid) + + # Also verify other expected metadata fields + expect(metadata[:flag_key]).to eq(feature_flag_key) + expect(metadata[:rule_key]).to eq('test_experiment_multivariate') + expect(metadata[:rule_type]).to eq('feature-test') + expect(metadata[:variation_key]).to eq('Fred') + expect(metadata[:enabled]).to eq(true) + end + end end describe '#decide_all' do @@ -4407,7 +4593,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - decision_list_to_return = [[decision_to_return, []]] + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + decision_list_to_return = [decision_result_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_variations_for_feature_list).and_return(decision_list_to_return) user_context = custom_project_instance.create_user_context('user1') @@ -4436,7 +4623,8 @@ def callback(_args); end variation_to_return, Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] ) - decision_list_to_return = [[decision_to_return, []]] + decision_result_to_return = Optimizely::DecisionService::DecisionResult.new(decision_to_return, false, []) + decision_list_to_return = [decision_result_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_variations_for_feature_list).and_return(decision_list_to_return) user_context = custom_project_instance.create_user_context('user1') @@ -4481,7 +4669,9 @@ def callback(_args); end "The user 'user1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'.", "Feature flag 'multi_variate_feature' is not used in a rollout." ], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: nil ) allow(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) user_context = custom_project_instance.create_user_context('user1') @@ -4521,7 +4711,9 @@ def callback(_args); end variation_key: nil, rule_key: nil, reasons: [], - decision_event_dispatched: true + decision_event_dispatched: true, + experiment_id: nil, + variation_id: nil ) allow(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) user_context = custom_project_instance.create_user_context('user1') diff --git a/spec/spec_params.rb b/spec/spec_params.rb index e43ce3cc..8735218f 100644 --- a/spec/spec_params.rb +++ b/spec/spec_params.rb @@ -19,6 +19,7 @@ module OptimizelySpec VALID_CONFIG_BODY = { + 'region' => 'US', 'accountId' => '12001', 'projectId' => '111001', 'anonymizeIP' => false,