Repository navigation
Expand file tree
/
Copy pathTaskManagerTest.java
More file actions
740 lines (625 loc) · 30.8 KB
/
Copy pathTaskManagerTest.java
File metadata and controls
740 lines (625 loc) · 30.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
package io.a2a.server.tasks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.a2a.spec.A2AServerException;
import io.a2a.spec.Artifact;
import io.a2a.spec.Message;
import io.a2a.spec.Task;
import io.a2a.spec.TaskArtifactUpdateEvent;
import io.a2a.spec.TaskState;
import io.a2a.spec.TaskStatus;
import io.a2a.spec.TaskStatusUpdateEvent;
import io.a2a.spec.TextPart;
import io.a2a.util.Utils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TaskManagerTest {
private static final String TASK_JSON = """
{
"id": "task-abc",
"contextId" : "session-xyz",
"status": {"state": "submitted"},
"kind": "task"
}""";
Task minimalTask;
TaskStore taskStore;
TaskManager taskManager;
@BeforeEach
public void init() throws Exception {
minimalTask = Utils.unmarshalFrom(TASK_JSON, Task.TYPE_REFERENCE);
taskStore = new InMemoryTaskStore();
taskManager = new TaskManager(minimalTask.getId(), minimalTask.getContextId(), taskStore, null);
}
@Test
public void testGetTaskExisting() {
Task expectedTask = minimalTask;
taskStore.save(expectedTask);
Task retrieved = taskManager.getTask();
assertSame(expectedTask, retrieved);
}
@Test
public void testGetTaskNonExistent() {
Task retrieved = taskManager.getTask();
assertNull(retrieved);
}
@Test
public void testSaveTaskEventNewTask() throws A2AServerException {
Task saved = taskManager.saveTaskEvent(minimalTask);
Task retrieved = taskManager.getTask();
assertSame(minimalTask, retrieved);
assertSame(retrieved, saved);
}
@Test
public void testSaveTaskEventStatusUpdate() throws A2AServerException {
Task initialTask = minimalTask;
taskStore.save(initialTask);
TaskStatus newStatus = new TaskStatus(
TaskState.WORKING,
new Message.Builder()
.role(Message.Role.AGENT)
.parts(Collections.singletonList(new TextPart("content")))
.messageId("messageId")
.build(),
null);
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent(
minimalTask.getId(),
newStatus,
minimalTask.getContextId(),
false,
new HashMap<>());
Task saved = taskManager.saveTaskEvent(event);
Task updated = taskManager.getTask();
assertNotSame(initialTask, updated);
assertSame(updated, saved);
assertEquals(initialTask.getId(), updated.getId());
assertEquals(initialTask.getContextId(), updated.getContextId());
// TODO type does not get unmarshalled
//assertEquals(initialTask.getType(), updated.getType());
assertSame(newStatus, updated.getStatus());
}
@Test
public void testSaveTaskEventArtifactUpdate() throws A2AServerException {
Task initialTask = minimalTask;
Artifact newArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("content")))
.build();
TaskArtifactUpdateEvent event = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(newArtifact)
.build();
Task saved = taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
assertSame(updatedTask, saved);
assertNotSame(initialTask, updatedTask);
assertEquals(initialTask.getId(), updatedTask.getId());
assertEquals(initialTask.getContextId(), updatedTask.getContextId());
assertSame(initialTask.getStatus().state(), updatedTask.getStatus().state());
assertEquals(1, updatedTask.getArtifacts().size());
assertEquals(newArtifact, updatedTask.getArtifacts().get(0));
}
@Test
public void testEnsureTaskExisting() {
// This tests the 'working case' of the internal logic to check a task being updated existas
// We are already testing that
}
@Test
public void testEnsureTaskNonExistentForStatusUpdate() throws A2AServerException {
// Tests that an update event instantiates a new task and that
TaskManager taskManagerWithoutId = new TaskManager(null, null, taskStore, null);
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("new-task")
.contextId("some-context")
.status(new TaskStatus(TaskState.SUBMITTED))
.isFinal(false)
.build();
Task task = taskManagerWithoutId.saveTaskEvent(event);
assertEquals(event.getTaskId(), taskManagerWithoutId.getTaskId());
assertEquals(event.getContextId(), taskManagerWithoutId.getContextId());
Task newTask = taskManagerWithoutId.getTask();
assertEquals(event.getTaskId(), newTask.getId());
assertEquals(event.getContextId(), newTask.getContextId());
assertEquals(TaskState.SUBMITTED, newTask.getStatus().state());
assertSame(newTask, task);
}
@Test
public void testSaveTaskEventNewTaskNoTaskId() throws A2AServerException {
TaskManager taskManagerWithoutId = new TaskManager(null, null, taskStore, null);
Task task = new Task.Builder()
.id("new-task-id")
.contextId("some-context")
.status(new TaskStatus(TaskState.WORKING))
.build();
Task saved = taskManagerWithoutId.saveTaskEvent(task);
assertEquals(task.getId(), taskManagerWithoutId.getTaskId());
assertEquals(task.getContextId(), taskManagerWithoutId.getContextId());
Task retrieved = taskManagerWithoutId.getTask();
assertSame(task, retrieved);
assertSame(retrieved, saved);
}
@Test
public void testGetTaskNoTaskId() {
TaskManager taskManagerWithoutId = new TaskManager(null, null, taskStore, null);
Task retrieved = taskManagerWithoutId.getTask();
assertNull(retrieved);
}
// Additional tests for missing coverage scenarios
@Test
public void testTaskArtifactUpdateEventAppendTrueWithExistingArtifact() throws A2AServerException {
// Setup: Create a task with an existing artifact
Task initialTask = minimalTask;
Artifact existingArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("existing content")))
.build();
Task taskWithArtifact = new Task.Builder(initialTask)
.artifacts(Collections.singletonList(existingArtifact))
.build();
taskStore.save(taskWithArtifact);
// Test: Append new parts to existing artifact
Artifact newArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("new content")))
.build();
TaskArtifactUpdateEvent event = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(newArtifact)
.append(true)
.build();
Task updatedTask = taskManager.saveTaskEvent(event);
assertEquals(1, updatedTask.getArtifacts().size());
Artifact updatedArtifact = updatedTask.getArtifacts().get(0);
assertEquals("artifact-id", updatedArtifact.artifactId());
assertEquals(2, updatedArtifact.parts().size());
assertEquals("existing content", ((TextPart) updatedArtifact.parts().get(0)).getText());
assertEquals("new content", ((TextPart) updatedArtifact.parts().get(1)).getText());
}
@Test
public void testTaskArtifactUpdateEventAppendTrueWithoutExistingArtifact() throws A2AServerException {
// Setup: Create a task without artifacts
Task initialTask = minimalTask;
taskStore.save(initialTask);
// Test: Try to append to non-existent artifact (should be ignored)
Artifact newArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("new content")))
.build();
TaskArtifactUpdateEvent event = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(newArtifact)
.append(true)
.build();
Task saved = taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
// Should have no artifacts since append was ignored
assertEquals(0, updatedTask.getArtifacts().size());
}
@Test
public void testTaskArtifactUpdateEventAppendFalseWithExistingArtifact() throws A2AServerException {
// Setup: Create a task with an existing artifact
Task initialTask = minimalTask;
Artifact existingArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("existing content")))
.build();
Task taskWithArtifact = new Task.Builder(initialTask)
.artifacts(Collections.singletonList(existingArtifact))
.build();
taskStore.save(taskWithArtifact);
// Test: Replace existing artifact (append=false)
Artifact newArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("replacement content")))
.build();
TaskArtifactUpdateEvent event = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(newArtifact)
.append(false)
.build();
Task saved = taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
assertEquals(1, updatedTask.getArtifacts().size());
Artifact updatedArtifact = updatedTask.getArtifacts().get(0);
assertEquals("artifact-id", updatedArtifact.artifactId());
assertEquals(1, updatedArtifact.parts().size());
assertEquals("replacement content", ((TextPart) updatedArtifact.parts().get(0)).getText());
}
@Test
public void testTaskArtifactUpdateEventAppendNullWithExistingArtifact() throws A2AServerException {
// Setup: Create a task with an existing artifact
Task initialTask = minimalTask;
Artifact existingArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("existing content")))
.build();
Task taskWithArtifact = new Task.Builder(initialTask)
.artifacts(Collections.singletonList(existingArtifact))
.build();
taskStore.save(taskWithArtifact);
// Test: Replace existing artifact (append=null, defaults to false)
Artifact newArtifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("replacement content")))
.build();
TaskArtifactUpdateEvent event = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(newArtifact)
.build(); // append is null
Task saved = taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
assertEquals(1, updatedTask.getArtifacts().size());
Artifact updatedArtifact = updatedTask.getArtifacts().get(0);
assertEquals("artifact-id", updatedArtifact.artifactId());
assertEquals(1, updatedArtifact.parts().size());
assertEquals("replacement content", ((TextPart) updatedArtifact.parts().get(0)).getText());
}
@Test
public void testAddingTaskWithDifferentIdFails() {
// Test that adding a task with a different id from the taskmanager's taskId fails
TaskManager taskManagerWithId = new TaskManager("task-abc", "session-xyz", taskStore, null);
Task differentTask = new Task.Builder()
.id("different-task-id")
.contextId("session-xyz")
.status(new TaskStatus(TaskState.SUBMITTED))
.build();
assertThrows(A2AServerException.class, () -> {
taskManagerWithId.saveTaskEvent(differentTask);
});
}
@Test
public void testAddingTaskWithDifferentIdViaStatusUpdateFails() {
// Test that adding a status update with different taskId fails
TaskManager taskManagerWithId = new TaskManager("task-abc", "session-xyz", taskStore, null);
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("different-task-id")
.contextId("session-xyz")
.status(new TaskStatus(TaskState.WORKING))
.isFinal(false)
.build();
assertThrows(A2AServerException.class, () -> {
taskManagerWithId.saveTaskEvent(event);
});
}
@Test
public void testAddingTaskWithDifferentIdViaArtifactUpdateFails() {
// Test that adding an artifact update with different taskId fails
TaskManager taskManagerWithId = new TaskManager("task-abc", "session-xyz", taskStore, null);
Artifact artifact = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("content")))
.build();
TaskArtifactUpdateEvent event = new TaskArtifactUpdateEvent.Builder()
.taskId("different-task-id")
.contextId("session-xyz")
.artifact(artifact)
.build();
assertThrows(A2AServerException.class, () -> {
taskManagerWithId.saveTaskEvent(event);
});
}
@Test
public void testTaskWithNoMessageUsesInitialMessage() throws A2AServerException {
// Test that adding a task with no message, and there is a TaskManager.initialMessage,
// the initialMessage gets used
Message initialMessage = new Message.Builder()
.role(Message.Role.USER)
.parts(Collections.singletonList(new TextPart("initial message")))
.messageId("initial-msg-id")
.build();
TaskManager taskManagerWithInitialMessage = new TaskManager(null, null, taskStore, initialMessage);
// Use a status update event instead of a Task to trigger createTask
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("new-task-id")
.contextId("some-context")
.status(new TaskStatus(TaskState.SUBMITTED))
.isFinal(false)
.build();
Task saved = taskManagerWithInitialMessage.saveTaskEvent(event);
Task retrieved = taskManagerWithInitialMessage.getTask();
// Check that the task has the initial message in its history
assertNotNull(retrieved.getHistory());
assertEquals(1, retrieved.getHistory().size());
Message historyMessage = retrieved.getHistory().get(0);
assertEquals(initialMessage.getMessageId(), historyMessage.getMessageId());
assertEquals(initialMessage.getRole(), historyMessage.getRole());
assertEquals("initial message", ((TextPart) historyMessage.getParts().get(0)).getText());
}
@Test
public void testTaskWithMessageDoesNotUseInitialMessage() throws A2AServerException {
// Test that adding a task with a message does not use the initial message
Message initialMessage = new Message.Builder()
.role(Message.Role.USER)
.parts(Collections.singletonList(new TextPart("initial message")))
.messageId("initial-msg-id")
.build();
TaskManager taskManagerWithInitialMessage = new TaskManager(null, null, taskStore, initialMessage);
Message taskMessage = new Message.Builder()
.role(Message.Role.AGENT)
.parts(Collections.singletonList(new TextPart("task message")))
.messageId("task-msg-id")
.build();
// Use TaskStatusUpdateEvent to trigger the creation of a task, which will check if the initialMessage is used.
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("new-task-id")
.contextId("some-context")
.status(new TaskStatus(TaskState.SUBMITTED, taskMessage, null))
.isFinal(false)
.build();
Task saved = taskManagerWithInitialMessage.saveTaskEvent(event);
Task retrieved = taskManagerWithInitialMessage.getTask();
// There should now be a history containing the initialMessage
// But the current message (taskMessage) should be in the state, not in the history
assertNotNull(retrieved.getHistory());
assertEquals(1, retrieved.getHistory().size());
assertEquals("initial message", ((TextPart) retrieved.getHistory().get(0).getParts().get(0)).getText());
// The message in the current state should be taskMessage
assertNotNull(retrieved.getStatus().message());
assertEquals("task message", ((TextPart) retrieved.getStatus().message().getParts().get(0)).getText());
}
@Test
public void testMultipleArtifactsWithSameArtifactId() throws A2AServerException {
// Test handling of multiple artifacts with the same artifactId
Task initialTask = minimalTask;
taskStore.save(initialTask);
// Add first artifact
Artifact artifact1 = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("content 1")))
.build();
TaskArtifactUpdateEvent event1 = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(artifact1)
.build();
taskManager.saveTaskEvent(event1);
// Add second artifact with same artifactId (should replace the first)
Artifact artifact2 = new Artifact.Builder()
.artifactId("artifact-id")
.name("artifact-2")
.parts(Collections.singletonList(new TextPart("content 2")))
.build();
TaskArtifactUpdateEvent event2 = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(artifact2)
.build();
taskManager.saveTaskEvent(event2);
Task updatedTask = taskManager.getTask();
assertEquals(1, updatedTask.getArtifacts().size());
Artifact finalArtifact = updatedTask.getArtifacts().get(0);
assertEquals("artifact-id", finalArtifact.artifactId());
assertEquals("artifact-2", finalArtifact.name());
assertEquals("content 2", ((TextPart) finalArtifact.parts().get(0)).getText());
}
@Test
public void testMultipleArtifactsWithDifferentArtifactIds() throws A2AServerException {
// Test handling of multiple artifacts with different artifactIds
Task initialTask = minimalTask;
taskStore.save(initialTask);
// Add first artifact
Artifact artifact1 = new Artifact.Builder()
.artifactId("artifact-id-1")
.name("artifact-1")
.parts(Collections.singletonList(new TextPart("content 1")))
.build();
TaskArtifactUpdateEvent event1 = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(artifact1)
.build();
taskManager.saveTaskEvent(event1);
// Add second artifact with different artifactId (should be added)
Artifact artifact2 = new Artifact.Builder()
.artifactId("artifact-id-2")
.name("artifact-2")
.parts(Collections.singletonList(new TextPart("content 2")))
.build();
TaskArtifactUpdateEvent event2 = new TaskArtifactUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.artifact(artifact2)
.build();
taskManager.saveTaskEvent(event2);
Task updatedTask = taskManager.getTask();
assertEquals(2, updatedTask.getArtifacts().size());
// Verify both artifacts are present
List<Artifact> artifacts = updatedTask.getArtifacts();
assertTrue(artifacts.stream()
.anyMatch(a -> "artifact-id-1".equals(a.artifactId())
&& "content 1".equals(((TextPart) a.parts().get(0)).getText()))
, "Artifact 1 should be present");
assertTrue(artifacts.stream()
.anyMatch(a -> "artifact-id-2".equals(a.artifactId())
&& "content 2".equals(((TextPart) a.parts().get(0)).getText()))
, "Artifact 2 should be present");
}
@Test
public void testInvalidTaskIdValidation() {
// Test that creating TaskManager with null taskId is allowed (Python allows None)
TaskManager taskManagerWithNullId = new TaskManager(null, "context", taskStore, null);
assertNull(taskManagerWithNullId.getTaskId());
// Test that empty string task ID is handled (Java doesn't have explicit validation like Python)
TaskManager taskManagerWithEmptyId = new TaskManager("", "context", taskStore, null);
assertEquals("", taskManagerWithEmptyId.getTaskId());
}
@Test
public void testSaveTaskEventMetadataUpdate() throws A2AServerException {
// Test that metadata from TaskStatusUpdateEvent gets saved to the task
Task initialTask = minimalTask;
taskStore.save(initialTask);
Map<String, Object> newMetadata = new HashMap<>();
newMetadata.put("meta_key_test", "meta_value_test");
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.status(new TaskStatus(TaskState.WORKING))
.isFinal(false)
.metadata(newMetadata)
.build();
taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
assertEquals(newMetadata, updatedTask.getMetadata());
}
@Test
public void testSaveTaskEventMetadataUpdateNull() throws A2AServerException {
// Test that null metadata in TaskStatusUpdateEvent doesn't affect task
Task initialTask = minimalTask;
taskStore.save(initialTask);
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.status(new TaskStatus(TaskState.WORKING))
.isFinal(false)
.metadata(null)
.build();
taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
// Should preserve original task's metadata (which is likely null for minimal task)
assertNull(updatedTask.getMetadata());
}
@Test
public void testSaveTaskEventMetadataMergeExisting() throws A2AServerException {
// Test that metadata update merges with existing metadata
Map<String, Object> originalMetadata = new HashMap<>();
originalMetadata.put("original_key", "original_value");
Task taskWithMetadata = new Task.Builder(minimalTask)
.metadata(originalMetadata)
.build();
taskStore.save(taskWithMetadata);
Map<String, Object> newMetadata = new HashMap<>();
newMetadata.put("new_key", "new_value");
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId(minimalTask.getId())
.contextId(minimalTask.getContextId())
.status(new TaskStatus(TaskState.WORKING))
.isFinal(false)
.metadata(newMetadata)
.build();
taskManager.saveTaskEvent(event);
Task updatedTask = taskManager.getTask();
Map<String, Object> mergedMetadata = new HashMap<>(originalMetadata);
mergedMetadata.putAll(newMetadata);
assertEquals(mergedMetadata, updatedTask.getMetadata());
}
@Test
public void testCreateTaskWithInitialMessage() throws A2AServerException {
// Test equivalent of _init_task_obj functionality
Message initialMessage = new Message.Builder()
.role(Message.Role.USER)
.parts(Collections.singletonList(new TextPart("initial message")))
.messageId("initial-msg-id")
.build();
TaskManager taskManagerWithMessage = new TaskManager(null, null, taskStore, initialMessage);
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("new-task-id")
.contextId("some-context")
.status(new TaskStatus(TaskState.SUBMITTED))
.isFinal(false)
.build();
Task savedTask = taskManagerWithMessage.saveTaskEvent(event);
// Verify task was created properly
assertNotNull(savedTask);
assertEquals("new-task-id", savedTask.getId());
assertEquals("some-context", savedTask.getContextId());
assertEquals(TaskState.SUBMITTED, savedTask.getStatus().state());
// Verify initial message is in history
assertNotNull(savedTask.getHistory());
assertEquals(1, savedTask.getHistory().size());
Message historyMessage = savedTask.getHistory().get(0);
assertEquals("initial-msg-id", historyMessage.getMessageId());
assertEquals("initial message", ((TextPart) historyMessage.getParts().get(0)).getText());
}
@Test
public void testCreateTaskWithoutInitialMessage() throws A2AServerException {
// Test task creation without initial message
TaskManager taskManagerWithoutMessage = new TaskManager(null, null, taskStore, null);
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("new-task-id")
.contextId("some-context")
.status(new TaskStatus(TaskState.SUBMITTED))
.isFinal(false)
.build();
Task savedTask = taskManagerWithoutMessage.saveTaskEvent(event);
// Verify task was created properly
assertNotNull(savedTask);
assertEquals("new-task-id", savedTask.getId());
assertEquals("some-context", savedTask.getContextId());
assertEquals(TaskState.SUBMITTED, savedTask.getStatus().state());
// Verify no history since there was no initial message
assertTrue(savedTask.getHistory().isEmpty());
}
@Test
public void testSaveTaskInternal() throws A2AServerException {
// Test equivalent of _save_task functionality through saveTaskEvent
TaskManager taskManagerWithoutId = new TaskManager(null, null, taskStore, null);
Task newTask = new Task.Builder()
.id("test-task-id")
.contextId("test-context")
.status(new TaskStatus(TaskState.WORKING))
.build();
Task savedTask = taskManagerWithoutId.saveTaskEvent(newTask);
// Verify internal state was updated
assertEquals("test-task-id", taskManagerWithoutId.getTaskId());
assertEquals("test-context", taskManagerWithoutId.getContextId());
assertSame(savedTask, taskManagerWithoutId.getTask());
}
@Test
public void testUpdateWithMessage() throws A2AServerException {
Message initialMessage = new Message.Builder()
.role(Message.Role.USER)
.parts(Collections.singletonList(new TextPart("initial message")))
.messageId("initial-msg-id")
.build();
TaskManager taskManagerWithInitialMessage = new TaskManager(null, null, taskStore, initialMessage);
Message taskMessage = new Message.Builder()
.role(Message.Role.AGENT)
.parts(Collections.singletonList(new TextPart("task message")))
.messageId("task-msg-id")
.build();
TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder()
.taskId("new-task-id")
.contextId("some-context")
.status(new TaskStatus(TaskState.SUBMITTED, taskMessage, null))
.isFinal(false)
.build();
Task saved = taskManagerWithInitialMessage.saveTaskEvent(event);
Message updateMessage = new Message.Builder()
.role(Message.Role.USER)
.parts(Collections.singletonList(new TextPart("update message")))
.messageId("update-msg-id")
.build();
Task updated = taskManagerWithInitialMessage.updateWithMessage(updateMessage, saved);
// There should now be a history containing the initialMessage, task message and update message
assertNotNull(updated.getHistory());
assertEquals(3, updated.getHistory().size());
assertEquals("initial message", ((TextPart) updated.getHistory().get(0).getParts().get(0)).getText());
// The message in the current state should be null
assertNull(updated.getStatus().message());
assertEquals("task message", ((TextPart) updated.getHistory().get(1).getParts().get(0)).getText());
assertEquals("update message", ((TextPart) updated.getHistory().get(2).getParts().get(0)).getText());
}
}