agent_tool_test.go

Overview

This file contains unit tests validating the functionality of the agenttool package, which implements tools wrapping sub-agents to be used as callable tools within AI workflows. The tests verify the correctness of agent tool creation, declaration generation, input/output schema validations, interaction with mock language models, and configuration flags such as skipping summarization.

The primary focus is on ensuring that the agenttool correctly:

This test file plays a crucial role in guaranteeing the robustness and correctness of the Agent Tools component of the Tooling System and its interaction with the Agent Framework.


Detailed Breakdown of Tests

Test Functions

1. TestAgentTool_Declaration

// Create an agent with input schema and get the function declaration.
agent := createAgent(t, inputSchema, nil)
agentTool := agenttool.New(agent, nil)
decl := agentTool.(toolinternal.FunctionTool).Declaration()

2. TestAgentTool_DeclarationWithoutSchema

3. TestAgentTool_Run_InputValidation

4. TestAgentTool_Run_OutputValidation

5. TestAgentTool_Run_Successful

6. TestAgentTool_Run_WithoutSchema

7. TestAgentTool_Run_EmptyModelResponse

8. TestAgentTool_Run_SkipSummarization


Helper Functions

createAgent

createAgentWithModel

createToolContext


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Structure and Workflow of agent_tool_test.go

flowchart TD
A[Test Functions] -->|createAgent| B[Agent Creation Helpers]
A -->|createAgentWithModel| B
A -->|createToolContext| C[Tool Context Creation]
A -->|agenttool.New| D[Agent Tool Instance]
D --> E[FunctionTool Interface]
E --> F["Declaration()"]
E --> G["Run()"]
F --> H[Validate Input Schema]
G --> I[Execute Sub-Agent]
I --> J[Use MockModel for LLM Response]
J --> K[Validate Output Schema]
G --> L[Skip Summarization Flag]
C --> M[In-Memory Session Service]
M --> N[Session Creation]
N --> O[Invocation Context Creation]

Usage Examples from Tests

// Example: Validate agent tool declaration with input schema
func TestAgentTool_Declaration(t *testing.T) {
    inputSchema := &genai.Schema{...}
    agent := createAgent(t, inputSchema, nil)
    agentTool := agenttool.New(agent, nil)
    decl := agentTool.(toolinternal.FunctionTool).Declaration()
    // Assert decl matches expected schema
}
// Example: Run agent tool with input validation error expected
func TestAgentTool_Run_InputValidation(t *testing.T) {
    ...
    _, err := toolImpl.Run(toolCtx, invalidArgs)
    if err == nil {
        t.Fatal("expected error due to invalid input")
    }
}
// Example: Run agent tool successfully with mock model
func TestAgentTool_Run_Successful(t *testing.T) {
    testLLM := &testutil.MockModel{...}
    agent := createAgentWithModel(t, inputSchema, outputSchema, testLLM)
    agentTool := agenttool.New(agent, nil)
    result, err := agentTool.(toolinternal.FunctionTool).Run(toolCtx, map[string]any{"is_magic": true})
    // Assert no error and result matches expected output
}

References to Related Topics


This documentation provides a comprehensive understanding of the agent_tool_test.go file, covering its purpose, test cases, helper functions, and integration points within the system.