"""Verify `hermes -c` picks the session the user most recently used.""" from __future__ import annotations from hermes_cli.main import _resolve_last_session class _FakeDB: def __init__(self, rows): self._rows = rows self.closed = False def search_sessions(self, source=None, limit=20, **_kw): rows = [r for r in self._rows if r.get("source") == source] if source else list(self._rows) return rows[:limit] def close(self): self.closed = True def test_resolve_last_session_prefers_last_active_over_started_at(monkeypatch): # `search_sessions` returns in started_at DESC order, but the most recently # *touched* session may have been started earlier. -c should pick by # last_active so closing the active session and typing `hermes -c` resumes # that one, not an older-but-newer-started session from another window. rows = [ { "id": "new_started_old_active", "source": "cli", "started_at": 1000.0, "last_active": 100.0, }, { "id": "old_started_recently_active", "source": "cli", "started_at": 500.0, "last_active": 999.0, }, ] fake_db = _FakeDB(rows) monkeypatch.setattr("hermes_state.SessionDB", lambda: fake_db) assert _resolve_last_session("cli") == "old_started_recently_active" assert fake_db.closed def test_resolve_last_session_returns_none_when_empty(monkeypatch): monkeypatch.setattr("hermes_state.SessionDB", lambda: _FakeDB([])) assert _resolve_last_session("cli") is None def test_resolve_last_session_falls_back_to_started_at(monkeypatch): # When last_active is missing entirely (legacy row), fall back to # started_at so the helper still picks the newest session. rows = [ {"id": "older", "source": "cli", "started_at": 10.0}, {"id": "newer", "source": "cli", "started_at": 20.0}, ] monkeypatch.setattr("hermes_state.SessionDB", lambda: _FakeDB(rows)) assert _resolve_last_session("cli") == "newer"