"""BaseAgent._complete: LLM-disabled + failure fallback contract.""" import pytest from researchlink.agents.base import BaseAgent class _DummyAgent(BaseAgent): name = "DummyAgent" def run(self): # pragma: no cover - not used return None @pytest.fixture(autouse=True) def _reset_settings(): """Rebuild the settings singleton after each test (tests mutate it).""" yield from researchlink import config config.get_settings.__globals__["_settings"] = None def test_complete_uses_extractive_when_offline(): agent = _DummyAgent() agent.settings.offline = True # hard offline → LLM disabled agent.settings.llm_mode = "anthropic" # even with a provider configured called = {"llm": False} def fake_llm(*a, **k): called["llm"] = True return "LLM" agent._call_llm = fake_llm # type: ignore[assignment] out = agent._complete("sys", "user", extractive=lambda: "EXTRACTIVE") assert out == "EXTRACTIVE" assert called["llm"] is False # never touched the model def test_complete_falls_back_when_llm_raises(): agent = _DummyAgent() agent.settings.offline = False agent.settings.llm_mode = "anthropic" # LLM enabled def boom(*a, **k): raise RuntimeError("no providers") agent._call_llm = boom # type: ignore[assignment] out = agent._complete("sys", "user", extractive=lambda: "EXTRACTIVE") assert out == "EXTRACTIVE" def test_complete_uses_llm_when_available(): agent = _DummyAgent() agent.settings.offline = False agent.settings.llm_mode = "anthropic" # LLM enabled agent._call_llm = lambda *a, **k: "LLM-OUTPUT" # type: ignore[assignment] out = agent._complete("sys", "user", extractive=lambda: "EXTRACTIVE") assert out == "LLM-OUTPUT" def test_llm_disabled_property(): agent = _DummyAgent() agent.settings.offline = False agent.settings.llm_mode = "offline" assert agent.settings.llm_disabled is True # llm_mode=offline disables LLM agent.settings.llm_mode = "anthropic" assert agent.settings.llm_disabled is False # but network still allowed assert agent.settings.public_config()["network_enabled"] is True