@@ -15,6 +15,39 @@ import (
15
15
"github.com/stretchr/testify/require"
16
16
)
17
17
18
+ var mockCommits = []* github.RepositoryCommit {
19
+ {
20
+ SHA : github .Ptr ("abc123def456" ),
21
+ Commit : & github.Commit {
22
+ Message : github .Ptr ("First commit" ),
23
+ Author : & github.CommitAuthor {
24
+ Name : github .Ptr ("Test User" ),
25
+ Email :
github .
Ptr (
"[email protected] " ),
26
+ Date : & github.Timestamp {Time : time .Now ().Add (- 48 * time .Hour )},
27
+ },
28
+ },
29
+ Author : & github.User {
30
+ Login : github .Ptr ("testuser" ),
31
+ },
32
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/abc123def456" ),
33
+ },
34
+ {
35
+ SHA : github .Ptr ("def456abc789" ),
36
+ Commit : & github.Commit {
37
+ Message : github .Ptr ("Second commit" ),
38
+ Author : & github.CommitAuthor {
39
+ Name : github .Ptr ("Another User" ),
40
+ Email :
github .
Ptr (
"[email protected] " ),
41
+ Date : & github.Timestamp {Time : time .Now ().Add (- 24 * time .Hour )},
42
+ },
43
+ },
44
+ Author : & github.User {
45
+ Login : github .Ptr ("anotheruser" ),
46
+ },
47
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/def456abc789" ),
48
+ },
49
+ }
50
+
18
51
func Test_GetFileContents (t * testing.T ) {
19
52
// Verify tool definition once
20
53
mockClient := github .NewClient (nil )
@@ -475,54 +508,121 @@ func Test_CreateBranch(t *testing.T) {
475
508
}
476
509
}
477
510
478
- func Test_ListCommits (t * testing.T ) {
511
+ func Test_GetCommit (t * testing.T ) {
479
512
// Verify tool definition once
480
513
mockClient := github .NewClient (nil )
481
- tool , _ := listCommits (mockClient , translations .NullTranslationHelper )
514
+ tool , _ := getCommit (mockClient , translations .NullTranslationHelper )
482
515
483
- assert .Equal (t , "list_commits " , tool .Name )
516
+ assert .Equal (t , "get_commit " , tool .Name )
484
517
assert .NotEmpty (t , tool .Description )
485
518
assert .Contains (t , tool .InputSchema .Properties , "owner" )
486
519
assert .Contains (t , tool .InputSchema .Properties , "repo" )
487
520
assert .Contains (t , tool .InputSchema .Properties , "sha" )
488
- assert .Contains (t , tool .InputSchema .Properties , "page" )
489
- assert .Contains (t , tool .InputSchema .Properties , "perPage" )
490
- assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" })
521
+ assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" , "sha" })
522
+
523
+ mockCommit := mockCommits [0 ]
524
+ // This one currently isn't defined in the mock package we're using.
525
+ var mockEndpointPattern = mock.EndpointPattern {
526
+ Pattern : "/repos/{owner}/{repo}/commits/{sha}" ,
527
+ Method : "GET" ,
528
+ }
491
529
492
- // Setup mock commits for success case
493
- mockCommits := []* github.RepositoryCommit {
530
+ tests := []struct {
531
+ name string
532
+ mockedClient * http.Client
533
+ requestArgs map [string ]interface {}
534
+ expectError bool
535
+ expectedCommit * github.RepositoryCommit
536
+ expectedErrMsg string
537
+ }{
494
538
{
495
- SHA : github . Ptr ( "abc123def456" ) ,
496
- Commit : & github. Commit {
497
- Message : github . Ptr ( "First commit" ),
498
- Author : & github. CommitAuthor {
499
- Name : github . Ptr ( "Test User" ),
500
- Email : github . Ptr ( "[email protected] " ),
501
- Date : & github. Timestamp { Time : time . Now (). Add ( - 48 * time . Hour )} ,
502
- },
503
- } ,
504
- Author : & github. User {
505
- Login : github . Ptr ( "testuser" ) ,
539
+ name : "successful commit fetch" ,
540
+ mockedClient : mock . NewMockedHTTPClient (
541
+ mock . WithRequestMatchHandler (
542
+ mockEndpointPattern ,
543
+ mockResponse ( t , http . StatusOK , mockCommit ),
544
+ ),
545
+ ) ,
546
+ requestArgs : map [ string ] interface {}{
547
+ "owner" : "owner" ,
548
+ "repo" : "repo" ,
549
+ "sha" : "abc123def456" ,
506
550
},
507
- HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/abc123def456" ),
551
+ expectError : false ,
552
+ expectedCommit : mockCommit ,
508
553
},
509
554
{
510
- SHA : github .Ptr ("def456abc789" ),
511
- Commit : & github.Commit {
512
- Message : github .Ptr ("Second commit" ),
513
- Author : & github.CommitAuthor {
514
- Name : github .Ptr ("Another User" ),
515
- Email :
github .
Ptr (
"[email protected] " ),
516
- Date : & github.Timestamp {Time : time .Now ().Add (- 24 * time .Hour )},
517
- },
518
- },
519
- Author : & github.User {
520
- Login : github .Ptr ("anotheruser" ),
555
+ name : "commit fetch fails" ,
556
+ mockedClient : mock .NewMockedHTTPClient (
557
+ mock .WithRequestMatchHandler (
558
+ mockEndpointPattern ,
559
+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
560
+ w .WriteHeader (http .StatusNotFound )
561
+ _ , _ = w .Write ([]byte (`{"message": "Not Found"}` ))
562
+ }),
563
+ ),
564
+ ),
565
+ requestArgs : map [string ]interface {}{
566
+ "owner" : "owner" ,
567
+ "repo" : "repo" ,
568
+ "sha" : "nonexistent-sha" ,
521
569
},
522
- HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/def456abc789" ),
570
+ expectError : true ,
571
+ expectedErrMsg : "failed to get commit" ,
523
572
},
524
573
}
525
574
575
+ for _ , tc := range tests {
576
+ t .Run (tc .name , func (t * testing.T ) {
577
+ // Setup client with mock
578
+ client := github .NewClient (tc .mockedClient )
579
+ _ , handler := getCommit (client , translations .NullTranslationHelper )
580
+
581
+ // Create call request
582
+ request := createMCPRequest (tc .requestArgs )
583
+
584
+ // Call handler
585
+ result , err := handler (context .Background (), request )
586
+
587
+ // Verify results
588
+ if tc .expectError {
589
+ require .Error (t , err )
590
+ assert .Contains (t , err .Error (), tc .expectedErrMsg )
591
+ return
592
+ }
593
+
594
+ require .NoError (t , err )
595
+
596
+ // Parse the result and get the text content if no error
597
+ textContent := getTextResult (t , result )
598
+
599
+ // Unmarshal and verify the result
600
+ var returnedCommit github.RepositoryCommit
601
+ err = json .Unmarshal ([]byte (textContent .Text ), & returnedCommit )
602
+ require .NoError (t , err )
603
+
604
+ assert .Equal (t , * tc .expectedCommit .SHA , * returnedCommit .SHA )
605
+ assert .Equal (t , * tc .expectedCommit .Commit .Message , * returnedCommit .Commit .Message )
606
+ assert .Equal (t , * tc .expectedCommit .Author .Login , * returnedCommit .Author .Login )
607
+ assert .Equal (t , * tc .expectedCommit .HTMLURL , * returnedCommit .HTMLURL )
608
+ })
609
+ }
610
+ }
611
+
612
+ func Test_ListCommits (t * testing.T ) {
613
+ // Verify tool definition once
614
+ mockClient := github .NewClient (nil )
615
+ tool , _ := listCommits (mockClient , translations .NullTranslationHelper )
616
+
617
+ assert .Equal (t , "list_commits" , tool .Name )
618
+ assert .NotEmpty (t , tool .Description )
619
+ assert .Contains (t , tool .InputSchema .Properties , "owner" )
620
+ assert .Contains (t , tool .InputSchema .Properties , "repo" )
621
+ assert .Contains (t , tool .InputSchema .Properties , "sha" )
622
+ assert .Contains (t , tool .InputSchema .Properties , "page" )
623
+ assert .Contains (t , tool .InputSchema .Properties , "perPage" )
624
+ assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" })
625
+
526
626
tests := []struct {
527
627
name string
528
628
mockedClient * http.Client
0 commit comments