musicube/test/test_bot.py

158 lines
6.5 KiB
Python
Raw Normal View History

2021-09-25 01:00:20 +00:00
import asyncio
2022-04-14 23:22:45 +00:00
from unittest.mock import AsyncMock, Mock, patch
2021-09-24 21:07:32 +00:00
import pytest
from discord.ext import commands
@pytest.mark.asyncio
2021-09-25 01:49:46 +00:00
async def test_bot_ensure_voice(mbot, ctx):
2021-09-25 18:59:02 +00:00
# TEST: Connects to voice channel of author if possible
2021-09-24 21:07:32 +00:00
ctx.voice_client = None
ctx.author.voice = AsyncMock()
await mbot.ensure_voice(ctx)
2022-04-14 23:22:45 +00:00
assert ctx.author.voice.channel.connect.call_count == 1, 'Did not connect to voice channel of author'
2021-09-24 21:07:32 +00:00
ctx.reset_mock(return_value=True)
2021-09-25 18:59:02 +00:00
# TEST: Error if author not inside a channel
2021-09-24 21:07:32 +00:00
ctx.voice_client = None
ctx.author.voice = None
with pytest.raises(commands.CommandError):
await mbot.ensure_voice(ctx)
2021-09-24 21:37:46 +00:00
def mock_ytdl_extract_info(ytdl, *, title, webpage_url, audio_url):
ytdl.extract_info.return_value = {'entries': [{'title': title, 'webpage_url': webpage_url, 'url': audio_url}]}
2021-09-25 16:05:46 +00:00
def mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio):
2021-10-02 00:10:41 +00:00
audio = Mock()
ffmpeg_pcm_audio.return_value = audio
return audio
2021-09-25 16:05:46 +00:00
2021-09-24 21:37:46 +00:00
@pytest.mark.asyncio
2021-09-25 16:05:46 +00:00
async def test_bot_playback(mbot, ctx):
2021-09-25 01:00:20 +00:00
with patch.object(mbot, '_ytdl') as ytdl:
with patch('discord.FFmpegPCMAudio') as ffmpeg_pcm_audio:
2021-09-25 18:59:02 +00:00
# TEST: First song queued is immediately played
2021-09-25 01:00:20 +00:00
ctx.voice_client.is_playing.return_value = False
2022-04-14 23:22:45 +00:00
url = 'https://www.youtube.com/watch?v=Wr9LZ1hAFpQ'
title = 'In Flames - Deliver Us (Official Video)'
mock_ytdl_extract_info(ytdl, title=title, webpage_url=url, audio_url=url)
2021-09-25 16:05:46 +00:00
deliver_us_audio = mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio)
2021-09-25 01:00:20 +00:00
query = 'in flames deliver us'
# pylint: disable=too-many-function-args
await mbot.play(mbot, ctx, query=query)
2021-09-25 16:05:46 +00:00
assert \
2022-04-14 23:22:45 +00:00
ytdl.extract_info.call_args.args == (query,) and ytdl.extract_info.call_args.kwargs == {'download': False}, \
f'ytdl.extract_info was not called with {query}, {{ download: False }}'
2021-09-25 16:05:46 +00:00
assert \
ffmpeg_pcm_audio.call_args is None, \
2022-04-14 23:22:45 +00:00
f'FFmpegPCMAudio was immediately called with {url} instead of being queued'
2021-09-25 16:05:46 +00:00
assert \
ctx.voice_client.play.call_args is None, \
2022-04-14 23:22:45 +00:00
'Did immediately playback audio instead of being queued'
2021-09-25 16:05:46 +00:00
assert \
ctx.send.call_args is None, \
"Did immediately send 'Now playing:' message of being queued"
await asyncio.sleep(0)
assert \
ffmpeg_pcm_audio.call_args.args == (url,), \
2022-04-14 23:22:45 +00:00
f'FFmpegPCMAudio was not called with {url}'
2021-09-25 16:05:46 +00:00
assert \
ctx.voice_client.play.call_args.args == (deliver_us_audio,), \
2022-04-14 23:22:45 +00:00
'Did not playback correct audio'
content = ctx.send.call_args.kwargs['content']
assert content == 'Now playing:', "Did not send 'Now playing:' message"
2022-04-14 23:22:45 +00:00
embed = ctx.send.call_args.kwargs['embed']
assert embed.title == title, "Did not send 'Now playing:' embed"
2021-09-25 16:05:46 +00:00
2021-09-25 18:59:02 +00:00
# TEST: Following songs are put inside a queue
2021-09-25 16:05:46 +00:00
ctx.voice_client.is_playing.return_value = True
2022-04-14 23:22:45 +00:00
url = 'https://www.youtube.com/watch?v=pMDcYX2wRSg'
title = 'Three Days Grace - Time of Dying (lyrics)'
mock_ytdl_extract_info(ytdl, title=title, webpage_url=url, audio_url=url)
2021-09-25 16:05:46 +00:00
time_of_dying_audio = mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio)
# pylint: disable=too-many-function-args
2022-04-14 23:22:45 +00:00
query = 'three days grace time of dying'
2021-09-25 16:05:46 +00:00
await mbot.play(mbot, ctx, query=query)
assert \
2022-04-14 23:22:45 +00:00
ytdl.extract_info.call_args.args == (query,) and ytdl.extract_info.call_args.kwargs == {'download': False}, \
f'ytdl.extract_info was not called with {query}, {{ download: False }}'
2021-09-25 16:05:46 +00:00
assert \
not ffmpeg_pcm_audio.call_args.args == (url,), \
2022-04-14 23:22:45 +00:00
f'FFmpegPCMAudio was immediately called with {url} instead of being queued'
2021-09-25 16:05:46 +00:00
assert \
not ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \
2022-04-14 23:22:45 +00:00
'Did immediately playback audio instead of being queued'
content = ctx.send.call_args.kwargs['content']
assert content == 'Queued:', "Did not send 'Queued:' message"
2022-04-14 23:22:45 +00:00
embed = ctx.send.call_args.kwargs['embed']
assert embed.title == title, "Did not send 'Queued:' embed"
2021-09-25 16:05:46 +00:00
await asyncio.sleep(0)
2021-09-25 18:59:02 +00:00
# Assert that there is still no playback because previous song is not finished yet
2021-09-25 16:05:46 +00:00
assert \
not ffmpeg_pcm_audio.call_args.args == (url,), \
2022-04-14 23:22:45 +00:00
f'FFmpegPCMAudio was called with {url} before previous song finished'
2021-09-25 18:59:02 +00:00
# Execute callback for song finish event
2022-04-14 23:22:45 +00:00
ctx.voice_client.play.call_args.kwargs['after'](None)
2021-09-25 01:00:20 +00:00
await asyncio.sleep(0)
2021-09-25 16:05:46 +00:00
assert \
2021-09-25 18:41:33 +00:00
ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \
2022-04-14 23:22:45 +00:00
'Did not queue next song'
2021-09-25 18:58:09 +00:00
@pytest.mark.asyncio
async def test_bot_skip(mbot, ctx):
with patch.object(mbot, '_ytdl') as ytdl:
with patch('discord.FFmpegPCMAudio') as ffmpeg_pcm_audio:
2021-09-25 18:59:02 +00:00
# TEST: One can skip songs to immediately play the next song in queue
# Queue first song
2021-09-25 18:58:09 +00:00
ctx.voice_client.is_playing.return_value = False
2022-04-14 23:22:45 +00:00
url = 'https://www.youtube.com/watch?v=Wr9LZ1hAFpQ'
title = 'In Flames - Deliver Us (Official Video)'
mock_ytdl_extract_info(ytdl, title=title, webpage_url=url, audio_url=url)
2021-09-25 18:58:09 +00:00
deliver_us_audio = mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio)
query = 'in flames deliver us'
# pylint: disable=too-many-function-args
await mbot.play(mbot, ctx, query=query)
await asyncio.sleep(0)
assert \
ctx.voice_client.play.call_args.args == (deliver_us_audio,), \
2022-04-14 23:22:45 +00:00
'Did not playback correct audio'
2021-09-25 18:58:09 +00:00
2021-09-25 18:59:02 +00:00
# Queue second song
2021-09-25 18:58:09 +00:00
ctx.voice_client.is_playing.return_value = True
2022-04-14 23:22:45 +00:00
url = 'https://www.youtube.com/watch?v=pMDcYX2wRSg'
title = 'Three Days Grace - Time of Dying (lyrics)'
mock_ytdl_extract_info(ytdl, title=title, webpage_url=url, audio_url=url)
2021-09-25 18:58:09 +00:00
time_of_dying_audio = mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio)
# pylint: disable=too-many-function-args
2022-04-14 23:22:45 +00:00
query = 'three days grace time of dying'
2021-09-25 18:58:09 +00:00
await mbot.play(mbot, ctx, query=query)
await asyncio.sleep(0)
assert \
not ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \
2022-04-14 23:22:45 +00:00
'Did immediately playback audio instead of being queued'
2021-09-25 18:58:09 +00:00
2021-09-25 18:59:02 +00:00
# Now skip first song
2021-09-25 18:58:09 +00:00
await mbot.skip(mbot, ctx)
await asyncio.sleep(0)
assert \
ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \
2022-04-14 23:22:45 +00:00
'Did not skip song'
2021-09-25 18:58:09 +00:00
# TEST: Error if no song playing
ctx.voice_client.is_playing.return_value = False
with pytest.raises(commands.CommandError):
await mbot.skip(mbot, ctx)
# TEST: Error if no voice client
ctx.voice_client = None
with pytest.raises(commands.CommandError):
await mbot.skip(mbot, ctx)