| 1 | package mqtt_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/eclipse/paho.golang/paho" |
| 9 | "github.com/stretchr/testify/mock" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/services/push/mqtt" |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 14 | ) |
| 15 | |
| 16 | // MockConnectionManager is a testify mock that implements the ConnectionManager interface. |
| 17 | type MockConnectionManager struct { |
| 18 | mock.Mock |
| 19 | } |
| 20 | |
| 21 | // mockLogger is a simple logger implementation for testing. |
| 22 | type mockLogger struct{} |
| 23 | |
| 24 | // publishMethodName is the constant for the Publish method to avoid string duplication. |
| 25 | const publishMethodName = "Publish" |
| 26 | |
| 27 | func (m *mockLogger) Print(_ ...any) {} |
| 28 | func (m *mockLogger) Printf(_ string, _ ...any) {} |
| 29 | func (m *mockLogger) Println(_ ...any) {} |
| 30 | |
| 31 | // AwaitConnection mocks waiting for the MQTT connection to be established. |
| 32 | func (m *MockConnectionManager) AwaitConnection(ctx context.Context) error { |
| 33 | args := m.Called(ctx) |
| 34 | |
| 35 | return args.Error(0) |
| 36 | } |
| 37 | |
| 38 | // Disconnect mocks gracefully closing the MQTT connection. |
| 39 | func (m *MockConnectionManager) Disconnect(ctx context.Context) error { |
| 40 | args := m.Called(ctx) |
| 41 | |
| 42 | return args.Error(0) |
| 43 | } |
| 44 | |
| 45 | // Publish mocks sending an MQTT message to the broker. |
| 46 | func (m *MockConnectionManager) Publish( |
| 47 | ctx context.Context, |
| 48 | publish *paho.Publish, |
| 49 | ) (*paho.PublishResponse, error) { |
| 50 | args := m.Called(ctx, publish) |
| 51 | |
| 52 | if args.Get(0) == nil { |
| 53 | return nil, args.Error(1) |
| 54 | } |
| 55 | |
| 56 | return args.Get(0).(*paho.PublishResponse), args.Error(1) |
| 57 | } |
| 58 | |
| 59 | // assertPublishCalled asserts that Publish was called with the expected topic. |
| 60 | func assertPublishCalled(t *testing.T, mockManager *MockConnectionManager, expectedTopic string) { |
| 61 | t.Helper() |
| 62 | |
| 63 | found := false |
| 64 | |
| 65 | for i := range mockManager.Calls { |
| 66 | call := &mockManager.Calls[i] |
| 67 | if call.Method == publishMethodName { |
| 68 | publish := call.Arguments[1].(*paho.Publish) |
| 69 | if publish.Topic == expectedTopic { |
| 70 | found = true |
| 71 | |
| 72 | break |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if !found { |
| 78 | t.Errorf("Expected Publish call with topic %q, but no matching call found", expectedTopic) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // assertPublishPayload asserts that Publish was called with the expected payload. |
| 83 | func assertPublishPayload( |
| 84 | t *testing.T, |
| 85 | mockManager *MockConnectionManager, |
| 86 | expectedPayload string, |
| 87 | ) { |
| 88 | t.Helper() |
| 89 | |
| 90 | found := false |
| 91 | |
| 92 | for i := range mockManager.Calls { |
| 93 | call := &mockManager.Calls[i] |
| 94 | if call.Method == publishMethodName { |
| 95 | publish := call.Arguments[1].(*paho.Publish) |
| 96 | if string(publish.Payload) == expectedPayload { |
| 97 | found = true |
| 98 | |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if !found { |
| 105 | t.Errorf( |
| 106 | "Expected Publish call with payload %q, but no matching call found", |
| 107 | expectedPayload, |
| 108 | ) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // assertPublishQoS asserts that Publish was called with the expected QoS level. |
| 113 | func assertPublishQoS(t *testing.T, mockManager *MockConnectionManager, expectedQoS byte) { |
| 114 | t.Helper() |
| 115 | |
| 116 | found := false |
| 117 | |
| 118 | for i := range mockManager.Calls { |
| 119 | call := &mockManager.Calls[i] |
| 120 | if call.Method == publishMethodName { |
| 121 | publish := call.Arguments[1].(*paho.Publish) |
| 122 | if publish.QoS == expectedQoS { |
| 123 | found = true |
| 124 | |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if !found { |
| 131 | t.Errorf("Expected Publish call with QoS %d, but no matching call found", expectedQoS) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // assertPublishRetained asserts that Publish was called with the expected retained flag. |
| 136 | func assertPublishRetained( |
| 137 | t *testing.T, |
| 138 | mockManager *MockConnectionManager, |
| 139 | expectedRetained bool, |
| 140 | ) { |
| 141 | t.Helper() |
| 142 | |
| 143 | found := false |
| 144 | |
| 145 | for i := range mockManager.Calls { |
| 146 | call := &mockManager.Calls[i] |
| 147 | if call.Method == publishMethodName { |
| 148 | publish := call.Arguments[1].(*paho.Publish) |
| 149 | if publish.Retain == expectedRetained { |
| 150 | found = true |
| 151 | |
| 152 | break |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if !found { |
| 158 | t.Errorf( |
| 159 | "Expected Publish call with Retain=%v, but no matching call found", |
| 160 | expectedRetained, |
| 161 | ) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // createMockPublishResponse creates a mock publish response with the given reason code. |
| 166 | func createMockPublishResponse(reasonCode byte) *paho.PublishResponse { |
| 167 | return &paho.PublishResponse{ |
| 168 | ReasonCode: reasonCode, |
| 169 | Properties: &paho.PublishResponseProperties{}, |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // createMockPublishResponseWithReason creates a mock publish response with reason code and string. |
| 174 | func createMockPublishResponseWithReason( |
| 175 | reasonCode byte, |
| 176 | reasonString string, |
| 177 | ) *paho.PublishResponse { |
| 178 | return &paho.PublishResponse{ |
| 179 | ReasonCode: reasonCode, |
| 180 | Properties: &paho.PublishResponseProperties{ |
| 181 | ReasonString: reasonString, |
| 182 | }, |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // createTestParams creates test parameters with the given key-value pairs. |
| 187 | func createTestParams(pairs ...string) *types.Params { |
| 188 | params := make(types.Params) |
| 189 | |
| 190 | for i := 0; i < len(pairs); i += 2 { |
| 191 | if i+1 < len(pairs) { |
| 192 | params[pairs[i]] = pairs[i+1] |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return ¶ms |
| 197 | } |
| 198 | |
| 199 | // createTestService creates an MQTT service instance configured for testing. |
| 200 | // If a mockConnectionManager is provided, it will be injected into the service |
| 201 | // to bypass the normal connection initialization. |
| 202 | func createTestService( |
| 203 | t *testing.T, |
| 204 | mqttURL string, |
| 205 | mockConnectionManager ...mqtt.ConnectionManager, |
| 206 | ) *mqtt.Service { |
| 207 | t.Helper() |
| 208 | |
| 209 | service := &mqtt.Service{} |
| 210 | |
| 211 | parsedURL, err := url.Parse(mqttURL) |
| 212 | require.NoError(t, err) |
| 213 | |
| 214 | err = service.Initialize(parsedURL, &mockLogger{}) |
| 215 | require.NoError(t, err) |
| 216 | |
| 217 | // Inject mock connection manager if provided |
| 218 | if len(mockConnectionManager) > 0 && mockConnectionManager[0] != nil { |
| 219 | service.SetConnectionManager(mockConnectionManager[0]) |
| 220 | } |
| 221 | |
| 222 | return service |
| 223 | } |
| 224 | |
| 225 | // getPublishCall retrieves the publish struct from the first Publish call. |
| 226 | // Returns nil if no Publish call was made. |
| 227 | func getPublishCall(mockManager *MockConnectionManager) *paho.Publish { |
| 228 | for i := range mockManager.Calls { |
| 229 | call := &mockManager.Calls[i] |
| 230 | if call.Method == publishMethodName { |
| 231 | if publish, ok := call.Arguments[1].(*paho.Publish); ok { |
| 232 | return publish |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return nil |
| 238 | } |