ForcePilot/backend/test/unit/channels/test_channels_directory.py
Kris 3264900bc9 test: 新增多渠道单元测试用例并配置测试环境变量
新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块
同时在测试配置中添加了测试用的OpenAI API密钥环境变量
2026-05-12 00:56:47 +08:00

60 lines
2.1 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channels.policy.directory import BaseDirectoryAdapter
class TestBaseDirectoryAdapter:
@pytest.mark.asyncio
async def test_search_peers_default_returns_empty(self):
class ConcreteAdapter(BaseDirectoryAdapter):
async def list_peers(self, config: dict, account_id: str = "default") -> list[dict]:
return []
async def list_groups(self, config: dict, account_id: str = "default") -> list[dict]:
return []
adapter = ConcreteAdapter()
result = await adapter.search_peers("test query")
assert result == []
@pytest.mark.asyncio
async def test_custom_search_peers_override(self):
class CustomAdapter(BaseDirectoryAdapter):
async def list_peers(self, config: dict, account_id: str = "default") -> list[dict]:
return []
async def list_groups(self, config: dict, account_id: str = "default") -> list[dict]:
return []
def search_peers(self, query: str) -> list[dict]:
return [{"id": "1", "name": query}]
adapter = CustomAdapter()
result = adapter.search_peers("alice")
assert result == [{"id": "1", "name": "alice"}]
@pytest.mark.asyncio
async def test_list_peers_contract(self):
class Adapter(BaseDirectoryAdapter):
async def list_peers(self, config: dict, account_id: str = "default") -> list[dict]:
return [{"id": "p1", "name": "User1"}]
async def list_groups(self, config: dict, account_id: str = "default") -> list[dict]:
return [{"id": "g1", "name": "Group1"}]
adapter = Adapter()
peers = await adapter.list_peers({})
assert isinstance(peers, list)
assert len(peers) == 1
assert peers[0]["id"] == "p1"
groups = await adapter.list_groups({})
assert isinstance(groups, list)
assert len(groups) == 1
assert groups[0]["id"] == "g1"
def test_cannot_instantiate_abstract(self):
with pytest.raises(TypeError):
BaseDirectoryAdapter()