|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import httpx |
| 6 | +import respx |
| 7 | +import pytest |
| 8 | + |
| 9 | +from agentex import Agentex, AsyncAgentex |
| 10 | +from agentex.lib.utils.metadata_filters import encode_metadata_filter |
| 11 | + |
| 12 | +BASE_URL = "http://127.0.0.1:4010" |
| 13 | +API_KEY = "My API Key" |
| 14 | + |
| 15 | + |
| 16 | +class TestEncodeMetadataFilter: |
| 17 | + def test_encodes_a_json_object(self) -> None: |
| 18 | + assert encode_metadata_filter({"permits_capable": True}) == '{"permits_capable":true}' |
| 19 | + |
| 20 | + def test_empty_mapping_encodes_to_an_empty_object(self) -> None: |
| 21 | + assert encode_metadata_filter({}) == "{}" |
| 22 | + |
| 23 | + def test_key_order_is_stable(self) -> None: |
| 24 | + assert ( |
| 25 | + encode_metadata_filter({"region": "us", "permits_capable": True}) |
| 26 | + == encode_metadata_filter({"permits_capable": True, "region": "us"}) |
| 27 | + == '{"permits_capable":true,"region":"us"}' |
| 28 | + ) |
| 29 | + |
| 30 | + def test_preserves_json_types_and_nesting(self) -> None: |
| 31 | + encoded = encode_metadata_filter({"flag": True, "count": 3, "ratio": 1.5, "nested": {"a": [1, "two", None]}}) |
| 32 | + assert json.loads(encoded) == { |
| 33 | + "flag": True, |
| 34 | + "count": 3, |
| 35 | + "ratio": 1.5, |
| 36 | + "nested": {"a": [1, "two", None]}, |
| 37 | + } |
| 38 | + |
| 39 | + @pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")]) |
| 40 | + def test_rejects_non_finite_floats(self, value: float) -> None: |
| 41 | + # The server rejects these with a 400; fail locally with a clearer message. |
| 42 | + with pytest.raises(ValueError, match="not encodable as JSON"): |
| 43 | + encode_metadata_filter({"x": value}) |
| 44 | + |
| 45 | + def test_rejects_a_non_mapping(self) -> None: |
| 46 | + with pytest.raises(TypeError, match="must be a mapping"): |
| 47 | + encode_metadata_filter([("permits_capable", True)]) # type: ignore[arg-type] |
| 48 | + |
| 49 | + def test_rejects_a_non_serializable_value(self) -> None: |
| 50 | + with pytest.raises(TypeError): |
| 51 | + encode_metadata_filter({"x": object()}) |
| 52 | + |
| 53 | + |
| 54 | +class TestAgentCardMetadataOnTheWire: |
| 55 | + """The encoded filter has to survive the client's query-string serialization. |
| 56 | +
|
| 57 | + The generated `agents.list` parameter is a plain `str` (the platform spec |
| 58 | + declares a JSON-encoded string, matching the shipped `task_metadata` |
| 59 | + filter), so these assert the exact query value the server will parse. |
| 60 | + """ |
| 61 | + |
| 62 | + @respx.mock(base_url=BASE_URL) |
| 63 | + def test_sync_client_sends_the_encoded_object(self, respx_mock: respx.MockRouter) -> None: |
| 64 | + route = respx_mock.get("/agents").mock(return_value=httpx.Response(200, json=[])) |
| 65 | + |
| 66 | + with Agentex(base_url=BASE_URL, api_key=API_KEY, _strict_response_validation=True) as client: |
| 67 | + client.agents.list( |
| 68 | + agent_card_metadata=encode_metadata_filter({"permits_capable": True, "region": "us"}), |
| 69 | + limit=5, |
| 70 | + ) |
| 71 | + |
| 72 | + params = route.calls.last.request.url.params |
| 73 | + raw = params["agent_card_metadata"] |
| 74 | + assert raw == '{"permits_capable":true,"region":"us"}' |
| 75 | + assert json.loads(raw) == {"permits_capable": True, "region": "us"} |
| 76 | + assert params["limit"] == "5" |
| 77 | + |
| 78 | + @respx.mock(base_url=BASE_URL) |
| 79 | + async def test_async_client_sends_the_encoded_object(self, respx_mock: respx.MockRouter) -> None: |
| 80 | + route = respx_mock.get("/agents").mock(return_value=httpx.Response(200, json=[])) |
| 81 | + |
| 82 | + async with AsyncAgentex(base_url=BASE_URL, api_key=API_KEY, _strict_response_validation=True) as client: |
| 83 | + await client.agents.list( |
| 84 | + agent_card_metadata=encode_metadata_filter({"permits_capable": True, "region": "us"}), |
| 85 | + limit=5, |
| 86 | + ) |
| 87 | + |
| 88 | + params = route.calls.last.request.url.params |
| 89 | + raw = params["agent_card_metadata"] |
| 90 | + assert raw == '{"permits_capable":true,"region":"us"}' |
| 91 | + assert json.loads(raw) == {"permits_capable": True, "region": "us"} |
| 92 | + assert params["limit"] == "5" |
| 93 | + |
| 94 | + @respx.mock(base_url=BASE_URL) |
| 95 | + def test_omitted_filter_is_absent_from_the_query(self, respx_mock: respx.MockRouter) -> None: |
| 96 | + route = respx_mock.get("/agents").mock(return_value=httpx.Response(200, json=[])) |
| 97 | + |
| 98 | + with Agentex(base_url=BASE_URL, api_key=API_KEY, _strict_response_validation=True) as client: |
| 99 | + client.agents.list() |
| 100 | + |
| 101 | + assert "agent_card_metadata" not in route.calls.last.request.url.params |
| 102 | + |
| 103 | + @respx.mock(base_url=BASE_URL) |
| 104 | + def test_empty_object_filter_is_sent_verbatim(self, respx_mock: respx.MockRouter) -> None: |
| 105 | + """`{}` is a meaningful filter server-side (agent must have card metadata), |
| 106 | + so it must reach the wire rather than being dropped as falsy.""" |
| 107 | + route = respx_mock.get("/agents").mock(return_value=httpx.Response(200, json=[])) |
| 108 | + |
| 109 | + with Agentex(base_url=BASE_URL, api_key=API_KEY, _strict_response_validation=True) as client: |
| 110 | + client.agents.list(agent_card_metadata=encode_metadata_filter({})) |
| 111 | + |
| 112 | + assert route.calls.last.request.url.params["agent_card_metadata"] == "{}" |
0 commit comments