a2a_agent_test.go

Overview

The a2a_agent_test.go file provides comprehensive unit and integration tests for the remote agent communication implementation using the Agent-To-Agent (A2A) protocol. It validates the interaction between local ADK agents and remote agents over the A2A protocol, focusing on:

This test file directly exercises and verifies the functionality described in the [A2A Agent Implementation](80565) topic, ensuring that the client-side remote agent proxy correctly sends and receives A2A protocol messages and events, integrates with session management, and respects agent lifecycle semantics.


Key Types and Utilities

mockA2AExecutor


Helper Functions


Test Suites and Their Purposes

TestRemoteAgent_ADK2ADK


TestRemoteAgent_ADK2A2A


TestRemoteAgent_EmptyResultForEmptySession


TestRemoteAgent_ResolvesAgentCard


TestRemoteAgent_ErrorEventIfNoCompatibleTransport


TestRemoteAgent_ErrorEventOnServerError


Important Implementation Details


Interaction with Other Parts of the Application


Visual Diagram: Flow of Core Test Setup and Execution

flowchart TD
Start[Test Initialization] --> SetupServer[Start bufconn gRPC Server]
SetupServer --> CreateExecutor["Create AgentExecutor (mock or replay)"]
CreateExecutor --> StartServer[Run gRPC Server with Executor]
StartServer --> CreateClient[Create A2A Client Factory]
CreateClient --> CreateAgent[Create Remote A2A Agent instance]
CreateAgent --> CreateInvocationCtx[Create InvocationContext with session events]
CreateInvocationCtx --> RunAgent[Run Remote Agent with InvocationContext]
RunAgent --> CollectEvents[Collect streamed Session Events]
CollectEvents --> Verify[Verify Events and Responses]
Verify --> End[Test Completion]

This flowchart outlines the common steps taken in each test case to start the in-memory server, create the remote agent, invoke it with a session context, collect streamed events, and verify results.


Summary of Functions and Their Parameters

Function

Parameters

Returns

Description

startA2AServer

t *testing.T, agentExecutor a2asrv.AgentExecutor, listener *bufconn.Listener

None (runs server)

Starts a gRPC server with the given executor on the provided in-memory listener.

newTestClientFactory

listener *bufconn.Listener

*a2aclient.Factory

Creates an A2A client factory configured to dial the in-memory server listener.

newA2ARemoteAgent

t *testing.T, name string, listener *bufconn.Listener

agent.Agent

Creates a remote A2A agent configured to communicate over the given listener.

newInvocationContext

t *testing.T, events []*session.Event

agent.InvocationContext

Creates a new invocation context with a session populated by the given events.

runAndCollect

ic agent.InvocationContext, agnt agent.Agent

[]*session.Event, error

Runs the agent and collects all session events yielded during invocation.

newADKEventReplay

t *testing.T, events []*session.Event

a2asrv.AgentExecutor

Creates an executor that replays a fixed sequence of ADK events for testing server responses.

newA2AEventReplay

t *testing.T, events []a2a.Event

a2asrv.AgentExecutor

Creates a mock executor that emits predefined A2A events to the queue during execution.

newUserHello

None

*session.Event

Generates a simple user event with content "hello".

newFinalStatusUpdate

task *a2a.Task, state a2a.TaskState, msgParts ...a2a.Part

*a2a.TaskStatusUpdateEvent

Creates a final status update event for the task with optional message parts.


Usage Examples in Tests

Example usage pattern in tests:

listener := bufconn.Listen(connBufSize)
executor := newADKEventReplay(t, remoteEvents) // or newA2AEventReplay for A2A events
go startA2AServer(t, executor, listener)

remoteAgent := newA2ARemoteAgent(t, "agent-name", listener)
ictx := newInvocationContext(t, []*session.Event{newUserHello()})
gotEvents, err := runAndCollect(ictx, remoteAgent)
if err != nil {
    t.Fatalf("agent.Run() error = %v", err)
}
// Assertions on gotEvents and converted LLM responses follow...

End of a2a_agent_test.go Documentation