|
| 1 | +from typing import Any |
| 2 | +import pytest |
| 3 | +from unittest.mock import MagicMock |
| 4 | +from uuid import uuid4 |
| 5 | +from a2a.utils.helpers import ( |
| 6 | + create_task_obj, |
| 7 | + append_artifact_to_task, |
| 8 | + build_text_artifact, |
| 9 | + validate, |
| 10 | +) |
| 11 | +from a2a.types import ( |
| 12 | + Artifact, |
| 13 | + MessageSendParams, |
| 14 | + Message, |
| 15 | + Task, |
| 16 | + TaskArtifactUpdateEvent, |
| 17 | + TaskState, |
| 18 | + TaskStatus, |
| 19 | + TextPart, |
| 20 | + Part, |
| 21 | +) |
| 22 | +from a2a.utils.errors import ServerError, UnsupportedOperationError |
| 23 | + |
| 24 | +# --- Helper Data --- |
| 25 | +TEXT_PART_DATA: dict[str, Any] = {'type': 'text', 'text': 'Hello'} |
| 26 | + |
| 27 | +MINIMAL_MESSAGE_USER: dict[str, Any] = { |
| 28 | + 'role': 'user', |
| 29 | + 'parts': [TEXT_PART_DATA], |
| 30 | + 'messageId': 'msg-123', |
| 31 | + 'type': 'message', |
| 32 | +} |
| 33 | + |
| 34 | +MINIMAL_TASK_STATUS: dict[str, Any] = {'state': 'submitted'} |
| 35 | + |
| 36 | +MINIMAL_TASK: dict[str, Any] = { |
| 37 | + 'id': 'task-abc', |
| 38 | + 'contextId': 'session-xyz', |
| 39 | + 'status': MINIMAL_TASK_STATUS, |
| 40 | + 'type': 'task', |
| 41 | +} |
| 42 | + |
| 43 | +# Test create_task_obj |
| 44 | +def test_create_task_obj(): |
| 45 | + message = Message(**MINIMAL_MESSAGE_USER) |
| 46 | + send_params = MessageSendParams(message=message) |
| 47 | + |
| 48 | + task = create_task_obj(send_params) |
| 49 | + assert task.id is not None |
| 50 | + assert task.contextId == message.contextId |
| 51 | + assert task.status.state == TaskState.submitted |
| 52 | + assert len(task.history) == 1 |
| 53 | + assert task.history[0] == message |
| 54 | + |
| 55 | + |
| 56 | +# Test append_artifact_to_task |
| 57 | +def test_append_artifact_to_task(): |
| 58 | + # Prepare base task |
| 59 | + task = Task(**MINIMAL_TASK) |
| 60 | + assert task.id == 'task-abc' |
| 61 | + assert task.contextId == 'session-xyz' |
| 62 | + assert task.status.state == TaskState.submitted |
| 63 | + assert task.history is None |
| 64 | + assert task.artifacts is None |
| 65 | + assert task.metadata is None |
| 66 | + |
| 67 | + # Prepare appending artifact and event |
| 68 | + artifact_1 = Artifact( |
| 69 | + artifactId="artifact-123", parts=[Part(root=TextPart(text="Hello"))] |
| 70 | + ) |
| 71 | + append_event_1 = TaskArtifactUpdateEvent(artifact=artifact_1, append=False, taskId="123", contextId="123") |
| 72 | + |
| 73 | + # Test adding a new artifact (not appending) |
| 74 | + append_artifact_to_task(task, append_event_1) |
| 75 | + assert len(task.artifacts) == 1 |
| 76 | + assert task.artifacts[0].artifactId == "artifact-123" |
| 77 | + assert task.artifacts[0].name == None |
| 78 | + assert len(task.artifacts[0].parts) == 1 |
| 79 | + assert task.artifacts[0].parts[0].root.text == "Hello" |
| 80 | + |
| 81 | + # Test replacing the artifact |
| 82 | + artifact_2 = Artifact( |
| 83 | + artifactId="artifact-123", name="updated name", parts=[Part(root=TextPart(text="Updated"))] |
| 84 | + ) |
| 85 | + append_event_2 = TaskArtifactUpdateEvent(artifact=artifact_2, append=False, taskId="123", contextId="123") |
| 86 | + append_artifact_to_task(task, append_event_2) |
| 87 | + assert len(task.artifacts) == 1 # Should still have one artifact |
| 88 | + assert task.artifacts[0].artifactId == "artifact-123" |
| 89 | + assert task.artifacts[0].name == "updated name" |
| 90 | + assert len(task.artifacts[0].parts) == 1 |
| 91 | + assert task.artifacts[0].parts[0].root.text == "Updated" |
| 92 | + |
| 93 | + # Test appending parts to an existing artifact |
| 94 | + artifact_with_parts = Artifact( |
| 95 | + artifactId="artifact-123", parts=[Part(root=TextPart(text="Part 2"))] |
| 96 | + ) |
| 97 | + append_event_3 = TaskArtifactUpdateEvent(artifact=artifact_with_parts, append=True, taskId="123", contextId="123") |
| 98 | + append_artifact_to_task(task, append_event_3) |
| 99 | + assert len(task.artifacts[0].parts) == 2 |
| 100 | + assert task.artifacts[0].parts[0].root.text == "Updated" |
| 101 | + assert task.artifacts[0].parts[1].root.text == "Part 2" |
| 102 | + |
| 103 | + # Test adding another new artifact |
| 104 | + another_artifact_with_parts = Artifact( |
| 105 | + artifactId="new_artifact", parts=[Part(root=TextPart(text="new artifact Part 1"))] |
| 106 | + ) |
| 107 | + append_event_4 = TaskArtifactUpdateEvent(artifact=another_artifact_with_parts, append=False, taskId="123", contextId="123") |
| 108 | + append_artifact_to_task(task, append_event_4) |
| 109 | + assert len(task.artifacts) == 2 |
| 110 | + assert task.artifacts[0].artifactId == "artifact-123" |
| 111 | + assert task.artifacts[1].artifactId == "new_artifact" |
| 112 | + assert len(task.artifacts[0].parts) == 2 |
| 113 | + assert len(task.artifacts[1].parts) == 1 |
| 114 | + |
| 115 | + # Test appending part to a task that does not have a matching artifact |
| 116 | + non_existing_artifact_with_parts = Artifact( |
| 117 | + artifactId="artifact-456", parts=[Part(root=TextPart(text="Part 1"))] |
| 118 | + ) |
| 119 | + append_event_5 = TaskArtifactUpdateEvent(artifact=non_existing_artifact_with_parts, append=True, taskId="123", contextId="123") |
| 120 | + append_artifact_to_task(task, append_event_5) |
| 121 | + assert len(task.artifacts) == 2 |
| 122 | + assert len(task.artifacts[0].parts) == 2 |
| 123 | + assert len(task.artifacts[1].parts) == 1 |
| 124 | + |
| 125 | +# Test build_text_artifact |
| 126 | +def test_build_text_artifact(): |
| 127 | + artifact_id = "text_artifact" |
| 128 | + text = "This is a sample text" |
| 129 | + artifact = build_text_artifact(text, artifact_id) |
| 130 | + |
| 131 | + assert artifact.artifactId == artifact_id |
| 132 | + assert len(artifact.parts) == 1 |
| 133 | + assert artifact.parts[0].root.text == text |
| 134 | + |
| 135 | + |
| 136 | +# Test validate decorator |
| 137 | +def test_validate_decorator(): |
| 138 | + class TestClass: |
| 139 | + condition = True |
| 140 | + |
| 141 | + @validate(lambda self: self.condition, "Condition not met") |
| 142 | + def test_method(self): |
| 143 | + return "Success" |
| 144 | + |
| 145 | + obj = TestClass() |
| 146 | + |
| 147 | + # Test passing condition |
| 148 | + assert obj.test_method() == "Success" |
| 149 | + |
| 150 | + # Test failing condition |
| 151 | + obj.condition = False |
| 152 | + with pytest.raises(ServerError) as exc_info: |
| 153 | + obj.test_method() |
| 154 | + assert "Condition not met" in str(exc_info.value) |
0 commit comments