Formatting
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""Edge case tests for the Agent."""
|
||||
import pytest
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from agent.agent import Agent
|
||||
from infrastructure.persistence import get_memory
|
||||
|
||||
@@ -15,19 +17,14 @@ class TestExecuteToolCallEdgeCases:
|
||||
|
||||
# Mock a tool that returns None
|
||||
from agent.registry import Tool
|
||||
|
||||
agent.tools["test_tool"] = Tool(
|
||||
name="test_tool",
|
||||
description="Test",
|
||||
func=lambda: None,
|
||||
parameters={}
|
||||
name="test_tool", description="Test", func=lambda: None, parameters={}
|
||||
)
|
||||
|
||||
tool_call = {
|
||||
"id": "call_123",
|
||||
"function": {
|
||||
"name": "test_tool",
|
||||
"arguments": '{}'
|
||||
}
|
||||
"function": {"name": "test_tool", "arguments": "{}"},
|
||||
}
|
||||
result = agent._execute_tool_call(tool_call)
|
||||
|
||||
@@ -38,22 +35,17 @@ class TestExecuteToolCallEdgeCases:
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
from agent.registry import Tool
|
||||
|
||||
def raise_interrupt():
|
||||
raise KeyboardInterrupt()
|
||||
|
||||
|
||||
agent.tools["test_tool"] = Tool(
|
||||
name="test_tool",
|
||||
description="Test",
|
||||
func=raise_interrupt,
|
||||
parameters={}
|
||||
name="test_tool", description="Test", func=raise_interrupt, parameters={}
|
||||
)
|
||||
|
||||
tool_call = {
|
||||
"id": "call_123",
|
||||
"function": {
|
||||
"name": "test_tool",
|
||||
"arguments": '{}'
|
||||
}
|
||||
"function": {"name": "test_tool", "arguments": "{}"},
|
||||
}
|
||||
|
||||
with pytest.raises(KeyboardInterrupt):
|
||||
@@ -68,8 +60,8 @@ class TestExecuteToolCallEdgeCases:
|
||||
"id": "call_123",
|
||||
"function": {
|
||||
"name": "list_folder",
|
||||
"arguments": '{"folder_type": "download", "extra_arg": "ignored"}'
|
||||
}
|
||||
"arguments": '{"folder_type": "download", "extra_arg": "ignored"}',
|
||||
},
|
||||
}
|
||||
|
||||
result = agent._execute_tool_call(tool_call)
|
||||
@@ -84,8 +76,8 @@ class TestExecuteToolCallEdgeCases:
|
||||
"id": "call_123",
|
||||
"function": {
|
||||
"name": "get_torrent_by_index",
|
||||
"arguments": '{"index": "not an int"}'
|
||||
}
|
||||
"arguments": '{"index": "not an int"}',
|
||||
},
|
||||
}
|
||||
|
||||
result = agent._execute_tool_call(tool_call)
|
||||
@@ -115,12 +107,10 @@ class TestStepEdgeCases:
|
||||
|
||||
def test_step_with_unicode_input(self, memory, mock_llm):
|
||||
"""Should handle unicode input."""
|
||||
|
||||
def mock_complete(messages, tools=None):
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": "日本語の応答"
|
||||
}
|
||||
|
||||
return {"role": "assistant", "content": "日本語の応答"}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
@@ -130,12 +120,10 @@ class TestStepEdgeCases:
|
||||
|
||||
def test_step_llm_returns_empty(self, memory, mock_llm):
|
||||
"""Should handle LLM returning empty string."""
|
||||
|
||||
def mock_complete(messages, tools=None):
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": ""
|
||||
}
|
||||
|
||||
return {"role": "assistant", "content": ""}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
@@ -161,18 +149,17 @@ class TestStepEdgeCases:
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": f"call_{call_count[0]}",
|
||||
"function": {
|
||||
"name": "list_folder",
|
||||
"arguments": '{"folder_type": "download"}'
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": f"call_{call_count[0]}",
|
||||
"function": {
|
||||
"name": "list_folder",
|
||||
"arguments": '{"folder_type": "download"}',
|
||||
},
|
||||
}
|
||||
}]
|
||||
],
|
||||
}
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": "Done looping"
|
||||
}
|
||||
return {"role": "assistant", "content": "Done looping"}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm, max_tool_iterations=3)
|
||||
@@ -212,11 +199,13 @@ class TestStepEdgeCases:
|
||||
|
||||
def test_step_with_active_downloads(self, memory, mock_llm):
|
||||
"""Should include active downloads in context."""
|
||||
memory.episodic.add_active_download({
|
||||
"task_id": "123",
|
||||
"name": "Movie.mkv",
|
||||
"progress": 50,
|
||||
})
|
||||
memory.episodic.add_active_download(
|
||||
{
|
||||
"task_id": "123",
|
||||
"name": "Movie.mkv",
|
||||
"progress": 50,
|
||||
}
|
||||
)
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
response = agent.step("Hello")
|
||||
@@ -257,29 +246,28 @@ class TestAgentConcurrencyEdgeCases:
|
||||
memory.ltm.set_config("download_folder", str(real_folder["downloads"]))
|
||||
|
||||
call_count = [0]
|
||||
|
||||
|
||||
def mock_complete(messages, tools=None):
|
||||
call_count[0] += 1
|
||||
if call_count[0] == 1:
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "set_path_for_folder",
|
||||
"arguments": f'{{"folder_name": "movie", "path_value": "{str(real_folder["movies"])}"}}'
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "set_path_for_folder",
|
||||
"arguments": f'{{"folder_name": "movie", "path_value": "{str(real_folder["movies"])}"}}',
|
||||
},
|
||||
}
|
||||
}]
|
||||
],
|
||||
}
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": "Path set successfully."
|
||||
}
|
||||
return {"role": "assistant", "content": "Path set successfully."}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
|
||||
response = agent.step("Set movie folder")
|
||||
|
||||
mem = get_memory()
|
||||
@@ -292,29 +280,28 @@ class TestAgentErrorRecovery:
|
||||
def test_recovers_from_tool_error(self, memory, mock_llm):
|
||||
"""Should recover from tool error and continue."""
|
||||
call_count = [0]
|
||||
|
||||
|
||||
def mock_complete(messages, tools=None):
|
||||
call_count[0] += 1
|
||||
if call_count[0] == 1:
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "list_folder",
|
||||
"arguments": '{"folder_type": "download"}'
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "list_folder",
|
||||
"arguments": '{"folder_type": "download"}',
|
||||
},
|
||||
}
|
||||
}]
|
||||
],
|
||||
}
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": "The folder is not configured."
|
||||
}
|
||||
return {"role": "assistant", "content": "The folder is not configured."}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
|
||||
response = agent.step("List downloads")
|
||||
|
||||
assert "not configured" in response.lower() or len(response) > 0
|
||||
@@ -322,29 +309,28 @@ class TestAgentErrorRecovery:
|
||||
def test_error_tracked_in_memory(self, memory, mock_llm):
|
||||
"""Should track errors in episodic memory."""
|
||||
call_count = [0]
|
||||
|
||||
|
||||
def mock_complete(messages, tools=None):
|
||||
call_count[0] += 1
|
||||
if call_count[0] == 1:
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "set_path_for_folder",
|
||||
"arguments": '{}' # Missing required args
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "set_path_for_folder",
|
||||
"arguments": "{}", # Missing required args
|
||||
},
|
||||
}
|
||||
}]
|
||||
],
|
||||
}
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": "Error occurred."
|
||||
}
|
||||
return {"role": "assistant", "content": "Error occurred."}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm)
|
||||
|
||||
|
||||
agent.step("Set folder")
|
||||
|
||||
mem = get_memory()
|
||||
@@ -360,18 +346,17 @@ class TestAgentErrorRecovery:
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": f"call_{call_count[0]}",
|
||||
"function": {
|
||||
"name": "set_path_for_folder",
|
||||
"arguments": '{}' # Missing required args - will error
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": f"call_{call_count[0]}",
|
||||
"function": {
|
||||
"name": "set_path_for_folder",
|
||||
"arguments": "{}", # Missing required args - will error
|
||||
},
|
||||
}
|
||||
}]
|
||||
],
|
||||
}
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": "All attempts failed."
|
||||
}
|
||||
return {"role": "assistant", "content": "All attempts failed."}
|
||||
|
||||
mock_llm.complete = Mock(side_effect=mock_complete)
|
||||
agent = Agent(llm=mock_llm, max_tool_iterations=3)
|
||||
|
||||
Reference in New Issue
Block a user