104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
from bot.cogs.cog_manager import BaseCog
|
|
import discord
|
|
import asyncio
|
|
import traceback
|
|
from unittest.mock import patch
|
|
|
|
class TestCog(BaseCog):
|
|
def __init__(self, bot):
|
|
super().__init__(bot)
|
|
|
|
async def cmd_test(self, message):
|
|
"""
|
|
Run tests to verify bot functionality
|
|
Usage: .test
|
|
"""
|
|
await message.edit(content="🧪 Running tests...")
|
|
|
|
results = []
|
|
total = 0
|
|
passed = 0
|
|
|
|
# Test AFK feature
|
|
total += 1
|
|
try:
|
|
# Check if AFK cog is loaded
|
|
afk_cog = next((cog for cog in self.bot.cog_manager.cogs.values()
|
|
if hasattr(cog, "afk")), None)
|
|
|
|
if afk_cog:
|
|
original_afk = afk_cog.afk
|
|
afk_cog.afk = False
|
|
|
|
# Create a test message
|
|
test_msg = await message.channel.send(".afk Test AFK")
|
|
|
|
# Run the AFK command
|
|
await self.bot.message_handler.handle_commands(test_msg)
|
|
|
|
# Check if AFK was set correctly
|
|
if afk_cog.afk and afk_cog.afk_reason == "Test AFK":
|
|
results.append("✅ AFK test passed")
|
|
passed += 1
|
|
else:
|
|
results.append("❌ AFK test failed")
|
|
|
|
# Clean up
|
|
await test_msg.delete()
|
|
afk_cog.afk = original_afk
|
|
else:
|
|
results.append("⚠️ AFK test skipped - cog not loaded")
|
|
except Exception as e:
|
|
results.append(f"❌ AFK test error: {str(e)}")
|
|
|
|
# Test message detection
|
|
total += 1
|
|
try:
|
|
reply_received = False
|
|
|
|
# Create a message that should trigger a reply
|
|
test_msg = await message.channel.send("This is a test message")
|
|
|
|
# Mock the reply method
|
|
original_reply = test_msg.reply
|
|
|
|
async def test_reply(content, **kwargs):
|
|
nonlocal reply_received
|
|
if content == "test success":
|
|
reply_received = True
|
|
return await original_reply(content, **kwargs)
|
|
|
|
# Patch the reply method
|
|
with patch.object(test_msg, 'reply', side_effect=test_reply):
|
|
# Create a handler specifically for this test
|
|
test_handler = lambda m: asyncio.ensure_future(test_msg.reply("test success"))
|
|
|
|
# Add the handler
|
|
self.bot.add_listener(test_handler, name='on_message')
|
|
|
|
# Trigger it
|
|
await self.bot.message_handler.handle_message(test_msg)
|
|
|
|
# Wait a moment
|
|
await asyncio.sleep(1)
|
|
|
|
# Remove the handler
|
|
self.bot.remove_listener(test_handler, name='on_message')
|
|
|
|
if reply_received:
|
|
results.append("✅ Message handler test passed")
|
|
passed += 1
|
|
else:
|
|
results.append("❌ Message handler test failed")
|
|
|
|
# Clean up
|
|
await test_msg.delete()
|
|
|
|
except Exception as e:
|
|
results.append(f"❌ Message handler test error: {traceback.format_exc()}")
|
|
|
|
# Show results
|
|
result_text = "\n".join(results)
|
|
summary = f"**Test Results:** {passed}/{total} passed"
|
|
|
|
await message.edit(content=f"{summary}\n\n{result_text}") |