import urllib.error import pytest from robotics.llm import providers as P def test_falls_through_on_connection_reset(monkeypatch): monkeypatch.setenv("GROQ_API_KEY", "x") monkeypatch.setenv("CEREBRAS_API_KEY", "y") calls = [] def fake_post(url, key, payload, timeout): calls.append(url) if "groq" in url: raise ConnectionResetError(104, "reset") return {"choices": [{"message": {"content": "hi"}}]} monkeypatch.setattr(P, "_post", fake_post) assert P.chat([{"role": "user", "content": "x"}], providers=["groq", "cerebras"]) == ("hi", "cerebras") def test_empty_content_is_an_error(monkeypatch): monkeypatch.setenv("GROQ_API_KEY", "x") monkeypatch.setattr(P, "_post", lambda *a: {"choices": [{"message": {"content": None}}]}) with pytest.raises(P.LLMError, match="empty content"): P.chat([{"role": "user", "content": "x"}], providers=["groq"]) def test_no_keys(monkeypatch): for p in P.PROVIDERS.values(): monkeypatch.delenv(p.key_env, raising=False) with pytest.raises(P.LLMError, match="No LLM provider keys"): P.chat([{"role": "user", "content": "x"}])