Skip to content

Commit 54ec5f0

Browse files
test: cover real Handoff object branch in visualization (#3100)
1 parent 3854c12 commit 54ec5f0

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

tests/test_visualization.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import graphviz # type: ignore
44
import pytest
55

6-
from agents import Agent
6+
from agents import Agent, handoff
77
from agents.extensions.visualization import (
88
draw_graph,
99
get_all_edges,
@@ -210,3 +210,35 @@ def test_draw_graph_with_real_agent_with_handoffs():
210210
assert '"ParentAgent" -> "__end__"' not in graph.source
211211
# Child has no handoffs, so should connect to __end__
212212
assert '"ChildAgent" -> "__end__"' in graph.source
213+
214+
215+
def test_draw_graph_with_real_handoff_object():
216+
"""Test draw_graph with a real Handoff object (not just Agent) in handoffs.
217+
218+
Exercises the ``isinstance(handoff, Handoff)`` branches in get_all_nodes /
219+
get_all_edges (rather than the ``isinstance(handoff, Agent)`` branches),
220+
using the public ``handoff()`` factory rather than ``Mock(spec=Handoff)``.
221+
"""
222+
child_agent = Agent(name="ChildAgent", instructions="Child instructions")
223+
real_handoff = handoff(child_agent)
224+
assert isinstance(real_handoff, Handoff)
225+
226+
parent_agent = Agent(
227+
name="ParentAgent",
228+
instructions="Parent instructions",
229+
handoffs=[real_handoff],
230+
)
231+
232+
graph = draw_graph(parent_agent)
233+
234+
assert isinstance(graph, graphviz.Source)
235+
assert '"ParentAgent"' in graph.source
236+
# Node uses agent_name from the Handoff object
237+
assert (
238+
'"ChildAgent" [label="ChildAgent", shape=box, style=filled, style=rounded, '
239+
"fillcolor=lightyellow, width=1.5, height=0.8];" in graph.source
240+
)
241+
# Edge points from parent to handoff agent_name
242+
assert '"ParentAgent" -> "ChildAgent";' in graph.source
243+
# Parent has handoffs, so should NOT connect directly to __end__
244+
assert '"ParentAgent" -> "__end__"' not in graph.source

0 commit comments

Comments
 (0)