"""Tests for the configuration management.""" import logging from typing import Any, Dict import pytest from hasskiosk import __version__ from hasskiosk.config import configure_logging, read_config @pytest.fixture(autouse=True) def mock_env(monkeypatch): """Environment mock to test values.""" monkeypatch.setenv("HASSKIOSK_TOPIC_PRESENCE", "home/test/presence") monkeypatch.setenv("HASSKIOSK_MQTT_HOST", "ha.example.com") monkeypatch.setenv("HASSKIOSK_MQTT_USERNAME", "testymctesterson") monkeypatch.setenv("HASSKIOSK_MQTT_PASSWORD", "hunter2") @pytest.fixture def logging_config() -> Dict[str, Any]: """logging configuration fixture.""" config = { "version": 1, "disable_existing_loggers": False, "formatters": { "simple": { "format": "%(asctime)s %(levelname)s (%(name)s) %(message)s", "converter": "time.gmtime", }, }, "handlers": { "stdout": { "class": "logging.StreamHandler", "level": "DEBUG", "formatter": "simple", "stream": "ext://sys.stdout", }, }, "loggers": { "hasskiosk": { "level": "DEBUG", "propagate": "yes", }, }, "root": { "level": "WARN", "handlers": ["stdout"], }, } return config def test_read_config(): """Test the read_config function.""" config = read_config() assert config["version"] == __version__ assert config["sysname"] == "hasskiosk" assert config["mqtt"] == { "host": "ha.example.com", "username": "testymctesterson", "password": "hunter2", "port": 1883, "keepalive": 60, } def test_configure_logging(logging_config): """Test the logging configuration.""" config = { "version": "1.2.3.test", "sysname": "testkiosk", "logging": logging_config, } configure_logging(config) log = logging.getLogger(__name__) rootlog = log while rootlog.parent: rootlog = rootlog.parent assert len(rootlog.handlers) == 1