213 lines
6.6 KiB
Python
213 lines
6.6 KiB
Python
"""
|
|
Agent 单元测试模块
|
|
测试各个 Agent 的基本功能
|
|
"""
|
|
import pytest
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# 添加项目根目录到路径
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
class TestLLMConfig:
|
|
"""测试 LLM 配置模块"""
|
|
|
|
def test_get_llm_config(self):
|
|
"""测试获取基本配置"""
|
|
from config.llm_config import get_llm_config
|
|
|
|
config = get_llm_config(
|
|
model="qwen3.5-flash",
|
|
api_key="test_key",
|
|
temperature=0.7
|
|
)
|
|
|
|
assert "config_list" in config
|
|
assert len(config["config_list"]) == 1
|
|
assert config["config_list"][0]["model"] == "qwen3.5-flash"
|
|
assert config["config_list"][0]["api_key"] == "test_key"
|
|
assert config["config_list"][0]["temperature"] == 0.7
|
|
|
|
def test_get_agent_llm_config(self):
|
|
"""测试获取特定 Agent 配置"""
|
|
from config.llm_config import get_agent_llm_config
|
|
|
|
config = get_agent_llm_config("PM_Agent")
|
|
|
|
assert "config_list" in config
|
|
assert config["config_list"][0]["model"] == "qwen3.5-flash"
|
|
|
|
def test_system_prompts_exist(self):
|
|
"""测试系统提示词存在"""
|
|
from config.llm_config import PM_PROMPT, QA_PROMPT, DEV_PROMPT, ORCH_PROMPT
|
|
|
|
assert PM_PROMPT is not None
|
|
assert len(PM_PROMPT) > 100
|
|
|
|
assert QA_PROMPT is not None
|
|
assert len(QA_PROMPT) > 100
|
|
|
|
assert DEV_PROMPT is not None
|
|
assert len(DEV_PROMPT) > 100
|
|
|
|
assert ORCH_PROMPT is not None
|
|
assert len(ORCH_PROMPT) > 100
|
|
|
|
|
|
class TestLogger:
|
|
"""测试日志模块"""
|
|
|
|
def test_logger_creation(self):
|
|
"""测试日志记录器创建"""
|
|
from utils.logger import ConversationLogger
|
|
|
|
logger = ConversationLogger(log_dir="test_logs")
|
|
|
|
assert logger is not None
|
|
assert logger.session_id is not None
|
|
|
|
def test_log_message(self):
|
|
"""测试消息记录"""
|
|
from utils.logger import ConversationLogger
|
|
import shutil
|
|
|
|
# 清理测试目录
|
|
test_dir = Path("test_logs")
|
|
if test_dir.exists():
|
|
shutil.rmtree(test_dir)
|
|
|
|
logger = ConversationLogger(log_dir="test_logs")
|
|
logger.log_message(
|
|
agent_name="Test_Agent",
|
|
message="这是一条测试消息",
|
|
role="assistant"
|
|
)
|
|
|
|
assert len(logger.conversation_history) == 1
|
|
assert logger.conversation_history[0]["agent_name"] == "Test_Agent"
|
|
assert logger.conversation_history[0]["message"] == "这是一条测试消息"
|
|
|
|
# 清理
|
|
shutil.rmtree(test_dir)
|
|
|
|
def test_callback_handler(self):
|
|
"""测试回调处理器"""
|
|
from utils.callback_handler import MessageCallbackHandler
|
|
|
|
handler = MessageCallbackHandler()
|
|
|
|
received_messages = []
|
|
|
|
def test_callback(msg):
|
|
received_messages.append(msg)
|
|
|
|
handler.register_callback(test_callback)
|
|
handler.on_message(
|
|
agent_name="Test_Agent",
|
|
message="测试回调消息"
|
|
)
|
|
|
|
assert len(received_messages) == 1
|
|
assert received_messages[0]["agent_name"] == "Test_Agent"
|
|
|
|
|
|
class TestAgentsCreation:
|
|
"""测试 Agent 创建"""
|
|
|
|
@pytest.mark.skipif(not os.getenv("DASHSCOPE_API_KEY"), reason="需要 API Key")
|
|
def test_create_pm_agent(self):
|
|
"""测试创建 PM Agent"""
|
|
from agents import create_pm_agent
|
|
from config.llm_config import get_llm_config
|
|
|
|
api_key = os.getenv("DASHSCOPE_API_KEY")
|
|
llm_config = get_llm_config(api_key=api_key)
|
|
|
|
agent = create_pm_agent(llm_config=llm_config)
|
|
|
|
assert agent is not None
|
|
assert agent.name == "PM_Agent"
|
|
|
|
@pytest.mark.skipif(not os.getenv("DASHSCOPE_API_KEY"), reason="需要 API Key")
|
|
def test_create_qa_agent(self):
|
|
"""测试创建 QA Agent"""
|
|
from agents import create_qa_agent
|
|
from config.llm_config import get_llm_config
|
|
|
|
api_key = os.getenv("DASHSCOPE_API_KEY")
|
|
llm_config = get_llm_config(api_key=api_key)
|
|
|
|
agent = create_qa_agent(llm_config=llm_config)
|
|
|
|
assert agent is not None
|
|
assert agent.name == "QA_Agent"
|
|
|
|
@pytest.mark.skipif(not os.getenv("DASHSCOPE_API_KEY"), reason="需要 API Key")
|
|
def test_create_dev_agent(self):
|
|
"""测试创建 Dev Agent"""
|
|
from agents import create_dev_agent
|
|
from config.llm_config import get_llm_config
|
|
|
|
api_key = os.getenv("DASHSCOPE_API_KEY")
|
|
llm_config = get_llm_config(api_key=api_key)
|
|
|
|
agent = create_dev_agent(llm_config=llm_config)
|
|
|
|
assert agent is not None
|
|
assert agent.name == "Dev_Agent"
|
|
|
|
@pytest.mark.skipif(not os.getenv("DASHSCOPE_API_KEY"), reason="需要 API Key")
|
|
def test_create_orchestrator(self):
|
|
"""测试创建 Orchestrator Agent"""
|
|
from agents import create_orchestrator_agent
|
|
from config.llm_config import get_llm_config
|
|
|
|
api_key = os.getenv("DASHSCOPE_API_KEY")
|
|
llm_config = get_llm_config(api_key=api_key)
|
|
|
|
agent = create_orchestrator_agent(llm_config=llm_config)
|
|
|
|
assert agent is not None
|
|
assert agent.name == "Orchestrator"
|
|
|
|
|
|
class TestWorkspaceManagement:
|
|
"""测试工作空间管理"""
|
|
|
|
def test_workspace_directory_exists(self):
|
|
"""测试工作目录存在"""
|
|
workspace_dir = Path(__file__).parent.parent / "workspace"
|
|
workspace_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
assert workspace_dir.exists()
|
|
assert workspace_dir.is_dir()
|
|
|
|
def test_create_sample_files(self):
|
|
"""测试创建示例文件"""
|
|
workspace_dir = Path(__file__).parent.parent / "workspace"
|
|
workspace_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 创建测试文件
|
|
test_file = workspace_dir / "test_sample.txt"
|
|
test_content = "这是一个测试文件"
|
|
|
|
with open(test_file, 'w', encoding='utf-8') as f:
|
|
f.write(test_content)
|
|
|
|
assert test_file.exists()
|
|
|
|
with open(test_file, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
assert content == test_content
|
|
|
|
# 清理
|
|
test_file.unlink()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 运行测试
|
|
pytest.main([__file__, "-v", "--tb=short"])
|