本次提交修复了多个测试文件中的问题: 1. 将 ChannelType 枚举调用改为字符串实例化方式 2. 修正了日志断言、异步mock使用、配置参数等多处测试细节 3. 新增了会话聚合根、跨渠道关联策略等单元测试用例 4. 修复了路由测试中的路径方法错误与断言逻辑 5. 调整了依赖导入与测试夹具的兼容性 6. 统一了重试回退调度的列表/元组使用规范
61 lines
2.8 KiB
Python
61 lines
2.8 KiB
Python
"""channels plugins 层单元测试共享 fixture。
|
||
|
||
提供 ``fake_plugin_host`` 桩,模拟 ``PluginHost`` 协议
|
||
(``yuxi.channels.contract.plugin.entry.PluginHost``)。插件适配器测试
|
||
通过此桩获取被驱动端口与注册扩展点,不依赖真实宿主实现。
|
||
|
||
桩使用 ``MagicMock()``:所有 ``getXxxPort()`` 返回对应 ``MagicMock`` 桩
|
||
(调用方可按需覆盖为带 ``spec`` 的端口桩),``registerXxx()`` 返回 ``None``,
|
||
``publishEvent`` 为 ``AsyncMock``(可 ``await``)。单文件独有的 helper 应保留
|
||
在对应测试文件内(见 testing-guidelines.md)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from unittest.mock import AsyncMock, MagicMock
|
||
|
||
import pytest
|
||
|
||
|
||
@pytest.fixture
|
||
def fake_plugin_host() -> MagicMock:
|
||
"""PluginHost 桩。
|
||
|
||
``MagicMock()`` 模拟 ``PluginHost``:
|
||
- 所有 ``getXxxPort()`` 返回 ``MagicMock`` 桩(调用方可覆盖为带
|
||
``spec`` 的端口桩,如 ``host.getLoggerPort.return_value = fake_logger``);
|
||
- ``registerAdapter()`` / ``registerStageSlot()`` / ``registerEventSubscription()``
|
||
/ ``registerConfigSource()`` / ``registerMatchTier()`` / ``registerMatcher()``
|
||
返回 ``None``;
|
||
- ``publishEvent`` 为 ``AsyncMock``(可 ``await``),返回 ``None``。
|
||
"""
|
||
host = MagicMock()
|
||
# 端口获取:返回 MagicMock 桩,调用方可覆盖为带 spec 的端口桩
|
||
host.getConfigPort.return_value = MagicMock()
|
||
host.getLoggerPort.return_value = MagicMock()
|
||
host.getPersistencePort.return_value = MagicMock()
|
||
host.getCachePort.return_value = MagicMock()
|
||
host.getTracerPort.return_value = MagicMock()
|
||
host.getQueuePort.return_value = MagicMock()
|
||
host.getConversationPort.return_value = MagicMock()
|
||
host.getAgentRunPort.return_value = MagicMock()
|
||
host.getIdentityResolverPort.return_value = MagicMock()
|
||
host.getChannelAccountRepositoryPort.return_value = MagicMock()
|
||
host.getChannelSessionRepositoryPort.return_value = MagicMock()
|
||
host.getPairingRepositoryPort.return_value = MagicMock()
|
||
host.getAuditLogRepositoryPort.return_value = MagicMock()
|
||
host.getOutboxRepositoryPort.return_value = MagicMock()
|
||
host.getUserIdentityRepositoryPort.return_value = MagicMock()
|
||
host.getIdempotencyRepositoryPort.return_value = MagicMock()
|
||
host.getPersistenceHealthPort.return_value = MagicMock()
|
||
# 扩展点注册:返回 None
|
||
host.registerAdapter.return_value = None
|
||
host.registerStageSlot.return_value = None
|
||
host.registerEventSubscription.return_value = None
|
||
host.registerConfigSource.return_value = None
|
||
host.registerMatchTier.return_value = None
|
||
host.registerMatcher.return_value = None
|
||
# 事件发布:async def,使用 AsyncMock
|
||
host.publishEvent = AsyncMock(return_value=None)
|
||
return host
|