|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/github/github-mcp-server/pkg/translations" |
| 8 | + "github.com/google/jsonschema-go/jsonschema" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// Test_FieldsParamVariants_MutuallyExclusive guards the dual-variant |
| 14 | +// registration for the fields_param feature flag. The flag-enabled tools |
| 15 | +// (search_code, get_file_contents) and their Legacy* counterparts share a tool |
| 16 | +// name, so exactly one of each pair must survive inventory filtering for any |
| 17 | +// flag state. If both ever leaked, a client could be offered two tools with the |
| 18 | +// same name. This asserts that each gated tool is present exactly once, |
| 19 | +// advertising the `fields` parameter only when fields_param is enabled. |
| 20 | +func Test_FieldsParamVariants_MutuallyExclusive(t *testing.T) { |
| 21 | + gatedTools := []string{"search_code", "get_file_contents"} |
| 22 | + |
| 23 | + for _, tc := range []struct { |
| 24 | + name string |
| 25 | + flagEnabled bool |
| 26 | + expectFields bool |
| 27 | + featureChecks func(context.Context, string) (bool, error) |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "flag off registers the legacy variant without fields", |
| 31 | + flagEnabled: false, |
| 32 | + expectFields: false, |
| 33 | + featureChecks: featureCheckerFor(), // fields_param disabled |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "flag on registers the fields variant with fields", |
| 37 | + flagEnabled: true, |
| 38 | + expectFields: true, |
| 39 | + featureChecks: featureCheckerFor(FeatureFlagFieldsParam), |
| 40 | + }, |
| 41 | + } { |
| 42 | + t.Run(tc.name, func(t *testing.T) { |
| 43 | + inv, err := NewInventory(translations.NullTranslationHelper). |
| 44 | + WithToolsets([]string{"all"}). |
| 45 | + WithFeatureChecker(tc.featureChecks). |
| 46 | + Build() |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + available := inv.AvailableTools(context.Background()) |
| 50 | + |
| 51 | + counts := make(map[string]int, len(available)) |
| 52 | + for _, tool := range available { |
| 53 | + counts[tool.Tool.Name]++ |
| 54 | + } |
| 55 | + |
| 56 | + // Each gated tool must be present exactly once (never both variants) |
| 57 | + // and advertise `fields` only when the flag is enabled. |
| 58 | + for _, name := range gatedTools { |
| 59 | + require.Equalf(t, 1, counts[name], "expected exactly one %q for flagEnabled=%v; dual variants must be mutually exclusive", name, tc.flagEnabled) |
| 60 | + |
| 61 | + tool := requireToolByName(t, available, name) |
| 62 | + schema, ok := tool.Tool.InputSchema.(*jsonschema.Schema) |
| 63 | + require.Truef(t, ok, "%q InputSchema should be *jsonschema.Schema", name) |
| 64 | + |
| 65 | + if tc.expectFields { |
| 66 | + assert.Containsf(t, schema.Properties, "fields", "%q should advertise fields when flag is on", name) |
| 67 | + assert.Equalf(t, FeatureFlagFieldsParam, tool.FeatureFlagEnable, "%q should be the flag-enabled variant", name) |
| 68 | + } else { |
| 69 | + assert.NotContainsf(t, schema.Properties, "fields", "%q must not advertise fields when flag is off", name) |
| 70 | + assert.Containsf(t, tool.FeatureFlagDisable, FeatureFlagFieldsParam, "%q should be the legacy (flag-disabled) variant", name) |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
0 commit comments