looping chatbot with ephemeral history

This commit is contained in:
2026-02-12 22:29:12 -07:00
parent bc7c5009f9
commit f8a68b9d02
4 changed files with 71 additions and 13 deletions
+42 -2
View File
@@ -2,9 +2,11 @@ from unittest.mock import AsyncMock, patch
import pytest
from agent.loop import run_turn
from agent.loop import run_session, run_turn
from tests.conftest import sample_history
@pytest.mark.unit
@pytest.mark.asyncio
async def test_run_turn_basic(mock_anthropic_client):
"""test that run_turn calls the API and returns a message"""
@@ -21,9 +23,27 @@ async def test_run_turn_basic(mock_anthropic_client):
# verify call has correct parameters
call_args = mock_anthropic_client.messages.create.call_args
assert call_args.kwargs["messages"][0]["content"] == "What is 2+2?"
@pytest.mark.unit
@pytest.mark.asyncio
async def test_run_turn_with_history(mock_anthropic_client, sample_history):
"""test that run_turn includes conversation history in the API call"""
# patch the client with our mock
with patch("agent.loop.client", mock_anthropic_client):
result = await run_turn("What is 2+2?", history=sample_history)
call_args = mock_anthropic_client.messages.create.call_args
messages = call_args.kwargs["messages"]
# verify all history was included plus new message
assert len(messages) == 4
assert messages[0]["content"] == "Hello"
assert messages[-1]["content"] == "What is 2+2?"
@pytest.mark.unit
@pytest.mark.asyncio
async def test_run_turn_uses_settings(mock_anthropic_client, settings):
"""Test that run_turn uses settings correctly."""
@@ -36,3 +56,23 @@ async def test_run_turn_uses_settings(mock_anthropic_client, settings):
call_args = mock_anthropic_client.messages.create.call_args
assert call_args.kwargs["model"] == settings.model
assert call_args.kwargs["max_tokens"] == settings.max_tokens
@pytest.mark.unit
@pytest.mark.asyncio
async def test_run_session_calls_run_turn_with_user_input():
"""Test that user input is passed to run_turn"""
# Mock the input()
with patch("builtins.input", side_effect=["hello", KeyboardInterrupt]):
# Mock run_turn to avoid actually calling the API
with patch("agent.loop.run_turn", new_callable=AsyncMock) as mock_run_turn:
mock_run_turn.return_value = AsyncMock(content=[AsyncMock(text="response")])
try:
await run_session()
except KeyboardInterrupt:
pass
mock_run_turn.assert_called()
assert mock_run_turn.call_args.args[0] == "hello"