|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/internal/toolsnaps" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + gh "github.com/google/go-github/v74/github" |
| 12 | + "github.com/migueleliasweb/go-github-mock/src/mock" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_ListProjects(t *testing.T) { |
| 18 | + // Verify tool definition and schema once |
| 19 | + mockClient := gh.NewClient(nil) |
| 20 | + tool, _ := ListProjects(stubGetClientFn(mockClient), translations.NullTranslationHelper) |
| 21 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 22 | + |
| 23 | + assert.Equal(t, "list_projects", tool.Name) |
| 24 | + assert.NotEmpty(t, tool.Description) |
| 25 | + assert.Contains(t, tool.InputSchema.Properties, "owner") |
| 26 | + assert.Contains(t, tool.InputSchema.Properties, "owner_type") |
| 27 | + assert.Contains(t, tool.InputSchema.Properties, "query") |
| 28 | + assert.Contains(t, tool.InputSchema.Properties, "before") |
| 29 | + assert.Contains(t, tool.InputSchema.Properties, "after") |
| 30 | + assert.Contains(t, tool.InputSchema.Properties, "per_page") |
| 31 | + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "owner_type"}) |
| 32 | + |
| 33 | + // Minimal project objects (fields chosen to likely exist on ProjectV2; test only asserts round-trip JSON array length) |
| 34 | + orgProjects := []map[string]any{{"id": 1, "title": "Org Project"}} |
| 35 | + userProjects := []map[string]any{{"id": 2, "title": "User Project"}} |
| 36 | + |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + mockedClient *http.Client |
| 40 | + requestArgs map[string]interface{} |
| 41 | + expectError bool |
| 42 | + expectedLength int |
| 43 | + expectedErrMsg string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "success organization", |
| 47 | + mockedClient: mock.NewMockedHTTPClient( |
| 48 | + mock.WithRequestMatchHandler( |
| 49 | + mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2", Method: http.MethodGet}, |
| 50 | + mockResponse(t, http.StatusOK, orgProjects), |
| 51 | + ), |
| 52 | + ), |
| 53 | + requestArgs: map[string]interface{}{ |
| 54 | + "owner": "octo-org", |
| 55 | + "owner_type": "organization", |
| 56 | + }, |
| 57 | + expectError: false, |
| 58 | + expectedLength: 1, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "success user", |
| 62 | + mockedClient: mock.NewMockedHTTPClient( |
| 63 | + mock.WithRequestMatchHandler( |
| 64 | + mock.EndpointPattern{Pattern: "/users/{username}/projectsV2", Method: http.MethodGet}, |
| 65 | + mockResponse(t, http.StatusOK, userProjects), |
| 66 | + ), |
| 67 | + ), |
| 68 | + requestArgs: map[string]interface{}{ |
| 69 | + "owner": "octocat", |
| 70 | + "owner_type": "user", |
| 71 | + }, |
| 72 | + expectError: false, |
| 73 | + expectedLength: 1, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "success organization with pagination & query", |
| 77 | + mockedClient: mock.NewMockedHTTPClient( |
| 78 | + mock.WithRequestMatchHandler( |
| 79 | + mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2", Method: http.MethodGet}, |
| 80 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 81 | + q := r.URL.Query() |
| 82 | + // Assert query params present |
| 83 | + if q.Get("after") == "cursor123" && q.Get("per_page") == "50" && q.Get("q") == "roadmap" { |
| 84 | + w.WriteHeader(http.StatusOK) |
| 85 | + _, _ = w.Write(mock.MustMarshal(orgProjects)) |
| 86 | + return |
| 87 | + } |
| 88 | + w.WriteHeader(http.StatusBadRequest) |
| 89 | + _, _ = w.Write([]byte(`{"message":"unexpected query params"}`)) |
| 90 | + }), |
| 91 | + ), |
| 92 | + ), |
| 93 | + requestArgs: map[string]interface{}{ |
| 94 | + "owner": "octo-org", |
| 95 | + "owner_type": "organization", |
| 96 | + "after": "cursor123", |
| 97 | + "per_page": float64(50), |
| 98 | + "query": "roadmap", |
| 99 | + }, |
| 100 | + expectError: false, |
| 101 | + expectedLength: 1, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "api error", |
| 105 | + mockedClient: mock.NewMockedHTTPClient( |
| 106 | + mock.WithRequestMatchHandler( |
| 107 | + mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2", Method: http.MethodGet}, |
| 108 | + mockResponse(t, http.StatusInternalServerError, map[string]string{"message": "boom"}), |
| 109 | + ), |
| 110 | + ), |
| 111 | + requestArgs: map[string]interface{}{ |
| 112 | + "owner": "octo-org", |
| 113 | + "owner_type": "organization", |
| 114 | + }, |
| 115 | + expectError: true, |
| 116 | + expectedErrMsg: "failed to list projects", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "missing owner", |
| 120 | + mockedClient: mock.NewMockedHTTPClient(), |
| 121 | + requestArgs: map[string]interface{}{ |
| 122 | + "owner_type": "organization", |
| 123 | + }, |
| 124 | + expectError: true, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "missing owner_type", |
| 128 | + mockedClient: mock.NewMockedHTTPClient(), |
| 129 | + requestArgs: map[string]interface{}{ |
| 130 | + "owner": "octo-org", |
| 131 | + }, |
| 132 | + expectError: true, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tc := range tests { |
| 137 | + t.Run(tc.name, func(t *testing.T) { |
| 138 | + client := gh.NewClient(tc.mockedClient) |
| 139 | + _, handler := ListProjects(stubGetClientFn(client), translations.NullTranslationHelper) |
| 140 | + request := createMCPRequest(tc.requestArgs) |
| 141 | + result, err := handler(context.Background(), request) |
| 142 | + |
| 143 | + require.NoError(t, err) |
| 144 | + if tc.expectError { |
| 145 | + require.True(t, result.IsError) |
| 146 | + text := getTextResult(t, result).Text |
| 147 | + if tc.expectedErrMsg != "" { |
| 148 | + assert.Contains(t, text, tc.expectedErrMsg) |
| 149 | + } |
| 150 | + // Parameter missing cases |
| 151 | + if tc.name == "missing owner" { |
| 152 | + assert.Contains(t, text, "missing required parameter: owner") |
| 153 | + } |
| 154 | + if tc.name == "missing owner_type" { |
| 155 | + assert.Contains(t, text, "missing required parameter: owner_type") |
| 156 | + } |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + require.False(t, result.IsError) |
| 161 | + textContent := getTextResult(t, result) |
| 162 | + var arr []map[string]any |
| 163 | + err = json.Unmarshal([]byte(textContent.Text), &arr) |
| 164 | + require.NoError(t, err) |
| 165 | + assert.Equal(t, tc.expectedLength, len(arr)) |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments