| 1 | package discord_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | "testing/synctest" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/stretchr/testify/mock" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 | ) |
| 14 | |
| 15 | func TestSendEmbedWithAuthor(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | synctest.Test(t, func(t *testing.T) { |
| 18 | mockClient := &MockHTTPClient{} |
| 19 | service := createTestService( |
| 20 | t, |
| 21 | "discord://test-token@test-webhook", |
| 22 | mockClient, |
| 23 | ) |
| 24 | |
| 25 | mockClient.On("Do", mock.Anything). |
| 26 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 27 | Once() |
| 28 | |
| 29 | items := []types.MessageItem{ |
| 30 | { |
| 31 | Text: "Test embed with author", |
| 32 | Fields: []types.Field{ |
| 33 | {Key: "embed_author_name", Value: "Test Author"}, |
| 34 | {Key: "embed_author_url", Value: "https://example.com/author"}, |
| 35 | {Key: "embed_author_icon_url", Value: "https://example.com/icon.png"}, |
| 36 | }, |
| 37 | }, |
| 38 | } |
| 39 | |
| 40 | err := service.SendItems(items, nil) |
| 41 | |
| 42 | require.NoError(t, err) |
| 43 | assertRequestContains( |
| 44 | t, |
| 45 | mockClient, |
| 46 | `"author":{"name":"Test Author","url":"https://example.com/author","icon_url":"https://example.com/icon.png"}`, |
| 47 | ) |
| 48 | |
| 49 | mockClient.AssertExpectations(t) |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | func TestSendEmbedWithImage(t *testing.T) { |
| 54 | t.Parallel() |
| 55 | synctest.Test(t, func(t *testing.T) { |
| 56 | mockClient := &MockHTTPClient{} |
| 57 | service := createTestService( |
| 58 | t, |
| 59 | "discord://test-token@test-webhook", |
| 60 | mockClient, |
| 61 | ) |
| 62 | |
| 63 | mockClient.On("Do", mock.Anything). |
| 64 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 65 | Once() |
| 66 | |
| 67 | items := []types.MessageItem{ |
| 68 | { |
| 69 | Text: "Test embed with image", |
| 70 | Fields: []types.Field{ |
| 71 | {Key: "embed_image_url", Value: "https://example.com/image.png"}, |
| 72 | }, |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | err := service.SendItems(items, nil) |
| 77 | |
| 78 | require.NoError(t, err) |
| 79 | assertRequestContains( |
| 80 | t, |
| 81 | mockClient, |
| 82 | `"image":{"url":"https://example.com/image.png"}`, |
| 83 | ) |
| 84 | |
| 85 | mockClient.AssertExpectations(t) |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | func TestSendEmbedWithThumbnail(t *testing.T) { |
| 90 | t.Parallel() |
| 91 | synctest.Test(t, func(t *testing.T) { |
| 92 | mockClient := &MockHTTPClient{} |
| 93 | service := createTestService( |
| 94 | t, |
| 95 | "discord://test-token@test-webhook", |
| 96 | mockClient, |
| 97 | ) |
| 98 | |
| 99 | mockClient.On("Do", mock.Anything). |
| 100 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 101 | Once() |
| 102 | |
| 103 | items := []types.MessageItem{ |
| 104 | { |
| 105 | Text: "Test embed with thumbnail", |
| 106 | Fields: []types.Field{ |
| 107 | {Key: "embed_thumbnail_url", Value: "https://example.com/thumb.png"}, |
| 108 | }, |
| 109 | }, |
| 110 | } |
| 111 | |
| 112 | err := service.SendItems(items, nil) |
| 113 | |
| 114 | require.NoError(t, err) |
| 115 | assertRequestContains( |
| 116 | t, |
| 117 | mockClient, |
| 118 | `"thumbnail":{"url":"https://example.com/thumb.png"}`, |
| 119 | ) |
| 120 | |
| 121 | mockClient.AssertExpectations(t) |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | func TestSendEmbedWithFields(t *testing.T) { |
| 126 | t.Parallel() |
| 127 | synctest.Test(t, func(t *testing.T) { |
| 128 | mockClient := &MockHTTPClient{} |
| 129 | service := createTestService( |
| 130 | t, |
| 131 | "discord://test-token@test-webhook", |
| 132 | mockClient, |
| 133 | ) |
| 134 | |
| 135 | mockClient.On("Do", mock.Anything). |
| 136 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 137 | Once() |
| 138 | |
| 139 | items := []types.MessageItem{ |
| 140 | { |
| 141 | Text: "Test embed with fields", |
| 142 | Fields: []types.Field{ |
| 143 | {Key: "Status", Value: "Active"}, |
| 144 | {Key: "Priority", Value: "High"}, |
| 145 | {Key: "Assignee", Value: "John Doe"}, |
| 146 | }, |
| 147 | Level: types.Info, |
| 148 | }, |
| 149 | } |
| 150 | |
| 151 | err := service.SendItems(items, nil) |
| 152 | |
| 153 | require.NoError(t, err) |
| 154 | assertRequestContains( |
| 155 | t, |
| 156 | mockClient, |
| 157 | `"fields":[{"name":"Status","value":"Active"},{"name":"Priority","value":"High"},{"name":"Assignee","value":"John Doe"}]`, |
| 158 | ) |
| 159 | |
| 160 | mockClient.AssertExpectations(t) |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | func TestSendEmbedWithTimestamp(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | synctest.Test(t, func(t *testing.T) { |
| 167 | mockClient := &MockHTTPClient{} |
| 168 | service := createTestService( |
| 169 | t, |
| 170 | "discord://test-token@test-webhook", |
| 171 | mockClient, |
| 172 | ) |
| 173 | |
| 174 | mockClient.On("Do", mock.Anything). |
| 175 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 176 | Once() |
| 177 | |
| 178 | testTime := time.Date(2023, 12, 25, 12, 0, 0, 0, time.UTC) |
| 179 | items := []types.MessageItem{ |
| 180 | { |
| 181 | Text: "Test embed with timestamp", |
| 182 | Timestamp: testTime, |
| 183 | }, |
| 184 | } |
| 185 | |
| 186 | err := service.SendItems(items, nil) |
| 187 | |
| 188 | require.NoError(t, err) |
| 189 | assertRequestContains(t, mockClient, `"timestamp":"2023-12-25T12:00:00Z"`) |
| 190 | |
| 191 | mockClient.AssertExpectations(t) |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | func TestSendEmbedWithColors(t *testing.T) { |
| 196 | t.Parallel() |
| 197 | synctest.Test(t, func(t *testing.T) { |
| 198 | mockClient := &MockHTTPClient{} |
| 199 | service := createTestService( |
| 200 | t, |
| 201 | "discord://test-token@test-webhook", |
| 202 | mockClient, |
| 203 | ) |
| 204 | |
| 205 | tests := []struct { |
| 206 | name string |
| 207 | level types.MessageLevel |
| 208 | }{ |
| 209 | {"info level", types.Info}, |
| 210 | {"warning level", types.Warning}, |
| 211 | {"error level", types.Error}, |
| 212 | } |
| 213 | |
| 214 | for _, tt := range tests { |
| 215 | mockClient.On("Do", mock.Anything). |
| 216 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 217 | Once() |
| 218 | |
| 219 | items := []types.MessageItem{ |
| 220 | {Text: "Test embed with color", Level: tt.level}, |
| 221 | } |
| 222 | |
| 223 | err := service.SendItems(items, nil) |
| 224 | |
| 225 | require.NoError(t, err) |
| 226 | // Color handling depends on configuration |
| 227 | } |
| 228 | |
| 229 | mockClient.AssertExpectations(t) |
| 230 | }) |
| 231 | } |
| 232 | |
| 233 | func TestSendMultipleEmbeds(t *testing.T) { |
| 234 | t.Parallel() |
| 235 | synctest.Test(t, func(t *testing.T) { |
| 236 | mockClient := &MockHTTPClient{} |
| 237 | service := createTestService( |
| 238 | t, |
| 239 | "discord://test-token@test-webhook", |
| 240 | mockClient, |
| 241 | ) |
| 242 | |
| 243 | mockClient.On("Do", mock.Anything). |
| 244 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 245 | Once() |
| 246 | |
| 247 | items := []types.MessageItem{ |
| 248 | {Text: "First embed"}, |
| 249 | {Text: "Second embed"}, |
| 250 | {Text: "Third embed"}, |
| 251 | } |
| 252 | |
| 253 | err := service.SendItems(items, nil) |
| 254 | |
| 255 | require.NoError(t, err) |
| 256 | // Verify multiple embeds are created |
| 257 | assertRequestContains(t, mockClient, `"description":"First embed"`) |
| 258 | assertRequestContains(t, mockClient, `"description":"Second embed"`) |
| 259 | assertRequestContains(t, mockClient, `"description":"Third embed"`) |
| 260 | |
| 261 | mockClient.AssertExpectations(t) |
| 262 | }) |
| 263 | } |