Files
hermes-agent/tests/hermes_cli/test_telegram_managed_bot.py
ethernet 427d4936c2 test: retarget merge-fallout tests at pm-clean's seams; drop tests of retired code
The merges from main kept or added about 45 test files written against the
legacy updater and installer seams that pm-clean replaced. Each failing test
was checked against the current code:

- Deleted, because every test targets code that pm-clean removed or that is
  only reachable from dead or frozen old-updater code:
  update_orphan/handoff_backend_reap, handoff_desktop_rebuild,
  interrupted_recovery (it also launched a real completion against the live
  checkout), update_stale_virtualenv, update_venv_health,
  verify_core_dependencies, lazy_refresh_venv_repair,
  certifi_repair (already dropped in 2f20ceb6f7; the merge resurrected it),
  banner_git_state (covered by test_source_check).
- Retargeted where production reads now:
  - version identity via version_info.get_code_identity
  - update receipts via a ContextVar scope
  - pm.extras / pm.installed_package / pm.ensure_import for matrix,
    computer_use, fal and tirith
  - install hints via pm.install_hint
  - the lock pins in pm/lock.json
  - probe_root for the critical-module probe
- Fixture repairs:
  - git-committed trees for source stamps
  - activate's real `--runtime-only` argument
  - a semver install stamp for requires_hermes gates
  - the Windows gateway restart after a verified update
  - snowflake-length ids that cannot collide with the runner uid
- Collection fix: one platforms() marker per test in gateway_proc_fallback.
2026-09-23 16:55:09 -04:00

192 lines
6.8 KiB
Python

