diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/cogs/test_trigger_integration.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/cogs/test_trigger_integration.py b/tests/cogs/test_trigger_integration.py new file mode 100644 index 0000000..4828387 --- /dev/null +++ b/tests/cogs/test_trigger_integration.py | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | import pytest | ||
| 2 | from unittest.mock import MagicMock, AsyncMock, patch | ||
| 3 | from cogs.trigger import Trigger | ||
| 4 | |||
| 5 | |||
| 6 | @pytest.fixture | ||
| 7 | def trigger_cog(bot): | ||
| 8 | from cogs.utils.database import init_database | ||
| 9 | init_database() | ||
| 10 | from cogs.trigger import Trigger | ||
| 11 | cog = Trigger(bot) | ||
| 12 | return cog | ||
| 13 | |||
| 14 | |||
| 15 | @pytest.mark.slow | ||
| 16 | @pytest.mark.asyncio | ||
| 17 | async def test_trigger_response_flow(trigger_cog): | ||
| 18 | """Test trigger creation and response in a simulated message flow.""" | ||
| 19 | cog = trigger_cog | ||
| 20 | |||
| 21 | ctx = MagicMock() | ||
| 22 | ctx.author = MagicMock() | ||
| 23 | ctx.author.id = 123 | ||
| 24 | ctx.guild = MagicMock() | ||
| 25 | ctx.guild.id = 456 | ||
| 26 | ctx.respond = AsyncMock() | ||
| 27 | |||
| 28 | # Create a trigger using the create command | ||
| 29 | await cog.create.callback(cog, ctx, name="test_trigger") | ||
| 30 | |||
| 31 | # Add a keyword and response | ||
| 32 | await cog.add.callback(cog, ctx, name="test_trigger", trigger="hello", response="world") | ||
| 33 | |||
| 34 | # Verify trigger was created - check DB | ||
| 35 | from cogs.utils.database import get_connection | ||
| 36 | with get_connection() as conn: | ||
| 37 | c = conn.cursor() | ||
| 38 | c.execute("SELECT id FROM triggers WHERE name=?", ("test_trigger",)) | ||
| 39 | result = c.fetchone() | ||
| 40 | assert result is not None | ||
| 41 | |||
| 42 | # Simulate a message that should trigger | ||
| 43 | message = MagicMock() | ||
| 44 | message.author = ctx.author | ||
| 45 | message.author.bot = False | ||
| 46 | message.guild = ctx.guild | ||
| 47 | message.content = "say hello to me" | ||
| 48 | message.channel.send = AsyncMock() # Make send awaitable | ||
| 49 | |||
| 50 | # Check if trigger matches using the static method | ||
| 51 | await Trigger.trigger(message) | ||
| 52 | |||
| 53 | # Verify message.channel.send was called with the response | ||
| 54 | message.channel.send.assert_called_once_with("world") | ||
