Files
nanobot/tests/test_registry_duck_typing.py
code-server 6adefbf190 feat(tools): add duck typing support to ToolRegistry
Registry now supports both function tools (to_schema) and native
tools (to_params) via hasattr checks. Enables mixed tool types.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-28 23:43:02 +00:00

95 lines
2.8 KiB
Python

"""Tests for registry duck typing support."""
import pytest
from nanobot.agent.tools.registry import ToolRegistry
from nanobot.agent.tools.anthropic.base import BaseAnthropicTool, ToolResult
class MockNativeTool(BaseAnthropicTool):
"""Mock native tool for testing."""
api_type = "test_20250227"
name = "native_test"
beta_flag = "test-beta"
async def __call__(self, **kwargs):
return ToolResult(output="native result")
def to_params(self):
return {"type": self.api_type, "name": self.name}
class MockFunctionTool:
"""Mock function tool for testing."""
def __init__(self):
self.name = "function_test"
def to_schema(self):
return {
"type": "function",
"function": {
"name": self.name,
"description": "Test function tool",
"parameters": {"type": "object", "properties": {}}
}
}
async def execute(self, **kwargs):
return "function result"
def test_registry_supports_native_tools():
"""Test registry can register and get definitions from native tools."""
registry = ToolRegistry()
native_tool = MockNativeTool()
registry.register(native_tool)
definitions = registry.get_definitions()
assert len(definitions) == 1
assert definitions[0]["type"] == "test_20250227"
assert definitions[0]["name"] == "native_test"
def test_registry_supports_function_tools():
"""Test registry still supports function tools."""
registry = ToolRegistry()
function_tool = MockFunctionTool()
registry.register(function_tool)
definitions = registry.get_definitions()
assert len(definitions) == 1
assert definitions[0]["type"] == "function"
assert definitions[0]["function"]["name"] == "function_test"
def test_registry_supports_mixed_tools():
"""Test registry can handle both native and function tools."""
registry = ToolRegistry()
native_tool = MockNativeTool()
function_tool = MockFunctionTool()
registry.register(native_tool)
registry.register(function_tool)
definitions = registry.get_definitions()
assert len(definitions) == 2
# Find each tool type in definitions
native_def = next(d for d in definitions if d.get("type") == "test_20250227")
function_def = next(d for d in definitions if d.get("type") == "function")
assert native_def["name"] == "native_test"
assert function_def["function"]["name"] == "function_test"
def test_registry_rejects_tools_without_schema_method():
"""Test registry raises error for tools with no schema method."""
registry = ToolRegistry()
class BadTool:
name = "bad"
registry.register(BadTool())
with pytest.raises(ValueError, match="has no schema method"):
registry.get_definitions()