@@ -29,7 +29,6 @@ import (
29
29
"github.com/stretchr/testify/mock"
30
30
"github.com/stretchr/testify/suite"
31
31
32
- "github.com/optimizely/go-sdk/v2/pkg/cmab"
33
32
"github.com/optimizely/go-sdk/v2/pkg/config"
34
33
"github.com/optimizely/go-sdk/v2/pkg/decide"
35
34
"github.com/optimizely/go-sdk/v2/pkg/decision"
@@ -3187,153 +3186,34 @@ func (s *ClientTestSuiteTrackNotification) TestRemoveOnTrackThrowsErrorWhenRemov
3187
3186
mockNotificationCenter .AssertExpectations (s .T ())
3188
3187
}
3189
3188
3190
- // MockCmabService for testing CMAB functionality
3191
- type MockCmabService struct {
3192
- mock.Mock
3193
- }
3194
-
3195
- // GetDecision safely implements the cmab.Service interface
3196
- func (m * MockCmabService ) GetDecision (projectConfig config.ProjectConfig , userContext entities.UserContext , ruleID string , options * decide.Options ) (cmab.Decision , error ) {
3197
- args := m .Called (projectConfig , userContext , ruleID , options )
3198
-
3199
- // IMPORTANT: Return a valid Decision struct with non-nil Reasons slice
3200
- decision , ok := args .Get (0 ).(cmab.Decision )
3201
- if ! ok {
3202
- // If conversion fails, return a safe default
3203
- return cmab.Decision {Reasons : []string {"Mock conversion failed" }}, args .Error (1 )
3189
+ func TestOptimizelyClient_handleDecisionServiceError (t * testing.T ) {
3190
+ // Create the client
3191
+ client := & OptimizelyClient {
3192
+ logger : logging .GetLogger ("" , "" ),
3204
3193
}
3205
3194
3206
- // Make sure Reasons is never nil
3207
- if decision .Reasons == nil {
3208
- decision .Reasons = []string {}
3209
- }
3210
-
3211
- return decision , args .Error (1 )
3212
- }
3195
+ // Create a CMAB error
3196
+ cmabErrorMessage := "Failed to fetch CMAB data for experiment exp_1."
3197
+ cmabError := fmt .Errorf (cmabErrorMessage )
3213
3198
3214
- func TestDecide_CmabSuccess (t * testing.T ) {
3215
- // Use the existing Mock types
3216
- mockConfig := new (MockProjectConfig )
3217
- mockConfigManager := new (MockProjectConfigManager )
3218
- mockEventProcessor := new (MockProcessor )
3219
- mockCmabService := new (MockCmabService )
3220
- mockDecisionService := new (MockDecisionService )
3221
- mockNotificationCenter := new (MockNotificationCenter )
3222
-
3223
- // Test data
3224
- featureKey := "test_feature"
3225
- experimentID := "exp_1"
3226
- variationID := "var_1"
3227
-
3228
- // Create feature with experiment IDs
3229
- testFeature := entities.Feature {
3230
- Key : featureKey ,
3231
- ExperimentIDs : []string {experimentID },
3199
+ // Create a user context - needs to match the signature expected by handleDecisionServiceError
3200
+ testUserContext := OptimizelyUserContext {
3201
+ UserID : "test_user" ,
3202
+ Attributes : map [string ]interface {}{},
3232
3203
}
3233
3204
3234
- // Create variation
3235
- testVariation := entities.Variation {
3236
- ID : variationID ,
3237
- Key : "variation_1" ,
3238
- FeatureEnabled : true ,
3239
- }
3240
-
3241
- // Create experiment with CMAB data
3242
- testExperiment := entities.Experiment {
3243
- ID : experimentID ,
3244
- Key : "exp_key" ,
3245
- Cmab : & entities.Cmab {
3246
- TrafficAllocation : 10000 ,
3247
- },
3248
- Variations : map [string ]entities.Variation {
3249
- variationID : testVariation ,
3250
- },
3251
- }
3252
-
3253
- // Mock GetConfig call
3254
- mockConfigManager .On ("GetConfig" ).Return (mockConfig , nil )
3255
-
3256
- // Log and track calls to GetExperimentByID
3257
- experimentCalls := make ([]string , 0 )
3258
- mockConfig .On ("GetExperimentByID" , mock .Anything ).Return (testExperiment , nil ).Run (
3259
- func (args mock.Arguments ) {
3260
- id := args .Get (0 ).(string )
3261
- experimentCalls = append (experimentCalls , id )
3262
- t .Logf ("GetExperimentByID called with: %s" , id )
3263
- })
3264
-
3265
- // Mock GetFeatureByKey
3266
- mockConfig .On ("GetFeatureByKey" , featureKey ).Return (testFeature , nil )
3267
-
3268
- // Track calls to CMAB service
3269
- cmabCalls := make ([]string , 0 )
3270
- mockCmabService .On ("GetDecision" , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).
3271
- Return (cmab.Decision {VariationID : variationID , CmabUUID : "uuid" }, nil ).
3272
- Run (func (args mock.Arguments ) {
3273
- id := args .Get (2 ).(string )
3274
- cmabCalls = append (cmabCalls , id )
3275
- t .Logf ("GetDecision called with id: %s" , id )
3276
- })
3277
-
3278
- // Mock event processor
3279
- mockEventProcessor .On ("ProcessEvent" , mock .Anything ).Return (true )
3280
-
3281
- // Mock notification center
3282
- mockNotificationCenter .On ("Send" , notification .Decision , mock .Anything ).Return (nil )
3283
-
3284
- // Let's add every field to client to be sure
3285
- client := OptimizelyClient {
3286
- ConfigManager : mockConfigManager ,
3287
- DecisionService : mockDecisionService ,
3288
- EventProcessor : mockEventProcessor ,
3289
- notificationCenter : mockNotificationCenter ,
3290
- cmabService : mockCmabService ,
3291
- logger : logging .GetLogger ("debug" , "TestCMAB" ),
3292
- ctx : context .Background (),
3293
- tracer : & MockTracer {},
3294
- defaultDecideOptions : & decide.Options {},
3295
- }
3296
-
3297
- // Create user context
3298
- userContext := client .CreateUserContext ("test_user" , nil )
3299
-
3300
- // Wrap the call in a panic handler
3301
- var decision OptimizelyDecision
3302
- var panicOccurred bool
3303
- var panicValue interface {}
3304
-
3305
- func () {
3306
- defer func () {
3307
- if r := recover (); r != nil {
3308
- panicOccurred = true
3309
- panicValue = r
3310
- t .Logf ("Panic occurred: %v" , r )
3311
- }
3312
- }()
3313
- decision = client .decide (& userContext , featureKey , nil )
3314
- }()
3315
-
3316
- t .Logf ("Panic occurred: %v" , panicOccurred )
3317
- if panicOccurred {
3318
- t .Logf ("Panic value: %v" , panicValue )
3319
- }
3320
- t .Logf ("GetExperimentByID calls: %v" , experimentCalls )
3321
- t .Logf ("GetDecision calls: %v" , cmabCalls )
3322
- t .Logf ("Decision: %+v" , decision )
3205
+ // Call the error handler directly
3206
+ decision := client .handleDecisionServiceError (cmabError , "test_flag" , testUserContext )
3323
3207
3324
- // Skip further assertions if we panicked
3325
- if panicOccurred {
3326
- t . Log ( "Test skipping assertions due to panic" )
3327
- return
3328
- }
3208
+ // Verify the decision is correctly formatted
3209
+ assert . False ( t , decision . Enabled )
3210
+ assert . Equal ( t , "" , decision . VariationKey ) // Should be empty string, not nil
3211
+ assert . Equal ( t , "" , decision . RuleKey ) // Should be empty string, not nil
3212
+ assert . Contains ( t , decision . Reasons , cmabErrorMessage )
3329
3213
3330
- // Basic assertions on the decision
3331
- if len (cmabCalls ) > 0 {
3332
- assert .Equal (t , featureKey , decision .FlagKey )
3333
- assert .Equal (t , "variation_1" , decision .VariationKey )
3334
- assert .Equal (t , "exp_key" , decision .RuleKey )
3335
- assert .True (t , decision .Enabled )
3336
- }
3214
+ // Check that reasons contains exactly the expected message
3215
+ assert .Equal (t , 1 , len (decision .Reasons ), "Reasons array should have exactly one item" )
3216
+ assert .Equal (t , cmabErrorMessage , decision .Reasons [0 ], "Error message should be added verbatim" )
3337
3217
}
3338
3218
3339
3219
func TestClientTestSuiteAB (t * testing.T ) {
0 commit comments