-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmargelet_test.go
159 lines (120 loc) · 4.49 KB
/
margelet_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
package margelet_test
import (
"testing"
"time"
"../margelet"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/telegram-bot-api.v4"
)
func TestMargelet(t *testing.T) {
Convey("When given margelet", t, func() {
m := getMargelet()
Convey("When adding new message handler", func() {
m.AddMessageHandler(EchoHandler{})
Convey("It should be aded to message handlers array", func() {
So(m.MessageHandlers, ShouldNotBeEmpty)
})
})
Convey("When adding new command handler", func() {
m.AddCommandHandler("/test", margelet.HelpHandler{})
Convey("It should be aded to command handler hash", func() {
So(m.CommandHandlers, ShouldNotBeEmpty)
})
})
Convey("When adding new session handler", func() {
m.AddSessionHandler("/test", SumSession{})
Convey("It should be aded to command handler array", func() {
So(m.SessionHandlers, ShouldNotBeEmpty)
})
})
Convey("When sending message", func() {
_, err := m.Send(tgbotapi.NewMessage(0, "TEST"))
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When quick sending message", func() {
_, err := m.QuickSend(0, "TEST", nil)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When quick replying message", func() {
_, err := m.QuickReply(0, 100500, "TEST", nil)
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When getting file direct URL", func() {
_, err := m.GetFileDirectURL("test")
Convey("Error should be nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When asking is message sent to me", func() {
msg := tgbotapi.Message{}
res := m.IsMessageToMe(msg)
Convey("It should return false", func() {
So(res, ShouldBeFalse)
})
})
Convey("When trying to get config repository", func() {
repo := m.GetConfigRepository()
Convey("It should not return nil", func() {
So(repo, ShouldNotBeNil)
})
})
Convey("Given configured margelet", func() {
m.AddMessageHandler(EchoHandler{})
m.AddMessageHandler(PanicHandler{})
m.AddSessionHandler("/sum", SumSession{})
m.InlineHandler = &InlineImage{}
m.CallbackHandler = &CallbackMessage{}
chat := tgbotapi.Chat{ID: 1}
from := tgbotapi.User{ID: 1}
Convey("When running should handle message without panic", func() {
go m.Run()
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "Test", Chat: &chat, From: &from}}
time.Sleep(10 * time.Millisecond)
m.Stop()
})
Convey("When running should handle command without panic", func() {
go m.Run()
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "/help", Chat: &chat, From: &from}}
time.Sleep(10 * time.Millisecond)
m.Stop()
})
Convey("When running panic should not crash bot", func() {
go m.Run()
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "/panic", Chat: &chat, From: &from}}
time.Sleep(10 * time.Millisecond)
m.Stop()
})
Convey("When running should handle session without panic", func() {
go m.Run()
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "/sum", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "10", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "20", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "test", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "end", Chat: &chat, From: &from}}
time.Sleep(100 * time.Millisecond)
m.Stop()
})
Convey("When running should handle session with early cancel without panic", func() {
go m.Run()
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "/sum", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "10", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "20", Chat: &chat, From: &from}}
botMock.Updates <- tgbotapi.Update{Message: &tgbotapi.Message{Text: "cancel", Chat: &chat, From: &from}}
time.Sleep(100 * time.Millisecond)
m.Stop()
})
Convey("When running should handle inline query without panic", func() {
go m.Run()
botMock.Updates <- tgbotapi.Update{InlineQuery: &tgbotapi.InlineQuery{ID: "test_id", Query: "test", From: &from}}
time.Sleep(100 * time.Millisecond)
m.Stop()
})
})
})
}