Files
hermes-agent/tests/scripts/install/test_install_ps1_python_fallback_venv.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

114 lines
4.6 KiB
Python

"""Regression: the Windows installer must honor its Python fallback at venv time.
A user on Windows 11 reported (#50769) that the installer correctly detected a
Python 3.12 fallback when 3.11 was absent::
[OK] Found fallback: Python 3.12.8
...
-> Creating virtual environment with Python 3.11...
and then failed with ``Failed to create virtual environment (uv venv exited
with 2)``.
Root cause: ``Test-Python`` records the fallback via an in-memory
``$script:PythonVersion = $fallbackVer`` mutation, but under Hermes-Setup.exe
each ``-Stage NAME`` runs in its *own* fresh ``powershell.exe`` process. The
``venv`` stage therefore starts with ``$PythonVersion`` back at its ``"3.11"``
default, so ``uv venv venv --python 3.11`` runs on a machine that has no 3.11.
The fix re-resolves the interpreter inside ``Install-Venv`` (via the
cross-process-safe ``Resolve-AvailablePythonVersion`` helper) before creating
the venv, so the venv stage uses whatever interpreter is actually present.
These tests lock that contract at the source level (the script only runs on
Windows, so there's no runner to execute it on Linux CI).
"""
import re
from pathlib import Path
import pytest
_INSTALL_PS1 = Path(__file__).resolve().parents[3] / "scripts" / "install.ps1"
@pytest.fixture(scope="module")
def source() -> str:
return _INSTALL_PS1.read_text(encoding="utf-8")
def _function_body(source: str, name: str) -> str:
"""Return the text of a PowerShell ``function <name> { ... }`` block."""
start = source.index(f"function {name}")
brace = source.index("{", start)
depth = 0
for i in range(brace, len(source)):
if source[i] == "{":
depth += 1
elif source[i] == "}":
depth -= 1
if depth == 0:
return source[brace : i + 1]
raise AssertionError(f"unterminated function body for {name}")
def test_resolver_helper_is_defined(source: str):
"""A cross-process-safe Python-version resolver must exist."""
assert "function Resolve-AvailablePythonVersion" in source, (
"expected a Resolve-AvailablePythonVersion helper that re-resolves the "
"interpreter independently of the in-memory $script:PythonVersion mutation"
)
def test_install_venv_reresolves_before_creating_venv(source: str):
"""Install-Venv must re-resolve the interpreter BEFORE `uv venv`.
Otherwise the venv stage's fresh process trusts the stale "3.11" default
and fails on machines where only the fallback (e.g. 3.12) is installed.
"""
body = _function_body(source, "Install-Venv")
resolve_at = body.find("Resolve-AvailablePythonVersion")
assert resolve_at != -1, (
"Install-Venv must call Resolve-AvailablePythonVersion so the venv "
"stage doesn't trust the stale $PythonVersion default across processes"
)
create_at = body.find("Creating virtual environment with Python")
assert create_at != -1, "expected the venv-creation log line in Install-Venv"
assert resolve_at < create_at, (
"the interpreter must be re-resolved BEFORE the 'Creating virtual "
"environment' step (and before `uv venv` runs)"
)
def test_fallback_list_is_single_source_of_truth(source: str):
"""The fallback versions live in one shared constant, used by both paths.
A drifting second copy of the list is how detection and venv creation
disagree in the first place.
"""
assert re.search(r"\$PythonFallbackVersions\s*=", source), (
"expected a shared $PythonFallbackVersions constant"
)
# Test-Python's fallback loop must iterate the shared constant, not an
# inline literal list.
test_python = _function_body(source, "Test-Python")
assert "foreach ($fallbackVer in $PythonFallbackVersions)" in test_python, (
"Test-Python must iterate the shared $PythonFallbackVersions constant"
)
# The resolver must seed its candidate list from the same constant.
resolver = _function_body(source, "Resolve-AvailablePythonVersion")
assert "$PythonFallbackVersions" in resolver, (
"Resolve-AvailablePythonVersion must reuse the shared fallback constant"
)
def test_resolver_prefers_requested_version_then_fallbacks(source: str):
"""The resolver tries the requested version first, then the fallbacks."""
resolver = _function_body(source, "Resolve-AvailablePythonVersion")
assert "@($PythonVersion) + $PythonFallbackVersions" in resolver, (
"resolver candidate order must be: requested $PythonVersion first, "
"then the shared fallbacks"
)
assert "uv" in resolver and "python find" in resolver, (
"resolver must probe availability via `uv python find`"
)