mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
feat: enhance HumanTool with validation, timeout, and async support
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -17,19 +17,60 @@ def test_human_tool_with_langgraph_interrupt():
|
||||
"""Test HumanTool with LangGraph interrupt handling."""
|
||||
tool = HumanTool()
|
||||
|
||||
# Test successful interrupt handling
|
||||
with patch('langgraph.prebuilt.state_graphs.interrupt') as mock_interrupt:
|
||||
mock_interrupt.return_value = {"data": "test response"}
|
||||
result = tool._run("test query")
|
||||
assert result == "test response"
|
||||
mock_interrupt.assert_called_with({"query": "test query"})
|
||||
mock_interrupt.assert_called_with({"query": "test query", "timeout": None})
|
||||
|
||||
# Test interrupt propagation
|
||||
|
||||
def test_human_tool_timeout():
|
||||
"""Test HumanTool timeout handling."""
|
||||
tool = HumanTool()
|
||||
timeout = 30.0
|
||||
|
||||
with patch('langgraph.prebuilt.state_graphs.interrupt') as mock_interrupt:
|
||||
mock_interrupt.side_effect = Exception("Interrupt")
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
tool._run("test query")
|
||||
assert "Interrupt" in str(exc_info.value)
|
||||
mock_interrupt.return_value = {"data": "test response"}
|
||||
result = tool._run("test query", timeout=timeout)
|
||||
assert result == "test response"
|
||||
mock_interrupt.assert_called_with({"query": "test query", "timeout": timeout})
|
||||
|
||||
|
||||
def test_human_tool_invalid_input():
|
||||
"""Test HumanTool input validation."""
|
||||
tool = HumanTool()
|
||||
|
||||
with pytest.raises(ValueError, match="Query must be a non-empty string"):
|
||||
tool._run("")
|
||||
|
||||
with pytest.raises(ValueError, match="Query must be a non-empty string"):
|
||||
tool._run(None)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_human_tool_async():
|
||||
"""Test async HumanTool functionality."""
|
||||
tool = HumanTool()
|
||||
|
||||
with patch('langgraph.prebuilt.state_graphs.interrupt') as mock_interrupt:
|
||||
mock_interrupt.return_value = {"data": "test response"}
|
||||
result = await tool._arun("test query")
|
||||
assert result == "test response"
|
||||
mock_interrupt.assert_called_with({"query": "test query", "timeout": None})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_human_tool_async_timeout():
|
||||
"""Test async HumanTool timeout handling."""
|
||||
tool = HumanTool()
|
||||
timeout = 30.0
|
||||
|
||||
with patch('langgraph.prebuilt.state_graphs.interrupt') as mock_interrupt:
|
||||
mock_interrupt.return_value = {"data": "test response"}
|
||||
result = await tool._arun("test query", timeout=timeout)
|
||||
assert result == "test response"
|
||||
mock_interrupt.assert_called_with({"query": "test query", "timeout": timeout})
|
||||
|
||||
|
||||
def test_human_tool_without_langgraph():
|
||||
"""Test HumanTool behavior when LangGraph is not installed."""
|
||||
|
||||
Reference in New Issue
Block a user