agent_test.go

Overview

This file contains unit tests for validating the behavior and lifecycle of custom AI agents created using the core agent framework. These tests specifically focus on:

The file utilizes a customAgent implementation as a test double to simulate agent behavior, counting calls and optionally ending invocations early. It integrates with session event objects and uses the cmp package to compare expected and actual event outputs.

This testing file validates core behaviors defined in the broader Agent Lifecycle and Callbacks and AI Agent Framework topics, ensuring that the callback mechanism and invocation flow operate correctly.


Detailed Explanations of Components

TestAgentCallbacks

This test verifies the lifecycle behavior of an agent with various configurations of before and after callbacks.

beforeCallback := func(ctx CallbackContext) (*genai.Content, error) {
    return genai.NewContentFromText("early response", genai.RoleModel), nil
}

agent, err := New(Config{
    Name: "test_agent",
    BeforeAgentCallbacks: []BeforeAgentCallback{beforeCallback},
    Run: custom.Run,
})

for event, err := range agent.Run(ctx) {
    // Process events here
}

TestEndInvocation_EndsBeforeMainCall

This test ensures that when the invocation context signals EndInvocation prior to the main agent run (even if before callbacks return nil), the main LLM call does not occur.

This tests the early termination mechanism described in Agent Lifecycle and Callbacks.


TestEndInvocation_EndsAfterMainCall

This test case verifies that when the EndInvocation flag is set during the main run (via the custom agent), subsequent after-agent callbacks are skipped.


customAgent Struct and Method


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Diagram: Agent Invocation Lifecycle in Tests

flowchart TD
Start["Test Start"]
CreateAgent["Create Agent with Callbacks"]
RunAgent["Run Agent.Run(ctx)"]
BeforeCB["Execute BeforeAgentCallbacks"]
CheckBefore["Callback returned content?"]
SkipRun["Skip Main Run, Yield Callback Event"]
MainRun["Execute Main Agent Run"]
AfterCB["Execute AfterAgentCallbacks"]
CheckAfter["Callback returned content?"]
YieldAfter["Yield After Callback Event"]
CollectEvents["Collect Events"]
VerifyCalls["Verify LLM calls count"]
VerifyEvents["Compare Produced Events"]
End["Test End"]
Start --> CreateAgent --> RunAgent --> BeforeCB --> CheckBefore
CheckBefore -- Yes --> SkipRun --> CollectEvents
CheckBefore -- No --> MainRun --> AfterCB --> CheckAfter
CheckAfter -- Yes --> YieldAfter --> CollectEvents
CheckAfter -- No --> CollectEvents
CollectEvents --> VerifyCalls --> VerifyEvents --> End

Summary of Functions and Methods

Function/Method

Description

Parameters

Returns

TestAgentCallbacks(t *testing.T)

Tests agent lifecycle with before and after callbacks, validating event emission and LLM calls.

t *testing.T

None (test)

TestEndInvocation_EndsBeforeMainCall(t *testing.T)

Tests that invocation ends early before main run if flagged.

t *testing.T

None (test)

TestEndInvocation_EndsAfterMainCall(t *testing.T)

Tests that invocation ends early after main run, skipping after callbacks.

t *testing.T

None (test)

(a *customAgent) Run(ctx InvocationContext) iter.Seq2[*session.Event, error]

Simulates agent run, increments call count, optionally ends invocation early, yields a fixed event.

ctx InvocationContext

Iterator function yielding events


Usage Context

This test file is intended for developers working on the AI Agent Framework, particularly those extending or maintaining lifecycle callback functionality in agents. It demonstrates how to:

This file directly supports the robustness and correctness of agent invocation lifecycle management as described in Agent Lifecycle and Callbacks.


End of Documentation for agent_test.go