-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_handlers_test.go
349 lines (294 loc) · 6.64 KB
/
message_handlers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"context"
"fmt"
"log/slog"
"testing"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
var ctx context.Context
var evt *event.Event
var testSender = func(text string, _ id.RoomID) (*mautrix.RespSendEvent, error) {
return nil, nil
}
func init() {
slog.SetLogLoggerLevel(slog.LevelError)
ctx = context.Background()
evt = &event.Event{
RoomID: id.RoomID("test-room"),
Sender: id.UserID("test-sender"),
}
client, _ = mautrix.NewClient("http://localhost", "", "")
}
// TODO: test non-existent package
func TestSub(t *testing.T) {
if err := setupDB(ctx, ":memory:"); err != nil {
panic(err)
}
tt := []struct {
ap string
lv string
err bool
}{
{
ap: "foo",
lv: "1970-01-01",
err: false,
},
{
ap: "python312Packages.foo",
lv: "1980-01-01",
err: true,
},
}
var count int
var lv string
var hasErr bool
for _, v := range tt {
h = handlers{
logFetcher: func(string) (string, bool) {
return v.lv, v.err
},
sender: testSender,
}
if _, err := db.Exec("INSERT INTO packages(attr_path) VALUES (?)", v.ap); err != nil {
panic(err)
}
subscribe(v.ap)
if err := db.QueryRow(`
SELECT COUNT(*)
FROM subscriptions
WHERE roomid = ?
AND mxid = ?
AND attr_path = ?`, evt.RoomID, evt.Sender, v.ap).
Scan(&count); err != nil {
panic(err)
}
if count == 0 {
t.Error("Subscription not found")
} else if count > 1 {
t.Error("Too many matches")
}
if err := db.QueryRow(`SELECT last_visited, error FROM packages WHERE attr_path = ?`, v.ap).Scan(&lv, &hasErr); err != nil {
panic(err)
}
if lv != v.lv {
t.Errorf("Wrong last_visited date: %s", lv)
}
if hasErr != v.err {
t.Errorf("Wrong hasError: %v", hasErr)
}
}
}
// TODO: test non-existent package
func TestUnsub(t *testing.T) {
if err := setupDB(ctx, ":memory:"); err != nil {
panic(err)
}
// TODO: what's the point of having two test cases here?
tt := []struct {
ap string
lv string
err bool
}{
{
ap: "foo",
lv: "1970-01-01",
err: false,
},
{
ap: "python312Packages.bar",
lv: "1980-01-01",
err: true,
},
}
var count int
for _, v := range tt {
h = handlers{
logFetcher: func(string) (string, bool) {
return v.lv, v.err
},
sender: testSender,
}
if _, err := db.Exec("INSERT INTO packages(attr_path, last_visited) VALUES (?, ?)", v.ap, v.lv); err != nil {
panic(err)
}
// NOTE: in this test, we insert last_visited ourselves instead of relying
// on the Go logic, since we've tested that logic in the TestSub test
if _, err := db.Exec("INSERT INTO subscriptions (roomid, mxid, attr_path) VALUES (?, ?, ?)",
evt.RoomID, evt.Sender, v.ap); err != nil {
panic(err)
}
unsubscribe(v.ap)
if err := db.QueryRow(`
SELECT COUNT(*)
FROM subscriptions
WHERE roomid = ?
AND mxid = ?
AND attr_path = ?`, evt.RoomID, evt.Sender, v.ap).
Scan(&count); err != nil {
panic(err)
}
if count != 0 {
t.Error("Subscription not removed")
}
}
}
// TODO: Test when user has subbed to p312Pkgs.foo, and does `unsub *.foo`
// currently, it prints out an error about not being subbed to e.g. p313Pkgs.foo
func TestSubUnsub(t *testing.T) {
if err := setupDB(ctx, ":memory:"); err != nil {
panic(err)
}
h = handlers{
logFetcher: func(string) (string, bool) {
return "1999", false
},
sender: testSender,
}
addPackages(
"bar",
"python31Packages.bar",
"python32Packages.bar",
"haskellPackages.bar",
)
patterns := []struct {
pattern string
matches int
}{
{
pattern: "python3?Packages.bar",
matches: 2,
},
{
pattern: "*.bar",
matches: 3,
},
}
var count int
for _, p := range patterns {
t.Run(p.pattern, func(t *testing.T) {
t.Run("subscribe", func(t *testing.T) {
subscribe(p.pattern)
if err := db.QueryRow(`
SELECT COUNT(*)
FROM subscriptions
WHERE attr_path GLOB ?`, p.pattern).
Scan(&count); err != nil {
panic(err)
}
if count != p.matches {
t.Errorf("Not enough matches for %s: %v", p.pattern, count)
}
})
t.Run("unsubscribe", func(t *testing.T) {
unsubscribe(p.pattern)
if err := db.QueryRow(`
SELECT COUNT(*)
FROM subscriptions
WHERE attr_path GLOB ?`, p.pattern).
Scan(&count); err != nil {
panic(err)
}
if count != 0 {
t.Errorf("Leftover subscriptions for %s: %v", p.pattern, count)
}
})
})
}
for _, pattern := range []string{
"*",
"**",
"?",
"python3Packages.*",
} {
t.Run("spammy queries", func(t *testing.T) {
var before, after int
if err := db.QueryRow(`SELECT COUNT(*) FROM subscriptions`, pattern).Scan(&before); err != nil {
panic(err)
}
subscribe(pattern)
if err := db.QueryRow(`SELECT COUNT(*) FROM subscriptions`, pattern).Scan(&after); err != nil {
panic(err)
}
if before != after {
t.Errorf("Should not have subscribed for query %s: %v, %v", pattern, before, after)
}
})
}
}
func TestOverlapping(t *testing.T) {
if err := setupDB(ctx, ":memory:"); err != nil {
panic(err)
}
h = handlers{
logFetcher: func(string) (string, bool) {
return "1999", false
},
sender: testSender,
}
aps := []string{
"python31Packages.foo",
"python32Packages.foo",
"python33Packages.foo",
}
addPackages(aps...)
subscribe(aps[0])
pattern := "python*.foo"
subscribe(pattern)
var count int
if err := db.QueryRow(`
SELECT COUNT(*)
FROM subscriptions
WHERE attr_path GLOB ?`, pattern).
Scan(&count); err != nil {
panic(err)
}
if count != len(aps) {
t.Errorf("Not enough matches for %s: %v", pattern, count)
}
}
func TestCheckIfSubExists(t *testing.T) {
if err := setupDB(ctx, ":memory:"); err != nil {
panic(err)
}
addPackages("foo")
exists, err := checkIfSubExists("foo", evt.RoomID.String())
if err != nil {
panic(err)
} else if exists {
t.Errorf("should not exist")
}
subscribe("foo")
exists, err = checkIfSubExists("foo", evt.RoomID.String())
if err != nil {
panic(err)
} else if !exists {
t.Errorf("should exist")
}
}
func fillEventContent(evt *event.Event, body string) {
evt.Content = event.Content{
Parsed: &event.MessageEventContent{
MsgType: event.MsgText,
Body: body,
},
}
}
func addPackages(aps ...string) {
for _, ap := range aps {
if _, err := db.Exec("INSERT INTO packages(attr_path) VALUES (?)", ap); err != nil {
panic(err)
}
}
}
func subscribe(ap string) {
fillEventContent(evt, fmt.Sprintf("sub %s", ap))
handleMessage(ctx, evt)
}
func unsubscribe(ap string) {
fillEventContent(evt, fmt.Sprintf("unsub %s", ap))
handleMessage(ctx, evt)
}