| 1 |
package discord_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"testing" |
| 6 |
"testing/synctest" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/mock" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSendMessageToThread(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
synctest.Test(t, func(t *testing.T) { |
| 17 |
mockClient := &MockHTTPClient{} |
| 18 |
mockClient.On("Do", mock.Anything). |
| 19 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 20 |
Once() |
| 21 |
|
| 22 |
// Create service with thread ID in URL |
| 23 |
service := createTestService( |
| 24 |
t, |
| 25 |
"discord://test-token@test-webhook?thread_id=123456789", |
| 26 |
mockClient, |
| 27 |
) |
| 28 |
|
| 29 |
err := service.Send("Message in thread", nil) |
| 30 |
|
| 31 |
require.NoError(t, err) |
| 32 |
assertRequestMade( |
| 33 |
t, |
| 34 |
mockClient, |
| 35 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=123456789", |
| 36 |
) |
| 37 |
|
| 38 |
mockClient.AssertExpectations(t) |
| 39 |
}) |
| 40 |
} |
| 41 |
|
| 42 |
func TestSendItemsToThread(t *testing.T) { |
| 43 |
t.Parallel() |
| 44 |
synctest.Test(t, func(t *testing.T) { |
| 45 |
mockClient := &MockHTTPClient{} |
| 46 |
mockClient.On("Do", mock.Anything). |
| 47 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 48 |
Once() |
| 49 |
|
| 50 |
// Create service with thread ID in URL |
| 51 |
service := createTestService( |
| 52 |
t, |
| 53 |
"discord://test-token@test-webhook?thread_id=987654321", |
| 54 |
mockClient, |
| 55 |
) |
| 56 |
|
| 57 |
items := []types.MessageItem{ |
| 58 |
{Text: "Thread message with embed"}, |
| 59 |
} |
| 60 |
|
| 61 |
err := service.SendItems(items, nil) |
| 62 |
|
| 63 |
require.NoError(t, err) |
| 64 |
assertRequestMade( |
| 65 |
t, |
| 66 |
mockClient, |
| 67 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=987654321", |
| 68 |
) |
| 69 |
|
| 70 |
mockClient.AssertExpectations(t) |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
func TestSendMessageToThreadWithParams(t *testing.T) { |
| 75 |
t.Parallel() |
| 76 |
synctest.Test(t, func(t *testing.T) { |
| 77 |
mockClient := &MockHTTPClient{} |
| 78 |
mockClient.On("Do", mock.Anything). |
| 79 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 80 |
Once() |
| 81 |
|
| 82 |
// Create service with thread ID in URL |
| 83 |
service := createTestService( |
| 84 |
t, |
| 85 |
"discord://test-token@test-webhook?thread_id=111222333", |
| 86 |
mockClient, |
| 87 |
) |
| 88 |
|
| 89 |
params := createTestParams("thread_id", "444555666") // Override thread_id via params |
| 90 |
|
| 91 |
err := service.Send("Message with param thread override", params) |
| 92 |
|
| 93 |
require.NoError(t, err) |
| 94 |
assertRequestMade( |
| 95 |
t, |
| 96 |
mockClient, |
| 97 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=444555666", |
| 98 |
) |
| 99 |
|
| 100 |
mockClient.AssertExpectations(t) |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func TestSendFileToThread(t *testing.T) { |
| 105 |
t.Parallel() |
| 106 |
synctest.Test(t, func(t *testing.T) { |
| 107 |
mockClient := &MockHTTPClient{} |
| 108 |
mockClient.On("Do", mock.Anything). |
| 109 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 110 |
Once() |
| 111 |
|
| 112 |
// Create service with thread ID in URL |
| 113 |
service := createTestService( |
| 114 |
t, |
| 115 |
"discord://test-token@test-webhook?thread_id=thread123", |
| 116 |
mockClient, |
| 117 |
) |
| 118 |
|
| 119 |
items := []types.MessageItem{ |
| 120 |
createTestMessageItemWithFile( |
| 121 |
"File in thread", |
| 122 |
"thread-file.txt", |
| 123 |
[]byte("thread content"), |
| 124 |
), |
| 125 |
} |
| 126 |
|
| 127 |
err := service.SendItems(items, nil) |
| 128 |
|
| 129 |
require.NoError(t, err) |
| 130 |
assertRequestMade( |
| 131 |
t, |
| 132 |
mockClient, |
| 133 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=thread123", |
| 134 |
) |
| 135 |
|
| 136 |
mockClient.AssertExpectations(t) |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func TestThreadIDWithSpecialCharacters(t *testing.T) { |
| 141 |
t.Parallel() |
| 142 |
synctest.Test(t, func(t *testing.T) { |
| 143 |
mockClient := &MockHTTPClient{} |
| 144 |
mockClient.On("Do", mock.Anything). |
| 145 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 146 |
Once() |
| 147 |
|
| 148 |
// Test thread ID with various characters |
| 149 |
service := createTestService( |
| 150 |
t, |
| 151 |
"discord://test-token@test-webhook?thread_id=thread-123_456.789", |
| 152 |
mockClient, |
| 153 |
) |
| 154 |
|
| 155 |
err := service.Send("Message with special thread ID", nil) |
| 156 |
|
| 157 |
require.NoError(t, err) |
| 158 |
assertRequestMade( |
| 159 |
t, |
| 160 |
mockClient, |
| 161 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=thread-123_456.789", |
| 162 |
) |
| 163 |
|
| 164 |
mockClient.AssertExpectations(t) |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
func TestThreadIDValidation(t *testing.T) { |
| 169 |
t.Parallel() |
| 170 |
synctest.Test(t, func(t *testing.T) { |
| 171 |
mockClient := &MockHTTPClient{} |
| 172 |
// Test that invalid thread IDs are handled properly |
| 173 |
service := createTestService( |
| 174 |
t, |
| 175 |
"discord://test-token@test-webhook?thread_id=invalid-thread-id", |
| 176 |
mockClient, |
| 177 |
) |
| 178 |
|
| 179 |
mockClient.On("Do", mock.Anything). |
| 180 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 181 |
Once() |
| 182 |
|
| 183 |
err := service.Send("Message with invalid thread ID", nil) |
| 184 |
|
| 185 |
require.NoError(t, err) |
| 186 |
// Should still work as Discord accepts various thread ID formats |
| 187 |
|
| 188 |
mockClient.AssertExpectations(t) |
| 189 |
}) |
| 190 |
} |
| 191 |
|
| 192 |
func TestThreadMessageWithEmbed(t *testing.T) { |
| 193 |
t.Parallel() |
| 194 |
synctest.Test(t, func(t *testing.T) { |
| 195 |
mockClient := &MockHTTPClient{} |
| 196 |
mockClient.On("Do", mock.Anything). |
| 197 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 198 |
Once() |
| 199 |
|
| 200 |
service := createTestService( |
| 201 |
t, |
| 202 |
"discord://test-token@test-webhook?thread_id=embed-thread", |
| 203 |
mockClient, |
| 204 |
) |
| 205 |
|
| 206 |
items := []types.MessageItem{ |
| 207 |
{ |
| 208 |
Text: "Thread message with rich embed", |
| 209 |
Fields: []types.Field{ |
| 210 |
{Key: "Thread", Value: "Yes"}, |
| 211 |
{Key: "Type", Value: "Test"}, |
| 212 |
}, |
| 213 |
Level: types.Info, |
| 214 |
}, |
| 215 |
} |
| 216 |
|
| 217 |
err := service.SendItems(items, nil) |
| 218 |
|
| 219 |
require.NoError(t, err) |
| 220 |
assertRequestMade( |
| 221 |
t, |
| 222 |
mockClient, |
| 223 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=embed-thread", |
| 224 |
) |
| 225 |
assertRequestContains( |
| 226 |
t, |
| 227 |
mockClient, |
| 228 |
`"description":"Thread message with rich embed"`, |
| 229 |
) |
| 230 |
assertRequestContains( |
| 231 |
t, |
| 232 |
mockClient, |
| 233 |
`"name":"Thread","value":"Yes"`, |
| 234 |
) |
| 235 |
|
| 236 |
mockClient.AssertExpectations(t) |
| 237 |
}) |
| 238 |
} |
| 239 |
|
| 240 |
func TestThreadMessageWithMultipleFiles(t *testing.T) { |
| 241 |
t.Parallel() |
| 242 |
synctest.Test(t, func(t *testing.T) { |
| 243 |
mockClient := &MockHTTPClient{} |
| 244 |
mockClient.On("Do", mock.Anything). |
| 245 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 246 |
Once() |
| 247 |
|
| 248 |
service := createTestService( |
| 249 |
t, |
| 250 |
"discord://test-token@test-webhook?thread_id=file-thread", |
| 251 |
mockClient, |
| 252 |
) |
| 253 |
|
| 254 |
items := []types.MessageItem{ |
| 255 |
createTestMessageItemWithFile("First file in thread", "file1.txt", []byte("content 1")), |
| 256 |
createTestMessageItemWithFile( |
| 257 |
"Second file in thread", |
| 258 |
"file2.txt", |
| 259 |
[]byte("content 2"), |
| 260 |
), |
| 261 |
} |
| 262 |
|
| 263 |
err := service.SendItems(items, nil) |
| 264 |
|
| 265 |
require.NoError(t, err) |
| 266 |
assertRequestMade( |
| 267 |
t, |
| 268 |
mockClient, |
| 269 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=file-thread", |
| 270 |
) |
| 271 |
assertRequestContains(t, mockClient, `filename="file1.txt"`) |
| 272 |
assertRequestContains(t, mockClient, `filename="file2.txt"`) |
| 273 |
|
| 274 |
mockClient.AssertExpectations(t) |
| 275 |
}) |
| 276 |
} |
| 277 |
|
| 278 |
func TestThreadMessageWithUsernameAvatar(t *testing.T) { |
| 279 |
t.Parallel() |
| 280 |
synctest.Test(t, func(t *testing.T) { |
| 281 |
mockClient := &MockHTTPClient{} |
| 282 |
mockClient.On("Do", mock.Anything). |
| 283 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 284 |
Once() |
| 285 |
|
| 286 |
service := createTestService( |
| 287 |
t, |
| 288 |
"discord://test-token@test-webhook?thread_id=custom-thread", |
| 289 |
mockClient, |
| 290 |
) |
| 291 |
|
| 292 |
params := createTestParams( |
| 293 |
"username", |
| 294 |
"ThreadBot", |
| 295 |
"avatar", |
| 296 |
"https://example.com/thread-avatar.png", |
| 297 |
) |
| 298 |
|
| 299 |
err := service.Send("Thread message with custom appearance", params) |
| 300 |
|
| 301 |
require.NoError(t, err) |
| 302 |
assertRequestMade( |
| 303 |
t, |
| 304 |
mockClient, |
| 305 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=custom-thread", |
| 306 |
) |
| 307 |
assertRequestContains(t, mockClient, `"username":"ThreadBot"`) |
| 308 |
assertRequestContains( |
| 309 |
t, |
| 310 |
mockClient, |
| 311 |
`"avatar_url":"https://example.com/thread-avatar.png"`, |
| 312 |
) |
| 313 |
|
| 314 |
mockClient.AssertExpectations(t) |
| 315 |
}) |
| 316 |
} |