|
| 1 | +""" |
| 2 | +Unit tests for Agent.as_tool() method. |
| 3 | +
|
| 4 | +Tests: |
| 5 | +1. as_tool() returns a Handoff with ContextPolicy.NONE |
| 6 | +2. as_tool() generates correct default tool name |
| 7 | +3. as_tool() uses custom description and name when provided |
| 8 | +4. as_tool() can be used in tools list |
| 9 | +""" |
| 10 | + |
| 11 | +from unittest.mock import MagicMock |
| 12 | + |
| 13 | + |
| 14 | +class TestAgentAsTool: |
| 15 | + """Tests for Agent.as_tool() method.""" |
| 16 | + |
| 17 | + def test_as_tool_returns_handoff(self): |
| 18 | + """Test that as_tool() returns a Handoff instance.""" |
| 19 | + from praisonaiagents.agent.handoff import Handoff, HandoffConfig, ContextPolicy |
| 20 | + |
| 21 | + # Create a mock agent |
| 22 | + mock_agent = MagicMock() |
| 23 | + mock_agent.name = "TestAgent" |
| 24 | + mock_agent.role = "Test Role" |
| 25 | + mock_agent.goal = "Test Goal" |
| 26 | + |
| 27 | + # Simulate as_tool behavior |
| 28 | + agent_name_snake = mock_agent.name.lower().replace(' ', '_').replace('-', '_') |
| 29 | + default_tool_name = f"invoke_{agent_name_snake}" |
| 30 | + |
| 31 | + result = Handoff( |
| 32 | + agent=mock_agent, |
| 33 | + tool_name_override=default_tool_name, |
| 34 | + tool_description_override=f"Invoke {mock_agent.name} to complete a subtask and return the result", |
| 35 | + config=HandoffConfig(context_policy=ContextPolicy.NONE), |
| 36 | + ) |
| 37 | + |
| 38 | + assert isinstance(result, Handoff) |
| 39 | + assert result.config.context_policy == ContextPolicy.NONE |
| 40 | + assert result.tool_name == "invoke_testagent" |
| 41 | + |
| 42 | + def test_as_tool_context_policy_none(self): |
| 43 | + """Test that as_tool() uses ContextPolicy.NONE (no history passed).""" |
| 44 | + from praisonaiagents.agent.handoff import Handoff, HandoffConfig, ContextPolicy |
| 45 | + |
| 46 | + mock_agent = MagicMock() |
| 47 | + mock_agent.name = "Researcher" |
| 48 | + |
| 49 | + result = Handoff( |
| 50 | + agent=mock_agent, |
| 51 | + tool_name_override="invoke_researcher", |
| 52 | + tool_description_override="Research topics", |
| 53 | + config=HandoffConfig(context_policy=ContextPolicy.NONE), |
| 54 | + ) |
| 55 | + |
| 56 | + assert result.config.context_policy == ContextPolicy.NONE |
| 57 | + |
| 58 | + def test_as_tool_default_tool_name(self): |
| 59 | + """Test that as_tool() generates correct default tool name.""" |
| 60 | + from praisonaiagents.agent.handoff import Handoff, HandoffConfig, ContextPolicy |
| 61 | + |
| 62 | + # Test various agent names |
| 63 | + test_cases = [ |
| 64 | + ("Researcher", "invoke_researcher"), |
| 65 | + ("Code Writer", "invoke_code_writer"), |
| 66 | + ("data-analyst", "invoke_data_analyst"), |
| 67 | + ("MyAgent123", "invoke_myagent123"), |
| 68 | + ] |
| 69 | + |
| 70 | + for agent_name, expected_tool_name in test_cases: |
| 71 | + mock_agent = MagicMock() |
| 72 | + mock_agent.name = agent_name |
| 73 | + |
| 74 | + agent_name_snake = agent_name.lower().replace(' ', '_').replace('-', '_') |
| 75 | + tool_name = f"invoke_{agent_name_snake}" |
| 76 | + |
| 77 | + result = Handoff( |
| 78 | + agent=mock_agent, |
| 79 | + tool_name_override=tool_name, |
| 80 | + tool_description_override="Test", |
| 81 | + config=HandoffConfig(context_policy=ContextPolicy.NONE), |
| 82 | + ) |
| 83 | + |
| 84 | + assert result.tool_name == expected_tool_name, f"Failed for {agent_name}" |
| 85 | + |
| 86 | + def test_as_tool_custom_description(self): |
| 87 | + """Test that as_tool() uses custom description when provided.""" |
| 88 | + from praisonaiagents.agent.handoff import Handoff, HandoffConfig, ContextPolicy |
| 89 | + |
| 90 | + mock_agent = MagicMock() |
| 91 | + mock_agent.name = "Coder" |
| 92 | + |
| 93 | + custom_description = "Write Python code for any task" |
| 94 | + |
| 95 | + result = Handoff( |
| 96 | + agent=mock_agent, |
| 97 | + tool_name_override="invoke_coder", |
| 98 | + tool_description_override=custom_description, |
| 99 | + config=HandoffConfig(context_policy=ContextPolicy.NONE), |
| 100 | + ) |
| 101 | + |
| 102 | + assert result.tool_description == custom_description |
| 103 | + |
| 104 | + def test_as_tool_custom_name(self): |
| 105 | + """Test that as_tool() uses custom tool name when provided.""" |
| 106 | + from praisonaiagents.agent.handoff import Handoff, HandoffConfig, ContextPolicy |
| 107 | + |
| 108 | + mock_agent = MagicMock() |
| 109 | + mock_agent.name = "Researcher" |
| 110 | + |
| 111 | + custom_name = "research_topic" |
| 112 | + |
| 113 | + result = Handoff( |
| 114 | + agent=mock_agent, |
| 115 | + tool_name_override=custom_name, |
| 116 | + tool_description_override="Research topics", |
| 117 | + config=HandoffConfig(context_policy=ContextPolicy.NONE), |
| 118 | + ) |
| 119 | + |
| 120 | + assert result.tool_name == custom_name |
| 121 | + |
| 122 | + |
| 123 | +class TestAgentAsToolIntegration: |
| 124 | + """Integration tests for Agent.as_tool() with real Agent instances.""" |
| 125 | + |
| 126 | + def test_as_tool_on_real_agent(self): |
| 127 | + """Test as_tool() on a real Agent instance (no LLM calls).""" |
| 128 | + from praisonaiagents import Agent |
| 129 | + from praisonaiagents.agent.handoff import Handoff, ContextPolicy |
| 130 | + |
| 131 | + # Create a real agent (no LLM calls needed for this test) |
| 132 | + agent = Agent( |
| 133 | + name="TestResearcher", |
| 134 | + instructions="Research topics thoroughly", |
| 135 | + ) |
| 136 | + |
| 137 | + # Call as_tool() |
| 138 | + result = agent.as_tool("Research a topic and return findings") |
| 139 | + |
| 140 | + # Verify result |
| 141 | + assert isinstance(result, Handoff) |
| 142 | + assert result.config.context_policy == ContextPolicy.NONE |
| 143 | + assert result.tool_name == "invoke_testresearcher" |
| 144 | + assert "Research a topic" in result.tool_description |
| 145 | + |
| 146 | + def test_as_tool_with_custom_params(self): |
| 147 | + """Test as_tool() with custom tool name and description.""" |
| 148 | + from praisonaiagents import Agent |
| 149 | + from praisonaiagents.agent.handoff import Handoff, ContextPolicy |
| 150 | + |
| 151 | + agent = Agent( |
| 152 | + name="Coder", |
| 153 | + instructions="Write clean Python code", |
| 154 | + ) |
| 155 | + |
| 156 | + result = agent.as_tool( |
| 157 | + description="Generate Python code for any programming task", |
| 158 | + tool_name="generate_code", |
| 159 | + ) |
| 160 | + |
| 161 | + assert isinstance(result, Handoff) |
| 162 | + assert result.tool_name == "generate_code" |
| 163 | + assert "Generate Python code" in result.tool_description |
| 164 | + |
| 165 | + def test_as_tool_in_tools_list(self): |
| 166 | + """Test that as_tool() result can be added to another agent's tools.""" |
| 167 | + from praisonaiagents import Agent |
| 168 | + from praisonaiagents.agent.handoff import Handoff |
| 169 | + |
| 170 | + # Create specialist agents |
| 171 | + researcher = Agent(name="Researcher", instructions="Research topics") |
| 172 | + coder = Agent(name="Coder", instructions="Write code") |
| 173 | + |
| 174 | + # Create parent agent with specialists as tools |
| 175 | + writer = Agent( |
| 176 | + name="Writer", |
| 177 | + instructions="Write articles using your tools", |
| 178 | + tools=[ |
| 179 | + researcher.as_tool("Research a topic"), |
| 180 | + coder.as_tool("Write Python code"), |
| 181 | + ], |
| 182 | + ) |
| 183 | + |
| 184 | + # Verify tools were added (they get converted to tool functions) |
| 185 | + # The handoffs should be processed into the tools list |
| 186 | + assert writer.name == "Writer" |
| 187 | + # Note: Handoffs are processed in _process_handoffs, so we check the agent was created |
0 commit comments