|
3 | 3 | import graphviz # type: ignore |
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from agents import Agent |
| 6 | +from agents import Agent, handoff |
7 | 7 | from agents.extensions.visualization import ( |
8 | 8 | draw_graph, |
9 | 9 | get_all_edges, |
@@ -210,3 +210,35 @@ def test_draw_graph_with_real_agent_with_handoffs(): |
210 | 210 | assert '"ParentAgent" -> "__end__"' not in graph.source |
211 | 211 | # Child has no handoffs, so should connect to __end__ |
212 | 212 | 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