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_tts.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/cogs/test_tts.py (limited to 'tests/cogs/test_tts.py') diff --git a/tests/cogs/test_tts.py b/tests/cogs/test_tts.py new file mode 100644 index 0000000..8c95670 --- /dev/null +++ b/tests/cogs/test_tts.py @@ -0,0 +1,58 @@ +import pytest +from unittest.mock import AsyncMock, patch, MagicMock +import discord +from tests.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 + + mock_temp_path = "/tmp/test_tts.mp3" + + with patch('cogs.tts.gTTS') as mock_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="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) \ No newline at end of file -- cgit v1.2.3