|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/internal/toolsnaps" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + "github.com/google/go-github/v82/github" |
| 13 | + "github.com/google/jsonschema-go/jsonschema" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_ListOrgIssueFields(t *testing.T) { |
| 19 | + // Verify tool definition |
| 20 | + serverTool := ListOrgIssueFields(translations.NullTranslationHelper) |
| 21 | + tool := serverTool.Tool |
| 22 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 23 | + |
| 24 | + assert.Equal(t, "list_org_issue_fields", tool.Name) |
| 25 | + assert.NotEmpty(t, tool.Description) |
| 26 | + assert.True(t, tool.Annotations.ReadOnlyHint) |
| 27 | + assert.Contains(t, tool.InputSchema.(*jsonschema.Schema).Properties, "org") |
| 28 | + assert.ElementsMatch(t, tool.InputSchema.(*jsonschema.Schema).Required, []string{"org"}) |
| 29 | + |
| 30 | + mockIssueFields := []*IssueField{ |
| 31 | + { |
| 32 | + ID: 1, |
| 33 | + NodeID: "IFT_kwDNAd3NAZo", |
| 34 | + Name: "DRI", |
| 35 | + Description: "Directly responsible individual", |
| 36 | + DataType: "text", |
| 37 | + CreatedAt: "2024-12-11T14:39:09Z", |
| 38 | + UpdatedAt: "2024-12-11T14:39:09Z", |
| 39 | + }, |
| 40 | + { |
| 41 | + ID: 2, |
| 42 | + NodeID: "IFSS_kwDNAd3NAZs", |
| 43 | + Name: "Priority", |
| 44 | + Description: "Level of importance", |
| 45 | + DataType: "single_select", |
| 46 | + Options: []IssueFieldOption{ |
| 47 | + {ID: 1, Name: "High"}, |
| 48 | + {ID: 2, Name: "Medium"}, |
| 49 | + {ID: 3, Name: "Low"}, |
| 50 | + }, |
| 51 | + CreatedAt: "2024-12-11T14:39:09Z", |
| 52 | + UpdatedAt: "2024-12-11T14:39:09Z", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + mockedClient *http.Client |
| 59 | + requestArgs map[string]any |
| 60 | + expectError bool |
| 61 | + expectedIssueFields []*IssueField |
| 62 | + expectedErrMsg string |
| 63 | + }{ |
| 64 | + { |
| 65 | + name: "successful issue fields retrieval", |
| 66 | + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 67 | + "GET /orgs/testorg/issue-fields": mockResponse(t, http.StatusOK, mockIssueFields), |
| 68 | + }), |
| 69 | + requestArgs: map[string]any{ |
| 70 | + "org": "testorg", |
| 71 | + }, |
| 72 | + expectError: false, |
| 73 | + expectedIssueFields: mockIssueFields, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "issue fields not enabled returns empty list", |
| 77 | + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 78 | + "GET /orgs/testorg/issue-fields": mockResponse(t, http.StatusNotFound, `{"message": "Not Found"}`), |
| 79 | + }), |
| 80 | + requestArgs: map[string]any{ |
| 81 | + "org": "testorg", |
| 82 | + }, |
| 83 | + expectError: false, |
| 84 | + expectedIssueFields: []*IssueField{}, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "missing org parameter", |
| 88 | + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 89 | + "GET /orgs/testorg/issue-fields": mockResponse(t, http.StatusOK, mockIssueFields), |
| 90 | + }), |
| 91 | + requestArgs: map[string]any{}, |
| 92 | + expectError: false, |
| 93 | + expectedErrMsg: "missing required parameter: org", |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tc := range tests { |
| 98 | + t.Run(tc.name, func(t *testing.T) { |
| 99 | + client := github.NewClient(tc.mockedClient) |
| 100 | + deps := BaseDeps{ |
| 101 | + Client: client, |
| 102 | + } |
| 103 | + handler := serverTool.Handler(deps) |
| 104 | + |
| 105 | + request := createMCPRequest(tc.requestArgs) |
| 106 | + |
| 107 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 108 | + |
| 109 | + if tc.expectError { |
| 110 | + if err != nil { |
| 111 | + assert.Contains(t, err.Error(), tc.expectedErrMsg) |
| 112 | + return |
| 113 | + } |
| 114 | + require.NotNil(t, result) |
| 115 | + require.True(t, result.IsError) |
| 116 | + errorContent := getErrorResult(t, result) |
| 117 | + assert.Contains(t, errorContent.Text, tc.expectedErrMsg) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + if result != nil && result.IsError { |
| 122 | + errorContent := getErrorResult(t, result) |
| 123 | + if tc.expectedErrMsg != "" && strings.Contains(errorContent.Text, tc.expectedErrMsg) { |
| 124 | + return |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + require.NoError(t, err) |
| 129 | + require.NotNil(t, result) |
| 130 | + require.False(t, result.IsError) |
| 131 | + textContent := getTextResult(t, result) |
| 132 | + |
| 133 | + var returnedFields []*IssueField |
| 134 | + err = json.Unmarshal([]byte(textContent.Text), &returnedFields) |
| 135 | + require.NoError(t, err) |
| 136 | + |
| 137 | + require.Equal(t, len(tc.expectedIssueFields), len(returnedFields)) |
| 138 | + for i, expected := range tc.expectedIssueFields { |
| 139 | + assert.Equal(t, expected.ID, returnedFields[i].ID) |
| 140 | + assert.Equal(t, expected.Name, returnedFields[i].Name) |
| 141 | + assert.Equal(t, expected.DataType, returnedFields[i].DataType) |
| 142 | + if expected.Options != nil { |
| 143 | + require.Equal(t, len(expected.Options), len(returnedFields[i].Options)) |
| 144 | + for j, opt := range expected.Options { |
| 145 | + assert.Equal(t, opt.Name, returnedFields[i].Options[j].Name) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
0 commit comments