Skip to content

The agent that grows with you

test(models): two invariants for the OpenRouter curated-catalog disk cache

kshitijk4poor441157f18482
diff --git a/tests/hermes_cli/test_models.py b/tests/hermes_cli/test_models.py
index ce6f25aa219..c5690d450c4 100644
--- a/tests/hermes_cli/test_models.py
+++ b/tests/hermes_cli/test_models.py
@@ -1,6 +1,8 @@
"""Tests for the hermes_cli models module."""
import json
+import pytest
+import time
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
from unittest.mock import patch, MagicMock
@@ -1519,3 +1521,54 @@ class TestLocalOllamaModelDiscovery:
assert result.success is True
assert result.api_key == "no-key-required"
+
+
+class TestOpenRouterCatalogDiskCache:
+ """A cold process must serve the curated OpenRouter picker list from disk within the TTL and
+ fall through to the live fetch when the snapshot is expired or corrupt."""
+
+ @pytest.fixture(autouse=True)
+ def _reset(self, monkeypatch):
+ monkeypatch.setattr(_models_mod, "_openrouter_catalog_cache", None)
+ yield
+ monkeypatch.setattr(_models_mod, "_openrouter_catalog_cache", None)
+
+ @staticmethod
+ def _install_network(monkeypatch, calls):
+ payload = {"data": [{"id": "a/one", "supported_parameters": ["tools"],
+ "pricing": {"prompt": "0", "completion": "0"}}]}
+
+ def fake_index(url, timeout, opener):
+ calls.append(url)
+ return payload["data"], {m["id"]: m for m in payload["data"]}
+
+ monkeypatch.setattr(_models_mod, "_fetch_live_catalog_index", fake_index)
+ monkeypatch.setattr("hermes_cli.model_catalog.get_curated_openrouter_models",
+ lambda: [("a/one", "")])
+
+ def test_fresh_snapshot_serves_cold_process_without_network(self, monkeypatch, tmp_path):
+ calls = []
+ self._install_network(monkeypatch, calls)
+ first = fetch_openrouter_models()
+ assert calls and _models_mod._openrouter_catalog_disk_path().is_file()
+
+ _models_mod._openrouter_catalog_cache = None # simulate a fresh process
+ calls.clear()
+ assert fetch_openrouter_models() == first
+ assert calls == []
+
+ def test_expired_or_corrupt_snapshot_falls_through_to_fetch(self, monkeypatch, tmp_path):
+ calls = []
+ self._install_network(monkeypatch, calls)
+ path = _models_mod._openrouter_catalog_disk_path()
+ path.parent.mkdir(parents=True, exist_ok=True)
+
+ stale = time.time() - _models_mod._openrouter_catalog_disk_ttl() - 1
+ path.write_text(json.dumps({"fetched_at": stale, "curated": [["old/model", ""]]}))
+ assert fetch_openrouter_models() == [("a/one", "free")]
+ assert len(calls) == 1
+
+ _models_mod._openrouter_catalog_cache = None
+ path.write_text("{not json")
+ assert fetch_openrouter_models() == [("a/one", "free")]
+ assert len(calls) == 2