hermes-agent/tests/tools/test_web_tools_truncate.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

110 lines
4.2 KiB
Python

"""Unit tests for the truncate-and-store web_extract path (no LLM).
Covers convert_base64_images_to_links, _truncate_with_footer, _store_full_text,
_get_extract_char_limit, and the end-to-end web_extract_tool truncation behavior.
"""
import asyncio
import json
import os
from unittest.mock import patch
import pytest
import tools.web_tools as wt
class TestImageConversion:
def test_markdown_base64_image_keeps_alt_drops_blob(self):
blob = "A" * 5000
text = f"before ![a cat]( data:image/png;base64,{blob}) after"
out = wt.convert_base64_images_to_links(text)
assert "[IMAGE: a cat]" in out
assert "base64" not in out
assert blob not in out
assert "before" in out and "after" in out
def test_bare_and_parenthesised_base64_become_placeholder(self):
blob = "Z" * 3000
bare = wt.convert_base64_images_to_links(f"data:image/gif;base64,{blob}")
assert bare == "[IMAGE]"
paren = wt.convert_base64_images_to_links(f"(data:image/gif;base64,{blob})")
assert paren == "[IMAGE]"
class TestTruncation:
def test_short_content_returned_whole(self):
content = "# Title\n\nshort body\n"
out, truncated = wt._truncate_with_footer(content, "https://e.com", 15000)
assert out == content
assert truncated is False
def test_truncation_stores_full_text_readable(self, tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
body = "UNIQUE_MIDDLE_MARKER\n" + ("\n".join(f"row {i}" for i in range(5000)))
out, truncated = wt._truncate_with_footer(body, "https://example.com/doc", 3000)
assert truncated is True
# Extract the stored path from the footer and confirm full text is there.
path_line = next(ln for ln in out.splitlines() if "Full text saved to:" in ln)
stored_path = path_line.split("Full text saved to:", 1)[1].strip()
assert os.path.exists(stored_path)
full = open(stored_path).read()
assert "UNIQUE_MIDDLE_MARKER" in full
assert "row 2500" in full # the omitted-middle row is in the stored file
class TestCharLimitConfig:
def test_default_when_unset(self):
with patch("tools.web_tools._load_web_config", return_value={}):
assert wt._get_extract_char_limit() == wt.DEFAULT_EXTRACT_CHAR_LIMIT
def test_bad_value_falls_back(self):
with patch("tools.web_tools._load_web_config", return_value={"extract_char_limit": "nope"}):
assert wt._get_extract_char_limit() == wt.DEFAULT_EXTRACT_CHAR_LIMIT
class TestEndToEnd:
def test_web_extract_truncates_large_page_no_llm(self, tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
big = "\n".join(f"para {i} " + "y" * 80 for i in range(3000))
class FakeProvider:
name = "fake"
display_name = "Fake"
def supports_extract(self):
return True
async def extract(self, urls, **kwargs):
return [{"url": urls[0], "title": "Big Page", "content": big,
"raw_content": big, "metadata": {}}]
with patch("tools.web_tools._ensure_web_plugins_loaded"), \
patch("tools.web_tools._get_extract_backend", return_value="fake"), \
patch("tools.web_tools.async_is_safe_url", new=_AsyncTrue()), \
patch("agent.web_search_registry.get_provider", return_value=FakeProvider()):
result = json.loads(asyncio.new_event_loop().run_until_complete(
wt.web_extract_tool(["https://example.com/big"], char_limit=5000)
))
assert "results" in result
content = result["results"][0]["content"]
assert "[TRUNCATED]" in content
assert "Full text saved to:" in content
# No LLM was involved: para 0 (head) and the last para (tail) are verbatim.
assert "para 0 " in content
assert "para 2999 " in content
def _make_awaitable(value):
async def _coro(*a, **k):
return value
return _coro()
class _AsyncTrue:
"""Async callable that always returns True (re-awaitable per call)."""
async def __call__(self, *a, **k):
return True