@@ -1293,3 +1293,118 @@ func Test_PushFiles(t *testing.T) {
1293
1293
})
1294
1294
}
1295
1295
}
1296
+
1297
+ func Test_ListBranches (t * testing.T ) {
1298
+ // Verify tool definition once
1299
+ mockClient := github .NewClient (nil )
1300
+ tool , _ := listBranches (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 .Ptr ("main" ),
1314
+ Commit : & github.RepositoryCommit {SHA : github .Ptr ("abc123" )},
1315
+ },
1316
+ {
1317
+ Name : github .Ptr ("develop" ),
1318
+ Commit : & github.RepositoryCommit {SHA : github .Ptr ("def456" )},
1319
+ },
1320
+ }
1321
+
1322
+ // Define test cases
1323
+ tests := []struct {
1324
+ name string
1325
+ mockedClient * http.Client
1326
+ requestArgs map [string ]interface {}
1327
+ expectError bool
1328
+ expectedErrMsg string
1329
+ }{
1330
+ {
1331
+ name : "success" ,
1332
+ mockedClient : mock .NewMockedHTTPClient (
1333
+ mock .WithRequestMatch (
1334
+ mock .GetReposBranchesByOwnerByRepo ,
1335
+ mockBranches ,
1336
+ ),
1337
+ ),
1338
+ requestArgs : map [string ]interface {}{
1339
+ "owner" : "owner" ,
1340
+ "repo" : "repo" ,
1341
+ },
1342
+ expectError : false ,
1343
+ },
1344
+ {
1345
+ name : "missing owner" ,
1346
+ mockedClient : mock .NewMockedHTTPClient (),
1347
+ requestArgs : map [string ]interface {}{
1348
+ "repo" : "repo" ,
1349
+ },
1350
+ expectError : false ,
1351
+ expectedErrMsg : "owner is required" ,
1352
+ },
1353
+ {
1354
+ name : "missing repo" ,
1355
+ mockedClient : mock .NewMockedHTTPClient (),
1356
+ requestArgs : map [string ]interface {}{
1357
+ "owner" : "owner" ,
1358
+ },
1359
+ expectError : false ,
1360
+ expectedErrMsg : "repo is required" ,
1361
+ },
1362
+ {
1363
+ name : "repository not found" ,
1364
+ mockedClient : mock .NewMockedHTTPClient (
1365
+ mock .WithRequestMatchHandler (
1366
+ mock .GetReposBranchesByOwnerByRepo ,
1367
+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
1368
+ w .WriteHeader (http .StatusNotFound )
1369
+ _ , _ = w .Write ([]byte (`{"message": "Not Found"}` ))
1370
+ }),
1371
+ ),
1372
+ ),
1373
+ requestArgs : map [string ]interface {}{
1374
+ "owner" : "owner" ,
1375
+ "repo" : "nonexistent-repo" ,
1376
+ },
1377
+ expectError : true ,
1378
+ expectedErrMsg : "failed to list branches" ,
1379
+ },
1380
+ }
1381
+
1382
+ for _ , tt := range tests {
1383
+ t .Run (tt .name , func (t * testing.T ) {
1384
+ client := github .NewClient (tt .mockedClient )
1385
+ _ , handler := listBranches (client , translations .NullTranslationHelper )
1386
+
1387
+ // Create call request using helper function
1388
+ request := createMCPRequest (tt .requestArgs )
1389
+
1390
+ // Call handler
1391
+ result , err := handler (context .Background (), request )
1392
+
1393
+ if tt .expectError {
1394
+ assert .Error (t , err )
1395
+ assert .Contains (t , err .Error (), tt .expectedErrMsg )
1396
+ } else {
1397
+ if tt .expectedErrMsg != "" {
1398
+ assert .NotNil (t , result )
1399
+ textContent := getTextResult (t , result )
1400
+ assert .Contains (t , textContent .Text , tt .expectedErrMsg )
1401
+ } else {
1402
+ assert .NoError (t , err )
1403
+ assert .NotNil (t , result )
1404
+ textContent := getTextResult (t , result )
1405
+ assert .NotEmpty (t , textContent .Text )
1406
+ }
1407
+ }
1408
+ })
1409
+ }
1410
+ }
0 commit comments