forked from github/github-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_tools_test.go
More file actions
141 lines (128 loc) · 4.2 KB
/
context_tools_test.go
File metadata and controls
141 lines (128 loc) · 4.2 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
package github
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/github/github-mcp-server/internal/toolsnaps"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v74/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) {
t.Parallel()
tool, _ := GetMe(nil, translations.NullTranslationHelper)
require.NoError(t, toolsnaps.Test(tool.Name, tool))
// Verify some basic very important properties
assert.Equal(t, "get_me", tool.Name)
assert.True(t, *tool.Annotations.ReadOnlyHint, "get_me tool should be read-only")
// 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"),
Hireable: github.Ptr(true),
TwitterUsername: github.Ptr("testuser_twitter"),
Plan: &github.Plan{
Name: github.Ptr("pro"),
},
}
tests := []struct {
name string
stubbedGetClientFn GetClientFn
requestArgs map[string]any
expectToolError bool
expectedUser *github.User
expectedToolErrMsg string
}{
{
name: "successful get user",
stubbedGetClientFn: stubGetClientFromHTTPFn(
mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetUser,
mockUser,
),
),
),
requestArgs: map[string]any{},
expectToolError: false,
expectedUser: mockUser,
},
{
name: "successful get user with reason",
stubbedGetClientFn: stubGetClientFromHTTPFn(
mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetUser,
mockUser,
),
),
),
requestArgs: map[string]any{
"reason": "Testing API",
},
expectToolError: false,
expectedUser: mockUser,
},
{
name: "getting client fails",
stubbedGetClientFn: stubGetClientFnErr("expected test error"),
requestArgs: map[string]any{},
expectToolError: true,
expectedToolErrMsg: "failed to get GitHub client: expected test error",
},
{
name: "get user fails",
stubbedGetClientFn: stubGetClientFromHTTPFn(
mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.GetUser,
badRequestHandler("expected test failure"),
),
),
),
requestArgs: map[string]any{},
expectToolError: true,
expectedToolErrMsg: "expected test failure",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, handler := GetMe(tc.stubbedGetClientFn, translations.NullTranslationHelper)
request := createMCPRequest(tc.requestArgs)
result, err := handler(context.Background(), request)
require.NoError(t, err)
textContent := getTextResult(t, result)
if tc.expectToolError {
assert.True(t, result.IsError, "expected tool call result to be an error")
assert.Contains(t, textContent.Text, tc.expectedToolErrMsg)
return
}
// Unmarshal and verify the result
var returnedUser MinimalUser
err = json.Unmarshal([]byte(textContent.Text), &returnedUser)
require.NoError(t, err)
// Verify minimal user details
assert.Equal(t, *tc.expectedUser.Login, returnedUser.Login)
assert.Equal(t, *tc.expectedUser.HTMLURL, returnedUser.ProfileURL)
// Verify user details
require.NotNil(t, returnedUser.Details)
assert.Equal(t, *tc.expectedUser.Name, returnedUser.Details.Name)
assert.Equal(t, *tc.expectedUser.Email, returnedUser.Details.Email)
assert.Equal(t, *tc.expectedUser.Bio, returnedUser.Details.Bio)
assert.Equal(t, *tc.expectedUser.Company, returnedUser.Details.Company)
assert.Equal(t, *tc.expectedUser.Location, returnedUser.Details.Location)
assert.Equal(t, *tc.expectedUser.Hireable, returnedUser.Details.Hireable)
assert.Equal(t, *tc.expectedUser.TwitterUsername, returnedUser.Details.TwitterUsername)
})
}
}