|
| 1 | +// Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.volcengine.ark.runtime.service; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | +import static org.junit.Assert.assertFalse; |
| 8 | +import static org.junit.Assert.assertSame; |
| 9 | +import static org.junit.Assert.assertTrue; |
| 10 | + |
| 11 | +import com.volcengine.ark.runtime.models.session.AgentWithUpgrades; |
| 12 | +import com.volcengine.ark.runtime.models.session.CreateSessionUpgradeRequest; |
| 13 | +import com.volcengine.ark.runtime.models.session.Session; |
| 14 | +import io.reactivex.Single; |
| 15 | +import java.lang.annotation.Annotation; |
| 16 | +import java.lang.reflect.InvocationHandler; |
| 17 | +import java.lang.reflect.Method; |
| 18 | +import java.lang.reflect.Proxy; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | +import org.junit.Test; |
| 22 | +import retrofit2.http.POST; |
| 23 | +import retrofit2.http.Path; |
| 24 | + |
| 25 | +public class ArkServiceSessionUpgradeTest { |
| 26 | + @Test |
| 27 | + public void upgradeSessionPassesRequestAndReturnsSession() { |
| 28 | + CapturingArkApi handler = new CapturingArkApi(); |
| 29 | + ArkService service = new ArkService(api(handler)); |
| 30 | + CreateSessionUpgradeRequest request = request(); |
| 31 | + |
| 32 | + Session out = service.upgradeSession("sess-1", request); |
| 33 | + |
| 34 | + assertEquals("sess-1", out.getId()); |
| 35 | + assertEquals("sess-1", handler.args[0]); |
| 36 | + assertSame(request, handler.args[1]); |
| 37 | + assertTrue(((Map<?, ?>) handler.args[2]).isEmpty()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void upgradeSessionApiKeepsPostContract() throws Exception { |
| 42 | + Method method = ArkApi.class.getMethod( |
| 43 | + "upgradeSession", |
| 44 | + String.class, |
| 45 | + CreateSessionUpgradeRequest.class, |
| 46 | + Map.class |
| 47 | + ); |
| 48 | + |
| 49 | + assertEquals("/api/v3/sessions/{sessionId}/upgrades", method.getAnnotation(POST.class).value()); |
| 50 | + assertEquals("sessionId", findAnnotation(method.getParameterAnnotations()[0], Path.class).value()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void upgradeSessionPreservesExplicitEmptyArrays() throws Exception { |
| 55 | + String json = ArkService.defaultObjectMapper().writeValueAsString(request()); |
| 56 | + |
| 57 | + assertTrue(json.contains("\"tools\":[]")); |
| 58 | + assertTrue(json.contains("\"vault_ids\":[]")); |
| 59 | + assertFalse(json.contains("initial_events")); |
| 60 | + } |
| 61 | + |
| 62 | + private static CreateSessionUpgradeRequest request() { |
| 63 | + AgentWithUpgrades agent = AgentWithUpgrades.builder() |
| 64 | + .type(AgentWithUpgrades.TypeEnum.AGENT_WITH_UPGRADES) |
| 65 | + .id("agent-1") |
| 66 | + .tools(Collections.emptyList()) |
| 67 | + .build(); |
| 68 | + return CreateSessionUpgradeRequest.builder() |
| 69 | + .agent(agent) |
| 70 | + .vaultIds(Collections.emptyList()) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + |
| 74 | + private static ArkApi api(CapturingArkApi handler) { |
| 75 | + return (ArkApi) Proxy.newProxyInstance( |
| 76 | + ArkApi.class.getClassLoader(), |
| 77 | + new Class<?>[] {ArkApi.class}, |
| 78 | + handler |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + private static <T extends Annotation> T findAnnotation(Annotation[] annotations, Class<T> type) { |
| 83 | + for (Annotation annotation : annotations) { |
| 84 | + if (type.isInstance(annotation)) { |
| 85 | + return type.cast(annotation); |
| 86 | + } |
| 87 | + } |
| 88 | + throw new AssertionError("missing annotation " + type.getSimpleName()); |
| 89 | + } |
| 90 | + |
| 91 | + private static final class CapturingArkApi implements InvocationHandler { |
| 92 | + private Object[] args; |
| 93 | + |
| 94 | + @Override |
| 95 | + public Object invoke(Object proxy, Method method, Object[] args) { |
| 96 | + if (method.getDeclaringClass() == Object.class) { |
| 97 | + return objectMethod(proxy, method, args); |
| 98 | + } |
| 99 | + if (!"upgradeSession".equals(method.getName())) { |
| 100 | + throw new AssertionError("unexpected API call: " + method.getName()); |
| 101 | + } |
| 102 | + this.args = args; |
| 103 | + return Single.just(new Session().id("sess-1")); |
| 104 | + } |
| 105 | + |
| 106 | + private Object objectMethod(Object proxy, Method method, Object[] args) { |
| 107 | + if ("toString".equals(method.getName())) { |
| 108 | + return "capturing ArkApi proxy"; |
| 109 | + } |
| 110 | + if ("hashCode".equals(method.getName())) { |
| 111 | + return System.identityHashCode(proxy); |
| 112 | + } |
| 113 | + if ("equals".equals(method.getName())) { |
| 114 | + return proxy == args[0]; |
| 115 | + } |
| 116 | + throw new AssertionError("unexpected Object method: " + method.getName()); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments