import pytest from unittest.mock import AsyncMock, patch, MagicMock import discord from conftest import MockApplicationContext @pytest.fixture def tts_cog(bot): from cogs.tts import TTS cog = TTS(bot) return cog @pytest.mark.asyncio async def test_tts_file_output(tts_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 "/tmp/test_tts.mp3" with patch('cogs.tts.gTTS') as _: with patch('os.path.exists', return_value=True): with patch('os.unlink'): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: with patch('cogs.tts.get_volume', return_value=1.0): await tts_cog.tts.callback(tts_cog, ctx, text="Hello", output="file") mock_respond.assert_called_once() @pytest.mark.asyncio async def test_tts_voice_output_not_in_channel(tts_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.author.voice = None with patch('cogs.tts.gTTS'): with patch('os.path.exists', return_value=True): with patch('os.unlink'): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: with patch('cogs.tts.get_volume', return_value=1.0): await tts_cog.tts.callback(tts_cog, ctx, text="Hello", output="vc") @pytest.mark.asyncio async def test_tts_error_handling(tts_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('cogs.tts.gTTS', side_effect=Exception("TTS Error")): with patch('os.path.exists', return_value=False): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await tts_cog.tts.callback(tts_cog, ctx, text="Hello", output="file") mock_respond.assert_called_once() args = mock_respond.call_args assert "Error" in str(args) or "eror" in str(args)