This repository was archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_test.go
More file actions
409 lines (388 loc) · 11.6 KB
/
Copy pathcommand_test.go
File metadata and controls
409 lines (388 loc) · 11.6 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
package busetabot
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
"github.com/yi-jiayu/telegram-bot-api"
"github.com/yi-jiayu/bus-eta-bot/v4/mocks"
"github.com/yi-jiayu/bus-eta-bot/v4/telegram"
)
// collectResponsesWithTimeout returns a slice of responses received from the provided channel. If nothing is received
// within timeout, it returns the responses received up until that point.
func collectResponsesWithTimeout(responses <-chan Response, timeout time.Duration) (collected []Response, err error) {
for {
deadline := time.NewTimer(timeout)
select {
case r, ok := <-responses:
if !ok {
return
}
collected = append(collected, r)
case <-deadline.C:
err = errors.New("timed out")
return
}
deadline.Stop()
}
}
func TestFallbackCommandHandler(t *testing.T) {
tgAPI, reqChan, errChan := NewMockTelegramAPIWithPath()
defer tgAPI.Close()
tg := &tgbotapi.BotAPI{
APIEndpoint: tgAPI.URL + "/bot%s/%s",
Client: http.DefaultClient,
}
bot := NewBot(handlers, tg, nil, nil, nil)
testCases := []struct {
Name string
ChatType string
Text string
Expected Request
}{
{
Name: "Bus stop code with slash in front, private chat",
ChatType: ChatTypePrivate,
Text: "/96049",
Expected: Request{
Path: "/bot/sendMessage",
Body: "chat_id=1&disable_notification=false&disable_web_page_preview=false&text=Oops%2C+that+was+not+a+valid+command%21+If+you+wanted+to+get+etas+for+bus+stop+96049%2C+just+send+the+bus+stop+code+without+the+leading+slash.",
},
},
{
Name: "Bus stop code with slash in front, group chat",
Text: "/96049",
Expected: Request{
Path: "/bot/sendMessage",
Body: "chat_id=1&disable_notification=false&disable_web_page_preview=false&reply_to_message_id=1&text=Oops%2C+that+was+not+a+valid+command%21+If+you+wanted+to+get+etas+for+bus+stop+96049%2C+just+send+the+bus+stop+code+without+the+leading+slash.",
},
},
{
Name: "Invalid command, private chat",
Text: "/invalid",
Expected: Request{
Path: "/bot/sendMessage",
Body: "chat_id=1&disable_notification=false&disable_web_page_preview=false&reply_to_message_id=1&text=Oops%2C+that+was+not+a+valid+command%21",
},
},
{
Name: "Invalid command, group chat",
Text: "/invalid",
Expected: Request{
Path: "/bot/sendMessage",
Body: "chat_id=1&disable_notification=false&disable_web_page_preview=false&reply_to_message_id=1&text=Oops%2C+that+was+not+a+valid+command%21",
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
message := MockMessageWithType(tc.ChatType)
message.Text = tc.Text
err := FallbackCommandHandler(nil, &bot, &message)
if err != nil {
t.Fatal(err)
}
select {
case req := <-reqChan:
actual := req
expected := tc.Expected
if actual != expected {
fmt.Printf("Expected:\n%#v\nActual:\n%#v\n", expected, actual)
t.Fail()
}
case err := <-errChan:
t.Fatal(err)
}
})
}
}
func TestEtaHandler(t *testing.T) {
type testCase struct {
Name string
ChatType string
Text string
Expected []Response
}
testCases := []testCase{
{
Name: "without arguments, in private chat",
ChatType: "private",
Text: "/eta",
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Alright, send me a bus stop code to get etas for.",
ReplyToMessageID: 1,
ReplyMarkup: telegram.NewForceReply(true),
}),
},
},
{
Name: "without arguments, in group chat",
ChatType: "group",
Text: "/eta",
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Alright, send me a bus stop code to get etas for.",
ReplyToMessageID: 1,
ReplyMarkup: telegram.NewForceReply(true),
}),
},
},
{
Name: "with invalid bus stop code, in private chat",
ChatType: "private",
Text: "/eta invalid",
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Oops, a bus stop code should be a 5-digit number.",
}),
},
},
{
Name: "with invalid bus stop code, in group chat",
ChatType: "group",
Text: "/eta invalid",
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Oops, a bus stop code should be a 5-digit number.",
ReplyToMessageID: 1,
ReplyMarkup: telegram.NewForceReply(true),
}),
},
},
}
bot := new(BusEtaBot)
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
message := MockMessageWithType(tc.ChatType)
message.Text = tc.Text
responses := make(chan Response, ResponseBufferSize)
go EtaHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := tc.Expected
assert.Equal(t, expected, actual)
})
}
}
func TestAboutHandler(t *testing.T) {
bot := new(BusEtaBot)
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go AboutHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Bus Eta Bot VERSION\nhttps://github.com/yi-jiayu/bus-eta-bot",
}),
}
assert.Equal(t, expected, actual)
}
func TestVersionHandler(t *testing.T) {
bot := new(BusEtaBot)
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go VersionHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Bus Eta Bot VERSION\nhttps://github.com/yi-jiayu/bus-eta-bot",
}),
}
assert.Equal(t, expected, actual)
}
func TestStartHandler(t *testing.T) {
bot := new(BusEtaBot)
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go StartHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
s := "SUTD"
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Hello Jiayu,\n\nBus Eta Bot is a Telegram bot which can tell you how long you have to wait for your bus to arrive.\n\nTo get started, try sending me a bus stop code such as `96049` to get etas for.\n\nAlternatively, you can also search for bus stops by sending me an inline query. To try this out, type @BusEtaBot followed by a bus stop code, description or road name in any chat.\n\nThanks for trying out Bus Eta Bot! If you find Bus Eta Bot useful, do help to spread the word or send /feedback to leave some feedback about how to help make Bus Eta Bot even better!\n\nIf you're stuck, you can send /help to view help.",
ParseMode: "markdown",
ReplyMarkup: &telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Get etas for bus stop 96049",
CallbackData: "{\"t\":\"eta_demo\"}",
},
{
Text: "Try an inline query",
CallbackData: "",
SwitchInlineQueryCurrentChat: &s,
},
},
},
},
}),
}
assert.Equal(t, expected, actual)
}
func TestHelpHandler(t *testing.T) {
bot := new(BusEtaBot)
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go HelpHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "You can find help on how to use Bus Eta Bot [here](http://telegra.ph/Bus-Eta-Bot-Help-02-23).",
ParseMode: "markdown",
}),
}
assert.Equal(t, expected, actual)
}
func TestPrivacyHandler(t *testing.T) {
bot := new(BusEtaBot)
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go PrivacyHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "You can find Bus Eta Bot's privacy policy [here](https://t.me/iv?url=https%3A%2F%2Fgithub.com%2Fyi-jiayu%2Fbus-eta-bot%2Fblob%2Fmaster%2FPRIVACY.md&rhash=a44cb5372834ee).",
ParseMode: "markdown",
}),
}
assert.Equal(t, expected, actual)
}
func TestFeedbackCmdHandler(t *testing.T) {
bot := new(BusEtaBot)
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go FeedbackCmdHandler(context.Background(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Oops, the feedback command has not been implemented yet. In the meantime, you can raise issues or show your support for Bus Eta Bot at its GitHub repository [here](https://github.com/yi-jiayu/bus-eta-bot).",
ParseMode: "markdown",
ReplyMarkup: nil,
}),
}
assert.Equal(t, expected, actual)
}
func TestShowFavouritesCmdHandler(t *testing.T) {
const userID = 1
type testCase struct {
Name string
Favourites []string
Expected []Response
}
testCases := []testCase{
{
Name: "when user has favourites",
Favourites: []string{"96049", "81111"},
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Favourites keyboard activated!",
ReplyMarkup: telegram.ReplyKeyboardMarkup{
Keyboard: [][]telegram.KeyboardButton{
{{Text: "96049"}},
{{Text: "81111"}},
},
ResizeKeyboard: true,
},
}),
},
},
{
Name: "when user has no favourites",
Favourites: nil,
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "You haven't set any favourites yet!",
}),
},
},
{
Name: "when user has empty favourites",
Favourites: []string{},
Expected: []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "You haven't set any favourites yet!",
}),
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockUserRepository(ctrl)
m.EXPECT().GetUserFavourites(gomock.Any(), userID).Return(tc.Favourites, nil)
bot := &BusEtaBot{
Users: m,
}
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go ShowFavouritesCmdHandler(context.TODO(), bot, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t, tc.Expected, actual) {
pretty.Println(actual)
}
})
}
}
func TestHideFavouritesCmdHandler(t *testing.T) {
message := MockMessage()
responses := make(chan Response, ResponseBufferSize)
go HideFavouritesCmdHandler(context.TODO(), nil, &message, responses)
actual, err := collectResponsesWithTimeout(responses, 5*time.Second)
if err != nil {
t.Fatal(err)
}
expected := []Response{
ok(telegram.SendMessageRequest{
ChatID: 1,
Text: "Favourites keyboard hidden!",
ReplyMarkup: telegram.ReplyKeyboardRemove{},
}),
}
if !assert.Equal(t, expected, actual) {
pretty.Println(actual)
}
}