agent_test.go

Overview

The agent_test.go file contains unit tests and helper implementations for validating the behavior of the Sequential Agent within the Agent Workflow Management system. Specifically, it tests the construction, execution, and error scenarios of the sequentialagent package, which implements an agent that runs its sub-agents sequentially exactly once per invocation.

This test suite ensures:

In addition to tests, the file includes mock implementations of LLM models (FakeLLM) and utility functions to create custom or sequential agents for testing purposes.


Detailed Explanation of Components

Test Function: TestNewSequentialAgent

Purpose

Validates the creation and runtime execution of a SequentialAgent with various configurations of sub-agents, including nested sequential agents and error cases related to duplicate agent names and multiple parenting.

Test Structure

Test Cases

  1. "ok": Runs two custom agents sequentially, expects events with LLMResponse content "hello 0" and "hello 1".

  2. "ok with inner sequential": Runs a three-step sequence where the middle step is itself a sequential agent running two custom agents. The expected events reflect the unfolding of all sub-agents in strict order.

  3. Error cases:

    • Duplicate agent names anywhere in the agent tree cause creation failure.

    • Duplicate usage of the same sub-agent instance in multiple places triggers errors.

    • Multiple parents for the same sub-agent instance also causes errors.

Implementation Details


Helper Function: newCustomAgent

Purpose

Creates a simple llmagent.Agent with a mocked LLM that returns a deterministic response identifying the agent by an integer ID.

Parameters

Returns

Usage Example

agent := newCustomAgent(t, 5) // Creates a custom agent named "custom_agent_5"

Helper Function: newSequentialAgent

Purpose

Constructs a SequentialAgent instance for testing, wrapping the sub-agents provided.

Parameters

Returns

Usage Example

seqAgent := newSequentialAgent(t, []agent.Agent{agent1, agent2}, "test_seq_agent")

Mock Implementation: FakeLLM

Purpose

Provides a mocked implementation of the model.LLM interface for controlled test responses.

Properties

Methods

Usage

Injected into llmagent instances to simulate LLM responses during tests.


Important Implementation Details


Interaction with Other Parts of the System

This file tests the end-to-end integration of these components in the context of sequential workflows.


Usage Example Snippet

// Create sub-agents
agent1 := newCustomAgent(t, 0)
agent2 := newCustomAgent(t, 1)

// Create sequential agent with sub-agents
seqAgent := newSequentialAgent(t, []agent.Agent{agent1, agent2}, "test_agent")

// Create runner and session service
sessionService := session.InMemoryService()
agentRunner, _ := runner.New(runner.Config{
    AppName:        "test_app",
    Agent:          seqAgent,
    SessionService: sessionService,
})

// Create session and run agent
ctx := context.Background()
sessionService.Create(ctx, &session.CreateRequest{
    AppName:   "test_app",
    UserID:    "user1",
    SessionID: "session1",
})

for event, err := range agentRunner.Run(ctx, "user1", "session1", genai.NewContentFromText("hello", genai.RoleUser), agent.RunConfig{}) {
    if err != nil {
        t.Error(err)
    }
    fmt.Println(event.Author, event.LLMResponse.Content.Parts[0].Text)
}

Visual Diagram: Structure and Workflow of agent_test.go

flowchart TD
TestNewSequentialAgent --> SequentialAgentCreation[Create SequentialAgent]
TestNewSequentialAgent --> RunnerSetup[Setup Runner & SessionService]
TestNewSequentialAgent --> AgentRun[Run Agent Twice]
TestNewSequentialAgent --> EventValidation[Validate Output Events]
SequentialAgentCreation --> SubAgentsCreation[Create Sub-Agents]
SubAgentsCreation --> FakeLLM[FakeLLM Returns "hello <id>"]
AgentRun --> SessionService[SessionService Stores Events]
EventValidation --> goCmp[Compare Using go-cmp]
subgraph Helpers
newCustomAgent
newSequentialAgent
FakeLLM
end
newCustomAgent --> FakeLLM
newSequentialAgent --> SequentialAgentCreation

This flowchart represents the core testing workflow: test case setups create sequential agents composed of custom sub-agents backed by FakeLLM. The agent is run via a runner that interacts with an in-memory session service. The test validates the streamed events against expected outputs.


References to Related Topics


This documentation describes the testing mechanisms, helper constructs, and system interactions for validating sequential agent workflows, ensuring robust and predictable multi-agent orchestration within the framework.