aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_trigger_integration.py
blob: 48283873b514ed19de1fde90caa03705c3ae5c0e (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
import pytest
from unittest.mock import MagicMock, AsyncMock, patch
from cogs.trigger import Trigger


@pytest.fixture
def trigger_cog(bot):
    from cogs.utils.database import init_database
    init_database()
    from cogs.trigger import Trigger
    cog = Trigger(bot)
    return cog


@pytest.mark.slow
@pytest.mark.asyncio
async def test_trigger_response_flow(trigger_cog):
    """Test trigger creation and response in a simulated message flow."""
    cog = trigger_cog

    ctx = MagicMock()
    ctx.author = MagicMock()
    ctx.author.id = 123
    ctx.guild = MagicMock()
    ctx.guild.id = 456
    ctx.respond = AsyncMock()

    # Create a trigger using the create command
    await cog.create.callback(cog, ctx, name="test_trigger")

    # Add a keyword and response
    await cog.add.callback(cog, ctx, name="test_trigger", trigger="hello", response="world")

    # Verify trigger was created - check DB
    from cogs.utils.database import get_connection
    with get_connection() as conn:
        c = conn.cursor()
        c.execute("SELECT id FROM triggers WHERE name=?", ("test_trigger",))
        result = c.fetchone()
        assert result is not None

    # Simulate a message that should trigger
    message = MagicMock()
    message.author = ctx.author
    message.author.bot = False
    message.guild = ctx.guild
    message.content = "say hello to me"
    message.channel.send = AsyncMock()  # Make send awaitable

    # Check if trigger matches using the static method
    await Trigger.trigger(message)

    # Verify message.channel.send was called with the response
    message.channel.send.assert_called_once_with("world")