HERMES-GIELIE/tests/test_streaming_agent_cache_lifecycle.py
2026-05-30 10:43:13 +02:00

73 lines
2.7 KiB
Python

from unittest.mock import MagicMock
def test_evicted_agent_lifecycle_commits_unregisters_and_shutdowns(monkeypatch):
import api.streaming as streaming
events = []
def fake_commit(session_id, *, agent=None, wait=False):
events.append(("commit", session_id, agent, wait))
return True
def fake_has_uncommitted_work(session_id):
events.append(("has_uncommitted", session_id))
return False
def fake_unregister(session_id):
events.append(("unregister", session_id))
monkeypatch.setattr(streaming, "_lifecycle_commit_session_memory", fake_commit)
monkeypatch.setattr(streaming, "_lifecycle_has_uncommitted_work", fake_has_uncommitted_work)
monkeypatch.setattr(streaming, "_lifecycle_unregister_agent", fake_unregister)
session_db = MagicMock()
agent = MagicMock()
agent._session_db = session_db
agent._session_messages = [{"role": "user", "content": "hello"}]
streaming._close_evicted_agent_at_session_boundary("old-session", agent)
assert ("commit", "old-session", agent, True) in events
assert ("has_uncommitted", "old-session") in events
assert ("unregister", "old-session") in events
agent.shutdown_memory_provider.assert_called_once_with(agent._session_messages)
session_db.close.assert_called_once()
def test_evicted_agent_lifecycle_shutdown_uses_empty_messages_when_missing(monkeypatch):
import api.streaming as streaming
monkeypatch.setattr(streaming, "_lifecycle_commit_session_memory", lambda *a, **kw: True)
monkeypatch.setattr(streaming, "_lifecycle_has_uncommitted_work", lambda session_id: False)
monkeypatch.setattr(streaming, "_lifecycle_unregister_agent", MagicMock())
agent = MagicMock()
agent._session_db = MagicMock()
streaming._close_evicted_agent_at_session_boundary("old-session", agent)
agent.shutdown_memory_provider.assert_called_once_with([])
agent._session_db.close.assert_called_once()
def test_evicted_agent_lifecycle_keeps_provider_alive_when_commit_still_dirty(monkeypatch):
import api.streaming as streaming
def fake_commit(session_id, *, agent=None, wait=False):
return True
def fake_has_uncommitted_work(session_id):
return True
monkeypatch.setattr(streaming, "_lifecycle_commit_session_memory", fake_commit)
monkeypatch.setattr(streaming, "_lifecycle_has_uncommitted_work", fake_has_uncommitted_work)
monkeypatch.setattr(streaming, "_lifecycle_unregister_agent", MagicMock())
agent = MagicMock()
agent._session_db = MagicMock()
streaming._close_evicted_agent_at_session_boundary("dirty-session", agent)
agent.shutdown_memory_provider.assert_not_called()
agent._session_db.close.assert_not_called()