feat: added proper settings handling

This commit is contained in:
2026-01-01 03:55:23 +01:00
parent b1507db4d0
commit 6a7f9c6521
23 changed files with 440 additions and 328 deletions
+34 -33
View File
@@ -2,6 +2,7 @@
from unittest.mock import Mock
from conftest import mock_llm
from alfred.agent.agent import Agent
from alfred.infrastructure.persistence import get_memory
@@ -9,24 +10,24 @@ from alfred.infrastructure.persistence import get_memory
class TestAgentInit:
"""Tests for Agent initialization."""
def test_init(self, memory, mock_llm):
def test_init(self, memory, mock_settings, mock_llm):
"""Should initialize agent with LLM."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm, max_tool_iterations=10)
assert agent.llm is mock_llm
assert agent.tools is not None
assert agent.prompt_builder is not None
assert agent.max_tool_iterations == 5
assert agent.max_tool_iterations == 10
def test_init_custom_iterations(self, memory, mock_llm):
def test_init_custom_iterations(self, memory, mock_settings, mock_llm):
"""Should accept custom max iterations."""
agent = Agent(llm=mock_llm, max_tool_iterations=10)
agent = Agent(settings=mock_settings, llm=mock_llm, max_tool_iterations=10)
assert agent.max_tool_iterations == 10
def test_tools_registered(self, memory, mock_llm):
def test_tools_registered(self, memory, mock_settings, mock_llm):
"""Should register all tools."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
expected_tools = [
"set_path_for_folder",
@@ -46,9 +47,9 @@ class TestAgentInit:
class TestExecuteToolCall:
"""Tests for _execute_tool_call method."""
def test_execute_known_tool(self, memory, mock_llm, real_folder):
def test_execute_known_tool(self, memory, mock_settings, mock_llm, real_folder):
"""Should execute known tool."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
memory.ltm.set_config("download_folder", str(real_folder["downloads"]))
tool_call = {
@@ -62,9 +63,9 @@ class TestExecuteToolCall:
assert result["status"] == "ok"
def test_execute_unknown_tool(self, memory, mock_llm):
def test_execute_unknown_tool(self, memory, mock_settings, mock_llm):
"""Should return error for unknown tool."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
tool_call = {
"id": "call_123",
@@ -75,9 +76,9 @@ class TestExecuteToolCall:
assert result["error"] == "unknown_tool"
assert "available_tools" in result
def test_execute_with_bad_args(self, memory, mock_llm):
def test_execute_with_bad_args(self, memory, mock_settings, mock_llm):
"""Should return error for bad arguments."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
tool_call = {
"id": "call_123",
@@ -87,9 +88,9 @@ class TestExecuteToolCall:
assert result["error"] == "bad_args"
def test_execute_tracks_errors(self, memory, mock_llm):
def test_execute_tracks_errors(self, memory, mock_settings, mock_llm):
"""Should track errors in episodic memory."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
# Use invalid arguments to trigger a TypeError
tool_call = {
@@ -104,9 +105,9 @@ class TestExecuteToolCall:
mem = get_memory()
assert len(mem.episodic.recent_errors) > 0
def test_execute_with_invalid_json(self, memory, mock_llm):
def test_execute_with_invalid_json(self, memory, mock_settings, mock_llm):
"""Should handle invalid JSON arguments."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
tool_call = {
"id": "call_123",
@@ -120,17 +121,17 @@ class TestExecuteToolCall:
class TestStep:
"""Tests for step method."""
def test_step_text_response(self, memory, mock_llm):
def test_step_text_response(self, memory, mock_settings, mock_llm):
"""Should return text response when no tool call."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
response = agent.step("Hello")
assert response == "I found what you're looking for!"
def test_step_saves_to_history(self, memory, mock_llm):
def test_step_saves_to_history(self, memory, mock_settings, mock_llm):
"""Should save conversation to STM history."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
agent.step("Hi there")
@@ -141,11 +142,11 @@ class TestStep:
assert history[0]["content"] == "Hi there"
assert history[1]["role"] == "assistant"
def test_step_with_tool_call(self, memory, mock_llm_with_tool_call, real_folder):
def test_step_with_tool_call(self, memory, mock_settings, mock_llm_with_tool_call, real_folder):
"""Should execute tool and continue."""
memory.ltm.set_config("download_folder", str(real_folder["downloads"]))
agent = Agent(llm=mock_llm_with_tool_call)
agent = Agent(settings=mock_settings, llm=mock_llm_with_tool_call)
response = agent.step("List my downloads")
@@ -157,7 +158,7 @@ class TestStep:
assert first_call_args[1]["tools"] is not None, "Tools not passed to LLM!"
assert len(first_call_args[1]["tools"]) > 0, "Tools list is empty!"
def test_step_max_iterations(self, memory, mock_llm):
def test_step_max_iterations(self, memory, mock_settings, mock_llm):
"""Should stop after max iterations."""
call_count = [0]
@@ -185,15 +186,15 @@ class TestStep:
return {"role": "assistant", "content": "I couldn't complete the task."}
mock_llm.complete = Mock(side_effect=mock_complete)
agent = Agent(llm=mock_llm, max_tool_iterations=3)
agent = Agent(settings=mock_settings, llm=mock_llm, max_tool_iterations=3)
agent.step("Do something")
assert call_count[0] == 4
def test_step_includes_history(self, memory_with_history, mock_llm):
def test_step_includes_history(self, memory_with_history, mock_settings, mock_llm):
"""Should include conversation history in prompt."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
agent.step("New message")
@@ -201,10 +202,10 @@ class TestStep:
messages_content = [m.get("content", "") for m in call_args]
assert any("Hello" in str(c) for c in messages_content)
def test_step_includes_events(self, memory, mock_llm):
def test_step_includes_events(self, memory, mock_settings, mock_llm):
"""Should include unread events in prompt."""
memory.episodic.add_background_event("download_complete", {"name": "Movie.mkv"})
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
agent.step("What's new?")
@@ -212,9 +213,9 @@ class TestStep:
messages_content = [m.get("content", "") for m in call_args]
assert any("download" in str(c).lower() for c in messages_content)
def test_step_saves_ltm(self, memory, mock_llm, temp_dir):
def test_step_saves_ltm(self, memory, mock_settings, mock_llm, temp_dir):
"""Should save LTM after step."""
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
agent.step("Hello")
@@ -225,7 +226,7 @@ class TestStep:
class TestAgentIntegration:
"""Integration tests for Agent."""
def test_multiple_tool_calls(self, memory, mock_llm, real_folder):
def test_multiple_tool_calls(self, memory, mock_settings, mock_llm, real_folder):
"""Should handle multiple tool calls in sequence."""
memory.ltm.set_config("download_folder", str(real_folder["downloads"]))
memory.ltm.set_config("movie_folder", str(real_folder["movies"]))
@@ -276,7 +277,7 @@ class TestAgentIntegration:
}
mock_llm.complete = Mock(side_effect=mock_complete)
agent = Agent(llm=mock_llm)
agent = Agent(settings=mock_settings, llm=mock_llm)
agent.step("List my downloads and movies")