import pytest from traderai.agent import OllamaAgent from traderai.memory import MemoryStore class EmptyTools: schemas = [] @property def pending_actions(self): return {} @pytest.mark.asyncio async def test_chat_events_warns_when_ollama_offline(): agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools()) events = [] async for event in agent.chat_events("hello"): events.append(event) assert events[0]["type"] == "warning" assert "Ollama is offline" in events[0]["message"] assert events[-1]["type"] == "done" def test_runtime_context_uses_previous_interaction_not_current_message(tmp_path): memory = MemoryStore(str(tmp_path / "memory.sqlite3")) memory.add_conversation("assistant", "older answer") previous = memory.last_interaction() assert previous is not None memory.add_conversation("user", "current question") current = memory.last_interaction() agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools(), memory=memory) context = agent._runtime_context("current question", previous_interaction=previous) assert f"Previous interaction before this message: {previous['created_at']}" in context assert f"Previous interaction before this message: {current['created_at']}" not in context assert "Current local date/time:" in context def test_runtime_context_includes_uex_user_identity(tmp_path): memory = MemoryStore(str(tmp_path / "memory.sqlite3")) memory.set_profile( "uex_user", { "username": "pilot_hudson", "name": "Hudson", "email": "hudson@example.test", "timezone": "America/New_York", "specializations": "trading,hauling", }, ) agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools(), memory=memory) context = agent._runtime_context("") assert "You are speaking with UEX user pilot_hudson (Hudson)." in context assert "timezone: America/New_York" in context assert "specializations: trading,hauling" in context assert "hudson@example.test" not in context