aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_ai.py
blob: a1c3fd4fa447095f5165a162dc40320dd2aca97a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
import discord
import httpx
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_client = MagicMock()
    mock_client.post = AsyncMock(return_value=mock_response)
    mock_client.__aenter__ = AsyncMock(return_value=mock_response)
    mock_client.__aexit__ = AsyncMock(return_value=None)

    with patch('cogs.ai.httpx.AsyncClient') as mock_httpx:
        instance = mock_httpx.return_value.__aenter__.return_value
        instance.post = AsyncMock(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_client = MagicMock()
    mock_client.post = AsyncMock(return_value=mock_response)
    mock_client.__aenter__ = AsyncMock(return_value=mock_response)
    mock_client.__aexit__ = AsyncMock(return_value=None)

    with patch('cogs.ai.httpx.AsyncClient') as mock_httpx:
        instance = mock_httpx.return_value.__aenter__.return_value
        instance.post = AsyncMock(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={})

    with patch('cogs.ai.httpx.AsyncClient') as mock_httpx:
        instance = mock_httpx.return_value.__aenter__.return_value
        instance.post = AsyncMock(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