use log::{error, warn}; use serde_json::{json, Value}; use std::collections::HashMap; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::{SystemTime, UNIX_EPOCH}; use crate::assigner::VariantAssigner; use crate::context_publisher::ContextPublisher; use crate::matcher::{AudienceMatcher, RuleAction}; use crate::models::*; use crate::switchback::{self, AttributePath, Serving}; use crate::utils::{array_equals_shallow, hash_unit_value}; pub type EventLogger = Box) + Send + Sync>; struct Experiment { data: ExperimentData, variables: Vec>, /// `data.assignment_rules` parsed once when the data is installed; `None` /// when there are none or they are not valid JSON. rules: Option>>, /// Changes only when a data load changes the experiment's /// `assignment_rules`, so cached assignments compare it instead of the /// rule text. rules_rev: u64, /// Sorted by id and unique, as in JS. Never read for a switchback: /// holdouts never apply to one. holdouts: Vec>, /// Set when `data` is a switchback experiment. switchback: Option, } /// A switchback experiment as installed. struct SwitchbackDef { /// Kept across a data load while the switchback identity is unchanged /// (see [`same_switchback_identity`]), otherwise a fresh value. Cached /// slots from another generation are dropped on the next resolution. generation: u64, /// `switchbackClusters` in declared order with parsed paths; `None` for /// a missing, empty or unreadable map (the Collector's legacy path). sources: Option>, /// `(seedHi << 32) | seedLo`. seed: i64, /// Whether the schedule can be drawn at all (see /// [`switchback_schedule_servable`]). servable: bool, } const SWITCHBACK_ANCHOR_ALIGNMENT_MS: i64 = 3_600_000; /// 10000-01-01T00:00:00Z: the Collector's Registry serves no anchor at or /// after it. const SWITCHBACK_MAX_ANCHOR_MS: i64 = 253_402_300_800_000; /// Period slots kept per switchback experiment, besides the pre-start slot. const SWITCHBACK_RETAINED_PERIODS: usize = 2; fn is_switchback(experiment: &ExperimentData) -> bool { experiment.randomization_type.as_deref() == Some("switchback") } /// Whether the Collector's Registry can serve the schedule: an hour-aligned /// anchor in `[0, 10000-01-01)` (used as served, never rounded), a period /// over 0 and at least two arms, one `variants` entry per arm (otherwise the /// draw could serve and expose an arm with no variant). An unservable /// schedule only replaces the draw: assignment rules, the audience and /// full-on still apply. fn switchback_schedule_servable(experiment: &ExperimentData) -> bool { experiment.switchback_start_at.is_some_and(|start| { (0..SWITCHBACK_MAX_ANCHOR_MS).contains(&start) && start % SWITCHBACK_ANCHOR_ALIGNMENT_MS == 0 }) && experiment .switchback_period_ms .is_some_and(|period| period > 0) && experiment.split.len() >= 2 && experiment.split.len() == experiment.variants.len() } /// `floorDiv(now - switchbackStartAt, switchbackPeriodMs)`, or `None` /// before the anchor, for an unservable schedule and on the legacy path. fn switchback_period(def: &SwitchbackDef, experiment: &ExperimentData, now: i64) -> Option { if !def.servable || def.sources.is_none() { return None; } let start = experiment.switchback_start_at?; let period_ms = experiment.switchback_period_ms?; // now >= start >= 0, so the difference cannot overflow and floor // division is plain division. (now >= start).then(|| (now - start) / period_ms) } /// Everything that maps clusters and periods to variants: id, iteration, /// the ordered cluster map, the allow-list, anchor, period length and /// design, plus `unitType` when either side takes the legacy path. Seed, /// split and the analysis-only fields are not identity. fn same_switchback_identity(previous: &Experiment, installed: &Experiment) -> bool { let (Some(old), Some(new)) = (&previous.switchback, &installed.switchback) else { return false; }; let (a, b) = (&previous.data, &installed.data); a.id == b.id && a.iteration == b.iteration && a.switchback_clusters == b.switchback_clusters && a.switchback_cluster_values == b.switchback_cluster_values && a.switchback_start_at == b.switchback_start_at && a.switchback_period_ms == b.switchback_period_ms && a.switchback_design_type == b.switchback_design_type && ((old.sources.is_some() && new.sources.is_some()) || a.unit_type == b.unit_type) } /// A unit's arm in one holdout epoch, shared by every assignment that /// pinned it so the holdout's exposure is queued once per context. #[derive(Debug)] struct HoldoutAssignment { id: i64, iteration: i64, variant: i32, /// `split.len()` when `variant` was drawn: arm 1 means something else /// for a 3-arm holdout, and a same-iteration refresh must not change it. arm_count: usize, unit_type: String, exposed: AtomicBool, } #[derive(Debug, Clone)] struct PinnedHoldout { data: Arc, assignment: Option>, } /// Arm 0 holds a unit out of every covered experiment. Arm 1 of a 3-arm /// (`all_full_on`) holdout only allows full-on experiments. fn is_held_out_by(variant: i32, arm_count: usize, full_on_variant: i64) -> bool { variant == 0 || (arm_count == 3 && variant == 1 && full_on_variant == 0) } /// What a cached assignment's rule outcome was resolved against. Kept beside /// the public [`Assignment`] rather than in it, so it stays an internal /// detail of the cache. #[derive(Debug, Clone, Copy, Default)] struct RuleState { /// The variant was set by a matching assignment rule. rule_override: bool, /// A split rule matched but the unit was missing, so the full-on variant /// was used; setting the unit later re-randomizes it while unexposed. missing_unit: bool, /// The experiment's `rules_rev` when the rules were resolved. rules_rev: u64, /// The experiment's variant count when the rules were resolved; a change /// can put a fixed variant out of range or invalidate a split. variant_count: usize, } #[derive(Debug, Default)] struct CachedAssignment { assignment: Assignment, rule: RuleState, /// A covering holdout holds the unit out: control values, and no /// exposure of its own unless an override or a rule set the variant. suppressed: bool, holdouts: Vec, /// A refresh changed the experiment's holdout coverage. Checked on the /// next read, as in JS, so a change that a later refresh undoes keeps /// the assignment and its exposure. coverage_stale: AtomicBool, /// Set for a switchback slot. switchback: Option>, } /// How a switchback slot was resolved. #[derive(Debug)] struct SwitchbackSlot { /// Read once per slot: a rebuild in the same period keeps it unless no /// linked unit was set (`Absent`). serving: Serving, /// The clock reading `serving` was resolved at. The Collector resolves /// the cluster again at `exposedAt`, so an exposure whose own reading /// no longer gives `serving` is stamped with this one instead. resolved_at: i64, /// The targeting the slot was resolved under; a refresh that changes it /// rebuilds the slot. audience: String, audience_strict: bool, /// The Collector records nothing for it: no linked unit, an ineligible /// cluster, or no draw (before the anchor or an unservable schedule). silent: bool, /// The custom assignment when the slot was resolved. custom: Option, } /// One switchback experiment's cached slots, all from one generation. #[derive(Debug, Default)] struct SwitchbackEpoch { generation: u64, /// Before the anchor, an unservable schedule, or the legacy path. pre_start: Option, /// By period index; at most [`SWITCHBACK_RETAINED_PERIODS`]. periods: Vec<(i64, CachedAssignment)>, } impl SwitchbackEpoch { fn slot(&self, period: Option) -> Option<&CachedAssignment> { match period { None => self.pre_start.as_ref(), Some(p) => self.periods.iter().find(|(i, _)| *i == p).map(|(_, c)| c), } } fn slot_mut(&mut self, period: Option) -> Option<&mut CachedAssignment> { match period { None => self.pre_start.as_mut(), Some(p) => self .periods .iter_mut() .find(|(i, _)| *i == p) .map(|(_, c)| c), } } /// Install `slot`. A third period evicts the lowest index other than /// `period`, so the slot just installed always stays; retention is by /// index, not recency. fn install(&mut self, period: Option, slot: CachedAssignment) { let Some(p) = period else { self.pre_start = Some(slot); return; }; match self.periods.iter_mut().find(|(i, _)| *i == p) { Some(entry) => entry.1 = slot, None => self.periods.push((p, slot)), } while self.periods.len() > SWITCHBACK_RETAINED_PERIODS { let evicted = self .periods .iter() .enumerate() .filter(|(_, (i, _))| *i != p) .min_by_key(|(_, (i, _))| *i) .map(|(at, _)| at) .expect("more than one period is retained"); self.periods.remove(evicted); } } } /// Where an experiment's current assignment is cached. Switchback slots /// carry the clock reading they were resolved at, which stamps their /// exposure. #[derive(Debug, Clone, Copy)] enum Slot { Unit, Switchback { period: Option, now: i64 }, } pub type DataFetcher = Box Result + Send + Sync>; pub struct Context { units: HashMap, attrs: Vec, data: ContextData, assignments: HashMap, exposures: Vec, goals: Vec, overrides: HashMap, cassignments: HashMap, state: ContextState, failed: bool, failed_error: Option, pending: usize, attrs_seq: u64, rules_rev: u64, index: HashMap, index_variables: HashMap>, /// Holdout arms by (holdout id, covered experiment's unit type); kept /// across refreshes and replaced only when the iteration changes. holdout_assignments: HashMap<(i64, String), Arc>, assigners: HashMap, hashes: HashMap, audience_matcher: AudienceMatcher, event_logger: Option, publish_delay: i64, refresh_period: i64, data_fetcher: Option, publisher: Option>, /// The client's `application` and `environment` names, seeded under /// user attributes when evaluating audiences and rules. system_attrs: HashMap, /// Switchback slots by experiment name. Unit experiments are cached in /// `assignments` only. switchbacks: HashMap, /// Source of [`SwitchbackDef::generation`]. switchback_generations: u64, /// The installed data has a switchback experiment. Reads check it before /// looking for one, so payloads without switchbacks pay nothing. has_switchbacks: bool, /// Test seam for the clock; `None` reads the system time. clock: Option i64 + Send + Sync>>, #[cfg(test)] rule_evaluations: usize, /// Switchback lookups made by `assign`. #[cfg(test)] switchback_lookups: usize, /// Reads that went through the generic (switchback-aware) `assign`. #[cfg(test)] generic_assigns: usize, } impl Context { fn base() -> Self { Self { units: HashMap::new(), attrs: Vec::new(), data: ContextData::default(), assignments: HashMap::new(), exposures: Vec::new(), goals: Vec::new(), overrides: HashMap::new(), cassignments: HashMap::new(), state: ContextState::Loading, failed: false, failed_error: None, pending: 0, attrs_seq: 0, rules_rev: 0, index: HashMap::new(), index_variables: HashMap::new(), holdout_assignments: HashMap::new(), switchbacks: HashMap::new(), switchback_generations: 0, has_switchbacks: false, assigners: HashMap::new(), hashes: HashMap::new(), audience_matcher: AudienceMatcher::new(), event_logger: None, publish_delay: 0, refresh_period: 0, data_fetcher: None, publisher: None, system_attrs: HashMap::new(), clock: None, #[cfg(test)] rule_evaluations: 0, #[cfg(test)] switchback_lookups: 0, #[cfg(test)] generic_assigns: 0, } } fn now(&self) -> i64 { match &self.clock { Some(clock) => clock(), None => now_millis(), } } pub fn new(data: ContextData) -> Self { let mut ctx = Self::base(); ctx.init(data); ctx.state = ContextState::Ready; ctx } /// Create a ready context. The `event_logger` in `options` receives the /// `ready` event before this returns. pub fn new_with_options(data: ContextData, options: ContextOptions) -> Self { let mut ctx = Self::new_loading_with_options(options); ctx.become_ready(data); ctx } pub fn new_loading() -> Self { Self::base() } /// Create a context that is still loading. Resolve it with /// [`Context::become_ready`] or [`Context::become_failed_with_error`]; the /// `event_logger` in `options` then receives the `ready` or `error` event. pub fn new_loading_with_options(options: ContextOptions) -> Self { let mut ctx = Self::base(); ctx.event_logger = options.event_logger; ctx.publish_delay = options.publish_delay; ctx.refresh_period = options.refresh_period; ctx } /// Move a loading context to ready and emit `ready` with the data. /// Does nothing if the context is not loading. pub fn become_ready(&mut self, data: ContextData) { if self.state == ContextState::Loading { self.init(data); self.state = ContextState::Ready; match serde_json::to_value(&self.data) { Ok(value) => self.log_event("ready", Some(value)), Err(e) => { error!("Failed to serialize context data for ready: {}", e); self.log_event("ready", None); } } } } /// Move a loading context to failed and emit `error` with no data. /// Does nothing if the context is not loading. pub fn become_failed(&mut self) { if self.state == ContextState::Loading { self.init(ContextData::default()); self.state = ContextState::Failed; self.failed = true; self.log_event("error", None); } } /// Move a loading context to failed, keep `error` for /// [`Context::ready_error`], and emit `error` with `{"message": error}`. /// Does nothing if the context is not loading. pub fn become_failed_with_error(&mut self, error: String) { if self.state == ContextState::Loading { self.init(ContextData::default()); self.state = ContextState::Failed; self.failed = true; self.failed_error = Some(error.clone()); self.log_error(&error); } } pub fn ready_error(&self) -> Option<&String> { self.failed_error.as_ref() } pub fn set_event_logger(&mut self, logger: EventLogger) { self.event_logger = Some(std::sync::Arc::from(logger)); } pub fn set_publisher(&mut self, publisher: Box) { self.publisher = Some(publisher); } /// Record the client's `application` and `environment` names so audiences /// can target them. These are not stored as user attributes: they seed the /// evaluation map and any user attribute of the same name overrides them. /// Empty names are ignored, so the key is simply absent when nothing is /// configured. Changing a stored value bumps the attribute sequence, so a /// previously cached assignment is re-evaluated against the new value. pub fn set_system_attributes(&mut self, application: Option<&str>, environment: Option<&str>) { let system_attrs: HashMap = [("application", application), ("environment", environment)] .into_iter() .filter_map(|(name, value)| { value .filter(|v| !v.is_empty()) .map(|v| (name.to_string(), Value::from(v))) }) .collect(); if system_attrs != self.system_attrs { self.system_attrs = system_attrs; self.attrs_seq += 1; } } pub fn publish_delay(&self) -> i64 { self.publish_delay } pub fn refresh_period(&self) -> i64 { self.refresh_period } fn init(&mut self, data: ContextData) { self.data = data; self.has_switchbacks = self.data.experiments.iter().any(is_switchback); let mut previous = std::mem::take(&mut self.index); self.index_variables.clear(); let holdouts_by_id: HashMap> = self .data .holdouts .iter() .filter(|holdout| !holdout.split.is_empty()) .map(|holdout| (holdout.id, Arc::new(holdout.clone()))) .collect(); for experiment in &self.data.experiments { let mut variables: Vec> = Vec::new(); for (variant_idx, variant) in experiment.variants.iter().enumerate() { let parsed: HashMap = variant .config .as_ref() .and_then(|c| { if c.is_empty() { None } else { match serde_json::from_str(c) { Ok(v) => Some(v), Err(e) => { error!( "Failed to parse variant config for experiment '{}', variant {}: {}. Config: '{}'", experiment.name, variant_idx, e, c ); None } } } }) .unwrap_or_default(); for key in parsed.keys() { self.index_variables .entry(key.clone()) .or_default() .push(experiment.name.clone()); } variables.push(parsed); } let old = previous.remove(&experiment.name); let unchanged = old .as_ref() .filter(|old| old.data.assignment_rules == experiment.assignment_rules); let (rules, rules_rev) = match unchanged { Some(old) => (old.rules.clone(), old.rules_rev), None => { let rules = match self .audience_matcher .parse_rules(&experiment.assignment_rules) { Ok(rules) => rules.map(Arc::new), Err(e) => { warn!( "Invalid assignmentRules JSON for experiment '{}'; its rules are ignored: {}", experiment.name, e ); None } }; self.rules_rev += 1; (rules, self.rules_rev) } }; let mut holdouts: Vec> = experiment .holdout_ids .iter() .filter_map(|id| holdouts_by_id.get(id).cloned()) .collect(); holdouts.sort_by_key(|holdout| holdout.id); holdouts.dedup_by_key(|holdout| holdout.id); let switchback = is_switchback(experiment).then(|| SwitchbackDef { generation: 0, sources: switchback::sources(experiment.switchback_clusters.as_deref()), seed: ((u64::from(experiment.seed_hi) << 32) | u64::from(experiment.seed_lo)) as i64, servable: switchback_schedule_servable(experiment), }); let mut installed = Experiment { data: experiment.clone(), variables, rules, rules_rev, holdouts, switchback, }; if installed.switchback.is_some() { let generation = match old.as_ref() { Some(old) if same_switchback_identity(old, &installed) => { old.switchback.as_ref().map_or(0, |def| def.generation) } _ => { self.switchback_generations += 1; self.switchback_generations } }; if let Some(def) = installed.switchback.as_mut() { def.generation = generation; } } self.index.insert(experiment.name.clone(), installed); } } /// Like the JS SDK, a context whose load failed is still ready (with empty /// data), so only a pending load is "not ready". pub fn is_ready(&self) -> bool { self.state != ContextState::Loading } /// Whether the context data failed to load. Stays `true` once the /// context is finalized. pub fn is_failed(&self) -> bool { self.failed } pub fn is_finalized(&self) -> bool { self.state == ContextState::Finalized } pub fn is_finalizing(&self) -> bool { self.state == ContextState::Finalizing } pub fn is_closed(&self) -> bool { self.is_finalized() } pub fn is_closing(&self) -> bool { self.is_finalizing() } pub fn pending(&self) -> usize { self.pending } /// The context data. `Err` before the context is ready; still available /// once finalized. pub fn data(&self) -> Result<&ContextData, String> { self.check_ready(false)?; Ok(&self.data) } /// Set the UID for `unit_type`. Like the JS SDK, a UID is a string or a /// number and keeps its type: `get_unit` returns it as given. Strings are /// trimmed and must not be blank; numbers are hashed as their rounded /// integer (see [`crate::utils::hash_unit_value`]). pub fn set_unit(&mut self, unit_type: &str, uid: impl Into) -> Result<(), String> { self.check_not_finalized()?; let uid = match uid.into() { Value::String(s) => { let trimmed = s.trim(); if trimmed.is_empty() { return Err(format!("Unit '{}' UID must not be blank.", unit_type)); } Value::String(trimmed.to_string()) } Value::Number(n) => Value::Number(n), _ => { return Err(format!( "Unit '{}' must be a string or a number.", unit_type )) } }; if let Some(existing) = self.units.get(unit_type) { if *existing != uid { return Err(format!("Unit '{}' UID already set.", unit_type)); } } self.units.insert(unit_type.to_string(), uid); // A split rule or a covering holdout resolved without this unit is // resolved again now that it is available, but only while the // assignment is still unseen. self.assignments.retain(|_, a| { !((a.rule.missing_unit || a.holdouts.iter().any(|h| h.assignment.is_none())) && !a.assignment.exposed && a.assignment.unit_type.as_deref() == Some(unit_type)) }); // A switchback resolved before any linked unit was set is resolved // again. Unless overridden it never published anything, so it goes // even if exposed. for epoch in self.switchbacks.values_mut() { let keep = |c: &CachedAssignment| { !c.switchback .as_ref() .is_some_and(|s| s.serving == Serving::Absent) || (c.assignment.overridden && c.assignment.exposed) }; if epoch.pre_start.as_ref().is_some_and(|c| !keep(c)) { epoch.pre_start = None; } epoch.periods.retain(|(_, c)| keep(c)); } Ok(()) } pub fn set_units(&mut self, units: I) -> Result<(), String> where I: IntoIterator, K: Into, V: Into, { for (unit_type, uid) in units { self.set_unit(&unit_type.into(), uid)?; } Ok(()) } pub fn get_unit(&self, unit_type: &str) -> Option<&Value> { self.units.get(unit_type) } pub fn get_units(&self) -> &HashMap { &self.units } pub fn set_attribute(&mut self, name: &str, value: impl Into) -> Result<(), String> { self.check_not_finalized()?; let set_at = self.now(); self.attrs.push(Attribute { name: name.to_string(), value: value.into(), set_at, }); self.attrs_seq += 1; Ok(()) } pub fn set_attributes(&mut self, attrs: I) -> Result<(), String> where I: IntoIterator, K: Into, V: Into, { self.check_not_finalized()?; let set_at = self.now(); for (name, value) in attrs { self.attrs.push(Attribute { name: name.into(), value: value.into(), set_at, }); self.attrs_seq += 1; } Ok(()) } pub fn get_attribute(&self, name: &str) -> Option<&Value> { self.attrs .iter() .rev() .find(|a| a.name == name) .map(|a| &a.value) } pub fn get_attributes(&self) -> HashMap { let mut attrs = HashMap::new(); for attr in &self.attrs { attrs.insert(attr.name.clone(), attr.value.clone()); } attrs } /// The attribute map audiences are evaluated against: the client's /// `application` and `environment` names first (when configured and /// non-empty), then every user-set attribute overlaid on top so a user /// value of the same name always wins, including one set after the context /// was created. Because this folds in the current attributes, a later /// `set_attribute` bumps `attrs_seq` and invalidates any cached /// assignment, which is then re-evaluated against the new map. fn evaluation_attributes(&self) -> HashMap { let mut attrs = self.system_attrs.clone(); for attr in &self.attrs { attrs.insert(attr.name.clone(), attr.value.clone()); } attrs } fn system_environment(&self) -> Option<&str> { self.system_attrs.get("environment").and_then(Value::as_str) } pub fn set_override(&mut self, experiment_name: &str, variant: i32) -> Result<(), String> { self.overrides.insert(experiment_name.to_string(), variant); Ok(()) } pub fn set_overrides(&mut self, overrides: I) -> Result<(), String> where I: IntoIterator, K: Into, { for (experiment_name, variant) in overrides { self.set_override(&experiment_name.into(), variant)?; } Ok(()) } pub fn set_custom_assignment( &mut self, experiment_name: &str, variant: i32, ) -> Result<(), String> { self.check_not_finalized()?; self.cassignments .insert(experiment_name.to_string(), variant); Ok(()) } pub fn set_custom_assignments(&mut self, assignments: I) -> Result<(), String> where I: IntoIterator, K: Into, { for (experiment_name, variant) in assignments { self.set_custom_assignment(&experiment_name.into(), variant)?; } Ok(()) } /// The variant assigned to `experiment_name`, without queuing an exposure. /// `Err` before the context is ready and once it is finalizing or finalized. pub fn peek(&mut self, experiment_name: &str) -> Result { self.check_ready(true)?; if !self.has_switchbacks { return Ok(self.assign_unit(experiment_name).assignment.variant); } Ok(self.assign(experiment_name, &mut None).0.assignment.variant) } /// The variant assigned to `experiment_name`; queues an exposure the first /// time. `Err` before the context is ready and once it is finalizing or /// finalized. pub fn treatment(&mut self, experiment_name: &str) -> Result { self.check_ready(true)?; if !self.has_switchbacks { let a = &self.assign_unit(experiment_name).assignment; let (variant, exposed) = (a.variant, a.exposed); if !exposed { if let Some(entry) = self.assignments.get_mut(experiment_name) { entry.assignment.exposed = true; } self.trigger_exposures(experiment_name, Slot::Unit); } return Ok(variant); } let (cached, slot) = self.assign(experiment_name, &mut None); let (variant, exposed) = (cached.assignment.variant, cached.assignment.exposed); if !exposed { if let Some(entry) = self.cached_mut(experiment_name, slot) { entry.assignment.exposed = true; } self.trigger_exposures(experiment_name, slot); } Ok(variant) } /// Queue a goal. `properties` must be a JSON object or null; anything /// else is rejected, as in the JS SDK. pub fn track(&mut self, goal_name: &str, properties: impl Into) -> Result<(), String> { self.check_not_finalized()?; let properties = properties.into(); let properties_map: Option> = match properties { Value::Null => None, Value::Object(map) => Some(map.into_iter().collect()), _ => { return Err(format!( "Goal '{}' properties must be of type object.", goal_name )); } }; let goal = Goal { name: goal_name.to_string(), properties: properties_map, achieved_at: self.now(), }; match serde_json::to_value(&goal) { Ok(value) => self.log_event("goal", Some(value)), Err(e) => { error!("Failed to serialize goal '{}': {}", goal_name, e); } } self.goals.push(goal); self.pending += 1; Ok(()) } pub fn variable_value( &mut self, key: &str, default_value: impl Into, ) -> Result { self.check_ready(true)?; Ok(self .resolve_variable_value(key, true) .unwrap_or_else(|| default_value.into())) } pub fn peek_variable_value( &mut self, key: &str, default_value: impl Into, ) -> Result { self.check_ready(true)?; Ok(self .resolve_variable_value(key, false) .unwrap_or_else(|| default_value.into())) } /// The value of `key` from the first experiment that assigns it. A /// held-out unit reads the control value of the first suppressed /// experiment instead of the caller's default when nothing else wins. fn resolve_variable_value(&mut self, key: &str, expose: bool) -> Option { let experiment_names = self.index_variables.get(key)?.clone(); let mut suppressed_fallback: Option> = None; // One clock reading for the whole lookup, taken only for a switchback. let mut now = None; for exp_name in experiment_names { let (cached, slot) = if self.has_switchbacks { self.assign(&exp_name, &mut now) } else { (self.assign_unit(&exp_name), Slot::Unit) }; let a = &cached.assignment; let Some(variables) = &a.variables else { continue; }; let value = variables .get(key) .filter(|_| a.assigned || a.overridden || cached.rule.rule_override) .cloned(); if value.is_none() && suppressed_fallback.is_none() && cached.suppressed && !a.overridden && !cached.rule.rule_override { suppressed_fallback = Some(variables.get(key).cloned()); } // As in java-sdk, a switchback that is not assigned, overridden or // rule-won (a strict audience mismatch, say) is walked past without // an exposure; any other is exposed even when its variant lacks the // key. A unit experiment walked past is exposed as before. let walked_past_switchback = matches!(slot, Slot::Switchback { .. }) && !(a.assigned || a.overridden || cached.rule.rule_override); if expose && !a.exposed && !walked_past_switchback { if let Some(entry) = self.cached_mut(&exp_name, slot) { entry.assignment.exposed = true; } self.trigger_exposures(&exp_name, slot); } if value.is_some() { return value; } } suppressed_fallback.flatten() } pub fn variable_keys(&self) -> Result>, String> { self.check_ready(true)?; Ok(self.index_variables.clone()) } pub fn custom_field_value( &self, experiment_name: &str, field_name: &str, ) -> Result, String> { self.check_ready(true)?; Ok(self.custom_field_value_unchecked(experiment_name, field_name)) } fn custom_field_value_unchecked( &self, experiment_name: &str, field_name: &str, ) -> Option { if let Some(exp) = self.index.get(experiment_name) { if let Some(ref custom_fields) = exp.data.custom_field_values { if let Some(field) = custom_fields.iter().find(|f| f.name == field_name) { return match field.field_type.as_str() { "text" | "string" => Some(Value::String(field.value.clone())), "number" => { match field.value.parse::() { Ok(n) => serde_json::Number::from_f64(n) .map(Value::Number) .or_else(|| { warn!( "Custom field '{}' number out of range: {}", field_name, n ); None }), Err(e) => { error!("Failed to parse custom field '{}' as number: {}. Value: '{}'", field_name, e, field.value); None } } } "json" => { if field.value == "null" { Some(Value::Null) } else if field.value.is_empty() { Some(Value::String(String::new())) } else { match serde_json::from_str(&field.value) { Ok(v) => Some(v), Err(e) => { error!("Failed to parse custom field '{}' JSON: {}. Value: '{}'", field_name, e, field.value); None } } } } "boolean" => Some(Value::Bool(field.value == "true")), _ => { warn!( "Unknown custom field type '{}' for field '{}'", field.field_type, field_name ); None } }; } } } None } pub fn custom_field_value_type( &self, experiment_name: &str, field_name: &str, ) -> Result, String> { self.check_ready(true)?; if let Some(exp) = self.index.get(experiment_name) { if let Some(ref custom_fields) = exp.data.custom_field_values { if let Some(field) = custom_fields.iter().find(|f| f.name == field_name) { return Ok(Some(field.field_type.clone())); } } } Ok(None) } pub fn custom_field_keys(&self) -> Result, String> { self.check_ready(true)?; let mut keys = std::collections::HashSet::new(); for exp in &self.data.experiments { if let Some(ref custom_fields) = exp.custom_field_values { for field in custom_fields { keys.insert(field.name.clone()); } } } Ok(keys.into_iter().collect()) } /// The names of the experiments in the context data. `Err` before the /// context is ready; still available once finalized. pub fn experiments(&self) -> Result, String> { self.check_ready(false)?; Ok(self .data .experiments .iter() .map(|e| e.name.clone()) .collect()) } pub fn set_data_fetcher(&mut self, fetcher: DataFetcher) { self.data_fetcher = Some(fetcher); } /// Fetch fresh data with the configured data fetcher and apply it. /// /// `Err` before the context is ready and once it is finalizing or /// finalized. On a failed context (data never loaded) this is a no-op that /// returns `Ok`, as in the JS SDK: a refresh does not recover it. A fetch /// failure is returned and also emitted as an `error` event. pub fn refresh(&mut self) -> Result<(), String> { self.check_ready(true)?; if self.failed { return Ok(()); } let Some(fetcher) = self.data_fetcher.as_ref() else { let e = "Cannot refresh: no data fetcher configured".to_string(); self.log_error(&e); return Err(e); }; match fetcher() { Ok(new_data) => { self.apply_refresh(new_data); Ok(()) } Err(e) => { self.log_error(&e); Err(e) } } } /// Apply `new_data` as a refresh, keeping assignments that are still /// valid. Returns `()` as in 0.1; if the context is not ready or is /// finalizing/finalized the data is left unchanged and an `error` event is /// emitted. Use [`Context::try_refresh_with`] to get the error back. pub fn refresh_with(&mut self, new_data: ContextData) { if let Err(e) = self.try_refresh_with(new_data) { self.log_error(&e); } } /// Like [`Context::refresh_with`] but returns the guard error. Same /// guards as [`Context::refresh`]: `Err` before ready and once finalizing /// or finalized, and a no-op `Ok` on a failed context. pub fn try_refresh_with(&mut self, new_data: ContextData) -> Result<(), String> { self.check_ready(true)?; if !self.failed { self.apply_refresh(new_data); } Ok(()) } fn apply_refresh(&mut self, new_data: ContextData) { self.init(new_data); let to_remove: Vec = { let assignments = &self.assignments; let index = &self.index; assignments .iter() .filter_map(|(name, cached)| { let assignment = &cached.assignment; if assignment.overridden { return None; } let should_remove = match index.get(name) { None => assignment.assigned || cached.rule.rule_override, Some(exp) => !Self::experiment_matches(&exp.data, assignment), }; if should_remove { Some(name.clone()) } else { None } }) .collect() }; for name in to_remove { self.assignments.remove(&name); } // Holdout coverage can only change here, so it is compared once per // refresh and the result is checked on the next read instead of the // pinned holdouts being compared on every read. for (name, cached) in self.assignments.iter_mut() { if let Some(exp) = self.index.get(name) { if !(cached.holdouts.is_empty() && exp.holdouts.is_empty()) { cached.coverage_stale.store(true, Ordering::Relaxed); } } } match serde_json::to_value(&self.data) { Ok(value) => self.log_event("refresh", Some(value)), Err(e) => { error!("Failed to serialize context data for refresh: {}", e); } } } /// Send the pending exposures and goals to the publisher. /// /// `Err` before the context is ready and once it is finalizing or /// finalized. On success the queue is cleared and `publish` is emitted. If /// the publisher returns `Err`, the events stay pending (so a later /// `publish()` retries them), `error` is emitted, and the error is returned. /// /// A failed context (data never loaded) does not publish: its queue is /// discarded with an `error` event, as in the JS SDK. pub fn publish(&mut self) -> Result<(), String> { self.check_ready(true)?; self.flush() } fn flush(&mut self) -> Result<(), String> { if self.pending == 0 { return Ok(()); } if self.failed { let message = format!( "Discarding {} exposures and {} goals because context failed to initialize", self.exposures.len(), self.goals.len() ); warn!("{}", message); self.pending = 0; self.exposures.clear(); self.goals.clear(); self.log_error(&message); return Ok(()); } let params = self.build_publish_params(); if let Some(publisher) = &self.publisher { if let Err(e) = publisher.publish(¶ms) { self.log_error(&e); return Err(e); } } self.pending = 0; self.exposures.clear(); self.goals.clear(); match serde_json::to_value(¶ms) { Ok(value) => self.log_event("publish", Some(value)), Err(e) => { error!("Failed to serialize publish params: {}", e); } } Ok(()) } pub fn get_publish_params(&mut self) -> PublishParams { self.build_publish_params() } /// Publish any pending events, then seal the context and emit `finalize`. /// /// If that publish fails, the context is not finalized: it returns to its /// previous state with the events still pending, and the error is returned /// so the caller can retry. pub fn finalize(&mut self) -> Result<(), String> { if self.is_finalized() { return Ok(()); } let previous_state = self.state; self.state = ContextState::Finalizing; if let Err(e) = self.flush() { self.state = previous_state; return Err(e); } self.state = ContextState::Finalized; self.log_event("finalize", None); Ok(()) } pub fn close(&mut self) -> Result<(), String> { self.finalize() } /// Make the cached assignment for `experiment_name` current, rebuilding /// it when stale, and return it with where it is cached, so a cache hit /// copies nothing. A switchback reads the clock into `now` unless the /// caller already did; unit experiments never read it here. fn assign( &mut self, experiment_name: &str, now: &mut Option, ) -> (&CachedAssignment, Slot) { #[cfg(test)] { self.generic_assigns += 1; } let slot = if self.has_switchbacks && { #[cfg(test)] { self.switchback_lookups += 1; } self.index .get(experiment_name) .is_some_and(|exp| exp.switchback.is_some()) } { let now = *now.get_or_insert_with(|| self.now()); self.refresh_switchback(experiment_name, now) } else { self.refresh_assignment(experiment_name); Slot::Unit }; (self.cached(experiment_name, slot), slot) } /// [`Context::assign`] for a payload without switchbacks: the same /// single call into `refresh_assignment` the unit path always made, so /// it inlines as it did before switchbacks. #[inline(always)] fn assign_unit(&mut self, experiment_name: &str) -> &CachedAssignment { self.refresh_assignment(experiment_name); &self.assignments[experiment_name] } fn cached(&self, experiment_name: &str, slot: Slot) -> &CachedAssignment { match slot { Slot::Unit => &self.assignments[experiment_name], Slot::Switchback { period, .. } => self.switchbacks[experiment_name] .slot(period) .expect("refresh_switchback installs the slot"), } } fn cached_mut(&mut self, experiment_name: &str, slot: Slot) -> Option<&mut CachedAssignment> { match slot { Slot::Unit => self.assignments.get_mut(experiment_name), Slot::Switchback { period, .. } => self .switchbacks .get_mut(experiment_name) .and_then(|epoch| epoch.slot_mut(period)), } } fn refresh_assignment(&mut self, experiment_name: &str) { let has_custom = self.cassignments.contains_key(experiment_name); let has_override = self.overrides.contains_key(experiment_name); let has_experiment = self.index.contains_key(experiment_name); // A refresh only flags a coverage change; it is compared here, on // the next read, so a change a later refresh undoes is no change. if let Some(cached) = self.assignments.get(experiment_name).filter(|cached| { !cached.coverage_stale.load(Ordering::Relaxed) || self.coverage_still_matches(experiment_name, cached) }) { let a = &cached.assignment; if has_override { if a.overridden && a.variant == self.overrides[experiment_name] { return; } } else if !has_experiment { // A rule-won assignment is not "assigned" either, but its // variant came from the experiment's rules, so it is rebuilt // once the experiment is gone. if !a.assigned && !cached.rule.rule_override { return; } } else if cached.rule.rule_override || !has_custom || self.cassignments[experiment_name] == a.variant // A suppressed assignment is control whatever the custom // assignment says, so a custom mismatch is not staleness. || cached.suppressed { let exp = &self.index[experiment_name]; if Self::experiment_matches(&exp.data, a) && self.audience_matches(&exp.data, cached) { let (rules_rev, variant_count) = (exp.rules_rev, exp.data.variants.len()); if cached.rule.rules_rev == rules_rev && cached.rule.variant_count == variant_count && self.attrs_seq <= a.attrs_seq { return; } if self.rule_still_resolves(experiment_name) { let attrs_seq = self.attrs_seq; let entry = self.assignments.get_mut(experiment_name).unwrap(); entry.rule.rules_rev = rules_rev; entry.rule.variant_count = variant_count; entry.assignment.attrs_seq = attrs_seq; return; } } } } self.rebuild_assignment(experiment_name, has_custom, has_override); } /// Build the unit assignment `refresh_assignment` found stale or missing. #[inline(never)] fn rebuild_assignment(&mut self, experiment_name: &str, has_custom: bool, has_override: bool) { // Slots of a switchback that is now a unit experiment, or gone. if !self.switchbacks.is_empty() { self.switchbacks.remove(experiment_name); } let exp_data_opt = self.index.get(experiment_name).map(|e| e.data.clone()); let mut assignment = Assignment { eligible: true, ..Default::default() }; let mut rule_state = RuleState::default(); let holdouts = self.resolve_holdouts(experiment_name); let suppressed = exp_data_opt.as_ref().is_some_and(|exp| { holdouts.iter().any(|h| { h.assignment .as_ref() .is_some_and(|a| is_held_out_by(a.variant, a.arm_count, exp.full_on_variant)) }) }); if has_override { if let Some(ref exp_data) = exp_data_opt { assignment.id = exp_data.id; assignment.unit_type = exp_data.unit_type.clone(); } assignment.overridden = true; assignment.variant = self.overrides[experiment_name]; } else if let Some(ref exp_data) = exp_data_opt { // A matching rule is an explicit, author-specified assignment, like // an override: it wins over holdout suppression and the audience, // traffic and custom assignments. The rules are recorded for every // assignment so a refresh that changes them can be detected. rule_state.rules_rev = self.index[experiment_name].rules_rev; rule_state.variant_count = exp_data.variants.len(); let rule = self.resolve_rule(experiment_name, None, false); if rule.is_none() && !suppressed && !exp_data.audience.is_empty() { let attrs = self.evaluation_attributes(); let result = self.audience_matcher.evaluate(&exp_data.audience, &attrs); if let Some(matched) = result { assignment.audience_mismatch = !matched; } } if let Some((variant, missing_unit)) = rule { rule_state.rule_override = true; rule_state.missing_unit = missing_unit; assignment.variant = variant; } else if suppressed || (exp_data.audience_strict && assignment.audience_mismatch) { assignment.variant = 0; } else if exp_data.full_on_variant == 0 { if let Some(ref unit_type) = exp_data.unit_type { if self.units.contains_key(unit_type) { let unit_hash = self.unit_hash(unit_type); if let Some(ref hash) = unit_hash { let assigner = self .assigners .entry(unit_type.clone()) .or_insert_with(|| VariantAssigner::new(hash)); let eligible = assigner.assign( &exp_data.traffic_split, exp_data.traffic_seed_hi, exp_data.traffic_seed_lo, ) == 1; assignment.assigned = true; assignment.eligible = eligible; if eligible { if has_custom { assignment.variant = self.cassignments[experiment_name]; assignment.custom = true; } else { assignment.variant = assigner.assign( &exp_data.split, exp_data.seed_hi, exp_data.seed_lo, ) as i32; } } else { assignment.variant = 0; } } } } } else { assignment.assigned = true; assignment.eligible = true; assignment.variant = exp_data.full_on_variant as i32; assignment.full_on = true; } assignment.unit_type = exp_data.unit_type.clone(); assignment.id = exp_data.id; assignment.iteration = exp_data.iteration; assignment.traffic_split = Some(exp_data.traffic_split.clone()); assignment.full_on_variant = exp_data.full_on_variant; assignment.attrs_seq = self.attrs_seq; } if let Some(exp) = self.index.get(experiment_name) { if (assignment.variant as usize) < exp.variables.len() { assignment.variables = Some(exp.variables[assignment.variant as usize].clone()); } } // An already-exposed rule win with the same variant is kept, with only // its rule bookkeeping refreshed: a new assignment would publish the // same decision a second time. if let Some(previous) = self.assignments.get_mut(experiment_name) { let same_run = exp_data_opt .as_ref() .is_some_and(|exp| Self::experiment_matches(exp, &previous.assignment)); // The read that got here settled the coverage flag. if same_run && !previous.coverage_stale.load(Ordering::Relaxed) && previous.assignment.exposed && previous.rule.rule_override && rule_state.rule_override && !previous.assignment.overridden && previous.assignment.variant == assignment.variant { previous.rule = rule_state; previous.assignment.attrs_seq = assignment.attrs_seq; return; } } self.assignments.insert( experiment_name.to_string(), CachedAssignment { assignment, rule: rule_state, suppressed, holdouts, coverage_stale: AtomicBool::new(false), switchback: None, }, ); } /// Make the switchback slot for the period `now` falls in current, /// rebuilding it when stale, as java-sdk's `getAssignment` does for a /// switchback. The caller checked that `experiment_name` is one. fn refresh_switchback(&mut self, experiment_name: &str, now: i64) -> Slot { let exp = &self.index[experiment_name]; let def = exp.switchback.as_ref().expect("a switchback experiment"); let period = switchback_period(def, &exp.data, now); let generation = def.generation; let slot = Slot::Switchback { period, now }; // A new identity starts a new epoch: every slot of the old one goes, // the pre-start slot included. if self .switchbacks .get(experiment_name) .is_some_and(|epoch| epoch.generation != generation) { self.switchbacks.remove(experiment_name); } if self.switchback_slot_is_current(experiment_name, period) { return slot; } self.rebuild_switchback(experiment_name, period, now); slot } fn switchback_slot_is_current(&mut self, experiment_name: &str, period: Option) -> bool { let Some(cached) = self .switchbacks .get(experiment_name) .and_then(|epoch| epoch.slot(period)) else { return false; }; let (Some(state), a) = (&cached.switchback, &cached.assignment) else { return false; }; if let Some(&variant) = self.overrides.get(experiment_name) { return a.overridden && a.variant == variant; } if a.overridden || !(cached.rule.rule_override || self.cassignments.get(experiment_name).copied() == state.custom) { return false; } let exp = &self.index[experiment_name]; let data = &exp.data; let same_run = data.id == a.id && data.iteration == a.iteration && data.full_on_variant == a.full_on_variant && a.traffic_split .as_ref() .is_some_and(|ts| array_equals_shallow(&data.traffic_split, ts)); if !same_run || data.audience_strict != state.audience_strict || data.audience != state.audience || !self.audience_matches(data, cached) { return false; } let (rules_rev, variant_count) = (exp.rules_rev, data.variants.len()); if cached.rule.rules_rev == rules_rev && cached.rule.variant_count == variant_count && self.attrs_seq <= a.attrs_seq { return true; } // Rules are consulted only for an eligible cluster. let rule_holds = match &state.serving { Serving::Eligible { unit_type, .. } => { let expected = cached .rule .rule_override .then_some((a.variant, cached.rule.missing_unit)); let unit_type = unit_type.clone(); self.resolve_rule(experiment_name, Some(&unit_type), false) == expected } _ => true, }; if rule_holds { let attrs_seq = self.attrs_seq; let entry = self .switchbacks .get_mut(experiment_name) .and_then(|e| e.slot_mut(period)); let entry = entry.expect("the slot was found above"); entry.rule.rules_rev = rules_rev; entry.rule.variant_count = variant_count; entry.assignment.attrs_seq = attrs_seq; } rule_holds } /// The Collector's switchback branch (R33). Holdouts never apply. Only /// the schedule draw needs a period: before the anchor or with an /// unservable schedule it is a silent control, while rules, the audience /// and full-on still apply. fn rebuild_switchback(&mut self, experiment_name: &str, period: Option, now: i64) { // A unit assignment from before the experiment became a switchback: // java-sdk drops it with the epoch, so a return to the same unit // experiment is exposed again. if !self.assignments.is_empty() { self.assignments.remove(experiment_name); } let exp = &self.index[experiment_name]; let def = exp.switchback.as_ref().expect("a switchback experiment"); let data = exp.data.clone(); let (generation, seed) = (def.generation, def.seed); let rules_rev = exp.rules_rev; // Read once per slot: a rebuild in the same period keeps the serving // unit and cluster key, unless no linked unit was set then. let previous = self .switchbacks .get(experiment_name) .and_then(|epoch| epoch.slot(period)); let (serving, resolved_at) = match previous.and_then(|c| c.switchback.as_ref()) { Some(state) if state.serving != Serving::Absent => { (state.serving.clone(), state.resolved_at) } _ => (self.resolve_switchback_serving(experiment_name, now), now), }; let fallback_unit = match &def.sources { Some(sources) => Some(sources[0].0.clone()), None => data.unit_type.clone(), }; let custom = self.cassignments.get(experiment_name).copied(); let mut assignment = Assignment { eligible: true, ..Default::default() }; let mut rule_state = RuleState::default(); let mut silent = false; if let Some(&variant) = self.overrides.get(experiment_name) { assignment.id = data.id; assignment.unit_type = match &serving { Serving::Eligible { unit_type, .. } | Serving::Ineligible { unit_type } => { Some(unit_type.clone()) } Serving::Absent => fallback_unit, }; assignment.overridden = true; assignment.variant = variant; } else { rule_state.rules_rev = rules_rev; rule_state.variant_count = data.variants.len(); assignment.id = data.id; assignment.iteration = data.iteration; assignment.traffic_split = Some(data.traffic_split.clone()); assignment.full_on_variant = data.full_on_variant; assignment.attrs_seq = self.attrs_seq; match &serving { Serving::Absent => { // No linked unit: the experiment does not apply. assignment.unit_type = fallback_unit; assignment.eligible = false; silent = true; } Serving::Ineligible { unit_type } => { assignment.unit_type = Some(unit_type.clone()); assignment.assigned = true; assignment.eligible = false; silent = true; } Serving::Eligible { unit_type, cluster_key, } => { assignment.unit_type = Some(unit_type.clone()); assignment.assigned = true; if let Some((variant, missing_unit)) = self.resolve_rule(experiment_name, Some(unit_type), false) { // Recorded like a unit experiment's rule win. assignment.assigned = false; rule_state.rule_override = true; rule_state.missing_unit = missing_unit; assignment.variant = variant; } else { if !data.audience.is_empty() { let attrs = self.evaluation_attributes(); if let Some(matched) = self.audience_matcher.evaluate(&data.audience, &attrs) { assignment.audience_mismatch = !matched; } } if data.audience_strict && assignment.audience_mismatch { // Recorded like a unit experiment's strict mismatch. assignment.assigned = false; } else if data.full_on_variant != 0 { assignment.variant = data.full_on_variant as i32; assignment.full_on = true; } else if let Some(period) = period { let eligible = self.unit_hash(unit_type).is_some_and(|hash| { self.assigners .entry(unit_type.clone()) .or_insert_with(|| VariantAssigner::new(&hash)) .assign( &data.traffic_split, data.traffic_seed_hi, data.traffic_seed_lo, ) == 1 }); if !eligible { // A traffic miss on an eligible cluster is recorded. assignment.eligible = false; } else if let Some(custom) = custom { assignment.variant = custom; assignment.custom = true; } else { assignment.variant = switchback::assign_switchback( cluster_key, period, seed, data.split.len(), ) .map_or(0, |v| v as i32); } } else { // Before the anchor or an unservable schedule: no // draw, so a control that records nothing. assignment.eligible = false; silent = true; } } } } } let exp = &self.index[experiment_name]; if (assignment.variant as usize) < exp.variables.len() { assignment.variables = Some(exp.variables[assignment.variant as usize].clone()); } let epoch = self .switchbacks .entry(experiment_name.to_string()) .or_insert_with(|| SwitchbackEpoch { generation, ..Default::default() }); epoch.install( period, CachedAssignment { assignment, rule: rule_state, switchback: Some(Box::new(SwitchbackSlot { serving, resolved_at, audience: data.audience.clone(), audience_strict: data.audience_strict, silent, custom, })), ..Default::default() }, ); } /// The Collector's `switchbackServing` at `now`, for every context unit /// of a linked type. A missing, empty or unreadable cluster map takes the /// legacy path. fn resolve_switchback_serving(&self, experiment_name: &str, now: i64) -> Serving { let exp = &self.index[experiment_name]; let def = exp.switchback.as_ref().expect("a switchback experiment"); let has_unit = |unit_type: &str| self.units.contains_key(unit_type); let Some(sources) = &def.sources else { return switchback::legacy_serving(exp.data.unit_type.as_deref(), has_unit); }; switchback::resolve_serving( sources, exp.data.switchback_cluster_values.as_deref(), has_unit, |path| { let attribute = self.latest_attribute(path.root_key(), now)?; switchback::cluster_key(path.resolve_attribute(&attribute.value)?) }, ) } /// When a switchback slot's exposure is stamped: the exposing call's /// reading `now`, unless the attributes at `now` no longer give the /// slot's serving unit and cluster key (an attribute changed since the /// slot was resolved in this period). Then it is the reading the serving /// was resolved at, so the Collector, which resolves the cluster again at /// `exposedAt`, serves the same cluster the variant came from. fn switchback_exposed_at(&self, experiment_name: &str, slot: Slot, now: i64) -> i64 { let Slot::Switchback { period, .. } = slot else { return now; }; let Some(state) = self .switchbacks .get(experiment_name) .and_then(|epoch| epoch.slot(period)) .and_then(|cached| cached.switchback.as_ref()) else { return now; }; // Equal readings stamp the same either way; skip re-resolving. if state.resolved_at == now || !self.index.contains_key(experiment_name) || self.resolve_switchback_serving(experiment_name, now) == state.serving { now } else { state.resolved_at } } /// The attribute the Collector records with an exposure at `now`: among /// those set at or before `now` whose trimmed name matches, the latest /// `setAt`, and on a tie the one set last. fn latest_attribute(&self, name: &str, now: i64) -> Option<&Attribute> { self.attrs .iter() .filter(|a| a.set_at <= now && a.name.trim() == name) .max_by_key(|a| a.set_at) } /// Pin the unit's arm in each holdout covering `experiment_name`. The /// holdouts are drawn with the covered experiment's unit type; an entry /// is `None` while that unit is missing. fn resolve_holdouts(&mut self, experiment_name: &str) -> Vec { let Some(exp) = self.index.get(experiment_name) else { return Vec::new(); }; if exp.holdouts.is_empty() { return Vec::new(); } let holdouts = exp.holdouts.clone(); let unit_type = exp.data.unit_type.clone(); holdouts .into_iter() .map(|data| { let assignment = unit_type .as_deref() .and_then(|unit_type| self.holdout_assignment(&data, unit_type)); PinnedHoldout { data, assignment } }) .collect() } /// The unit's arm in `live` (the holdout as in the current data) for /// `unit_type`, drawn once per holdout iteration: a same-iteration seed or /// split edit keeps the arm, so an exposed unit never lands in a second /// arm. fn holdout_assignment( &mut self, live: &ExperimentData, unit_type: &str, ) -> Option> { let key = (live.id, unit_type.to_string()); if let Some(cached) = self.holdout_assignments.get(&key) { if cached.iteration == live.iteration { return Some(cached.clone()); } } let hash = self.unit_hash(unit_type)?; let assigner = self .assigners .entry(unit_type.to_string()) .or_insert_with(|| VariantAssigner::new(&hash)); let assignment = Arc::new(HoldoutAssignment { id: live.id, iteration: live.iteration, variant: assigner.assign(&live.split, live.seed_hi, live.seed_lo) as i32, arm_count: live.split.len(), unit_type: unit_type.to_string(), exposed: AtomicBool::new(false), }); self.holdout_assignments.insert(key, assignment.clone()); Some(assignment) } /// Settle a refresh's coverage flag: `true` (and the flag cleared) when /// the experiment is still covered by the same holdouts. #[cold] fn coverage_still_matches(&self, experiment_name: &str, cached: &CachedAssignment) -> bool { let matches = match self.index.get(experiment_name) { Some(exp) => Self::holdout_set_matches(&exp.holdouts, &cached.holdouts), None => true, }; cached.coverage_stale.store(!matches, Ordering::Relaxed); matches } /// Holdout coverage is compared by (id, iteration) only: a seed or split /// edit must not re-resolve, and so re-expose, the experiment. fn holdout_set_matches(fresh: &[Arc], pinned: &[PinnedHoldout]) -> bool { fresh.len() == pinned.len() && fresh .iter() .zip(pinned) .all(|(f, p)| f.id == p.data.id && f.iteration == p.data.iteration) } /// Queue the experiment's exposure (unless it is held out) and the /// exposures of the holdouts pinned in its assignment that this unit /// has not seen yet. As in the JS SDK, each exposure is queued before /// the event logger sees it. /// /// A switchback exposure is stamped with the clock reading that chose its /// period, so analysis places it in the period whose variant it served. /// A switchback the Collector records nothing for queues nothing. fn trigger_exposures(&mut self, experiment_name: &str, slot: Slot) { let now = match slot { Slot::Unit => self.now(), Slot::Switchback { now, .. } => self.switchback_exposed_at(experiment_name, slot, now), }; let cached = match slot { Slot::Unit => self.assignments.get(experiment_name), Slot::Switchback { period, .. } => self .switchbacks .get(experiment_name) .and_then(|epoch| epoch.slot(period)), }; let Some(cached) = cached else { return; }; let first = self.exposures.len(); let silent = cached.switchback.as_ref().is_some_and(|s| s.silent); if !silent && (!cached.suppressed || cached.assignment.overridden || cached.rule.rule_override) { let a = &cached.assignment; self.exposures.push(Exposure { id: a.id, name: experiment_name.to_string(), exposed_at: now, unit: a.unit_type.clone(), variant: a.variant, assigned: a.assigned, eligible: a.eligible, overridden: a.overridden, full_on: a.full_on, custom: a.custom, audience_mismatch: a.audience_mismatch, rule_override: cached.rule.rule_override, }); } for holdout in &cached.holdouts { let Some(h) = &holdout.assignment else { continue; }; if !h.exposed.swap(true, Ordering::Relaxed) { self.exposures.push(Exposure { id: h.id, name: holdout.data.name.clone(), exposed_at: now, unit: Some(h.unit_type.clone()), variant: h.variant, assigned: true, eligible: true, overridden: false, full_on: false, custom: false, audience_mismatch: false, rule_override: false, }); } } self.pending += self.exposures.len() - first; for i in first..self.exposures.len() { match serde_json::to_value(&self.exposures[i]) { Ok(value) => self.log_event("exposure", Some(value)), Err(e) => { error!( "Failed to serialize exposure for experiment '{}': {}", self.exposures[i].name, e ); } } } } /// Resolve the experiment's assignment rules to `(variant, missing_unit)`, /// or `None` when no rule applies. A fixed variant out of range, or a split /// whose length differs from the variant count, is ignored. A split with /// no unit to randomize over (or with `unit_pinned_missing`) gives the /// full-on variant and is flagged `missing_unit`. /// A switchback's split rule randomizes over its serving unit, given as /// `serving_unit`; every other experiment's over its `unitType`. fn resolve_rule( &mut self, experiment_name: &str, serving_unit: Option<&str>, unit_pinned_missing: bool, ) -> Option<(i32, bool)> { let exp = self.index.get(experiment_name)?; let rules = exp.rules.clone()?; let variant_count = exp.data.variants.len(); let unit_type = match serving_unit { Some(unit_type) => Some(unit_type.to_string()), None => exp.data.unit_type.clone(), }; let (full_on_variant, seed_hi, seed_lo) = (exp.data.full_on_variant, exp.data.seed_hi, exp.data.seed_lo); #[cfg(test)] { self.rule_evaluations += 1; } let attrs = self.evaluation_attributes(); let action = self.audience_matcher .evaluate_rules(&rules, self.system_environment(), &attrs)?; match action { RuleAction::Assign(variant) => { let in_bounds = usize::try_from(variant).is_ok_and(|v| v < variant_count); in_bounds.then_some((variant as i32, false)) } RuleAction::Split(split) => { if split.len() != variant_count { return None; } let hash = match unit_type { Some(ref unit_type) if !unit_pinned_missing => self.unit_hash(unit_type), _ => None, }; let (Some(unit_type), Some(hash)) = (unit_type, hash) else { return Some((full_on_variant as i32, true)); }; let assigner = self .assigners .entry(unit_type) .or_insert_with(|| VariantAssigner::new(&hash)); Some((assigner.assign(&split, seed_hi, seed_lo) as i32, false)) } } } /// Whether re-resolving the experiment's rules still gives the cached /// assignment's rule outcome. Called only when the rules, the variant /// count or the attributes changed since the assignment was built. fn rule_still_resolves(&mut self, experiment_name: &str) -> bool { let cached = &self.assignments[experiment_name]; // An exposed split resolved without its unit keeps resolving as if // the unit were still missing, so a unit set afterwards cannot // switch the variant the user already saw. let pinned = cached.rule.missing_unit && cached.assignment.exposed; let expected = cached .rule .rule_override .then_some((cached.assignment.variant, cached.rule.missing_unit)); self.resolve_rule(experiment_name, None, pinned) == expected } fn experiment_matches(experiment: &ExperimentData, assignment: &Assignment) -> bool { experiment.id == assignment.id && experiment.unit_type == assignment.unit_type && experiment.iteration == assignment.iteration && experiment.full_on_variant == assignment.full_on_variant && assignment .traffic_split .as_ref() .is_some_and(|ts| array_equals_shallow(&experiment.traffic_split, ts)) } fn audience_matches(&self, experiment: &ExperimentData, cached: &CachedAssignment) -> bool { // A held-out unit never had its audience evaluated. if cached.rule.rule_override || cached.suppressed { return true; } let assignment = &cached.assignment; if !experiment.audience.is_empty() && self.attrs_seq > assignment.attrs_seq { let attrs = self.evaluation_attributes(); let result = self.audience_matcher.evaluate(&experiment.audience, &attrs); if let Some(matched) = result { return matched != assignment.audience_mismatch; } } true } fn unit_hash(&mut self, unit_type: &str) -> Option { if let Some(hash) = self.hashes.get(unit_type) { return Some(hash.clone()); } if let Some(unit) = self.units.get(unit_type) { let hash = hash_unit_value(unit)?; self.hashes.insert(unit_type.to_string(), hash.clone()); return Some(hash); } None } fn build_publish_params(&mut self) -> PublishParams { let unit_types: Vec = self.units.keys().cloned().collect(); for unit_type in &unit_types { self.unit_hash(unit_type); } let units: Vec = self .units .keys() .map(|unit_type| Unit { unit_type: unit_type.clone(), uid: self.hashes.get(unit_type).cloned(), }) .collect(); PublishParams { published_at: self.now(), units, hashed: true, exposures: if self.exposures.is_empty() { None } else { Some(self.exposures.clone()) }, goals: if self.goals.is_empty() { None } else { Some(self.goals.clone()) }, attributes: if self.attrs.is_empty() { None } else { Some(self.attrs.clone()) }, } } fn check_not_finalized(&self) -> Result<(), String> { match self.state { ContextState::Finalized => Err("ABsmartly Context is finalized.".to_string()), ContextState::Finalizing => Err("ABsmartly Context is finalizing.".to_string()), _ => Ok(()), } } /// `Err("ABsmartly Context is not yet ready.")` while loading and, with /// `expect_not_finalized`, once finalizing or finalized. A failed context /// counts as ready (its data is empty), as in the JS SDK. fn check_ready(&self, expect_not_finalized: bool) -> Result<(), String> { if self.state == ContextState::Loading { return Err("ABsmartly Context is not yet ready.".to_string()); } if expect_not_finalized { self.check_not_finalized()?; } Ok(()) } fn log_event(&self, event_name: &str, data: Option) { if let Some(ref logger) = self.event_logger { logger(self, event_name, data); } } fn log_error(&self, message: &str) { self.log_event("error", Some(json!({ "message": message }))); } } fn now_millis() -> i64 { SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_millis() as i64) .unwrap_or_else(|e| { warn!("System time error, returning 0: {}", e); 0 }) } #[cfg(test)] #[path = "context_holdout_tests.rs"] mod holdout_tests; #[cfg(test)] #[path = "context_switchback_tests.rs"] mod switchback_tests; #[cfg(test)] mod tests { use super::*; use serde_json::json; fn make_experiment(name: &str, variants: Vec<&str>, split: Vec) -> ExperimentData { ExperimentData { id: 1, name: name.to_string(), unit_type: Some("session_id".to_string()), iteration: 1, seed_hi: 0, seed_lo: 0, split, traffic_seed_hi: 0, traffic_seed_lo: 0, traffic_split: vec![0.0, 1.0], full_on_variant: 0, audience: String::new(), audience_strict: false, variants: variants .iter() .map(|c| Variant { config: if c.is_empty() { None } else { Some(c.to_string()) }, }) .collect(), variables: HashMap::new(), custom_field_values: None, assignment_rules: String::new(), holdout_ids: vec![], holdout_type: None, ..Default::default() } } fn make_context_data(experiments: Vec) -> ContextData { ContextData { experiments, holdouts: vec![], } } #[test] fn test_context_is_ready_after_creation() { let data = make_context_data(vec![]); let context = Context::new(data); assert!(context.is_ready()); assert!(!context.is_failed()); assert!(!context.is_finalized()); } #[test] fn test_ready_error_is_none_when_ready() { let data = make_context_data(vec![]); let context = Context::new(data); assert!(context.ready_error().is_none()); } #[test] fn test_ready_error_is_none_after_become_failed_without_error() { let mut context = Context::new_loading(); context.become_failed(); assert!(context.is_failed()); assert!(context.ready_error().is_none()); } #[test] fn test_ready_error_returns_error_after_become_failed_with_error() { let mut context = Context::new_loading(); context.become_failed_with_error("data fetch failed".to_string()); assert!(context.is_failed()); assert_eq!( context.ready_error(), Some(&"data fetch failed".to_string()) ); } #[test] fn test_context_set_unit() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_unit("session_id", "user123").is_ok()); assert_eq!( context.get_unit("session_id"), Some(&serde_json::json!("user123")) ); } #[test] fn test_context_set_unit_cannot_change() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_unit("session_id", "user123").is_ok()); assert!(context.set_unit("session_id", "user456").is_err()); } #[test] fn test_context_set_unit_same_value_ok() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_unit("session_id", "user123").is_ok()); assert!(context.set_unit("session_id", "user123").is_ok()); } #[test] fn test_context_set_unit_blank_not_allowed() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_unit("session_id", "").is_err()); assert!(context.set_unit("session_id", " ").is_err()); } #[test] fn test_context_set_attribute() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_attribute("country", json!("US")).is_ok()); assert_eq!(context.get_attribute("country"), Some(&json!("US"))); } #[test] fn test_context_peek_returns_zero_for_nonexistent() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert_eq!(context.peek("nonexistent_experiment").unwrap(), 0); } #[test] fn test_context_treatment_returns_zero_for_nonexistent() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert_eq!(context.treatment("nonexistent_experiment").unwrap(), 0); } #[test] fn test_context_treatment_with_experiment() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); let variant = context.treatment("test_exp").unwrap(); assert!(variant == 0 || variant == 1); } #[test] fn test_context_peek_does_not_queue_exposure() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); context.peek("test_exp").unwrap(); assert_eq!(context.pending(), 0); } #[test] fn test_context_treatment_queues_exposure() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); context.treatment("test_exp").unwrap(); assert_eq!(context.pending(), 1); } #[test] fn test_context_treatment_only_queues_once() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); context.treatment("test_exp").unwrap(); context.treatment("test_exp").unwrap(); context.treatment("test_exp").unwrap(); assert_eq!(context.pending(), 1); } #[test] fn test_context_set_override() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_override("test_exp", 1).unwrap(); context.set_unit("session_id", "test_user").unwrap(); assert_eq!(context.treatment("test_exp").unwrap(), 1); } #[test] fn test_context_set_custom_assignment() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_custom_assignment("test_exp", 1).unwrap(); context.set_unit("session_id", "test_user").unwrap(); let variant = context.treatment("test_exp").unwrap(); assert_eq!(variant, 1); } #[test] fn test_context_track() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.track("purchase", json!({"amount": 99.99})).is_ok()); assert_eq!(context.pending(), 1); } #[test] fn test_context_track_without_properties() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.track("click", ()).is_ok()); assert_eq!(context.pending(), 1); } #[test] fn test_context_publish_clears_pending() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); context.treatment("test_exp").unwrap(); context.track("click", ()).unwrap(); assert_eq!(context.pending(), 2); context.publish().unwrap(); assert_eq!(context.pending(), 0); } #[test] fn test_context_finalize() { let data = make_context_data(vec![]); let mut context = Context::new(data); context.finalize().unwrap(); assert!(context.is_finalized()); } #[test] fn test_context_cannot_track_after_finalize() { let data = make_context_data(vec![]); let mut context = Context::new(data); context.finalize().unwrap(); assert!(context.track("click", ()).is_err()); } #[test] fn test_context_cannot_set_unit_after_finalize() { let data = make_context_data(vec![]); let mut context = Context::new(data); context.finalize().unwrap(); assert!(context.set_unit("session_id", "user123").is_err()); } #[test] fn test_context_cannot_set_attribute_after_finalize() { let data = make_context_data(vec![]); let mut context = Context::new(data); context.finalize().unwrap(); assert!(context.set_attribute("country", json!("US")).is_err()); } #[test] fn test_context_variable_value_returns_default_for_nonexistent() { let data = make_context_data(vec![]); let mut context = Context::new(data); let value = context .variable_value("nonexistent", json!("default")) .unwrap(); assert_eq!(value, json!("default")); } #[test] fn test_context_peek_variable_value_returns_default_for_nonexistent() { let data = make_context_data(vec![]); let mut context = Context::new(data); let value = context .peek_variable_value("nonexistent", json!(42)) .unwrap(); assert_eq!(value, json!(42)); } #[test] fn test_context_experiments_list() { let exp1 = make_experiment("exp1", vec!["{}"], vec![1.0]); let exp2 = make_experiment("exp2", vec!["{}"], vec![1.0]); let data = make_context_data(vec![exp1, exp2]); let context = Context::new(data); let experiments = context.experiments().unwrap(); assert!(experiments.contains(&"exp1".to_string())); assert!(experiments.contains(&"exp2".to_string())); } #[test] fn test_context_full_on_variant() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.full_on_variant = 1; let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "any_user").unwrap(); assert_eq!(context.treatment("test_exp").unwrap(), 1); } #[test] fn test_context_audience_mismatch_strict() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); context.set_attribute("country", json!("UK")).unwrap(); assert_eq!(context.treatment("test_exp").unwrap(), 0); } #[test] fn test_context_audience_match() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); let mut context = Context::new(data); context.set_unit("session_id", "test_user").unwrap(); context.set_attribute("country", json!("US")).unwrap(); assert_eq!(context.treatment("test_exp").unwrap(), 1); } #[test] fn test_context_refresh() { let exp1 = make_experiment("exp1", vec!["{}"], vec![1.0]); let data1 = make_context_data(vec![exp1]); let mut context = Context::new(data1); let exp2 = make_experiment("exp2", vec!["{}"], vec![1.0]); let data2 = make_context_data(vec![exp2]); context.set_data_fetcher(Box::new(move || Ok(data2.clone()))); context.refresh().unwrap(); let experiments = context.experiments().unwrap(); assert!(experiments.contains(&"exp2".to_string())); assert!(!experiments.contains(&"exp1".to_string())); } #[test] fn test_ergonomic_set_attribute_with_string() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_attribute("country", "US").is_ok()); assert_eq!(context.get_attribute("country"), Some(&json!("US"))); } #[test] fn test_ergonomic_set_attribute_with_number() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_attribute("age", 25).is_ok()); assert_eq!(context.get_attribute("age"), Some(&json!(25))); } #[test] fn test_ergonomic_set_attribute_with_bool() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.set_attribute("premium", true).is_ok()); assert_eq!(context.get_attribute("premium"), Some(&json!(true))); } #[test] fn test_set_attributes_with_array() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context .set_attributes([ ("country", json!("US")), ("age", json!(25)), ("premium", json!(true)), ]) .is_ok()); assert_eq!(context.get_attribute("country"), Some(&json!("US"))); assert_eq!(context.get_attribute("age"), Some(&json!(25))); assert_eq!(context.get_attribute("premium"), Some(&json!(true))); } #[test] fn test_set_attributes_with_hashmap() { let data = make_context_data(vec![]); let mut context = Context::new(data); let attrs = std::collections::HashMap::from([ ("country".to_string(), json!("UK")), ("tier".to_string(), json!("gold")), ]); assert!(context.set_attributes(attrs).is_ok()); assert_eq!(context.get_attribute("country"), Some(&json!("UK"))); assert_eq!(context.get_attribute("tier"), Some(&json!("gold"))); } #[test] fn test_ergonomic_variable_value_with_string_default() { let data = make_context_data(vec![]); let mut context = Context::new(data); let value = context .variable_value("nonexistent", "default_value") .unwrap(); assert_eq!(value, json!("default_value")); } #[test] fn test_ergonomic_variable_value_with_number_default() { let data = make_context_data(vec![]); let mut context = Context::new(data); let value = context.variable_value("nonexistent", 42).unwrap(); assert_eq!(value, json!(42)); } #[test] fn test_ergonomic_peek_variable_value_with_bool_default() { let data = make_context_data(vec![]); let mut context = Context::new(data); let value = context.peek_variable_value("nonexistent", false).unwrap(); assert_eq!(value, json!(false)); } #[test] fn test_ergonomic_track_with_json_properties() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context .track( "purchase", json!({ "item_count": 1, "total_amount": 99.99 }) ) .is_ok()); assert_eq!(context.pending(), 1); } #[test] fn test_ergonomic_track_with_unit_no_properties() { let data = make_context_data(vec![]); let mut context = Context::new(data); assert!(context.track("click", ()).is_ok()); assert_eq!(context.pending(), 1); } fn make_experiment_with_id( name: &str, id: i64, variants: Vec<&str>, split: Vec, ) -> ExperimentData { ExperimentData { id, name: name.to_string(), unit_type: Some("session_id".to_string()), iteration: 1, seed_hi: 0, seed_lo: 0, split, traffic_seed_hi: 0, traffic_seed_lo: 0, traffic_split: vec![0.0, 1.0], full_on_variant: 0, audience: String::new(), audience_strict: false, variants: variants .iter() .map(|c| Variant { config: if c.is_empty() { None } else { Some(c.to_string()) }, }) .collect(), variables: HashMap::new(), custom_field_values: None, assignment_rules: String::new(), holdout_ids: vec![], holdout_type: None, ..Default::default() } } use std::sync::{Arc, Mutex}; #[derive(Clone, Debug)] struct LogEntry { event: String, data: Option, } fn make_logging_context(data: ContextData) -> (Context, Arc>>) { let log = Arc::new(Mutex::new(Vec::new())); let log_clone = log.clone(); let mut ctx = Context::new(data); ctx.set_event_logger(Box::new(move |_ctx, event, data| { log_clone.lock().unwrap().push(LogEntry { event: event.to_string(), data, }); })); (ctx, log) } #[test] fn test_refresh_without_fetcher_emits_error_event() { let (mut ctx, log) = make_logging_context(make_context_data(vec![])); assert!(ctx.refresh().is_err()); assert!(log.lock().unwrap().iter().any(|e| e.event == "error")); } #[test] fn test_refresh_with_returns_unit_and_logs_error_when_finalized() { let mut ctx = Context::new(make_context_data(vec![])); let log = Arc::new(Mutex::new(Vec::new())); let sink = log.clone(); ctx.set_event_logger(Box::new(move |_, event, _| { sink.lock().unwrap().push(event.to_string()); })); ctx.finalize().unwrap(); let unit: () = ctx.refresh_with(make_context_data(vec![variable_experiment()])); assert_eq!(unit, ()); assert!(ctx.experiments().unwrap().is_empty()); assert!(log.lock().unwrap().iter().any(|e| e == "error")); } #[test] fn test_refresh_after_finalize_is_rejected() { let mut ctx = Context::new(make_context_data(vec![])); ctx.finalize().unwrap(); assert_eq!( ctx.try_refresh_with(make_context_data(vec![])), Err("ABsmartly Context is finalized.".to_string()) ); assert_eq!( ctx.refresh(), Err("ABsmartly Context is finalized.".to_string()) ); } const NOT_READY: &str = "ABsmartly Context is not yet ready."; const FINALIZED: &str = "ABsmartly Context is finalized."; fn variable_experiment() -> ExperimentData { let mut exp = make_experiment("exp", vec!["{}", r#"{"color":"red"}"#], vec![0.0, 1.0]); exp.custom_field_values = Some(vec![CustomFieldValue { name: "field".to_string(), value: "x".to_string(), field_type: "string".to_string(), }]); exp } /// Every read guarded like JS `_checkReady(true)`, as `(name, result)`. fn guarded_reads(ctx: &mut Context) -> Vec<(&'static str, Result<(), String>)> { vec![ ("peek", ctx.peek("exp").map(|_| ())), ("treatment", ctx.treatment("exp").map(|_| ())), ( "variable_value", ctx.variable_value("color", "x").map(|_| ()), ), ( "peek_variable_value", ctx.peek_variable_value("color", "x").map(|_| ()), ), ("variable_keys", ctx.variable_keys().map(|_| ())), ( "custom_field_value", ctx.custom_field_value("exp", "field").map(|_| ()), ), ( "custom_field_value_type", ctx.custom_field_value_type("exp", "field").map(|_| ()), ), ("custom_field_keys", ctx.custom_field_keys().map(|_| ())), ("publish", ctx.publish()), ("refresh", ctx.refresh()), ( "refresh_with", ctx.try_refresh_with(make_context_data(vec![variable_experiment()])), ), ] } #[test] fn test_reads_before_ready_return_not_ready_error() { let mut ctx = Context::new_loading(); ctx.set_unit("session_id", "u").unwrap(); for (name, result) in guarded_reads(&mut ctx) { assert_eq!(result, Err(NOT_READY.to_string()), "{}", name); } assert_eq!(ctx.experiments(), Err(NOT_READY.to_string())); assert!(ctx.data().is_err()); assert_eq!(ctx.pending(), 0, "no exposure queued while loading"); } #[test] fn test_reads_after_finalize_return_finalized_error() { let mut ctx = Context::new(make_context_data(vec![variable_experiment()])); ctx.set_unit("session_id", "u").unwrap(); ctx.finalize().unwrap(); for (name, result) in guarded_reads(&mut ctx) { assert_eq!(result, Err(FINALIZED.to_string()), "{}", name); } assert_eq!(ctx.pending(), 0, "no exposure queued after finalize"); // As in JS, experiments() and data() only require the context to be ready. assert_eq!(ctx.experiments(), Ok(vec!["exp".to_string()])); assert!(ctx.data().is_ok()); } #[test] fn test_reads_while_finalizing_return_finalizing_error() { struct OkPublisher; impl ContextPublisher for OkPublisher { fn publish(&self, _: &PublishParams) -> Result<(), String> { Ok(()) } } let seen = Arc::new(Mutex::new(Vec::new())); let seen_in_logger = seen.clone(); let mut ctx = Context::new(make_context_data(vec![variable_experiment()])); ctx.set_unit("session_id", "u").unwrap(); ctx.set_publisher(Box::new(OkPublisher)); ctx.treatment("exp").unwrap(); ctx.set_event_logger(Box::new(move |c, event, _| { if event == "publish" { seen_in_logger .lock() .unwrap() .push((c.is_finalizing(), c.variable_keys().err())); } })); ctx.finalize().unwrap(); assert_eq!( *seen.lock().unwrap(), vec![(true, Some("ABsmartly Context is finalizing.".to_string()))] ); } #[test] fn test_failed_context_reads_return_defaults() { let mut ctx = Context::new_loading(); ctx.become_failed_with_error("boom".to_string()); assert_eq!(ctx.treatment("exp"), Ok(0)); assert_eq!(ctx.peek("exp"), Ok(0)); assert_eq!(ctx.variable_value("color", "blue"), Ok(json!("blue"))); assert_eq!(ctx.variable_keys(), Ok(HashMap::new())); assert_eq!(ctx.experiments(), Ok(vec![])); } #[test] fn test_refresh_on_failed_context_is_noop_ok() { let (mut ctx, log) = { let log: Arc>> = Arc::new(Mutex::new(Vec::new())); let log_clone = log.clone(); let mut ctx = Context::new_loading(); ctx.set_event_logger(Box::new(move |_ctx, event, data| { log_clone.lock().unwrap().push(LogEntry { event: event.to_string(), data, }); })); (ctx, log) }; ctx.become_failed_with_error("boom".to_string()); let fetches = Arc::new(Mutex::new(0)); let fetches_in_fetcher = fetches.clone(); ctx.set_data_fetcher(Box::new(move || { *fetches_in_fetcher.lock().unwrap() += 1; Ok(make_context_data(vec![variable_experiment()])) })); log.lock().unwrap().clear(); assert_eq!(ctx.refresh(), Ok(())); assert_eq!( ctx.try_refresh_with(make_context_data(vec![variable_experiment()])), Ok(()) ); assert_eq!( *fetches.lock().unwrap(), 0, "a failed context does not fetch" ); assert!(ctx.is_failed()); assert_eq!( ctx.experiments(), Ok(vec![]), "a failed context is not recovered" ); assert!(log.lock().unwrap().is_empty(), "no refresh or error event"); } #[test] fn test_failed_context_publish_discards_with_error_event() { let log: Arc>> = Arc::new(Mutex::new(Vec::new())); let log_clone = log.clone(); let mut ctx = Context::new_loading(); ctx.set_event_logger(Box::new(move |_ctx, event, data| { log_clone.lock().unwrap().push(LogEntry { event: event.to_string(), data, }); })); ctx.become_failed(); ctx.track("goal", json!(null)).unwrap(); log.lock().unwrap().clear(); assert_eq!(ctx.publish(), Ok(())); assert_eq!(ctx.pending(), 0); let log = log.lock().unwrap(); assert_eq!(log.len(), 1); assert_eq!(log[0].event, "error"); assert_eq!( log[0].data, Some( json!({"message": "Discarding 0 exposures and 1 goals because context failed to initialize"}) ) ); } #[test] fn test_failed_context_stays_failed_after_finalize() { let mut ctx = Context::new_loading(); ctx.become_failed(); ctx.finalize().unwrap(); assert!(ctx.is_finalized()); assert!(ctx.is_failed()); } #[test] fn test_event_logger_on_exposure() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); let entries = log.lock().unwrap(); assert!(entries.iter().any(|e| e.event == "exposure")); } #[test] fn test_event_logger_on_goal() { let data = make_context_data(vec![]); let (mut ctx, log) = make_logging_context(data); ctx.track("purchase", json!({"amount": 9.99})).unwrap(); let entries = log.lock().unwrap(); assert!(entries.iter().any(|e| e.event == "goal")); } #[test] fn test_event_logger_on_publish() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); ctx.publish().unwrap(); let entries = log.lock().unwrap(); assert!(entries.iter().any(|e| e.event == "publish")); } #[test] fn test_event_logger_on_finalize() { let data = make_context_data(vec![]); let (mut ctx, log) = make_logging_context(data); ctx.finalize().unwrap(); let entries = log.lock().unwrap(); assert!(entries.iter().any(|e| e.event == "finalize")); } #[test] fn test_event_logger_on_refresh() { let exp = make_experiment("exp1", vec!["{}"], vec![1.0]); let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); let exp2 = make_experiment("exp2", vec!["{}"], vec![1.0]); let data2 = make_context_data(vec![exp2]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); let entries = log.lock().unwrap(); assert!(entries.iter().any(|e| e.event == "refresh")); } #[test] fn test_loading_state_become_ready() { let mut ctx = Context::new_loading(); assert!(!ctx.is_ready()); assert!(!ctx.is_failed()); let data = make_context_data(vec![]); ctx.become_ready(data); assert!(ctx.is_ready()); } #[test] fn test_loading_state_become_failed() { let mut ctx = Context::new_loading(); assert!(!ctx.is_ready()); ctx.become_failed(); assert!(ctx.is_failed()); // A failed context is ready with empty data so reads return defaults. assert!(ctx.is_ready()); assert!(ctx.data().unwrap().experiments.is_empty()); assert_eq!(ctx.treatment("exp1").unwrap(), 0); assert_eq!( ctx.variable_value("button", json!("default")).unwrap(), json!("default") ); } #[test] fn test_become_ready_only_once() { let mut ctx = Context::new_loading(); let data1 = make_context_data(vec![]); ctx.become_ready(data1); assert!(ctx.is_ready()); let exp = make_experiment("exp1", vec!["{}"], vec![1.0]); let data2 = make_context_data(vec![exp]); ctx.become_ready(data2); assert!(ctx.experiments().unwrap().is_empty()); } #[test] fn test_become_failed_only_from_loading() { let mut ctx = Context::new_loading(); let data = make_context_data(vec![]); ctx.become_ready(data); assert!(ctx.is_ready()); ctx.become_failed(); assert!(!ctx.is_failed()); assert!(ctx.is_ready()); } #[test] fn test_set_unit_callable_before_ready() { let mut ctx = Context::new_loading(); assert!(ctx.set_unit("session_id", "user123").is_ok()); assert_eq!( ctx.get_unit("session_id"), Some(&serde_json::json!("user123")) ); } #[test] fn test_set_attribute_callable_before_ready() { let mut ctx = Context::new_loading(); assert!(ctx.set_attribute("country", json!("US")).is_ok()); assert_eq!(ctx.get_attribute("country"), Some(&json!("US"))); } #[test] fn test_set_override_callable_before_ready() { let mut ctx = Context::new_loading(); ctx.set_override("exp1", 1).unwrap(); let exp = make_experiment("exp1", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); let data = make_context_data(vec![exp]); ctx.become_ready(data); ctx.set_unit("session_id", "test_user").unwrap(); assert_eq!(ctx.treatment("exp1").unwrap(), 1); } #[test] fn test_set_custom_assignment_callable_before_ready() { let mut ctx = Context::new_loading(); ctx.set_custom_assignment("exp1", 1).unwrap(); let exp = make_experiment("exp1", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); let data = make_context_data(vec![exp]); ctx.become_ready(data); ctx.set_unit("session_id", "test_user").unwrap(); assert_eq!(ctx.treatment("exp1").unwrap(), 1); } #[test] fn test_set_custom_assignment_throws_after_finalize() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); assert!(ctx.set_custom_assignment("exp1", 1).is_err()); } #[test] fn test_treatment_queues_exposure_after_peek() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.peek("test_exp").unwrap(); assert_eq!(ctx.pending(), 0); ctx.treatment("test_exp").unwrap(); assert_eq!(ctx.pending(), 1); } #[test] fn test_treatment_queues_exposure_with_override_variant() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_override("test_exp", 1).unwrap(); ctx.treatment("test_exp").unwrap(); assert_eq!(ctx.pending(), 1); let entries = log.lock().unwrap(); let exposure_entry = entries.iter().find(|e| e.event == "exposure").unwrap(); let exposure_data = exposure_entry.data.as_ref().unwrap(); assert_eq!(exposure_data["overridden"], json!(true)); assert_eq!(exposure_data["variant"], json!(1)); } #[test] fn test_treatment_queues_exposure_with_custom_assignment_variant() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_custom_assignment("test_exp", 1).unwrap(); ctx.treatment("test_exp").unwrap(); assert_eq!(ctx.pending(), 1); let entries = log.lock().unwrap(); let exposure_entry = entries.iter().find(|e| e.event == "exposure").unwrap(); let exposure_data = exposure_entry.data.as_ref().unwrap(); assert_eq!(exposure_data["custom"], json!(true)); assert_eq!(exposure_data["variant"], json!(1)); } #[test] fn test_treatment_base_variant_on_unknown_experiment() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); assert_eq!(ctx.treatment("unknown_exp").unwrap(), 0); assert_eq!(ctx.pending(), 1); } #[test] fn test_treatment_not_requeue_on_unknown_experiment() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("unknown_exp").unwrap(); ctx.treatment("unknown_exp").unwrap(); assert_eq!(ctx.pending(), 1); } #[test] fn test_peek_returns_override_variant() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_override("test_exp", 1).unwrap(); assert_eq!(ctx.peek("test_exp").unwrap(), 1); assert_eq!(ctx.pending(), 0); } #[test] fn test_peek_audience_mismatch_non_strict() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = false; let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("UK")).unwrap(); let variant = ctx.peek("test_exp").unwrap(); assert_eq!(variant, 1); } #[test] fn test_peek_audience_mismatch_strict() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("UK")).unwrap(); assert_eq!(ctx.peek("test_exp").unwrap(), 0); } #[test] fn test_treatment_audience_match_queues_with_audience_mismatch_false() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = false; let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("US")).unwrap(); ctx.treatment("test_exp").unwrap(); let entries = log.lock().unwrap(); let exposure_entry = entries.iter().find(|e| e.event == "exposure").unwrap(); let exposure_data = exposure_entry.data.as_ref().unwrap(); assert_eq!(exposure_data["audienceMismatch"], json!(false)); } #[test] fn test_treatment_audience_mismatch_queues_with_audience_mismatch_true() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = false; let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("UK")).unwrap(); ctx.treatment("test_exp").unwrap(); let entries = log.lock().unwrap(); let exposure_entry = entries.iter().find(|e| e.event == "exposure").unwrap(); let exposure_data = exposure_entry.data.as_ref().unwrap(); assert_eq!(exposure_data["audienceMismatch"], json!(true)); } #[test] fn test_treatment_audience_mismatch_strict_queues_control() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("UK")).unwrap(); let variant = ctx.treatment("test_exp").unwrap(); assert_eq!(variant, 0); let entries = log.lock().unwrap(); let exposure_entry = entries.iter().find(|e| e.event == "exposure").unwrap(); let exposure_data = exposure_entry.data.as_ref().unwrap(); assert_eq!(exposure_data["audienceMismatch"], json!(true)); assert_eq!(exposure_data["variant"], json!(0)); } #[test] fn test_variable_value_returns_default_when_unassigned() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![1.0, 0.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); let value = ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(value, json!("default")); } #[test] fn test_variable_value_returns_override_values() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_override("test_exp", 1).unwrap(); let value = ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(value, json!("red")); } #[test] fn test_variable_value_queues_exposure() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(ctx.pending(), 1); } #[test] fn test_variable_value_queues_exposure_after_peek() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.peek_variable_value("button", json!("default")).unwrap(); assert_eq!(ctx.pending(), 0); ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(ctx.pending(), 1); } #[test] fn test_variable_value_queues_exposure_only_once() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.variable_value("button", json!("default")).unwrap(); ctx.variable_value("button", json!("default")).unwrap(); ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(ctx.pending(), 1); } #[test] fn test_peek_variable_value_does_not_queue_exposure() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.peek_variable_value("button", json!("default")).unwrap(); assert_eq!(ctx.pending(), 0); } #[test] fn test_peek_variable_value_returns_override_values() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_override("test_exp", 1).unwrap(); let value = ctx.peek_variable_value("button", json!("default")).unwrap(); assert_eq!(value, json!("red")); } #[test] fn test_peek_variable_value_returns_default_when_unassigned() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![1.0, 0.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); let value = ctx.peek_variable_value("button", json!("default")).unwrap(); assert_eq!(value, json!("default")); } #[test] fn test_variable_keys_returns_all_active_keys() { let exp1 = make_experiment( "exp1", vec!["{}", r#"{"button":"red","header":"large"}"#], vec![0.5, 0.5], ); let exp2 = make_experiment("exp2", vec!["{}", r#"{"color":"blue"}"#], vec![0.5, 0.5]); let data = make_context_data(vec![exp1, exp2]); let ctx = Context::new(data); let keys = ctx.variable_keys().unwrap(); assert!(keys.contains_key("button")); assert!(keys.contains_key("header")); assert!(keys.contains_key("color")); } #[test] fn test_track_with_null_properties() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); assert!(ctx.track("click", Value::Null).is_ok()); assert_eq!(ctx.pending(), 1); } #[test] fn test_track_with_number_properties() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); assert!(ctx .track("purchase", json!({"amount": 99.99, "count": 1})) .is_ok()); assert_eq!(ctx.pending(), 1); } #[test] fn test_track_rejects_non_object_properties() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); for props in [json!(5), json!("str"), json!([1, 2]), json!(true)] { assert_eq!( ctx.track("purchase", props), Err("Goal 'purchase' properties must be of type object.".to_string()) ); } assert_eq!(ctx.pending(), 0); } #[test] fn test_numeric_unit_keeps_type_and_hashes_like_js() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_unit("user_id", 125).unwrap(); ctx.set_unit("float_id", 125.75).unwrap(); assert_eq!(ctx.get_unit("user_id"), Some(&json!(125))); assert_eq!(ctx.get_unit("float_id"), Some(&json!(125.75))); assert_eq!( ctx.set_unit("user_id", "125"), Err("Unit 'user_id' UID already set.".to_string()) ); assert_eq!( ctx.set_unit("bad", json!(true)), Err("Unit 'bad' must be a string or a number.".to_string()) ); ctx.track("g", Value::Null).unwrap(); let params = ctx.get_publish_params(); let float = params .units .iter() .find(|u| u.unit_type == "float_id") .unwrap(); assert_eq!(float.uid, Some(crate::utils::hash_unit("126"))); } #[test] fn test_track_callable_before_ready() { let mut ctx = Context::new_loading(); assert!(ctx.track("click", ()).is_ok()); assert_eq!(ctx.pending(), 1); } #[test] fn test_track_throws_after_finalize() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); assert!(ctx.track("click", ()).is_err()); } #[test] fn test_publish_does_not_call_when_queue_empty() { let data = make_context_data(vec![]); let (mut ctx, log) = make_logging_context(data); ctx.publish().unwrap(); let entries = log.lock().unwrap(); assert!(!entries.iter().any(|e| e.event == "publish")); } #[test] fn test_publish_clears_queue_on_success() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); ctx.track("click", ()).unwrap(); assert!(ctx.pending() > 0); ctx.publish().unwrap(); assert_eq!(ctx.pending(), 0); } #[test] fn test_finalize_does_not_call_publish_when_queue_empty() { let data = make_context_data(vec![]); let (mut ctx, log) = make_logging_context(data); ctx.finalize().unwrap(); let entries = log.lock().unwrap(); assert!(!entries.iter().any(|e| e.event == "publish")); assert!(entries.iter().any(|e| e.event == "finalize")); } #[test] fn test_finalize_calls_publish_when_pending() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let (mut ctx, log) = make_logging_context(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); assert!(ctx.pending() > 0); ctx.finalize().unwrap(); assert!(ctx.is_finalized()); assert_eq!(ctx.pending(), 0); let entries = log.lock().unwrap(); assert!(entries.iter().any(|e| e.event == "publish")); assert!(entries.iter().any(|e| e.event == "finalize")); } #[test] fn test_finalize_is_idempotent() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); assert!(ctx.is_finalized()); ctx.finalize().unwrap(); assert!(ctx.is_finalized()); } #[test] fn test_refresh_keeps_overrides() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp.clone()]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_override("test_exp", 1).unwrap(); assert_eq!(ctx.treatment("test_exp").unwrap(), 1); let data2 = make_context_data(vec![exp]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); assert_eq!(ctx.treatment("test_exp").unwrap(), 1); } #[test] fn test_refresh_keeps_custom_assignments() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp.clone()]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_custom_assignment("test_exp", 1).unwrap(); assert_eq!(ctx.treatment("test_exp").unwrap(), 1); let data2 = make_context_data(vec![exp]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); assert_eq!(ctx.treatment("test_exp").unwrap(), 1); } #[test] fn test_refresh_picks_up_fullon_change() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); let mut exp2 = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp2.full_on_variant = 1; let data2 = make_context_data(vec![exp2]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); assert_eq!(ctx.treatment("test_exp").unwrap(), 1); } #[test] fn test_refresh_picks_up_traffic_split_change() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); assert_eq!(ctx.treatment("test_exp").unwrap(), 1); let mut exp2 = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp2.traffic_split = vec![1.0, 0.0]; let data2 = make_context_data(vec![exp2]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); // The unit is now out of traffic, so the cached assignment must be dropped. assert_eq!(ctx.treatment("test_exp").unwrap(), 0); } #[test] fn test_refresh_picks_up_iteration_change() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); let pending_before = ctx.pending(); let mut exp2 = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp2.iteration = 2; let data2 = make_context_data(vec![exp2]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); ctx.treatment("test_exp").unwrap(); assert!(ctx.pending() > pending_before); } #[test] fn test_refresh_picks_up_id_change() { let exp = make_experiment_with_id( "test_exp", 1, vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); let pending_before = ctx.pending(); let exp2 = make_experiment_with_id( "test_exp", 2, vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data2 = make_context_data(vec![exp2]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); ctx.treatment("test_exp").unwrap(); assert!(ctx.pending() > pending_before); } #[test] fn test_refresh_no_change_same_variant() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp.clone()]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); let variant1 = ctx.treatment("test_exp").unwrap(); assert_eq!(ctx.pending(), 1); let data2 = make_context_data(vec![exp]); ctx.set_data_fetcher(Box::new(move || Ok(data2.clone()))); ctx.refresh().unwrap(); let variant2 = ctx.treatment("test_exp").unwrap(); assert_eq!(variant1, variant2); } #[test] fn test_custom_field_keys() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![ CustomFieldValue { name: "country".to_string(), value: "US".to_string(), field_type: "string".to_string(), }, CustomFieldValue { name: "overrides".to_string(), value: r#"{"key":"value"}"#.to_string(), field_type: "json".to_string(), }, ]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); let keys = ctx.custom_field_keys().unwrap(); assert!(keys.contains(&"country".to_string())); assert!(keys.contains(&"overrides".to_string())); } #[test] fn test_custom_field_value_string() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "country".to_string(), value: "US".to_string(), field_type: "string".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "country").unwrap(), Some(json!("US")) ); } #[test] fn test_custom_field_value_text() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "description".to_string(), value: "A test experiment".to_string(), field_type: "text".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "description").unwrap(), Some(json!("A test experiment")) ); } #[test] fn test_custom_field_value_json() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "overrides".to_string(), value: r#"{"key":"value"}"#.to_string(), field_type: "json".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "overrides").unwrap(), Some(json!({"key": "value"})) ); } #[test] fn test_custom_field_value_number() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "priority".to_string(), value: "5".to_string(), field_type: "number".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "priority").unwrap(), Some(json!(5.0)) ); } #[test] fn test_custom_field_value_decimal() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "weight".to_string(), value: "1.5".to_string(), field_type: "number".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "weight").unwrap(), Some(json!(1.5)) ); } #[test] fn test_custom_field_value_boolean() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "enabled".to_string(), value: "true".to_string(), field_type: "boolean".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "enabled").unwrap(), Some(json!(true)) ); } #[test] fn test_custom_field_value_boolean_false() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "enabled".to_string(), value: "false".to_string(), field_type: "boolean".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "enabled").unwrap(), Some(json!(false)) ); } #[test] fn test_custom_field_value_null_for_nonexistent_field() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "country".to_string(), value: "US".to_string(), field_type: "string".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "nonexistent").unwrap(), None ); } #[test] fn test_custom_field_value_null_for_nonexistent_experiment() { let data = make_context_data(vec![]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("nonexistent_exp", "field").unwrap(), None ); } #[test] fn test_custom_field_value_null_for_experiment_without_custom_fields() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!(ctx.custom_field_value("test_exp", "field").unwrap(), None); } #[test] fn test_custom_field_value_type() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![ CustomFieldValue { name: "country".to_string(), value: "US".to_string(), field_type: "string".to_string(), }, CustomFieldValue { name: "priority".to_string(), value: "5".to_string(), field_type: "number".to_string(), }, CustomFieldValue { name: "enabled".to_string(), value: "true".to_string(), field_type: "boolean".to_string(), }, ]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value_type("test_exp", "country").unwrap(), Some("string".to_string()) ); assert_eq!( ctx.custom_field_value_type("test_exp", "priority").unwrap(), Some("number".to_string()) ); assert_eq!( ctx.custom_field_value_type("test_exp", "enabled").unwrap(), Some("boolean".to_string()) ); assert_eq!( ctx.custom_field_value_type("test_exp", "nonexistent") .unwrap(), None ); } #[test] fn test_get_attribute_returns_last_set_value() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_attribute("country", json!("US")).unwrap(); ctx.set_attribute("country", json!("UK")).unwrap(); assert_eq!(ctx.get_attribute("country"), Some(&json!("UK"))); } #[test] fn test_get_units_returns_all() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "user123").unwrap(); ctx.set_unit("device_id", "device456").unwrap(); let units = ctx.get_units(); assert_eq!(units.get("session_id"), Some(&serde_json::json!("user123"))); assert_eq!( units.get("device_id"), Some(&serde_json::json!("device456")) ); } #[test] fn test_set_units_multiple() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_units([("session_id", "user123"), ("device_id", "device456")]) .unwrap(); assert_eq!( ctx.get_unit("session_id"), Some(&serde_json::json!("user123")) ); assert_eq!( ctx.get_unit("device_id"), Some(&serde_json::json!("device456")) ); } #[test] fn test_set_overrides_multiple() { let exp1 = make_experiment("exp1", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); let exp2 = make_experiment("exp2", vec!["{}", r#"{"color":"blue"}"#], vec![0.5, 0.5]); let data = make_context_data(vec![exp1, exp2]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_overrides([("exp1", 1), ("exp2", 0)]).unwrap(); assert_eq!(ctx.treatment("exp1").unwrap(), 1); assert_eq!(ctx.treatment("exp2").unwrap(), 0); } #[test] fn test_set_custom_assignments_multiple() { let exp1 = make_experiment("exp1", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); let exp2 = make_experiment("exp2", vec!["{}", r#"{"color":"blue"}"#], vec![0.5, 0.5]); let data = make_context_data(vec![exp1, exp2]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_custom_assignments([("exp1", 1), ("exp2", 0)]) .unwrap(); assert_eq!(ctx.treatment("exp1").unwrap(), 1); assert_eq!(ctx.treatment("exp2").unwrap(), 0); } #[test] fn test_set_attributes_throws_after_finalize() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); assert!(ctx.set_attributes([("country", json!("US"))]).is_err()); } #[test] fn test_set_custom_assignments_throws_after_finalize() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); assert!(ctx.set_custom_assignments([("exp1", 1)]).is_err()); } #[test] fn test_get_attributes_returns_all() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_attribute("country", json!("US")).unwrap(); ctx.set_attribute("age", json!(25)).unwrap(); let attrs = ctx.get_attributes(); assert_eq!(attrs.get("country"), Some(&json!("US"))); assert_eq!(attrs.get("age"), Some(&json!(25))); } #[test] fn test_data_returns_context_data() { let exp = make_experiment("exp1", vec!["{}"], vec![1.0]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); let ctx_data = ctx.data().unwrap(); assert_eq!(ctx_data.experiments.len(), 1); assert_eq!(ctx_data.experiments[0].name, "exp1"); } #[test] fn test_context_not_finalizing_initially() { let data = make_context_data(vec![]); let ctx = Context::new(data); assert!(!ctx.is_finalizing()); } #[test] fn test_custom_field_value_json_null() { let mut exp = make_experiment("test_exp", vec!["{}"], vec![1.0]); exp.custom_field_values = Some(vec![CustomFieldValue { name: "nullfield".to_string(), value: "null".to_string(), field_type: "json".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!( ctx.custom_field_value("test_exp", "nullfield").unwrap(), Some(Value::Null) ); } #[test] fn test_variable_value_audience_mismatch_strict_returns_default() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("UK")).unwrap(); let value = ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(value, json!("default")); } #[test] fn test_variable_value_audience_match_returns_value() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.set_attribute("country", json!("US")).unwrap(); let value = ctx.variable_value("button", json!("default")).unwrap(); assert_eq!(value, json!("red")); } #[test] fn test_base_constructor_deduplication() { let ctx1 = Context::new(make_context_data(vec![])); let ctx2 = Context::new_loading(); assert!(ctx1.is_ready()); assert!(!ctx2.is_ready()); assert_eq!(ctx1.pending(), 0); assert_eq!(ctx2.pending(), 0); } #[test] fn test_new_with_options_sets_fields() { let options = ContextOptions { publish_delay: 100, refresh_period: 200, event_logger: None, }; let ctx = Context::new_with_options(make_context_data(vec![]), options); assert!(ctx.is_ready()); assert_eq!(ctx.publish_delay(), 100); assert_eq!(ctx.refresh_period(), 200); } #[test] fn test_custom_field_number_out_of_range_returns_none() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "bad_num".to_string(), value: "NaN".to_string(), field_type: "number".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!(ctx.custom_field_value("test_exp", "bad_num").unwrap(), None); } #[test] fn test_custom_field_infinity_returns_none() { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); exp.custom_field_values = Some(vec![CustomFieldValue { name: "inf_num".to_string(), value: "inf".to_string(), field_type: "number".to_string(), }]); let data = make_context_data(vec![exp]); let ctx = Context::new(data); assert_eq!(ctx.custom_field_value("test_exp", "inf_num").unwrap(), None); } #[test] fn test_publish_clears_pending_even_on_serialization_path() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); ctx.track("click", ()).unwrap(); assert_eq!(ctx.pending(), 2); ctx.publish().unwrap(); assert_eq!(ctx.pending(), 0); ctx.publish().unwrap(); assert_eq!(ctx.pending(), 0); } #[test] fn test_variable_keys_returns_clone() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red","header":"large"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let ctx = Context::new(data); let keys = ctx.variable_keys().unwrap(); assert!(keys.contains_key("button")); assert!(keys.contains_key("header")); } #[test] fn test_publish_params_include_all_unit_hashes() { let exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5], ); let data = make_context_data(vec![exp]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "user123").unwrap(); ctx.set_unit("device_id", "device456").unwrap(); ctx.track("click", ()).unwrap(); let params = ctx.get_publish_params(); for unit in ¶ms.units { assert!( unit.uid.is_some(), "Unit '{}' should have a hashed uid", unit.unit_type ); } assert_eq!(params.units.len(), 2); } #[test] fn test_set_override_succeeds_after_finalize() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); assert!(ctx.is_finalized()); let result = ctx.set_override("exp_test", 2); assert!(result.is_ok(), "set_override should succeed after finalize"); } #[test] fn test_close_is_alias_for_finalize() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); assert!(!ctx.is_closed()); ctx.close().unwrap(); assert!(ctx.is_closed()); assert!(ctx.is_finalized()); } #[test] fn test_is_closed_matches_is_finalized() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); assert_eq!(ctx.is_closed(), ctx.is_finalized()); ctx.finalize().unwrap(); assert_eq!(ctx.is_closed(), ctx.is_finalized()); } #[test] fn test_is_closing_matches_is_finalizing() { let data = make_context_data(vec![]); let ctx = Context::new(data); assert_eq!(ctx.is_closing(), ctx.is_finalizing()); } #[test] fn test_finalized_error_message() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.finalize().unwrap(); let result = ctx.set_unit("session_id", "uid"); assert_eq!(result.unwrap_err(), "ABsmartly Context is finalized."); } #[test] fn test_unit_uid_blank_error_message() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); let result = ctx.set_unit("session_id", ""); assert_eq!( result.unwrap_err(), "Unit 'session_id' UID must not be blank." ); } #[test] fn test_unit_uid_already_set_error_message() { let data = make_context_data(vec![]); let mut ctx = Context::new(data); ctx.set_unit("session_id", "uid1").unwrap(); let result = ctx.set_unit("session_id", "uid2"); assert_eq!(result.unwrap_err(), "Unit 'session_id' UID already set."); } fn logging_options() -> (ContextOptions, Arc>>) { let log = Arc::new(Mutex::new(Vec::new())); let log_clone = log.clone(); let options = ContextOptions { event_logger: Some(Arc::new(move |_ctx, event, data| { log_clone.lock().unwrap().push(LogEntry { event: event.to_string(), data, }); })), ..Default::default() }; (options, log) } fn event_names(log: &Arc>>) -> Vec { log.lock() .unwrap() .iter() .map(|e| e.event.clone()) .collect() } /// Publisher whose next `fail_next` calls fail; counts every call. struct FlakyPublisher { fail_next: Arc>, calls: Arc>, } impl ContextPublisher for FlakyPublisher { fn publish(&self, _params: &PublishParams) -> Result<(), String> { *self.calls.lock().unwrap() += 1; let mut fail_next = self.fail_next.lock().unwrap(); if *fail_next > 0 { *fail_next -= 1; return Err("Publish failed".to_string()); } Ok(()) } } fn flaky_publisher(failures: usize) -> (Box, Arc>) { let calls = Arc::new(Mutex::new(0)); let publisher = Box::new(FlakyPublisher { fail_next: Arc::new(Mutex::new(failures)), calls: calls.clone(), }); (publisher, calls) } fn exposed_context(options: ContextOptions) -> Context { let exp = make_experiment("test_exp", vec!["{}", "{}"], vec![0.5, 0.5]); let mut ctx = Context::new_with_options(make_context_data(vec![exp]), options); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); ctx.track("click", ()).unwrap(); ctx } #[test] fn test_new_with_options_emits_ready_with_data() { let exp = make_experiment("exp1", vec!["{}"], vec![1.0]); let (options, log) = logging_options(); let ctx = Context::new_with_options(make_context_data(vec![exp]), options); assert!(ctx.is_ready()); let entries = log.lock().unwrap(); assert_eq!(entries.len(), 1); assert_eq!(entries[0].event, "ready"); let data = entries[0] .data .as_ref() .expect("ready carries context data"); assert_eq!(data["experiments"][0]["name"], "exp1"); } #[test] fn test_become_ready_emits_ready_once() { let (options, log) = logging_options(); let mut ctx = Context::new_loading_with_options(options); assert!(event_names(&log).is_empty()); ctx.become_ready(make_context_data(vec![])); ctx.become_ready(make_context_data(vec![])); assert_eq!(event_names(&log), vec!["ready"]); } #[test] fn test_become_failed_with_error_emits_error_and_sets_ready_error() { let (options, log) = logging_options(); let mut ctx = Context::new_loading_with_options(options); ctx.become_failed_with_error("fetch failed".to_string()); assert!(ctx.is_failed()); assert_eq!(ctx.ready_error(), Some(&"fetch failed".to_string())); let entries = log.lock().unwrap(); assert_eq!(entries.len(), 1); assert_eq!(entries[0].event, "error"); assert_eq!(entries[0].data, Some(json!({"message": "fetch failed"}))); } #[test] fn test_become_failed_emits_error_once() { let (options, log) = logging_options(); let mut ctx = Context::new_loading_with_options(options); ctx.become_failed(); ctx.become_failed(); ctx.become_ready(make_context_data(vec![])); assert!(ctx.is_failed()); assert_eq!(event_names(&log), vec!["error"]); } #[test] fn test_failed_context_discards_pending_on_publish() { let (publisher, calls) = flaky_publisher(0); let mut ctx = Context::new_loading(); ctx.set_publisher(publisher); ctx.track("click", ()).unwrap(); ctx.become_failed_with_error("fetch failed".to_string()); assert!(ctx.publish().is_ok()); assert_eq!(ctx.pending(), 0); assert_eq!(*calls.lock().unwrap(), 0); } #[test] fn test_publish_failure_preserves_pending_and_emits_error() { let (options, log) = logging_options(); let mut ctx = exposed_context(options); let (publisher, calls) = flaky_publisher(1); ctx.set_publisher(publisher); assert_eq!(ctx.pending(), 2); log.lock().unwrap().clear(); let result = ctx.publish(); assert_eq!(result, Err("Publish failed".to_string())); assert_eq!(ctx.pending(), 2); assert_eq!(*calls.lock().unwrap(), 1); let entries = log.lock().unwrap(); assert_eq!(entries.len(), 1); assert_eq!(entries[0].event, "error"); assert_eq!(entries[0].data, Some(json!({"message": "Publish failed"}))); } #[test] fn test_publish_retry_after_failure_sends_same_events() { let exp = make_experiment("test_exp", vec!["{}", "{}"], vec![0.5, 0.5]); let mut ctx = Context::new(make_context_data(vec![exp])); ctx.set_unit("session_id", "test_user").unwrap(); ctx.treatment("test_exp").unwrap(); ctx.track("click", ()).unwrap(); let sent = Arc::new(Mutex::new(Vec::::new())); struct Recorder { attempts: Mutex, sent: Arc>>, } impl ContextPublisher for Recorder { fn publish(&self, params: &PublishParams) -> Result<(), String> { let mut attempts = self.attempts.lock().unwrap(); *attempts += 1; if *attempts == 1 { return Err("Publish failed".to_string()); } self.sent.lock().unwrap().push(params.clone()); Ok(()) } } ctx.set_publisher(Box::new(Recorder { attempts: Mutex::new(0), sent: sent.clone(), })); assert!(ctx.publish().is_err()); assert!(ctx.publish().is_ok()); assert_eq!(ctx.pending(), 0); let sent = sent.lock().unwrap(); assert_eq!(sent.len(), 1); assert_eq!(sent[0].exposures.as_ref().map(Vec::len), Some(1)); assert_eq!(sent[0].goals.as_ref().map(Vec::len), Some(1)); } #[test] fn test_publish_success_emits_publish_and_clears_pending() { let (options, log) = logging_options(); let mut ctx = exposed_context(options); let (publisher, calls) = flaky_publisher(0); ctx.set_publisher(publisher); log.lock().unwrap().clear(); assert!(ctx.publish().is_ok()); assert_eq!(ctx.pending(), 0); assert_eq!(*calls.lock().unwrap(), 1); assert_eq!(event_names(&log), vec!["publish"]); } #[test] fn test_finalize_failure_keeps_context_open_and_pending() { let (options, log) = logging_options(); let mut ctx = exposed_context(options); let (publisher, _calls) = flaky_publisher(1); ctx.set_publisher(publisher); log.lock().unwrap().clear(); assert_eq!(ctx.finalize(), Err("Publish failed".to_string())); assert!(!ctx.is_finalized()); assert!(!ctx.is_finalizing()); assert!(ctx.is_ready()); assert_eq!(ctx.pending(), 2); assert_eq!(event_names(&log), vec!["error"]); assert!(ctx.finalize().is_ok()); assert!(ctx.is_finalized()); assert_eq!(ctx.pending(), 0); assert_eq!(event_names(&log), vec!["error", "publish", "finalize"]); } #[test] fn test_refresh_fetch_failure_emits_error() { let (options, log) = logging_options(); let mut ctx = Context::new_with_options(make_context_data(vec![]), options); ctx.set_data_fetcher(Box::new(|| Err("Refresh fetch failed: boom".to_string()))); log.lock().unwrap().clear(); assert_eq!(ctx.refresh(), Err("Refresh fetch failed: boom".to_string())); assert_eq!(event_names(&log), vec!["error"]); } /// A strict audience experiment with a 0/100 split to variant 1, so a /// matching audience yields variant 1 and a mismatch yields variant 0. fn make_audience_experiment(audience: &str) -> ExperimentData { let mut exp = make_experiment( "test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0], ); exp.audience = audience.to_string(); exp.audience_strict = true; exp } fn context_with_system_attrs( exp: ExperimentData, application: Option<&str>, environment: Option<&str>, ) -> Context { let mut context = Context::new(make_context_data(vec![exp])); context.set_system_attributes(application, environment); context.set_unit("session_id", "test_user").unwrap(); context } const PRODUCTION_FILTER: &str = r#"{"filter":[{"eq":[{"var":"environment"},{"value":"production"}]}]}"#; /// A strict-audience context for the "website" app in "production". fn production_context(audience: &str) -> Context { context_with_system_attrs( make_audience_experiment(audience), Some("website"), Some("production"), ) } fn audience_mismatch(context: &Context, name: &str) -> bool { context .assignments .get(name) .map(|a| a.assignment.audience_mismatch) .unwrap_or(false) } #[test] fn test_system_attributes_in_strict_audience() { for (audience, variant) in [ (PRODUCTION_FILTER, 1), ( r#"{"filter":[{"eq":[{"var":"application"},{"value":"website"}]}]}"#, 1, ), ( r#"{"filter":[{"eq":[{"var":"environment"},{"value":"staging"}]}]}"#, 0, ), ( r#"{"filter":[{"eq":[{"var":"app_version"},{"value":"0"}]}]}"#, 0, ), (r#"{"filter":[{"null":{"var":"app_version"}}]}"#, 1), (r#"{"filter":[{"null":{"var":"environment"}}]}"#, 0), ] { let mut context = production_context(audience); assert_eq!( context.treatment("test_exp").unwrap(), variant, "{}", audience ); assert_eq!(audience_mismatch(&context, "test_exp"), variant == 0); } } #[test] fn test_user_attribute_overrides_system_environment() { let staging_exp = make_audience_experiment( r#"{"filter":[{"eq":[{"var":"environment"},{"value":"staging"}]}]}"#, ); let mut context = context_with_system_attrs(staging_exp, Some("website"), Some("production")); assert_eq!(context.peek("test_exp").unwrap(), 0); context.set_attribute("environment", "staging").unwrap(); assert_eq!(context.treatment("test_exp").unwrap(), 1); assert!(!audience_mismatch(&context, "test_exp")); let mut context2 = production_context(PRODUCTION_FILTER); context2.set_attribute("environment", "staging").unwrap(); assert_eq!(context2.treatment("test_exp").unwrap(), 0); assert!(audience_mismatch(&context2, "test_exp")); } #[test] fn test_missing_environment_config_leaves_key_absent() { let exp = make_audience_experiment(PRODUCTION_FILTER); let mut context = context_with_system_attrs(exp, Some(""), None); assert_eq!(context.treatment("test_exp").unwrap(), 0); assert!(audience_mismatch(&context, "test_exp")); for (application, environment) in [(Some(""), Some("")), (None, None)] { for name in ["application", "environment", "app_version"] { let audience = format!(r#"{{"filter":[{{"null":{{"var":"{}"}}}}]}}"#, name); let mut context = context_with_system_attrs( make_audience_experiment(&audience), application, environment, ); assert_eq!(context.treatment("test_exp").unwrap(), 1, "{}", name); } } } #[test] fn test_system_attributes_not_added_to_publish_payload() { let mut context = production_context(PRODUCTION_FILTER); context.treatment("test_exp").unwrap(); let params = context.get_publish_params(); let json = serde_json::to_value(¶ms).unwrap(); let attributes = json.get("attributes"); assert!( attributes.is_none() || attributes == Some(&Value::Null), "expected no attributes in publish payload, got: {:?}", attributes ); } #[test] fn test_user_attribute_still_published_but_system_not() { let mut context = production_context(PRODUCTION_FILTER); context.set_attribute("country", "US").unwrap(); context.treatment("test_exp").unwrap(); let params = context.get_publish_params(); let json = serde_json::to_value(¶ms).unwrap(); let attributes = json .get("attributes") .and_then(|a| a.as_array()) .cloned() .unwrap_or_default(); let names: Vec<&str> = attributes .iter() .filter_map(|a| a.get("name").and_then(|n| n.as_str())) .collect(); assert!(names.contains(&"country")); assert!(!names.contains(&"application")); assert!(!names.contains(&"environment")); } #[test] fn test_changing_system_attributes_invalidates_cached_assignment() { let mut context = production_context(PRODUCTION_FILTER); assert_eq!(context.peek("test_exp").unwrap(), 1); assert!(!audience_mismatch(&context, "test_exp")); context.set_system_attributes(Some("website"), Some("staging")); assert_eq!(context.peek("test_exp").unwrap(), 0); assert!(audience_mismatch(&context, "test_exp")); context.set_system_attributes(Some("website"), Some("production")); assert_eq!(context.peek("test_exp").unwrap(), 1); assert!(!audience_mismatch(&context, "test_exp")); } #[test] fn test_system_attributes_build_context_wires_config() { let sdk = crate::sdk::Client::new( "https://test.absmartly.io/v1", "test-api-key", "website", "production", ) .unwrap(); let exp = make_audience_experiment(PRODUCTION_FILTER); let mut context = sdk.create_context_with( [("session_id", "test_user")], make_context_data(vec![exp]), None, ); assert_eq!(context.treatment("test_exp").unwrap(), 1); } fn rules_experiment(rules: Value) -> ExperimentData { let mut exp = make_experiment( "rules_exp", vec![ r#"{"color":"red"}"#, r#"{"color":"green"}"#, r#"{"color":"blue"}"#, ], vec![1.0, 0.0, 0.0], ); exp.assignment_rules = rules.to_string(); exp } fn rules_context(rules: Value) -> Context { let mut ctx = Context::new(make_context_data(vec![rules_experiment(rules)])); ctx.set_system_attributes(Some("website"), Some("production")); ctx } fn exposures(ctx: &mut Context) -> Vec { ctx.get_publish_params().exposures.unwrap_or_default() } #[test] fn test_assign_rule_wins_and_flags_exposure() { let mut ctx = rules_context(json!({"rules": [ {"type": "assign", "variant": 2, "conditions": {"eq": [{"var": "tier"}, {"value": "gold"}]}} ]})); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 0); ctx.set_attribute("tier", "gold").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 2); assert_eq!(ctx.variable_value("color", "none").unwrap(), json!("blue")); let exposures = exposures(&mut ctx); assert_eq!(exposures.len(), 1); assert!(exposures[0].rule_override); assert!(!exposures[0].assigned); assert!(!exposures[0].overridden); let wire = serde_json::to_value(&exposures[0]).unwrap(); assert_eq!(wire["ruleOverride"], json!(true)); } #[test] fn test_assign_rule_without_unit_still_applies() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1}]})); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); assert_eq!(ctx.variable_value("color", "none").unwrap(), json!("green")); } #[test] fn test_rule_beats_audience_strict_and_custom_assignment() { let mut exp = rules_experiment(json!({"rules": [{"type": "assign", "variant": 1}]})); exp.audience = r#"{"filter":[{"value":false}]}"#.to_string(); exp.audience_strict = true; let mut ctx = Context::new(make_context_data(vec![exp])); ctx.set_unit("session_id", "user").unwrap(); ctx.set_custom_assignment("rules_exp", 2).unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); let exposure = &exposures(&mut ctx)[0]; assert!(exposure.rule_override); assert!(!exposure.audience_mismatch); assert!(!exposure.custom); } #[test] fn test_override_beats_rule() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1}]})); ctx.set_unit("session_id", "user").unwrap(); ctx.set_override("rules_exp", 2).unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 2); let exposure = &exposures(&mut ctx)[0]; assert!(exposure.overridden); assert!(!exposure.rule_override); } #[test] fn test_out_of_range_or_mismatched_split_falls_back_without_trying_later_rules() { for first in [ json!({"type": "assign", "variant": 3}), json!({"type": "assign", "variant": -1}), // Integral but beyond i32: still the first match, as in JS, which // then fails the bounds check instead of moving on. json!({"type": "assign", "variant": 2147483648_i64}), json!({"type": "assign", "variant": -2147483649_i64}), json!({"type": "assign", "variant": 1e300}), json!({"type": "split", "percentages": "50/50"}), ] { let mut ctx = rules_context(json!({"rules": [first, {"type": "assign", "variant": 2}]})); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); assert!(!exposures(&mut ctx)[0].rule_override); } } #[test] fn test_rule_scope_ignores_user_environment_attribute() { // Scope uses the configured environment; a condition still sees the // user's value. let mut ctx = rules_context( json!({"rules": [{"type": "assign", "variant": 1, "environments": ["staging"]}]}), ); ctx.set_unit("session_id", "user").unwrap(); ctx.set_attribute("environment", "staging").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 0); let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1, "conditions": {"eq": [{"var": "environment"}, {"value": "staging"}]}}]})); ctx.set_unit("session_id", "user").unwrap(); ctx.set_attribute("environment", "staging").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 1); } #[test] fn test_rule_wins_over_zero_traffic() { let mut exp = rules_experiment(json!({"rules": [{"type": "assign", "variant": 2}]})); exp.traffic_split = vec![1.0, 0.0]; let mut ctx = Context::new(make_context_data(vec![exp])); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 2); assert!(exposures(&mut ctx)[0].rule_override); } #[test] fn test_exposed_rule_win_rebuilt_to_same_variant_is_not_exposed_again() { // The exposed split resolved without its unit (full-on variant 0); the // refreshed rule assigns 0 directly. The outcome changed but the // variant did not, so the decision must not be published again. let mut ctx = rules_context(json!({"rules": [{"type": "split", "percentages": "0/0/100"}]})); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); let mut data = ctx.data().unwrap().clone(); data.experiments[0].assignment_rules = json!({"rules": [{"type": "assign", "variant": 0}]}).to_string(); ctx.refresh_with(data); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); assert_eq!(ctx.variable_value("color", "none").unwrap(), json!("red")); let exposures = exposures(&mut ctx); assert_eq!(exposures.len(), 1); assert!(exposures[0].rule_override); } #[test] fn test_same_outcome_refresh_advances_the_rules_revision() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 2}]})); ctx.set_unit("session_id", "user").unwrap(); ctx.treatment("rules_exp").unwrap(); let mut data = ctx.data().unwrap().clone(); data.experiments[0].assignment_rules = json!({"rules": [{"name": "renamed", "type": "assign", "variant": 2}]}).to_string(); ctx.refresh_with(data); assert_eq!(ctx.peek("rules_exp").unwrap(), 2); let current = ctx.index["rules_exp"].rules_rev; assert_eq!(ctx.assignments["rules_exp"].rule.rules_rev, current); let data = ctx.data().unwrap().clone(); ctx.refresh_with(data); assert_eq!(ctx.index["rules_exp"].rules_rev, current); } #[test] fn test_user_app_version_is_evaluated_and_published() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1, "conditions": {"semver_gte": [{"var": "app_version"}, {"value": "2.0.0"}]}}]})); ctx.set_unit("session_id", "user").unwrap(); ctx.set_attribute("app_version", "2.1.0").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); let attributes = ctx.get_publish_params().attributes.unwrap_or_default(); let names: Vec<&str> = attributes.iter().map(|a| a.name.as_str()).collect(); assert_eq!(names, ["app_version"]); } #[test] fn test_every_exposure_serializes_rule_override() { let mut ctx = Context::new(make_context_data(vec![make_experiment( "plain", vec!["", ""], vec![0.5, 0.5], )])); ctx.set_unit("session_id", "user").unwrap(); ctx.treatment("plain").unwrap(); let wire = serde_json::to_value(&exposures(&mut ctx)[0]).unwrap(); assert_eq!(wire["ruleOverride"], json!(false)); } #[test] fn test_unknown_payload_fields_are_ignored_at_every_level() { let data: ContextData = serde_json::from_value(json!({ "experiments": [ { "id": 1, "name": "a", "unitType": "session_id", "iteration": 1, "split": [0.0, 1.0], "trafficSplit": [0.0, 1.0], "variants": [{"config": null, "future": [1]}, {"config": "{}", "x": {"y": 1}}], "customFieldValues": [{"name": "f", "value": "v", "type": "string", "extra": true}], "assignmentRules": "{\"rules\":[{\"type\":\"future\"}],\"version\":2}", "holdouts": [{"id": 9}], "switchback": {"period": 60}, "newScalar": 1 }, { "id": 2, "name": "b", "unitType": "session_id", "iteration": 1, "split": [0.0, 1.0], "trafficSplit": [0.0, 1.0], "variants": [{}, {}], "assignmentRules": {"rules": []}, "tags": ["t"] } ], "application": {"name": "x"}, "futureRoot": [1, {"a": null}], "flag": true })) .unwrap(); let mut ctx = Context::new(data); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("a").unwrap(), 1); assert_eq!(ctx.treatment("b").unwrap(), 1); } #[test] fn test_rule_environments_use_client_environment() { let rules = json!({"rules": [{"type": "assign", "variant": 1, "environments": ["staging"]}]}); let mut ctx = rules_context(rules.clone()); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 0); let mut ctx = Context::new(make_context_data(vec![rules_experiment(rules)])); ctx.set_system_attributes(Some("website"), Some("staging")); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 1); } #[test] fn test_rule_conditions_see_system_attributes_and_semver() { let mut ctx = rules_context(json!({"rules": [{ "type": "assign", "variant": 1, "conditions": {"and": [ {"eq": [{"var": "application"}, {"value": "website"}]}, {"semver_gte": [{"var": "version"}, {"value": "2.0.0"}]} ]} }]})); ctx.set_unit("session_id", "user").unwrap(); ctx.set_attribute("version", "1.9.9").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 0); ctx.set_attribute("version", "v2.0.0").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 1); } #[test] fn test_split_rule_randomizes_over_its_percentages() { let mut ctx = rules_context(json!({"rules": [{"type": "split", "percentages": "0/50/50"}]})); let mut seen = std::collections::HashSet::new(); for i in 0..50 { let mut c = Context::new(ctx.data().unwrap().clone()); c.set_unit("session_id", format!("user-{}", i)).unwrap(); let variant = c.treatment("rules_exp").unwrap(); assert_ne!(variant, 0); seen.insert(variant); assert!(exposures(&mut c)[0].rule_override); } assert_eq!(seen.len(), 2); ctx.set_unit("session_id", "user-7").unwrap(); let hash = crate::utils::hash_unit("user-7"); let expected = VariantAssigner::new(&hash).assign(&[0.0, 0.5, 0.5], 0, 0) as i32; assert_eq!(ctx.treatment("rules_exp").unwrap(), expected); } #[test] fn test_split_rule_matches_javascript_per_uid_with_experiment_seeds() { // From javascript-sdk's VariantAssigner for a 20/30/50 split: seeds // 123/456 give these; the traffic seeds 789/1011 would give // [2, 1, 2, 1, 2, 2] and 0/0 [2, 2, 2, 2, 1, 2]. let mut exp = rules_experiment(json!({"rules": [{"type": "split", "percentages": "20/30/50"}]})); (exp.seed_hi, exp.seed_lo) = (123, 456); (exp.traffic_seed_hi, exp.traffic_seed_lo) = (789, 1011); let data = make_context_data(vec![exp]); let variants: Vec = ["alice", "bob", "charlie", "dave", "eve", "user-7"] .into_iter() .map(|uid| { let mut ctx = Context::new(data.clone()); ctx.set_unit("session_id", uid).unwrap(); ctx.treatment("rules_exp").unwrap() }) .collect(); assert_eq!(variants, [1, 2, 1, 1, 2, 2]); } #[test] fn test_split_rule_without_unit_uses_full_on_and_rerandomizes_when_unexposed() { let mut ctx = rules_context(json!({"rules": [{"type": "split", "percentages": "0/0/100"}]})); assert_eq!(ctx.peek("rules_exp").unwrap(), 0); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.peek("rules_exp").unwrap(), 2); } #[test] fn test_exposed_split_rule_without_unit_stays_pinned() { let mut ctx = rules_context(json!({"rules": [{"type": "split", "percentages": "0/0/100"}]})); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); ctx.set_unit("session_id", "user").unwrap(); ctx.set_attribute("anything", 1).unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); let exposures = exposures(&mut ctx); assert_eq!(exposures.len(), 1); assert!(exposures[0].rule_override); } #[test] fn test_attribute_change_reevaluates_rule() { let mut ctx = rules_context(json!({"rules": [ {"type": "assign", "variant": 1, "conditions": {"eq": [{"var": "beta"}, {"value": true}]}} ]})); ctx.set_unit("session_id", "user").unwrap(); ctx.set_attribute("beta", true).unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); ctx.set_attribute("beta", false).unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); assert_eq!(exposures(&mut ctx).len(), 2); } #[test] fn test_unrelated_attribute_change_keeps_exposed_rule_assignment() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1}]})); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); ctx.set_attribute("other", "x").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); assert_eq!(exposures(&mut ctx).len(), 1); } #[test] fn test_refresh_changing_rules_reassigns() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1}]})); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); let refreshed = rules_experiment(json!({"rules": [{"type": "assign", "variant": 2}]})); ctx.refresh_with(make_context_data(vec![refreshed])); assert_eq!(ctx.treatment("rules_exp").unwrap(), 2); ctx.refresh_with(make_context_data(vec![rules_experiment( json!({"rules": []}), )])); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); let variants: Vec = exposures(&mut ctx).iter().map(|e| e.variant).collect(); assert_eq!(variants, vec![1, 2, 0]); } fn two_variant_rules_experiment(rules: Value) -> ExperimentData { let mut exp = make_experiment( "rules_exp", vec![r#"{"color":"red"}"#, r#"{"color":"green"}"#], vec![1.0, 0.0], ); exp.assignment_rules = rules.to_string(); exp } #[test] fn test_refresh_removing_variant_drops_out_of_range_assign_rule() { let rules = json!({"rules": [{"type": "assign", "variant": 2}]}); let mut ctx = rules_context(rules.clone()); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 2); ctx.refresh_with(make_context_data(vec![two_variant_rules_experiment(rules)])); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); let exposures = exposures(&mut ctx); assert_eq!(exposures.len(), 2); assert!(!exposures[1].rule_override); assert!(exposures[1].assigned); } #[test] fn test_refresh_removing_variant_drops_mismatched_split_rule() { let rules = json!({"rules": [{"type": "split", "percentages": "0/0/100"}]}); let mut ctx = rules_context(rules.clone()); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 2); ctx.refresh_with(make_context_data(vec![two_variant_rules_experiment(rules)])); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); let exposures = exposures(&mut ctx); assert_eq!(exposures.len(), 2); assert!(!exposures[1].rule_override); assert!(exposures[1].assigned); } #[test] fn test_refresh_removing_experiment_drops_rule_assignment() { let mut ctx = rules_context(json!({"rules": [{"type": "assign", "variant": 1}]})); assert_eq!(ctx.treatment("rules_exp").unwrap(), 1); ctx.refresh_with(make_context_data(vec![])); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); } #[test] fn test_invalid_rules_json_is_ignored() { let mut exp = rules_experiment(json!({})); exp.assignment_rules = "{not json".to_string(); let mut ctx = Context::new(make_context_data(vec![exp])); ctx.set_unit("session_id", "user").unwrap(); assert_eq!(ctx.treatment("rules_exp").unwrap(), 0); assert!(!exposures(&mut ctx)[0].rule_override); } }