test_tool_errors.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Tests for tool error detection."""
  2. from __future__ import annotations
  3. from supply_agent.tools.base import tool
  4. from supply_agent.tools.errors import content_indicates_error
  5. from supply_agent.tools.registry import ToolRegistry
  6. def test_content_indicates_error_truthy_error_field() -> None:
  7. assert content_indicates_error('{"error": "boom"}')
  8. assert content_indicates_error('{\n "error": "boom",\n "title": "x"\n}')
  9. def test_content_indicates_error_ignores_null_or_missing() -> None:
  10. assert not content_indicates_error('{"error": null, "ok": true}')
  11. assert not content_indicates_error('{"error": "", "ok": true}')
  12. assert not content_indicates_error('{"ok": true}')
  13. assert not content_indicates_error("plain text success")
  14. assert not content_indicates_error('["not", "an", "object"]')
  15. def test_content_indicates_error_does_not_use_prefix_heuristic() -> None:
  16. # Would be true under startswith('{"error"'), but is a success payload.
  17. assert not content_indicates_error('{"error_code": 0, "message": "ok"}')
  18. def test_registry_execute_sets_is_error_from_json() -> None:
  19. registry = ToolRegistry()
  20. @tool(name="fail")
  21. def fail() -> str:
  22. return '{\n "error": "bad args",\n "input_error": true\n}'
  23. @tool(name="ok")
  24. def ok() -> str:
  25. return '{"error": null, "items": []}'
  26. registry.register(fail, name="fail")
  27. registry.register(ok, name="ok")
  28. assert registry.execute("1", "fail", "{}").is_error is True
  29. assert registry.execute("2", "ok", "{}").is_error is False
  30. assert registry.execute("3", "missing", "{}").is_error is True