diff --git a/app/application/commands.py b/app/application/commands.py index 111028601..b5cdf240e 100644 --- a/app/application/commands.py +++ b/app/application/commands.py @@ -40,7 +40,8 @@ def get_command_object() -> Any: def get_commands() -> Dict[str, Any]: """返回全部已注册命令。""" - return get_command_object().get_commands() + commands: dict[str, object] = get_command_object().get_commands() + return commands def get_command(name: str) -> Optional[Any]: @@ -69,7 +70,8 @@ def dispatch_command( normalized = f"/{normalized}" command_name = normalized.split()[0] matched = get_command(command_name) - if matched is None: + # Command.get 使用空字典表示未注册命令,不能将其报告为执行成功。 + if not matched: available = [f"{name} - {info.get('description', '无描述')}" for name, info in get_commands().items()] raise ValueError(f"命令 {command_name} 不存在" + (f";可用命令: {'; '.join(available)}" if available else "")) publish_event( diff --git a/docs/code-quality.md b/docs/code-quality.md index 0813003db..b45db38f1 100644 --- a/docs/code-quality.md +++ b/docs/code-quality.md @@ -75,7 +75,7 @@ uv run --locked --no-sync python scripts/architecture/complexity.py 回调和返回值类型;全仓错误计数为零并不保证单独纳入 strict frontier 后也为零。 2026-10-16 开始逐文件清理类型债务,`app/domain/title.py` 通过局部可空类型声明保留 -既有搜索解析行为,并纳入严格清单;当前清单共 99 个文件(98 个生产文件和 1 个脚本)。 +既有搜索解析行为,并纳入严格清单;当前清单共 100 个文件(99 个生产文件和 1 个脚本)。 每个治理提交聚焦一个生产文件及其必要测试、门禁配置、真实下降基线和指标文档。 验收同时检查完整严格清单与全宿主 Linux 分析,避免 `follow_imports = skip` 隐藏依赖 签名冲突;不通过忽略诊断或放宽类型合同接受回退。最新文件清单以 `mypy.ini` 为准, diff --git a/docs/refactor/optimization-checklist.md b/docs/refactor/optimization-checklist.md index deb769a09..dbeeb9e46 100644 --- a/docs/refactor/optimization-checklist.md +++ b/docs/refactor/optimization-checklist.md @@ -106,9 +106,9 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁 | Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 | | Module Contract | 215 specs / 211 methods / 263 calls | 动态方法名为 0;分类事实富化通过显式合同进入插件调度,旧 YAML 分类方法已移除;download 新增 custom_words 可选参数,旧 provider 签名通过显式合同兼容 | | Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement | -| Python 源码量 | 372,305 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 | +| Python 源码量 | 372,307 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 | | 长方法 | 309 个超过 80 行 | 本次 AST 扫描排除 `app/plugins/**`;61 个超过 150 行,17 个超过 250 行;新增函数仍受 C901/嵌套深度门禁约束 | -| 全量 mypy 历史债务 | 8,824 / 455 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 | +| 全量 mypy 历史债务 | 8,823 / 454 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 | | Ruff 历史诊断 | 98 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` | | 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 | diff --git a/mypy.ini b/mypy.ini index 601b6126b..7d3e20ab3 100644 --- a/mypy.ini +++ b/mypy.ini @@ -73,6 +73,7 @@ files = app/application/security/twofactor.py, app/application/security/otp.py, app/application/security/token.py, + app/application/commands.py, app/application/outbox.py, app/application/torrent/cache.py, app/application/configuration.py, diff --git a/tests/fixtures/architecture/mypy-baseline.json b/tests/fixtures/architecture/mypy-baseline.json index 67860ceba..8746946ad 100644 --- a/tests/fixtures/architecture/mypy-baseline.json +++ b/tests/fixtures/architecture/mypy-baseline.json @@ -576,9 +576,6 @@ "app/application/agent.py": { "no-any-return": 4 }, - "app/application/commands.py": { - "no-any-return": 1 - }, "app/application/dashboard.py": { "operator": 5 }, diff --git a/tests/test_command_dispatch.py b/tests/test_command_dispatch.py new file mode 100644 index 000000000..1220327ac --- /dev/null +++ b/tests/test_command_dispatch.py @@ -0,0 +1,59 @@ +"""命令门面与真实注册表的缺省值契约回归。""" + +from unittest.mock import Mock + +import pytest + +from app.application import commands +from app.command import Command +from app.schemas.types import EventType, NotificationChannel + + +@pytest.fixture +def command_registry(monkeypatch): + """保留真实注册表查询逻辑,跳过单例初始化和外部服务装配。""" + registry = object.__new__(Command) + registry._commands = { + "/demo": {"description": "示例命令", "pid": "demo-plugin"}, + } + monkeypatch.setattr(commands, "_command_class", lambda: registry) + return registry + + +@pytest.mark.parametrize("command", ["/missing", " missing 参数 "]) +def test_missing_command_is_rejected_before_publishing(command_registry, command): + """真实注册表用空字典表示缺失,门面应报错且不发送执行事件。""" + assert command_registry.get("/missing") == {} + publish_event = Mock() + + with pytest.raises(ValueError, match="命令 /missing 不存在.*示例命令"): + commands.dispatch_command(command, user_id="1", publish_event=publish_event) + + publish_event.assert_not_called() + + +def test_registered_command_keeps_arguments_and_channel(command_registry): + """已注册命令仍传递原参数、用户和渠道,并返回插件描述。""" + publish_event = Mock() + assert commands.get_commands() is command_registry._commands + + result = commands.dispatch_command( + " demo 参数 ", + user_id="7", + channel=NotificationChannel.Wechat, + source="企业微信", + publish_event=publish_event, + ) + + publish_event.assert_called_once_with( + EventType.CommandExcute, + { + "cmd": "/demo 参数", + "user": "7", + "channel": NotificationChannel.Wechat, + "source": "企业微信", + }, + ) + assert result["command"] == "/demo 参数" + assert result["command_desc"] == "示例命令" + assert result["plugin_id"] == "demo-plugin"