@@ -35,8 +35,7 @@ func Test_GetFileContents(t *testing.T) {
35
35
assert .Contains (t , tool .InputSchema .Properties , "sha" )
36
36
assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" })
37
37
38
- // Mock response for raw content
39
- mockRawContent := []byte ("# Test Repository\n \n This is a test repository." )
38
+
40
39
41
40
// Setup mock directory content for success case
42
41
mockDirContent := []* github.RepositoryContent {
@@ -67,20 +66,31 @@ func Test_GetFileContents(t *testing.T) {
67
66
expectStatus int
68
67
}{
69
68
{
70
- name : "successful text content fetch" ,
69
+ name : "successful file content fetch" ,
71
70
mockedClient : mock .NewMockedHTTPClient (
72
71
mock .WithRequestMatchHandler (
73
72
mock .GetReposGitRefByOwnerByRepoByRef ,
74
73
http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
75
74
w .WriteHeader (http .StatusOK )
76
- _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": ""}}` ))
75
+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": "abc123 "}}` ))
77
76
}),
78
77
),
79
78
mock .WithRequestMatchHandler (
80
- raw . GetRawReposContentsByOwnerByRepoByBranchByPath ,
79
+ mock . GetReposContentsByOwnerByRepoByPath ,
81
80
http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
82
- w .Header ().Set ("Content-Type" , "text/markdown" )
83
- _ , _ = w .Write (mockRawContent )
81
+ w .WriteHeader (http .StatusOK )
82
+ fileContent := & github.RepositoryContent {
83
+ Type : github .Ptr ("file" ),
84
+ Name : github .Ptr ("README.md" ),
85
+ Path : github .Ptr ("README.md" ),
86
+ SHA : github .Ptr ("abc123" ),
87
+ Size : github .Ptr (42 ),
88
+ Content : github .Ptr (base64 .StdEncoding .EncodeToString ([]byte ("# Test Repository\n \n This is a test repository." ))),
89
+ Encoding : github .Ptr ("base64" ),
90
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/README.md" ),
91
+ }
92
+ responseBody , _ := json .Marshal (fileContent )
93
+ _ , _ = w .Write (responseBody )
84
94
}),
85
95
),
86
96
),
@@ -91,27 +101,43 @@ func Test_GetFileContents(t *testing.T) {
91
101
"ref" : "refs/heads/main" ,
92
102
},
93
103
expectError : false ,
94
- expectedResult : mcp.TextResourceContents {
95
- URI : "repo://owner/repo/refs/heads/main/contents/README.md" ,
96
- Text : "# Test Repository\n \n This is a test repository." ,
97
- MIMEType : "text/markdown" ,
104
+ expectedResult : & github.RepositoryContent {
105
+ Type : github .Ptr ("file" ),
106
+ Name : github .Ptr ("README.md" ),
107
+ Path : github .Ptr ("README.md" ),
108
+ SHA : github .Ptr ("abc123" ),
109
+ Size : github .Ptr (42 ),
110
+ Content : github .Ptr (base64 .StdEncoding .EncodeToString ([]byte ("# Test Repository\n \n This is a test repository." ))),
111
+ Encoding : github .Ptr ("base64" ),
112
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/README.md" ),
98
113
},
99
114
},
100
115
{
101
- name : "successful file blob content fetch" ,
116
+ name : "successful binary file content fetch" ,
102
117
mockedClient : mock .NewMockedHTTPClient (
103
118
mock .WithRequestMatchHandler (
104
119
mock .GetReposGitRefByOwnerByRepoByRef ,
105
120
http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
106
121
w .WriteHeader (http .StatusOK )
107
- _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": ""}}` ))
122
+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": "def456 "}}` ))
108
123
}),
109
124
),
110
125
mock .WithRequestMatchHandler (
111
- raw . GetRawReposContentsByOwnerByRepoByBranchByPath ,
126
+ mock . GetReposContentsByOwnerByRepoByPath ,
112
127
http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
113
- w .Header ().Set ("Content-Type" , "image/png" )
114
- _ , _ = w .Write (mockRawContent )
128
+ w .WriteHeader (http .StatusOK )
129
+ fileContent := & github.RepositoryContent {
130
+ Type : github .Ptr ("file" ),
131
+ Name : github .Ptr ("test.png" ),
132
+ Path : github .Ptr ("test.png" ),
133
+ SHA : github .Ptr ("def456" ),
134
+ Size : github .Ptr (1024 ),
135
+ Content : github .Ptr (base64 .StdEncoding .EncodeToString ([]byte ("binary-image-content" ))),
136
+ Encoding : github .Ptr ("base64" ),
137
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/test.png" ),
138
+ }
139
+ responseBody , _ := json .Marshal (fileContent )
140
+ _ , _ = w .Write (responseBody )
115
141
}),
116
142
),
117
143
),
@@ -122,10 +148,15 @@ func Test_GetFileContents(t *testing.T) {
122
148
"ref" : "refs/heads/main" ,
123
149
},
124
150
expectError : false ,
125
- expectedResult : mcp.BlobResourceContents {
126
- URI : "repo://owner/repo/refs/heads/main/contents/test.png" ,
127
- Blob : base64 .StdEncoding .EncodeToString (mockRawContent ),
128
- MIMEType : "image/png" ,
151
+ expectedResult : & github.RepositoryContent {
152
+ Type : github .Ptr ("file" ),
153
+ Name : github .Ptr ("test.png" ),
154
+ Path : github .Ptr ("test.png" ),
155
+ SHA : github .Ptr ("def456" ),
156
+ Size : github .Ptr (1024 ),
157
+ Content : github .Ptr (base64 .StdEncoding .EncodeToString ([]byte ("binary-image-content" ))),
158
+ Encoding : github .Ptr ("base64" ),
159
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/test.png" ),
129
160
},
130
161
},
131
162
{
@@ -151,14 +182,7 @@ func Test_GetFileContents(t *testing.T) {
151
182
mockResponse (t , http .StatusOK , mockDirContent ),
152
183
),
153
184
),
154
- mock .WithRequestMatchHandler (
155
- raw .GetRawReposContentsByOwnerByRepoByPath ,
156
- expectQueryParams (t , map [string ]string {
157
- "branch" : "main" ,
158
- }).andThen (
159
- mockResponse (t , http .StatusNotFound , nil ),
160
- ),
161
- ),
185
+
162
186
),
163
187
requestArgs : map [string ]interface {}{
164
188
"owner" : "owner" ,
@@ -186,7 +210,7 @@ func Test_GetFileContents(t *testing.T) {
186
210
}),
187
211
),
188
212
mock .WithRequestMatchHandler (
189
- raw . GetRawReposContentsByOwnerByRepoByPath ,
213
+ mock . GetReposGitTreesByOwnerByRepoByTreeSha ,
190
214
http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
191
215
w .WriteHeader (http .StatusNotFound )
192
216
_ , _ = w .Write ([]byte (`{"message": "Not Found"}` ))
@@ -227,12 +251,19 @@ func Test_GetFileContents(t *testing.T) {
227
251
require .NoError (t , err )
228
252
// Use the correct result helper based on the expected type
229
253
switch expected := tc .expectedResult .(type ) {
230
- case mcp.TextResourceContents :
231
- textResource := getTextResourceResult (t , result )
232
- assert .Equal (t , expected , textResource )
233
- case mcp.BlobResourceContents :
234
- blobResource := getBlobResourceResult (t , result )
235
- assert .Equal (t , expected , blobResource )
254
+ case * github.RepositoryContent :
255
+ // File content fetch returns a text result (JSON object)
256
+ textContent := getTextResult (t , result )
257
+ var returnedContent github.RepositoryContent
258
+ err = json .Unmarshal ([]byte (textContent .Text ), & returnedContent )
259
+ require .NoError (t , err , "Failed to unmarshal file content result: %v" , textContent .Text )
260
+ assert .Equal (t , * expected .Name , * returnedContent .Name )
261
+ assert .Equal (t , * expected .Path , * returnedContent .Path )
262
+ assert .Equal (t , * expected .Type , * returnedContent .Type )
263
+ assert .Equal (t , * expected .SHA , * returnedContent .SHA )
264
+ if expected .Content != nil && returnedContent .Content != nil {
265
+ assert .Equal (t , * expected .Content , * returnedContent .Content )
266
+ }
236
267
case []* github.RepositoryContent :
237
268
// Directory content fetch returns a text result (JSON array)
238
269
textContent := getTextResult (t , result )
0 commit comments