aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs
diff options
context:
space:
mode:
authorsalmonik <salmonik@noreply.codeberg.org>2026-06-03 14:00:03 +0200
committersalmonik <salmonik@noreply.codeberg.org>2026-06-03 14:00:03 +0200
commitc027964952bd0d9700a9c83f72fb23bd5bffad3e (patch)
tree1a7f31b08539288eab7fb58bd0ce3b14b2bb0194 /tests/cogs
parentMerge pull request 'Voice related fixes, alongside simplifying assistant' (#5... (diff)
parentfeat: add local API support for OpenAI compatible APIs (diff)
downloadsenator-sperma-c027964952bd0d9700a9c83f72fb23bd5bffad3e.tar.gz
senator-sperma-c027964952bd0d9700a9c83f72fb23bd5bffad3e.zip
Merge pull request 'feat: add local API support for OpenAI compatible APIs' (#6) from dev into main
Reviewed-on: https://codeberg.org/salmonik/senator-sperma/pulls/6
Diffstat (limited to '')
-rw-r--r--tests/cogs/test_ai.py60
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 @@
1import pytest 1import pytest
2from unittest.mock import AsyncMock, patch, MagicMock 2from unittest.mock import AsyncMock, patch, MagicMock
3import discord
4import httpx
5from conftest import MockApplicationContext 3from 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
16def 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
29async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): 14async 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
49async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): 25async 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
69async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): 36async 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