|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + "github.com/google/go-github/v92/github" |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestNotificationOutputFidelity(t *testing.T) { |
| 18 | + for _, subjectType := range []string{"Issue", "PullRequest", "Discussion", "FutureSubject"} { |
| 19 | + t.Run(subjectType, func(t *testing.T) { |
| 20 | + notification := &github.Notification{ |
| 21 | + ID: new("123"), Reason: new("agent_session_finished"), |
| 22 | + Subject: &github.NotificationSubject{ |
| 23 | + Type: new(subjectType), |
| 24 | + URL: new("https://github.example/api/v3/repos/owner/repo/issues/42"), |
| 25 | + LatestCommentURL: new("legacy-only-comment-url"), |
| 26 | + }, |
| 27 | + URL: new("legacy-only-thread-url"), |
| 28 | + } |
| 29 | + deps := BaseDeps{Client: mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 30 | + GetNotifications: mockResponse(t, http.StatusOK, []*github.Notification{notification}), |
| 31 | + GetReposNotificationsByOwnerByRepo: mockResponse(t, http.StatusOK, []*github.Notification{notification}), |
| 32 | + GetNotificationsThreadsByThreadID: mockResponse(t, http.StatusOK, notification), |
| 33 | + }))} |
| 34 | + for _, protocol := range []string{"2025-11-25", inventory.ProtocolVersionMultiRoundTrip} { |
| 35 | + server := mcp.NewServer(&mcp.Implementation{Name: "notification-fidelity", Version: "v1"}, nil) |
| 36 | + server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) |
| 37 | + for _, tool := range []inventory.ServerTool{ |
| 38 | + ListNotifications(translations.NullTranslationHelper), |
| 39 | + GetNotificationDetails(translations.NullTranslationHelper), |
| 40 | + } { |
| 41 | + tool.RegisterFunc(server, deps) |
| 42 | + } |
| 43 | + client := connectCommentVisibilityClient(t, server, protocol) |
| 44 | + for _, call := range []mcp.CallToolParams{ |
| 45 | + {Name: "list_notifications", Arguments: map[string]any{}}, |
| 46 | + {Name: "list_notifications", Arguments: map[string]any{"owner": "owner", "repo": "repo"}}, |
| 47 | + {Name: "get_notification_details", Arguments: map[string]any{"notificationID": "123"}}, |
| 48 | + } { |
| 49 | + result, err := client.CallTool(context.Background(), &call) |
| 50 | + require.NoError(t, err) |
| 51 | + require.False(t, result.IsError, "%s", getTextResult(t, result).Text) |
| 52 | + if protocol == "2025-11-25" { |
| 53 | + var expected any = notification |
| 54 | + if call.Name == "list_notifications" { |
| 55 | + expected = []*github.Notification{notification} |
| 56 | + } |
| 57 | + assert.Equal(t, mustMarshalJSON(t, expected), getTextResult(t, result).Text) |
| 58 | + assert.Nil(t, result.StructuredContent) |
| 59 | + continue |
| 60 | + } |
| 61 | + require.NotNil(t, result.StructuredContent) |
| 62 | + text := mustMarshalJSON(t, result.StructuredContent) |
| 63 | + assert.JSONEq(t, text, getTextResult(t, result).Text) |
| 64 | + assert.Contains(t, text, `"reason":"agent_session_finished"`) |
| 65 | + assert.Contains(t, text, `"type":"`+subjectType+`"`) |
| 66 | + assert.Contains(t, text, `"url":"`+notification.Subject.GetURL()+`"`) |
| 67 | + assert.NotContains(t, text, "legacy-only") |
| 68 | + resolved, err := notificationOutputSchema(call.Name == "list_notifications").Resolve(nil) |
| 69 | + require.NoError(t, err) |
| 70 | + var output any |
| 71 | + require.NoError(t, json.Unmarshal([]byte(text), &output)) |
| 72 | + require.NoError(t, resolved.Validate(output)) |
| 73 | + } |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments