"""Tests for tool error detection.""" from __future__ import annotations from supply_agent.tools.base import tool from supply_agent.tools.errors import content_indicates_error from supply_agent.tools.registry import ToolRegistry def test_content_indicates_error_truthy_error_field() -> None: assert content_indicates_error('{"error": "boom"}') assert content_indicates_error('{\n "error": "boom",\n "title": "x"\n}') def test_content_indicates_error_ignores_null_or_missing() -> None: assert not content_indicates_error('{"error": null, "ok": true}') assert not content_indicates_error('{"error": "", "ok": true}') assert not content_indicates_error('{"ok": true}') assert not content_indicates_error("plain text success") assert not content_indicates_error('["not", "an", "object"]') def test_content_indicates_error_does_not_use_prefix_heuristic() -> None: # Would be true under startswith('{"error"'), but is a success payload. assert not content_indicates_error('{"error_code": 0, "message": "ok"}') def test_registry_execute_sets_is_error_from_json() -> None: registry = ToolRegistry() @tool(name="fail") def fail() -> str: return '{\n "error": "bad args",\n "input_error": true\n}' @tool(name="ok") def ok() -> str: return '{"error": null, "items": []}' registry.register(fail, name="fail") registry.register(ok, name="ok") assert registry.execute("1", "fail", "{}").is_error is True assert registry.execute("2", "ok", "{}").is_error is False assert registry.execute("3", "missing", "{}").is_error is True