-
Notifications
You must be signed in to change notification settings - Fork 4
fix(go): Better error messaging for sticky rules #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
33ecd5f
fix(go): Better error messaging for sticky rules
fabriziodemaria afe639e
refactor: test renaming
fabriziodemaria a1e6ddd
fix(go): remove non sticky resolve api
fabriziodemaria 9c251ed
fix(go): test missing materializations
fabriziodemaria 690aabb
refactor(go): clean up test suite for sticky rules
fabriziodemaria 0d22f37
refactor(go): more test cleanups
fabriziodemaria 5721f2c
test(go): more sticky tests
fabriziodemaria 6015165
test(go): remove non relevant tests
fabriziodemaria e0248e3
test(go): improve test more
fabriziodemaria 157bdcc
chore: apply suggestions from code review
nicklasl 2e58bfe
format(go): formatting
fabriziodemaria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
openfeature-provider/go/confidence/local_resolver_provider_resolve_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| package confidence | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/open-feature/go-sdk/openfeature" | ||
| adminv1 "github.com/spotify/confidence-resolver/openfeature-provider/go/confidence/proto/confidence/flags/admin/v1" | ||
| iamv1 "github.com/spotify/confidence-resolver/openfeature-provider/go/confidence/proto/confidence/iam/v1" | ||
| "github.com/tetratelabs/wazero" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| func TestLocalResolverProvider_ReturnsDefaultOnError(t *testing.T) { | ||
| ctx := context.Background() | ||
| runtime := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig()) | ||
| defer runtime.Close(ctx) | ||
|
|
||
| // Create minimal state with wrong client secret | ||
| state := &adminv1.ResolverState{ | ||
| Flags: []*adminv1.Flag{}, | ||
| ClientCredentials: []*iamv1.ClientCredential{ | ||
| { | ||
| Credential: &iamv1.ClientCredential_ClientSecret_{ | ||
| ClientSecret: &iamv1.ClientCredential_ClientSecret{ | ||
| Secret: "wrong-secret", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| stateBytes, _ := proto.Marshal(state) | ||
|
|
||
| flagLogger := NewNoOpWasmFlagLogger() | ||
| swap, err := NewSwapWasmResolverApi(ctx, runtime, defaultWasmBytes, flagLogger, stateBytes, "test-account") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create SwapWasmResolverApi: %v", err) | ||
| } | ||
| defer swap.Close(ctx) | ||
|
|
||
| factory := &LocalResolverFactory{ | ||
| resolverAPI: swap, | ||
| } | ||
|
|
||
| // Use different client secret that won't match | ||
| provider := NewLocalResolverProvider(factory, "test-secret") | ||
|
|
||
| evalCtx := openfeature.FlattenedContext{ | ||
| "user_id": "test-user", | ||
| } | ||
|
|
||
| t.Run("StringEvaluation returns default on error", func(t *testing.T) { | ||
| defaultValue := "default-value" | ||
| result := provider.StringEvaluation(ctx, "non-existent-flag.field", defaultValue, evalCtx) | ||
|
|
||
| if result.Value != defaultValue { | ||
| t.Errorf("Expected default value %v, got %v", defaultValue, result.Value) | ||
| } | ||
|
|
||
| if result.Reason != openfeature.ErrorReason { | ||
| t.Errorf("Expected ErrorReason, got %v", result.Reason) | ||
| } | ||
|
|
||
| if result.ResolutionError.Error() == "" { | ||
| t.Error("Expected ResolutionError to not be empty") | ||
| } | ||
|
|
||
| t.Logf("✓ StringEvaluation correctly returned default value: %s", defaultValue) | ||
| }) | ||
| } | ||
|
|
||
| func TestLocalResolverProvider_ReturnsCorrectValue(t *testing.T) { | ||
| ctx := context.Background() | ||
| runtime := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig()) | ||
| defer runtime.Close(ctx) | ||
|
|
||
| // Load real test state | ||
| testState := loadTestResolverState(t) | ||
| testAcctID := loadTestAccountID(t) | ||
|
|
||
| flagLogger := NewNoOpWasmFlagLogger() | ||
| swap, err := NewSwapWasmResolverApi(ctx, runtime, defaultWasmBytes, flagLogger, testState, testAcctID) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create SwapWasmResolverApi: %v", err) | ||
| } | ||
| defer swap.Close(ctx) | ||
|
|
||
| factory := &LocalResolverFactory{ | ||
| resolverAPI: swap, | ||
| } | ||
|
|
||
| // Use the correct client secret from test data | ||
| provider := NewLocalResolverProvider(factory, "mkjJruAATQWjeY7foFIWfVAcBWnci2YF") | ||
|
|
||
| evalCtx := openfeature.FlattenedContext{ | ||
| "visitor_id": "tutorial_visitor", | ||
| } | ||
|
|
||
| t.Run("StringEvaluation returns correct variant value", func(t *testing.T) { | ||
| defaultValue := "default-message" | ||
| result := provider.StringEvaluation(ctx, "tutorial-feature.message", defaultValue, evalCtx) | ||
|
|
||
| // The exciting-welcome variant has a specific message | ||
| expectedMessage := "We are very excited to welcome you to Confidence! This is a message from the tutorial flag." | ||
|
|
||
| if result.Value != expectedMessage { | ||
| t.Errorf("Expected value '%s', got '%s'", expectedMessage, result.Value) | ||
| } | ||
|
|
||
| if result.Reason != openfeature.TargetingMatchReason { | ||
| t.Errorf("Expected TargetingMatchReason, got %v", result.Reason) | ||
| } | ||
|
|
||
| if result.ResolutionError.Error() != "" { | ||
| t.Errorf("Expected no error, got %v", result.ResolutionError) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("ObjectEvaluation returns correct variant structure", func(t *testing.T) { | ||
| defaultValue := map[string]interface{}{ | ||
| "message": "default", | ||
| "title": "default", | ||
| } | ||
| result := provider.ObjectEvaluation(ctx, "tutorial-feature", defaultValue, evalCtx) | ||
|
|
||
| if result.Value == nil { | ||
| t.Fatal("Expected result value to not be nil") | ||
| } | ||
|
|
||
| resultMap, ok := result.Value.(map[string]interface{}) | ||
| if !ok { | ||
| t.Fatalf("Expected result value to be a map, got %T", result.Value) | ||
| } | ||
|
|
||
| expectedMessage := "We are very excited to welcome you to Confidence! This is a message from the tutorial flag." | ||
| expectedTitle := "Welcome to Confidence!" | ||
|
|
||
| if resultMap["message"] != expectedMessage { | ||
| t.Errorf("Expected message '%s', got '%v'", expectedMessage, resultMap["message"]) | ||
| } | ||
|
|
||
| if resultMap["title"] != expectedTitle { | ||
| t.Errorf("Expected title '%s', got '%v'", expectedTitle, resultMap["title"]) | ||
| } | ||
|
|
||
| if result.Reason != openfeature.TargetingMatchReason { | ||
| t.Errorf("Expected TargetingMatchReason, got %v", result.Reason) | ||
| } | ||
|
|
||
| if result.ResolutionError.Error() != "" { | ||
| t.Errorf("Expected no error, got %v", result.ResolutionError) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestLocalResolverProvider_MissingMaterializations(t *testing.T) { | ||
| ctx := context.Background() | ||
| runtime := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig()) | ||
| defer runtime.Close(ctx) | ||
|
|
||
| t.Run("Provider returns resolved value for flag without sticky rules", func(t *testing.T) { | ||
| // Load real test state | ||
| testState := loadTestResolverState(t) | ||
| testAcctID := loadTestAccountID(t) | ||
|
|
||
| flagLogger := NewNoOpWasmFlagLogger() | ||
| swap, err := NewSwapWasmResolverApi(ctx, runtime, defaultWasmBytes, flagLogger, testState, testAcctID) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create SwapWasmResolverApi: %v", err) | ||
| } | ||
| defer swap.Close(ctx) | ||
|
|
||
| factory := &LocalResolverFactory{ | ||
| resolverAPI: swap, | ||
| } | ||
|
|
||
| provider := NewLocalResolverProvider(factory, "mkjJruAATQWjeY7foFIWfVAcBWnci2YF") | ||
|
|
||
| evalCtx := openfeature.FlattenedContext{ | ||
| "visitor_id": "tutorial_visitor", | ||
| } | ||
|
|
||
| // The tutorial-feature flag in the test data doesn't have materialization requirements | ||
| // so resolving with empty materializations should succeed | ||
| defaultValue := "default" | ||
| result := provider.StringEvaluation(ctx, "tutorial-feature.message", defaultValue, evalCtx) | ||
|
|
||
| if result.ResolutionError.Error() != "" { | ||
| t.Errorf("Expected successful resolve for flag without sticky rules, got error: %v", result.ResolutionError) | ||
| } | ||
|
|
||
| if result.Value == defaultValue { | ||
| t.Error("Expected resolved value, got default value") | ||
| } | ||
|
|
||
| if result.Reason != openfeature.TargetingMatchReason { | ||
| t.Errorf("Expected TargetingMatchReason, got %v", result.Reason) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Provider returns missing materializations error message", func(t *testing.T) { | ||
| // Create state with a flag that requires materializations | ||
| stickyState := createStateWithStickyFlag() | ||
| accountId := "test-account" | ||
|
|
||
| flagLogger := NewNoOpWasmFlagLogger() | ||
| swap, err := NewSwapWasmResolverApi(ctx, runtime, defaultWasmBytes, flagLogger, stickyState, accountId) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create SwapWasmResolverApi: %v", err) | ||
| } | ||
| defer swap.Close(ctx) | ||
|
|
||
| factory := &LocalResolverFactory{ | ||
| resolverAPI: swap, | ||
| } | ||
|
|
||
| provider := NewLocalResolverProvider(factory, "test-secret") | ||
|
|
||
| evalCtx := openfeature.FlattenedContext{ | ||
| "user_id": "test-user-123", | ||
| } | ||
|
|
||
| defaultValue := false | ||
| result := provider.BooleanEvaluation(ctx, "sticky-test-flag.enabled", defaultValue, evalCtx) | ||
|
|
||
| if result.Value != defaultValue { | ||
| t.Errorf("Expected default value %v when materializations missing, got %v", defaultValue, result.Value) | ||
| } | ||
|
|
||
| if result.Reason != openfeature.ErrorReason { | ||
| t.Errorf("Expected ErrorReason when materializations missing, got %v", result.Reason) | ||
| } | ||
|
|
||
| if result.ResolutionError.Error() == "" { | ||
| t.Error("Expected ResolutionError when materializations missing") | ||
| } | ||
|
|
||
| expectedErrorMsg := "missing materializations" | ||
| if result.ResolutionError.Error() != "GENERAL: missing materializations" { | ||
| t.Errorf("Expected error message 'GENERAL: %s', got: %v", expectedErrorMsg, result.ResolutionError) | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.