blob: f094f6d22eeacd95170de74fdae15fa6b8e714da [file] [log] [blame] [raw]
"""
简化的Agent核心模块测试
"""
import pytest
from unittest.mock import Mock, patch, AsyncMock
from claude_agent.core.agent import AgentCore, ThinkingMode, Task
class TestAgentCore:
"""AgentCore类的基础测试"""
def test_thinking_mode_enum(self):
"""测试思考模式枚举"""
assert ThinkingMode.INTERACTIVE.value == "interactive"
assert ThinkingMode.YOLO.value == "yolo"
def test_agent_initialization_basic(self):
"""测试Agent基础初始化"""
with patch('claude_agent.core.agent.ClaudeSDKClient'):
agent = AgentCore()
assert agent.thinking_mode == ThinkingMode.INTERACTIVE
assert agent.tasks == []
assert agent.conversation_history == []
assert agent.mcp_tools == {}
def test_set_thinking_mode(self):
"""测试设置思考模式"""
with patch('claude_agent.core.agent.ClaudeSDKClient'):
agent = AgentCore()
agent.set_thinking_mode(ThinkingMode.YOLO)
assert agent.thinking_mode == ThinkingMode.YOLO
agent.set_thinking_mode(ThinkingMode.INTERACTIVE)
assert agent.thinking_mode == ThinkingMode.INTERACTIVE
def test_extract_json_from_response(self):
"""测试从响应中提取JSON"""
with patch('claude_agent.core.agent.ClaudeSDKClient'):
agent = AgentCore()
# 测试带markdown代码块的JSON
response_with_markdown = "```json\n{\"test\": \"value\"}\n```"
result = agent._extract_json_from_response(response_with_markdown)
assert result == {"test": "value"}
# 测试纯JSON
response_plain = '{"test": "value"}'
result = agent._extract_json_from_response(response_plain)
assert result == {"test": "value"}
# 测试无效JSON
response_invalid = "这不是JSON"
result = agent._extract_json_from_response(response_invalid)
assert result is None
def test_add_mcp_tool(self):
"""测试添加MCP工具"""
with patch('claude_agent.core.agent.ClaudeSDKClient'):
agent = AgentCore()
tool_instance = Mock()
agent.add_mcp_tool("test_tool", tool_instance)
assert "test_tool" in agent.mcp_tools
assert agent.mcp_tools["test_tool"] == tool_instance
def test_conversation_history_management(self):
"""测试对话历史管理"""
with patch('claude_agent.core.agent.ClaudeSDKClient'):
agent = AgentCore()
# 添加一些历史记录
agent.conversation_history = [
{"role": "user", "content": "你好"},
{"role": "assistant", "content": "你好!"}
]
# 获取历史记录
history = agent.get_conversation_history()
assert len(history) == 2
assert history is not agent.conversation_history # 应该是副本
# 清空历史记录
agent.clear_history()
assert len(agent.conversation_history) == 0
assert len(agent.tasks) == 0
class TestTask:
"""Task数据类的测试"""
def test_task_creation(self):
"""测试任务创建"""
task = Task(id="test_1", description="测试任务")
assert task.id == "test_1"
assert task.description == "测试任务"
assert task.status == "pending"
assert task.result is None
assert task.subtasks == []
def test_task_with_subtasks(self):
"""测试带子任务的任务"""
subtask = Task(id="sub_1", description="子任务")
task = Task(
id="main_1",
description="主任务",
subtasks=[subtask]
)
assert len(task.subtasks) == 1
assert task.subtasks[0] == subtask