agentgraphgenerator_test.go
Overview
This file contains unit tests for the agent graph generation functionality within the services package. It primarily focuses on verifying correct behavior for visualizing agents, tools, and their relationships in graph form using the gographviz library. The tests cover creation of agent and tool nodes, their captions and shapes, cluster generation for workflow agents, edge drawing between nodes, and highlighting logic for nodes and edges.
The file includes helper types and functions to mock models, agents, and tools necessary for testing. It ensures that graph construction routines accurately represent agent hierarchies, sub-agent compositions, and tool associations, as well as correctly apply visual attributes like colors, shapes, labels, and styles based on agent types and highlight states.
Helper Types and Functions
dummyLLM
Purpose: A mock implementation of an LLM (Large Language Model) used for testing llmagent related functionality.
Fields:
name string- Identifier for the dummy LLM.
Methods:
Name() string- Returns the LLM's name.GenerateContent(ctx context.Context, req *model.LLMRequest, stream bool) iter.Seq2[*model.LLMResponse, error]
Returns a fixed LLM response with static text"Response from agentgrapgenerator test."via a generator function.
newTestAgent
Purpose: Factory to create generic agent.Agent instances of various workflow or LLM agent types for testing.
Parameters:
t *testing.T- Testing context to report fatal errors.name string- Agent name.description string- Agent description.agentType agentinternal.Type- The type of agent to create (e.g., sequential, loop, parallel, LLM).subAgents []agent.Agent- Optional sub-agents composing this agent.tools []tool.Tool- Optional tools associated with the agent.
Returns: agent.Agent - Constructed agent instance.
Behavior: Creates an agent based on the specified type using respective constructors. Fails the test if an unsupported type is specified or creation returns an error.
mockTool
Purpose: A simple mock implementation of the tool.Tool interface.
Fields:
name string- Tool name.
Methods:
Name() string- Returns the tool's name.Description() string- Returns an empty description.IsLongRunning() bool- Returns false.
Test Functions
Each test function targets a specific aspect of the graph generation code related to agents and tools visualization and graph structure.
TestNodeName
Purpose: Verifies nodeName function returns correct node names for agents, tools, and unknown types.
Test Cases:
Agent instance returns agent name.
Tool instance returns tool name.
Unknown instance returns
"Unknown instance type".
Usage: Confirms correct identification of node names for graph nodes.
TestNodeCaption
Purpose: Tests nodeCaption function for generating appropriate labels with icons or type suffixes.
Test Cases:
LLM agent: prepends robot emoji 🤖.
Sequential, loop, and parallel agents: append agent type in parentheses.
Tool: prepends wrench emoji 🔧.
Unknown: returns
"Unsupported agent or tool type".
Usage: Validates user-friendly node labels for graph visualization.
TestNodeShape
Purpose: Checks nodeShape function returns correct shapes for different node types.
Test Cases:
Agent: ellipse.
Tool: box.
Unknown: cylinder.
Usage: Ensures graph nodes have distinct shapes for agents vs tools.
TestShouldBuildAgentCluster
Purpose: Validates if certain agent types are represented as graph clusters.
Test Cases:
Sequential, loop, and parallel agents return
true(clusters).LLM agents, tools, and unknowns return false.
Usage: Determines cluster creation logic for complex workflow agents.
TestHighlighted
Purpose: Tests whether a node name is included in any highlight pair list.
Parameters:
nodeName string- Name of the node to test.highlightedPairs [][]string- List of node name pairs to highlight.
Returns: bool - Whether the node is highlighted.
Test Cases: Confirm correct detection of highlighting presence.
TestEdgeHighlighted
Purpose: Checks if an edge between two nodes is highlighted and directionally marked.
Parameters:
from string,to string- Edge endpoints.highlightedPairs [][]string- Highlighted node pairs.
Returns: *bool
Test Cases: Forward, backward, no match, and partial match scenarios.
TestDrawNode
Purpose: Validates the drawNode function that adds agent or tool nodes to the graph with correct attributes.
Parameters:
agent agent.Agentortool tool.Tool- Node instance to draw.highlightedPairs [][]string- Highlighted pairs for styling.
Expected Attributes:
Color, label, shape, font color, style (rounded or filled).
Test Cases:
Agent node normal and highlighted.
Tool node normal.
Checks: Node presence, attribute correctness, and marking visited nodes.
TestDrawClusterNode
Purpose: Tests cluster node drawing for workflow agents generating subgraph clusters.
Behavior:
Creates a cluster subgraph with label showing name and agent type.
Style set to "rounded".
Marks the cluster agent visited.
Checks: Cluster presence, label correctness, style, and visited status.
TestDrawEdge
Purpose: Verifies drawEdge adds edges between nodes with correct attributes based on highlighting.
Parameters:
from,
to- Node names.highlightedPairs - Highlighted node pairs.
Attributes Tested:
Color (light gray or green), arrowhead style (none or normal), direction (normal or back).
Test Cases: Unhighlighted, highlighted forward, and highlighted backward edges.
TestDrawCluster
Purpose: Tests complete cluster drawing process for different workflow agent types.
Scenario:
Creates a parent graph and a cluster graph.
Adds sub-agents to the cluster agent.
Calls drawCluster to render the cluster and sub-agent nodes.
Checks:
All sub-agent nodes exist in the parent graph.
For sequential agents, edges exist between sub-agents with correct arrowheads.
For parallel agents, no edges between sub-agents.
For loop agents, edges form loops among sub-agents.
Failure: Tests invalid agent types.
TestBuildGraph
Purpose: End-to-end test building a complete graph of a multi-level agent hierarchy with tools.
Setup:
Main agent with sub-agents and associated tools.
Builds graph and marks visited nodes.
Verifications:
All expected nodes (agents and tools) are present.
All nodes are marked visited.
Edges from agents to their tools exist.
Important Implementation Details
Agents are categorized by types defined in agentinternal.Type (sequential, loop, parallel, LLM, custom).
Clusters are built only for workflow agents that contain sub-agents (sequential, parallel, loop) but not for LLM or custom agents.
Node appearance (label, shape, color) depends on whether the node is an agent or tool, and on agent type.
Highlighting is supported on both nodes and edges to visually emphasize relationships.
The gographviz library is used extensively for in-memory graph construction and attribute assignment.
The use of maps like
visitedNodesensures nodes are not duplicated during graph traversal.newTestAgentabstracts agent creation and simplifies generation of diverse agent types for testing.
Interaction with Other Parts of the System
Relies on agent types and constructors from packages such as sequentialagent, loopagent, parallelagent, and llmagent (refer to Agent Workflow Management and LLM Integration and Agents).
Uses the tool interface implementations for tools associated with agents (Tooling System).
Invokes LLM requests via dummyLLM for mock responses, simulating LLM Integration and Agents.
Graph drawing utilities work on gographviz.Graph objects representing the agent and tool relationship graphs.
Highlights and node shapes are part of the user interface visualization logic, which may be used in REST or Web UI components (REST API and Web Launchers, Web UI Launcher).
Visual Diagram
flowchart TD
subgraph AgentGraphGeneratorTest
direction TB
dummyLLM["dummyLLM"]
newTestAgent["newTestAgent()"]
mockTool["mockTool"]
TestNodeName["TestNodeName()"]
TestNodeCaption["TestNodeCaption()"]
TestNodeShape["TestNodeShape()"]
TestShouldBuildAgentCluster["TestShouldBuildAgentCluster()"]
TestHighlighted["TestHighlighted()"]
TestEdgeHighlighted["TestEdgeHighlighted()"]
TestDrawNode["TestDrawNode()"]
TestDrawClusterNode["TestDrawClusterNode()"]
TestDrawEdge["TestDrawEdge()"]
TestDrawCluster["TestDrawCluster()"]
TestBuildGraph["TestBuildGraph()"]
dummyLLM --> newTestAgent
mockTool --> TestNodeName
newTestAgent --> TestNodeName
newTestAgent --> TestNodeCaption
newTestAgent --> TestNodeShape
newTestAgent --> TestShouldBuildAgentCluster
mockTool --> TestShouldBuildAgentCluster
TestNodeName --> TestDrawNode
TestNodeCaption --> TestDrawNode
TestNodeShape --> TestDrawNode
TestShouldBuildAgentCluster --> TestDrawClusterNode
TestHighlighted --> TestDrawNode
TestEdgeHighlighted --> TestDrawEdge
TestDrawNode --> TestDrawClusterNode
TestDrawNode --> TestDrawEdge
TestDrawClusterNode --> TestDrawCluster
TestDrawEdge --> TestDrawCluster
TestDrawCluster --> TestBuildGraph
end
This flowchart shows the dependencies and interactions among helper types and test functions in this file, illustrating the progression from basic utilities to comprehensive graph building tests.