Skip to content

Commit 0feb5bd

Browse files
committed
adding 12 tests for utils/message.py
1 parent 224d4f5 commit 0feb5bd

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

tests/utils/test_message.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
from unittest.mock import patch
2+
import uuid
3+
4+
from a2a.types import (
5+
Message,
6+
Part,
7+
Role,
8+
TextPart,
9+
)
10+
from a2a.utils import new_agent_text_message, get_text_parts, get_message_text
11+
12+
13+
class TestNewAgentTextMessage:
14+
def test_new_agent_text_message_basic(self):
15+
# Setup
16+
text = "Hello, I'm an agent"
17+
18+
# Exercise - with a fixed uuid for testing
19+
with patch('uuid.uuid4', return_value=uuid.UUID('12345678-1234-5678-1234-567812345678')):
20+
message = new_agent_text_message(text)
21+
22+
# Verify
23+
assert message.role == Role.agent
24+
assert len(message.parts) == 1
25+
assert message.parts[0].root.text == text
26+
assert message.messageId == '12345678-1234-5678-1234-567812345678'
27+
assert message.taskId is None
28+
assert message.contextId is None
29+
30+
def test_new_agent_text_message_with_context_id(self):
31+
# Setup
32+
text = "Message with context"
33+
context_id = "test-context-id"
34+
35+
# Exercise
36+
with patch('uuid.uuid4', return_value=uuid.UUID('12345678-1234-5678-1234-567812345678')):
37+
message = new_agent_text_message(text, context_id=context_id)
38+
39+
# Verify
40+
assert message.role == Role.agent
41+
assert message.parts[0].root.text == text
42+
assert message.messageId == '12345678-1234-5678-1234-567812345678'
43+
assert message.contextId == context_id
44+
assert message.taskId is None
45+
46+
def test_new_agent_text_message_with_task_id(self):
47+
# Setup
48+
text = "Message with task id"
49+
task_id = "test-task-id"
50+
51+
# Exercise
52+
with patch('uuid.uuid4', return_value=uuid.UUID('12345678-1234-5678-1234-567812345678')):
53+
message = new_agent_text_message(text, task_id=task_id)
54+
55+
# Verify
56+
assert message.role == Role.agent
57+
assert message.parts[0].root.text == text
58+
assert message.messageId == '12345678-1234-5678-1234-567812345678'
59+
assert message.taskId == task_id
60+
assert message.contextId is None
61+
62+
def test_new_agent_text_message_with_both_ids(self):
63+
# Setup
64+
text = "Message with both ids"
65+
context_id = "test-context-id"
66+
task_id = "test-task-id"
67+
68+
# Exercise
69+
with patch('uuid.uuid4', return_value=uuid.UUID('12345678-1234-5678-1234-567812345678')):
70+
message = new_agent_text_message(text, context_id=context_id, task_id=task_id)
71+
72+
# Verify
73+
assert message.role == Role.agent
74+
assert message.parts[0].root.text == text
75+
assert message.messageId == '12345678-1234-5678-1234-567812345678'
76+
assert message.contextId == context_id
77+
assert message.taskId == task_id
78+
79+
def test_new_agent_text_message_empty_text(self):
80+
# Setup
81+
text = ""
82+
83+
# Exercise
84+
with patch('uuid.uuid4', return_value=uuid.UUID('12345678-1234-5678-1234-567812345678')):
85+
message = new_agent_text_message(text)
86+
87+
# Verify
88+
assert message.role == Role.agent
89+
assert message.parts[0].root.text == ""
90+
assert message.messageId == '12345678-1234-5678-1234-567812345678'
91+
92+
93+
class TestGetTextParts:
94+
def test_get_text_parts_single_text_part(self):
95+
# Setup
96+
parts = [Part(root=TextPart(text="Hello world"))]
97+
98+
# Exercise
99+
result = get_text_parts(parts)
100+
101+
# Verify
102+
assert result == ["Hello world"]
103+
104+
def test_get_text_parts_multiple_text_parts(self):
105+
# Setup
106+
parts = [
107+
Part(root=TextPart(text="First part")),
108+
Part(root=TextPart(text="Second part")),
109+
Part(root=TextPart(text="Third part")),
110+
]
111+
112+
# Exercise
113+
result = get_text_parts(parts)
114+
115+
# Verify
116+
assert result == ["First part", "Second part", "Third part"]
117+
118+
def test_get_text_parts_empty_list(self):
119+
# Setup
120+
parts = []
121+
122+
# Exercise
123+
result = get_text_parts(parts)
124+
125+
# Verify
126+
assert result == []
127+
128+
129+
class TestGetMessageText:
130+
def test_get_message_text_single_part(self):
131+
# Setup
132+
message = Message(
133+
role=Role.agent,
134+
parts=[Part(root=TextPart(text="Hello world"))],
135+
messageId="test-message-id",
136+
)
137+
138+
# Exercise
139+
result = get_message_text(message)
140+
141+
# Verify
142+
assert result == "Hello world"
143+
144+
def test_get_message_text_multiple_parts(self):
145+
# Setup
146+
message = Message(
147+
role=Role.agent,
148+
parts=[
149+
Part(root=TextPart(text="First line")),
150+
Part(root=TextPart(text="Second line")),
151+
Part(root=TextPart(text="Third line")),
152+
],
153+
messageId="test-message-id",
154+
)
155+
156+
# Exercise
157+
result = get_message_text(message)
158+
159+
# Verify - default delimiter is newline
160+
assert result == "First line\nSecond line\nThird line"
161+
162+
def test_get_message_text_custom_delimiter(self):
163+
# Setup
164+
message = Message(
165+
role=Role.agent,
166+
parts=[
167+
Part(root=TextPart(text="First part")),
168+
Part(root=TextPart(text="Second part")),
169+
Part(root=TextPart(text="Third part")),
170+
],
171+
messageId="test-message-id",
172+
)
173+
174+
# Exercise
175+
result = get_message_text(message, delimiter=" | ")
176+
177+
# Verify
178+
assert result == "First part | Second part | Third part"
179+
180+
def test_get_message_text_empty_parts(self):
181+
# Setup
182+
message = Message(
183+
role=Role.agent,
184+
parts=[],
185+
messageId="test-message-id",
186+
)
187+
188+
# Exercise
189+
result = get_message_text(message)
190+
191+
# Verify
192+
assert result == ""

0 commit comments

Comments
 (0)