-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathtest_grpc_handler.py
More file actions
427 lines (359 loc) · 13.5 KB
/
test_grpc_handler.py
File metadata and controls
427 lines (359 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
from unittest.mock import AsyncMock, MagicMock
import grpc
import grpc.aio
import pytest
from a2a import types
from a2a.extensions.common import HTTP_EXTENSION_HEADER
from a2a.grpc import a2a_pb2
from a2a.server.context import ServerCallContext
from a2a.server.request_handlers import GrpcHandler, RequestHandler
from a2a.utils.errors import ServerError
# --- Fixtures ---
@pytest.fixture
def mock_request_handler() -> AsyncMock:
return AsyncMock(spec=RequestHandler)
@pytest.fixture
def mock_grpc_context() -> AsyncMock:
context = AsyncMock(spec=grpc.aio.ServicerContext)
context.abort = AsyncMock()
context.set_trailing_metadata = MagicMock()
return context
@pytest.fixture
def sample_agent_card() -> types.AgentCard:
return types.AgentCard(
name='Test Agent',
description='A test agent',
url='http://localhost',
version='1.0.0',
capabilities=types.AgentCapabilities(
streaming=True, push_notifications=True
),
default_input_modes=['text/plain'],
default_output_modes=['text/plain'],
skills=[],
)
@pytest.fixture
def grpc_handler(
mock_request_handler: AsyncMock, sample_agent_card: types.AgentCard
) -> GrpcHandler:
return GrpcHandler(
agent_card=sample_agent_card, request_handler=mock_request_handler
)
# --- Test Cases ---
@pytest.mark.asyncio
async def test_send_message_success(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test successful SendMessage call."""
request_proto = a2a_pb2.SendMessageRequest(
request=a2a_pb2.Message(message_id='msg-1')
)
response_model = types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.completed),
)
mock_request_handler.on_message_send.return_value = response_model
response = await grpc_handler.SendMessage(request_proto, mock_grpc_context)
mock_request_handler.on_message_send.assert_awaited_once()
assert isinstance(response, a2a_pb2.SendMessageResponse)
assert response.HasField('task')
assert response.task.id == 'task-1'
@pytest.mark.asyncio
async def test_send_message_server_error(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test SendMessage call when handler raises a ServerError."""
request_proto = a2a_pb2.SendMessageRequest()
error = ServerError(error=types.InvalidParamsError(message='Bad params'))
mock_request_handler.on_message_send.side_effect = error
await grpc_handler.SendMessage(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once_with(
grpc.StatusCode.INVALID_ARGUMENT, 'InvalidParamsError: Bad params'
)
@pytest.mark.asyncio
async def test_get_task_success(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test successful GetTask call."""
request_proto = a2a_pb2.GetTaskRequest(name='tasks/task-1')
response_model = types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.working),
)
mock_request_handler.on_get_task.return_value = response_model
response = await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_request_handler.on_get_task.assert_awaited_once()
assert isinstance(response, a2a_pb2.Task)
assert response.id == 'task-1'
@pytest.mark.asyncio
async def test_get_task_not_found(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetTask call when task is not found."""
request_proto = a2a_pb2.GetTaskRequest(name='tasks/task-1')
mock_request_handler.on_get_task.return_value = None
await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once_with(
grpc.StatusCode.NOT_FOUND, 'TaskNotFoundError: Task not found'
)
@pytest.mark.asyncio
async def test_cancel_task_server_error(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test CancelTask call when handler raises ServerError."""
request_proto = a2a_pb2.CancelTaskRequest(name='tasks/task-1')
error = ServerError(error=types.TaskNotCancelableError())
mock_request_handler.on_cancel_task.side_effect = error
await grpc_handler.CancelTask(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once_with(
grpc.StatusCode.UNIMPLEMENTED,
'TaskNotCancelableError: Task cannot be canceled',
)
@pytest.mark.asyncio
async def test_send_streaming_message(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test successful SendStreamingMessage call."""
async def mock_stream():
yield types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.working),
)
mock_request_handler.on_message_send_stream.return_value = mock_stream()
request_proto = a2a_pb2.SendMessageRequest()
results = [
result
async for result in grpc_handler.SendStreamingMessage(
request_proto, mock_grpc_context
)
]
assert len(results) == 1
assert results[0].HasField('task')
assert results[0].task.id == 'task-1'
@pytest.mark.asyncio
async def test_get_agent_card(
grpc_handler: GrpcHandler,
sample_agent_card: types.AgentCard,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetAgentCard call."""
request_proto = a2a_pb2.GetAgentCardRequest()
response = await grpc_handler.GetAgentCard(request_proto, mock_grpc_context)
assert response.name == sample_agent_card.name
assert response.version == sample_agent_card.version
@pytest.mark.asyncio
async def test_get_agent_card_with_modifier(
mock_request_handler: AsyncMock,
sample_agent_card: types.AgentCard,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetAgentCard call with a card_modifier."""
def modifier(card: types.AgentCard) -> types.AgentCard:
modified_card = card.model_copy(deep=True)
modified_card.name = 'Modified gRPC Agent'
return modified_card
grpc_handler_modified = GrpcHandler(
agent_card=sample_agent_card,
request_handler=mock_request_handler,
card_modifier=modifier,
)
request_proto = a2a_pb2.GetAgentCardRequest()
response = await grpc_handler_modified.GetAgentCard(
request_proto, mock_grpc_context
)
assert response.name == 'Modified gRPC Agent'
assert response.version == sample_agent_card.version
@pytest.mark.asyncio
@pytest.mark.parametrize(
'server_error, grpc_status_code, error_message_part',
[
(
ServerError(error=types.JSONParseError()),
grpc.StatusCode.INTERNAL,
'JSONParseError',
),
(
ServerError(error=types.InvalidRequestError()),
grpc.StatusCode.INVALID_ARGUMENT,
'InvalidRequestError',
),
(
ServerError(error=types.MethodNotFoundError()),
grpc.StatusCode.NOT_FOUND,
'MethodNotFoundError',
),
(
ServerError(error=types.InvalidParamsError()),
grpc.StatusCode.INVALID_ARGUMENT,
'InvalidParamsError',
),
(
ServerError(error=types.InternalError()),
grpc.StatusCode.INTERNAL,
'InternalError',
),
(
ServerError(error=types.TaskNotFoundError()),
grpc.StatusCode.NOT_FOUND,
'TaskNotFoundError',
),
(
ServerError(error=types.TaskNotCancelableError()),
grpc.StatusCode.UNIMPLEMENTED,
'TaskNotCancelableError',
),
(
ServerError(error=types.PushNotificationNotSupportedError()),
grpc.StatusCode.UNIMPLEMENTED,
'PushNotificationNotSupportedError',
),
(
ServerError(error=types.UnsupportedOperationError()),
grpc.StatusCode.UNIMPLEMENTED,
'UnsupportedOperationError',
),
(
ServerError(error=types.ContentTypeNotSupportedError()),
grpc.StatusCode.UNIMPLEMENTED,
'ContentTypeNotSupportedError',
),
(
ServerError(error=types.InvalidAgentResponseError()),
grpc.StatusCode.INTERNAL,
'InvalidAgentResponseError',
),
(
ServerError(error=types.JSONRPCError(code=99, message='Unknown')),
grpc.StatusCode.UNKNOWN,
'Unknown error',
),
],
)
async def test_abort_context_error_mapping( # noqa: PLR0913
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
server_error: ServerError,
grpc_status_code: grpc.StatusCode,
error_message_part: str,
) -> None:
mock_request_handler.on_get_task.side_effect = server_error
request_proto = a2a_pb2.GetTaskRequest(name='tasks/any')
await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once()
call_args, _ = mock_grpc_context.abort.call_args
assert call_args[0] == grpc_status_code
assert error_message_part in call_args[1]
@pytest.mark.asyncio
class TestGrpcExtensions:
async def test_send_message_with_extensions(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
mock_grpc_context.invocation_metadata.return_value = grpc.aio.Metadata(
(HTTP_EXTENSION_HEADER, 'foo'),
(HTTP_EXTENSION_HEADER, 'bar'),
)
def side_effect(request, context: ServerCallContext):
context.activated_extensions.add('foo')
context.activated_extensions.add('baz')
return types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.completed),
)
mock_request_handler.on_message_send.side_effect = side_effect
await grpc_handler.SendMessage(
a2a_pb2.SendMessageRequest(), mock_grpc_context
)
mock_request_handler.on_message_send.assert_awaited_once()
call_context = mock_request_handler.on_message_send.call_args[0][1]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo', 'bar'}
mock_grpc_context.set_trailing_metadata.assert_called_once()
called_metadata = (
mock_grpc_context.set_trailing_metadata.call_args.args[0]
)
assert set(called_metadata) == {
(HTTP_EXTENSION_HEADER, 'foo'),
(HTTP_EXTENSION_HEADER, 'baz'),
}
async def test_send_message_with_comma_separated_extensions(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
mock_grpc_context.invocation_metadata.return_value = grpc.aio.Metadata(
(HTTP_EXTENSION_HEADER, 'foo ,, bar,'),
(HTTP_EXTENSION_HEADER, 'baz , bar'),
)
mock_request_handler.on_message_send.return_value = types.Message(
message_id='1',
role=types.Role.agent,
parts=[types.Part(root=types.TextPart(text='test'))],
)
await grpc_handler.SendMessage(
a2a_pb2.SendMessageRequest(), mock_grpc_context
)
mock_request_handler.on_message_send.assert_awaited_once()
call_context = mock_request_handler.on_message_send.call_args[0][1]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo', 'bar', 'baz'}
async def test_send_streaming_message_with_extensions(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
mock_grpc_context.invocation_metadata.return_value = grpc.aio.Metadata(
(HTTP_EXTENSION_HEADER, 'foo'),
(HTTP_EXTENSION_HEADER, 'bar'),
)
async def side_effect(request, context: ServerCallContext):
context.activated_extensions.add('foo')
context.activated_extensions.add('baz')
yield types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.working),
)
mock_request_handler.on_message_send_stream.side_effect = side_effect
results = [
result
async for result in grpc_handler.SendStreamingMessage(
a2a_pb2.SendMessageRequest(), mock_grpc_context
)
]
assert results
mock_request_handler.on_message_send_stream.assert_called_once()
call_context = mock_request_handler.on_message_send_stream.call_args[0][
1
]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo', 'bar'}
mock_grpc_context.set_trailing_metadata.assert_called_once()
called_metadata = (
mock_grpc_context.set_trailing_metadata.call_args.args[0]
)
assert set(called_metadata) == {
(HTTP_EXTENSION_HEADER, 'foo'),
(HTTP_EXTENSION_HEADER, 'baz'),
}