|
1 | 1 | package io.a2a.server.tasks; |
2 | 2 |
|
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | + |
3 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.assertNotSame; |
5 | 8 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
6 | 10 | import static org.junit.jupiter.api.Assertions.assertNull; |
7 | 11 | import static org.junit.jupiter.api.Assertions.assertSame; |
8 | 12 | import static org.junit.jupiter.api.Assertions.assertThrows; |
9 | 13 | import static org.junit.jupiter.api.Assertions.assertTrue; |
10 | | - |
11 | | -import java.util.Collections; |
12 | | -import java.util.HashMap; |
13 | | -import java.util.List; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
14 | 16 |
|
15 | 17 | import io.a2a.spec.A2AServerException; |
16 | 18 | import io.a2a.spec.Artifact; |
|
22 | 24 | import io.a2a.spec.TaskStatusUpdateEvent; |
23 | 25 | import io.a2a.spec.TextPart; |
24 | 26 | import io.a2a.util.Utils; |
25 | | -import org.junit.jupiter.api.BeforeEach; |
26 | | -import org.junit.jupiter.api.Test; |
27 | 27 |
|
28 | 28 | public class TaskManagerTest { |
29 | 29 | private static final String TASK_JSON = """ |
@@ -209,7 +209,7 @@ public void testTaskArtifactUpdateEventAppendTrueWithExistingArtifact() throws A |
209 | 209 | .append(true) |
210 | 210 | .build(); |
211 | 211 |
|
212 | | -Task updatedTask = taskManager.saveTaskEvent(event); |
| 212 | + Task updatedTask = taskManager.saveTaskEvent(event); |
213 | 213 |
|
214 | 214 | assertEquals(1, updatedTask.getArtifacts().size()); |
215 | 215 | Artifact updatedArtifact = updatedTask.getArtifacts().get(0); |
@@ -421,17 +421,26 @@ public void testTaskWithMessageDoesNotUseInitialMessage() throws A2AServerExcept |
421 | 421 | .messageId("task-msg-id") |
422 | 422 | .build(); |
423 | 423 |
|
424 | | - Task taskWithMessage = new Task.Builder() |
425 | | - .id("new-task-id") |
| 424 | + // Use TaskStatusUpdateEvent to trigger the creation of a task, which will check if the initialMessage is used. |
| 425 | + TaskStatusUpdateEvent event = new TaskStatusUpdateEvent.Builder() |
| 426 | + .taskId("new-task-id") |
426 | 427 | .contextId("some-context") |
427 | 428 | .status(new TaskStatus(TaskState.SUBMITTED, taskMessage, null)) |
| 429 | + .isFinal(false) |
428 | 430 | .build(); |
429 | 431 |
|
430 | | - Task saved = taskManagerWithInitialMessage.saveTaskEvent(taskWithMessage); |
| 432 | + Task saved = taskManagerWithInitialMessage.saveTaskEvent(event); |
431 | 433 | Task retrieved = taskManagerWithInitialMessage.getTask(); |
432 | 434 |
|
433 | | - // Check that the task does not have the initial message in its history |
434 | | - assertNull(retrieved.getHistory()); |
| 435 | + // There should now be a history containing the initialMessage |
| 436 | + // But the current message (taskMessage) should be in the state, not in the history |
| 437 | + assertNotNull(retrieved.getHistory()); |
| 438 | + assertEquals(1, retrieved.getHistory().size()); |
| 439 | + assertEquals("initial message", ((TextPart) retrieved.getHistory().get(0).getParts().get(0)).getText()); |
| 440 | + |
| 441 | + // The message in the current state should be taskMessage |
| 442 | + assertNotNull(retrieved.getStatus().message()); |
| 443 | + assertEquals("task message", ((TextPart) retrieved.getStatus().message().getParts().get(0)).getText()); |
435 | 444 | } |
436 | 445 |
|
437 | 446 | @Test |
@@ -510,8 +519,14 @@ public void testMultipleArtifactsWithDifferentArtifactIds() throws A2AServerExce |
510 | 519 | assertEquals(2, updatedTask.getArtifacts().size()); |
511 | 520 |
|
512 | 521 | // Verify both artifacts are present |
513 | | -List<Artifact> artifacts = updatedTask.getArtifacts(); |
514 | | -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"); |
515 | | -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"); |
| 522 | + List<Artifact> artifacts = updatedTask.getArtifacts(); |
| 523 | + assertTrue(artifacts.stream() |
| 524 | + .anyMatch(a -> "artifact-id-1".equals(a.artifactId()) |
| 525 | + && "content 1".equals(((TextPart) a.parts().get(0)).getText())) |
| 526 | + , "Artifact 1 should be present"); |
| 527 | + assertTrue(artifacts.stream() |
| 528 | + .anyMatch(a -> "artifact-id-2".equals(a.artifactId()) |
| 529 | + && "content 2".equals(((TextPart) a.parts().get(0)).getText())) |
| 530 | + , "Artifact 2 should be present"); |
516 | 531 | } |
517 | 532 | } |
0 commit comments