* refactor: re-architect tests to mirror the codebase
* Update tests.yml
* fix: add missing tool_error imports after registry refactor
* fix(tests): replace patch.dict with monkeypatch to prevent env var leaks under xdist
patch.dict(os.environ) can leak TERMINAL_ENV across xdist workers,
causing test_code_execution tests to hit the Modal remote path.
* fix(tests): fix update_check and telegram xdist failures
- test_update_check: replace patch("hermes_cli.banner.os.getenv") with
monkeypatch.setenv("HERMES_HOME") — banner.py no longer imports os
directly, it uses get_hermes_home() from hermes_constants.
- test_telegram_conflict/approval_buttons: provide real exception classes
for telegram.error mock (NetworkError, TimedOut, BadRequest) so the
except clause in connect() doesn't fail with "catching classes that do
not inherit from BaseException" when xdist pollutes sys.modules.
* fix(tests): accept unavailable_models kwarg in _prompt_model_selection mock
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from run_agent import AIAgent
|
|
|
|
|
|
def _mock_response(*, usage: dict, content: str = "done"):
|
|
msg = SimpleNamespace(content=content, tool_calls=None)
|
|
choice = SimpleNamespace(message=msg, finish_reason="stop")
|
|
return SimpleNamespace(
|
|
choices=[choice],
|
|
model="test/model",
|
|
usage=SimpleNamespace(**usage),
|
|
)
|
|
|
|
|
|
def _make_agent(session_db, *, platform: str):
|
|
with (
|
|
patch("run_agent.get_tool_definitions", return_value=[]),
|
|
patch("run_agent.check_toolset_requirements", return_value={}),
|
|
patch("run_agent.OpenAI"),
|
|
):
|
|
agent = AIAgent(
|
|
api_key="test-key",
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
session_db=session_db,
|
|
session_id=f"{platform}-session",
|
|
platform=platform,
|
|
)
|
|
agent.client = MagicMock()
|
|
agent.client.chat.completions.create.return_value = _mock_response(
|
|
usage={
|
|
"prompt_tokens": 11,
|
|
"completion_tokens": 7,
|
|
"total_tokens": 18,
|
|
}
|
|
)
|
|
return agent
|
|
|
|
|
|
def test_run_conversation_persists_tokens_for_telegram_sessions():
|
|
session_db = MagicMock()
|
|
agent = _make_agent(session_db, platform="telegram")
|
|
|
|
result = agent.run_conversation("hello")
|
|
|
|
assert result["final_response"] == "done"
|
|
session_db.update_token_counts.assert_called_once()
|
|
assert session_db.update_token_counts.call_args.args[0] == "telegram-session"
|
|
|
|
|
|
def test_run_conversation_persists_tokens_for_cron_sessions():
|
|
session_db = MagicMock()
|
|
agent = _make_agent(session_db, platform="cron")
|
|
|
|
result = agent.run_conversation("hello")
|
|
|
|
assert result["final_response"] == "done"
|
|
session_db.update_token_counts.assert_called_once()
|
|
assert session_db.update_token_counts.call_args.args[0] == "cron-session"
|