Skip to content

Commit

Permalink
More discordplayer test scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
fakelag committed Feb 25, 2024
1 parent c7660ac commit d9cbd73
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions discordplayer/discordplayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ var _ = Describe("Discord Player", func() {

currentMediaDone := make(chan error)
mockDcaStreamingSession := NewMockDcaStreamingSession(ctrl)
playerContext := JoinMockVoiceChannelAndPlayEx(context.TODO(), ctrl, currentMediaDone, true, mockDcaStreamingSession)
playerContext := JoinMockVoiceChannelAndPlayEx(context.TODO(), ctrl, currentMediaDone, false, mockDcaStreamingSession)

mockIsPaused := false
mockDcaStreamingSession.EXPECT().SetPaused(gomock.Any()).Do(func(pause bool) {
Expand All @@ -436,6 +436,12 @@ var _ = Describe("Discord Player", func() {
return mockIsPaused
}).MinTimes(1)

Expect(playerContext.dms.SetPaused(true)).To(MatchError(discordplayer.ErrorNotStreaming))

Expect(playerContext.dms.EnqueueMedia(playerContext.mockMedia)).NotTo(HaveOccurred())
_, err := playerContext.dms.Start()
Expect(err).NotTo(HaveOccurred())

c := make(chan struct{})

Eventually(func() error {
Expand Down Expand Up @@ -522,7 +528,8 @@ var _ = Describe("Discord Player", func() {
ctrl := gomock.NewController(GinkgoT())

currentMediaDone := make(chan error)
playerContext := JoinMockVoiceChannelAndPlay(ctrl, currentMediaDone)
mockDcaStreamingSession := NewMockDcaStreamingSession(ctrl)
playerContext := JoinMockVoiceChannelAndPlayEx(context.TODO(), ctrl, currentMediaDone, false, mockDcaStreamingSession)

c := make(chan struct{})

Expand All @@ -533,6 +540,12 @@ var _ = Describe("Discord Player", func() {
}),
)

Expect(playerContext.dms.Jump(30 * time.Second)).To(MatchError(discordplayer.ErrorNoMediaFound))

Expect(playerContext.dms.EnqueueMedia(playerContext.mockMedia)).NotTo(HaveOccurred())
_, err := playerContext.dms.Start()
Expect(err).NotTo(HaveOccurred())

Eventually(func() entities.Media {
return playerContext.dms.GetCurrentlyPlayingMedia()
}).WithTimeout(failTimeout).WithPolling(50 * time.Millisecond).ShouldNot(BeNil())
Expand Down Expand Up @@ -1371,18 +1384,15 @@ var _ = Describe("Discord Player", func() {

Expect(err).NotTo(HaveOccurred())
Expect(dms).NotTo(BeNil())

Expect(dms.GetGuildID()).To(Equal(gID))
Expect(dms.GetVoiceChannelID()).To(Equal(cID))

newChannelID := "new-channel-id"
workerStopped, err := dms.SetVoiceChannelID(newChannelID)
Expect(err).NotTo(HaveOccurred())
Expect(workerStopped).To(BeFalse())

Expect(dms.GetGuildID()).To(Equal(gID))
Expect(dms.GetVoiceChannelID()).To(Equal(newChannelID))

Expect(dms.Leave()).To(MatchError(discordplayer.ErrorWorkerNotActive))
})

Expand All @@ -1406,7 +1416,6 @@ var _ = Describe("Discord Player", func() {
workerStopped, err := playerContext.dms.SetVoiceChannelID(newChannelID)
Expect(err).NotTo(HaveOccurred())
Expect(workerStopped).To(BeTrue())

Expect(playerContext.dms.GetMediaQueue()).To(HaveLen(0))
Expect(playerContext.dms.EnqueueMedia(playerContext.mockMedia)).To(Succeed())

Expand All @@ -1427,6 +1436,32 @@ var _ = Describe("Discord Player", func() {
Fail("Voice worker timed out")
}
})

It("Does nothing if changing to the same voice channel", func() {
ctrl := gomock.NewController(GinkgoT())

mockDca := NewMockDiscordAudio(ctrl)
mockDiscordSession := NewMockDiscordSession(ctrl)

dms, err := discordplayer.NewDiscordMusicSessionEx(context.TODO(), mockDca, mockDiscordSession, 100*time.Millisecond,
&discordplayer.DiscordMusicSessionOptions{
GuildID: gID,
VoiceChannelID: cID,
MediaQueueMaxSize: 10,
})

Expect(err).NotTo(HaveOccurred())
Expect(dms).NotTo(BeNil())
Expect(dms.GetGuildID()).To(Equal(gID))
Expect(dms.GetVoiceChannelID()).To(Equal(cID))

workerStopped, err := dms.SetVoiceChannelID(cID)
Expect(err).NotTo(HaveOccurred())
Expect(workerStopped).To(BeFalse())
Expect(dms.GetGuildID()).To(Equal(gID))
Expect(dms.GetVoiceChannelID()).To(Equal(cID))
Expect(dms.Leave()).To(MatchError(discordplayer.ErrorWorkerNotActive))
})
})

When("Bot should leave by itself", func() {
Expand Down Expand Up @@ -1558,4 +1593,19 @@ var _ = Describe("Discord Player", func() {
Entry("When not setting LeaveAfterEmptyQueueTime", time.Duration(0)),
)
})

When("Using the API invalidly", func() {
It("Returns a sensible error if attempting to Start() without a voice channel", func() {
dms, err := discordplayer.NewDiscordMusicSession(context.TODO(), nil, &discordplayer.DiscordMusicSessionOptions{
GuildID: gID,
VoiceChannelID: "",
MediaQueueMaxSize: 10,
})

Expect(err).NotTo(HaveOccurred())
Expect(dms).NotTo(BeNil())
_, err = dms.Start()
Expect(err).To(MatchError(discordplayer.ErrorNoVoiceChannelSet))
})
})
})

0 comments on commit d9cbd73

Please sign in to comment.