Files
barsic/tests/test_tools_registry.py
2026-11-07 22:34:12 +03:00

65 lines
1.9 KiB
Python

"""Unit tests for tool definition, schema generation, and registry."""
import pytest
from homeagent.tools.base import _python_type_to_schema, tool
from homeagent.tools.registry import ToolRegistry
def test_type_to_schema_mapping():
assert _python_type_to_schema(str) == {"type": "string"}
assert _python_type_to_schema(int) == {"type": "integer"}
assert _python_type_to_schema(float) == {"type": "number"}
assert _python_type_to_schema(bool) == {"type": "boolean"}
assert _python_type_to_schema(list[str]) == {
"type": "array",
"items": {"type": "string"},
}
def test_tool_decorator_schema_and_execution():
@tool(name="calculate_sum", description="Add two numbers")
def calculate_sum(a: int, b: int = 10) -> int:
"""Add two numbers.
Args:
a: First number
b: Second number
"""
return a + b
tool_def = getattr(calculate_sum, "__tool_definition__")
assert tool_def.name == "calculate_sum"
assert tool_def.description == "Add two numbers"
assert not tool_def.is_async
schema = tool_def.parameters_schema
assert schema["type"] == "object"
assert "a" in schema["properties"]
assert "b" in schema["properties"]
assert schema["properties"]["a"]["type"] == "integer"
assert schema["properties"]["a"]["description"] == "First number"
assert schema["required"] == ["a"] # b has default
@pytest.mark.asyncio
async def test_async_tool_execution():
@tool(description="Async greeting")
async def greet(name: str) -> str:
"""Greet a user.
Args:
name: User name
"""
return f"Hello, {name}!"
registry = ToolRegistry()
registry.register(greet)
res = await registry.execute("greet", {"name": "Alice"})
assert res == "Hello, Alice!"
# Gemini function declarations
decls = registry.get_function_declarations()
assert len(decls) == 1
assert decls[0].name == "greet"