From 857a7364ddb9b10529593c5e9c3578eb95c1da9b Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 18 Apr 2026 16:24:42 +0200 Subject: add a test suite Signed-off-by: salmonik --- tests/cogs/test_search.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/cogs/test_search.py (limited to 'tests/cogs/test_search.py') diff --git a/tests/cogs/test_search.py b/tests/cogs/test_search.py new file mode 100644 index 0000000..4fde494 --- /dev/null +++ b/tests/cogs/test_search.py @@ -0,0 +1,83 @@ +import pytest +from unittest.mock import AsyncMock, patch, MagicMock +import discord +from tests.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 \ No newline at end of file -- cgit v1.2.3