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.
123 lines
3.8 KiB
Python
123 lines
3.8 KiB
Python
"""/api/audio/speak-stream — desktop streaming TTS over WebSocket."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from urllib.parse import urlencode
|
|
|
|
import pytest
|
|
from starlette.testclient import TestClient
|
|
from starlette.websockets import WebSocketDisconnect
|
|
|
|
from hermes_cli import web_server
|
|
|
|
|
|
@pytest.fixture
|
|
def stream_client(monkeypatch, _isolate_hermes_home):
|
|
previous_auth_required = getattr(web_server.app.state, "auth_required", None)
|
|
web_server.app.state.auth_required = False
|
|
|
|
client = TestClient(web_server.app)
|
|
try:
|
|
yield client
|
|
finally:
|
|
close = getattr(client, "close", None)
|
|
if close is not None:
|
|
close()
|
|
if previous_auth_required is None:
|
|
if hasattr(web_server.app.state, "auth_required"):
|
|
delattr(web_server.app.state, "auth_required")
|
|
else:
|
|
web_server.app.state.auth_required = previous_auth_required
|
|
|
|
|
|
def _url(token: str | None = None) -> str:
|
|
return f"/api/audio/speak-stream?{urlencode({'token': token or web_server._SESSION_TOKEN})}"
|
|
|
|
|
|
class _FakeStreamer:
|
|
sample_rate = 24000
|
|
channels = 1
|
|
|
|
def __init__(self, chunks):
|
|
self.chunks = chunks
|
|
self.requests: list[str] = []
|
|
|
|
def stream(self, text):
|
|
self.requests.append(text)
|
|
yield from self.chunks
|
|
|
|
|
|
def _patch_provider(monkeypatch, streamer, cap=4000):
|
|
monkeypatch.setattr("tools.tts_streaming.resolve_streaming_provider", lambda cfg: streamer)
|
|
monkeypatch.setattr("tools.tts_tool._load_tts_config", lambda: {})
|
|
monkeypatch.setattr("tools.tts_tool._get_provider", lambda cfg: "fake")
|
|
monkeypatch.setattr("tools.tts_tool._resolve_max_text_length", lambda provider, cfg: cap)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_streams_pcm_frames_then_end(stream_client, monkeypatch):
|
|
streamer = _FakeStreamer([b"\x01\x02\x03\x04", b"\x05\x06"])
|
|
_patch_provider(monkeypatch, streamer)
|
|
|
|
with stream_client.websocket_connect(_url()) as conn:
|
|
start = conn.receive_json()
|
|
assert start == {"type": "start", "sample_rate": 24000, "channels": 1}
|
|
|
|
conn.send_text(json.dumps({"text": "Hello there.", "done": True}))
|
|
assert conn.receive_bytes() == b"\x01\x02\x03\x04"
|
|
assert conn.receive_bytes() == b"\x05\x06"
|
|
assert conn.receive_json() == {"type": "end"}
|
|
|
|
assert streamer.requests == ["Hello there."]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_long_text_is_split_across_provider_requests(stream_client, monkeypatch):
|
|
streamer = _FakeStreamer([b"\x00\x00"])
|
|
_patch_provider(monkeypatch, streamer, cap=24)
|
|
|
|
with stream_client.websocket_connect(_url()) as conn:
|
|
assert conn.receive_json()["type"] == "start"
|
|
conn.send_text(
|
|
json.dumps(
|
|
{"text": "First sentence here. Second sentence here. Third one.", "done": True}
|
|
)
|
|
)
|
|
# One PCM frame per split piece, then end.
|
|
frames = 0
|
|
while True:
|
|
message = conn.receive()
|
|
if message.get("bytes") is not None:
|
|
frames += 1
|
|
else:
|
|
assert json.loads(message["text"]) == {"type": "end"}
|
|
break
|
|
|
|
assert len(streamer.requests) > 1
|
|
assert frames == len(streamer.requests)
|
|
# Nothing lost in the split: every sentence reached the provider.
|
|
joined = " ".join(streamer.requests)
|
|
for fragment in ("First sentence here.", "Second sentence here.", "Third one."):
|
|
assert fragment in joined
|
|
|
|
|
|
def test_split_text_respects_cap_and_preserves_content():
|
|
text = "Alpha beta. Gamma delta epsilon. Zeta eta theta iota kappa."
|
|
pieces = web_server._split_text_for_speak_stream(text, 30)
|
|
assert pieces
|
|
assert all(len(piece) <= 30 for piece in pieces)
|
|
joined = " ".join(pieces)
|
|
for word in text.replace(".", "").split():
|
|
assert word in joined
|
|
|
|
|