test_agent_tools.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Unit tests for agent tool validation."""
  2. from typing import get_args, get_origin
  3. import pytest
  4. from agents import ComputerTool, FunctionTool, Tool
  5. from agency_swarm.agent.tools import validate_hosted_tools, validate_tools
  6. from agency_swarm.tools import BaseTool
  7. def _hosted_tool_types() -> list[object]:
  8. hosted_tools: list[object] = []
  9. for tool_type in get_args(Tool):
  10. tool_class = get_origin(tool_type) or tool_type
  11. if tool_class is FunctionTool:
  12. continue
  13. hosted_tools.append(tool_type)
  14. return hosted_tools
  15. def test_validate_hosted_tools_rejects_uninitialized_hosted_tool_classes() -> None:
  16. """All hosted tool classes must be instantiated before validation."""
  17. for tool_type in _hosted_tool_types():
  18. with pytest.raises(TypeError):
  19. validate_hosted_tools([tool_type])
  20. def test_validate_tools_rejects_invalid_entries() -> None:
  21. """FunctionTool classes, invalid objects, and BaseTool instances should fail validation."""
  22. class SampleTool(BaseTool):
  23. def run(self) -> str:
  24. return "ok"
  25. invalid_cases: list[list[object]] = [
  26. [FunctionTool],
  27. [object()],
  28. [SampleTool()],
  29. ]
  30. for tools in invalid_cases:
  31. with pytest.raises(TypeError):
  32. validate_tools(tools)
  33. def test_validate_tools_accepts_supported_entries() -> None:
  34. """BaseTool classes and initialized hosted tools should pass validation."""
  35. class SampleTool(BaseTool):
  36. def run(self) -> str:
  37. return "ok"
  38. validate_tools([SampleTool])
  39. validate_hosted_tools([ComputerTool(computer=object())])