11from typing import Any
22import pytest
3- from unittest .mock import MagicMock
4- from uuid import uuid4
53from a2a .utils .helpers import (
64 create_task_obj ,
75 append_artifact_to_task ,
1513 Task ,
1614 TaskArtifactUpdateEvent ,
1715 TaskState ,
18- TaskStatus ,
1916 TextPart ,
2017 Part ,
2118)
22- from a2a .utils .errors import ServerError , UnsupportedOperationError
19+ from a2a .utils .errors import ServerError
2320
2421# --- Helper Data ---
2522TEXT_PART_DATA : dict [str , Any ] = {'type' : 'text' , 'text' : 'Hello' }
4037 'type' : 'task' ,
4138}
4239
40+
4341# Test create_task_obj
4442def test_create_task_obj ():
4543 message = Message (** MINIMAL_MESSAGE_USER )
@@ -55,7 +53,7 @@ def test_create_task_obj():
5553
5654# Test append_artifact_to_task
5755def test_append_artifact_to_task ():
58- # Prepare base task
56+ # Prepare base task
5957 task = Task (** MINIMAL_TASK )
6058 assert task .id == 'task-abc'
6159 assert task .contextId == 'session-xyz'
@@ -66,66 +64,86 @@ def test_append_artifact_to_task():
6664
6765 # Prepare appending artifact and event
6866 artifact_1 = Artifact (
69- artifactId = "artifact-123" , parts = [Part (root = TextPart (text = "Hello" ))]
67+ artifactId = 'artifact-123' , parts = [Part (root = TextPart (text = 'Hello' ))]
68+ )
69+ append_event_1 = TaskArtifactUpdateEvent (
70+ artifact = artifact_1 , append = False , taskId = '123' , contextId = '123'
7071 )
71- append_event_1 = TaskArtifactUpdateEvent (artifact = artifact_1 , append = False , taskId = "123" , contextId = "123" )
7272
7373 # Test adding a new artifact (not appending)
7474 append_artifact_to_task (task , append_event_1 )
7575 assert len (task .artifacts ) == 1
76- assert task .artifacts [0 ].artifactId == " artifact-123"
76+ assert task .artifacts [0 ].artifactId == ' artifact-123'
7777 assert task .artifacts [0 ].name == None
7878 assert len (task .artifacts [0 ].parts ) == 1
79- assert task .artifacts [0 ].parts [0 ].root .text == " Hello"
79+ assert task .artifacts [0 ].parts [0 ].root .text == ' Hello'
8080
8181 # Test replacing the artifact
8282 artifact_2 = Artifact (
83- artifactId = "artifact-123" , name = "updated name" , parts = [Part (root = TextPart (text = "Updated" ))]
83+ artifactId = 'artifact-123' ,
84+ name = 'updated name' ,
85+ parts = [Part (root = TextPart (text = 'Updated' ))],
86+ )
87+ append_event_2 = TaskArtifactUpdateEvent (
88+ artifact = artifact_2 , append = False , taskId = '123' , contextId = '123'
8489 )
85- append_event_2 = TaskArtifactUpdateEvent (artifact = artifact_2 , append = False , taskId = "123" , contextId = "123" )
8690 append_artifact_to_task (task , append_event_2 )
8791 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"
92+ assert task .artifacts [0 ].artifactId == ' artifact-123'
93+ assert task .artifacts [0 ].name == ' updated name'
9094 assert len (task .artifacts [0 ].parts ) == 1
91- assert task .artifacts [0 ].parts [0 ].root .text == " Updated"
95+ assert task .artifacts [0 ].parts [0 ].root .text == ' Updated'
9296
9397 # Test appending parts to an existing artifact
9498 artifact_with_parts = Artifact (
95- artifactId = "artifact-123" , parts = [Part (root = TextPart (text = "Part 2" ))]
99+ artifactId = 'artifact-123' , parts = [Part (root = TextPart (text = 'Part 2' ))]
100+ )
101+ append_event_3 = TaskArtifactUpdateEvent (
102+ artifact = artifact_with_parts , append = True , taskId = '123' , contextId = '123'
96103 )
97- append_event_3 = TaskArtifactUpdateEvent (artifact = artifact_with_parts , append = True , taskId = "123" , contextId = "123" )
98104 append_artifact_to_task (task , append_event_3 )
99105 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"
106+ assert task .artifacts [0 ].parts [0 ].root .text == ' Updated'
107+ assert task .artifacts [0 ].parts [1 ].root .text == ' Part 2'
102108
103109 # Test adding another new artifact
104110 another_artifact_with_parts = Artifact (
105- artifactId = "new_artifact" , parts = [Part (root = TextPart (text = "new artifact Part 1" ))]
111+ artifactId = 'new_artifact' ,
112+ parts = [Part (root = TextPart (text = 'new artifact Part 1' ))],
113+ )
114+ append_event_4 = TaskArtifactUpdateEvent (
115+ artifact = another_artifact_with_parts ,
116+ append = False ,
117+ taskId = '123' ,
118+ contextId = '123' ,
106119 )
107- append_event_4 = TaskArtifactUpdateEvent (artifact = another_artifact_with_parts , append = False , taskId = "123" , contextId = "123" )
108120 append_artifact_to_task (task , append_event_4 )
109121 assert len (task .artifacts ) == 2
110- assert task .artifacts [0 ].artifactId == " artifact-123"
111- assert task .artifacts [1 ].artifactId == " new_artifact"
122+ assert task .artifacts [0 ].artifactId == ' artifact-123'
123+ assert task .artifacts [1 ].artifactId == ' new_artifact'
112124 assert len (task .artifacts [0 ].parts ) == 2
113125 assert len (task .artifacts [1 ].parts ) == 1
114126
115127 # Test appending part to a task that does not have a matching artifact
116128 non_existing_artifact_with_parts = Artifact (
117- artifactId = "artifact-456" , parts = [Part (root = TextPart (text = "Part 1" ))]
129+ artifactId = 'artifact-456' , parts = [Part (root = TextPart (text = 'Part 1' ))]
130+ )
131+ append_event_5 = TaskArtifactUpdateEvent (
132+ artifact = non_existing_artifact_with_parts ,
133+ append = True ,
134+ taskId = '123' ,
135+ contextId = '123' ,
118136 )
119- append_event_5 = TaskArtifactUpdateEvent (artifact = non_existing_artifact_with_parts , append = True , taskId = "123" , contextId = "123" )
120137 append_artifact_to_task (task , append_event_5 )
121138 assert len (task .artifacts ) == 2
122139 assert len (task .artifacts [0 ].parts ) == 2
123140 assert len (task .artifacts [1 ].parts ) == 1
124141
142+
125143# Test build_text_artifact
126144def test_build_text_artifact ():
127- artifact_id = " text_artifact"
128- text = " This is a sample text"
145+ artifact_id = ' text_artifact'
146+ text = ' This is a sample text'
129147 artifact = build_text_artifact (text , artifact_id )
130148
131149 assert artifact .artifactId == artifact_id
@@ -138,17 +156,17 @@ def test_validate_decorator():
138156 class TestClass :
139157 condition = True
140158
141- @validate (lambda self : self .condition , " Condition not met" )
159+ @validate (lambda self : self .condition , ' Condition not met' )
142160 def test_method (self ):
143- return " Success"
161+ return ' Success'
144162
145163 obj = TestClass ()
146164
147165 # Test passing condition
148- assert obj .test_method () == " Success"
166+ assert obj .test_method () == ' Success'
149167
150168 # Test failing condition
151169 obj .condition = False
152170 with pytest .raises (ServerError ) as exc_info :
153171 obj .test_method ()
154- assert " Condition not met" in str (exc_info .value )
172+ assert ' Condition not met' in str (exc_info .value )
0 commit comments