完成渠道模块的单元测试目录搭建,新增多个领域模型、端口、中间件、事件、服务以及基础设施层的单元测试文件,同时补充了conftest.py的环境变量配置,完善测试基础环境。
117 lines
4.0 KiB
Python
117 lines
4.0 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from yuxi.channel.application.pipeline.builder import build_inbound_pipeline
|
|
from yuxi.channel.application.service.auth_service import AuthService
|
|
from yuxi.channel.domain.service.pipeline import Pipeline
|
|
|
|
|
|
class TestBuildInboundPipeline:
|
|
@pytest.fixture
|
|
def mock_auth_service(self):
|
|
return MagicMock(spec=AuthService)
|
|
|
|
@pytest.fixture
|
|
def mock_cache(self):
|
|
return MagicMock()
|
|
|
|
@pytest.fixture
|
|
def mock_rate_limiter(self):
|
|
return MagicMock()
|
|
|
|
@pytest.fixture
|
|
def mock_queue(self):
|
|
return MagicMock()
|
|
|
|
def test_build_basic_pipeline(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
)
|
|
assert isinstance(pipeline, Pipeline)
|
|
assert len(pipeline.middlewares) == 8
|
|
|
|
def test_build_pipeline_with_config(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
config = {
|
|
"keyword_blocklist": ["bad"],
|
|
"allow_from": {"web": ["user1"]},
|
|
"access_policies": {"web": {"dm_policy": "open"}},
|
|
}
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
channel_config=config,
|
|
)
|
|
assert isinstance(pipeline, Pipeline)
|
|
assert len(pipeline.middlewares) == 8
|
|
|
|
def test_build_pipeline_with_signature_verifier(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
verifier = MagicMock()
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
signature_verifier=verifier,
|
|
)
|
|
assert isinstance(pipeline, Pipeline)
|
|
|
|
def test_build_pipeline_with_metrics(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
metrics = MagicMock()
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
metrics=metrics,
|
|
)
|
|
assert isinstance(pipeline, Pipeline)
|
|
|
|
def test_build_pipeline_with_bot_id(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
bot_id="bot123",
|
|
)
|
|
assert isinstance(pipeline, Pipeline)
|
|
|
|
def test_build_pipeline_with_keyword_matcher(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
matcher = MagicMock()
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
keyword_matcher=matcher,
|
|
)
|
|
assert isinstance(pipeline, Pipeline)
|
|
|
|
def test_pipeline_middleware_order(self, mock_auth_service, mock_cache, mock_rate_limiter, mock_queue):
|
|
pipeline = build_inbound_pipeline(
|
|
auth_service=mock_auth_service,
|
|
cache_port=mock_cache,
|
|
rate_limit_port=mock_rate_limiter,
|
|
queue_port=mock_queue,
|
|
)
|
|
names = [mw.name for mw in pipeline.middlewares]
|
|
expected = [
|
|
"auth",
|
|
"validation",
|
|
"keyword_filter",
|
|
"dedup",
|
|
"rate_limit",
|
|
"access_policy",
|
|
"mention_gate",
|
|
"enqueue_mq",
|
|
]
|
|
assert names == expected
|