|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import graphviz # type: ignore |
| 4 | +import pytest |
| 5 | + |
| 6 | +from agents import Agent |
| 7 | +from agents.extensions.visualization import ( |
| 8 | + draw_graph, |
| 9 | + get_all_edges, |
| 10 | + get_all_nodes, |
| 11 | + get_main_graph, |
| 12 | +) |
| 13 | +from agents.handoffs import Handoff |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_agent(): |
| 18 | + tool1 = Mock() |
| 19 | + tool1.name = "Tool1" |
| 20 | + tool2 = Mock() |
| 21 | + tool2.name = "Tool2" |
| 22 | + |
| 23 | + handoff1 = Mock(spec=Handoff) |
| 24 | + handoff1.agent_name = "Handoff1" |
| 25 | + |
| 26 | + agent = Mock(spec=Agent) |
| 27 | + agent.name = "Agent1" |
| 28 | + agent.tools = [tool1, tool2] |
| 29 | + agent.handoffs = [handoff1] |
| 30 | + |
| 31 | + return agent |
| 32 | + |
| 33 | + |
| 34 | +def test_get_main_graph(mock_agent): |
| 35 | + result = get_main_graph(mock_agent) |
| 36 | + print(result) |
| 37 | + assert "digraph G" in result |
| 38 | + assert "graph [splines=true];" in result |
| 39 | + assert 'node [fontname="Arial"];' in result |
| 40 | + assert "edge [penwidth=1.5];" in result |
| 41 | + assert ( |
| 42 | + '"__start__" [label="__start__", shape=ellipse, style=filled, ' |
| 43 | + "fillcolor=lightblue, width=0.5, height=0.3];" in result |
| 44 | + ) |
| 45 | + assert ( |
| 46 | + '"__end__" [label="__end__", shape=ellipse, style=filled, ' |
| 47 | + "fillcolor=lightblue, width=0.5, height=0.3];" in result |
| 48 | + ) |
| 49 | + assert ( |
| 50 | + '"Agent1" [label="Agent1", shape=box, style=filled, ' |
| 51 | + "fillcolor=lightyellow, width=1.5, height=0.8];" in result |
| 52 | + ) |
| 53 | + assert ( |
| 54 | + '"Tool1" [label="Tool1", shape=ellipse, style=filled, ' |
| 55 | + "fillcolor=lightgreen, width=0.5, height=0.3];" in result |
| 56 | + ) |
| 57 | + assert ( |
| 58 | + '"Tool2" [label="Tool2", shape=ellipse, style=filled, ' |
| 59 | + "fillcolor=lightgreen, width=0.5, height=0.3];" in result |
| 60 | + ) |
| 61 | + assert ( |
| 62 | + '"Handoff1" [label="Handoff1", shape=box, style=filled, style=rounded, ' |
| 63 | + "fillcolor=lightyellow, width=1.5, height=0.8];" in result |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def test_get_all_nodes(mock_agent): |
| 68 | + result = get_all_nodes(mock_agent) |
| 69 | + assert ( |
| 70 | + '"__start__" [label="__start__", shape=ellipse, style=filled, ' |
| 71 | + "fillcolor=lightblue, width=0.5, height=0.3];" in result |
| 72 | + ) |
| 73 | + assert ( |
| 74 | + '"__end__" [label="__end__", shape=ellipse, style=filled, ' |
| 75 | + "fillcolor=lightblue, width=0.5, height=0.3];" in result |
| 76 | + ) |
| 77 | + assert ( |
| 78 | + '"Agent1" [label="Agent1", shape=box, style=filled, ' |
| 79 | + "fillcolor=lightyellow, width=1.5, height=0.8];" in result |
| 80 | + ) |
| 81 | + assert ( |
| 82 | + '"Tool1" [label="Tool1", shape=ellipse, style=filled, ' |
| 83 | + "fillcolor=lightgreen, width=0.5, height=0.3];" in result |
| 84 | + ) |
| 85 | + assert ( |
| 86 | + '"Tool2" [label="Tool2", shape=ellipse, style=filled, ' |
| 87 | + "fillcolor=lightgreen, width=0.5, height=0.3];" in result |
| 88 | + ) |
| 89 | + assert ( |
| 90 | + '"Handoff1" [label="Handoff1", shape=box, style=filled, style=rounded, ' |
| 91 | + "fillcolor=lightyellow, width=1.5, height=0.8];" in result |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_get_all_edges(mock_agent): |
| 96 | + result = get_all_edges(mock_agent) |
| 97 | + assert '"__start__" -> "Agent1";' in result |
| 98 | + assert '"Agent1" -> "__end__";' |
| 99 | + assert '"Agent1" -> "Tool1" [style=dotted, penwidth=1.5];' in result |
| 100 | + assert '"Tool1" -> "Agent1" [style=dotted, penwidth=1.5];' in result |
| 101 | + assert '"Agent1" -> "Tool2" [style=dotted, penwidth=1.5];' in result |
| 102 | + assert '"Tool2" -> "Agent1" [style=dotted, penwidth=1.5];' in result |
| 103 | + assert '"Agent1" -> "Handoff1";' in result |
| 104 | + |
| 105 | + |
| 106 | +def test_draw_graph(mock_agent): |
| 107 | + graph = draw_graph(mock_agent) |
| 108 | + assert isinstance(graph, graphviz.Source) |
| 109 | + assert "digraph G" in graph.source |
| 110 | + assert "graph [splines=true];" in graph.source |
| 111 | + assert 'node [fontname="Arial"];' in graph.source |
| 112 | + assert "edge [penwidth=1.5];" in graph.source |
| 113 | + assert ( |
| 114 | + '"__start__" [label="__start__", shape=ellipse, style=filled, ' |
| 115 | + "fillcolor=lightblue, width=0.5, height=0.3];" in graph.source |
| 116 | + ) |
| 117 | + assert ( |
| 118 | + '"__end__" [label="__end__", shape=ellipse, style=filled, ' |
| 119 | + "fillcolor=lightblue, width=0.5, height=0.3];" in graph.source |
| 120 | + ) |
| 121 | + assert ( |
| 122 | + '"Agent1" [label="Agent1", shape=box, style=filled, ' |
| 123 | + "fillcolor=lightyellow, width=1.5, height=0.8];" in graph.source |
| 124 | + ) |
| 125 | + assert ( |
| 126 | + '"Tool1" [label="Tool1", shape=ellipse, style=filled, ' |
| 127 | + "fillcolor=lightgreen, width=0.5, height=0.3];" in graph.source |
| 128 | + ) |
| 129 | + assert ( |
| 130 | + '"Tool2" [label="Tool2", shape=ellipse, style=filled, ' |
| 131 | + "fillcolor=lightgreen, width=0.5, height=0.3];" in graph.source |
| 132 | + ) |
| 133 | + assert ( |
| 134 | + '"Handoff1" [label="Handoff1", shape=box, style=filled, style=rounded, ' |
| 135 | + "fillcolor=lightyellow, width=1.5, height=0.8];" in graph.source |
| 136 | + ) |
0 commit comments