import pytest from unittest.mock import AsyncMock, patch, MagicMock import discord from conftest import MockApplicationContext, MockMessage @pytest.fixture def downloader_cog(bot): from cogs.downloader import Downloader cog = Downloader(bot) return cog @pytest.mark.asyncio async def test_download_command_success_file(downloader_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_file_path = "/tmp/test_video.mp4" with patch('cogs.downloader.Downloader.download_standalone', return_value={ "status": "success", "type": "file", "content": mock_file_path, "filename": "test_video.mp4" }): with patch('discord.File', return_value=MagicMock()) as mock_file: with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await downloader_cog.download.callback(downloader_cog, ctx, url="https://example.com/video", catbox=False, fformat="mp4", filename_user=None) mock_respond.assert_called_once() call_args = mock_respond.call_args assert 'file' in str(call_args) or mock_file.called @pytest.mark.asyncio async def test_download_command_success_url(downloader_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) with patch('cogs.downloader.Downloader.download_standalone', return_value={ "status": "success", "type": "url", "content": "https://catbox.2137697.xyz/sperma/test.mp4" }): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await downloader_cog.download.callback(downloader_cog, ctx, url="https://example.com/video", catbox=True, fformat="mp4", filename_user=None) mock_respond.assert_called_once() call_args = mock_respond.call_args assert "pobioron" in str(call_args) @pytest.mark.asyncio async def test_download_command_error(downloader_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) with patch('cogs.downloader.Downloader.download_standalone', return_value={ "status": "error", "type": "error", "content": "Download failed", "details": "Connection timeout" }): with patch('cogs.downloader.logger') as mock_logger: await downloader_cog.download.callback(downloader_cog, ctx, url="https://badurl.com", catbox=False, fformat="mp4", filename_user=None) assert mock_logger.error.called @pytest.mark.asyncio async def test_message_download_with_url(downloader_cog, mock_channel, mock_guild, bot, mock_user): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_message = MagicMock() mock_message.content = "Check this out https://example.com/video" with patch('cogs.downloader.Downloader.download_standalone', return_value={ "status": "success", "type": "file", "content": "/tmp/test.mp4" }): with patch.object(downloader_cog, 'download', new_callable=AsyncMock) as mock_download: await downloader_cog.message_download.callback(downloader_cog, ctx, message=mock_message) mock_download.assert_called_once() @pytest.mark.asyncio async def test_message_download_no_url(downloader_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_message = MagicMock() mock_message.content = "No URL here" with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await downloader_cog.message_download.callback(downloader_cog, ctx, message=mock_message) mock_respond.assert_called_once() call_args = mock_respond.call_args assert "Brak" in str(call_args)