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_ai.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/cogs/test_ai.py (limited to 'tests/cogs/test_ai.py') diff --git a/tests/cogs/test_ai.py b/tests/cogs/test_ai.py new file mode 100644 index 0000000..bd8f813 --- /dev/null +++ b/tests/cogs/test_ai.py @@ -0,0 +1,69 @@ +import pytest +from unittest.mock import AsyncMock, patch, MagicMock +import discord +from tests.conftest import MockApplicationContext + + +@pytest.fixture +def ai_cog(bot): + from cogs.ai import AI + cog = AI(bot) + return cog + + +@pytest.fixture +def mock_ai_response(): + return { + "choices": [ + { + "message": { + "content": "Test response from AI" + } + } + ] + } + + +@pytest.mark.asyncio +async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_response = MagicMock() + mock_response.json = MagicMock(return_value=mock_ai_response) + mock_response.status_code = 200 + + with patch('cogs.ai.requests.post', return_value=mock_response): + with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: + await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) + mock_followup.assert_called_once_with("Test response from AI") + + +@pytest.mark.asyncio +async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_response = MagicMock() + mock_response.json = MagicMock(return_value={"choices": []}) + mock_response.status_code = 200 + + with patch('cogs.ai.requests.post', return_value=mock_response): + with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: + await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) + mock_followup.assert_called_once_with("bot siem zesroĊ‚") + + +@pytest.mark.asyncio +async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_response = MagicMock() + mock_response.json = MagicMock(return_value={}) + mock_response.status_code = 200 + + with patch('cogs.ai.requests.post', return_value=mock_response): + with patch('cogs.ai.logger') as mock_logger: + try: + await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) + except KeyError: + pass + assert mock_logger.info.called \ No newline at end of file -- cgit v1.2.3