@@ -475,6 +475,136 @@ func Test_CreateBranch(t *testing.T) {
475
475
}
476
476
}
477
477
478
+ func Test_GetCommit (t * testing.T ) {
479
+ // Verify tool definition once
480
+ mockClient := github .NewClient (nil )
481
+ tool , _ := GetCommit (stubGetClientFn (mockClient ), translations .NullTranslationHelper )
482
+
483
+ assert .Equal (t , "get_commit" , tool .Name )
484
+ assert .NotEmpty (t , tool .Description )
485
+ assert .Contains (t , tool .InputSchema .Properties , "owner" )
486
+ assert .Contains (t , tool .InputSchema .Properties , "repo" )
487
+ assert .Contains (t , tool .InputSchema .Properties , "sha" )
488
+ assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" , "sha" })
489
+
490
+ mockCommit := & github.RepositoryCommit {
491
+ SHA : github .Ptr ("abc123def456" ),
492
+ Commit : & github.Commit {
493
+ Message : github .Ptr ("First commit" ),
494
+ Author : & github.CommitAuthor {
495
+ Name : github .Ptr ("Test User" ),
496
+ Email : github .Ptr ("test@example.com" ),
497
+ Date : & github.Timestamp {Time : time .Now ().Add (- 48 * time .Hour )},
498
+ },
499
+ },
500
+ Author : & github.User {
501
+ Login : github .Ptr ("testuser" ),
502
+ },
503
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/abc123def456" ),
504
+ Stats : & github.CommitStats {
505
+ Additions : github .Ptr (10 ),
506
+ Deletions : github .Ptr (2 ),
507
+ Total : github .Ptr (12 ),
508
+ },
509
+ Files : []* github.CommitFile {
510
+ {
511
+ Filename : github .Ptr ("file1.go" ),
512
+ Status : github .Ptr ("modified" ),
513
+ Additions : github .Ptr (10 ),
514
+ Deletions : github .Ptr (2 ),
515
+ Changes : github .Ptr (12 ),
516
+ Patch : github .Ptr ("@@ -1,2 +1,10 @@" ),
517
+ },
518
+ },
519
+ }
520
+ // This one currently isn't defined in the mock package we're using.
521
+ var mockEndpointPattern = mock.EndpointPattern {
522
+ Pattern : "/repos/{owner}/{repo}/commits/{sha}" ,
523
+ Method : "GET" ,
524
+ }
525
+
526
+ tests := []struct {
527
+ name string
528
+ mockedClient * http.Client
529
+ requestArgs map [string ]interface {}
530
+ expectError bool
531
+ expectedCommit * github.RepositoryCommit
532
+ expectedErrMsg string
533
+ }{
534
+ {
535
+ name : "successful commit fetch" ,
536
+ mockedClient : mock .NewMockedHTTPClient (
537
+ mock .WithRequestMatchHandler (
538
+ mockEndpointPattern ,
539
+ mockResponse (t , http .StatusOK , mockCommit ),
540
+ ),
541
+ ),
542
+ requestArgs : map [string ]interface {}{
543
+ "owner" : "owner" ,
544
+ "repo" : "repo" ,
545
+ "sha" : "abc123def456" ,
546
+ },
547
+ expectError : false ,
548
+ expectedCommit : mockCommit ,
549
+ },
550
+ {
551
+ name : "commit fetch fails" ,
552
+ mockedClient : mock .NewMockedHTTPClient (
553
+ mock .WithRequestMatchHandler (
554
+ mockEndpointPattern ,
555
+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
556
+ w .WriteHeader (http .StatusNotFound )
557
+ _ , _ = w .Write ([]byte (`{"message": "Not Found"}` ))
558
+ }),
559
+ ),
560
+ ),
561
+ requestArgs : map [string ]interface {}{
562
+ "owner" : "owner" ,
563
+ "repo" : "repo" ,
564
+ "sha" : "nonexistent-sha" ,
565
+ },
566
+ expectError : true ,
567
+ expectedErrMsg : "failed to get commit" ,
568
+ },
569
+ }
570
+
571
+ for _ , tc := range tests {
572
+ t .Run (tc .name , func (t * testing.T ) {
573
+ // Setup client with mock
574
+ client := github .NewClient (tc .mockedClient )
575
+ _ , handler := GetCommit (stubGetClientFn (client ), translations .NullTranslationHelper )
576
+
577
+ // Create call request
578
+ request := createMCPRequest (tc .requestArgs )
579
+
580
+ // Call handler
581
+ result , err := handler (context .Background (), request )
582
+
583
+ // Verify results
584
+ if tc .expectError {
585
+ require .Error (t , err )
586
+ assert .Contains (t , err .Error (), tc .expectedErrMsg )
587
+ return
588
+ }
589
+
590
+ require .NoError (t , err )
591
+
592
+ // Parse the result and get the text content if no error
593
+ textContent := getTextResult (t , result )
594
+
595
+ // Unmarshal and verify the result
596
+ var returnedCommit github.RepositoryCommit
597
+ err = json .Unmarshal ([]byte (textContent .Text ), & returnedCommit )
598
+ require .NoError (t , err )
599
+
600
+ assert .Equal (t , * tc .expectedCommit .SHA , * returnedCommit .SHA )
601
+ assert .Equal (t , * tc .expectedCommit .Commit .Message , * returnedCommit .Commit .Message )
602
+ assert .Equal (t , * tc .expectedCommit .Author .Login , * returnedCommit .Author .Login )
603
+ assert .Equal (t , * tc .expectedCommit .HTMLURL , * returnedCommit .HTMLURL )
604
+ })
605
+ }
606
+ }
607
+
478
608
func Test_ListCommits (t * testing.T ) {
479
609
// Verify tool definition once
480
610
mockClient := github .NewClient (nil )
0 commit comments