ForcePilot/backend/test/unit/channels/plugins/feishu/test_constants.py
Kris 532e7c59ec test: add and fix multi-channel unit tests
1. 新增QQBot、Wecom、Dingtalk、Instagram、WechatMp等渠道的单元测试夹具与测试用例
2. 修复WechatILink的缓存键顺序与测试用例
3. 补全Feishu适配器的日志断言与测试逻辑
4. 清理WechatILink冗余的clear_context_token测试代码
2026-07-08 22:59:24 +08:00

127 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""feishu 插件 _constants.py 模块测试。
验证 C1 优化创建的共享常量模块:
- 模块导出完整(``__all__`` 与实际定义一致)。
- 常量值与 ``manifest.json`` 中对应字段对齐F-03 单真相源):
- ``CHANNEL_TARGET`` 与 manifest ``channel_type`` 一致。
- ``HTTP_TIMEOUT_SECONDS`` 与 manifest ``request_timeout_ms.default`` 对齐。
- ``HTTP_MAX_CONNECTIONS`` 与 manifest ``resource_quota.max_connections`` 对齐。
_constants.py 作为 ConfigPort 未命中时的本地回退默认值,其值必须与
manifest 声明保持同步,避免双源不一致。
"""
from __future__ import annotations
import os
import pytest
from yuxi.channels.application.lifecycle.manifest_loader import load_manifest_from_dir
from yuxi.channels.plugins.feishu import _constants
pytestmark = pytest.mark.unit
@pytest.fixture
def feishu_manifest():
plugin_dir = os.path.join(
os.path.dirname(__file__),
"..",
"..",
"..",
"..",
"..",
"package",
"yuxi",
"channels",
"plugins",
"feishu",
)
return load_manifest_from_dir(os.path.abspath(plugin_dir))
# ------------------------------------------------------------------
# 模块导出完整性
# ------------------------------------------------------------------
@pytest.mark.unit
class TestConstantsExports:
"""验证 _constants.py 模块导出完整。"""
def test_all_contains_channel_target(self):
assert "CHANNEL_TARGET" in _constants.__all__
def test_all_contains_feishu_api_dep(self):
assert "FEISHU_API_DEP" in _constants.__all__
def test_all_contains_http_constants(self):
assert "HTTP_TIMEOUT_SECONDS" in _constants.__all__
assert "HTTP_MAX_CONNECTIONS" in _constants.__all__
assert "HTTP_MAX_KEEPALIVE_CONNECTIONS" in _constants.__all__
def test_all_matches_defined_names(self):
"""``__all__`` 应恰好包含模块中定义的全部公开常量。"""
expected = {
"CHANNEL_TARGET",
"FEISHU_API_DEP",
"HTTP_TIMEOUT_SECONDS",
"HTTP_MAX_CONNECTIONS",
"HTTP_MAX_KEEPALIVE_CONNECTIONS",
}
assert set(_constants.__all__) == expected
# ------------------------------------------------------------------
# 常量值与 manifest 对齐F-03 单真相源)
# ------------------------------------------------------------------
@pytest.mark.unit
class TestConstantsAlignWithManifest:
"""验证 _constants.py 常量值与 manifest.json 声明对齐。"""
def test_channel_target_matches_manifest_channel_type(self, feishu_manifest):
assert _constants.CHANNEL_TARGET == feishu_manifest.channel_type
def test_channel_target_matches_manifest_provides(self, feishu_manifest):
"""CHANNEL_TARGET 也应出现在 manifest provides 中。"""
assert _constants.CHANNEL_TARGET in feishu_manifest.provides
def test_http_timeout_aligns_with_request_timeout_ms(self, feishu_manifest):
"""HTTP_TIMEOUT_SECONDS与 manifest request_timeout_ms.default毫秒对齐。"""
field = next(f for f in feishu_manifest.config_schema if f.key == "request_timeout_ms")
assert _constants.HTTP_TIMEOUT_SECONDS == field.default / 1000.0
def test_http_max_connections_aligns_with_resource_quota(self, feishu_manifest):
"""HTTP_MAX_CONNECTIONS 与 manifest resource_quota.max_connections 对齐。"""
assert feishu_manifest.resource_quota is not None
assert _constants.HTTP_MAX_CONNECTIONS == feishu_manifest.resource_quota.max_connections
# ------------------------------------------------------------------
# 常量值合理性
# ------------------------------------------------------------------
@pytest.mark.unit
class TestConstantsSanity:
"""验证 _constants.py 常量值的基本合理性。"""
def test_channel_target_is_feishu(self):
assert _constants.CHANNEL_TARGET == "feishu"
def test_feishu_api_dep_is_string(self):
assert isinstance(_constants.FEISHU_API_DEP, str)
assert _constants.FEISHU_API_DEP == "feishu_api"
def test_http_timeout_positive(self):
assert _constants.HTTP_TIMEOUT_SECONDS > 0
def test_http_max_connections_positive(self):
assert _constants.HTTP_MAX_CONNECTIONS > 0
def test_http_keepalive_le_max_connections(self):
"""keepalive 连接数上限不应超过最大连接数。"""
assert _constants.HTTP_MAX_KEEPALIVE_CONNECTIONS <= _constants.HTTP_MAX_CONNECTIONS