feat: 添加测试覆盖率并修复相关问题
refactor(redis_manager): 移除冗余的ConnectionError处理 refactor(event_handler): 优化Bot类型注解 refactor(factory): 移除未使用的GroupCardNoticeEvent test: 添加全面的单元测试覆盖 - 添加test_import.py测试模块导入 - 添加test_debug.py测试插件加载调试 - 添加test_plugin_error.py测试错误处理 - 添加test_config_loader.py测试配置加载 - 添加test_redis_manager.py测试Redis管理 - 添加test_bot.py测试Bot功能 - 扩展test_models.py测试消息模型 - 添加test_plugin_manager_coverage.py测试插件管理 - 添加test_executor.py测试代码执行器 - 添加test_ws.py测试WebSocket - 添加test_api.py测试API接口 - 添加test_core_managers.py测试核心管理模块 fix(plugin_manager): 修复插件加载日志变量问题 覆盖率已到达86%(忽略插件)
This commit is contained in:
128
tests/test_bot.py
Normal file
128
tests/test_bot.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, AsyncMock, patch
|
||||
from models.message import MessageSegment
|
||||
from models.objects import GroupInfo, StrangerInfo
|
||||
from core.bot import Bot
|
||||
|
||||
|
||||
class TestBot:
|
||||
def test_bot_initialization(self):
|
||||
"""测试 Bot 类初始化。"""
|
||||
mock_ws = MagicMock()
|
||||
mock_ws.self_id = 123456
|
||||
bot = Bot(mock_ws)
|
||||
assert bot.self_id == 123456
|
||||
assert bot.code_executor is None
|
||||
|
||||
def test_build_forward_node(self):
|
||||
"""测试构建合并转发消息节点。"""
|
||||
mock_ws = MagicMock()
|
||||
bot = Bot(mock_ws)
|
||||
node = bot.build_forward_node(123456, "TestUser", "Hello World")
|
||||
assert node["type"] == "node"
|
||||
assert node["data"]["uin"] == 123456
|
||||
assert node["data"]["name"] == "TestUser"
|
||||
assert node["data"]["content"] == "Hello World"
|
||||
|
||||
def test_build_forward_node_with_segment(self):
|
||||
"""测试使用消息段构建合并转发消息节点。"""
|
||||
mock_ws = MagicMock()
|
||||
bot = Bot(mock_ws)
|
||||
segment = MessageSegment.text("Hello")
|
||||
node = bot.build_forward_node(123456, "TestUser", segment)
|
||||
assert node["type"] == "node"
|
||||
assert node["data"]["content"][0]["type"] == segment.type
|
||||
assert node["data"]["content"][0]["data"] == segment.data
|
||||
|
||||
def test_build_forward_node_with_segment_list(self):
|
||||
"""测试使用消息段列表构建合并转发消息节点。"""
|
||||
mock_ws = MagicMock()
|
||||
bot = Bot(mock_ws)
|
||||
segments = [MessageSegment.text("Hello"), MessageSegment.at(123456)]
|
||||
node = bot.build_forward_node(123456, "TestUser", segments)
|
||||
assert node["type"] == "node"
|
||||
assert len(node["data"]["content"]) == 2
|
||||
assert node["data"]["content"][0]["type"] == segments[0].type
|
||||
assert node["data"]["content"][0]["data"] == segments[0].data
|
||||
assert node["data"]["content"][1]["type"] == segments[1].type
|
||||
assert node["data"]["content"][1]["data"] == segments[1].data
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_forwarded_messages_group(self):
|
||||
"""测试发送群聊合并转发消息。"""
|
||||
mock_ws = MagicMock()
|
||||
bot = Bot(mock_ws)
|
||||
bot.send_group_forward_msg = AsyncMock()
|
||||
nodes = [bot.build_forward_node(123456, "TestUser", "Hello")]
|
||||
await bot.send_forwarded_messages(111111, nodes)
|
||||
bot.send_group_forward_msg.assert_called_once_with(111111, nodes)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_forwarded_messages_private(self):
|
||||
"""测试发送私聊合并转发消息。"""
|
||||
mock_ws = AsyncMock()
|
||||
bot = Bot(mock_ws)
|
||||
bot.send_private_forward_msg = AsyncMock()
|
||||
nodes = [bot.build_forward_node(123456, "TestUser", "Hello")]
|
||||
from models.events.base import OneBotEvent
|
||||
mock_event = MagicMock(spec=OneBotEvent)
|
||||
mock_event.group_id = None
|
||||
mock_event.user_id = 222222
|
||||
await bot.send_forwarded_messages(mock_event, nodes)
|
||||
bot.send_private_forward_msg.assert_called_once_with(222222, nodes)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_forwarded_messages_group_event(self):
|
||||
"""测试通过群聊事件发送合并转发消息。"""
|
||||
mock_ws = AsyncMock()
|
||||
bot = Bot(mock_ws)
|
||||
bot.send_group_forward_msg = AsyncMock()
|
||||
nodes = [bot.build_forward_node(123456, "TestUser", "Hello")]
|
||||
from models.events.base import OneBotEvent
|
||||
mock_event = MagicMock(spec=OneBotEvent)
|
||||
mock_event.group_id = 111111
|
||||
mock_event.user_id = 222222
|
||||
await bot.send_forwarded_messages(mock_event, nodes)
|
||||
bot.send_group_forward_msg.assert_called_once_with(111111, nodes)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_forwarded_messages_invalid_target(self):
|
||||
"""测试发送合并转发消息到无效目标。"""
|
||||
mock_ws = AsyncMock()
|
||||
bot = Bot(mock_ws)
|
||||
nodes = [bot.build_forward_node(123456, "TestUser", "Hello")]
|
||||
from models.events.base import OneBotEvent
|
||||
mock_event = MagicMock(spec=OneBotEvent)
|
||||
mock_event.group_id = None
|
||||
mock_event.user_id = None
|
||||
with pytest.raises(ValueError, match="Event has neither group_id nor user_id"):
|
||||
await bot.send_forwarded_messages(mock_event, nodes)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_group_list(self):
|
||||
"""测试获取群列表。"""
|
||||
mock_ws = MagicMock()
|
||||
bot = Bot(mock_ws)
|
||||
# 测试返回字典列表的情况
|
||||
super_get_group_list = AsyncMock(return_value=[{"group_id": 123456, "group_name": "Test Group"}])
|
||||
with patch.object(bot.__class__.__bases__[1], 'get_group_list', super_get_group_list):
|
||||
groups = await bot.get_group_list(no_cache=True)
|
||||
assert len(groups) == 1
|
||||
assert groups[0].group_id == 123456
|
||||
assert groups[0].group_name == "Test Group"
|
||||
assert isinstance(groups[0], GroupInfo)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_stranger_info(self):
|
||||
"""测试获取陌生人信息。"""
|
||||
mock_ws = MagicMock()
|
||||
bot = Bot(mock_ws)
|
||||
# 测试返回字典的情况
|
||||
super_get_stranger_info = AsyncMock(return_value={"user_id": 123456, "nickname": "TestUser", "sex": "male", "age": 18})
|
||||
with patch.object(bot.__class__.__bases__[2], 'get_stranger_info', super_get_stranger_info):
|
||||
info = await bot.get_stranger_info(123456, no_cache=True)
|
||||
assert info.user_id == 123456
|
||||
assert info.nickname == "TestUser"
|
||||
assert info.sex == "male"
|
||||
assert info.age == 18
|
||||
assert isinstance(info, StrangerInfo)
|
||||
Reference in New Issue
Block a user