|
| 1 | +/* |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.IO; |
| 20 | +using System.Linq; |
| 21 | +using Moq; |
| 22 | +using Newtonsoft.Json.Linq; |
| 23 | +using NUnit.Framework; |
| 24 | +using OptimizelySDK.Bucketing; |
| 25 | +using OptimizelySDK.Config; |
| 26 | +using OptimizelySDK.Entity; |
| 27 | +using OptimizelySDK.ErrorHandler; |
| 28 | +using OptimizelySDK.Event; |
| 29 | +using OptimizelySDK.Event.Dispatcher; |
| 30 | +using OptimizelySDK.Logger; |
| 31 | +using OptimizelySDK.OptimizelyDecisions; |
| 32 | + |
| 33 | +namespace OptimizelySDK.Tests |
| 34 | +{ |
| 35 | + [TestFixture] |
| 36 | + public class OptimizelyUserContextHoldoutTest |
| 37 | + { |
| 38 | + private Mock<ILogger> LoggerMock; |
| 39 | + private Mock<IEventDispatcher> EventDispatcherMock; |
| 40 | + private DatafileProjectConfig Config; |
| 41 | + private JObject TestData; |
| 42 | + private Optimizely OptimizelyInstance; |
| 43 | + |
| 44 | + private const string TestUserId = "testUserId"; |
| 45 | + private const string TestBucketingId = "testBucketingId"; |
| 46 | + |
| 47 | + [SetUp] |
| 48 | + public void Initialize() |
| 49 | + { |
| 50 | + LoggerMock = new Mock<ILogger>(); |
| 51 | + EventDispatcherMock = new Mock<IEventDispatcher>(); |
| 52 | + |
| 53 | + // Load test data |
| 54 | + var testDataPath = Path.Combine(TestContext.CurrentContext.TestDirectory, |
| 55 | + "TestData", "HoldoutTestData.json"); |
| 56 | + var jsonContent = File.ReadAllText(testDataPath); |
| 57 | + TestData = JObject.Parse(jsonContent); |
| 58 | + |
| 59 | + // Use datafile with holdouts for proper config setup |
| 60 | + var datafileWithHoldouts = TestData["datafileWithHoldouts"].ToString(); |
| 61 | + |
| 62 | + // Create an Optimizely instance with the test data |
| 63 | + OptimizelyInstance = new Optimizely(datafileWithHoldouts, EventDispatcherMock.Object, LoggerMock.Object); |
| 64 | + |
| 65 | + // Get the config from the Optimizely instance to ensure they're synchronized |
| 66 | + Config = OptimizelyInstance.ProjectConfigManager.GetConfig() as DatafileProjectConfig; |
| 67 | + |
| 68 | + // Verify that the config contains holdouts |
| 69 | + Assert.IsNotNull(Config.Holdouts, "Config should have holdouts"); |
| 70 | + Assert.IsTrue(Config.Holdouts.Length > 0, "Config should contain holdouts"); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public void TestDecide_GlobalHoldout() |
| 75 | + { |
| 76 | + // Test Decide() method with global holdout decision |
| 77 | + var featureFlag = Config.FeatureKeyMap["test_flag_1"]; |
| 78 | + Assert.IsNotNull(featureFlag, "Feature flag should exist"); |
| 79 | + |
| 80 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 81 | + new UserAttributes { { "country", "us" } }); |
| 82 | + |
| 83 | + var decision = userContext.Decide("test_flag_1"); |
| 84 | + |
| 85 | + Assert.IsNotNull(decision, "Decision should not be null"); |
| 86 | + Assert.AreEqual("test_flag_1", decision.FlagKey, "Flag key should match"); |
| 87 | + |
| 88 | + // With real bucketer, we can't guarantee specific variation but can verify structure |
| 89 | + // The decision should either be from holdout, experiment, or rollout |
| 90 | + Assert.IsTrue(!string.IsNullOrEmpty(decision.VariationKey) || decision.VariationKey == null, |
| 91 | + "Variation key should be valid or null"); |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public void TestDecide_IncludedFlagsHoldout() |
| 96 | + { |
| 97 | + // Test holdout with includedFlags configuration |
| 98 | + var featureFlag = Config.FeatureKeyMap["test_flag_1"]; |
| 99 | + Assert.IsNotNull(featureFlag, "Feature flag should exist"); |
| 100 | + |
| 101 | + // Check if there's a holdout that includes this flag |
| 102 | + var includedHoldout = Config.Holdouts.FirstOrDefault(h => |
| 103 | + h.IncludedFlags != null && h.IncludedFlags.Contains(featureFlag.Id)); |
| 104 | + |
| 105 | + if (includedHoldout != null) |
| 106 | + { |
| 107 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 108 | + new UserAttributes { { "country", "us" } }); |
| 109 | + |
| 110 | + var decision = userContext.Decide("test_flag_1"); |
| 111 | + |
| 112 | + Assert.IsNotNull(decision, "Decision should not be null"); |
| 113 | + Assert.AreEqual("test_flag_1", decision.FlagKey, "Flag key should match"); |
| 114 | + |
| 115 | + // Verify decision is valid |
| 116 | + Assert.IsTrue(decision.VariationKey != null || decision.VariationKey == null, |
| 117 | + "Decision should have valid structure"); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + Assert.Inconclusive("No included holdout found for test_flag_1"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void TestDecide_ExcludedFlagsHoldout() |
| 127 | + { |
| 128 | + // Test holdout with excludedFlags configuration |
| 129 | + // Based on test data, flag_3 and flag_4 are excluded by holdout_excluded_1 |
| 130 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 131 | + new UserAttributes { { "country", "us" } }); |
| 132 | + |
| 133 | + // Test with an excluded flag (test_flag_3 maps to flag_3) |
| 134 | + var excludedDecision = userContext.Decide("test_flag_3"); |
| 135 | + |
| 136 | + Assert.IsNotNull(excludedDecision, "Decision should not be null for excluded flag"); |
| 137 | + Assert.AreEqual("test_flag_3", excludedDecision.FlagKey, "Flag key should match"); |
| 138 | + |
| 139 | + // For excluded flags, the decision should not come from the excluded holdout |
| 140 | + // The excluded holdout has key "excluded_holdout" |
| 141 | + Assert.AreNotEqual("excluded_holdout", excludedDecision.RuleKey, |
| 142 | + "Decision should not come from excluded holdout for flag_3"); |
| 143 | + |
| 144 | + // Also test with a non-excluded flag (test_flag_1 maps to flag_1) |
| 145 | + var nonExcludedDecision = userContext.Decide("test_flag_1"); |
| 146 | + |
| 147 | + Assert.IsNotNull(nonExcludedDecision, "Decision should not be null for non-excluded flag"); |
| 148 | + Assert.AreEqual("test_flag_1", nonExcludedDecision.FlagKey, "Flag key should match"); |
| 149 | + |
| 150 | + // For non-excluded flags, they can potentially be affected by holdouts |
| 151 | + // (depending on other holdout configurations like global or included holdouts) |
| 152 | + } |
| 153 | + |
| 154 | + [Test] |
| 155 | + public void TestDecideAll_MultipleHoldouts() |
| 156 | + { |
| 157 | + // Test DecideAll() with multiple holdouts affecting different flags |
| 158 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 159 | + new UserAttributes { { "country", "us" } }); |
| 160 | + |
| 161 | + var decisions = userContext.DecideAll(); |
| 162 | + |
| 163 | + Assert.IsNotNull(decisions, "Decisions should not be null"); |
| 164 | + Assert.IsTrue(decisions.Count > 0, "Should have at least one decision"); |
| 165 | + |
| 166 | + // Verify each decision has proper structure |
| 167 | + foreach (var kvp in decisions) |
| 168 | + { |
| 169 | + var flagKey = kvp.Key; |
| 170 | + var decision = kvp.Value; |
| 171 | + |
| 172 | + Assert.AreEqual(flagKey, decision.FlagKey, $"Flag key should match for {flagKey}"); |
| 173 | + Assert.IsNotNull(decision, $"Decision should not be null for {flagKey}"); |
| 174 | + |
| 175 | + // Decision should have either a variation or be properly null |
| 176 | + Assert.IsTrue(decision.VariationKey != null || decision.VariationKey == null, |
| 177 | + $"Decision structure should be valid for {flagKey}"); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [Test] |
| 182 | + public void TestDecide_HoldoutImpressionEvent() |
| 183 | + { |
| 184 | + // Test that impression events are sent for holdout decisions |
| 185 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 186 | + new UserAttributes { { "country", "us" } }); |
| 187 | + |
| 188 | + var decision = userContext.Decide("test_flag_1"); |
| 189 | + |
| 190 | + Assert.IsNotNull(decision, "Decision should not be null"); |
| 191 | + |
| 192 | + // Verify that event dispatcher was called |
| 193 | + // Note: With real bucketer, we can't guarantee holdout selection, |
| 194 | + // but we can verify event structure |
| 195 | + EventDispatcherMock.Verify( |
| 196 | + e => e.DispatchEvent(It.IsAny<LogEvent>()), |
| 197 | + Times.AtLeastOnce, |
| 198 | + "Event should be dispatched for decision" |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + [Test] |
| 203 | + public void TestDecide_HoldoutWithDecideOptions() |
| 204 | + { |
| 205 | + // Test decide options (like ExcludeVariables) with holdout decisions |
| 206 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 207 | + new UserAttributes { { "country", "us" } }); |
| 208 | + |
| 209 | + // Test with exclude variables option |
| 210 | + var decisionWithVariables = userContext.Decide("test_flag_1"); |
| 211 | + var decisionWithoutVariables = userContext.Decide("test_flag_1", |
| 212 | + new OptimizelyDecideOption[] { OptimizelyDecideOption.EXCLUDE_VARIABLES }); |
| 213 | + |
| 214 | + Assert.IsNotNull(decisionWithVariables, "Decision with variables should not be null"); |
| 215 | + Assert.IsNotNull(decisionWithoutVariables, "Decision without variables should not be null"); |
| 216 | + |
| 217 | + // When variables are excluded, the Variables object should be empty |
| 218 | + Assert.IsTrue(decisionWithoutVariables.Variables.ToDictionary().Count == 0, |
| 219 | + "Variables should be empty when excluded"); |
| 220 | + } |
| 221 | + |
| 222 | + [Test] |
| 223 | + public void TestDecide_HoldoutWithAudienceTargeting() |
| 224 | + { |
| 225 | + // Test holdout decisions with different user attributes for audience targeting |
| 226 | + var featureFlag = Config.FeatureKeyMap["test_flag_1"]; |
| 227 | + Assert.IsNotNull(featureFlag, "Feature flag should exist"); |
| 228 | + |
| 229 | + // Test with matching attributes |
| 230 | + var userContextMatch = OptimizelyInstance.CreateUserContext(TestUserId, |
| 231 | + new UserAttributes { { "country", "us" } }); |
| 232 | + var decisionMatch = userContextMatch.Decide("test_flag_1"); |
| 233 | + |
| 234 | + // Test with non-matching attributes |
| 235 | + var userContextNoMatch = OptimizelyInstance.CreateUserContext(TestUserId, |
| 236 | + new UserAttributes { { "country", "ca" } }); |
| 237 | + var decisionNoMatch = userContextNoMatch.Decide("test_flag_1"); |
| 238 | + |
| 239 | + Assert.IsNotNull(decisionMatch, "Decision with matching attributes should not be null"); |
| 240 | + Assert.IsNotNull(decisionNoMatch, "Decision with non-matching attributes should not be null"); |
| 241 | + |
| 242 | + // Both decisions should have proper structure regardless of targeting |
| 243 | + Assert.AreEqual("test_flag_1", decisionMatch.FlagKey, "Flag key should match"); |
| 244 | + Assert.AreEqual("test_flag_1", decisionNoMatch.FlagKey, "Flag key should match"); |
| 245 | + } |
| 246 | + |
| 247 | + [Test] |
| 248 | + public void TestDecide_InactiveHoldout() |
| 249 | + { |
| 250 | + // Test decide when holdout is not running |
| 251 | + var featureFlag = Config.FeatureKeyMap["test_flag_1"]; |
| 252 | + Assert.IsNotNull(featureFlag, "Feature flag should exist"); |
| 253 | + |
| 254 | + // Find a holdout and set it to inactive |
| 255 | + var holdout = Config.Holdouts.FirstOrDefault(); |
| 256 | + if (holdout != null) |
| 257 | + { |
| 258 | + var originalStatus = holdout.Status; |
| 259 | + holdout.Status = "Paused"; // Make holdout inactive |
| 260 | + |
| 261 | + try |
| 262 | + { |
| 263 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 264 | + new UserAttributes { { "country", "us" } }); |
| 265 | + |
| 266 | + var decision = userContext.Decide("test_flag_1"); |
| 267 | + |
| 268 | + Assert.IsNotNull(decision, "Decision should not be null even with inactive holdout"); |
| 269 | + Assert.AreEqual("test_flag_1", decision.FlagKey, "Flag key should match"); |
| 270 | + |
| 271 | + // Should not get decision from the inactive holdout |
| 272 | + if (!string.IsNullOrEmpty(decision.RuleKey)) |
| 273 | + { |
| 274 | + Assert.AreNotEqual(holdout.Key, decision.RuleKey, |
| 275 | + "Decision should not come from inactive holdout"); |
| 276 | + } |
| 277 | + } |
| 278 | + finally |
| 279 | + { |
| 280 | + holdout.Status = originalStatus; // Restore original status |
| 281 | + } |
| 282 | + } |
| 283 | + else |
| 284 | + { |
| 285 | + Assert.Inconclusive("No holdout found to test inactive scenario"); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + [Test] |
| 290 | + public void TestDecide_EmptyUserId() |
| 291 | + { |
| 292 | + // Test decide with empty user ID (should still work per Swift SDK behavior) |
| 293 | + var userContext = OptimizelyInstance.CreateUserContext("", |
| 294 | + new UserAttributes { { "country", "us" } }); |
| 295 | + |
| 296 | + var decision = userContext.Decide("test_flag_1"); |
| 297 | + |
| 298 | + Assert.IsNotNull(decision, "Decision should not be null with empty user ID"); |
| 299 | + Assert.AreEqual("test_flag_1", decision.FlagKey, "Flag key should match"); |
| 300 | + |
| 301 | + // Should not log error about invalid user ID since empty string is valid for bucketing |
| 302 | + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, |
| 303 | + It.Is<string>(s => s.Contains("User ID") && (s.Contains("null") || s.Contains("empty")))), |
| 304 | + Times.Never); |
| 305 | + } |
| 306 | + |
| 307 | + [Test] |
| 308 | + public void TestDecide_WithDecisionReasons() |
| 309 | + { |
| 310 | + // Test that decision reasons are properly populated for holdout decisions |
| 311 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 312 | + new UserAttributes { { "country", "us" } }); |
| 313 | + |
| 314 | + var decision = userContext.Decide("test_flag_1", |
| 315 | + new OptimizelyDecideOption[] { OptimizelyDecideOption.INCLUDE_REASONS }); |
| 316 | + |
| 317 | + Assert.IsNotNull(decision, "Decision should not be null"); |
| 318 | + Assert.AreEqual("test_flag_1", decision.FlagKey, "Flag key should match"); |
| 319 | + |
| 320 | + // Decision reasons should be populated when requested |
| 321 | + Assert.IsNotNull(decision.Reasons, "Decision reasons should not be null"); |
| 322 | + // With real bucketer, we expect some decision reasons to be generated |
| 323 | + Assert.IsTrue(decision.Reasons.Length >= 0, "Decision reasons should be present"); |
| 324 | + } |
| 325 | + |
| 326 | + [Test] |
| 327 | + public void TestDecide_HoldoutPriority() |
| 328 | + { |
| 329 | + // Test holdout evaluation priority (global vs included vs excluded) |
| 330 | + var featureFlag = Config.FeatureKeyMap["test_flag_1"]; |
| 331 | + Assert.IsNotNull(featureFlag, "Feature flag should exist"); |
| 332 | + |
| 333 | + // Check if we have multiple holdouts |
| 334 | + var globalHoldouts = Config.Holdouts.Where(h => |
| 335 | + h.IncludedFlags == null || h.IncludedFlags.Length == 0).ToList(); |
| 336 | + var includedHoldouts = Config.Holdouts.Where(h => |
| 337 | + h.IncludedFlags != null && h.IncludedFlags.Contains(featureFlag.Id)).ToList(); |
| 338 | + |
| 339 | + if (globalHoldouts.Count > 0 || includedHoldouts.Count > 0) |
| 340 | + { |
| 341 | + var userContext = OptimizelyInstance.CreateUserContext(TestUserId, |
| 342 | + new UserAttributes { { "country", "us" } }); |
| 343 | + |
| 344 | + var decision = userContext.Decide("test_flag_1"); |
| 345 | + |
| 346 | + Assert.IsNotNull(decision, "Decision should not be null"); |
| 347 | + Assert.AreEqual("test_flag_1", decision.FlagKey, "Flag key should match"); |
| 348 | + |
| 349 | + // Decision should be valid regardless of which holdout is selected |
| 350 | + Assert.IsTrue(decision.VariationKey != null || decision.VariationKey == null, |
| 351 | + "Decision should have valid structure"); |
| 352 | + } |
| 353 | + else |
| 354 | + { |
| 355 | + Assert.Inconclusive("No holdouts found to test priority"); |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + |
| 360 | + } |
| 361 | +} |
0 commit comments