import pytest from unittest.mock import AsyncMock, patch, MagicMock import os from tests.conftest import MockApplicationContext @pytest.fixture def play_commands_cog(bot): with patch('cogs.utils.temp.get_temp_dir', return_value="/tmp"): from cogs.music.play_commands import PlayCommands cog = PlayCommands(bot) return cog @pytest.fixture def loop_cog(bot): from cogs.music.loop import Loop cog = Loop(bot) return cog @pytest.mark.asyncio async def test_create_search_embed_empty(play_commands_cog): embed, view = await play_commands_cog._create_search_embed([]) assert embed.title == "Wyniki wyszukiwania" assert len(view.children) == 0 @pytest.mark.asyncio async def test_create_search_embed_with_candidates(play_commands_cog): candidates = [ "https://example.com/test1.mp3", "https://example.com/test2.mp3" ] embed, view = await play_commands_cog._create_search_embed(candidates) assert embed.title == "Wyniki wyszukiwania" assert len(embed.fields) == 2 @pytest.mark.asyncio async def test_play_command_no_file_or_search(play_commands_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await play_commands_cog.play.callback(play_commands_cog, ctx, melodia=None, file=None, compressed=False) mock_respond.assert_called_once() args = mock_respond.call_args assert "Musisz podać" in str(args) @pytest.mark.asyncio async def test_play_command_both_file_and_search(play_commands_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_file = MagicMock() mock_file.filename = "test.mp3" with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await play_commands_cog.play.callback(play_commands_cog, ctx, melodia="test", file=mock_file, compressed=False) mock_respond.assert_called_once() args = mock_respond.call_args assert "nie oba" in str(args) or "LUB" in str(args) def test_loop_cog_is_looping_initial(loop_cog): assert loop_cog.is_looping(123) == False def test_loop_cog_set_looping(loop_cog): loop_cog.set_looping(123, True) assert loop_cog.is_looping(123) == True @pytest.mark.asyncio async def test_loop_command_toggle_on(loop_cog, mock_channel, mock_guild, bot, mock_user): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) ctx.user = mock_user ctx.author = mock_user ctx.guild.id = 123 with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await loop_cog.loop.callback(loop_cog, ctx) mock_respond.assert_called_once() args = mock_respond.call_args assert "włączony" in str(args) @pytest.mark.asyncio async def test_loop_command_toggle_off(loop_cog, mock_channel, mock_guild, bot, mock_user): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) ctx.user = mock_user ctx.author = mock_user ctx.guild.id = 456 loop_cog.set_looping(456, True) with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await loop_cog.loop.callback(loop_cog, ctx) mock_respond.assert_called_once() args = mock_respond.call_args assert "wyłączony" in str(args)