aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_search.py
blob: c9c7d0e94b1fe93df4d6207416dfcda3058a1897 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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