-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathtest_jsonrpc_app_compat.py
More file actions
144 lines (119 loc) · 3.74 KB
/
test_jsonrpc_app_compat.py
File metadata and controls
144 lines (119 loc) · 3.74 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
import logging
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import pytest
from starlette.testclient import TestClient
from starlette.applications import Starlette
from a2a.server.routes import create_jsonrpc_routes
from a2a.server.request_handlers.request_handler import RequestHandler
from a2a.types.a2a_pb2 import (
AgentCard,
AgentCapabilities,
AgentInterface,
Message as Message10,
Part as Part10,
Role as Role10,
Task as Task10,
TaskStatus as TaskStatus10,
TaskState as TaskState10,
)
from a2a.compat.v0_3 import a2a_v0_3_pb2
logger = logging.getLogger(__name__)
@pytest.fixture
def mock_handler():
handler = AsyncMock(spec=RequestHandler)
handler.on_message_send.return_value = Message10(
message_id='test',
role=Role10.ROLE_AGENT,
parts=[Part10(text='response message')],
)
handler.on_get_task.return_value = Task10(
id='test_task_id',
context_id='test_context_id',
status=TaskStatus10(
state=TaskState10.TASK_STATE_COMPLETED,
),
)
return handler
@pytest.fixture
def test_app(mock_handler):
agent_card = AgentCard(
name='TestAgent',
description='Test Description',
version='1.0.0',
capabilities=AgentCapabilities(
streaming=False, push_notifications=True, extended_agent_card=True
),
)
interface = agent_card.supported_interfaces.add()
interface.url = 'http://mockurl.com'
interface.protocol_binding = 'jsonrpc'
interface.protocol_version = '0.3'
jsonrpc_routes = create_jsonrpc_routes(
agent_card=agent_card,
request_handler=mock_handler,
enable_v0_3_compat=True,
rpc_url='/',
)
return Starlette(routes=jsonrpc_routes)
@pytest.fixture
def client(test_app):
return TestClient(test_app)
def test_send_message_v03_compat(
client: TestClient, mock_handler: AsyncMock
) -> None:
request_payload = {
'jsonrpc': '2.0',
'id': '1',
'method': 'message/send',
'params': {
'message': {
'messageId': 'req',
'role': 'user',
'parts': [{'text': 'hello'}],
}
},
}
response = client.post('/', json=request_payload)
assert response.status_code == 200
data = response.json()
assert data['jsonrpc'] == '2.0'
assert data['id'] == '1'
assert 'result' in data
assert data['result']['messageId'] == 'test'
assert data['result']['parts'][0]['text'] == 'response message'
def test_get_task_v03_compat(
client: TestClient, mock_handler: AsyncMock
) -> None:
request_payload = {
'jsonrpc': '2.0',
'id': '2',
'method': 'tasks/get',
'params': {'id': 'test_task_id'},
}
response = client.post('/', json=request_payload)
assert response.status_code == 200
data = response.json()
assert data['jsonrpc'] == '2.0'
assert data['id'] == '2'
assert 'result' in data
assert data['result']['id'] == 'test_task_id'
assert data['result']['status']['state'] == 'completed'
def test_get_extended_agent_card_v03_compat(
client: TestClient,
) -> None:
"""Test that the v0.3 method name 'agent/getAuthenticatedExtendedCard' is correctly routed."""
request_payload = {
'jsonrpc': '2.0',
'id': '3',
'method': 'agent/getAuthenticatedExtendedCard',
'params': {},
}
response = client.post('/', json=request_payload)
assert response.status_code == 200
data = response.json()
assert data['jsonrpc'] == '2.0'
assert data['id'] == '3'
assert 'result' in data
# The result should be a v0.3 AgentCard
assert 'supportsAuthenticatedExtendedCard' in data['result']