hermes-agent/tests/hermes_cli/test_mcp_add_command_dest.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

92 lines
3.1 KiB
Python

"""Regression test: ``hermes mcp add --command`` must not clobber the
top-level ``args.command`` subparser dest.
The top-level argparse parser uses ``dest="command"`` for its subparsers
(``hermes_cli/_parser.py``). The dispatcher in ``hermes_cli/main.py``
reads ``args.command`` to decide which command to run; if it is ``None``
it falls through to interactive chat.
The ``mcp add`` subparser exposes a ``--command`` flag (the stdio command
for an MCP server, e.g. ``npx``). Without an explicit ``dest=``, argparse
derives the dest from the flag name and writes ``args.command = None``
when the flag is omitted, overwriting the top-level ``"mcp"`` value. As a
result, ``hermes mcp add foo --url ...`` silently launches chat instead
of registering an MCP server.
The fix: declare the flag with ``dest="mcp_command"``. The CLI flag name
is unchanged; only the in-memory attribute moves.
We replicate the relevant parser shape here rather than importing the
real builder, mirroring ``test_argparse_flag_propagation.py`` and
``test_subparser_routing_fallback.py``.
"""
import argparse
def _build_parser():
"""Minimal replica of the slice of the hermes parser that exhibits
the bug: top-level subparsers (dest="command") and ``mcp add`` with
its ``--command`` flag.
"""
parser = argparse.ArgumentParser(prog="hermes")
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("chat")
mcp_p = subparsers.add_parser("mcp")
mcp_sub = mcp_p.add_subparsers(dest="mcp_action")
mcp_add = mcp_sub.add_parser("add")
mcp_add.add_argument("name")
mcp_add.add_argument("--url")
mcp_add.add_argument("--command", dest="mcp_command")
mcp_add.add_argument("--connect-timeout", type=float)
mcp_add.add_argument("--args", nargs=argparse.REMAINDER, default=[])
return parser
class TestMcpAddCommandDest:
def test_url_invocation_preserves_top_level_command(self):
"""`hermes mcp add foo --url ...` must keep args.command == "mcp".
Before the dest fix this was clobbered to None, sending the
dispatcher into the chat fallback.
"""
parser = _build_parser()
args = parser.parse_args(
["mcp", "add", "foo", "--url", "https://example.com/mcp"]
)
assert args.command == "mcp"
assert args.mcp_action == "add"
assert args.name == "foo"
assert args.url == "https://example.com/mcp"
assert args.mcp_command is None
def test_args_passthrough_keeps_nested_option_flags(self):
"""`--args` must keep command flags like Docker MCP's --profile."""
parser = _build_parser()
args = parser.parse_args(
[
"mcp",
"add",
"docker-research",
"--command",
"docker",
"--args",
"mcp",
"gateway",
"run",
"--profile",
"research",
]
)
assert args.command == "mcp"
assert args.mcp_command == "docker"
assert args.args == ["mcp", "gateway", "run", "--profile", "research"]