diff --git a/.env.example b/.env.example index e291cdc..5575e7b 100644 --- a/.env.example +++ b/.env.example @@ -89,4 +89,4 @@ GW2_WVW_COOLDOWN=20 GW2_API_RETRY_MAX_ATTEMPTS=5 GW2_API_RETRY_DELAY=3.0 GW2_API_SESSION_RETRY_BG_DELAY=30.0 -GW2_API_SESSION_END_DELAY=120 +GW2_API_SESSION_END_DELAY=180 diff --git a/src/gw2/constants/gw2_settings.py b/src/gw2/constants/gw2_settings.py index a68a69e..20dfc91 100644 --- a/src/gw2/constants/gw2_settings.py +++ b/src/gw2/constants/gw2_settings.py @@ -1,6 +1,6 @@ from dotenv import load_dotenv from functools import lru_cache -from pydantic import Field +from pydantic import Field, model_validator from pydantic_settings import BaseSettings, SettingsConfigDict # Lazy loading flag for dotenv @@ -31,7 +31,13 @@ class Gw2Settings(BaseSettings): api_retry_max_attempts: int | None = Field(default=5) api_retry_delay: float | None = Field(default=3.0) api_session_retry_bg_delay: float | None = Field(default=30.0) - api_session_end_delay: float | None = Field(default=120.0) + api_session_end_delay: float | None = Field(default=180.0) + + @model_validator(mode="after") + def _clamp_session_end_delay(self) -> Gw2Settings: + if self.api_session_end_delay is not None and self.api_session_end_delay < 180.0: + self.api_session_end_delay = 180.0 + return self @lru_cache(maxsize=1) diff --git a/tests/unit/gw2/constants/test_gw2_settings.py b/tests/unit/gw2/constants/test_gw2_settings.py index c33f7d1..6e58af5 100644 --- a/tests/unit/gw2/constants/test_gw2_settings.py +++ b/tests/unit/gw2/constants/test_gw2_settings.py @@ -157,6 +157,27 @@ def test_large_values_are_preserved(self): assert settings.session_cooldown == 3600 assert settings.account_cooldown == 86400 + def test_session_end_delay_below_180_clamped(self): + """Test that api_session_end_delay below 180 is clamped to 180.""" + env_vars = {"GW2_API_SESSION_END_DELAY": "60"} + with patch.dict(os.environ, env_vars, clear=True): + settings = Gw2Settings() + assert settings.api_session_end_delay == 180.0 + + def test_session_end_delay_at_180_preserved(self): + """Test that api_session_end_delay at exactly 180 is preserved.""" + env_vars = {"GW2_API_SESSION_END_DELAY": "180"} + with patch.dict(os.environ, env_vars, clear=True): + settings = Gw2Settings() + assert settings.api_session_end_delay == 180.0 + + def test_session_end_delay_above_180_preserved(self): + """Test that api_session_end_delay above 180 is preserved.""" + env_vars = {"GW2_API_SESSION_END_DELAY": "300"} + with patch.dict(os.environ, env_vars, clear=True): + settings = Gw2Settings() + assert settings.api_session_end_delay == 300.0 + def test_all_cooldown_fields_exist(self): """Test that all expected cooldown fields exist with correct types.""" settings = Gw2Settings()