-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter_test.go
More file actions
433 lines (388 loc) · 12.1 KB
/
Copy pathrouter_test.go
File metadata and controls
433 lines (388 loc) · 12.1 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package maxigobot
import (
"encoding/json"
"testing"
maxigo "github.com/maxigo-bot/maxigo-client"
)
func TestParseCommand(t *testing.T) {
tests := []struct {
name string
text string
wantCmd string
wantPl string
wantIsCmd bool
}{
{"simple command", "/start", "start", "", true},
{"command with payload", "/start:hello", "start", "hello", true},
{"payload with colons", "/start:a:b:c", "start", "a:b:c", true},
{"command with empty payload", "/start:", "start", "", true},
{"plain text", "hello world", "", "", false},
{"empty text", "", "", "", false},
{"slash only", "/", "", "", true},
{"slash with colon", "/:payload", "", "payload", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, pl, isCmd := parseCommand(tt.text)
if cmd != tt.wantCmd {
t.Errorf("command = %q, want %q", cmd, tt.wantCmd)
}
if pl != tt.wantPl {
t.Errorf("payload = %q, want %q", pl, tt.wantPl)
}
if isCmd != tt.wantIsCmd {
t.Errorf("isCommand = %v, want %v", isCmd, tt.wantIsCmd)
}
})
}
}
func TestResolveEndpoint(t *testing.T) {
tests := []struct {
name string
update any
wantEndpoint string
wantCmd string
wantPayload string
}{
{
"command message",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{Text: ptrString("/help")}},
},
"/help", "help", "",
},
{
"command with payload",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{Text: ptrString("/start:hello")}},
},
"/start", "start", "hello",
},
{
"plain text",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{Text: ptrString("hello")}},
},
OnText, "", "",
},
{
"contact attachment with text",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Text: ptrString("John Doe"),
Attachments: []json.RawMessage{json.RawMessage(`{"type":"contact"}`)},
}},
},
OnContact, "", "",
},
{
"contact attachment without text",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Attachments: []json.RawMessage{json.RawMessage(`{"type":"contact"}`)},
}},
},
OnContact, "", "",
},
{
"photo attachment",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Attachments: []json.RawMessage{json.RawMessage(`{"type":"image"}`)},
}},
},
OnPhoto, "", "",
},
{
"location attachment",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Attachments: []json.RawMessage{json.RawMessage(`{"type":"location"}`)},
}},
},
OnLocation, "", "",
},
{
"unknown attachment falls through to text",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Text: ptrString("hello"),
Attachments: []json.RawMessage{json.RawMessage(`{"type":"sticker"}`)},
}},
},
OnText, "", "",
},
{
"unknown attachment no text",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Attachments: []json.RawMessage{json.RawMessage(`{"type":"sticker"}`)},
}},
},
OnMessage, "", "",
},
{
"malformed attachment JSON falls through to text",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Text: ptrString("hello"),
Attachments: []json.RawMessage{json.RawMessage(`{invalid}`)},
}},
},
OnText, "", "",
},
{
"command with photo attachment routes to photo",
&maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{
Text: ptrString("/start"),
Attachments: []json.RawMessage{json.RawMessage(`{"type":"image"}`)},
}},
},
OnPhoto, "", "",
},
{
"no text",
&maxigo.MessageCreatedUpdate{},
OnMessage, "", "",
},
{
"callback",
&maxigo.MessageCallbackUpdate{
Callback: maxigo.Callback{Payload: "confirm"},
},
OnCallback("confirm"), "", "",
},
{
"edited message",
&maxigo.MessageEditedUpdate{},
OnEdited, "", "",
},
{
"removed message",
&maxigo.MessageRemovedUpdate{},
OnRemoved, "", "",
},
{
"bot started",
&maxigo.BotStartedUpdate{},
OnBotStarted, "", "",
},
{
"bot stopped",
&maxigo.BotStoppedUpdate{},
OnBotStopped, "", "",
},
{
"bot added",
&maxigo.BotAddedUpdate{},
OnBotAdded, "", "",
},
{
"bot removed",
&maxigo.BotRemovedUpdate{},
OnBotRemoved, "", "",
},
{
"user added",
&maxigo.UserAddedUpdate{},
OnUserAdded, "", "",
},
{
"user removed",
&maxigo.UserRemovedUpdate{},
OnUserRemoved, "", "",
},
{
"chat title changed",
&maxigo.ChatTitleChangedUpdate{},
OnChatTitleChanged, "", "",
},
{
"chat created",
&maxigo.MessageChatCreatedUpdate{},
OnChatCreated, "", "",
},
{
"dialog muted",
&maxigo.DialogMutedUpdate{},
OnDialogMuted, "", "",
},
{
"dialog unmuted",
&maxigo.DialogUnmutedUpdate{},
OnDialogUnmuted, "", "",
},
{
"dialog cleared",
&maxigo.DialogClearedUpdate{},
OnDialogCleared, "", "",
},
{
"dialog removed",
&maxigo.DialogRemovedUpdate{},
OnDialogRemoved, "", "",
},
{
"unknown type",
"something",
"", "", "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ep, cmd, pl := resolveEndpoint(tt.update)
if ep != tt.wantEndpoint {
t.Errorf("endpoint = %q, want %q", ep, tt.wantEndpoint)
}
if cmd != tt.wantCmd {
t.Errorf("command = %q, want %q", cmd, tt.wantCmd)
}
if pl != tt.wantPayload {
t.Errorf("payload = %q, want %q", pl, tt.wantPayload)
}
})
}
}
func TestFindHandler_fallback(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
// Register OnText as fallback.
onTextHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnText] = onTextHandler
// A command that has no handler should fall back to OnText.
entry, _ := b.findHandler("/unknown", &maxigo.MessageCreatedUpdate{})
if entry != onTextHandler {
t.Error("should fall back to OnText handler")
}
}
func TestFindHandler_onMessageCatchAll(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
onMsgHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnMessage] = onMsgHandler
// OnText not registered — should fall back to OnMessage.
entry, _ := b.findHandler(OnText, &maxigo.MessageCreatedUpdate{})
if entry != onMsgHandler {
t.Error("should fall back to OnMessage handler")
}
}
func TestFindHandler_callbackFallback(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
catchAll := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnCallback("")] = catchAll
// Specific callback not registered — should fall back to catch-all.
entry, _ := b.findHandler(OnCallback("unknown"), &maxigo.MessageCallbackUpdate{})
if entry != catchAll {
t.Error("should fall back to catch-all callback handler")
}
}
func TestFindHandler_exactMatch(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
startHandler := &handlerEntry{handler: func(c Context) error { return nil }}
onTextHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers["/start"] = startHandler
b.handlers[OnText] = onTextHandler
// Exact command match should win over OnText.
entry, _ := b.findHandler("/start", &maxigo.MessageCreatedUpdate{})
if entry != startHandler {
t.Error("should match exact /start handler, not OnText")
}
}
func TestFindHandler_groupPriority(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
botHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers["/start"] = botHandler
g := b.Group()
groupHandler := &handlerEntry{handler: func(c Context) error { return nil }}
g.handlers["/start"] = groupHandler
// Group handler should be found first.
entry, _ := b.findHandler("/start", &maxigo.MessageCreatedUpdate{})
if entry != groupHandler {
t.Error("group handler should take priority over bot handler")
}
}
func TestFindHandler_attachmentExactMatch(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
contactHandler := &handlerEntry{handler: func(c Context) error { return nil }}
onTextHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnContact] = contactHandler
b.handlers[OnText] = onTextHandler
// OnContact registered — should match exactly, not fall back to OnText.
entry, _ := b.findHandler(OnContact, &maxigo.MessageCreatedUpdate{})
if entry != contactHandler {
t.Error("should match exact OnContact handler")
}
}
func TestFindHandler_attachmentFallbackToOnText(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
onTextHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnText] = onTextHandler
// OnContact not registered, message has text — should fall back to OnText.
entry, _ := b.findHandler(OnContact, &maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{Text: ptrString("John Doe")}},
})
if entry != onTextHandler {
t.Error("should fall back to OnText when attachment handler not registered and message has text")
}
}
func TestFindHandler_attachmentFallbackToOnMessage(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
onMsgHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnMessage] = onMsgHandler
// OnContact not registered, no OnText — should fall back to OnMessage.
entry, _ := b.findHandler(OnContact, &maxigo.MessageCreatedUpdate{})
if entry != onMsgHandler {
t.Error("should fall back to OnMessage when no attachment handler and no OnText")
}
}
func TestFindHandler_attachmentSkipsOnTextWhenNoText(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
onTextHandler := &handlerEntry{handler: func(c Context) error { return nil }}
onMsgHandler := &handlerEntry{handler: func(c Context) error { return nil }}
b.handlers[OnText] = onTextHandler
b.handlers[OnMessage] = onMsgHandler
// OnContact not registered, message has no text — should skip OnText, fall back to OnMessage.
entry, _ := b.findHandler(OnContact, &maxigo.MessageCreatedUpdate{})
if entry != onMsgHandler {
t.Error("should skip OnText and fall back to OnMessage when message has no text")
}
}
func ptrString(s string) *string { return &s }
func TestFindHandler_attachmentNoHandler(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
// Attachment endpoint with no handlers at all — should return nil.
entry, _ := b.findHandler(OnContact, &maxigo.MessageCreatedUpdate{})
if entry != nil {
t.Error("should return nil when no handler matches attachment endpoint")
}
}
func TestFindHandler_attachmentGroupFallbackToOnText(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
g := b.Group()
groupTextHandler := &handlerEntry{handler: func(c Context) error { return nil }}
g.handlers[OnText] = groupTextHandler
// OnPhoto not registered, message has text, group has OnText — should fall back to group OnText.
entry, _ := b.findHandler(OnPhoto, &maxigo.MessageCreatedUpdate{
Message: maxigo.Message{Body: maxigo.MessageBody{Text: ptrString("caption")}},
})
if entry != groupTextHandler {
t.Error("should fall back to group OnText handler for attachment with text")
}
}
func TestFindHandler_attachmentGroupFallbackToOnMessage(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
g := b.Group()
groupMsgHandler := &handlerEntry{handler: func(c Context) error { return nil }}
g.handlers[OnMessage] = groupMsgHandler
// OnLocation not registered, no text, group has OnMessage — should fall back to group OnMessage.
entry, _ := b.findHandler(OnLocation, &maxigo.MessageCreatedUpdate{})
if entry != groupMsgHandler {
t.Error("should fall back to group OnMessage handler for attachment")
}
}
func TestFindHandler_noMatch(t *testing.T) {
b := &Bot{handlers: make(map[string]*handlerEntry)}
entry, _ := b.findHandler("/nothing", &maxigo.MessageCreatedUpdate{})
if entry != nil {
t.Error("should return nil when no handler matches")
}
}