9
9
"testing"
10
10
"time"
11
11
12
+ "github.com/github/github-mcp-server/internal/githubv4mock"
12
13
"github.com/github/github-mcp-server/internal/toolsnaps"
13
14
"github.com/github/github-mcp-server/pkg/raw"
14
15
"github.com/github/github-mcp-server/pkg/translations"
@@ -20,11 +21,39 @@ import (
20
21
"github.com/stretchr/testify/require"
21
22
)
22
23
23
- // mockGetFileSHA is a test helper that mocks the getFileSHA function so that we don't need to mock the GraphQL client in Test_GetFileContents.
24
- func mockGetFileSHA (expectedSHA string ) func (context.Context , * githubv4.Client , string , string , string , * raw.ContentOpts ) (string , error ) {
25
- return func (_ context.Context , _ * githubv4.Client , _ , _ , _ string , _ * raw.ContentOpts ) (string , error ) {
26
- return expectedSHA , nil
24
+ func mockGQLClientFileSHA (t * testing.T , owner , repo , expression , expectedSHA string ) * githubv4.Client {
25
+ // Create the query structure that matches getFileSHA function
26
+ query := struct {
27
+ Repository struct {
28
+ Object struct {
29
+ Blob struct {
30
+ OID string
31
+ } `graphql:"... on Blob"`
32
+ } `graphql:"object(expression: $expression)"`
33
+ } `graphql:"repository(owner: $owner, name: $repo)"`
34
+ }{}
35
+
36
+ // Match any variables, we don't care about the exact values in this test
37
+ variables := map [string ]interface {}{
38
+ "owner" : githubv4 .String (owner ),
39
+ "repo" : githubv4 .String (repo ),
40
+ "expression" : githubv4 .String (expression ),
27
41
}
42
+
43
+ // Create the mock response with the expected SHA
44
+ mockResponse := githubv4mock .DataResponse (map [string ]any {
45
+ "repository" : map [string ]any {
46
+ "object" : map [string ]any {
47
+ "oid" : expectedSHA ,
48
+ },
49
+ },
50
+ })
51
+
52
+ // Create the matcher and mock client
53
+ matcher := githubv4mock .NewQueryMatcher (query , variables , mockResponse )
54
+ httpClient := githubv4mock .NewMockedHTTPClient (matcher )
55
+
56
+ return githubv4 .NewClient (httpClient )
28
57
}
29
58
30
59
func Test_GetFileContents (t * testing.T ) {
@@ -68,7 +97,7 @@ func Test_GetFileContents(t *testing.T) {
68
97
tests := []struct {
69
98
name string
70
99
mockedClient * http.Client
71
- mockFileSHA string
100
+ mockGQLClient * githubv4. Client
72
101
requestArgs map [string ]interface {}
73
102
expectError bool
74
103
expectedResult interface {}
@@ -107,7 +136,7 @@ func Test_GetFileContents(t *testing.T) {
107
136
}),
108
137
),
109
138
),
110
- mockFileSHA : " abc123" ,
139
+ mockGQLClient : mockGQLClientFileSHA ( t , "owner" , "repo" , "refs/heads/main:README.md" , " abc123") ,
111
140
requestArgs : map [string ]interface {}{
112
141
"owner" : "owner" ,
113
142
"repo" : "repo" ,
@@ -153,7 +182,7 @@ func Test_GetFileContents(t *testing.T) {
153
182
}),
154
183
),
155
184
),
156
- mockFileSHA : " def456" ,
185
+ mockGQLClient : mockGQLClientFileSHA ( t , "owner" , "repo" , "refs/heads/main:test.png" , " def456") ,
157
186
requestArgs : map [string ]interface {}{
158
187
"owner" : "owner" ,
159
188
"repo" : "repo" ,
@@ -199,7 +228,7 @@ func Test_GetFileContents(t *testing.T) {
199
228
),
200
229
),
201
230
),
202
- mockFileSHA : "" , // Directory content doesn't need SHA
231
+ mockGQLClient : nil ,
203
232
requestArgs : map [string ]interface {}{
204
233
"owner" : "owner" ,
205
234
"repo" : "repo" ,
@@ -233,7 +262,7 @@ func Test_GetFileContents(t *testing.T) {
233
262
}),
234
263
),
235
264
),
236
- mockFileSHA : " " , // Error case doesn't need SHA
265
+ mockGQLClient : mockGQLClientFileSHA ( t , "owner " , "repo" , "refs/heads/main:nonexistent.md" , "" ),
237
266
requestArgs : map [string ]interface {}{
238
267
"owner" : "owner" ,
239
268
"repo" : "repo" ,
@@ -247,20 +276,10 @@ func Test_GetFileContents(t *testing.T) {
247
276
248
277
for _ , tc := range tests {
249
278
t .Run (tc .name , func (t * testing.T ) {
250
- // Mock the getFileSHA function if mockFileSHA is provided
251
- if tc .mockFileSHA != "" {
252
- originalGetFileSHA := getFileSHAFunc
253
- getFileSHAFunc = mockGetFileSHA (tc .mockFileSHA )
254
- defer func () {
255
- getFileSHAFunc = originalGetFileSHA
256
- }()
257
- }
258
-
259
279
// Setup client with mock
260
280
client := github .NewClient (tc .mockedClient )
261
281
mockRawClient := raw .NewClient (client , & url.URL {Scheme : "https" , Host : "raw.example.com" , Path : "/" })
262
- mockGQLClient := githubv4 .NewClient (tc .mockedClient )
263
- _ , handler := GetFileContents (stubGetClientFn (client ), stubGetRawClientFn (mockRawClient ), stubGetGQLClientFn (mockGQLClient ), translations .NullTranslationHelper )
282
+ _ , handler := GetFileContents (stubGetClientFn (client ), stubGetRawClientFn (mockRawClient ), stubGetGQLClientFn (tc .mockGQLClient ), translations .NullTranslationHelper )
264
283
265
284
// Create call request
266
285
request := createMCPRequest (tc .requestArgs )
0 commit comments