Debian Codegraph @

testing/integration/matrix/send_test.go

raw
1 package matrix_test
2
3 import (
4 "context"
5 "net/url"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9
10 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix"
11 )
12
13 // TestServiceSendWithCancelledContext tests that the service handles context
14 // cancellation properly when attempting to send messages.
15 // Since this is an integration test without external calls, we verify the
16 // service properly detects and reports the uninitialized client state.
17 func TestServiceSendWithCancelledContext(t *testing.T) {
18 t.Parallel()
19
20 // Create a service with dummy URL (client won't be initialized)
21 service, _ := createTestService(t, "matrix://[email protected]")
22
23 // Create a canceled context
24 ctx, cancel := context.WithCancel(context.Background())
25 cancel() // Cancel immediately
26
27 // Attempting to send with an uninitialized client should return
28 // ErrClientNotInitialized, regardless of context state
29 err := service.SendWithContext(ctx, "Test message", nil)
30
31 require.Error(t, err, "Expected error when sending with uninitialized client")
32 require.ErrorIs(t, err, matrix.ErrClientNotInitialized,
33 "Expected ErrClientNotInitialized, got %v", err)
34 }
35
36 // TestServiceSendToMultipleRooms tests that sending to multiple rooms
37 // is properly configured and the service handles room lists correctly.
38 // Rooms are set directly on the config since URL params only support one room.
39 func TestServiceSendToMultipleRooms(t *testing.T) {
40 t.Parallel()
41
42 tests := []struct {
43 name string
44 serviceURL string
45 rooms []string
46 expectedRooms []string
47 }{
48 {
49 name: "single room set directly",
50 serviceURL: "matrix://user:[email protected]",
51 rooms: []string{"#general:example.com"},
52 expectedRooms: []string{"#general:example.com"},
53 },
54 {
55 name: "multiple rooms set directly",
56 serviceURL: "matrix://user:[email protected]",
57 rooms: []string{"#general:example.com", "#alerts:example.com"},
58 expectedRooms: []string{"#general:example.com", "#alerts:example.com"},
59 },
60 {
61 name: "three rooms set directly",
62 serviceURL: "matrix://user:[email protected]",
63 rooms: []string{"#room1:example.com", "#room2:example.com", "#room3:example.com"},
64 expectedRooms: []string{"#room1:example.com", "#room2:example.com", "#room3:example.com"},
65 },
66 {
67 name: "rooms with room IDs (not aliases)",
68 serviceURL: "matrix://user:[email protected]",
69 rooms: []string{"!room1id:example.com"},
70 expectedRooms: []string{"!room1id:example.com"},
71 },
72 {
73 name: "mixed room aliases and IDs",
74 serviceURL: "matrix://user:[email protected]",
75 rooms: []string{"#general:example.com", "!alertid:example.com"},
76 expectedRooms: []string{"#general:example.com", "!alertid:example.com"},
77 },
78 }
79
80 for _, tt := range tests {
81 t.Run(tt.name, func(t *testing.T) {
82 t.Parallel()
83
84 parsedURL, err := url.Parse(tt.serviceURL)
85 require.NoError(t, err, "URL should be parseable for %s", tt.name)
86
87 // Set rooms directly on config
88 config := &matrix.Config{
89 Rooms: tt.rooms,
90 }
91 err = config.SetURL(parsedURL)
92 require.NoError(t, err, "Config should be set for %s", tt.name)
93 require.Equal(t, tt.expectedRooms, config.Rooms,
94 "Rooms mismatch for %s", tt.name)
95 })
96 }
97 }
98
99 // TestServiceSendWithSpecialCharacters tests message content with
100 // HTML and Markdown special characters that might need escaping
101 // or special handling in Matrix messages.
102 //
103 //nolint:gosmopolitan // Intentional string literal containing rune in Han script
104 func TestServiceSendWithSpecialCharacters(t *testing.T) {
105 t.Parallel()
106
107 tests := []struct {
108 name string
109 message string
110 title string
111 }{
112 {
113 name: "message with HTML tags",
114 message: "<p>This is a <strong>bold</strong> message</p>",
115 title: "",
116 },
117 {
118 name: "message with Markdown bold",
119 message: "This is **bold** and *italic* text",
120 title: "",
121 },
122 {
123 name: "message with special chars ampersand",
124 message: "Test & more <> characters",
125 title: "",
126 },
127 {
128 name: "message with newlines",
129 message: "Line 1\nLine 2\nLine 3",
130 title: "",
131 },
132 {
133 name: "message with tabs",
134 message: "Col1\tCol2\tCol3",
135 title: "",
136 },
137 {
138 name: "message with title and special chars",
139 message: "Alert: <error> critical issue",
140 title: "Warning",
141 },
142 {
143 name: "message with backticks",
144 message: "Use `code` for inline code",
145 title: "",
146 },
147 {
148 name: "message with Unicode characters",
149 message: "Hello 世界 🌍 αβγδ",
150 title: "",
151 },
152 {
153 name: "message with JSON-like content",
154 message: `{"key": "value", "number": 123}`,
155 title: "",
156 },
157 {
158 name: "message with URL",
159 message: "Check https://example.com/path?param=value for more info",
160 title: "",
161 },
162 {
163 name: "empty message",
164 message: "",
165 title: "",
166 },
167 {
168 name: "message with only special characters",
169 message: "!@#$%^&*()_+-=[]{}|;':\",./<>?",
170 title: "",
171 },
172 }
173
174 for _, tt := range tests {
175 t.Run(tt.name, func(t *testing.T) {
176 t.Parallel()
177
178 // Build URL with title if needed
179 serviceURL := "matrix://[email protected]"
180 if tt.title != "" {
181 serviceURL += "?title=" + url.QueryEscape(tt.title)
182 }
183
184 service, _ := createTestService(t, serviceURL)
185
186 // The service should accept any message content
187 // The actual sending would fail with uninitialized client,
188 // but we're testing that the message is accepted
189 err := service.Send(tt.message, nil)
190
191 // With dummy URL, client is not initialized so we expect error
192 require.Error(t, err, "Expected error with uninitialized client for %s", tt.name)
193 require.ErrorIs(t, err, matrix.ErrClientNotInitialized,
194 "Expected ErrClientNotInitialized for %s", tt.name)
195 })
196 }
197 }
198
199 // TestServiceSendWithTitle tests that the title is properly prepended
200 // to the message when sending notifications.
201 func TestServiceSendWithTitle(t *testing.T) {
202 t.Parallel()
203
204 tests := []struct {
205 name string
206 serviceURL string
207 message string
208 wantTitle string
209 }{
210 {
211 name: "title set via URL",
212 serviceURL: "matrix://user:[email protected]?title=Alert",
213 message: "This is the message",
214 wantTitle: "Alert",
215 },
216 {
217 name: "title with spaces",
218 serviceURL: "matrix://user:[email protected]?title=Critical%20Alert",
219 message: "Critical issue detected",
220 wantTitle: "Critical Alert",
221 },
222 {
223 name: "no title",
224 serviceURL: "matrix://user:[email protected]",
225 message: "Plain message",
226 wantTitle: "",
227 },
228 }
229
230 for _, tt := range tests {
231 t.Run(tt.name, func(t *testing.T) {
232 t.Parallel()
233
234 parsedURL, err := url.Parse(tt.serviceURL)
235 require.NoError(t, err, "URL should be parseable for %s", tt.name)
236
237 config := &matrix.Config{}
238 err = config.SetURL(parsedURL)
239 require.NoError(t, err, "Config should be set for %s", tt.name)
240 require.Equal(t, tt.wantTitle, config.Title,
241 "Title mismatch for %s", tt.name)
242 })
243 }
244 }
245
246 // TestServiceSendWithoutRooms tests behavior when no rooms are configured.
247 func TestServiceSendWithoutRooms(t *testing.T) {
248 t.Parallel()
249
250 // Create service without room configuration
251 service, _ := createTestService(t, "matrix://[email protected]")
252
253 // Verify no rooms are configured
254 require.Empty(t, service.Config.Rooms, "Rooms should be empty when not configured")
255 }