"""Tests for hermes_cli.telegram_managed_bot — QR codes, deep links, pairing."""
from __future__ import annotations
from pathlib import PureWindowsPath
from unittest.mock import MagicMock, patch
from hermes_cli.telegram_managed_bot import (
TELEGRAM_ONBOARDING_URL_ENV,
TelegramPairing,
create_pairing,
poll_for_setup_result,
print_qr_code,
)
from hermes_cli import setup_platforms
VALID_TOKEN = "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
SECOND_VALID_TOKEN = "987654321:abcdefghijklmnopqrstuvwxyzABCDEF"
class TestQRCode:
def test_print_qr_code_with_url(self, capsys):
print_qr_code("https://t.me/newbot/Bot/test_bot")
captured = capsys.readouterr()
assert "https://t.me/newbot/Bot/test_bot" in captured.out
def test_print_qr_code_tip_targets_active_interpreter(self, capsys):
# Regression for #111695: a bare `pip install` targets the wrong
# environment when Hermes runs in an isolated venv (which has no pip
# module at all). The fallback tip must route through PM instead.
from pm import install_hint
with patch.dict("sys.modules", {"qrcode": None}):
print_qr_code("https://t.me/newbot/Bot/test_bot")
captured = capsys.readouterr()
assert install_hint("messaging") in captured.out
assert "pip install" not in captured.out
class TestCreatePairing:
def test_success(self):
mock_resp = MagicMock()
mock_resp.status_code = 201
mock_resp.json.return_value = {
"pairing_id": "abcdefghijklmnop",
"poll_token": "secret-token",
"suggested_username": "hermes_abcdefghijklmnop_bot",
"deep_link": "https://t.me/newbot/HermesSetupBot/hermes_abcdefghijklmnop_bot?name=Hermes+Agent",
"qr_payload": "https://t.me/newbot/HermesSetupBot/hermes_abcdefghijklmnop_bot?name=Hermes+Agent",
"expires_at": "2026-05-18T00:00:00.000Z",
}
with patch(
"hermes_cli.telegram_managed_bot.httpx.post", return_value=mock_resp
) as post:
pairing = create_pairing("https://api.example.com", bot_name="Hermes Agent")
assert pairing == TelegramPairing(
pairing_id="abcdefghijklmnop",
poll_token="secret-token",
suggested_username="hermes_abcdefghijklmnop_bot",
deep_link="https://t.me/newbot/HermesSetupBot/hermes_abcdefghijklmnop_bot?name=Hermes+Agent",
qr_payload="https://t.me/newbot/HermesSetupBot/hermes_abcdefghijklmnop_bot?name=Hermes+Agent",
expires_at="2026-05-18T00:00:00.000Z",
)
post.assert_called_once_with(
"https://api.example.com/v1/telegram/pairings",
json={"bot_name": "Hermes Agent"},
timeout=10.0,
)
def test_failure_status(self):
mock_resp = MagicMock()
mock_resp.status_code = 500
with patch(
"hermes_cli.telegram_managed_bot.httpx.post", return_value=mock_resp
):
assert create_pairing("https://api.example.com") is None
def test_uses_env_override(self, monkeypatch):
monkeypatch.setenv(TELEGRAM_ONBOARDING_URL_ENV, "https://worker.example")
mock_resp = MagicMock()
mock_resp.status_code = 500
with patch(
"hermes_cli.telegram_managed_bot.httpx.post", return_value=mock_resp
) as post:
create_pairing()
assert post.call_args.args[0] == "https://worker.example/v1/telegram/pairings"
class TestPollForToken:
def pairing(self):
return TelegramPairing(
pairing_id="abcdefghijklmnop",
poll_token="secret-token",
suggested_username="hermes_abcdefghijklmnop_bot",
deep_link="https://t.me/newbot/HermesSetupBot/hermes_abcdefghijklmnop_bot",
qr_payload="https://t.me/newbot/HermesSetupBot/hermes_abcdefghijklmnop_bot",
)
def test_immediate_success(self):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {
"bot_username": "hermes_abcdefghijklmnop_bot",
"owner_user_id": 42,
"status": "ready",
"token": VALID_TOKEN,
}
with patch(
"hermes_cli.telegram_managed_bot.httpx.get", return_value=mock_resp
) as get:
with patch("hermes_cli.telegram_managed_bot.time.sleep"):
result = poll_for_setup_result(
"https://api.example.com", self.pairing(), timeout=5
)
assert result is not None and result.token == VALID_TOKEN
assert (
get.call_args.args[0]
== "https://api.example.com/v1/telegram/pairings/abcdefghijklmnop"
)
assert get.call_args.kwargs["headers"] == {
"Authorization": "Bearer secret-token"
}
def test_eventual_success(self):
not_ready = MagicMock()
not_ready.status_code = 200
not_ready.json.return_value = {"status": "waiting"}
ready = MagicMock()
ready.status_code = 200
ready.json.return_value = {"status": "ready", "token": SECOND_VALID_TOKEN}
call_count = 0
def fake_get(*args, **kwargs):
nonlocal call_count
call_count += 1
if call_count < 3:
return not_ready
return ready
with patch("hermes_cli.telegram_managed_bot.httpx.get", side_effect=fake_get):
with patch("hermes_cli.telegram_managed_bot.time.sleep"):
result = poll_for_setup_result(
"https://api.example.com", self.pairing(), timeout=30
)
assert result is not None and result.token == SECOND_VALID_TOKEN
class TestSetupTelegramAuto:
def test_setup_result_passes_profile_name_for_profile_home(self, monkeypatch, tmp_path):
from hermes_cli import setup
seen = {}
profile_home = tmp_path / ".hermes" / "profiles" / "oracle"
profile_home.mkdir(parents=True)
monkeypatch.setattr(setup, "get_hermes_home", lambda: profile_home)
def fake_auto_setup_telegram_bot_result(*, profile_name=None):
seen["profile_name"] = profile_name
return None
monkeypatch.setattr(
"hermes_cli.telegram_managed_bot.auto_setup_telegram_bot_result",
fake_auto_setup_telegram_bot_result,
)
assert setup_platforms._setup_telegram_auto_result() is None
assert seen["profile_name"] == "oracle"
def test_profile_name_from_home_path_handles_windows_separators(self):
from hermes_cli.setup_platforms import _profile_name_from_hermes_home
assert (
_profile_name_from_hermes_home(
PureWindowsPath(r"C:\Users\test\AppData\Local\hermes\profiles\oracle")
)
== "oracle"
)