| 1 | package discord_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/stretchr/testify/mock" |
| 12 | |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/discord" |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 15 | ) |
| 16 | |
| 17 | // MockHTTPClient is a testify mock that implements the HTTPClient interface. |
| 18 | type MockHTTPClient struct { |
| 19 | mock.Mock |
| 20 | } |
| 21 | |
| 22 | // mockLogger is a simple logger implementation for testing. |
| 23 | type mockLogger struct{} |
| 24 | |
| 25 | func (m *mockLogger) Print(_ ...any) {} |
| 26 | func (m *mockLogger) Printf(_ string, _ ...any) {} |
| 27 | func (m *mockLogger) Println(_ ...any) {} |
| 28 | |
| 29 | // createTestService creates a Discord service instance configured for testing. |
| 30 | func createTestService( |
| 31 | t *testing.T, |
| 32 | webhookURL string, |
| 33 | httpClients ...discord.HTTPClient, |
| 34 | ) *discord.Service { |
| 35 | t.Helper() |
| 36 | |
| 37 | service := &discord.Service{} |
| 38 | |
| 39 | parsedURL, err := url.Parse(webhookURL) |
| 40 | if err != nil { |
| 41 | t.Fatalf("failed to parse webhook URL: %v", err) |
| 42 | } |
| 43 | |
| 44 | err = service.Initialize(parsedURL, &mockLogger{}) |
| 45 | if err != nil { |
| 46 | t.Fatalf("failed to initialize service: %v", err) |
| 47 | } |
| 48 | |
| 49 | // Override the HTTPClient if provided (after Initialize sets the default) |
| 50 | if len(httpClients) > 0 && httpClients[0] != nil { |
| 51 | service.HTTPClient = httpClients[0] |
| 52 | } |
| 53 | |
| 54 | return service |
| 55 | } |
| 56 | |
| 57 | // createTestMessageItem creates a test MessageItem with the given text. |
| 58 | func createTestMessageItem(text string) types.MessageItem { |
| 59 | return types.MessageItem{Text: text} |
| 60 | } |
| 61 | |
| 62 | // createTestMessageItemWithFile creates a test MessageItem with a file attachment. |
| 63 | func createTestMessageItemWithFile(text, filename string, data []byte) types.MessageItem { |
| 64 | return types.MessageItem{ |
| 65 | Text: text, |
| 66 | File: &types.File{ |
| 67 | Name: filename, |
| 68 | Data: data, |
| 69 | }, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // createTestParams creates test parameters with the given key-value pairs. |
| 74 | func createTestParams(pairs ...string) *types.Params { |
| 75 | params := make(types.Params) |
| 76 | |
| 77 | for i := 0; i < len(pairs); i += 2 { |
| 78 | if i+1 < len(pairs) { |
| 79 | params[pairs[i]] = pairs[i+1] |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return ¶ms |
| 84 | } |
| 85 | |
| 86 | func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) { |
| 87 | // Capture the request body before it's consumed |
| 88 | if req.Body != nil { |
| 89 | body, err := io.ReadAll(req.Body) |
| 90 | if err == nil { |
| 91 | req.Body = io.NopCloser(bytes.NewReader(body)) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | args := m.Called(req) |
| 96 | |
| 97 | if args.Get(0) == nil { |
| 98 | return nil, args.Error(1) |
| 99 | } |
| 100 | |
| 101 | return args.Get(0).(*http.Response), args.Error(1) |
| 102 | } |
| 103 | |
| 104 | // createMockResponse creates a mock HTTP response with the given status code and body. |
| 105 | func createMockResponse(statusCode int, body string, headers ...map[string]string) *http.Response { |
| 106 | resp := &http.Response{ |
| 107 | StatusCode: statusCode, |
| 108 | Body: io.NopCloser(strings.NewReader(body)), |
| 109 | Header: make(http.Header), |
| 110 | } |
| 111 | |
| 112 | if len(headers) > 0 && headers[0] != nil { |
| 113 | for k, v := range headers[0] { |
| 114 | resp.Header.Set(k, v) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return resp |
| 119 | } |
| 120 | |
| 121 | // assertRequestMade asserts that an HTTP POST request was made to the expected URL. |
| 122 | func assertRequestMade( |
| 123 | t *testing.T, |
| 124 | mockClient *MockHTTPClient, |
| 125 | expectedURL string, |
| 126 | ) { |
| 127 | t.Helper() |
| 128 | |
| 129 | found := false |
| 130 | |
| 131 | for i := range mockClient.Calls { |
| 132 | call := &mockClient.Calls[i] |
| 133 | if call.Method == "Do" { |
| 134 | req := call.Arguments[0].(*http.Request) |
| 135 | if req.Method == http.MethodPost && req.URL.String() == expectedURL { |
| 136 | found = true |
| 137 | |
| 138 | break |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if !found { |
| 144 | t.Errorf("Expected POST request to %s, but no matching call found", expectedURL) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // assertRequestContains asserts that the HTTP request body contains the expected content. |
| 149 | func assertRequestContains(t *testing.T, mockClient *MockHTTPClient, expectedContent string) { |
| 150 | t.Helper() |
| 151 | |
| 152 | found := false |
| 153 | |
| 154 | for i := range mockClient.Calls { |
| 155 | call := &mockClient.Calls[i] |
| 156 | if call.Method == "Do" { |
| 157 | req := call.Arguments[0].(*http.Request) |
| 158 | |
| 159 | body, err := io.ReadAll(req.Body) |
| 160 | if err != nil { |
| 161 | continue |
| 162 | } |
| 163 | // Reset the body for potential future reads |
| 164 | req.Body = io.NopCloser(bytes.NewReader(body)) |
| 165 | if strings.Contains(string(body), expectedContent) { |
| 166 | found = true |
| 167 | |
| 168 | break |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if !found { |
| 174 | t.Errorf("Expected request body to contain %q, but no matching call found", expectedContent) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // assertRequestBody asserts that the HTTP request body exactly matches the expected content. |
| 179 | func assertRequestBody(t *testing.T, mockClient *MockHTTPClient, expectedBody string) { |
| 180 | t.Helper() |
| 181 | |
| 182 | found := false |
| 183 | |
| 184 | for i := range mockClient.Calls { |
| 185 | call := &mockClient.Calls[i] |
| 186 | if call.Method == "Do" { |
| 187 | req := call.Arguments[0].(*http.Request) |
| 188 | |
| 189 | body, err := io.ReadAll(req.Body) |
| 190 | if err != nil { |
| 191 | continue |
| 192 | } |
| 193 | // Reset the body for potential future reads |
| 194 | req.Body = io.NopCloser(bytes.NewReader(body)) |
| 195 | if string(body) == expectedBody { |
| 196 | found = true |
| 197 | |
| 198 | break |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if !found { |
| 204 | t.Errorf("Expected request body to be %q, but no matching call found", expectedBody) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // assertRequestMatches asserts that at least one HTTP request matches the given predicate. |
| 209 | func assertRequestMatches( |
| 210 | t *testing.T, |
| 211 | mockClient *MockHTTPClient, |
| 212 | predicate func(*http.Request) bool, |
| 213 | description string, |
| 214 | ) { |
| 215 | t.Helper() |
| 216 | |
| 217 | found := false |
| 218 | |
| 219 | for i := range mockClient.Calls { |
| 220 | call := &mockClient.Calls[i] |
| 221 | if call.Method == "Do" { |
| 222 | req := call.Arguments[0].(*http.Request) |
| 223 | if predicate(req) { |
| 224 | found = true |
| 225 | |
| 226 | break |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if !found { |
| 232 | t.Errorf("Expected request to match %s, but no matching call found", description) |
| 233 | } |
| 234 | } |