Commit 889743aa by sunqi02

test: add ToolManager unit tests (12 tests)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent db93fecb
from app.service.tool_manager import ToolManager
class TestToolManagerRegister:
def test_register_function(self):
tm = ToolManager()
def get_weather(city: str) -> str:
"""查询城市天气"""
return f"{city}: 晴天 25°C"
tm.register_tool(get_weather)
assert "get_weather" in tm._tools
assert tm._tools["get_weather"].group == "basic"
assert "查询城市天气" in tm._tools["get_weather"].description
def test_register_with_custom_name_and_group(self):
tm = ToolManager()
def f(x: str) -> str:
"""desc"""
return x
tm.register_tool(f, name="custom_name", group="custom_group", description="自定义描述")
assert "custom_name" in tm._tools
assert tm._tools["custom_name"].group == "custom_group"
assert tm._tools["custom_name"].description == "自定义描述"
assert "custom_group" in tm.groups
def test_register_decorator(self):
tm = ToolManager()
@tm.register(group="vision")
def analyze_image(image_url: str) -> str:
"""分析图片内容"""
return f"分析结果: {image_url}"
assert "analyze_image" in tm._tools
assert tm._tools["analyze_image"].group == "vision"
assert tm.groups["vision"].active is False # 非 basic 组默认未激活
schema = tm._tools["analyze_image"].input_schema
assert "image_url" in schema["properties"]
class TestToolManagerCall:
def test_call_tool_success(self):
tm = ToolManager()
def add(a: int, b: int) -> int:
return a + b
tm.register_tool(add)
result = tm.call_tool({"name": "add", "input": {"a": 1, "b": 2}})
assert result == {"result": 3}
def test_call_tool_not_found(self):
tm = ToolManager()
result = tm.call_tool({"name": "nonexistent", "input": {}})
assert "error" in result
assert "未注册" in result["error"]
def test_call_tool_inactive_group(self):
tm = ToolManager()
def secret() -> str:
return "secret"
tm.register_tool(secret, group="admin")
result = tm.call_tool({"name": "secret", "input": {}})
assert "error" in result
assert "未激活" in result["error"]
def test_call_tool_execution_error(self):
tm = ToolManager()
def crash() -> str:
raise ValueError("boom")
tm.register_tool(crash)
result = tm.call_tool({"name": "crash", "input": {}})
assert "error" in result
assert "boom" in result["error"]
class TestToolManagerSchemas:
def test_get_tool_schemas_only_active_groups(self):
tm = ToolManager()
def tool_a(x: str) -> str:
"""Tool A"""
return x
def tool_b(y: int) -> int:
"""Tool B"""
return y
tm.register_tool(tool_a, group="basic")
tm.register_tool(tool_b, group="admin")
schemas = tm.get_tool_schemas()
assert len(schemas) == 1
assert schemas[0]["function"]["name"] == "tool_a"
def test_get_tool_schemas_empty(self):
tm = ToolManager()
schemas = tm.get_tool_schemas()
assert schemas == []
def test_schema_format_openai_compatible(self):
tm = ToolManager()
def search(query: str, limit: int = 10) -> str:
"""搜索知识库"""
return f"results for {query}"
tm.register_tool(search)
schemas = tm.get_tool_schemas()
assert len(schemas) == 1
s = schemas[0]
assert s["type"] == "function"
assert s["function"]["name"] == "search"
assert s["function"]["description"] == "搜索知识库"
assert "query" in s["function"]["parameters"]["properties"]
assert s["function"]["parameters"]["required"] == ["query"]
class TestToolManagerGroups:
def test_reset_equipped_tools(self):
tm = ToolManager()
def tool_a(x: str) -> str:
"""A"""
return x
def tool_b(y: str) -> str:
"""B"""
return y
tm.register_tool(tool_a, group="basic")
tm.register_tool(tool_b, group="vision")
assert len(tm.get_tool_schemas()) == 1
tm.reset_equipped_tools(basic=False, vision=True)
schemas = tm.get_tool_schemas()
assert len(schemas) == 1
assert schemas[0]["function"]["name"] == "tool_b"
def test_default_groups(self):
tm = ToolManager()
assert "basic" in tm.groups
assert tm.groups["basic"].active is True
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!