|
| 1 | +"""Tests for bot configuration loading.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +class TestBotSettings: |
| 9 | + """Tests for BotSettings class.""" |
| 10 | + |
| 11 | + def test_bot_settings_loads_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 12 | + """Test that BotSettings loads from environment variables.""" |
| 13 | + from byte_bot.config import BotSettings |
| 14 | + |
| 15 | + # Set up environment variables |
| 16 | + monkeypatch.setenv("DISCORD_TOKEN", "test-token-123") |
| 17 | + monkeypatch.setenv("DISCORD_DEV_GUILD_ID", "987654321") |
| 18 | + monkeypatch.setenv("API_SERVICE_URL", "http://test-api:9000") |
| 19 | + monkeypatch.setenv("ENVIRONMENT", "test") |
| 20 | + |
| 21 | + # Load settings |
| 22 | + settings = BotSettings() |
| 23 | + |
| 24 | + # Verify settings loaded correctly |
| 25 | + assert settings.discord_token == "test-token-123" |
| 26 | + assert settings.discord_dev_guild_id == 987654321 |
| 27 | + assert settings.api_service_url == "http://test-api:9000" |
| 28 | + assert settings.environment == "test" |
| 29 | + |
| 30 | + def test_command_prefix_assembly(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 31 | + """Test command prefix assembly based on environment.""" |
| 32 | + from byte_bot.config import BotSettings |
| 33 | + |
| 34 | + monkeypatch.setenv("DISCORD_TOKEN", "test-token") |
| 35 | + monkeypatch.setenv("ENVIRONMENT", "dev") |
| 36 | + |
| 37 | + settings = BotSettings() |
| 38 | + |
| 39 | + # Dev environment should have "nibble " prefix |
| 40 | + assert "nibble " in settings.command_prefix |
| 41 | + |
| 42 | + def test_presence_url_assembly(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 43 | + """Test presence URL assembly based on environment.""" |
| 44 | + from byte_bot.config import BotSettings |
| 45 | + |
| 46 | + monkeypatch.setenv("DISCORD_TOKEN", "test-token") |
| 47 | + monkeypatch.setenv("ENVIRONMENT", "dev") |
| 48 | + |
| 49 | + settings = BotSettings() |
| 50 | + |
| 51 | + # Dev environment should use dev URL |
| 52 | + assert "dev.byte-bot.app" in settings.presence_url |
| 53 | + |
| 54 | + |
| 55 | +class TestLogSettings: |
| 56 | + """Tests for LogSettings class.""" |
| 57 | + |
| 58 | + def test_log_settings_has_defaults(self) -> None: |
| 59 | + """Test that LogSettings has reasonable default values.""" |
| 60 | + from byte_bot.config import LogSettings |
| 61 | + |
| 62 | + settings = LogSettings() |
| 63 | + |
| 64 | + # Should have default values (may be from env or defaults) |
| 65 | + assert isinstance(settings.level, int) |
| 66 | + assert isinstance(settings.discord_level, int) |
| 67 | + assert isinstance(settings.websockets_level, int) |
| 68 | + assert isinstance(settings.asyncio_level, int) |
| 69 | + assert isinstance(settings.httpx_level, int) |
| 70 | + |
| 71 | + |
| 72 | +class TestLoadSettings: |
| 73 | + """Tests for load_settings function.""" |
| 74 | + |
| 75 | + def test_load_settings_success(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 76 | + """Test successful settings loading.""" |
| 77 | + from byte_bot.config import load_settings |
| 78 | + |
| 79 | + monkeypatch.setenv("DISCORD_TOKEN", "test-token") |
| 80 | + |
| 81 | + # Reimport to avoid cached module-level settings |
| 82 | + from importlib import reload |
| 83 | + |
| 84 | + import byte_bot.config |
| 85 | + |
| 86 | + reload(byte_bot.config) |
| 87 | + from byte_bot.config import BotSettings, LogSettings |
| 88 | + |
| 89 | + bot_settings, log_settings = load_settings() |
| 90 | + |
| 91 | + assert isinstance(bot_settings, BotSettings) |
| 92 | + assert isinstance(log_settings, LogSettings) |
| 93 | + assert bot_settings.discord_token |
| 94 | + |
| 95 | + def test_load_settings_returns_tuple(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 96 | + """Test that load_settings returns a tuple of settings.""" |
| 97 | + from byte_bot.config import load_settings |
| 98 | + |
| 99 | + monkeypatch.setenv("DISCORD_TOKEN", "test-token") |
| 100 | + |
| 101 | + from importlib import reload |
| 102 | + |
| 103 | + import byte_bot.config |
| 104 | + |
| 105 | + reload(byte_bot.config) |
| 106 | + from byte_bot.config import BotSettings, LogSettings |
| 107 | + |
| 108 | + bot_settings, log_settings = load_settings() |
| 109 | + |
| 110 | + assert isinstance(bot_settings, BotSettings) |
| 111 | + assert isinstance(log_settings, LogSettings) |
0 commit comments