import pytest from unittest.mock import AsyncMock, patch, MagicMock from cogs.web import Web from conftest import MockApplicationContext @pytest.fixture def web_cog(bot): cog = Web(bot) return cog @pytest.mark.asyncio async def test_web_command_success(web_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = ''' Test Result Test Result 2 ''' with patch('cogs.web.requests.get', return_value=mock_response) as mock_get: with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await web_cog.web.callback(web_cog, ctx, query="test query") mock_get.assert_called_once() mock_respond.assert_called_once() @pytest.mark.asyncio async def test_web_command_no_results(web_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = '' with patch('cogs.web.requests.get', return_value=mock_response): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await web_cog.web.callback(web_cog, ctx, query="test") mock_respond.assert_called_once_with(content="No results found.", ephemeral=True) @pytest.mark.asyncio async def test_web_command_http_error(web_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) mock_response = MagicMock() mock_response.status_code = 500 with patch('cogs.web.requests.get', return_value=mock_response): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await web_cog.web.callback(web_cog, ctx, query="test") mock_respond.assert_called_once_with(content="Error: 500", ephemeral=True)