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
|
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
import os
from conftest import MockApplicationContext
@pytest.mark.asyncio
async def test_validate_voice_channel_not_in_voice():
from cogs.music.audio_playback import AudioPlaybackManager
mock_ctx = MagicMock()
mock_ctx.author.voice = None
mock_ctx.respond = AsyncMock()
result = await AudioPlaybackManager.validate_voice_channel(mock_ctx)
assert result is None
mock_ctx.respond.assert_called_once()
@pytest.mark.asyncio
async def test_validate_voice_channel_in_voice():
from cogs.music.audio_playback import AudioPlaybackManager
mock_channel = MagicMock()
mock_ctx = MagicMock()
mock_ctx.author.voice = MagicMock()
mock_ctx.author.voice.channel = mock_channel
mock_ctx.respond = AsyncMock()
result = await AudioPlaybackManager.validate_voice_channel(mock_ctx)
assert result == mock_channel
@pytest.mark.asyncio
async def test_prepare_file_source_file_not_found():
from cogs.music.audio_playback import AudioPlaybackManager
display_name, absolute_path = await AudioPlaybackManager.prepare_file_source("/nonexistent/file.mp3")
assert display_name is None
assert absolute_path is None
@pytest.mark.asyncio
async def test_prepare_file_source_absolute_path():
from cogs.music.audio_playback import AudioPlaybackManager
with patch('os.path.exists', return_value=True):
with patch('os.path.isabs', return_value=True):
display_name, absolute_path = await AudioPlaybackManager.prepare_file_source("/tmp/test.mp3")
assert display_name == "test"
assert absolute_path == "/tmp/test.mp3"
@pytest.mark.asyncio
async def test_prepare_source_url():
from cogs.music.audio_playback import AudioPlaybackManager
source, _, is_url = await AudioPlaybackManager.prepare_source(MagicMock(), "https://example.com/stream")
assert is_url == True
assert source == "https://example.com/stream"
@pytest.mark.asyncio
async def test_prepare_source_local_file():
from cogs.music.audio_playback import AudioPlaybackManager
with patch('os.path.exists', return_value=True):
source, display_name, is_url = await AudioPlaybackManager.prepare_source(MagicMock(), "/tmp/test.mp3")
assert is_url == False
assert display_name == "test"
@pytest.mark.asyncio
async def test_cleanup_temp_file():
from cogs.music.audio_playback import AudioPlaybackManager
with patch('os.path.exists', return_value=True):
with patch('os.remove'):
await AudioPlaybackManager.cleanup_temp_file("/tmp/test.mp3")
@pytest.mark.asyncio
async def test_cleanup_temp_file_not_exists():
from cogs.music.audio_playback import AudioPlaybackManager
with patch('os.path.exists', return_value=False):
await AudioPlaybackManager.cleanup_temp_file("/nonexistent/file.mp3")
|