hermes-agent/tests/hermes_cli/test_update_check.py
Teknium b2f8e231dd fix(test): test get_update_result timeout behavior, not result-value identity
My previous attempt (patching check_for_updates) still lost the race:
the background update-check thread captures check_for_updates via
global lookup at call time, but on CI the thread was already past that
point (mid-git-fetch) by the time the test's patch took effect.  The
real fetch returned 4954 commits-behind and wrote that to
banner._update_result before the test's assertion ran.

Fix: test what we actually care about — that get_update_result respects
its timeout parameter — and drop the asserting-on-result-value that
races with legitimate background activity.  The get_update_result
function's job is to return after `timeout` seconds if the event isn't
set.  The value of `_update_result` is incidental to that test.

Validation: tests/hermes_cli/test_update_check.py now 9/9 pass under
CI-parity env, and the test no longer has a correctness dependency on
module-level state that other threads can write.
2026-04-19 19:18:19 -07:00

180 lines
6.7 KiB
Python

"""Tests for the update check mechanism in hermes_cli.banner."""
import json
import os
import threading
import time
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
def test_version_string_no_v_prefix():
"""__version__ should be bare semver without a 'v' prefix."""
from hermes_cli import __version__
assert not __version__.startswith("v"), f"__version__ should not start with 'v', got {__version__!r}"
def test_check_for_updates_uses_cache(tmp_path, monkeypatch):
"""When cache is fresh, check_for_updates should return cached value without calling git."""
from hermes_cli.banner import check_for_updates
# Create a fake git repo and fresh cache
repo_dir = tmp_path / "hermes-agent"
repo_dir.mkdir()
(repo_dir / ".git").mkdir()
cache_file = tmp_path / ".update_check"
cache_file.write_text(json.dumps({"ts": time.time(), "behind": 3}))
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
with patch("hermes_cli.banner.subprocess.run") as mock_run:
result = check_for_updates()
assert result == 3
mock_run.assert_not_called()
def test_check_for_updates_expired_cache(tmp_path, monkeypatch):
"""When cache is expired, check_for_updates should call git fetch."""
from hermes_cli.banner import check_for_updates
repo_dir = tmp_path / "hermes-agent"
repo_dir.mkdir()
(repo_dir / ".git").mkdir()
# Write an expired cache (timestamp far in the past)
cache_file = tmp_path / ".update_check"
cache_file.write_text(json.dumps({"ts": 0, "behind": 1}))
mock_result = MagicMock(returncode=0, stdout="5\n")
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
with patch("hermes_cli.banner.subprocess.run", return_value=mock_result) as mock_run:
result = check_for_updates()
assert result == 5
assert mock_run.call_count == 2 # git fetch + git rev-list
def test_check_for_updates_no_git_dir(tmp_path, monkeypatch):
"""Returns None when .git directory doesn't exist anywhere."""
import hermes_cli.banner as banner
# Create a fake banner.py so the fallback path also has no .git
fake_banner = tmp_path / "hermes_cli" / "banner.py"
fake_banner.parent.mkdir(parents=True, exist_ok=True)
fake_banner.touch()
monkeypatch.setattr(banner, "__file__", str(fake_banner))
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
with patch("hermes_cli.banner.subprocess.run") as mock_run:
result = banner.check_for_updates()
assert result is None
mock_run.assert_not_called()
def test_check_for_updates_fallback_to_project_root(tmp_path, monkeypatch):
"""Dev install: falls back to Path(__file__).parent.parent when HERMES_HOME has no git repo."""
import hermes_cli.banner as banner
project_root = Path(banner.__file__).parent.parent.resolve()
if not (project_root / ".git").exists():
pytest.skip("Not running from a git checkout")
# Point HERMES_HOME at a temp dir with no hermes-agent/.git
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
with patch("hermes_cli.banner.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="0\n")
result = banner.check_for_updates()
# Should have fallen back to project root and run git commands
assert mock_run.call_count >= 1
def test_prefetch_non_blocking():
"""prefetch_update_check() should return immediately without blocking."""
import hermes_cli.banner as banner
# Reset module state
banner._update_result = None
banner._update_check_done = threading.Event()
with patch.object(banner, "check_for_updates", return_value=5):
start = time.monotonic()
banner.prefetch_update_check()
elapsed = time.monotonic() - start
# Should return almost immediately (well under 1 second)
assert elapsed < 1.0
# Wait for the background thread to finish
banner._update_check_done.wait(timeout=5)
assert banner._update_result == 5
def test_get_update_result_timeout():
"""get_update_result() waits up to ``timeout`` seconds and returns.
The original assertion — that the return value is ``None`` — races on
CI: a background update-check thread (from hermes_cli.main's
prefetch_update_check() or an earlier test in the same xdist worker)
can finish a real ``git fetch`` mid-test and write a genuine commits-
behind count into module-level ``banner._update_result`` (observed:
4950, 4954). The behavior we actually care about here is that
``get_update_result`` respects its ``timeout`` — blocking calls to
``Event.wait()`` should return after the timeout even when the event
is never set. Test that directly.
"""
import hermes_cli.banner as banner
# Fresh Event so we hit the timeout branch deterministically.
banner._update_check_done = threading.Event()
start = time.monotonic()
banner.get_update_result(timeout=0.1)
elapsed = time.monotonic() - start
# Waited at least the timeout, but returned well before a "real" wait
# would have (the default 5s a fully-blocking call would imply).
assert 0.05 < elapsed < 0.5
def test_invalidate_update_cache_clears_all_profiles(tmp_path):
"""_invalidate_update_cache() should delete .update_check from ALL profiles."""
from hermes_cli.main import _invalidate_update_cache
# Build a fake ~/.hermes with default + two named profiles
default_home = tmp_path / ".hermes"
default_home.mkdir()
(default_home / ".update_check").write_text('{"ts":1,"behind":50}')
profiles_root = default_home / "profiles"
for name in ("ops", "dev"):
p = profiles_root / name
p.mkdir(parents=True)
(p / ".update_check").write_text('{"ts":1,"behind":50}')
with patch.object(Path, "home", return_value=tmp_path), \
patch.dict(os.environ, {"HERMES_HOME": str(default_home)}):
_invalidate_update_cache()
# All three caches should be gone
assert not (default_home / ".update_check").exists(), "default profile cache not cleared"
assert not (profiles_root / "ops" / ".update_check").exists(), "ops profile cache not cleared"
assert not (profiles_root / "dev" / ".update_check").exists(), "dev profile cache not cleared"
def test_invalidate_update_cache_no_profiles_dir(tmp_path):
"""Works fine when no profiles directory exists (single-profile setup)."""
from hermes_cli.main import _invalidate_update_cache
default_home = tmp_path / ".hermes"
default_home.mkdir()
(default_home / ".update_check").write_text('{"ts":1,"behind":5}')
with patch.object(Path, "home", return_value=tmp_path), \
patch.dict(os.environ, {"HERMES_HOME": str(default_home)}):
_invalidate_update_cache()
assert not (default_home / ".update_check").exists()