|
| 1 | +package dev.arcp.client; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 7 | +import dev.arcp.core.auth.Auth; |
| 8 | +import dev.arcp.core.capabilities.AgentDescriptor; |
| 9 | +import dev.arcp.core.capabilities.Feature; |
| 10 | +import dev.arcp.core.credentials.Credential; |
| 11 | +import dev.arcp.core.credentials.CredentialId; |
| 12 | +import dev.arcp.core.credentials.CredentialScheme; |
| 13 | +import dev.arcp.core.ids.JobId; |
| 14 | +import dev.arcp.core.ids.SessionId; |
| 15 | +import dev.arcp.core.lease.LeaseConstraints; |
| 16 | +import dev.arcp.core.messages.JobAccepted; |
| 17 | +import dev.arcp.core.messages.JobSummary; |
| 18 | +import dev.arcp.core.transport.MemoryTransport; |
| 19 | +import dev.arcp.core.wire.ArcpMapper; |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.EnumSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.concurrent.ScheduledExecutorService; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +class ClientValueTypesTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + void pageSubscribeOptionsAndSessionDefensivelyCopyInputs() { |
| 33 | + Page<JobSummary> empty = Page.empty(); |
| 34 | + assertThat(empty.items()).isEmpty(); |
| 35 | + assertThat(empty.hasNext()).isFalse(); |
| 36 | + |
| 37 | + Page<String> page = new Page<>(List.of("a", "b"), "next"); |
| 38 | + assertThat(page.items()).containsExactly("a", "b"); |
| 39 | + assertThat(page.hasNext()).isTrue(); |
| 40 | + |
| 41 | + assertThat(SubscribeOptions.live()).isEqualTo(new SubscribeOptions(false, 0)); |
| 42 | + assertThat(SubscribeOptions.withHistory(4)).isEqualTo(new SubscribeOptions(true, 4)); |
| 43 | + |
| 44 | + EnumSet<Feature> mutableFeatures = EnumSet.of(Feature.SUBSCRIBE); |
| 45 | + List<AgentDescriptor> agents = List.of(new AgentDescriptor("echo", List.of("1.0.0"), "1.0.0")); |
| 46 | + Session session = |
| 47 | + new Session( |
| 48 | + SessionId.of("sess_client"), mutableFeatures, "resume", Duration.ofSeconds(30), agents); |
| 49 | + mutableFeatures.add(Feature.LIST_JOBS); |
| 50 | + |
| 51 | + assertThat(session.negotiatedFeatures()).containsExactly(Feature.SUBSCRIBE); |
| 52 | + assertThat(session.availableAgents()).isEqualTo(agents); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void jobHandleCredentialDefaultReturnsOptionalCredentials() { |
| 57 | + Credential credential = |
| 58 | + new Credential( |
| 59 | + CredentialId.of("cred_1"), |
| 60 | + CredentialScheme.BEARER, |
| 61 | + "secret", |
| 62 | + "https://api.example.test", |
| 63 | + null, |
| 64 | + null); |
| 65 | + JobHandle withCredentials = |
| 66 | + new FakeJobHandle( |
| 67 | + new JobAccepted( |
| 68 | + JobId.of("job_with_creds"), |
| 69 | + "echo@1.0.0", |
| 70 | + null, |
| 71 | + null, |
| 72 | + null, |
| 73 | + List.of(credential), |
| 74 | + Instant.parse("2026-05-25T12:00:00Z"), |
| 75 | + null)); |
| 76 | + assertThat(withCredentials.credentials()).contains(List.of(credential)); |
| 77 | + |
| 78 | + JobHandle withoutCredentials = |
| 79 | + new FakeJobHandle( |
| 80 | + new JobAccepted( |
| 81 | + JobId.of("job_without_creds"), |
| 82 | + "echo@1.0.0", |
| 83 | + null, |
| 84 | + null, |
| 85 | + null, |
| 86 | + null, |
| 87 | + Instant.parse("2026-05-25T12:00:00Z"), |
| 88 | + null)); |
| 89 | + assertThat(withoutCredentials.credentials()).isEqualTo(Optional.empty()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void clientBuilderAndJobSubmitHelpersPopulateConfiguration() { |
| 94 | + MemoryTransport.Pair pair = MemoryTransport.pair(); |
| 95 | + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 96 | + try (ArcpClient client = |
| 97 | + ArcpClient.builder(pair.client()) |
| 98 | + .mapper(ArcpMapper.shared()) |
| 99 | + .client("custom-client", "2.0.0") |
| 100 | + .auth(Auth.anonymous()) |
| 101 | + .bearer("token") |
| 102 | + .features(EnumSet.of(Feature.SUBSCRIBE)) |
| 103 | + .autoAck(false) |
| 104 | + .ackInterval(Duration.ofSeconds(1)) |
| 105 | + .scheduler(scheduler) |
| 106 | + .resumeToken("resume-token") |
| 107 | + .lastEventSeq(7) |
| 108 | + .build()) { |
| 109 | + assertThat(client.lastSeenSeq()).isEqualTo(-1); |
| 110 | + assertThat( |
| 111 | + ArcpClient.jobSubmit("echo@1.0.0", JsonNodeFactory.instance.objectNode()) |
| 112 | + .agent() |
| 113 | + .wire()) |
| 114 | + .isEqualTo("echo@1.0.0"); |
| 115 | + assertThat( |
| 116 | + ArcpClient.jobSubmit( |
| 117 | + "echo@1.0.0", |
| 118 | + JsonNodeFactory.instance.objectNode(), |
| 119 | + null, |
| 120 | + LeaseConstraints.of(Instant.now().plusSeconds(60)), |
| 121 | + "key", |
| 122 | + 5) |
| 123 | + .idempotencyKey()) |
| 124 | + .isEqualTo("key"); |
| 125 | + assertThatThrownBy( |
| 126 | + () -> |
| 127 | + ArcpClient.jobSubmit( |
| 128 | + "echo@1.0.0", |
| 129 | + JsonNodeFactory.instance.objectNode(), |
| 130 | + null, |
| 131 | + LeaseConstraints.of(Instant.now().minusSeconds(60)), |
| 132 | + null, |
| 133 | + null)) |
| 134 | + .isInstanceOf(IllegalArgumentException.class) |
| 135 | + .hasMessageContaining("expires_at"); |
| 136 | + } finally { |
| 137 | + scheduler.shutdownNow(); |
| 138 | + pair.runtime().close(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private record FakeJobHandle(JobAccepted accepted) implements JobHandle { |
| 143 | + @Override |
| 144 | + public JobId jobId() { |
| 145 | + return accepted.jobId(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public String resolvedAgent() { |
| 150 | + return accepted.agent(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public java.util.concurrent.Flow.Publisher<dev.arcp.core.events.EventBody> events() { |
| 155 | + return subscriber -> {}; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public java.util.concurrent.CompletableFuture<dev.arcp.core.messages.JobResult> result() { |
| 160 | + return new java.util.concurrent.CompletableFuture<>(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void cancel() {} |
| 165 | + } |
| 166 | +} |
0 commit comments