|
| 1 | +"""Unit tests for src/agents/_tool_identity.py pure helpers. |
| 2 | +
|
| 3 | +These cover the small, pure functions in `_tool_identity` that build / |
| 4 | +parse function-tool lookup keys and trace names. The module had no |
| 5 | +direct test file even though it's imported across the runner, tracing, |
| 6 | +and tool-output trimmer code paths. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from agents._tool_identity import ( |
| 14 | + deserialize_function_tool_lookup_key, |
| 15 | + get_function_tool_lookup_key, |
| 16 | + get_tool_call_name, |
| 17 | + get_tool_call_namespace, |
| 18 | + get_tool_call_qualified_name, |
| 19 | + get_tool_call_trace_name, |
| 20 | + is_reserved_synthetic_tool_namespace, |
| 21 | + serialize_function_tool_lookup_key, |
| 22 | + tool_qualified_name, |
| 23 | + tool_trace_name, |
| 24 | +) |
| 25 | +from agents.exceptions import UserError |
| 26 | + |
| 27 | + |
| 28 | +class TestToolQualifiedName: |
| 29 | + def test_returns_name_when_no_namespace(self) -> None: |
| 30 | + assert tool_qualified_name("search") == "search" |
| 31 | + |
| 32 | + def test_returns_dotted_when_namespace_provided(self) -> None: |
| 33 | + assert tool_qualified_name("search", "tools") == "tools.search" |
| 34 | + |
| 35 | + def test_returns_none_for_empty_name(self) -> None: |
| 36 | + assert tool_qualified_name("") is None |
| 37 | + assert tool_qualified_name(None) is None |
| 38 | + |
| 39 | + def test_returns_none_for_non_string_name(self) -> None: |
| 40 | + assert tool_qualified_name(123) is None # type: ignore[arg-type] |
| 41 | + |
| 42 | + def test_ignores_empty_namespace(self) -> None: |
| 43 | + assert tool_qualified_name("search", "") == "search" |
| 44 | + assert tool_qualified_name("search", None) == "search" |
| 45 | + |
| 46 | + |
| 47 | +class TestIsReservedSyntheticToolNamespace: |
| 48 | + def test_true_when_name_equals_namespace(self) -> None: |
| 49 | + assert is_reserved_synthetic_tool_namespace("search", "search") is True |
| 50 | + |
| 51 | + def test_false_when_different(self) -> None: |
| 52 | + assert is_reserved_synthetic_tool_namespace("search", "tools") is False |
| 53 | + |
| 54 | + def test_false_when_either_missing(self) -> None: |
| 55 | + assert is_reserved_synthetic_tool_namespace("", "") is False |
| 56 | + assert is_reserved_synthetic_tool_namespace("search", None) is False |
| 57 | + assert is_reserved_synthetic_tool_namespace(None, "search") is False |
| 58 | + |
| 59 | + |
| 60 | +class TestToolTraceName: |
| 61 | + def test_collapses_synthetic_namespace(self) -> None: |
| 62 | + # When namespace == name, trace name is just the bare name. |
| 63 | + assert tool_trace_name("search", "search") == "search" |
| 64 | + |
| 65 | + def test_qualifies_real_namespace(self) -> None: |
| 66 | + assert tool_trace_name("search", "tools") == "tools.search" |
| 67 | + |
| 68 | + def test_returns_bare_when_no_namespace(self) -> None: |
| 69 | + assert tool_trace_name("search", None) == "search" |
| 70 | + |
| 71 | + |
| 72 | +class TestToolCallExtractors: |
| 73 | + def test_get_tool_call_name_from_dict(self) -> None: |
| 74 | + assert get_tool_call_name({"name": "search"}) == "search" |
| 75 | + |
| 76 | + def test_get_tool_call_name_from_object(self) -> None: |
| 77 | + class Call: |
| 78 | + name = "search" |
| 79 | + |
| 80 | + assert get_tool_call_name(Call()) == "search" |
| 81 | + |
| 82 | + def test_get_tool_call_name_returns_none_for_empty(self) -> None: |
| 83 | + assert get_tool_call_name({"name": ""}) is None |
| 84 | + assert get_tool_call_name({}) is None |
| 85 | + assert get_tool_call_name({"name": 123}) is None |
| 86 | + |
| 87 | + def test_get_tool_call_namespace(self) -> None: |
| 88 | + assert get_tool_call_namespace({"namespace": "tools"}) == "tools" |
| 89 | + assert get_tool_call_namespace({"namespace": ""}) is None |
| 90 | + assert get_tool_call_namespace({}) is None |
| 91 | + |
| 92 | + def test_get_tool_call_qualified_name_with_namespace(self) -> None: |
| 93 | + call = {"name": "search", "namespace": "tools"} |
| 94 | + assert get_tool_call_qualified_name(call) == "tools.search" |
| 95 | + |
| 96 | + def test_get_tool_call_qualified_name_without_namespace(self) -> None: |
| 97 | + assert get_tool_call_qualified_name({"name": "search"}) == "search" |
| 98 | + |
| 99 | + def test_get_tool_call_trace_name_collapses_synthetic_namespace(self) -> None: |
| 100 | + call = {"name": "search", "namespace": "search"} |
| 101 | + assert get_tool_call_trace_name(call) == "search" |
| 102 | + |
| 103 | + def test_get_tool_call_trace_name_qualifies_real_namespace(self) -> None: |
| 104 | + call = {"name": "search", "namespace": "tools"} |
| 105 | + assert get_tool_call_trace_name(call) == "tools.search" |
| 106 | + |
| 107 | + |
| 108 | +class TestGetFunctionToolLookupKey: |
| 109 | + def test_bare_when_no_namespace(self) -> None: |
| 110 | + assert get_function_tool_lookup_key("search") == ("bare", "search") |
| 111 | + |
| 112 | + def test_namespaced_when_namespace_present(self) -> None: |
| 113 | + assert get_function_tool_lookup_key("search", "tools") == ( |
| 114 | + "namespaced", |
| 115 | + "tools", |
| 116 | + "search", |
| 117 | + ) |
| 118 | + |
| 119 | + def test_deferred_top_level_when_namespace_equals_name(self) -> None: |
| 120 | + assert get_function_tool_lookup_key("search", "search") == ( |
| 121 | + "deferred_top_level", |
| 122 | + "search", |
| 123 | + ) |
| 124 | + |
| 125 | + def test_returns_none_for_empty_name(self) -> None: |
| 126 | + assert get_function_tool_lookup_key("") is None |
| 127 | + assert get_function_tool_lookup_key(None) is None |
| 128 | + |
| 129 | + |
| 130 | +class TestSerializeRoundTrip: |
| 131 | + @pytest.mark.parametrize( |
| 132 | + "lookup_key", |
| 133 | + [ |
| 134 | + ("bare", "search"), |
| 135 | + ("namespaced", "tools", "search"), |
| 136 | + ("deferred_top_level", "search"), |
| 137 | + ], |
| 138 | + ) |
| 139 | + def test_roundtrip(self, lookup_key) -> None: |
| 140 | + serialized = serialize_function_tool_lookup_key(lookup_key) |
| 141 | + assert serialized is not None |
| 142 | + assert deserialize_function_tool_lookup_key(serialized) == lookup_key |
| 143 | + |
| 144 | + def test_serialize_none_returns_none(self) -> None: |
| 145 | + assert serialize_function_tool_lookup_key(None) is None |
| 146 | + |
| 147 | + def test_deserialize_invalid_returns_none(self) -> None: |
| 148 | + assert deserialize_function_tool_lookup_key(None) is None |
| 149 | + assert deserialize_function_tool_lookup_key({}) is None |
| 150 | + assert deserialize_function_tool_lookup_key({"kind": "bare"}) is None |
| 151 | + assert deserialize_function_tool_lookup_key({"kind": "bare", "name": ""}) is None |
| 152 | + assert deserialize_function_tool_lookup_key({"kind": "unknown", "name": "x"}) is None |
| 153 | + # namespaced kind requires a non-empty namespace |
| 154 | + assert deserialize_function_tool_lookup_key({"kind": "namespaced", "name": "x"}) is None |
| 155 | + |
| 156 | + |
| 157 | +def test_validate_function_tool_namespace_shape_rejects_synthetic() -> None: |
| 158 | + """The internal validator must refuse synthetic name==namespace shapes.""" |
| 159 | + from agents._tool_identity import validate_function_tool_namespace_shape |
| 160 | + |
| 161 | + # Valid shapes don't raise. |
| 162 | + validate_function_tool_namespace_shape("search", "tools") |
| 163 | + validate_function_tool_namespace_shape("search", None) |
| 164 | + |
| 165 | + # The reserved synthetic shape (name == namespace) is rejected. |
| 166 | + with pytest.raises(UserError, match="reserves the synthetic namespace"): |
| 167 | + validate_function_tool_namespace_shape("search", "search") |
0 commit comments