aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_music_play.py
blob: 76847294f120dda7544929a52a8082a5421e477f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
import os
from conftest import MockApplicationContext


@pytest.fixture
def play_commands_cog(bot):
    with patch('cogs.utils.temp.get_temp_dir', return_value="/tmp"):
        from cogs.music.play_commands import PlayCommands
        cog = PlayCommands(bot)
    return cog


@pytest.fixture
def loop_cog(bot):
    from cogs.music.loop import Loop
    cog = Loop(bot)
    return cog


@pytest.mark.asyncio
async def test_create_search_embed_empty(play_commands_cog):
    embed, view = await play_commands_cog._create_search_embed([])

    assert embed.title == "Wyniki wyszukiwania"
    assert len(view.children) == 0


@pytest.mark.asyncio
async def test_create_search_embed_with_candidates(play_commands_cog):
    candidates = [
        "https://example.com/test1.mp3",
        "https://example.com/test2.mp3"
    ]

    embed, view = await play_commands_cog._create_search_embed(candidates)

    assert embed.title == "Wyniki wyszukiwania"
    assert len(embed.fields) == 2


@pytest.mark.asyncio
async def test_play_command_no_file_or_search(play_commands_cog, mock_channel, mock_guild, bot):
    ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)

    with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
        await play_commands_cog.play.callback(play_commands_cog, ctx, melodia=None, file=None, compressed=False)
        mock_respond.assert_called_once()
        args = mock_respond.call_args
        assert "Musisz podać" in str(args)


@pytest.mark.asyncio
async def test_play_command_both_file_and_search(play_commands_cog, mock_channel, mock_guild, bot):
    ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)

    mock_file = MagicMock()
    mock_file.filename = "test.mp3"

    with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
        await play_commands_cog.play.callback(play_commands_cog, ctx, melodia="test", file=mock_file, compressed=False)
        mock_respond.assert_called_once()
        args = mock_respond.call_args
        assert "nie oba" in str(args) or "LUB" in str(args)


def test_loop_cog_is_looping_initial(loop_cog):
    assert loop_cog.is_looping(123) == False


def test_loop_cog_set_looping(loop_cog):
    loop_cog.set_looping(123, True)
    assert loop_cog.is_looping(123) == True


@pytest.mark.asyncio
async def test_loop_command_toggle_on(loop_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
    ctx.guild.id = 123

    with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
        await loop_cog.loop.callback(loop_cog, ctx)
        mock_respond.assert_called_once()
        args = mock_respond.call_args
        assert "włączony" in str(args)


@pytest.mark.asyncio
async def test_loop_command_toggle_off(loop_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
    ctx.guild.id = 456

    loop_cog.set_looping(456, True)

    with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
        await loop_cog.loop.callback(loop_cog, ctx)
        mock_respond.assert_called_once()
        args = mock_respond.call_args
        assert "wyłączony" in str(args)