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