import pytest from unittest.mock import AsyncMock, patch, MagicMock import discord from conftest import MockApplicationContext @pytest.fixture def search_cog(bot): from cogs.search import Search cog = Search(bot) return cog @pytest.mark.asyncio async def test_search_command_success(search_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 search_cog._cached_results[mock_user.id] = { "testfile.mp4": "https://example.com/files/testfile.mp4" } with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: await search_cog.search.callback(search_cog, ctx, filename="testfile.mp4") mock_followup.assert_called_once_with("https://example.com/files/testfile.mp4") assert mock_user.id not in search_cog._cached_results @pytest.mark.asyncio async def test_search_command_no_cache(search_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 with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: await search_cog.search.callback(search_cog, ctx, filename="testfile.mp4") mock_followup.assert_called_once_with("Search results expired. Please search again.") @pytest.mark.asyncio async def test_search_command_file_not_found(search_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 search_cog._cached_results[mock_user.id] = { "testfile.mp4": "https://example.com/files/testfile.mp4" } with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: await search_cog.search.callback(search_cog, ctx, filename="nonexistent.mp4") mock_followup.assert_called_once_with("File 'nonexistent.mp4' not found.") @pytest.mark.asyncio async def test_search_command_empty_filename(search_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 with patch.object(ctx, 'reply', new_callable=AsyncMock) as mock_reply: await search_cog.search.callback(search_cog, ctx, filename="") mock_reply.assert_called_once_with("Please select a file from the autocomplete options.") @pytest.mark.asyncio async def test_file_autocomplete(search_cog, mock_channel, mock_guild, bot, mock_user): mock_autocomplete_ctx = MagicMock() mock_autocomplete_ctx.value = "test" mock_autocomplete_ctx.interaction = MagicMock() mock_autocomplete_ctx.interaction.user = mock_user with patch('cogs.utils.file_search.UniversalSearcher.search_copyparty_autocomplete', new_callable=AsyncMock) as mock_search: mock_search.return_value = [ "https://example.com/files/test1.mp4", "https://example.com/files/test2.mp4" ] choices = await search_cog.file_autocomplete(mock_autocomplete_ctx) assert len(choices) == 2 assert mock_user.id in search_cog._cached_results