-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
345 lines (267 loc) · 11.3 KB
/
handler_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
package main
import (
"fmt"
"sync"
"testing"
"github.com/bwmarrin/discordgo"
"github.com/golang/mock/gomock"
mock_main "github.com/jamestjw/lyrical/mocks/mock_main"
"github.com/jamestjw/lyrical/mocks/mock_models"
mock_voice "github.com/jamestjw/lyrical/mocks/mock_voice"
"github.com/jamestjw/lyrical/models"
"github.com/jamestjw/lyrical/playlist"
"github.com/jamestjw/lyrical/voice"
"github.com/stretchr/testify/mock"
)
func TestHelpRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().SendMessage(gomock.Any())
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
helpRequest(mockEvent, "")
}
func TestLeaveVoiceChannelRequestWhenConnected(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().IsPlayingMusic().Return(true)
mockChannel.EXPECT().StopMusic()
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().Disconnect().Times(1)
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
mockEvent.EXPECT().SendMessage("Left voice channel 👋")
leaveVoiceChannelRequest(mockEvent, "")
}
func TestLeaveVoiceChannelRequestWhenNotConnected(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
mockEvent.EXPECT().GetVoiceConnection().Return(nil, false)
mockEvent.EXPECT().SendMessage("I am not in a voice channel.")
leaveVoiceChannelRequest(mockEvent, "")
}
func TestNowPlayingRequestWhileNotConnected(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
mockEvent.EXPECT().GetVoiceConnection().Return(nil, false)
mockEvent.EXPECT().SendMessage("Hey I dont remember being invited to a voice channel. 😔")
nowPlayingRequest(mockEvent, "")
}
func TestNowPlayingRequestWhileConnectedAndPlayingMusic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().IsPlayingMusic().Return(true)
mockChannel.EXPECT().GetNowPlayingName().Return("current song name")
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().GetGuildID().Return("guildID")
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().SendMessage("Now playing: **current song name**")
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
nowPlayingRequest(mockEvent, "")
}
func TestNowPlayingRequestWhileConnectedAndNotPlayingMusic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().IsPlayingMusic().Return(false)
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().GetGuildID().Return("guildID")
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().SendMessage("Well I am not playing any music currently 🤔")
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
nowPlayingRequest(mockEvent, "")
}
func TestSkipMusicRequestWhileConnectedAndPlayingMusic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().IsPlayingMusic().Return(true)
mockChannel.EXPECT().StopMusic()
mockChannel.EXPECT().ExistsNext().Return(false)
mockChannel.EXPECT().ExistsBackupNext().Return(false)
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().GetGuildID().Return("guildID")
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
skipMusicRequest(mockEvent, "")
}
func TestStopMusicRequestWhileConnectedAndPlayingMusic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().IsPlayingMusic().Return(true)
mockChannel.EXPECT().StopMusic()
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().GetGuildID().Return("guildID")
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
stopMusicRequest(mockEvent, "")
}
func TestPlayMusicRequestWhileConnectedAndPlayingMusic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
audiochan := make(chan []byte)
// Waiting is required because we call a goroutine.
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().IsPlayingMusic().Return(false)
mockChannel.EXPECT().ExistsNext().AnyTimes().Return(true)
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockPlayer := mock_voice.NewMockMusicPlayer(ctrl)
mockPlayer.EXPECT().PlayMusic(audiochan, "guildID", mockChannel, true).Do(func(chan []byte, string, voice.Channel, bool) { wg.Done() })
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().GetGuildID().Return("guildID")
mockConnection.EXPECT().GetAudioInputChannel().Return(audiochan)
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
mockEvent.EXPECT().SendMessage("Starting music... 🎵")
voice.DefaultMusicPlayer = mockPlayer
playMusicRequest(mockEvent, "")
}
// AddToPlaylist
type mockYoutubeService struct {
mock.Mock
}
func (s *mockYoutubeService) GetVideoID(id string) (string, error) {
args := s.Called(id)
return args.String(0), args.Error(1)
}
type mockMusicDownloader struct {
mock.Mock
}
func (m *mockMusicDownloader) Download(query string) (title string, err error) {
args := m.Called(query)
return args.String(0), args.Error(1)
}
func TestAddToPlaylist(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockSS := new(mockYoutubeService)
mockSS.On("GetVideoID", "song name").Return("video id", nil)
mockDS := mock_models.NewMockDatastore(ctrl)
mockDS.EXPECT().AddSongToDB("song name", "video id").Return(nil)
mockDS.EXPECT().SongExists("video id").Return("", false)
mockDl := new(mockMusicDownloader)
mockDl.On("Download", "video id").Return("song name", nil)
// Waiting is required because we call a goroutine.
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
audiochan := make(chan []byte)
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().FetchPlaylist().Return(&playlist.Playlist{})
gomock.InOrder(
mockChannel.EXPECT().ExistsNext().Return(false),
mockChannel.EXPECT().ExistsNext().Return(true),
)
mockChannel.EXPECT().SetNext(gomock.Any())
mockChannel.EXPECT().IsPlayingMusic().Return(false)
mockPlayer := mock_voice.NewMockMusicPlayer(ctrl)
mockPlayer.EXPECT().PlayMusic(audiochan, "guildID", mockChannel, true).Do(func(chan []byte, string, voice.Channel, bool) { wg.Done() })
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnection.EXPECT().GetGuildID().Return("guildID")
mockConnection.EXPECT().GetAudioInputChannel().Return(audiochan)
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetVoiceConnection().Return(mockConnection, true)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
gomock.InOrder(
mockEvent.EXPECT().SendMessage("Adding to playlist 😉"),
mockEvent.EXPECT().SendMessage("Your song **song name** was added 👍"),
mockEvent.EXPECT().SendMessage("Playing next song in the playlist... 🎵"),
)
searchService = mockSS
models.DS = mockDS
voice.Dl = mockDl
voice.DefaultMusicPlayer = mockPlayer
voice.ActiveVoiceChannels["guildID"] = mockChannel
addToPlaylistRequest(mockEvent, "song name")
}
func TestJoinChannelRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connMap := make(map[string]voice.Connection)
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().ExistsNext().Return(false)
mockChannel.EXPECT().ExistsBackupNext().Return(false)
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockConnection := mock_voice.NewMockConnection(ctrl)
mockConnectable := mock_voice.NewMockConnectable(ctrl)
mockConnectable.EXPECT().GetVoiceConnections().Return(connMap)
mockConnectable.EXPECT().JoinVoiceChannel("guildID", "channel-id").Return(mockConnection, nil)
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().FindVoiceChannel("channel-name").Return("channel-id", nil)
mockEvent.EXPECT().GetSession().AnyTimes().Return(mockConnectable)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
gomock.InOrder(
mockEvent.EXPECT().SendMessage("Connecting to channel name: channel-name"),
mockEvent.EXPECT().SendMessage("Playlist is still empty."),
)
joinVoiceChannelRequest(mockEvent, "channel-name")
}
func TestUpNextRequest(t *testing.T) {
config.UpNextMaxSongsCount = 2
var songs []*playlist.Song
for i := 0; i < 2; i++ {
songName := fmt.Sprintf("Song %v", i)
song := &playlist.Song{
YoutubeID: "",
Name: songName,
Next: nil,
}
songs = append(songs, song)
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChannel := mock_voice.NewMockChannel(ctrl)
mockChannel.EXPECT().GetNextSongs().Return(songs, true)
mockChannel.EXPECT().GetNextBackupSongs().Return([]*playlist.Song{}, false)
voice.ActiveVoiceChannels["guildID"] = mockChannel
mockEvent := mock_main.NewMockEvent(ctrl)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
gomock.InOrder(
mockEvent.EXPECT().SendMessage("Coming Up Next:\n1. Song 0\n2. Song 1"),
)
upNextRequest(mockEvent, "")
}
func TestNewPollRequest(t *testing.T) {
// Waiting is required because we call a goroutine.
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
sentMessage := &discordgo.Message{ID: "id"}
reactions := map[string][]string{"1️⃣": {"user1"}, "1️2️⃣": {"user2", "user3"}}
expectedPollMessage := "A poll has been started!\n**title**\n1️⃣. option1\n2️⃣. option2\nExercise your right to vote by reacting accordingly! The poll will close in 1s."
mockEvent := mock_main.NewMockEvent(ctrl)
gomock.InOrder(
mockEvent.EXPECT().SendMessage(expectedPollMessage).Return(sentMessage),
mockEvent.EXPECT().ReactToMessage("1️⃣", "id"),
mockEvent.EXPECT().ReactToMessage("2️⃣", "id"),
mockEvent.EXPECT().SendQuotedMessageWithMentions(expectedPollMessage, gomock.Any(), gomock.Any()).Do(func(string, string, []string) { wg.Done() }),
)
mockEvent.EXPECT().GetGuildID().AnyTimes().Return("guildID")
mockEvent.EXPECT().GetReactionsFromMessage("id").Return(reactions, nil)
mockEvent.EXPECT().GetUserForBot().Return(&discordgo.User{ID: "user_id"}, nil)
params := "title 1 option1 option2"
newPollRequest(mockEvent, params)
}