|
| 1 | +"""Tests for the SimpleAgent orchestration logic.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Iterable, List |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from simple_agent.agent import SimpleAgent, _truncate |
| 10 | +from simple_agent.backends.base import LLMBackend, Message |
| 11 | +from simple_agent.tools.base import SimpleTool, Tool |
| 12 | + |
| 13 | + |
| 14 | +class DummyBackend(LLMBackend): |
| 15 | + """Backend that returns predefined responses for each call.""" |
| 16 | + |
| 17 | + def __init__(self, responses: Iterable[str]) -> None: |
| 18 | + self._responses = list(responses) |
| 19 | + self.calls: List[List[Message]] = [] |
| 20 | + |
| 21 | + def generate(self, messages: List[Message]) -> str: |
| 22 | + if not self._responses: |
| 23 | + raise AssertionError("DummyBackend has no more responses queued.") |
| 24 | + # Capture a shallow copy so tests can inspect the conversation. |
| 25 | + self.calls.append([msg.copy() for msg in messages]) |
| 26 | + return self._responses.pop(0) |
| 27 | + |
| 28 | + |
| 29 | +class RecordingTool(SimpleTool): |
| 30 | + """Tool that records the inputs it receives.""" |
| 31 | + |
| 32 | + def __init__(self) -> None: |
| 33 | + super().__init__(name="echo", description="Echo the provided input.") |
| 34 | + self.invocations: list[str] = [] |
| 35 | + |
| 36 | + def run(self, query: str) -> str: |
| 37 | + self.invocations.append(query) |
| 38 | + return f"tool ran with: {query}" |
| 39 | + |
| 40 | + |
| 41 | +def _make_agent(responses: Iterable[str], tools: Iterable[Tool] = ()) -> tuple[SimpleAgent, DummyBackend]: |
| 42 | + backend = DummyBackend(responses) |
| 43 | + agent = SimpleAgent(backend=backend, tools=list(tools), system_prompt="Be helpful.") |
| 44 | + return agent, backend |
| 45 | + |
| 46 | + |
| 47 | +def test_agent_returns_direct_response_without_tool_use() -> None: |
| 48 | + agent, backend = _make_agent([" final answer "]) |
| 49 | + |
| 50 | + result = agent.run("Question?") |
| 51 | + |
| 52 | + assert result == "final answer" |
| 53 | + assert len(backend.calls) == 1 |
| 54 | + assert backend.calls[0][0]["role"] == "system" |
| 55 | + |
| 56 | + |
| 57 | +def test_agent_executes_requested_tool_and_returns_model_reply() -> None: |
| 58 | + tool = RecordingTool() |
| 59 | + agent, backend = _make_agent( |
| 60 | + responses=[ |
| 61 | + '{"tool":"echo","input":"calculate pi"}', |
| 62 | + "Result is 3.14", |
| 63 | + ], |
| 64 | + tools=[tool], |
| 65 | + ) |
| 66 | + |
| 67 | + result = agent.run("What is pi?", max_turns=2) |
| 68 | + |
| 69 | + assert result == "Result is 3.14" |
| 70 | + assert tool.invocations == ["calculate pi"] |
| 71 | + assert len(backend.calls) == 2 |
| 72 | + # Ensure the second backend call contains the tool output in the history. |
| 73 | + assert backend.calls[1][-1]["content"].startswith("[Tool:echo] tool ran with: calculate pi") |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize( |
| 77 | + "text,expected", |
| 78 | + [ |
| 79 | + ('{"tool":"echo","input":"test"}', {"tool": "echo", "input": "test"}), |
| 80 | + ("```json\n{\"tool\": \"echo\"}\n```", {"tool": "echo"}), |
| 81 | + ("Some text", None), |
| 82 | + ], |
| 83 | +) |
| 84 | +def test_maybe_extract_tool_request_variants(text: str, expected: dict | None) -> None: |
| 85 | + assert SimpleAgent._maybe_extract_tool_request(text) == expected # type: ignore[arg-type] |
| 86 | + |
| 87 | + |
| 88 | +def test_truncate_adds_ellipsis_when_text_is_long() -> None: |
| 89 | + text = "abc" * 200 |
| 90 | + truncated = _truncate(text, limit=10) |
| 91 | + assert truncated.endswith("…") |
| 92 | + assert len(truncated) == 11 |
0 commit comments