"""Unit tests for structured and rotating logging configuration.""" from __future__ import annotations import logging from pathlib import Path import pytest from warehouse_twin.logging_config import ( get_logger, get_simulation_logger, setup_logging, ) def test_logging_directory_creation(tmp_path: Path): """Verify that setup_logging automatically creates the requested log directory.""" test_log_dir = tmp_path / "custom_logs" assert not test_log_dir.exists() setup_logging(log_dir=test_log_dir, force_reinit=True) assert test_log_dir.exists() assert test_log_dir.is_dir() def test_log_files_created_and_written(tmp_path: Path): """Verify application.log, simulation.log, and errors.log creation and message routing.""" test_log_dir = tmp_path / "test_logs" setup_logging( log_dir=test_log_dir, console_level=logging.WARNING, file_level=logging.DEBUG, force_reinit=True, ) app_logger = get_logger("test_module") sim_logger = get_simulation_logger() # Emit various levels of logs app_logger.debug("Test debug message") app_logger.info("Test info message") app_logger.error("Test error message occurred") sim_logger.info("Simulation order released: ORD-1001") # Flush all handlers for handler in logging.getLogger().handlers: handler.flush() for handler in sim_logger.handlers: handler.flush() app_log = test_log_dir / "application.log" err_log = test_log_dir / "errors.log" sim_log = test_log_dir / "simulation.log" assert app_log.exists(), "application.log was not created" assert err_log.exists(), "errors.log was not created" assert sim_log.exists(), "simulation.log was not created" app_content = app_log.read_text(encoding="utf-8") err_content = err_log.read_text(encoding="utf-8") sim_content = sim_log.read_text(encoding="utf-8") # application.log should contain debug, info, and error assert "Test debug message" in app_content assert "Test info message" in app_content assert "Test error message occurred" in app_content # errors.log must ONLY contain the error, not debug or info assert "Test error message occurred" in err_content assert "Test info message" not in err_content assert "Test debug message" not in err_content # simulation.log must contain the simulation event assert "ORD-1001" in sim_content def test_log_file_rotation(tmp_path: Path): """Verify that log files rotate when exceeding the configured max_bytes.""" test_log_dir = tmp_path / "rotating_logs" # Set a very small max_bytes threshold (250 bytes) with 3 backups setup_logging( log_dir=test_log_dir, file_level=logging.INFO, max_bytes=250, backup_count=3, force_reinit=True, ) logger = get_logger("rotator") # Write enough lines to exceed 250 bytes multiple times for i in range(40): logger.info(f"Writing log rotation payload line {i:03d} - padding to exceed byte limit") for handler in logging.getLogger().handlers: handler.flush() app_log = test_log_dir / "application.log" backup_1 = test_log_dir / "application.log.1" assert app_log.exists() assert backup_1.exists(), "Expected rotated backup log file 'application.log.1' to exist"