| 1 | package zulip_testscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src . bytes/" |
| 5 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 6 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 7 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 9 | "testingscip-go gomod github.com/golang/go/src . testing/" |
| 10 | |
| 11 | "github.com/stretchr/testify/mockscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/" |
| 12 | |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulipscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/" |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/" |
| 15 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 16 | ) |
| 17 | |
| 18 | // mockLogger is a simple logger implementation for testing. |
| 19 | type mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger# struct{} |
| 20 | |
| 21 | func (m *mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#) Printscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#Print().(_ ...any) {} |
| 22 | func (m *mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#) Printfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#Printf().(_ string, _ ...any) {} |
| 23 | func (m *mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#) Printlnscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#Println().(_ ...any) {} |
| 24 | |
| 25 | // createTestService creates a Zulip service instance configured for testing. |
| 26 | func createTestServicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/createTestService().( |
| 27 | t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, |
| 28 | serviceURL string, |
| 29 | httpClients ...zulipscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/HTTPClient#, |
| 30 | ) *zulipscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/.Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service# { |
| 31 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 32 | |
| 33 | service := &zulipscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/.Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#{} |
| 34 | |
| 35 | parsedURL, err := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().(serviceURL) |
| 36 | if err != nil { |
| 37 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("failed to parse service URL: %v", err) |
| 38 | } |
| 39 | |
| 40 | err = service.Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Initialize().(parsedURL, &mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/mockLogger#{}) |
| 41 | if err != nil { |
| 42 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#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.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient. = httpClients[0] |
| 48 | } |
| 49 | |
| 50 | return service |
| 51 | } |
| 52 | |
| 53 | // createTestParams creates test parameters with the given key-value pairs. |
| 54 | func createTestParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/createTestParams().(pairs ...string) *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params# { |
| 55 | params := make(typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/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 createMockResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/createMockResponse().(statusCode int, body string) *httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response# { |
| 68 | resp := &httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#{ |
| 69 | StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode.: statusCode, |
| 70 | Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.: ioscip-go gomod github.com/golang/go/src . io/.NopCloserscip-go gomod github.com/golang/go/src . io/NopCloser().(stringsscip-go gomod github.com/golang/go/src . strings/.NewReaderscip-go gomod github.com/golang/go/src . strings/NewReader().(body)), |
| 71 | Headerscip-go gomod github.com/golang/go/src . `net/http`/Response#Header.: make(httpscip-go gomod github.com/golang/go/src . `net/http`/.Headerscip-go gomod github.com/golang/go/src . `net/http`/Header#), |
| 72 | } |
| 73 | |
| 74 | return resp |
| 75 | } |
| 76 | |
| 77 | // assertRequestContains asserts that the HTTP request body contains the expected content. |
| 78 | func assertRequestContainsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/assertRequestContains().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, mockClient *mocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/.MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/MockHTTPClient#, expectedContent string) { |
| 79 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 80 | |
| 81 | found := false |
| 82 | |
| 83 | for i := range mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls. { |
| 84 | call := &mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls.[i] |
| 85 | if call.Methodscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Method. == "Do" { |
| 86 | req := call.Argumentsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Arguments.[0].(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) |
| 87 | |
| 88 | body, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body.) |
| 89 | if err != nil { |
| 90 | continue |
| 91 | } |
| 92 | // Reset the body for potential future reads |
| 93 | req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body. = ioscip-go gomod github.com/golang/go/src . io/.NopCloserscip-go gomod github.com/golang/go/src . io/NopCloser().(bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().(body)) |
| 94 | if stringsscip-go gomod github.com/golang/go/src . strings/.Containsscip-go gomod github.com/golang/go/src . strings/Contains().(string(body), expectedContent) { |
| 95 | found = true |
| 96 | |
| 97 | break |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if !found { |
| 103 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#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 assertRequestMatchesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/assertRequestMatches().( |
| 109 | t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, |
| 110 | mockClient *mocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/.MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/MockHTTPClient#, |
| 111 | predicate func(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) bool, |
| 112 | description string, |
| 113 | ) { |
| 114 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 115 | |
| 116 | found := false |
| 117 | |
| 118 | for i := range mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls. { |
| 119 | call := &mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls.[i] |
| 120 | if call.Methodscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Method. == "Do" { |
| 121 | req := call.Argumentsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Arguments.[0].(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) |
| 122 | if predicate(req) { |
| 123 | found = true |
| 124 | |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if !found { |
| 131 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#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 assertNoMessagesAPICallscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/assertNoMessagesAPICall().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, mockClient *mocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/.MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/MockHTTPClient#) { |
| 138 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 139 | |
| 140 | for i := range mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls. { |
| 141 | call := &mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls.[i] |
| 142 | if call.Methodscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Method. == "Do" { |
| 143 | req := call.Argumentsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Arguments.[0].(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) |
| 144 | if req.URLscip-go gomod github.com/golang/go/src . `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src . `net/url`/URL#Path. == "/api/v1/messages" { |
| 145 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#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 setupRegisterThenMessagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/setupRegisterThenMessage().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, mockClient *mocksscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/.MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks`/MockHTTPClient#, msgResp *httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#) { |
| 158 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 159 | |
| 160 | // register (limits fetch) - body without max_* fields -> fallback to defaults |
| 161 | mockClient.Onscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#On().("Do", mockscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/.AnythingOfTypescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/AnythingOfType().("*http.Request")). |
| 162 | Returnscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Return().(createMockResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/zulip_test`/createMockResponse().(httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusOKscip-go gomod github.com/golang/go/src . `net/http`/StatusOK., `{"result": "success"}`), nil). |
| 163 | Oncescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Once().() |
| 164 | |
| 165 | // actual messages send |
| 166 | mockClient.Onscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#On().("Do", mockscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/.AnythingOfTypescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/AnythingOfType().("*http.Request")). |
| 167 | Returnscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Return().(msgResp, nil). |
| 168 | Oncescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Once().() |
| 169 | } |