|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.kt.a2a.agent |
| 18 | + |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import com.google.adk.kt.a2a.android.androidA2AAgent |
| 21 | +import com.google.adk.kt.agents.InvocationContext |
| 22 | +import com.google.adk.kt.events.Event |
| 23 | +import com.google.adk.kt.sessions.Session |
| 24 | +import com.google.adk.kt.sessions.SessionKey |
| 25 | +import com.google.adk.kt.testing.DummyAgent |
| 26 | +import com.google.adk.kt.testing.userMessage |
| 27 | +import com.google.common.truth.Truth.assertThat |
| 28 | +import kotlinx.coroutines.flow.toList |
| 29 | +import kotlinx.coroutines.test.runTest |
| 30 | +import okhttp3.mockwebserver.MockResponse |
| 31 | +import okhttp3.mockwebserver.MockWebServer |
| 32 | +import okhttp3.mockwebserver.RecordedRequest |
| 33 | +import org.a2aproject.sdk.jsonrpc.common.json.JsonUtil |
| 34 | +import org.a2aproject.sdk.jsonrpc.common.wrappers.SendMessageResponse |
| 35 | +import org.a2aproject.sdk.spec.AgentCapabilities |
| 36 | +import org.a2aproject.sdk.spec.AgentCard |
| 37 | +import org.a2aproject.sdk.spec.AgentInterface |
| 38 | +import org.a2aproject.sdk.spec.Message |
| 39 | +import org.a2aproject.sdk.spec.Task |
| 40 | +import org.a2aproject.sdk.spec.TaskState |
| 41 | +import org.a2aproject.sdk.spec.TaskStatus |
| 42 | +import org.a2aproject.sdk.spec.TextPart |
| 43 | +import org.a2aproject.sdk.spec.TransportProtocol |
| 44 | +import org.junit.After |
| 45 | +import org.junit.Before |
| 46 | +import org.junit.Test |
| 47 | +import org.junit.runner.RunWith |
| 48 | + |
| 49 | +/** |
| 50 | + * On-device (ART) counterpart of `A2AAgentAndroidTest`: builds an Android A2A agent via |
| 51 | + * [androidA2AAgent] and runs a round-trip against an in-process [MockWebServer], exercising the |
| 52 | + * proto-free Android transport. |
| 53 | + * |
| 54 | + * The MockWebServer returns a real JSON-RPC response built with the SDK's own |
| 55 | + * [JsonUtil] + [SendMessageResponse], and the transport parses it back with [JsonUtil], so this |
| 56 | + * exercises the actual proto-free A2A serialization on a real Android runtime in both directions. |
| 57 | + */ |
| 58 | +@RunWith(AndroidJUnit4::class) |
| 59 | +class A2AAgentInstrumentationTest { |
| 60 | + |
| 61 | + private lateinit var server: MockWebServer |
| 62 | + |
| 63 | + @Before |
| 64 | + fun setUp() { |
| 65 | + server = MockWebServer() |
| 66 | + server.start() |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + fun tearDown() { |
| 71 | + server.shutdown() |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun runAsync_androidHttpClient_realRoundTrip_emitsAgentEventAndSendsRequest() = runTest { |
| 76 | + val agentReply = "Hello from the Android A2A agent!" |
| 77 | + |
| 78 | + // Build a real JSON-RPC `message/send` response with the SDK's own proto-free serialization: |
| 79 | + // a completed Task whose status message carries the agent text. Using a completed Task (rather |
| 80 | + // than a bare Message) lets the agent's non-streaming flow recognise the turn as terminal. |
| 81 | + val agentMessage = |
| 82 | + Message.builder() |
| 83 | + .messageId("agent-message-1") |
| 84 | + .role(Message.Role.ROLE_AGENT) |
| 85 | + .parts(listOf(TextPart(agentReply))) |
| 86 | + .build() |
| 87 | + val responseTask = |
| 88 | + Task.builder() |
| 89 | + .id("android-task-1") |
| 90 | + .contextId("android-context-1") |
| 91 | + .status(TaskStatus(TaskState.TASK_STATE_COMPLETED, agentMessage, null)) |
| 92 | + .build() |
| 93 | + val responseBody = JsonUtil.toJson(SendMessageResponse("2.0", "1", responseTask, null)) |
| 94 | + server.enqueue(MockResponse().setBody(responseBody)) |
| 95 | + val serverUrl = server.url("/a2a").toString() |
| 96 | + |
| 97 | + val agentCard = |
| 98 | + AgentCard.builder() |
| 99 | + .name("remote-agent") |
| 100 | + .description("Remote Agent") |
| 101 | + .url(serverUrl) |
| 102 | + .version("1.0.0") |
| 103 | + .defaultInputModes(listOf("text")) |
| 104 | + .defaultOutputModes(listOf("text")) |
| 105 | + .skills(listOf()) |
| 106 | + .supportedInterfaces( |
| 107 | + listOf(AgentInterface(TransportProtocol.JSONRPC.asString(), serverUrl)) |
| 108 | + ) |
| 109 | + .capabilities(AgentCapabilities.builder().streaming(false).build()) |
| 110 | + .build() |
| 111 | + |
| 112 | + val agent = androidA2AAgent(name = "remote-agent", agentCard = agentCard, streaming = false) |
| 113 | + |
| 114 | + val session = |
| 115 | + Session( |
| 116 | + key = SessionKey(appName = "demo", userId = "user", id = "session-1"), |
| 117 | + events = |
| 118 | + mutableListOf( |
| 119 | + Event(invocationId = "invocation-0", author = "user", content = userMessage("hello")) |
| 120 | + ), |
| 121 | + ) |
| 122 | + val context = InvocationContext(agent = DummyAgent(), session = session, runConfig = null) |
| 123 | + |
| 124 | + val events = agent.runAsync(context).toList() |
| 125 | + |
| 126 | + // The agent emitted an ADK Event carrying the agent text from the real round-trip. |
| 127 | + val emittedTexts = events.mapNotNull { it.content?.parts?.firstOrNull()?.text } |
| 128 | + assertThat(emittedTexts).contains(agentReply) |
| 129 | + |
| 130 | + // The user message actually traversed AndroidA2AHttpClient and reached the server. |
| 131 | + val recorded = server.takeRecordedRequestOrFail() |
| 132 | + assertThat(recorded.path).isEqualTo("/a2a") |
| 133 | + assertThat(recorded.method).isEqualTo("POST") |
| 134 | + assertThat(recorded.body.readUtf8()).contains("hello") |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun runAsync_autoFetchCard_realRoundTrip_fetchesCardThenSendsMessage() = runTest { |
| 139 | + val agentReply = "Hello from the auto-fetched Android A2A agent!" |
| 140 | + val serverUrl = server.url("/a2a").toString() |
| 141 | + |
| 142 | + val agentCard = |
| 143 | + AgentCard.builder() |
| 144 | + .name("remote-agent") |
| 145 | + .description("Remote Agent") |
| 146 | + .url(serverUrl) |
| 147 | + .version("1.0.0") |
| 148 | + .defaultInputModes(listOf("text")) |
| 149 | + .defaultOutputModes(listOf("text")) |
| 150 | + .skills(listOf()) |
| 151 | + .supportedInterfaces( |
| 152 | + listOf(AgentInterface(TransportProtocol.JSONRPC.asString(), serverUrl)) |
| 153 | + ) |
| 154 | + .capabilities(AgentCapabilities.builder().streaming(false).build()) |
| 155 | + .build() |
| 156 | + // 1) Served for the auto-fetch GET of `/.well-known/agent-card.json`. |
| 157 | + server.enqueue(MockResponse().setBody(JsonUtil.toJson(agentCard))) |
| 158 | + |
| 159 | + val agentMessage = |
| 160 | + Message.builder() |
| 161 | + .messageId("agent-message-1") |
| 162 | + .role(Message.Role.ROLE_AGENT) |
| 163 | + .parts(listOf(TextPart(agentReply))) |
| 164 | + .build() |
| 165 | + val responseTask = |
| 166 | + Task.builder() |
| 167 | + .id("android-task-1") |
| 168 | + .contextId("android-context-1") |
| 169 | + .status(TaskStatus(TaskState.TASK_STATE_COMPLETED, agentMessage, null)) |
| 170 | + .build() |
| 171 | + // 2) Served for the `message/send` POST. |
| 172 | + server.enqueue( |
| 173 | + MockResponse().setBody(JsonUtil.toJson(SendMessageResponse("2.0", "1", responseTask, null))) |
| 174 | + ) |
| 175 | + |
| 176 | + // No card supplied: the agent auto-fetches it from the well-known endpoint on-device. |
| 177 | + val agent = |
| 178 | + androidA2AAgent( |
| 179 | + name = "remote-agent", |
| 180 | + agentCardUrl = server.url("/").toString(), |
| 181 | + streaming = false, |
| 182 | + ) |
| 183 | + |
| 184 | + val session = |
| 185 | + Session( |
| 186 | + key = SessionKey(appName = "demo", userId = "user", id = "session-1"), |
| 187 | + events = |
| 188 | + mutableListOf( |
| 189 | + Event(invocationId = "invocation-0", author = "user", content = userMessage("hello")) |
| 190 | + ), |
| 191 | + ) |
| 192 | + val context = InvocationContext(agent = DummyAgent(), session = session, runConfig = null) |
| 193 | + |
| 194 | + val events = agent.runAsync(context).toList() |
| 195 | + |
| 196 | + val emittedTexts = events.mapNotNull { it.content?.parts?.firstOrNull()?.text } |
| 197 | + assertThat(emittedTexts).contains(agentReply) |
| 198 | + |
| 199 | + // The card was fetched from the well-known endpoint first, then the message was sent. |
| 200 | + val cardRequest = server.takeRecordedRequestOrFail() |
| 201 | + assertThat(cardRequest.path).isEqualTo("/.well-known/agent-card.json") |
| 202 | + assertThat(cardRequest.method).isEqualTo("GET") |
| 203 | + val sendRequest = server.takeRecordedRequestOrFail() |
| 204 | + assertThat(sendRequest.path).isEqualTo("/a2a") |
| 205 | + assertThat(sendRequest.method).isEqualTo("POST") |
| 206 | + assertThat(sendRequest.body.readUtf8()).contains("hello") |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +private fun MockWebServer.takeRecordedRequestOrFail(): RecordedRequest = |
| 211 | + try { |
| 212 | + takeRequest() |
| 213 | + } catch (e: InterruptedException) { |
| 214 | + Thread.currentThread().interrupt() |
| 215 | + throw AssertionError("Interrupted while waiting for the recorded request", e) |
| 216 | + } |
0 commit comments