-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathserver_test.go
More file actions
230 lines (212 loc) · 5.52 KB
/
server_test.go
File metadata and controls
230 lines (212 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v69/github"
"github.com/migueleliasweb/go-github-mock/src/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_GetMe(t *testing.T) {
// Verify tool definition
mockClient := github.NewClient(nil)
tool, _ := getMe(mockClient, translations.NullTranslationHelper)
assert.Equal(t, "get_me", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.Contains(t, tool.InputSchema.Properties, "reason")
assert.Empty(t, tool.InputSchema.Required) // No required parameters
// Setup mock user response
mockUser := &github.User{
Login: github.Ptr("testuser"),
Name: github.Ptr("Test User"),
Email: github.Ptr("test@example.com"),
Bio: github.Ptr("GitHub user for testing"),
Company: github.Ptr("Test Company"),
Location: github.Ptr("Test Location"),
HTMLURL: github.Ptr("https://github.com/testuser"),
CreatedAt: &github.Timestamp{Time: time.Now().Add(-365 * 24 * time.Hour)},
Type: github.Ptr("User"),
Plan: &github.Plan{
Name: github.Ptr("pro"),
},
}
tests := []struct {
name string
mockedClient *http.Client
requestArgs map[string]interface{}
expectError bool
expectedUser *github.User
expectedErrMsg string
}{
{
name: "successful get user",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetUser,
mockUser,
),
),
requestArgs: map[string]interface{}{},
expectError: false,
expectedUser: mockUser,
},
{
name: "successful get user with reason",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetUser,
mockUser,
),
),
requestArgs: map[string]interface{}{
"reason": "Testing API",
},
expectError: false,
expectedUser: mockUser,
},
{
name: "get user fails",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.GetUser,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"message": "Unauthorized"}`))
}),
),
),
requestArgs: map[string]interface{}{},
expectError: true,
expectedErrMsg: "failed to get user",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := getMe(client, translations.NullTranslationHelper)
// Create call request
request := createMCPRequest(tc.requestArgs)
// Call handler
result, err := handler(context.Background(), request)
// Verify results
if tc.expectError {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.expectedErrMsg)
return
}
require.NoError(t, err)
// Parse result and get text content if no error
textContent := getTextResult(t, result)
// Unmarshal and verify the result
var returnedUser github.User
err = json.Unmarshal([]byte(textContent.Text), &returnedUser)
require.NoError(t, err)
// Verify user details
assert.Equal(t, *tc.expectedUser.Login, *returnedUser.Login)
assert.Equal(t, *tc.expectedUser.Name, *returnedUser.Name)
assert.Equal(t, *tc.expectedUser.Email, *returnedUser.Email)
assert.Equal(t, *tc.expectedUser.Bio, *returnedUser.Bio)
assert.Equal(t, *tc.expectedUser.HTMLURL, *returnedUser.HTMLURL)
assert.Equal(t, *tc.expectedUser.Type, *returnedUser.Type)
})
}
}
func Test_IsAcceptedError(t *testing.T) {
tests := []struct {
name string
err error
expectAccepted bool
}{
{
name: "github AcceptedError",
err: &github.AcceptedError{},
expectAccepted: true,
},
{
name: "regular error",
err: fmt.Errorf("some other error"),
expectAccepted: false,
},
{
name: "nil error",
err: nil,
expectAccepted: false,
},
{
name: "wrapped AcceptedError",
err: fmt.Errorf("wrapped: %w", &github.AcceptedError{}),
expectAccepted: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := isAcceptedError(tc.err)
assert.Equal(t, tc.expectAccepted, result)
})
}
}
func Test_ParseCommaSeparatedList(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{
name: "simple comma separated values",
input: "one,two,three",
expected: []string{"one", "two", "three"},
},
{
name: "values with spaces",
input: "one, two, three",
expected: []string{"one", "two", "three"},
},
{
name: "values with extra spaces",
input: " one , two , three ",
expected: []string{"one", "two", "three"},
},
{
name: "empty values in between",
input: "one,,three",
expected: []string{"one", "three"},
},
{
name: "only spaces",
input: " , , ",
expected: []string{},
},
{
name: "empty string",
input: "",
expected: nil,
},
{
name: "single value",
input: "one",
expected: []string{"one"},
},
{
name: "trailing comma",
input: "one,two,",
expected: []string{"one", "two"},
},
{
name: "leading comma",
input: ",one,two",
expected: []string{"one", "two"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := parseCommaSeparatedList(tc.input)
assert.Equal(t, tc.expected, result)
})
}
}