|
| 1 | +from unittest.mock import AsyncMock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from a2a.client import ( |
| 6 | + Client, |
| 7 | + ClientConfig, |
| 8 | + ClientCallContext, |
| 9 | + create_text_client, |
| 10 | + minimal_agent_card, |
| 11 | + TextClient, |
| 12 | +) |
| 13 | +from a2a.types import Part, StreamResponse |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_client() -> AsyncMock: |
| 18 | + return AsyncMock(spec=Client) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def text_client(mock_client: AsyncMock) -> TextClient: |
| 23 | + return TextClient(mock_client) |
| 24 | + |
| 25 | + |
| 26 | +def test_client_property( |
| 27 | + text_client: TextClient, mock_client: AsyncMock |
| 28 | +) -> None: |
| 29 | + assert text_client.client is mock_client |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_create_client_and_wrap() -> None: |
| 34 | + # Create a minimal card |
| 35 | + card = minimal_agent_card(url='http://test.com', transports=['JSONRPC']) |
| 36 | + |
| 37 | + config = ClientConfig(supported_protocol_bindings=['JSONRPC']) |
| 38 | + |
| 39 | + text_client = await create_text_client(card, client_config=config) |
| 40 | + |
| 41 | + assert isinstance(text_client, TextClient) |
| 42 | + assert isinstance(text_client.client, Client) |
| 43 | + |
| 44 | + # Clean up |
| 45 | + await text_client.close() |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.asyncio |
| 49 | +async def test_send_text_message( |
| 50 | + text_client: TextClient, mock_client: AsyncMock |
| 51 | +) -> None: |
| 52 | + async def create_stream(*args, **kwargs): |
| 53 | + # Event 0: task (ignored) |
| 54 | + resp0 = StreamResponse() |
| 55 | + resp0.task.id = 'task-1' |
| 56 | + yield resp0 |
| 57 | + |
| 58 | + # Event 1: direct message |
| 59 | + resp1 = StreamResponse() |
| 60 | + resp1.message.parts.append(Part(text='Hello')) |
| 61 | + yield resp1 |
| 62 | + |
| 63 | + # Event 2: status update without message |
| 64 | + resp2 = StreamResponse() |
| 65 | + resp2.status_update.status.state = 1 |
| 66 | + yield resp2 |
| 67 | + |
| 68 | + # Event 3: status update with message |
| 69 | + resp3 = StreamResponse() |
| 70 | + resp3.status_update.status.message.parts.append(Part(text='Processing')) |
| 71 | + yield resp3 |
| 72 | + |
| 73 | + # Event 4: artifact update |
| 74 | + resp4 = StreamResponse() |
| 75 | + resp4.artifact_update.artifact.parts.append(Part(text='World!')) |
| 76 | + yield resp4 |
| 77 | + |
| 78 | + mock_client.send_message.return_value = create_stream() |
| 79 | + |
| 80 | + response = await text_client.send_text_message('Hi') |
| 81 | + |
| 82 | + assert response == 'Hello Processing World!' |
| 83 | + mock_client.send_message.assert_called_once() |
| 84 | + # Verify request construction |
| 85 | + args, _ = mock_client.send_message.call_args |
| 86 | + request = args[0] |
| 87 | + assert request.message.parts[0].text == 'Hi' |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +async def test_send_text_message_forwards_context( |
| 92 | + text_client: TextClient, mock_client: AsyncMock |
| 93 | +) -> None: |
| 94 | + |
| 95 | + async def empty_stream(*args, **kwargs): |
| 96 | + return |
| 97 | + yield |
| 98 | + |
| 99 | + mock_client.send_message.return_value = empty_stream() |
| 100 | + context = ClientCallContext() |
| 101 | + |
| 102 | + await text_client.send_text_message('Hi', context=context) |
| 103 | + |
| 104 | + _, kwargs = mock_client.send_message.call_args |
| 105 | + assert kwargs['context'] is context |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +async def test_close(text_client: TextClient, mock_client: AsyncMock) -> None: |
| 110 | + await text_client.close() |
| 111 | + mock_client.close.assert_awaited_once() |
0 commit comments