@@ -192,6 +192,115 @@ func Test_ListGists(t *testing.T) {
192192 }
193193}
194194
195+ func Test_GetGist (t * testing.T ) {
196+ // Verify tool definition
197+ mockClient := github .NewClient (nil )
198+ tool , _ := GetGist (stubGetClientFn (mockClient ), translations .NullTranslationHelper )
199+
200+ assert .Equal (t , "get_gist" , tool .Name )
201+ assert .NotEmpty (t , tool .Description )
202+ assert .Contains (t , tool .InputSchema .Properties , "gist_id" )
203+
204+ assert .Contains (t , tool .InputSchema .Required , "gist_id" )
205+
206+ // Setup mock gist for success case
207+ mockGist := github.Gist {
208+ ID : github .Ptr ("gist1" ),
209+ Description : github .Ptr ("First Gist" ),
210+ HTMLURL : github .Ptr ("https://gist.github.com/user/gist1" ),
211+ Public : github .Ptr (true ),
212+ CreatedAt : & github.Timestamp {Time : time .Date (2023 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )},
213+ Owner : & github.User {Login : github .Ptr ("user" )},
214+ Files : map [github.GistFilename ]github.GistFile {
215+ github .GistFilename ("file1.txt" ): {
216+ Filename : github .Ptr ("file1.txt" ),
217+ Content : github .Ptr ("content of file 1" ),
218+ },
219+ },
220+ }
221+
222+ tests := []struct {
223+ name string
224+ mockedClient * http.Client
225+ requestArgs map [string ]interface {}
226+ expectError bool
227+ expectedGists github.Gist
228+ expectedErrMsg string
229+ }{
230+ {
231+ name : "Successful fetching different gist" ,
232+ mockedClient : mock .NewMockedHTTPClient (
233+ mock .WithRequestMatchHandler (
234+ mock .GetGistsByGistId ,
235+ mockResponse (t , http .StatusOK , mockGist ),
236+ ),
237+ ),
238+ requestArgs : map [string ]interface {}{
239+ "gist_id" : "gist1" ,
240+ },
241+ expectError : false ,
242+ expectedGists : mockGist ,
243+ },
244+ {
245+ name : "gist_id parameter missing" ,
246+ mockedClient : mock .NewMockedHTTPClient (
247+ mock .WithRequestMatchHandler (
248+ mock .GetGistsByGistId ,
249+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
250+ w .WriteHeader (http .StatusUnprocessableEntity )
251+ _ , _ = w .Write ([]byte (`{"message": "Invalid Request"}` ))
252+ }),
253+ ),
254+ ),
255+ requestArgs : map [string ]interface {}{},
256+ expectError : true ,
257+ expectedErrMsg : "missing required parameter: gist_id" ,
258+ },
259+ }
260+
261+ for _ , tc := range tests {
262+ t .Run (tc .name , func (t * testing.T ) {
263+ // Setup client with mock
264+ client := github .NewClient (tc .mockedClient )
265+ _ , handler := GetGist (stubGetClientFn (client ), translations .NullTranslationHelper )
266+
267+ // Create call request
268+ request := createMCPRequest (tc .requestArgs )
269+
270+ // Call handler
271+ result , err := handler (context .Background (), request )
272+
273+ // Verify results
274+ if tc .expectError {
275+ if err != nil {
276+ assert .Contains (t , err .Error (), tc .expectedErrMsg )
277+ } else {
278+ // For errors returned as part of the result, not as an error
279+ assert .NotNil (t , result )
280+ textContent := getTextResult (t , result )
281+ assert .Contains (t , textContent .Text , tc .expectedErrMsg )
282+ }
283+ return
284+ }
285+
286+ require .NoError (t , err )
287+
288+ // Parse the result and get the text content if no error
289+ textContent := getTextResult (t , result )
290+
291+ // Unmarshal and verify the result
292+ var returnedGists github.Gist
293+ err = json .Unmarshal ([]byte (textContent .Text ), & returnedGists )
294+ require .NoError (t , err )
295+
296+ assert .Equal (t , * tc .expectedGists .ID , * returnedGists .ID )
297+ assert .Equal (t , * tc .expectedGists .Description , * returnedGists .Description )
298+ assert .Equal (t , * tc .expectedGists .HTMLURL , * returnedGists .HTMLURL )
299+ assert .Equal (t , * tc .expectedGists .Public , * returnedGists .Public )
300+ })
301+ }
302+ }
303+
195304func Test_CreateGist (t * testing.T ) {
196305 // Verify tool definition
197306 mockClient := github .NewClient (nil )
0 commit comments