forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_jsonrpc_app.py
More file actions
360 lines (312 loc) · 12.4 KB
/
test_jsonrpc_app.py
File metadata and controls
360 lines (312 loc) · 12.4 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
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import pytest
from starlette.testclient import TestClient
# Attempt to import StarletteBaseUser, fallback to MagicMock if not available
try:
from starlette.authentication import BaseUser as StarletteBaseUser
except ImportError:
StarletteBaseUser = MagicMock() # type: ignore
from a2a.extensions.common import HTTP_EXTENSION_HEADER
from a2a.server.apps.jsonrpc import (
jsonrpc_app, # Keep this import for optional deps test
)
from a2a.server.apps.jsonrpc.jsonrpc_app import (
JSONRPCApplication,
StarletteUserProxy,
)
from a2a.server.apps.jsonrpc.starlette_app import A2AStarletteApplication
from a2a.server.context import ServerCallContext
from a2a.server.request_handlers.request_handler import (
RequestHandler,
) # For mock spec
from a2a.types import (
AgentCard,
Message,
MessageSendParams,
Part,
Role,
SendMessageRequest,
SendMessageResponse,
SendMessageSuccessResponse,
TextPart,
)
# --- StarletteUserProxy Tests ---
class TestStarletteUserProxy:
def test_starlette_user_proxy_is_authenticated_true(self):
starlette_user_mock = MagicMock(spec=StarletteBaseUser)
starlette_user_mock.is_authenticated = True
proxy = StarletteUserProxy(starlette_user_mock)
assert proxy.is_authenticated is True
def test_starlette_user_proxy_is_authenticated_false(self):
starlette_user_mock = MagicMock(spec=StarletteBaseUser)
starlette_user_mock.is_authenticated = False
proxy = StarletteUserProxy(starlette_user_mock)
assert proxy.is_authenticated is False
def test_starlette_user_proxy_user_name(self):
starlette_user_mock = MagicMock(spec=StarletteBaseUser)
starlette_user_mock.display_name = 'Test User DisplayName'
proxy = StarletteUserProxy(starlette_user_mock)
assert proxy.user_name == 'Test User DisplayName'
def test_starlette_user_proxy_user_name_raises_attribute_error(self):
"""
Tests that if the underlying starlette user object is missing the
display_name attribute, the proxy currently raises an AttributeError.
"""
starlette_user_mock = MagicMock(spec=StarletteBaseUser)
# Ensure display_name is not present on the mock to trigger AttributeError
del starlette_user_mock.display_name
proxy = StarletteUserProxy(starlette_user_mock)
with pytest.raises(AttributeError, match='display_name'):
_ = proxy.user_name
# --- JSONRPCApplication Tests (Selected) ---
class TestJSONRPCApplicationSetup: # Renamed to avoid conflict
def test_jsonrpc_app_build_method_abstract_raises_typeerror(
self,
): # Renamed test
mock_handler = MagicMock(spec=RequestHandler)
# Mock agent_card with essential attributes accessed in JSONRPCApplication.__init__
mock_agent_card = MagicMock(spec=AgentCard)
# Ensure 'url' attribute exists on the mock_agent_card, as it's accessed in __init__
mock_agent_card.url = 'http://mockurl.com'
# Ensure 'supportsAuthenticatedExtendedCard' attribute exists
mock_agent_card.supports_authenticated_extended_card = False
# This will fail at definition time if an abstract method is not implemented
with pytest.raises(
TypeError,
match=".*abstract class IncompleteJSONRPCApp .* abstract method '?build'?",
):
class IncompleteJSONRPCApp(JSONRPCApplication):
# Intentionally not implementing 'build'
def some_other_method(self):
pass
IncompleteJSONRPCApp(
agent_card=mock_agent_card, http_handler=mock_handler
)
class TestJSONRPCApplicationOptionalDeps:
# Running tests in this class requires optional dependencies starlette and
# sse-starlette to be present in the test environment.
@pytest.fixture(scope='class', autouse=True)
def ensure_pkg_starlette_is_present(self):
try:
import starlette as _starlette
import sse_starlette as _sse_starlette
except ImportError:
pytest.fail(
f'Running tests in {self.__class__.__name__} requires'
' optional dependencies starlette and sse-starlette to be'
' present in the test environment. Run `uv sync --dev ...`'
' before running the test suite.'
)
@pytest.fixture(scope='class')
def mock_app_params(self) -> dict:
# Mock http_handler
mock_handler = MagicMock(spec=RequestHandler)
# Mock agent_card with essential attributes accessed in __init__
mock_agent_card = MagicMock(spec=AgentCard)
# Ensure 'url' attribute exists on the mock_agent_card, as it's accessed
# in __init__
mock_agent_card.url = 'http://example.com'
# Ensure 'supportsAuthenticatedExtendedCard' attribute exists
mock_agent_card.supports_authenticated_extended_card = False
return {'agent_card': mock_agent_card, 'http_handler': mock_handler}
@pytest.fixture(scope='class')
def mark_pkg_starlette_not_installed(self):
pkg_starlette_installed_flag = jsonrpc_app._package_starlette_installed
jsonrpc_app._package_starlette_installed = False
yield
jsonrpc_app._package_starlette_installed = pkg_starlette_installed_flag
def test_create_jsonrpc_based_app_with_present_deps_succeeds(
self, mock_app_params: dict
):
class DummyJSONRPCApp(JSONRPCApplication):
def build(
self,
agent_card_url='/.well-known/agent.json',
rpc_url='/',
**kwargs,
):
return object()
try:
_app = DummyJSONRPCApp(**mock_app_params)
except ImportError:
pytest.fail(
'With packages starlette and see-starlette present, creating a'
' JSONRPCApplication-based instance should not raise'
' ImportError'
)
def test_create_jsonrpc_based_app_with_missing_deps_raises_importerror(
self, mock_app_params: dict, mark_pkg_starlette_not_installed: Any
):
class DummyJSONRPCApp(JSONRPCApplication):
def build(
self,
agent_card_url='/.well-known/agent.json',
rpc_url='/',
**kwargs,
):
return object()
with pytest.raises(
ImportError,
match=(
'Packages `starlette` and `sse-starlette` are required to use'
' the `JSONRPCApplication`'
),
):
_app = DummyJSONRPCApp(**mock_app_params)
class TestJSONRPCExtensions:
@pytest.fixture
def mock_handler(self):
handler = AsyncMock(spec=RequestHandler)
handler.on_message_send.return_value = SendMessageResponse(
root=SendMessageSuccessResponse(
id='1',
result=Message(
message_id='test',
role=Role.agent,
parts=[Part(TextPart(text='response message'))],
),
)
)
return handler
@pytest.fixture
def test_app(self, mock_handler):
mock_agent_card = MagicMock(spec=AgentCard)
mock_agent_card.url = 'http://mockurl.com'
mock_agent_card.supports_authenticated_extended_card = False
return A2AStarletteApplication(
agent_card=mock_agent_card, http_handler=mock_handler
)
@pytest.fixture
def client(self, test_app):
return TestClient(test_app.build())
def test_request_with_single_extension(self, client, mock_handler):
headers = {HTTP_EXTENSION_HEADER: 'foo'}
response = client.post(
'/',
headers=headers,
json=SendMessageRequest(
id='1',
params=MessageSendParams(
message=Message(
message_id='1',
role=Role.user,
parts=[Part(TextPart(text='hi'))],
)
),
).model_dump(),
)
response.raise_for_status()
mock_handler.on_message_send.assert_called_once()
call_context = mock_handler.on_message_send.call_args[0][1]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo'}
def test_request_with_comma_separated_extensions(
self, client, mock_handler
):
headers = {HTTP_EXTENSION_HEADER: 'foo, bar'}
response = client.post(
'/',
headers=headers,
json=SendMessageRequest(
id='1',
params=MessageSendParams(
message=Message(
message_id='1',
role=Role.user,
parts=[Part(TextPart(text='hi'))],
)
),
).model_dump(),
)
response.raise_for_status()
mock_handler.on_message_send.assert_called_once()
call_context = mock_handler.on_message_send.call_args[0][1]
assert call_context.requested_extensions == {'foo', 'bar'}
def test_request_with_comma_separated_extensions_no_space(
self, client, mock_handler
):
headers = [
(HTTP_EXTENSION_HEADER, 'foo, bar'),
(HTTP_EXTENSION_HEADER, 'baz'),
]
response = client.post(
'/',
headers=headers,
json=SendMessageRequest(
id='1',
params=MessageSendParams(
message=Message(
message_id='1',
role=Role.user,
parts=[Part(TextPart(text='hi'))],
)
),
).model_dump(),
)
response.raise_for_status()
mock_handler.on_message_send.assert_called_once()
call_context = mock_handler.on_message_send.call_args[0][1]
assert call_context.requested_extensions == {'foo', 'bar', 'baz'}
def test_request_with_multiple_extension_headers(
self, client, mock_handler
):
headers = [
(HTTP_EXTENSION_HEADER, 'foo'),
(HTTP_EXTENSION_HEADER, 'bar'),
]
response = client.post(
'/',
headers=headers,
json=SendMessageRequest(
id='1',
params=MessageSendParams(
message=Message(
message_id='1',
role=Role.user,
parts=[Part(TextPart(text='hi'))],
)
),
).model_dump(),
)
response.raise_for_status()
mock_handler.on_message_send.assert_called_once()
call_context = mock_handler.on_message_send.call_args[0][1]
assert call_context.requested_extensions == {'foo', 'bar'}
def test_response_with_activated_extensions(self, client, mock_handler):
def side_effect(request, context: ServerCallContext):
context.activated_extensions.add('foo')
context.activated_extensions.add('baz')
return SendMessageResponse(
root=SendMessageSuccessResponse(
id='1',
result=Message(
message_id='test',
role=Role.agent,
parts=[Part(TextPart(text='response message'))],
),
)
)
mock_handler.on_message_send.side_effect = side_effect
response = client.post(
'/',
json=SendMessageRequest(
id='1',
params=MessageSendParams(
message=Message(
message_id='1',
role=Role.user,
parts=[Part(TextPart(text='hi'))],
)
),
).model_dump(),
)
response.raise_for_status()
assert response.status_code == 200
assert HTTP_EXTENSION_HEADER in response.headers
assert set(response.headers[HTTP_EXTENSION_HEADER].split(', ')) == {
'foo',
'baz',
}
if __name__ == '__main__':
pytest.main([__file__])