"""Tests for config/logging_config.py.""" import json import logging from pathlib import Path from config.logging_config import configure_logging def test_configure_logging_writes_json_to_file(tmp_path): """configure_logging writes JSON lines to the specified file.""" log_file = str(tmp_path / "test.log") configure_logging(log_file, force=True) # Emit a log via stdlib (intercepted to loguru) logger = logging.getLogger("test.module") logger.info("Test message for JSON") # Force flush - loguru may buffer from loguru import logger as loguru_logger loguru_logger.complete() content = Path(log_file).read_text(encoding="utf-8") lines = [line for line in content.strip().split("\n") if line] assert len(lines) >= 1 # Each line should be valid JSON for line in lines: record = json.loads(line) assert "text" in record or "message" in record or "record" in record def test_configure_logging_idempotent(tmp_path): """configure_logging is idempotent - safe to call twice with force.""" log_file = str(tmp_path / "test.log") configure_logging(log_file, force=True) configure_logging(log_file, force=True) # Should not raise logger = logging.getLogger("test.idempotent") logger.info("After second configure") def test_configure_logging_skips_when_already_configured(tmp_path): """Without force, second call is a no-op (avoids reconfig on hot reload).""" log_file = str(tmp_path / "test.log") configure_logging(log_file, force=True) # Second call without force - should skip; no exception, log file unchanged configure_logging(str(tmp_path / "other.log"), force=False) # Logs still go to first file logger = logging.getLogger("test.skip") logger.info("Still goes to first file") from loguru import logger as loguru_logger loguru_logger.complete() assert (tmp_path / "test.log").exists() assert "Still goes to first file" in (tmp_path / "test.log").read_text( encoding="utf-8" )