Debian Codegraph @

testing/integration/matrix/service_test.go

raw
1 package matrix_test
2
3 import (
4 "net/url"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix"
10 )
11
12 func TestServiceInitializeWithValidURL(t *testing.T) {
13 t.Parallel()
14
15 tests := []struct {
16 name string
17 serviceURL string
18 }{
19 {
20 name: "dummy URL for testing",
21 serviceURL: "matrix://[email protected]",
22 },
23 {
24 name: "dummy URL with password",
25 serviceURL: "matrix://dummy:[email protected]",
26 },
27 {
28 name: "dummy URL with custom title",
29 serviceURL: "matrix://[email protected]?title=Notification",
30 },
31 }
32
33 for _, tt := range tests {
34 t.Run(tt.name, func(t *testing.T) {
35 t.Parallel()
36
37 // Use createTestService which handles URL parsing internally
38 service, err := createTestService(t, tt.serviceURL)
39 require.NoError(t, err, "Expected no error for %s", tt.name)
40 require.NotNil(t, service, "Service should be initialized for %s", tt.name)
41 require.NotNil(t, service.Config, "Config should be initialized for %s", tt.name)
42 })
43 }
44 }
45
46 func TestServiceInitializeWithInvalidURL(t *testing.T) {
47 t.Parallel()
48
49 tests := []struct {
50 name string
51 serviceURL string
52 wantErr error
53 }{
54 {
55 name: "missing host",
56 serviceURL: "matrix://user:token@",
57 wantErr: matrix.ErrMissingHost,
58 },
59 {
60 name: "missing credentials",
61 serviceURL: "matrix://matrix.example.com",
62 wantErr: matrix.ErrMissingCredentials,
63 },
64 }
65
66 for _, tt := range tests {
67 t.Run(tt.name, func(t *testing.T) {
68 t.Parallel()
69
70 service := &matrix.Service{}
71
72 parsedURL, err := url.Parse(tt.serviceURL)
73 require.NoError(t, err, "URL should be parseable for %s", tt.name)
74
75 err = service.Initialize(parsedURL, &testLogger{})
76
77 require.Error(t, err, "Expected error for %s", tt.name)
78
79 if tt.wantErr != nil {
80 require.ErrorIs(t, err, tt.wantErr,
81 "Expected error %v, got %v", tt.wantErr, err)
82 }
83 })
84 }
85 }
86
87 func TestServiceGetID(t *testing.T) {
88 t.Parallel()
89
90 service, _ := createTestService(t, "matrix://[email protected]")
91
92 id := service.GetID()
93
94 require.Equal(t, "matrix", id, "Expected service ID to be 'matrix'")
95 }
96
97 func TestServiceSendWithDummyClient(t *testing.T) {
98 t.Parallel()
99
100 // When using the dummy URL, the client is not initialized
101 // and Send should return ErrClientNotInitialized
102 service, _ := createTestService(t, "matrix://[email protected]")
103
104 err := service.Send("Test message", nil)
105
106 require.Error(t, err, "Expected error when sending with uninitialized client")
107 require.ErrorIs(t, err, matrix.ErrClientNotInitialized,
108 "Expected ErrClientNotInitialized, got %v", err)
109 }
110
111 func TestServiceSendWithNilParams(t *testing.T) {
112 t.Parallel()
113
114 // Using dummy URL - client won't be initialized
115 service, _ := createTestService(t, "matrix://[email protected]")
116
117 // This will fail because the client is nil, but we can verify the behavior
118 err := service.Send("Test message", nil)
119
120 require.Error(t, err)
121 require.ErrorIs(t, err, matrix.ErrClientNotInitialized)
122 }
123
124 func TestServiceSendWithEmptyMessage(t *testing.T) {
125 t.Parallel()
126
127 service, _ := createTestService(t, "matrix://[email protected]")
128
129 err := service.Send("", nil)
130
131 // Even empty messages will fail with client not initialized
132 require.Error(t, err)
133 }
134
135 func TestServiceSendWithValidConfig(t *testing.T) {
136 t.Parallel()
137
138 // This test verifies that the service can be created with valid config
139 // and the config values are correctly set
140 service, err := createTestService(t, "matrix://dummy:[email protected]")
141 require.NoError(t, err, "Expected no error for dummy URL")
142 require.NotNil(t, service, "Service should not be nil")
143
144 // Verify the config is set correctly
145 require.Equal(t, "dummy.com", service.Config.Host)
146 require.Equal(t, "token", service.Config.Password)
147 }