from typing import Any from app.core.config import Settings from app.modules.ai_agent import adapters class DummyResponse: def __init__(self, data: dict[str, Any], status_code: int = 200): self._data = data self.status_code = status_code self.text = str(data) def json(self) -> dict[str, Any]: return self._data class DummyClient: calls: list[dict[str, Any]] = [] def __init__(self, timeout: int): self.timeout = timeout def __enter__(self) -> "DummyClient": return self def __exit__(self, exc_type, exc, traceback) -> None: return None def post( self, url: str, json: dict[str, Any], headers: dict[str, str], ) -> DummyResponse: self.calls.append({"url": url, "json": json, "headers": headers}) if url.endswith("/api/v1/agent/ask"): assert json["context"]["hermes_memory"] == "remembered project preference" return DummyResponse({"answer": "openclaw final answer", "tool_calls": 1}) mode = json["context"]["mode"] if mode == "memory_recall": return DummyResponse({"answer": "remembered project preference"}) if mode == "memory_write": return DummyResponse({"answer": "stored"}) raise AssertionError(f"unexpected Hermes mode: {mode}") def test_openclaw_hermes_adapter_runs_recall_answer_and_remember(monkeypatch) -> None: DummyClient.calls = [] monkeypatch.setattr(adapters.httpx, "Client", DummyClient) settings = Settings( model_provider="openclaw_hermes", openclaw_base_url="http://openclaw.local", openclaw_api_key="openclaw-key", hermes_base_url="http://hermes.local", hermes_api_key="hermes-key", ) result = adapters.OpenClawHermesAdapter(settings).ask( "summarize project risk", {"project_code": "P-001"}, ) assert result["answer"] == "openclaw final answer" assert result["raw"]["pipeline"] == "hermes_recall -> openclaw_answer -> hermes_remember" assert [call["url"] for call in DummyClient.calls] == [ "http://hermes.local/api/v1/ask", "http://openclaw.local/api/v1/agent/ask", "http://hermes.local/api/v1/ask", ] assert DummyClient.calls[0]["headers"]["Authorization"] == "Bearer hermes-key" assert DummyClient.calls[1]["headers"]["Authorization"] == "Bearer openclaw-key" assert DummyClient.calls[1]["json"]["context"]["agent_pipeline"] == "openclaw_hermes"