"""Tests for llm_backend.parse_backend + state machine.""" import os import importlib import sys def test_parse_backend_groq_aliases(): if 'llm_backend' in sys.modules: del sys.modules['llm_backend'] m = importlib.import_module('llm_backend') for alias in ('groq', 'GROQ', 'cloud', 'облако', 'клауд', ' Groq '): assert m.parse_backend(alias) == 'groq', f"failed: {alias!r}" def test_parse_backend_ollama_aliases(): if 'llm_backend' in sys.modules: del sys.modules['llm_backend'] m = importlib.import_module('llm_backend') for alias in ('ollama', 'OLLAMA', 'local', 'локальный', 'локал', 'локальн'): assert m.parse_backend(alias) == 'ollama', f"failed: {alias!r}" def test_parse_backend_unknown_returns_none(): if 'llm_backend' in sys.modules: del sys.modules['llm_backend'] m = importlib.import_module('llm_backend') assert m.parse_backend('openai') is None assert m.parse_backend('claude') is None assert m.parse_backend('') is None assert m.parse_backend(None) is None def test_persist_round_trip(tmp_path, monkeypatch): if 'llm_backend' in sys.modules: del sys.modules['llm_backend'] m = importlib.import_module('llm_backend') monkeypatch.setattr(m, '_CHOICE_FILE', str(tmp_path / 'llm_backend.txt')) m._persist('ollama') assert m._read_persisted() == 'ollama' m._persist('groq') assert m._read_persisted() == 'groq' def test_auto_detect_fallback_to_ollama(tmp_path, monkeypatch): """When no GROQ_TOKEN in config, auto-detect should pick ollama.""" if 'llm_backend' in sys.modules: del sys.modules['llm_backend'] m = importlib.import_module('llm_backend') # Mock config module with no GROQ_TOKEN class FakeConfig: GROQ_TOKEN = '' monkeypatch.setitem(sys.modules, 'config', FakeConfig()) assert m._auto_detect_backend() == 'ollama' def test_auto_detect_picks_groq_when_token_present(tmp_path, monkeypatch): if 'llm_backend' in sys.modules: del sys.modules['llm_backend'] m = importlib.import_module('llm_backend') class FakeConfig: GROQ_TOKEN = 'gsk_fake_test_token' monkeypatch.setitem(sys.modules, 'config', FakeConfig()) assert m._auto_detect_backend() == 'groq'