Files
hermes-agent/tests/scripts/install/test_install_sh_node_global_prefix.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

59 lines
2.7 KiB
Python

"""Regression tests for the Hermes-managed Node's npm global prefix.
When the installer falls back to a bundled Node under ``$HERMES_HOME/node``,
npm's default global prefix is that Node dir, so ``npm install -g <pkg>``
drops the package binary in ``$HERMES_HOME/node/bin`` — which is NOT on PATH
(only the command link dir is) and is wiped on every Node upgrade. Users then
report "I can ``npm i -g`` but the package isn't usable on the command line".
The fix redirects the bundled Node's global prefix to the command link dir's
parent (so global bins land in the already-on-PATH link dir alongside
node/npm/npx), scoped to the bundled Node via its prefix-local global npmrc.
"""
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent.parent.parent
INSTALL_SH = REPO_ROOT / "scripts" / "install.sh"
NODE_BOOTSTRAP = REPO_ROOT / "scripts" / "lib" / "node-bootstrap.sh"
def test_install_sh_redirects_bundled_npm_global_prefix_to_link_dir() -> None:
text = INSTALL_SH.read_text()
# The redirect must target the link dir's PARENT so global bins resolve to
# <parent>/bin == the command link dir (node/npm/npx live there and it is
# guaranteed on PATH by the installer's PATH setup).
assert "configure_managed_node_npm_prefix()" in text
assert 'printf \'prefix=%s\\n\' "$(dirname "$link_dir")" > "$HERMES_HOME/node/etc/npmrc"' in text
def test_install_sh_repairs_existing_managed_node_on_rerun() -> None:
"""The redirect must run on every install (not just fresh Node installs),
so re-running the installer repairs pre-existing managed installs whose
Node is already up to date and would otherwise skip install_node."""
text = INSTALL_SH.read_text()
check_node_body = text.split("check_node()", 1)[1].split("\ninstall_node()", 1)[0]
assert "configure_managed_node_npm_prefix" in check_node_body
# No-op guard so it's safe to call when there is no managed Node.
assert '[ -x "$HERMES_HOME/node/bin/npm" ] || return 0' in text
def test_node_bootstrap_redirects_bundled_npm_global_prefix_to_link_dir() -> None:
text = NODE_BOOTSTRAP.read_text()
assert "_nb_configure_npm_prefix()" in text
assert 'printf \'prefix=%s\\n\' "$(dirname "$_link_dir")" > "$HERMES_HOME/node/etc/npmrc"' in text
# Runs at the top of ensure_node so existing managed installs are repaired
# even when a modern Node is already present (early return path).
ensure_node_body = text.split("ensure_node()", 1)[1]
assert "_nb_configure_npm_prefix" in ensure_node_body
assert '[ -x "$HERMES_HOME/node/bin/npm" ] || return 0' in text
assert "heal_managed_node()" in text
assert "_nb_managed_tool_broken" in text
assert "for tool in node npm npx" in text