diff options
Diffstat (limited to 'tests/cogs/test_ai.py')
| -rw-r--r-- | tests/cogs/test_ai.py | 69 |
1 files changed, 69 insertions, 0 deletions
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 @@ | |||
| 1 | import pytest | ||
| 2 | from unittest.mock import AsyncMock, patch, MagicMock | ||
| 3 | import discord | ||
| 4 | from tests.conftest import MockApplicationContext | ||
| 5 | |||
| 6 | |||
| 7 | @pytest.fixture | ||
| 8 | def ai_cog(bot): | ||
| 9 | from cogs.ai import AI | ||
| 10 | cog = AI(bot) | ||
| 11 | return cog | ||
| 12 | |||
| 13 | |||
| 14 | @pytest.fixture | ||
| 15 | def mock_ai_response(): | ||
| 16 | return { | ||
| 17 | "choices": [ | ||
| 18 | { | ||
| 19 | "message": { | ||
| 20 | "content": "Test response from AI" | ||
| 21 | } | ||
| 22 | } | ||
| 23 | ] | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | @pytest.mark.asyncio | ||
| 28 | async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): | ||
| 29 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 30 | |||
| 31 | mock_response = MagicMock() | ||
| 32 | mock_response.json = MagicMock(return_value=mock_ai_response) | ||
| 33 | mock_response.status_code = 200 | ||
| 34 | |||
| 35 | with patch('cogs.ai.requests.post', return_value=mock_response): | ||
| 36 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: | ||
| 37 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | ||
| 38 | mock_followup.assert_called_once_with("Test response from AI") | ||
| 39 | |||
| 40 | |||
| 41 | @pytest.mark.asyncio | ||
| 42 | async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): | ||
| 43 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 44 | |||
| 45 | mock_response = MagicMock() | ||
| 46 | mock_response.json = MagicMock(return_value={"choices": []}) | ||
| 47 | mock_response.status_code = 200 | ||
| 48 | |||
| 49 | with patch('cogs.ai.requests.post', return_value=mock_response): | ||
| 50 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: | ||
| 51 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | ||
| 52 | mock_followup.assert_called_once_with("bot siem zesroĊ") | ||
| 53 | |||
| 54 | |||
| 55 | @pytest.mark.asyncio | ||
| 56 | async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): | ||
| 57 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 58 | |||
| 59 | mock_response = MagicMock() | ||
| 60 | mock_response.json = MagicMock(return_value={}) | ||
| 61 | mock_response.status_code = 200 | ||
| 62 | |||
| 63 | with patch('cogs.ai.requests.post', return_value=mock_response): | ||
| 64 | with patch('cogs.ai.logger') as mock_logger: | ||
| 65 | try: | ||
| 66 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | ||
| 67 | except KeyError: | ||
| 68 | pass | ||
| 69 | assert mock_logger.info.called \ No newline at end of file | ||
