"""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