|
17 | 17 | package decision |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "errors" |
20 | 21 | "testing" |
21 | 22 |
|
22 | 23 | "github.com/optimizely/go-sdk/v2/pkg/decide" |
@@ -230,6 +231,69 @@ func (s *FeatureExperimentServiceTestSuite) TestGetDecisionWithCmabUUID() { |
230 | 231 | s.mockExperimentService.AssertExpectations(s.T()) |
231 | 232 | } |
232 | 233 |
|
| 234 | +func (s *FeatureExperimentServiceTestSuite) TestGetDecisionWithCmabError() { |
| 235 | + testUserContext := entities.UserContext{ |
| 236 | + ID: "test_user_1", |
| 237 | + } |
| 238 | + |
| 239 | + // Create a NEW CMAB experiment (don't modify existing testExp1113) |
| 240 | + cmabExperiment := entities.Experiment{ |
| 241 | + ID: "cmab_experiment_id", |
| 242 | + Key: "cmab_experiment_key", |
| 243 | + Cmab: &entities.Cmab{ |
| 244 | + AttributeIds: []string{"attr1", "attr2"}, |
| 245 | + TrafficAllocation: 5000, // 50% |
| 246 | + }, |
| 247 | + Variations: testExp1113.Variations, // Reuse variations for simplicity |
| 248 | + } |
| 249 | + |
| 250 | + // Setup experiment decision context for CMAB experiment |
| 251 | + testExperimentDecisionContext := ExperimentDecisionContext{ |
| 252 | + Experiment: &cmabExperiment, |
| 253 | + ProjectConfig: s.mockConfig, |
| 254 | + } |
| 255 | + |
| 256 | + // Mock the experiment service to return a CMAB error |
| 257 | + cmabError := errors.New("Failed to fetch CMAB data for experiment cmab_experiment_key.") |
| 258 | + s.mockExperimentService.On("GetDecision", testExperimentDecisionContext, testUserContext, s.options). |
| 259 | + Return(ExperimentDecision{}, s.reasons, cmabError) |
| 260 | + |
| 261 | + // Create a test feature that uses our CMAB experiment |
| 262 | + testFeatureWithCmab := entities.Feature{ |
| 263 | + ID: "test_feature_cmab", |
| 264 | + Key: "test_feature_cmab_key", |
| 265 | + FeatureExperiments: []entities.Experiment{ |
| 266 | + cmabExperiment, // Only our CMAB experiment |
| 267 | + }, |
| 268 | + } |
| 269 | + |
| 270 | + // Create feature decision context with our CMAB feature |
| 271 | + testFeatureDecisionContextWithCmab := FeatureDecisionContext{ |
| 272 | + Feature: &testFeatureWithCmab, |
| 273 | + ProjectConfig: s.mockConfig, |
| 274 | + Variable: testVariable, |
| 275 | + ForcedDecisionService: NewForcedDecisionService("test_user"), |
| 276 | + } |
| 277 | + |
| 278 | + // Create service under test |
| 279 | + featureExperimentService := &FeatureExperimentService{ |
| 280 | + compositeExperimentService: s.mockExperimentService, |
| 281 | + logger: logging.GetLogger("sdkKey", "FeatureExperimentService"), |
| 282 | + } |
| 283 | + |
| 284 | + // Call GetDecision |
| 285 | + actualFeatureDecision, actualReasons, err := featureExperimentService.GetDecision(testFeatureDecisionContextWithCmab, testUserContext, s.options) |
| 286 | + |
| 287 | + // Verify that CMAB error results in empty feature decision (not error) |
| 288 | + s.NoError(err, "CMAB errors should not propagate as Go errors") |
| 289 | + s.Equal(FeatureDecision{}, actualFeatureDecision, "Should return empty FeatureDecision when CMAB fails") |
| 290 | + |
| 291 | + // Verify that reasons include the CMAB error (should be in actualReasons from mock) |
| 292 | + s.NotNil(actualReasons, "Decision reasons should not be nil") |
| 293 | + |
| 294 | + s.mockExperimentService.AssertExpectations(s.T()) |
| 295 | +} |
| 296 | + |
233 | 297 | func TestFeatureExperimentServiceTestSuite(t *testing.T) { |
234 | 298 | suite.Run(t, new(FeatureExperimentServiceTestSuite)) |
235 | 299 | } |
0 commit comments