"""覆盖 ACE 相关 MCP 工具在 Graphiti 未初始化 / ACE 未启用时的错误分支."""
import pytest
from unittest.mock import Mock
from src.tools import handle_tool_call
from src.config_manager import ConfigManager
from src.graphiti_client import GraphitiClient
class TestToolsAceErrorBranches:
"""确保各 ACE 工具在前置依赖缺失时给出明确错误信息."""
@pytest.fixture
def config_manager(self, temp_config_dir):
"""为每个用例提供独立的 ConfigManager."""
return ConfigManager(config_path=temp_config_dir / ".graphitiace" / "config.json")
@pytest.mark.asyncio
@pytest.mark.parametrize(
"tool_name",
[
"query_strategies",
"list_strategy_versions",
"get_learning_trends",
"bulk_update_strategies",
"bulk_export_strategies",
"get_strategy_alerts",
"get_strategy_stats",
"export_strategies",
"import_strategies",
"toggle_strategy",
"validate_strategies",
"render_strategy_insights",
],
)
async def test_ace_tools_graphiti_not_initialized(self, config_manager, tool_name):
"""Graphiti 客户端为 None 时,应直接返回未初始化错误."""
# graphiti_client=None 触发 tools.py 中的首个分支
result = await handle_tool_call(
tool_name=tool_name,
arguments={},
config_manager=config_manager,
graphiti_client=None,
ace_manager=Mock(),
)
text = result[0].text
assert "Graphiti 客户端未初始化" in text
@pytest.mark.asyncio
@pytest.mark.parametrize(
"tool_name",
[
"query_strategies",
"list_strategy_versions",
"get_learning_trends",
"bulk_update_strategies",
"bulk_export_strategies",
"get_strategy_alerts",
"get_strategy_stats",
"export_strategies",
"import_strategies",
"toggle_strategy",
"validate_strategies",
"render_strategy_insights",
"rate_result",
],
)
async def test_ace_tools_manager_disabled(self, config_manager, tool_name):
"""ACE Manager 未启用时,应返回统一的错误提示."""
graphiti_client = Mock(spec=GraphitiClient)
graphiti_client.is_connected.return_value = True
ace_manager = Mock()
ace_manager.is_enabled.return_value = False
result = await handle_tool_call(
tool_name=tool_name,
arguments={},
config_manager=config_manager,
graphiti_client=graphiti_client,
ace_manager=ace_manager,
)
text = result[0].text
assert "ACE Manager 未启用" in text