更新/help (#31)

* 滚木

* feat: 重构核心架构,增强类型安全与插件管理

本次提交对核心模块进行了深度重构,引入 Pydantic 增强配置管理的类型安全性,并全面优化了插件管理系统。

主要变更详情:

1. 核心架构与配置
   - 重构配置加载模块:引入 Pydantic 模型 (`core/config_models.py`),提供严格的配置项类型检查、验证及默认值管理。
   - 统一模块结构:规范化模块导入路径,移除冗余的 `__init__.py` 文件,提升项目结构的清晰度。
   - 性能优化:集成 Redis 缓存支持 (`RedisManager`),有效降低高频 API 调用开销,提升响应速度。

2. 插件系统升级
   - 实现热重载机制:新增插件文件变更监听功能,支持开发过程中自动重载插件,提升开发效率。
   - 优化生命周期管理:改进插件加载与卸载逻辑,支持精确卸载指定插件及其关联的命令、事件处理器和定时任务。

3. 功能特性增强
   - 新增媒体 API:引入 `MediaAPI` 模块,封装图片、语音等富媒体资源的获取与处理接口。
   - 完善权限体系:重构权限管理系统,实现管理员与操作员的分级控制,支持更细粒度的命令权限校验。

4. 代码质量与稳定性
   - 全面类型修复:解决 `mypy` 静态类型检查发现的大量类型错误(包括 `CommandManager`、`EventFactory` 及 `Bot` API 签名不匹配问题)。
   - 增强错误处理:优化消息处理管道的异常捕获机制,完善关键路径的日志记录,提升系统运行稳定性。

* feat: 添加测试用例并优化代码结构

refactor(permission_manager): 调整初始化顺序和逻辑
fix(admin_manager): 修复初始化逻辑和目录创建问题
feat(ws): 优化Bot实例初始化条件
feat(message): 增强MessageSegment功能并添加测试
feat(events): 支持字符串格式的消息解析
test: 添加核心功能测试用例
refactor(plugin_manager): 改进插件路径处理
style: 清理无用导入和代码
chore: 更新依赖项

* refactor(handler): 移除TYPE_CHECKING并直接导入Bot类

简化类型注解,直接导入Bot类而非使用TYPE_CHECKING条件导入,提高代码可读性和维护性

* fix(command_manager): 修复插件卸载时元信息移除不精确的问题

修复 CommandManager 中 unload_plugin 方法移除插件元信息时使用 startswith 导致可能误删其他插件的问题,改为精确匹配
同时调整相关测试用例验证精确匹配行为

* refactor: 清理未使用的导入和更新文档结构

docs: 添加config_models.py到项目结构文档
docs: 调整数据目录位置到core/data下
docs: 更新权限管理器文档描述

* 文档更新

* 更新thpic插件 支持一次返回多张图

* 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%(忽略插件)

* 更新/help指令,现在会发送图片

---------

Co-authored-by: K2cr2O1 <2221577113@qq.com>
Co-authored-by: 镀铬酸钾 <148796996+K2cr2O1@users.noreply.github.com>
This commit is contained in:
baby2016
2026-01-10 20:39:52 +08:00
committed by GitHub
parent 5f16c288bf
commit 651d982e19
20 changed files with 2077 additions and 124 deletions

179
tests/test_ws.py Normal file
View File

@@ -0,0 +1,179 @@
import pytest
import asyncio
from unittest.mock import MagicMock, AsyncMock, patch
from core.ws import WS
from core.bot import Bot
from models.objects import GroupInfo, StrangerInfo
class TestWS:
@pytest.mark.asyncio
async def test_ws_initialization(self):
"""测试 WS 类初始化。"""
# 模拟全局配置
with patch('core.ws.global_config') as mock_config:
mock_config.napcat_ws.uri = "ws://localhost:8080"
mock_config.napcat_ws.token = "test_token"
mock_config.napcat_ws.reconnect_interval = 5
ws = WS()
assert ws.url == "ws://localhost:8080"
assert ws.token == "test_token"
assert ws.reconnect_interval == 5
assert ws.ws is None
assert ws.bot is None
assert ws.self_id is None
assert ws.code_executor is None
@pytest.mark.asyncio
async def test_call_api(self):
"""测试调用 API 方法。"""
with patch('core.ws.global_config') as mock_config:
mock_config.napcat_ws.uri = "ws://localhost:8080"
mock_config.napcat_ws.token = "test_token"
mock_config.napcat_ws.reconnect_interval = 5
ws = WS()
# 测试 WebSocket 未初始化的情况
result = await ws.call_api("send_group_msg", {"group_id": 123456, "message": "test"})
assert result == {"status": "failed", "msg": "websocket not initialized"}
# 测试 WebSocket 已初始化但未连接的情况
mock_ws = MagicMock()
mock_ws.state = None
ws.ws = mock_ws
result = await ws.call_api("send_group_msg", {"group_id": 123456, "message": "test"})
assert result == {"status": "failed", "msg": "websocket is not open"}
@pytest.mark.asyncio
async def test_on_event_bot_initialization(self):
"""测试事件处理中的 Bot 初始化。"""
with patch('core.ws.global_config') as mock_config:
mock_config.napcat_ws.uri = "ws://localhost:8080"
mock_config.napcat_ws.token = "test_token"
mock_config.napcat_ws.reconnect_interval = 5
ws = WS()
# 模拟包含 self_id 的事件
event_data = {
"post_type": "message",
"message_type": "private",
"self_id": 123456,
"user_id": 789012,
"message": "test",
"raw_message": "test"
}
# 模拟事件工厂
with patch('core.ws.EventFactory') as mock_factory:
mock_event = MagicMock()
mock_event.post_type = "message"
mock_event.self_id = 123456
mock_event.sender = None
mock_event.message_type = "private"
mock_event.user_id = 789012
mock_event.raw_message = "test"
mock_factory.create_event.return_value = mock_event
# 模拟命令管理器
with patch('core.ws.matcher') as mock_matcher:
mock_matcher.handle_event = AsyncMock()
await ws.on_event(event_data)
# 验证 Bot 已初始化
assert ws.bot is not None
assert isinstance(ws.bot, Bot)
assert ws.self_id == 123456
# 验证事件处理
mock_factory.create_event.assert_called_once_with(event_data)
mock_matcher.handle_event.assert_called_once()
@pytest.mark.asyncio
async def test_on_event_no_bot(self):
"""测试 Bot 未初始化时的事件处理。"""
with patch('core.ws.global_config') as mock_config:
mock_config.napcat_ws.uri = "ws://localhost:8080"
mock_config.napcat_ws.token = "test_token"
mock_config.napcat_ws.reconnect_interval = 5
ws = WS()
# 模拟不包含 self_id 的事件
event_data = {
"post_type": "message",
"message_type": "private",
"user_id": 789012,
"message": "test",
"raw_message": "test"
}
# 模拟事件工厂
with patch('core.ws.EventFactory') as mock_factory:
mock_event = MagicMock()
mock_event.post_type = "message"
# 确保事件没有 self_id 属性
del mock_event.self_id
mock_event.sender = None
mock_event.message_type = "private"
mock_event.user_id = 789012
mock_event.raw_message = "test"
mock_factory.create_event.return_value = mock_event
# 模拟命令管理器
with patch('core.ws.matcher') as mock_matcher:
mock_matcher.handle_event = AsyncMock()
await ws.on_event(event_data)
# 验证 Bot 未初始化
assert ws.bot is None
assert ws.self_id is None
# 验证事件处理未被调用
mock_matcher.handle_event.assert_not_called()
@pytest.mark.asyncio
async def test_call_api_with_code_executor(self):
"""测试带代码执行器的 WS 初始化。"""
with patch('core.ws.global_config') as mock_config:
mock_config.napcat_ws.uri = "ws://localhost:8080"
mock_config.napcat_ws.token = "test_token"
mock_config.napcat_ws.reconnect_interval = 5
mock_executor = MagicMock()
ws = WS(code_executor=mock_executor)
# 模拟包含 self_id 的事件
event_data = {
"post_type": "message",
"message_type": "private",
"self_id": 123456,
"user_id": 789012,
"message": "test",
"raw_message": "test"
}
# 模拟事件工厂
with patch('core.ws.EventFactory') as mock_factory:
mock_event = MagicMock()
mock_event.post_type = "message"
mock_event.self_id = 123456
mock_event.sender = None
mock_event.message_type = "private"
mock_event.user_id = 789012
mock_event.raw_message = "test"
mock_factory.create_event.return_value = mock_event
# 模拟命令管理器
with patch('core.ws.matcher') as mock_matcher:
mock_matcher.handle_event = AsyncMock()
await ws.on_event(event_data)
# 验证代码执行器已注入
assert ws.bot.code_executor is mock_executor
assert mock_executor.bot is ws.bot