"""工具集成测试."""
import pytest
from src.tools import get_tools, handle_tool_call
from src.config_manager import ConfigManager
from src.graphiti_client import GraphitiClient
@pytest.mark.integration
class TestTools:
"""工具集成测试类."""
def test_get_tools(self, config_manager):
"""测试获取工具列表."""
tools = get_tools(config_manager)
assert len(tools) > 0
tool_names = [tool.name for tool in tools]
assert "configure_neo4j" in tool_names
assert "add_episode" in tool_names
assert "search_entities" in tool_names
@pytest.mark.asyncio
async def test_handle_configure_neo4j(self, config_manager):
"""测试处理配置 Neo4j 工具."""
result = await handle_tool_call(
tool_name="configure_neo4j",
arguments={
"uri": "bolt://localhost:7687",
"username": "neo4j",
# 使用与实际 Neo4j 容器一致的密码,避免错误配置
"password": "password",
},
config_manager=config_manager
)
assert len(result) > 0
assert "✅" in result[0].text or "成功" in result[0].text
@pytest.mark.asyncio
async def test_handle_check_configuration(self, config_manager):
"""测试处理检查配置工具."""
result = await handle_tool_call(
tool_name="check_configuration",
arguments={},
config_manager=config_manager
)
assert len(result) > 0
@pytest.mark.asyncio
async def test_handle_reset_configuration(self, config_manager):
"""测试处理重置配置工具."""
result = await handle_tool_call(
tool_name="reset_configuration",
arguments={},
config_manager=config_manager
)
assert len(result) > 0
@pytest.mark.asyncio
async def test_handle_set_group_id(self, config_manager):
"""测试处理设置组 ID 工具."""
result = await handle_tool_call(
tool_name="set_group_id",
arguments={"group_id": "test_group"},
config_manager=config_manager
)
assert len(result) > 0
@pytest.mark.asyncio
async def test_handle_get_cache_stats(self):
"""测试处理获取缓存统计工具."""
result = await handle_tool_call(
tool_name="get_cache_stats",
arguments={},
config_manager=ConfigManager()
)
assert len(result) > 0
assert "缓存统计" in result[0].text or "Cache" in result[0].text
@pytest.mark.asyncio
async def test_handle_unknown_tool(self, config_manager):
"""测试处理未知工具."""
result = await handle_tool_call(
tool_name="unknown_tool",
arguments={},
config_manager=config_manager
)
assert len(result) > 0
assert "未知工具" in result[0].text or "unknown" in result[0].text.lower()
def test_handle_tool_with_invalid_args(self, config_manager):
"""测试处理工具时参数错误."""
try:
result = handle_tool_call(
tool_name="configure_neo4j",
arguments={}, # 缺少必需参数
config_manager=config_manager
)
# 应该返回错误信息
assert len(result) > 0
except Exception:
# 或者抛出异常
pass