|
| 1 | +package github_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/github/github-mcp-server/pkg/github" |
| 9 | + "github.com/github/github-mcp-server/pkg/translations" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestIsFeatureEnabled_WithEnabledFlag(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + // Create a feature checker that returns true for "test_flag" |
| 17 | + checker := func(_ context.Context, flagName string) (bool, error) { |
| 18 | + return flagName == "test_flag", nil |
| 19 | + } |
| 20 | + |
| 21 | + // Create deps with the checker using NewBaseDeps |
| 22 | + deps := github.NewBaseDeps( |
| 23 | + nil, // client |
| 24 | + nil, // gqlClient |
| 25 | + nil, // rawClient |
| 26 | + nil, // repoAccessCache |
| 27 | + translations.NullTranslationHelper, |
| 28 | + github.FeatureFlags{}, |
| 29 | + 0, // contentWindowSize |
| 30 | + checker, // featureChecker |
| 31 | + ) |
| 32 | + |
| 33 | + // Test enabled flag |
| 34 | + result := deps.IsFeatureEnabled(context.Background(), "test_flag") |
| 35 | + assert.True(t, result, "Expected test_flag to be enabled") |
| 36 | + |
| 37 | + // Test disabled flag |
| 38 | + result = deps.IsFeatureEnabled(context.Background(), "other_flag") |
| 39 | + assert.False(t, result, "Expected other_flag to be disabled") |
| 40 | +} |
| 41 | + |
| 42 | +func TestIsFeatureEnabled_WithoutChecker(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + // Create deps without feature checker (nil) |
| 46 | + deps := github.NewBaseDeps( |
| 47 | + nil, // client |
| 48 | + nil, // gqlClient |
| 49 | + nil, // rawClient |
| 50 | + nil, // repoAccessCache |
| 51 | + translations.NullTranslationHelper, |
| 52 | + github.FeatureFlags{}, |
| 53 | + 0, // contentWindowSize |
| 54 | + nil, // featureChecker (nil) |
| 55 | + ) |
| 56 | + |
| 57 | + // Should return false when checker is nil |
| 58 | + result := deps.IsFeatureEnabled(context.Background(), "any_flag") |
| 59 | + assert.False(t, result, "Expected false when checker is nil") |
| 60 | +} |
| 61 | + |
| 62 | +func TestIsFeatureEnabled_EmptyFlagName(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + |
| 65 | + // Create a feature checker |
| 66 | + checker := func(_ context.Context, _ string) (bool, error) { |
| 67 | + return true, nil |
| 68 | + } |
| 69 | + |
| 70 | + deps := github.NewBaseDeps( |
| 71 | + nil, // client |
| 72 | + nil, // gqlClient |
| 73 | + nil, // rawClient |
| 74 | + nil, // repoAccessCache |
| 75 | + translations.NullTranslationHelper, |
| 76 | + github.FeatureFlags{}, |
| 77 | + 0, // contentWindowSize |
| 78 | + checker, // featureChecker |
| 79 | + ) |
| 80 | + |
| 81 | + // Should return false for empty flag name |
| 82 | + result := deps.IsFeatureEnabled(context.Background(), "") |
| 83 | + assert.False(t, result, "Expected false for empty flag name") |
| 84 | +} |
| 85 | + |
| 86 | +func TestIsFeatureEnabled_CheckerError(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + |
| 89 | + // Create a feature checker that returns an error |
| 90 | + checker := func(_ context.Context, _ string) (bool, error) { |
| 91 | + return false, errors.New("checker error") |
| 92 | + } |
| 93 | + |
| 94 | + deps := github.NewBaseDeps( |
| 95 | + nil, // client |
| 96 | + nil, // gqlClient |
| 97 | + nil, // rawClient |
| 98 | + nil, // repoAccessCache |
| 99 | + translations.NullTranslationHelper, |
| 100 | + github.FeatureFlags{}, |
| 101 | + 0, // contentWindowSize |
| 102 | + checker, // featureChecker |
| 103 | + ) |
| 104 | + |
| 105 | + // Should return false and log error (not crash) |
| 106 | + result := deps.IsFeatureEnabled(context.Background(), "error_flag") |
| 107 | + assert.False(t, result, "Expected false when checker returns error") |
| 108 | +} |
0 commit comments