diff options
Diffstat (limited to 'tests/cogs')
| -rw-r--r-- | tests/cogs/test_ai.py | 60 |
1 files changed, 10 insertions, 50 deletions
diff --git a/tests/cogs/test_ai.py b/tests/cogs/test_ai.py index 6d92f55..aab9861 100644 --- a/tests/cogs/test_ai.py +++ b/tests/cogs/test_ai.py | |||
| @@ -1,7 +1,5 @@ | |||
| 1 | import pytest | 1 | import pytest |
| 2 | from unittest.mock import AsyncMock, patch, MagicMock | 2 | from unittest.mock import AsyncMock, patch, MagicMock |
| 3 | import discord | ||
| 4 | import httpx | ||
| 5 | from conftest import MockApplicationContext | 3 | from conftest import MockApplicationContext |
| 6 | 4 | ||
| 7 | 5 | ||
| @@ -12,36 +10,14 @@ def ai_cog(bot): | |||
| 12 | return cog | 10 | return cog |
| 13 | 11 | ||
| 14 | 12 | ||
| 15 | @pytest.fixture | ||
| 16 | def mock_ai_response(): | ||
| 17 | return { | ||
| 18 | "choices": [ | ||
| 19 | { | ||
| 20 | "message": { | ||
| 21 | "content": "Test response from AI" | ||
| 22 | } | ||
| 23 | } | ||
| 24 | ] | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | @pytest.mark.asyncio | 13 | @pytest.mark.asyncio |
| 29 | async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): | 14 | async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot): |
| 30 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | 15 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) |
| 31 | 16 | ||
| 32 | mock_response = MagicMock() | 17 | with patch('cogs.ai.llm_completion', new_callable=AsyncMock, return_value="Test response from AI") as mock_llm: |
| 33 | mock_response.json = MagicMock(return_value=mock_ai_response) | ||
| 34 | |||
| 35 | mock_client = MagicMock() | ||
| 36 | mock_client.post = AsyncMock(return_value=mock_response) | ||
| 37 | mock_client.__aenter__ = AsyncMock(return_value=mock_response) | ||
| 38 | mock_client.__aexit__ = AsyncMock(return_value=None) | ||
| 39 | |||
| 40 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | ||
| 41 | instance = mock_httpx.return_value.__aenter__.return_value | ||
| 42 | instance.post = AsyncMock(return_value=mock_response) | ||
| 43 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: | 18 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: |
| 44 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | 19 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) |
| 20 | mock_llm.assert_awaited_once() | ||
| 45 | mock_followup.assert_called_once_with("Test response from AI") | 21 | mock_followup.assert_called_once_with("Test response from AI") |
| 46 | 22 | ||
| 47 | 23 | ||
| @@ -49,19 +25,10 @@ async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mo | |||
| 49 | async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): | 25 | async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): |
| 50 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | 26 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) |
| 51 | 27 | ||
| 52 | mock_response = MagicMock() | 28 | with patch('cogs.ai.llm_completion', new_callable=AsyncMock, return_value=None) as mock_llm: |
| 53 | mock_response.json = MagicMock(return_value={"choices": []}) | ||
| 54 | |||
| 55 | mock_client = MagicMock() | ||
| 56 | mock_client.post = AsyncMock(return_value=mock_response) | ||
| 57 | mock_client.__aenter__ = AsyncMock(return_value=mock_response) | ||
| 58 | mock_client.__aexit__ = AsyncMock(return_value=None) | ||
| 59 | |||
| 60 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | ||
| 61 | instance = mock_httpx.return_value.__aenter__.return_value | ||
| 62 | instance.post = AsyncMock(return_value=mock_response) | ||
| 63 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: | 29 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: |
| 64 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | 30 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) |
| 31 | mock_llm.assert_awaited_once() | ||
| 65 | mock_followup.assert_called_once_with("bot siem zesroł") | 32 | mock_followup.assert_called_once_with("bot siem zesroł") |
| 66 | 33 | ||
| 67 | 34 | ||
| @@ -69,15 +36,8 @@ async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, b | |||
| 69 | async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): | 36 | async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): |
| 70 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | 37 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) |
| 71 | 38 | ||
| 72 | mock_response = MagicMock() | 39 | with patch('cogs.ai.llm_completion', new_callable=AsyncMock, return_value=None) as mock_llm: |
| 73 | mock_response.json = MagicMock(return_value={}) | 40 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: |
| 74 | 41 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | |
| 75 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | 42 | mock_llm.assert_awaited_once() |
| 76 | instance = mock_httpx.return_value.__aenter__.return_value | 43 | mock_followup.assert_called_once_with("bot siem zesroł") |
| 77 | instance.post = AsyncMock(return_value=mock_response) | ||
| 78 | with patch('cogs.ai.logger') as mock_logger: | ||
| 79 | try: | ||
| 80 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | ||
| 81 | except KeyError: | ||
| 82 | pass | ||
| 83 | assert mock_logger.info.called | ||
