diff options
| author | salmonik <salmonik@noreply.codeberg.org> | 2026-04-19 18:10:09 +0200 |
|---|---|---|
| committer | salmonik <salmonik@noreply.codeberg.org> | 2026-04-19 18:10:09 +0200 |
| commit | f8e1cf36e1efa0602bf2e450835f2a67654762a6 (patch) | |
| tree | fe6b12226d7eebe2a0dc6525de3414d0c3718cc8 /tests/cogs/test_ai.py | |
| parent | add support for x^2+y^2=0,4 and alike (diff) | |
| parent | fix: proper aiohttp async context manager mocking in test (diff) | |
| download | senator-sperma-f8e1cf36e1efa0602bf2e450835f2a67654762a6.tar.gz senator-sperma-f8e1cf36e1efa0602bf2e450835f2a67654762a6.zip | |
Merge pull request 'massive changes' (#3) from dev into main
Reviewed-on: https://codeberg.org/salmonik/senator-sperma/pulls/3
stable enough
Diffstat (limited to 'tests/cogs/test_ai.py')
| -rw-r--r-- | tests/cogs/test_ai.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/cogs/test_ai.py b/tests/cogs/test_ai.py new file mode 100644 index 0000000..a1c3fd4 --- /dev/null +++ b/tests/cogs/test_ai.py | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | import pytest | ||
| 2 | from unittest.mock import AsyncMock, patch, MagicMock | ||
| 3 | import discord | ||
| 4 | import httpx | ||
| 5 | from tests.conftest import MockApplicationContext | ||
| 6 | |||
| 7 | |||
| 8 | @pytest.fixture | ||
| 9 | def ai_cog(bot): | ||
| 10 | from cogs.ai import AI | ||
| 11 | cog = AI(bot) | ||
| 12 | return cog | ||
| 13 | |||
| 14 | |||
| 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 | ||
| 29 | async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): | ||
| 30 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 31 | |||
| 32 | mock_response = MagicMock() | ||
| 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: | ||
| 44 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | ||
| 45 | mock_followup.assert_called_once_with("Test response from AI") | ||
| 46 | |||
| 47 | |||
| 48 | @pytest.mark.asyncio | ||
| 49 | 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) | ||
| 51 | |||
| 52 | mock_response = MagicMock() | ||
| 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: | ||
| 64 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | ||
| 65 | mock_followup.assert_called_once_with("bot siem zesroĊ") | ||
| 66 | |||
| 67 | |||
| 68 | @pytest.mark.asyncio | ||
| 69 | 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) | ||
| 71 | |||
| 72 | mock_response = MagicMock() | ||
| 73 | mock_response.json = MagicMock(return_value={}) | ||
| 74 | |||
| 75 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | ||
| 76 | instance = mock_httpx.return_value.__aenter__.return_value | ||
| 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 | ||
