|
| 1 | +"""Tests for the ClientFactory.""" |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +from a2a.client import ClientConfig, ClientFactory |
| 7 | +from a2a.client.transports import JsonRpcTransport, RestTransport |
| 8 | +from a2a.types import ( |
| 9 | + AgentCard, |
| 10 | + AgentCapabilities, |
| 11 | + AgentInterface, |
| 12 | + TransportProtocol, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def base_agent_card() -> AgentCard: |
| 18 | + """Provides a base AgentCard for tests.""" |
| 19 | + return AgentCard( |
| 20 | + name="Test Agent", |
| 21 | + description="An agent for testing.", |
| 22 | + url="http://primary-url.com", |
| 23 | + version="1.0.0", |
| 24 | + capabilities=AgentCapabilities(), |
| 25 | + skills=[], |
| 26 | + default_input_modes=[], |
| 27 | + default_output_modes=[], |
| 28 | + preferred_transport=TransportProtocol.jsonrpc, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_client_factory_selects_preferred_transport(base_agent_card: AgentCard): |
| 33 | + """Verify that the factory selects the preferred transport by default.""" |
| 34 | + config = ClientConfig( |
| 35 | + httpx_client=httpx.AsyncClient(), |
| 36 | + supported_transports=[TransportProtocol.jsonrpc, TransportProtocol.http_json], |
| 37 | + ) |
| 38 | + factory = ClientFactory(config) |
| 39 | + client = factory.create(base_agent_card) |
| 40 | + |
| 41 | + assert isinstance(client._transport, JsonRpcTransport) |
| 42 | + assert client._transport.url == "http://primary-url.com" |
| 43 | + |
| 44 | + |
| 45 | +def test_client_factory_selects_secondary_transport_url(base_agent_card: AgentCard): |
| 46 | + """Verify that the factory selects the correct URL for a secondary transport.""" |
| 47 | + base_agent_card.additional_interfaces = [ |
| 48 | + AgentInterface( |
| 49 | + transport=TransportProtocol.http_json, url="http://secondary-url.com" |
| 50 | + ) |
| 51 | + ] |
| 52 | + # Client prefers REST, which is available as a secondary transport |
| 53 | + config = ClientConfig( |
| 54 | + httpx_client=httpx.AsyncClient(), |
| 55 | + supported_transports=[TransportProtocol.http_json, TransportProtocol.jsonrpc], |
| 56 | + use_client_preference=True, |
| 57 | + ) |
| 58 | + factory = ClientFactory(config) |
| 59 | + client = factory.create(base_agent_card) |
| 60 | + |
| 61 | + assert isinstance(client._transport, RestTransport) |
| 62 | + assert client._transport.url == "http://secondary-url.com" |
| 63 | + |
| 64 | + |
| 65 | +def test_client_factory_server_preference(base_agent_card: AgentCard): |
| 66 | + """Verify that the factory respects server transport preference.""" |
| 67 | + base_agent_card.preferred_transport = TransportProtocol.http_json |
| 68 | + base_agent_card.additional_interfaces = [ |
| 69 | + AgentInterface( |
| 70 | + transport=TransportProtocol.jsonrpc, url="http://secondary-url.com" |
| 71 | + ) |
| 72 | + ] |
| 73 | + # Client supports both, but server prefers REST |
| 74 | + config = ClientConfig( |
| 75 | + httpx_client=httpx.AsyncClient(), |
| 76 | + supported_transports=[TransportProtocol.jsonrpc, TransportProtocol.http_json], |
| 77 | + ) |
| 78 | + factory = ClientFactory(config) |
| 79 | + client = factory.create(base_agent_card) |
| 80 | + |
| 81 | + assert isinstance(client._transport, RestTransport) |
| 82 | + assert client._transport.url == "http://primary-url.com" |
| 83 | + |
| 84 | + |
| 85 | +def test_client_factory_no_compatible_transport(base_agent_card: AgentCard): |
| 86 | + """Verify that the factory raises an error if no compatible transport is found.""" |
| 87 | + config = ClientConfig( |
| 88 | + httpx_client=httpx.AsyncClient(), |
| 89 | + supported_transports=[TransportProtocol.grpc], |
| 90 | + ) |
| 91 | + factory = ClientFactory(config) |
| 92 | + with pytest.raises(ValueError, match="no compatible transports found"): |
| 93 | + factory.create(base_agent_card) |
0 commit comments