77 "net/http"
88 "testing"
99
10+ "github.com/github/github-mcp-server/v2/pkg/ifc"
1011 "github.com/github/github-mcp-server/v2/pkg/inventory"
1112 "github.com/github/github-mcp-server/v2/pkg/translations"
1213 "github.com/modelcontextprotocol/go-sdk/mcp"
@@ -16,6 +17,88 @@ import (
1617
1718const agentMergeEnrollmentResponse = `{"allowed_actions":["fix_ci"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}`
1819
20+ func TestGetPullRequestAgentMergeEnrollment (t * testing.T ) {
21+ for _ , protocol := range []string {"2025-11-25" , inventory .ProtocolVersionMultiRoundTrip } {
22+ for _ , tc := range []struct {
23+ name , response string
24+ status int
25+ isError bool
26+ }{
27+ {"enabled" , agentMergeEnrollmentResponse , http .StatusOK , false },
28+ {"future action" , `{"allowed_actions":["respond_to_copilot_code_reviews"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}` , http .StatusOK , false },
29+ {"not found" , `{"message":"Not Found"}` , http .StatusNotFound , true },
30+ {"forbidden" , `{"message":"Forbidden"}` , http .StatusForbidden , true },
31+ {"unavailable" , `{"message":"Unavailable"}` , http .StatusServiceUnavailable , true },
32+ {"unexpected status" , "" , http .StatusNoContent , true },
33+ {"incomplete" , `{}` , http .StatusOK , true },
34+ {"malformed" , `{` , http .StatusOK , true },
35+ } {
36+ t .Run (protocol + "/" + tc .name , func (t * testing.T ) {
37+ calls := 0
38+ deps := & BaseDeps {
39+ Client : mustNewGHClient (t , MockHTTPClientWithHandler (func (w http.ResponseWriter , r * http.Request ) {
40+ calls ++
41+ assert .Equal (t , "GET /agents/repos/octo/widgets/pulls/42/agent-merge" , r .Method + " " + r .URL .Path )
42+ assert .Equal (t , "2026-03-10" , r .Header .Get ("X-GitHub-Api-Version" ))
43+ w .WriteHeader (tc .status )
44+ _ , _ = fmt .Fprint (w , tc .response )
45+ })),
46+ featureChecker : featureCheckerFor (FeatureFlagIFCLabels ),
47+ }
48+ server := mcp .NewServer (& mcp.Implementation {Name : "agent-merge-read-test" , Version : "1" }, nil )
49+ server .AddReceivingMiddleware (InjectDepsMiddleware (deps ))
50+ tool := GranularGetPullRequestAgentMergeEnrollment (translations .NullTranslationHelper )
51+ tool .RegisterFunc (server , deps )
52+ session := connectCommentVisibilityClient (t , server , protocol )
53+ result , err := session .CallTool (t .Context (), & mcp.CallToolParams {
54+ Name : tool .Tool .Name , Arguments : json .RawMessage (`{"owner":"octo","repo":"widgets","pullNumber":42}` ),
55+ })
56+ require .NoError (t , err )
57+ assert .Equal (t , 1 , calls )
58+ require .Equal (t , tc .isError , result .IsError , "%+v" , result .Content )
59+ if tc .isError {
60+ if tc .status == http .StatusNotFound {
61+ assert .Contains (t , getTextResult (t , result ).Text , "404 may mean" )
62+ }
63+ return
64+ }
65+ expected := `{"enabled":true,"enrollment":` + tc .response + `}`
66+ assert .JSONEq (t , expected , getTextResult (t , result ).Text )
67+ if protocol == inventory .ProtocolVersionMultiRoundTrip {
68+ assert .JSONEq (t , expected , mustMarshalJSON (t , result .StructuredContent ))
69+ }
70+ assert .JSONEq (t , mustMarshalJSON (t , ifc .PrivateTrusted ()), mustMarshalJSON (t , result .Meta ["ifc" ]))
71+ })
72+ }
73+ }
74+ }
75+
76+ func TestGetPullRequestAgentMergeEnrollmentValidation (t * testing.T ) {
77+ tool := GranularGetPullRequestAgentMergeEnrollment (translations .NullTranslationHelper )
78+ calls := 0
79+ deps := BaseDeps {Client : mustNewGHClient (t , MockHTTPClientWithHandler (func (http.ResponseWriter , * http.Request ) {
80+ calls ++
81+ }))}
82+ for _ , args := range []string {
83+ `{"owner":" ","repo":"widgets","pullNumber":42}` ,
84+ `{"owner":"octo","repo":"","pullNumber":42}` ,
85+ `{"owner":"octo","repo":"widgets","pullNumber":0}` ,
86+ `{"owner":"octo","repo":"widgets","pullNumber":1.5}` ,
87+ } {
88+ t .Run (args , func (t * testing.T ) {
89+ var input map [string ]any
90+ require .NoError (t , json .Unmarshal ([]byte (args ), & input ))
91+ request := createMCPRequest (input )
92+ result , err := tool .Handler (deps )(ContextWithDeps (t .Context (), deps ), & request )
93+ if err == nil {
94+ require .NotNil (t , result )
95+ assert .True (t , result .IsError )
96+ }
97+ })
98+ }
99+ assert .Zero (t , calls )
100+ }
101+
19102func TestUpdatePullRequestAgentMergeEnrollment (t * testing.T ) {
20103 for _ , protocol := range []string {"2025-11-25" , inventory .ProtocolVersionMultiRoundTrip } {
21104 for _ , tc := range []struct {
@@ -123,11 +206,12 @@ func TestUpdatePullRequestAgentMergeEnrollmentFeatureGate(t *testing.T) {
123206 }
124207 inv , err := NewInventory (translations .NullTranslationHelper ).WithFeatureChecker (checker ).Build ()
125208 require .NoError (t , err )
126- found := false
209+ found := map [ string ] bool {}
127210 for _ , tool := range inv .AvailableTools (t .Context ()) {
128- found = found || tool .Tool .Name == "update_pull_request_agent_merge_enrollment"
211+ found [ tool .Tool .Name ] = true
129212 }
130- assert .Equal (t , enabled , found )
213+ assert .Equal (t , enabled , found ["get_pull_request_agent_merge_enrollment" ])
214+ assert .Equal (t , enabled , found ["update_pull_request_agent_merge_enrollment" ])
131215 })
132216 }
133217}
0 commit comments