loader_test.go
Overview
This file contains unit tests for validating the behavior of agent loading functionality, specifically focusing on detecting duplicate agent names within a multi-agent loader setup. It defines a minimal mock implementation of the Agent interface, testAgent, primarily to facilitate testing of the NewMultiLoader function's duplicate detection logic. The file verifies that the loader correctly differentiates agents by name and enforces uniqueness constraints when constructing a multi-agent loader.
Contents
Definition of the
testAgentstruct implementing theAgentinterface with stub methods.The
TestDuplicateNametest function, which checks various scenarios involving agents with duplicate or unique names.Use of the
NewMultiLoaderfunction to create loaders and validate error conditions related to duplicate agent names or instances.
testAgent Struct
Purpose
testAgent is a lightweight mock implementation of the Agent interface, created solely for testing name duplication logic in agent loaders.
Structure
type testAgent struct {
name string
}
Implemented Methods
Name() string: Returns the agent's name.Description() string: Panics with "not implemented" (not required for this test).Run(InvocationContext) iter.Seq2[*session.Event, error]: Panics with "not implemented" (not required).SubAgents() []Agent: Panics with "not implemented".internal() *agent: Panics with "not implemented".
These stubs satisfy the Agent interface, allowing testAgent instances to be passed to loader construction without implementing full behavior.
Usage Example
agent := &testAgent{name: "example_agent"}
fmt.Println(agent.Name()) // Output: example_agent
Test Function: TestDuplicateName
Purpose
Tests the behavior of NewMultiLoader when provided with agents that may have duplicate names or duplicate references, ensuring correct error reporting.
Test Setup
Three
testAgentinstances are created:agent1with name"weather_time_agent"agent2with duplicate name"weather_time_agent"(different instance)agent3with unique name"unique"
Test cases cover combinations including:
Root agent only, no duplicates.
Root agent duplicated as a sub-agent (object identity).
Root agent duplicated by name with different instance.
Non-root agents with duplicate names.
Non-root agents with duplicate object references.
No duplicates between root and sub-agents.
Test Case Structure
Each test case is defined as:
{
name: "test case description",
root: Agent, // root agent instance
agents: []Agent, // slice of sub-agents
wantErr: bool, // whether an error is expected
}
Test Execution
For each test case:
NewMultiLoaderis called with the root and sub-agents.The returned error is checked against the expected error flag (
wantErr).If the error state does not match expectation, the test logs a failure.
Example Test Case
{
name: "root duplicate object",
root: agent1,
agents: []Agent{agent1},
wantErr: true,
}
This case expects an error because the root agent is duplicated exactly in the sub-agents slice.
Implementation Details
The test relies on
NewMultiLoaderto perform name uniqueness validation.Duplicate detection considers both agent instance identity and agent name strings.
The test ensures that
NewMultiLoaderenforces a strict uniqueness constraint across all agents managed by the loader.
Interaction with Other System Components
NewMultiLoaderis part of the agent loading mechanism referenced in the AI Agent Framework, specifically under agent loader management.The test ensures correctness of agent composition before agents are invoked within sessions or workflows.
The
testAgentmocks theAgentinterface defined in the core framework, which supports agent lifecycle and invocation as explained in AI Agent Framework.The test indirectly relates to agent invocation contexts and event streaming but focuses purely on loader-level validation.
Important Implementation Details
The test does not invoke actual agent logic — the
Runand other methods panic if called.The focus is on validating that the loader constructor rejects duplicate agent names or duplicates of the same agent instance.
This prevents ambiguity or conflicts during agent selection or invocation in runtime.
Mermaid Diagram: Function and Type Relationships in loader_test.go
flowchart TD
TA[testAgent Struct]
Test[TestDuplicateName Function]
NML[NewMultiLoader Function]
TA -->|implements| AgentInterface[Agent Interface]
Test -->|creates instances of| TA
Test -->|calls| NML
NML -->|validates uniqueness of| AgentNames[Agent Names Collection]
AgentInterface -->|defines| Methods["Name(), Description(), Run(), etc."]
This diagram illustrates how testAgent implements the Agent interface, TestDuplicateName creates and uses testAgent instances, and calls NewMultiLoader to validate agent uniqueness constraints by checking agent names.
References
For details on the
Agentinterface and its lifecycle, see AI Agent Framework.For loader interfaces and implementations, see related documentation on agent loading and management in the framework.
For session and event management contexts used by agents, see Agent Invocation Context and Agent Lifecycle and Callbacks.