@@ -1100,89 +1100,67 @@ func TestGranularCreatePullRequestReview(t *testing.T) {
11001100
11011101func TestGranularUpdatePullRequestDraftState (t * testing.T ) {
11021102 tests := []struct {
1103- name string
1104- draft bool
1103+ name string
1104+ initialDraftState bool
1105+ requestedDraftState bool
1106+ expectedActionCalls int
1107+ getStatusCode int
1108+ actionStatusCode int
1109+ expectError bool
1110+ expectedErrContains string
11051111 }{
1106- {name : "convert to draft" , draft : true },
1107- {name : "mark ready for review" , draft : false },
1112+ {name : "convert to draft" , initialDraftState : false , requestedDraftState : true , expectedActionCalls : 1 , getStatusCode : http .StatusOK , actionStatusCode : http .StatusOK },
1113+ {name : "mark ready for review" , initialDraftState : true , requestedDraftState : false , expectedActionCalls : 1 , getStatusCode : http .StatusOK , actionStatusCode : http .StatusOK },
1114+ {name : "no-op when already requested state" , initialDraftState : true , requestedDraftState : true , expectedActionCalls : 0 , getStatusCode : http .StatusOK , actionStatusCode : http .StatusOK },
1115+ {name : "returns error when action endpoint is forbidden" , initialDraftState : false , requestedDraftState : true , expectedActionCalls : 1 , getStatusCode : http .StatusOK , actionStatusCode : http .StatusForbidden , expectError : true , expectedErrContains : "failed to convert pull request to draft" },
1116+ {name : "returns error when pull request lookup is not found" , initialDraftState : false , requestedDraftState : true , expectedActionCalls : 0 , getStatusCode : http .StatusNotFound , actionStatusCode : http .StatusOK , expectError : true , expectedErrContains : "failed to get pull request" },
11081117 }
11091118
11101119 for _ , tc := range tests {
11111120 t .Run (tc .name , func (t * testing.T ) {
1112- var matchers []githubv4mock.Matcher
1113-
1114- matchers = append (matchers , githubv4mock .NewQueryMatcher (
1115- struct {
1116- Repository struct {
1117- PullRequest struct {
1118- ID githubv4.ID
1119- } `graphql:"pullRequest(number: $number)"`
1120- } `graphql:"repository(owner: $owner, name: $name)"`
1121- }{},
1122- map [string ]any {
1123- "owner" : githubv4 .String ("owner" ),
1124- "name" : githubv4 .String ("repo" ),
1125- "number" : githubv4 .Int (1 ),
1121+ actionCalls := 0
1122+ client := mustNewGHClient (t , MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
1123+ GetReposPullsByOwnerByRepoByPullNumber : func (w http.ResponseWriter , _ * http.Request ) {
1124+ if tc .getStatusCode != http .StatusOK {
1125+ writeJSONResponse (t , w , tc .getStatusCode , map [string ]any {"message" : "not found" })
1126+ return
1127+ }
1128+ writeJSONResponse (t , w , http .StatusOK , & gogithub.PullRequest {
1129+ Number : gogithub .Ptr (1 ),
1130+ Draft : gogithub .Ptr (tc .initialDraftState ),
1131+ })
11261132 },
1127- githubv4mock .DataResponse (map [string ]any {
1128- "repository" : map [string ]any {
1129- "pullRequest" : map [string ]any {"id" : "PR_123" },
1130- },
1131- }),
1132- ))
1133-
1134- if tc .draft {
1135- matchers = append (matchers , githubv4mock .NewMutationMatcher (
1136- struct {
1137- ConvertPullRequestToDraft struct {
1138- PullRequest struct {
1139- ID githubv4.ID
1140- IsDraft githubv4.Boolean
1141- }
1142- } `graphql:"convertPullRequestToDraft(input: $input)"`
1143- }{},
1144- githubv4.ConvertPullRequestToDraftInput {PullRequestID : githubv4 .ID ("PR_123" )},
1145- nil ,
1146- githubv4mock .DataResponse (map [string ]any {
1147- "convertPullRequestToDraft" : map [string ]any {
1148- "pullRequest" : map [string ]any {"id" : "PR_123" , "isDraft" : true },
1149- },
1150- }),
1151- ))
1152- } else {
1153- matchers = append (matchers , githubv4mock .NewMutationMatcher (
1154- struct {
1155- MarkPullRequestReadyForReview struct {
1156- PullRequest struct {
1157- ID githubv4.ID
1158- IsDraft githubv4.Boolean
1159- }
1160- } `graphql:"markPullRequestReadyForReview(input: $input)"`
1161- }{},
1162- githubv4.MarkPullRequestReadyForReviewInput {PullRequestID : githubv4 .ID ("PR_123" )},
1163- nil ,
1164- githubv4mock .DataResponse (map [string ]any {
1165- "markPullRequestReadyForReview" : map [string ]any {
1166- "pullRequest" : map [string ]any {"id" : "PR_123" , "isDraft" : false },
1167- },
1168- }),
1169- ))
1170- }
1133+ PostReposPullsByOwnerByRepoByPullNumberConvertToDraft : func (w http.ResponseWriter , _ * http.Request ) {
1134+ actionCalls ++
1135+ writeJSONResponse (t , w , tc .actionStatusCode , map [string ]any {"message" : "forbidden" })
1136+ },
1137+ PostReposPullsByOwnerByRepoByPullNumberReadyForReview : func (w http.ResponseWriter , _ * http.Request ) {
1138+ actionCalls ++
1139+ writeJSONResponse (t , w , tc .actionStatusCode , map [string ]any {"message" : "forbidden" })
1140+ },
1141+ }))
11711142
1172- gqlClient := githubv4 .NewClient (githubv4mock .NewMockedHTTPClient (matchers ... ))
1173- deps := BaseDeps {GQLClient : gqlClient }
1143+ deps := BaseDeps {Client : client }
11741144 serverTool := GranularUpdatePullRequestDraftState (translations .NullTranslationHelper )
11751145 handler := serverTool .Handler (deps )
11761146
11771147 request := createMCPRequest (map [string ]any {
11781148 "owner" : "owner" ,
11791149 "repo" : "repo" ,
11801150 "pullNumber" : float64 (1 ),
1181- "draft" : tc .draft ,
1151+ "draft" : tc .requestedDraftState ,
11821152 })
11831153 result , err := handler (ContextWithDeps (context .Background (), deps ), & request )
11841154 require .NoError (t , err )
1155+ if tc .expectError {
1156+ assert .True (t , result .IsError )
1157+ errorContent := getErrorResult (t , result )
1158+ assert .Contains (t , errorContent .Text , tc .expectedErrContains )
1159+ assert .Equal (t , tc .expectedActionCalls , actionCalls )
1160+ return
1161+ }
11851162 assert .False (t , result .IsError )
1163+ assert .Equal (t , tc .expectedActionCalls , actionCalls )
11861164 })
11871165 }
11881166}
0 commit comments