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")