|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + "github.com/google/go-github/v69/github" |
| 12 | + "github.com/migueleliasweb/go-github-mock/src/mock" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_ListNotifications(t *testing.T) { |
| 18 | + // Verify tool definition |
| 19 | + mockClient := github.NewClient(nil) |
| 20 | + tool, _ := ListNotifications(stubGetClientFn(mockClient), translations.NullTranslationHelper) |
| 21 | + |
| 22 | + assert.Equal(t, "list_notifications", tool.Name) |
| 23 | + assert.NotEmpty(t, tool.Description) |
| 24 | + assert.Contains(t, tool.InputSchema.Properties, "page") |
| 25 | + assert.Contains(t, tool.InputSchema.Properties, "per_page") |
| 26 | + assert.Contains(t, tool.InputSchema.Properties, "all") |
| 27 | + |
| 28 | + // Setup mock notifications |
| 29 | + mockNotifications := []*github.Notification{ |
| 30 | + { |
| 31 | + ID: github.String("1"), |
| 32 | + Reason: github.String("mention"), |
| 33 | + Subject: &github.NotificationSubject{ |
| 34 | + Title: github.String("Test Notification 1"), |
| 35 | + }, |
| 36 | + UpdatedAt: &github.Timestamp{Time: time.Now()}, |
| 37 | + URL: github.String("https://example.com/notifications/threads/1"), |
| 38 | + }, |
| 39 | + { |
| 40 | + ID: github.String("2"), |
| 41 | + Reason: github.String("team_mention"), |
| 42 | + Subject: &github.NotificationSubject{ |
| 43 | + Title: github.String("Test Notification 2"), |
| 44 | + }, |
| 45 | + UpdatedAt: &github.Timestamp{Time: time.Now()}, |
| 46 | + URL: github.String("https://example.com/notifications/threads/1"), |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + mockedClient *http.Client |
| 53 | + requestArgs map[string]interface{} |
| 54 | + expectError bool |
| 55 | + expectedResponse []*github.Notification |
| 56 | + expectedErrMsg string |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "list all notifications", |
| 60 | + mockedClient: mock.NewMockedHTTPClient( |
| 61 | + mock.WithRequestMatch( |
| 62 | + mock.GetNotifications, |
| 63 | + mockNotifications, |
| 64 | + ), |
| 65 | + ), |
| 66 | + requestArgs: map[string]interface{}{ |
| 67 | + "all": true, |
| 68 | + }, |
| 69 | + expectError: false, |
| 70 | + expectedResponse: mockNotifications, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "list unread notifications", |
| 74 | + mockedClient: mock.NewMockedHTTPClient( |
| 75 | + mock.WithRequestMatch( |
| 76 | + mock.GetNotifications, |
| 77 | + mockNotifications[:1], // Only the first notification |
| 78 | + ), |
| 79 | + ), |
| 80 | + requestArgs: map[string]interface{}{ |
| 81 | + "all": false, |
| 82 | + }, |
| 83 | + expectError: false, |
| 84 | + expectedResponse: mockNotifications[:1], |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + for _, tc := range tests { |
| 89 | + t.Run(tc.name, func(t *testing.T) { |
| 90 | + // Setup client with mock |
| 91 | + client := github.NewClient(tc.mockedClient) |
| 92 | + _, handler := ListNotifications(stubGetClientFn(client), translations.NullTranslationHelper) |
| 93 | + |
| 94 | + // Create call request |
| 95 | + request := createMCPRequest(tc.requestArgs) |
| 96 | + // Call handler |
| 97 | + result, err := handler(context.Background(), request) |
| 98 | + |
| 99 | + // Verify results |
| 100 | + if tc.expectError { |
| 101 | + require.Error(t, err) |
| 102 | + assert.Contains(t, err.Error(), tc.expectedErrMsg) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + require.NoError(t, err) |
| 107 | + textContent := getTextResult(t, result) |
| 108 | + |
| 109 | + // Unmarshal and verify the result |
| 110 | + var returnedNotifications []*github.Notification |
| 111 | + err = json.Unmarshal([]byte(textContent.Text), &returnedNotifications) |
| 112 | + require.NoError(t, err) |
| 113 | + assert.Equal(t, len(tc.expectedResponse), len(returnedNotifications)) |
| 114 | + for i, notification := range returnedNotifications { |
| 115 | + // Ensure all required fields are mocked |
| 116 | + assert.NotNil(t, notification.Subject, "Subject should not be nil") |
| 117 | + assert.NotNil(t, notification.Subject.Title, "Title should not be nil") |
| 118 | + assert.NotNil(t, notification.Reason, "Reason should not be nil") |
| 119 | + assert.NotNil(t, notification.URL, "URL should not be nil") |
| 120 | + assert.NotNil(t, notification.UpdatedAt, "UpdatedAt should not be nil") |
| 121 | + // assert.Equal(t, *tc.expectedResponse[i].ID, *notification.ID) |
| 122 | + assert.Equal(t, *tc.expectedResponse[i].Reason, *notification.Reason) |
| 123 | + // assert.Equal(t, *tc.expectedResponse[i].Subject.Title, *notification.Subject.Title) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments