"""Tests for configuration module.""" import pytest from pydantic import ValidationError from web.config import SettingsCache, WebSettings # Shared test defaults for required fields _TEST_DEFAULTS = { "searxng_base_url": "http://localhost:55100", "llm_base_url": "http://localhost:14011", "external_url": "http://localhost:51100", } class TestWebSettings: """Tests for WebSettings.""" def test_required_searxng_base_url(self) -> None: """Test that searxng_base_url is required.""" with pytest.raises(ValidationError) as exc_info: WebSettings( llm_base_url="http://localhost:14011", external_url="http://localhost:51100", ) errors = exc_info.value.errors() assert any(e["loc"] == ("searxng_base_url",) for e in errors) def test_required_llm_base_url(self) -> None: """Test that llm_base_url is required (no default).""" with pytest.raises(ValidationError) as exc_info: WebSettings( searxng_base_url="http://localhost:55100", external_url="http://localhost:51100", ) errors = exc_info.value.errors() assert any(e["loc"] == ("llm_base_url",) for e in errors) def test_required_external_url(self) -> None: """Test that external_url is required.""" with pytest.raises(ValidationError) as exc_info: WebSettings( searxng_base_url="http://localhost:55100", llm_base_url="http://localhost:14011", ) errors = exc_info.value.errors() assert any(e["loc"] == ("external_url",) for e in errors) def test_valid_settings(self) -> None: """Test valid settings creation.""" settings = WebSettings(**_TEST_DEFAULTS) assert settings.searxng_base_url == "http://localhost:55100" assert settings.port == 51100 assert settings.host == "0.0.0.0" assert settings.search_default_max_results == 10 assert settings.connect_timeout == 5.0 def test_api_tokens_parsing(self) -> None: """Test comma-separated API tokens parsing.""" settings = WebSettings( **_TEST_DEFAULTS, api_tokens="token1,token2,token3", ) assert settings.api_tokens == frozenset(["token1", "token2", "token3"]) assert settings.auth_enabled is True def test_auth_disabled_by_default(self) -> None: """Test that auth is disabled when no tokens set.""" settings = WebSettings(**_TEST_DEFAULTS) assert settings.api_tokens is None assert settings.auth_enabled is False class TestSettingsCache: """Tests for SettingsCache.""" def test_cache_returns_same_instance(self) -> None: """Test that get() returns the same instance.""" SettingsCache.set(WebSettings(**_TEST_DEFAULTS)) settings1 = SettingsCache.get() settings2 = SettingsCache.get() assert settings1 is settings2 def test_cache_clear(self) -> None: """Test that clear() resets the cache.""" SettingsCache.set(WebSettings(**_TEST_DEFAULTS)) SettingsCache.clear() # This should raise because no .env and no required config vars with pytest.raises(ValidationError): SettingsCache.get()