mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
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.
185 lines
6.7 KiB
Python
185 lines
6.7 KiB
Python
"""Tests for ``hermes_cli.diagnostics_upload`` — the Nous-S3 upload client.
|
|
|
|
All network I/O is mocked at ``urllib.request.urlopen``; no real requests
|
|
are made.
|
|
"""
|
|
|
|
import io
|
|
import json
|
|
import urllib.error
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
def _resp(*, status=200, body=b""):
|
|
"""Build a context-manager mock mimicking ``urllib.request.urlopen``."""
|
|
m = MagicMock()
|
|
m.status = status
|
|
m.getcode.return_value = status
|
|
m.read.return_value = body
|
|
m.__enter__ = lambda s: s
|
|
m.__exit__ = MagicMock(return_value=False)
|
|
return m
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# request_upload_url
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestRequestUploadUrl:
|
|
def test_happy_path_posts_json_and_returns_dict(self):
|
|
from hermes_cli.diagnostics_upload import request_upload_url
|
|
|
|
payload = {
|
|
"success": True,
|
|
"id": "abc-123",
|
|
"uploadUrl": "https://bucket.s3.amazonaws.com/uploads/abc-123.json.gz?sig",
|
|
"viewUrl": "https://support.example.com/diagnostics/abc-123",
|
|
"uploadExpiresInSeconds": 900,
|
|
}
|
|
resp = _resp(status=200, body=json.dumps(payload).encode())
|
|
|
|
with patch(
|
|
"hermes_cli.diagnostics_upload.urllib.request.urlopen",
|
|
return_value=resp,
|
|
) as urlopen:
|
|
result = request_upload_url(content_type="application/gzip", size_bytes=512)
|
|
|
|
assert result == payload
|
|
|
|
# The request object passed to urlopen carries our JSON body + headers.
|
|
req = urlopen.call_args[0][0]
|
|
assert req.method == "POST"
|
|
assert req.full_url.endswith("/api/diagnostics/upload-url")
|
|
sent = json.loads(req.data.decode())
|
|
assert sent["contentType"] == "application/gzip"
|
|
assert sent["sizeBytes"] == 512
|
|
# urllib lower-cases header keys.
|
|
assert req.headers["Content-type"] == "application/json"
|
|
|
|
def test_non_2xx_raises(self):
|
|
from hermes_cli.diagnostics_upload import request_upload_url
|
|
|
|
resp = _resp(status=500, body=b"boom")
|
|
with patch(
|
|
"hermes_cli.diagnostics_upload.urllib.request.urlopen",
|
|
return_value=resp,
|
|
):
|
|
with pytest.raises(RuntimeError):
|
|
request_upload_url()
|
|
|
|
|
|
def test_base_url_env_override(self, monkeypatch):
|
|
# NAS_BASE is read at import time; re-import the module under the
|
|
# patched env to confirm the override is honoured.
|
|
import importlib
|
|
|
|
monkeypatch.setenv("HERMES_DIAGNOSTICS_BASE_URL", "https://staging.example.com")
|
|
import hermes_cli.diagnostics_upload as mod
|
|
|
|
mod = importlib.reload(mod)
|
|
try:
|
|
assert mod.NAS_BASE == "https://staging.example.com"
|
|
resp = _resp(
|
|
status=200,
|
|
body=json.dumps({"uploadUrl": "u", "id": "i", "viewUrl": "v"}).encode(),
|
|
)
|
|
with patch(
|
|
"hermes_cli.diagnostics_upload.urllib.request.urlopen",
|
|
return_value=resp,
|
|
) as urlopen:
|
|
mod.request_upload_url()
|
|
req = urlopen.call_args[0][0]
|
|
assert req.full_url == "https://staging.example.com/api/diagnostics/upload-url"
|
|
finally:
|
|
monkeypatch.delenv("HERMES_DIAGNOSTICS_BASE_URL", raising=False)
|
|
importlib.reload(mod)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# put_bundle
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestPutBundle:
|
|
def test_put_sends_exact_body_and_content_type(self):
|
|
from hermes_cli.diagnostics_upload import put_bundle
|
|
|
|
data = b"\x1f\x8b\x08gzipped-bytes"
|
|
resp = _resp(status=200, body=b"")
|
|
|
|
with patch(
|
|
"hermes_cli.diagnostics_upload.urllib.request.urlopen",
|
|
return_value=resp,
|
|
) as urlopen:
|
|
put_bundle("https://bucket.s3.amazonaws.com/uploads/x.json.gz?sig", data)
|
|
|
|
req = urlopen.call_args[0][0]
|
|
assert req.method == "PUT"
|
|
# PUT body must be the bundle bytes, unchanged.
|
|
assert req.data == data
|
|
assert req.headers["Content-type"] == "application/gzip"
|
|
|
|
|
|
|
|
def test_http_error_propagates(self):
|
|
from hermes_cli.diagnostics_upload import put_bundle
|
|
|
|
err = urllib.error.HTTPError("https://u", 500, "err", {}, io.BytesIO(b""))
|
|
with patch(
|
|
"hermes_cli.diagnostics_upload.urllib.request.urlopen",
|
|
side_effect=err,
|
|
):
|
|
with pytest.raises(urllib.error.HTTPError):
|
|
put_bundle("https://u", b"data")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# share_to_nous (orchestration)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestShareToNous:
|
|
def test_orchestrates_request_then_put(self):
|
|
from hermes_cli import diagnostics_upload as mod
|
|
|
|
info = {
|
|
"id": "id-9",
|
|
"uploadUrl": "https://bucket/uploads/id-9.json.gz?sig",
|
|
"viewUrl": "https://support/diagnostics/id-9",
|
|
"expiresAt": "2026-06-20T00:00:00Z",
|
|
}
|
|
blob = b"gzipped-bundle"
|
|
|
|
with patch.object(mod, "request_upload_url", return_value=info) as req, \
|
|
patch.object(mod, "put_bundle") as put:
|
|
result = mod.share_to_nous(blob)
|
|
|
|
assert result == info
|
|
req.assert_called_once()
|
|
# request was told the real byte size (NAS signs it into ContentLength)
|
|
assert req.call_args.kwargs["size_bytes"] == len(blob)
|
|
# PUT got the signed URL + the exact blob
|
|
put.assert_called_once_with(
|
|
info["uploadUrl"], blob, content_type="application/gzip"
|
|
)
|
|
|
|
def test_put_failure_propagates(self):
|
|
from hermes_cli import diagnostics_upload as mod
|
|
|
|
info = {"id": "id-9", "uploadUrl": "https://u", "viewUrl": "v"}
|
|
with patch.object(mod, "request_upload_url", return_value=info), \
|
|
patch.object(mod, "put_bundle", side_effect=RuntimeError("PUT failed")):
|
|
with pytest.raises(RuntimeError):
|
|
mod.share_to_nous(b"data")
|
|
|
|
def test_share_succeeds_without_id_in_response(self):
|
|
from hermes_cli import diagnostics_upload as mod
|
|
|
|
# NAS is stateless and there is no confirm step, so the share must
|
|
# succeed regardless of whether the response carries an ``id``.
|
|
info = {"uploadUrl": "https://u", "viewUrl": "v"} # no id
|
|
with patch.object(mod, "request_upload_url", return_value=info), \
|
|
patch.object(mod, "put_bundle") as put:
|
|
result = mod.share_to_nous(b"data")
|
|
assert result == info
|
|
put.assert_called_once()
|