"""Tests for log introspection — describe_logging() (Wave 1)."""

from __future__ import annotations

import logging

from mgf.common.observability import LogInfo, describe_logging


class TestDescribeLogging:
    def test_returns_loginfo_with_locations(self) -> None:
        info = describe_logging()
        assert isinstance(info, LogInfo)
        # The "where do my logs live?" answer.
        assert set(info.locations) == {"log_file", "crash_dir", "state_dir"}
        assert info.locations["log_file"].endswith(".log")

    def test_reports_root_level(self) -> None:
        info = describe_logging()
        assert info.root_level == logging.getLevelName(logging.getLogger().getEffectiveLevel())
        assert isinstance(info.root_level_no, int)

    def test_picks_up_component_level_override(self) -> None:
        lg = logging.getLogger("test_introspect.widget")
        prev = lg.level
        lg.setLevel(logging.DEBUG)
        try:
            info = describe_logging()
            assert info.component_levels.get("test_introspect.widget") == "DEBUG"
        finally:
            lg.setLevel(prev)

    def test_reports_active_handlers(self) -> None:
        root = logging.getLogger()
        h = logging.StreamHandler()
        h.setLevel(logging.WARNING)
        root.addHandler(h)
        try:
            info = describe_logging()
            kinds = [hi.kind for hi in info.handlers]
            assert "StreamHandler" in kinds
        finally:
            root.removeHandler(h)

    def test_to_dict_shape(self) -> None:
        d = describe_logging().to_dict()
        for key in (
            "app",
            "root_level",
            "handlers",
            "component_levels",
            "locations",
            "schema_version",
        ):
            assert key in d
        assert isinstance(d["handlers"], list)
