-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoller_test.go
More file actions
103 lines (88 loc) · 2.53 KB
/
Copy pathpoller_test.go
File metadata and controls
103 lines (88 loc) · 2.53 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
package maxigobot
import (
"encoding/json"
"testing"
maxigo "github.com/maxigo-bot/maxigo-client"
)
func TestParseUpdate_messageCreated(t *testing.T) {
raw := json.RawMessage(`{
"update_type": "message_created",
"timestamp": 1000,
"message": {
"sender": {"user_id": 1, "first_name": "Test", "is_bot": false, "last_activity_time": 0},
"recipient": {"chat_id": 2, "chat_type": "dialog"},
"timestamp": 1000,
"body": {"mid": "m1", "seq": 1, "text": "hello"}
}
}`)
upd, err := ParseUpdate(raw)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if upd == nil {
t.Fatal("expected non-nil update")
}
if msg, ok := upd.(*maxigo.MessageCreatedUpdate); !ok {
t.Errorf("expected *maxigo.MessageCreatedUpdate, got %T", upd)
} else if msg.Message.Body.MID != "m1" {
t.Errorf("MID = %q, want %q", msg.Message.Body.MID, "m1")
}
}
func TestParseUpdate_botStarted(t *testing.T) {
raw := json.RawMessage(`{
"update_type": "bot_started",
"timestamp": 2000,
"chat_id": 123,
"user": {"user_id": 456, "first_name": "User", "is_bot": false, "last_activity_time": 0}
}`)
upd, err := ParseUpdate(raw)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if upd == nil {
t.Fatal("expected non-nil update")
}
}
func TestParseUpdate_unknownType(t *testing.T) {
raw := json.RawMessage(`{"update_type": "future_event", "timestamp": 3000}`)
upd, err := ParseUpdate(raw)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if upd != nil {
t.Error("unknown update type should return nil")
}
}
func TestParseUpdate_invalidJSON(t *testing.T) {
raw := json.RawMessage(`{invalid`)
_, err := ParseUpdate(raw)
if err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestParseUpdate_allTypes(t *testing.T) {
types := []string{
"message_created", "message_callback", "message_edited", "message_removed",
"bot_started", "bot_stopped", "bot_added", "bot_removed",
"user_added", "user_removed", "chat_title_changed", "message_chat_created",
"dialog_muted", "dialog_unmuted", "dialog_cleared", "dialog_removed",
}
for _, ut := range types {
t.Run(ut, func(t *testing.T) {
raw := json.RawMessage(`{"update_type":"` + ut + `","timestamp":1}`)
upd, err := ParseUpdate(raw)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if upd == nil {
t.Fatalf("expected non-nil update for type %q", ut)
}
})
}
}
func TestLongPoller_defaultTimeout(t *testing.T) {
lp := &LongPoller{}
if lp.Timeout != 0 {
t.Errorf("default Timeout = %d, want 0 (will use 30 in Poll)", lp.Timeout)
}
}