|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "maps" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + "github.com/google/jsonschema-go/jsonschema" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAffectedDispatchersReportSupportedMethods(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + tool func() inventory.ServerTool |
| 23 | + requestArg map[string]any |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "pull request read", |
| 27 | + tool: func() inventory.ServerTool { |
| 28 | + return PullRequestRead(translations.NullTranslationHelper) |
| 29 | + }, |
| 30 | + requestArg: map[string]any{ |
| 31 | + "owner": "owner", |
| 32 | + "repo": "repo", |
| 33 | + "pullNumber": float64(1), |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "issue read", |
| 38 | + tool: func() inventory.ServerTool { |
| 39 | + return IssueRead(translations.NullTranslationHelper) |
| 40 | + }, |
| 41 | + requestArg: map[string]any{ |
| 42 | + "owner": "owner", |
| 43 | + "repo": "repo", |
| 44 | + "issue_number": float64(1), |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "sub issue write", |
| 49 | + tool: func() inventory.ServerTool { |
| 50 | + return SubIssueWrite(translations.NullTranslationHelper) |
| 51 | + }, |
| 52 | + requestArg: map[string]any{ |
| 53 | + "owner": "owner", |
| 54 | + "repo": "repo", |
| 55 | + "issue_number": float64(1), |
| 56 | + "sub_issue_id": float64(2), |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "actions list", |
| 61 | + tool: func() inventory.ServerTool { |
| 62 | + return ActionsList(translations.NullTranslationHelper) |
| 63 | + }, |
| 64 | + requestArg: map[string]any{ |
| 65 | + "owner": "owner", |
| 66 | + "repo": "repo", |
| 67 | + "resource_id": "1", |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "actions get", |
| 72 | + tool: func() inventory.ServerTool { |
| 73 | + return ActionsGet(translations.NullTranslationHelper) |
| 74 | + }, |
| 75 | + requestArg: map[string]any{ |
| 76 | + "owner": "owner", |
| 77 | + "repo": "repo", |
| 78 | + "resource_id": "1", |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "actions run", |
| 83 | + tool: func() inventory.ServerTool { |
| 84 | + return ActionsRunTrigger(translations.NullTranslationHelper) |
| 85 | + }, |
| 86 | + requestArg: map[string]any{ |
| 87 | + "owner": "owner", |
| 88 | + "repo": "repo", |
| 89 | + "run_id": float64(1), |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "projects list", |
| 94 | + tool: func() inventory.ServerTool { |
| 95 | + return ProjectsList(translations.NullTranslationHelper) |
| 96 | + }, |
| 97 | + requestArg: map[string]any{ |
| 98 | + "owner": "owner", |
| 99 | + "owner_type": "org", |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "projects get", |
| 104 | + tool: func() inventory.ServerTool { |
| 105 | + return ProjectsGet(translations.NullTranslationHelper) |
| 106 | + }, |
| 107 | + requestArg: map[string]any{ |
| 108 | + "owner": "owner", |
| 109 | + "owner_type": "org", |
| 110 | + "project_number": float64(1), |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "projects write", |
| 115 | + tool: func() inventory.ServerTool { |
| 116 | + return ProjectsWrite(translations.NullTranslationHelper) |
| 117 | + }, |
| 118 | + requestArg: map[string]any{ |
| 119 | + "owner": "owner", |
| 120 | + "owner_type": "org", |
| 121 | + "project_number": float64(1), |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "ui get", |
| 126 | + tool: func() inventory.ServerTool { |
| 127 | + return UIGet(translations.NullTranslationHelper) |
| 128 | + }, |
| 129 | + requestArg: map[string]any{ |
| 130 | + "owner": "owner", |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for _, tc := range tests { |
| 136 | + t.Run(tc.name, func(t *testing.T) { |
| 137 | + tool := tc.tool() |
| 138 | + schema := tool.Tool.InputSchema.(*jsonschema.Schema) |
| 139 | + methodSchema := schema.Properties["method"] |
| 140 | + require.NotNil(t, methodSchema) |
| 141 | + require.NotEmpty(t, methodSchema.Enum) |
| 142 | + |
| 143 | + methods := make([]string, len(methodSchema.Enum)) |
| 144 | + for i, method := range methodSchema.Enum { |
| 145 | + methods[i] = method.(string) |
| 146 | + } |
| 147 | + |
| 148 | + args := make(map[string]any, len(tc.requestArg)+1) |
| 149 | + maps.Copy(args, tc.requestArg) |
| 150 | + args["method"] = "unknown_method" |
| 151 | + |
| 152 | + client := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})) |
| 153 | + deps := BaseDeps{Client: client, GQLClient: defaultGQLClient} |
| 154 | + request := createMCPRequest(args) |
| 155 | + result, err := tool.Handler(deps)(ContextWithDeps(context.Background(), deps), &request) |
| 156 | + |
| 157 | + require.NoError(t, err) |
| 158 | + require.True(t, result.IsError) |
| 159 | + assert.Equal(t, |
| 160 | + "unknown method: unknown_method. Supported methods are: "+strings.Join(methods, ", "), |
| 161 | + getErrorResult(t, result).Text, |
| 162 | + ) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestPullRequestReviewWriteMissingMethodIsRequired(t *testing.T) { |
| 168 | + t.Parallel() |
| 169 | + |
| 170 | + tool := PullRequestReviewWrite(translations.NullTranslationHelper) |
| 171 | + deps := BaseDeps{GQLClient: defaultGQLClient} |
| 172 | + request := createMCPRequest(map[string]any{ |
| 173 | + "owner": "owner", |
| 174 | + "repo": "repo", |
| 175 | + "pullNumber": float64(1), |
| 176 | + }) |
| 177 | + |
| 178 | + result, err := tool.Handler(deps)(ContextWithDeps(context.Background(), deps), &request) |
| 179 | + |
| 180 | + require.NoError(t, err) |
| 181 | + require.True(t, result.IsError) |
| 182 | + assert.Equal(t, "missing required parameter: method", getErrorResult(t, result).Text) |
| 183 | +} |
0 commit comments