hermes-agent/tests/agent/test_local_probe_disk_cache.py
Teknium 6b81590c55
test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

118 lines
4.4 KiB
Python

"""Tests for the local-endpoint probe disk L2 cache in agent/model_metadata.
Contract:
- only SUCCESSFUL probes are persisted (failures never pin a verdict);
- fresh disk entries serve cross-process (in-proc caches cleared);
- entries expire after _LOCAL_PROBE_DISK_TTL_SECONDS;
- corrupted cache files degrade to a miss;
- detect_local_server_type and query_ollama_num_ctx both use it.
"""
import json
import time
from unittest.mock import MagicMock, patch
import agent.model_metadata as MM
def _clear_in_proc():
MM._endpoint_probe_path_cache.clear()
MM._LOCAL_CTX_PROBE_CACHE.clear()
def _cache_file():
return MM._local_probe_disk_cache_path()
class TestDiskHelpers:
def test_expired_entry_is_miss(self):
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
path = _cache_file()
data = json.loads(path.read_text(encoding="utf-8"))
for entry in data.values():
entry["ts"] = time.time() - MM._LOCAL_PROBE_DISK_TTL_SECONDS - 1
path.write_text(json.dumps(data), encoding="utf-8")
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:11434") is None
def test_corrupted_file_is_miss(self):
path = _cache_file()
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("not json{{{", encoding="utf-8")
assert MM._local_probe_disk_get("server_type", "anything") is None
# And put() recovers by rewriting cleanly
MM._local_probe_disk_put("server_type", "k", "vllm")
assert MM._local_probe_disk_get("server_type", "k") == "vllm"
class TestDetectLocalServerTypeDiskL2:
def test_disk_hit_skips_http_entirely(self):
_clear_in_proc()
MM._local_probe_disk_put("server_type", "http://127.0.0.1:9999", "ollama")
with patch("httpx.Client") as mock_client:
result = MM.detect_local_server_type("http://127.0.0.1:9999/v1")
assert result == "ollama"
mock_client.assert_not_called()
def test_successful_probe_persists_to_disk(self):
_clear_in_proc()
ok = MagicMock(status_code=200)
ok.json.return_value = {"models": []}
notfound = MagicMock(status_code=404, text="")
def route(url, *a, **k):
return ok if url.endswith("/api/tags") else notfound
client = MagicMock()
client.get.side_effect = route
client.__enter__ = lambda s: client
client.__exit__ = lambda s, *a: False
with patch("httpx.Client", return_value=client):
result = MM.detect_local_server_type("http://127.0.0.1:8888/v1")
assert result == "ollama"
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:8888") == "ollama"
def test_failed_probe_not_persisted(self):
_clear_in_proc()
client = MagicMock()
client.get.side_effect = Exception("connection refused")
client.__enter__ = lambda s: client
client.__exit__ = lambda s, *a: False
with patch("httpx.Client", return_value=client):
result = MM.detect_local_server_type("http://127.0.0.1:7777/v1")
assert result is None
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:7777") is None
class TestOllamaNumCtxDiskL2:
def test_disk_hit_skips_api_show(self):
_clear_in_proc()
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
MM._local_probe_disk_put(
"ollama_num_ctx", "http://127.0.0.1:11434|llama3", 131072
)
with patch("httpx.Client") as mock_client:
ctx = MM.query_ollama_num_ctx("llama3", "http://127.0.0.1:11434/v1")
assert ctx == 131072
mock_client.assert_not_called()
def test_successful_show_persists(self):
_clear_in_proc()
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
resp = MagicMock(status_code=200)
resp.json.return_value = {
"parameters": "",
"model_info": {"llama.context_length": 8192},
}
client = MagicMock()
client.post.return_value = resp
client.__enter__ = lambda s: client
client.__exit__ = lambda s, *a: False
with patch("httpx.Client", return_value=client):
ctx = MM.query_ollama_num_ctx("llama3", "http://127.0.0.1:11434/v1")
assert ctx == 8192
assert (
MM._local_probe_disk_get("ollama_num_ctx", "http://127.0.0.1:11434|llama3")
== 8192
)