完成渠道模块的单元测试目录搭建,新增多个领域模型、端口、中间件、事件、服务以及基础设施层的单元测试文件,同时补充了conftest.py的环境变量配置,完善测试基础环境。
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from yuxi.channel.infrastructure.agent.agent_adapter import AgentAdapter
|
|
|
|
|
|
class TestAgentAdapter:
|
|
@pytest.fixture
|
|
def mock_agent_port(self):
|
|
return AsyncMock()
|
|
|
|
@pytest.fixture
|
|
def adapter(self, mock_agent_port):
|
|
return AgentAdapter(agent_port=mock_agent_port)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stream_chat(self, adapter, mock_agent_port):
|
|
mock_agent_port.stream_chat.return_value = "Hello, user!"
|
|
result = await adapter.stream_chat(
|
|
thread_id="thread123",
|
|
message="Hello",
|
|
agent_config_id=1,
|
|
)
|
|
assert result == "Hello, user!"
|
|
mock_agent_port.stream_chat.assert_awaited_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stream_chat_with_history(self, adapter, mock_agent_port):
|
|
mock_agent_port.stream_chat.return_value = "Hello, user!"
|
|
result = await adapter.stream_chat(
|
|
thread_id="thread123",
|
|
message="Hello",
|
|
agent_config_id=1,
|
|
history=[{"role": "user", "content": "Hi"}],
|
|
)
|
|
assert result == "Hello, user!"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stream_chat_exception(self, adapter, mock_agent_port):
|
|
mock_agent_port.stream_chat.side_effect = Exception("agent error")
|
|
with pytest.raises(Exception):
|
|
await adapter.stream_chat(
|
|
thread_id="thread123",
|
|
message="Hello",
|
|
agent_config_id=1,
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_agent_info(self, adapter, mock_agent_port):
|
|
mock_agent_port.get_agent_info.return_value = {"name": "Test Agent"}
|
|
result = await adapter.get_agent_info(agent_config_id=1)
|
|
assert result == {"name": "Test Agent"}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_health_check(self, adapter, mock_agent_port):
|
|
mock_agent_port.health_check.return_value = True
|
|
result = await adapter.health_check()
|
|
assert result is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_health_check_failure(self, adapter, mock_agent_port):
|
|
mock_agent_port.health_check.return_value = False
|
|
result = await adapter.health_check()
|
|
assert result is False
|