|
| 1 | +package org.a2aproject.sdk.server.tasks; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.a2aproject.sdk.spec.Artifact; |
| 12 | +import org.a2aproject.sdk.spec.ListTasksParams; |
| 13 | +import org.a2aproject.sdk.spec.Message; |
| 14 | +import org.a2aproject.sdk.spec.Task; |
| 15 | +import org.a2aproject.sdk.spec.TaskState; |
| 16 | +import org.a2aproject.sdk.spec.TaskStatus; |
| 17 | +import org.a2aproject.sdk.spec.TextPart; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +public class InMemoryTaskStoreTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testSaveAndGet() { |
| 24 | + InMemoryTaskStore store = new InMemoryTaskStore(); |
| 25 | + Task task = sampleTask("task-abc"); |
| 26 | + |
| 27 | + store.save(task, false); |
| 28 | + |
| 29 | + Task retrieved = store.get(task.id()); |
| 30 | + assertSame(task, retrieved); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testGetNonExistent() { |
| 35 | + InMemoryTaskStore store = new InMemoryTaskStore(); |
| 36 | + |
| 37 | + Task retrieved = store.get("nonexistent"); |
| 38 | + assertNull(retrieved); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testDelete() { |
| 43 | + InMemoryTaskStore store = new InMemoryTaskStore(); |
| 44 | + Task task = sampleTask("task-abc"); |
| 45 | + |
| 46 | + store.save(task, false); |
| 47 | + store.delete(task.id()); |
| 48 | + |
| 49 | + Task retrieved = store.get(task.id()); |
| 50 | + assertNull(retrieved); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testDeleteNonExistent() { |
| 55 | + InMemoryTaskStore store = new InMemoryTaskStore(); |
| 56 | + |
| 57 | + store.delete("non-existent"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testListTransformsHistoryAndArtifacts() { |
| 62 | + InMemoryTaskStore store = new InMemoryTaskStore(); |
| 63 | + Task task = Task.builder() |
| 64 | + .id("task-abc") |
| 65 | + .contextId("session-xyz") |
| 66 | + .status(new TaskStatus(TaskState.TASK_STATE_WORKING)) |
| 67 | + .history(List.of(sampleMessage("msg-1"), sampleMessage("msg-2"))) |
| 68 | + .artifacts(List.of(sampleArtifact("artifact-1"))) |
| 69 | + .metadata(Map.of("origin", "test")) |
| 70 | + .build(); |
| 71 | + |
| 72 | + store.save(task, false); |
| 73 | + |
| 74 | + ListTasksParams params = ListTasksParams.builder().build(); |
| 75 | + List<Task> tasks = store.list(params, null).tasks(); |
| 76 | + |
| 77 | + assertEquals(1, tasks.size()); |
| 78 | + Task listed = tasks.get(0); |
| 79 | + assertEquals(task.id(), listed.id()); |
| 80 | + assertEquals(task.contextId(), listed.contextId()); |
| 81 | + assertTrue(listed.history().isEmpty(), "Default list() should omit history"); |
| 82 | + assertTrue(listed.artifacts().isEmpty(), "Default list() should omit artifacts"); |
| 83 | + assertEquals(task.metadata(), listed.metadata()); |
| 84 | + } |
| 85 | + |
| 86 | + private static Task sampleTask(String id) { |
| 87 | + return Task.builder() |
| 88 | + .id(id) |
| 89 | + .contextId("session-xyz") |
| 90 | + .status(new TaskStatus(TaskState.TASK_STATE_SUBMITTED)) |
| 91 | + .build(); |
| 92 | + } |
| 93 | + |
| 94 | + private static Message sampleMessage(String messageId) { |
| 95 | + return Message.builder() |
| 96 | + .role(Message.Role.ROLE_USER) |
| 97 | + .parts(List.of(new TextPart("content"))) |
| 98 | + .messageId(messageId) |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + private static Artifact sampleArtifact(String artifactId) { |
| 103 | + return Artifact.builder() |
| 104 | + .artifactId(artifactId) |
| 105 | + .parts(List.of(new TextPart("artifact content"))) |
| 106 | + .build(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
0 commit comments