@@ -1022,6 +1022,131 @@ func Test_CreateBranch(t *testing.T) {
10221022 }
10231023}
10241024
1025+ func Test_DeleteBranch (t * testing.T ) {
1026+ // Verify tool definition once
1027+ serverTool := DeleteBranch (translations .NullTranslationHelper )
1028+ tool := serverTool .Tool
1029+ require .NoError (t , toolsnaps .Test (tool .Name , tool ))
1030+
1031+ schema , ok := tool .InputSchema .(* jsonschema.Schema )
1032+ require .True (t , ok , "InputSchema should be *jsonschema.Schema" )
1033+
1034+ assert .Equal (t , "delete_branch" , tool .Name )
1035+ assert .NotEmpty (t , tool .Description )
1036+ assert .False (t , tool .Annotations .ReadOnlyHint , "delete_branch must not be read-only" )
1037+ assert .Contains (t , schema .Properties , "owner" )
1038+ assert .Contains (t , schema .Properties , "repo" )
1039+ assert .Contains (t , schema .Properties , "branch" )
1040+ assert .ElementsMatch (t , schema .Required , []string {"owner" , "repo" , "branch" })
1041+
1042+ unprotectedBranch := & github.Branch {
1043+ Name : github .Ptr ("feature" ),
1044+ Protected : github .Ptr (false ),
1045+ }
1046+ protectedBranch := & github.Branch {
1047+ Name : github .Ptr ("main" ),
1048+ Protected : github .Ptr (true ),
1049+ }
1050+
1051+ tests := []struct {
1052+ name string
1053+ mockedClient * http.Client
1054+ requestArgs map [string ]any
1055+ expectError bool
1056+ expectedErrMsg string
1057+ expectedSuccess string
1058+ }{
1059+ {
1060+ name : "successful branch deletion" ,
1061+ mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
1062+ GetReposBranchesByOwnerByRepoByBranch : mockResponse (t , http .StatusOK , unprotectedBranch ),
1063+ DeleteReposGitRefsByOwnerByRepoByRef : mockResponse (t , http .StatusNoContent , nil ),
1064+ }),
1065+ requestArgs : map [string ]any {
1066+ "owner" : "owner" ,
1067+ "repo" : "repo" ,
1068+ "branch" : "feature" ,
1069+ },
1070+ expectError : false ,
1071+ expectedSuccess : `Successfully deleted branch "feature"` ,
1072+ },
1073+ {
1074+ name : "refuses to delete protected branch" ,
1075+ mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
1076+ GetReposBranchesByOwnerByRepoByBranch : mockResponse (t , http .StatusOK , protectedBranch ),
1077+ }),
1078+ requestArgs : map [string ]any {
1079+ "owner" : "owner" ,
1080+ "repo" : "repo" ,
1081+ "branch" : "main" ,
1082+ },
1083+ expectError : true ,
1084+ expectedErrMsg : "protected and cannot be deleted" ,
1085+ },
1086+ {
1087+ name : "branch not found" ,
1088+ mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
1089+ GetReposBranchesByOwnerByRepoByBranch : func (w http.ResponseWriter , _ * http.Request ) {
1090+ w .WriteHeader (http .StatusNotFound )
1091+ _ , _ = w .Write ([]byte (`{"message": "Branch not found"}` ))
1092+ },
1093+ }),
1094+ requestArgs : map [string ]any {
1095+ "owner" : "owner" ,
1096+ "repo" : "repo" ,
1097+ "branch" : "nonexistent" ,
1098+ },
1099+ expectError : true ,
1100+ expectedErrMsg : "failed to get branch" ,
1101+ },
1102+ {
1103+ name : "fail to delete reference" ,
1104+ mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
1105+ GetReposBranchesByOwnerByRepoByBranch : mockResponse (t , http .StatusOK , unprotectedBranch ),
1106+ DeleteReposGitRefsByOwnerByRepoByRef : func (w http.ResponseWriter , _ * http.Request ) {
1107+ w .WriteHeader (http .StatusUnprocessableEntity )
1108+ _ , _ = w .Write ([]byte (`{"message": "Reference does not exist"}` ))
1109+ },
1110+ }),
1111+ requestArgs : map [string ]any {
1112+ "owner" : "owner" ,
1113+ "repo" : "repo" ,
1114+ "branch" : "feature" ,
1115+ },
1116+ expectError : true ,
1117+ expectedErrMsg : "failed to delete branch" ,
1118+ },
1119+ }
1120+
1121+ for _ , tc := range tests {
1122+ t .Run (tc .name , func (t * testing.T ) {
1123+ client := mustNewGHClient (t , tc .mockedClient )
1124+ deps := BaseDeps {
1125+ Client : client ,
1126+ }
1127+ handler := serverTool .Handler (deps )
1128+
1129+ request := createMCPRequest (tc .requestArgs )
1130+
1131+ result , err := handler (ContextWithDeps (context .Background (), deps ), & request )
1132+
1133+ if tc .expectError {
1134+ require .NoError (t , err )
1135+ require .True (t , result .IsError )
1136+ errorContent := getErrorResult (t , result )
1137+ assert .Contains (t , errorContent .Text , tc .expectedErrMsg )
1138+ return
1139+ }
1140+
1141+ require .NoError (t , err )
1142+ require .False (t , result .IsError )
1143+
1144+ textContent := getTextResult (t , result )
1145+ assert .Contains (t , textContent .Text , tc .expectedSuccess )
1146+ })
1147+ }
1148+ }
1149+
10251150func Test_GetCommit (t * testing.T ) {
10261151 // Verify tool definition once
10271152 serverTool := GetCommit (translations .NullTranslationHelper )
0 commit comments