import io
import logging

from src.log import ColorFormatter, get_logger


def test_color_formatter():
    fmt = "%(levelname)s: %(message)s"
    formatter = ColorFormatter(fmt)
    record = logging.LogRecord(
        name="test", level=logging.INFO, pathname=__file__, lineno=1, msg="hello", args=(), exc_info=None
    )
    formatted = formatter.format(record)
    assert "\x1b[38;5;39m" in formatted
    assert "hello" in formatted


def test_get_logger_dev():
    logger = get_logger(stage="dev", port=12345)
    assert logger.level == logging.DEBUG
    stream = io.StringIO()
    handler = logging.StreamHandler(stream)
    logger.addHandler(handler)
    logger.info("test_dev")
    handler.flush()
    assert "test_dev" in stream.getvalue()
