Debian Codegraph @

testing/integration/matrix/send_test.go

raw symbols SCIP
1 package matrix_testscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/
2
3 import (
4 "contextscip-go gomod github.com/golang/go/src . context/"
5 "net/urlscip-go gomod github.com/golang/go/src . `net/url`/"
6 "testingscip-go gomod github.com/golang/go/src . testing/"
7
8 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/"
9
10 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrixscip-go gomod github.com/nicholas-fedor/shoutrrr . `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 TestServiceSendWithCancelledContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/TestServiceSendWithCancelledContext().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
18 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
19
20 // Create a service with dummy URL (client won't be initialized)
21 service, _ := createTestServicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/createTestService().(t, "matrix://[email protected]")
22
23 // Create a canceled context
24 ctx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithCancelscip-go gomod github.com/golang/go/src . context/WithCancel().(contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . 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.SendWithContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Service#SendWithContext().(ctx, "Test message", nil)
30
31 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Errorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Error().(t, err, "Expected error when sending with uninitialized client")
32 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.ErrorIsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/ErrorIs().(t, err, matrixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/.ErrClientNotInitializedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/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 TestServiceSendToMultipleRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/TestServiceSendToMultipleRooms().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
40 t.Parallelscip-go gomod github.com/golang/go/src . testing/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.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
82 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
83
84 parsedURL, err := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().(tt.serviceURL)
85 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err, "URL should be parseable for %s", tt.name)
86
87 // Set rooms directly on config
88 config := &matrixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#{
89 Roomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#Rooms.: tt.rooms,
90 }
91 err = config.SetURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#SetURL().(parsedURL)
92 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err, "Config should be set for %s", tt.name)
93 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, tt.expectedRooms, config.Roomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/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
104 func TestServiceSendWithSpecialCharactersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/TestServiceSendWithSpecialCharacters().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
105 t.Parallelscip-go gomod github.com/golang/go/src . testing/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.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
176 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
177
178 // Build URL with title if needed
179 serviceURL := "matrix://[email protected]"
180 if tt.title != "" {
181 serviceURL += "?title=" + urlscip-go gomod github.com/golang/go/src . `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src . `net/url`/QueryEscape().(tt.title)
182 }
183
184 service, _ := createTestServicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/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.Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Service#Send().(tt.message, nil)
190
191 // With dummy URL, client is not initialized so we expect error
192 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Errorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Error().(t, err, "Expected error with uninitialized client for %s", tt.name)
193 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.ErrorIsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/ErrorIs().(t, err, matrixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/.ErrClientNotInitializedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/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 TestServiceSendWithTitlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/TestServiceSendWithTitle().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
202 t.Parallelscip-go gomod github.com/golang/go/src . testing/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.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
232 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
233
234 parsedURL, err := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().(tt.serviceURL)
235 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err, "URL should be parseable for %s", tt.name)
236
237 config := &matrixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#{}
238 err = config.SetURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#SetURL().(parsedURL)
239 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err, "Config should be set for %s", tt.name)
240 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, tt.wantTitle, config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#Title.,
241 "Title mismatch for %s", tt.name)
242 })
243 }
244 }
245
246 // TestServiceSendWithoutRooms tests behavior when no rooms are configured.
247 func TestServiceSendWithoutRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/TestServiceSendWithoutRooms().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
248 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
249
250 // Create service without room configuration
251 service, _ := createTestServicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/matrix_test`/createTestService().(t, "matrix://[email protected]")
252
253 // Verify no rooms are configured
254 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Emptyscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Empty().(t, service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Service#Config..Roomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/Config#Rooms., "Rooms should be empty when not configured")
255 }