44 "context"
55 "encoding/json"
66 "net/http"
7- "net/http/httptest"
8- "net/url"
97 "testing"
108 "time"
119
@@ -1297,42 +1295,37 @@ func Test_PushFiles(t *testing.T) {
12971295}
12981296
12991297func Test_ListBranches (t * testing.T ) {
1300- // Create a test server
1301- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1302- assert .Equal (t , "/repos/owner/repo/branches" , r .URL .Path )
1303- assert .Equal (t , "GET" , r .Method )
1304-
1305- // Check query parameters
1306- query := r .URL .Query ()
1307- if page := query .Get ("page" ); page != "" {
1308- assert .Equal (t , "2" , page )
1309- }
1310- if perPage := query .Get ("per_page" ); perPage != "" {
1311- assert .Equal (t , "30" , perPage )
1312- }
1313-
1314- // Return mock branches
1315- mockBranches := []github.Branch {
1316- {Name : github .String ("main" )},
1317- {Name : github .String ("develop" )},
1318- }
1319- mockResponse (t , http .StatusOK , mockBranches )(w , r )
1320- }))
1321- defer ts .Close ()
1322-
1323- // Create a GitHub client using the test server URL
1324- client := github .NewClient (nil )
1325- client .BaseURL , _ = url .Parse (ts .URL + "/" )
1326-
1327- // Create the tool
1328- tool , handler := ListBranches (stubGetClientFn (client ), translations .NullTranslationHelper )
1298+ // Verify tool definition once
1299+ mockClient := github .NewClient (nil )
1300+ tool , _ := ListBranches (stubGetClientFn (mockClient ), translations .NullTranslationHelper )
1301+
1302+ assert .Equal (t , "list_branches" , tool .Name )
1303+ assert .NotEmpty (t , tool .Description )
1304+ assert .Contains (t , tool .InputSchema .Properties , "owner" )
1305+ assert .Contains (t , tool .InputSchema .Properties , "repo" )
1306+ assert .Contains (t , tool .InputSchema .Properties , "page" )
1307+ assert .Contains (t , tool .InputSchema .Properties , "perPage" )
1308+ assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" })
1309+
1310+ // Setup mock branches for success case
1311+ mockBranches := []* github.Branch {
1312+ {
1313+ Name : github .String ("main" ),
1314+ Commit : & github.RepositoryCommit {SHA : github .String ("abc123" )},
1315+ },
1316+ {
1317+ Name : github .String ("develop" ),
1318+ Commit : & github.RepositoryCommit {SHA : github .String ("def456" )},
1319+ },
1320+ }
13291321
13301322 // Test cases
13311323 tests := []struct {
1332- name string
1333- args map [string ]interface {}
1334- wantErr bool
1335- errContains string
1324+ name string
1325+ args map [string ]interface {}
1326+ mockResponses []mock.MockBackendOption
1327+ wantErr bool
1328+ errContains string
13361329 }{
13371330 {
13381331 name : "success" ,
@@ -1341,6 +1334,12 @@ func Test_ListBranches(t *testing.T) {
13411334 "repo" : "repo" ,
13421335 "page" : float64 (2 ),
13431336 },
1337+ mockResponses : []mock.MockBackendOption {
1338+ mock .WithRequestMatch (
1339+ mock .GetReposBranchesByOwnerByRepo ,
1340+ mockBranches ,
1341+ ),
1342+ },
13441343 wantErr : false ,
13451344 },
13461345 {
@@ -1363,16 +1362,19 @@ func Test_ListBranches(t *testing.T) {
13631362
13641363 for _ , tt := range tests {
13651364 t .Run (tt .name , func (t * testing.T ) {
1365+ // Create mock client
1366+ mockClient := github .NewClient (mock .NewMockedHTTPClient (tt .mockResponses ... ))
1367+ _ , handler := ListBranches (stubGetClientFn (mockClient ), translations .NullTranslationHelper )
1368+
13661369 // Create request
13671370 request := createMCPRequest (tt .args )
13681371
13691372 // Call handler
13701373 result , err := handler (context .Background (), request )
13711374 if tt .wantErr {
1372- require .NoError (t , err )
1373- textContent := getTextResult (t , result )
1375+ require .Error (t , err )
13741376 if tt .errContains != "" {
1375- assert .Contains (t , textContent . Text , tt .errContains )
1377+ assert .Contains (t , err . Error () , tt .errContains )
13761378 }
13771379 return
13781380 }
@@ -1383,21 +1385,12 @@ func Test_ListBranches(t *testing.T) {
13831385 require .NotEmpty (t , textContent .Text )
13841386
13851387 // Verify response
1386- var branches []github.Branch
1388+ var branches []* github.Branch
13871389 err = json .Unmarshal ([]byte (textContent .Text ), & branches )
13881390 require .NoError (t , err )
13891391 assert .Len (t , branches , 2 )
13901392 assert .Equal (t , "main" , * branches [0 ].Name )
13911393 assert .Equal (t , "develop" , * branches [1 ].Name )
13921394 })
13931395 }
1394-
1395- // Verify tool definition
1396- assert .Equal (t , "list_branches" , tool .Name )
1397- assert .NotEmpty (t , tool .Description )
1398- assert .Contains (t , tool .InputSchema .Properties , "owner" )
1399- assert .Contains (t , tool .InputSchema .Properties , "repo" )
1400- assert .Contains (t , tool .InputSchema .Properties , "page" )
1401- assert .Contains (t , tool .InputSchema .Properties , "perPage" )
1402- assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" })
14031396}
0 commit comments