| 1 |
package zulip_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/zulip" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks" |
| 15 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 16 |
) |
| 17 |
|
| 18 |
// mockLogger is a simple logger implementation for testing. |
| 19 |
type mockLogger struct{} |
| 20 |
|
| 21 |
func (m *mockLogger) Print(_ ...any) {} |
| 22 |
func (m *mockLogger) Printf(_ string, _ ...any) {} |
| 23 |
func (m *mockLogger) Println(_ ...any) {} |
| 24 |
|
| 25 |
// createTestService creates a Zulip service instance configured for testing. |
| 26 |
func createTestService( |
| 27 |
t *testing.T, |
| 28 |
serviceURL string, |
| 29 |
httpClients ...zulip.HTTPClient, |
| 30 |
) *zulip.Service { |
| 31 |
t.Helper() |
| 32 |
|
| 33 |
service := &zulip.Service{} |
| 34 |
|
| 35 |
parsedURL, err := url.Parse(serviceURL) |
| 36 |
if err != nil { |
| 37 |
t.Fatalf("failed to parse service URL: %v", err) |
| 38 |
} |
| 39 |
|
| 40 |
err = service.Initialize(parsedURL, &mockLogger{}) |
| 41 |
if err != nil { |
| 42 |
t.Fatalf("failed to initialize service: %v", err) |
| 43 |
} |
| 44 |
|
| 45 |
// Override the HTTPClient if provided (after Initialize sets the default) |
| 46 |
if len(httpClients) > 0 && httpClients[0] != nil { |
| 47 |
service.HTTPClient = httpClients[0] |
| 48 |
} |
| 49 |
|
| 50 |
return service |
| 51 |
} |
| 52 |
|
| 53 |
// createTestParams creates test parameters with the given key-value pairs. |
| 54 |
func createTestParams(pairs ...string) *types.Params { |
| 55 |
params := make(types.Params) |
| 56 |
|
| 57 |
for i := 0; i < len(pairs); i += 2 { |
| 58 |
if i+1 < len(pairs) { |
| 59 |
params[pairs[i]] = pairs[i+1] |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return ¶ms |
| 64 |
} |
| 65 |
|
| 66 |
// createMockResponse creates a mock HTTP response with the given status code and body. |
| 67 |
func createMockResponse(statusCode int, body string) *http.Response { |
| 68 |
resp := &http.Response{ |
| 69 |
StatusCode: statusCode, |
| 70 |
Body: io.NopCloser(strings.NewReader(body)), |
| 71 |
Header: make(http.Header), |
| 72 |
} |
| 73 |
|
| 74 |
return resp |
| 75 |
} |
| 76 |
|
| 77 |
// assertRequestContains asserts that the HTTP request body contains the expected content. |
| 78 |
func assertRequestContains(t *testing.T, mockClient *mocks.MockHTTPClient, expectedContent string) { |
| 79 |
t.Helper() |
| 80 |
|
| 81 |
found := false |
| 82 |
|
| 83 |
for i := range mockClient.Calls { |
| 84 |
call := &mockClient.Calls[i] |
| 85 |
if call.Method == "Do" { |
| 86 |
req := call.Arguments[0].(*http.Request) |
| 87 |
|
| 88 |
body, err := io.ReadAll(req.Body) |
| 89 |
if err != nil { |
| 90 |
continue |
| 91 |
} |
| 92 |
// Reset the body for potential future reads |
| 93 |
req.Body = io.NopCloser(bytes.NewReader(body)) |
| 94 |
if strings.Contains(string(body), expectedContent) { |
| 95 |
found = true |
| 96 |
|
| 97 |
break |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if !found { |
| 103 |
t.Errorf("Expected request body to contain %q, but no matching call found", expectedContent) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// assertRequestMatches asserts that at least one HTTP request matches the given predicate. |
| 108 |
func assertRequestMatches( |
| 109 |
t *testing.T, |
| 110 |
mockClient *mocks.MockHTTPClient, |
| 111 |
predicate func(*http.Request) bool, |
| 112 |
description string, |
| 113 |
) { |
| 114 |
t.Helper() |
| 115 |
|
| 116 |
found := false |
| 117 |
|
| 118 |
for i := range mockClient.Calls { |
| 119 |
call := &mockClient.Calls[i] |
| 120 |
if call.Method == "Do" { |
| 121 |
req := call.Arguments[0].(*http.Request) |
| 122 |
if predicate(req) { |
| 123 |
found = true |
| 124 |
|
| 125 |
break |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if !found { |
| 131 |
t.Errorf("Expected request to match %s, but no matching call found", description) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// assertNoMessagesAPICall asserts that no HTTP call was made to the Zulip messages endpoint |
| 136 |
// (used for early validation errors after the register fetch has occurred). |
| 137 |
func assertNoMessagesAPICall(t *testing.T, mockClient *mocks.MockHTTPClient) { |
| 138 |
t.Helper() |
| 139 |
|
| 140 |
for i := range mockClient.Calls { |
| 141 |
call := &mockClient.Calls[i] |
| 142 |
if call.Method == "Do" { |
| 143 |
req := call.Arguments[0].(*http.Request) |
| 144 |
if req.URL.Path == "/api/v1/messages" { |
| 145 |
t.Errorf("unexpected call to messages API path") |
| 146 |
|
| 147 |
return |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// setupRegisterThenMessage sets up the mock client to expect the internal /register call |
| 154 |
// (for server limits, returns no specific limits so defaults are used) followed by the |
| 155 |
// messages API call with the provided response. This is required because Send now triggers |
| 156 |
// fetchLimits on first use. |
| 157 |
func setupRegisterThenMessage(t *testing.T, mockClient *mocks.MockHTTPClient, msgResp *http.Response) { |
| 158 |
t.Helper() |
| 159 |
|
| 160 |
// register (limits fetch) - body without max_* fields -> fallback to defaults |
| 161 |
mockClient.On("Do", mock.AnythingOfType("*http.Request")). |
| 162 |
Return(createMockResponse(http.StatusOK, `{"result": "success"}`), nil). |
| 163 |
Once() |
| 164 |
|
| 165 |
// actual messages send |
| 166 |
mockClient.On("Do", mock.AnythingOfType("*http.Request")). |
| 167 |
Return(msgResp, nil). |
| 168 |
Once() |
| 169 |
} |