import os
import json
import logging
from types import SimpleNamespace

from pyucc.utils import logger as ulogger


def test_apply_saved_logger_levels(tmp_path, monkeypatch):
    # Create a fake ConfigManager that points to tmp_path
    class FakeCfg:
        def __init__(self, filepath):
            self.filepath = str(filepath)
        def get_general_settings(self):
            return {}

    cfgdir = tmp_path / "cfgdir"
    cfgdir.mkdir()
    prefs = {"saved_levels": {"pyucc.test": "DEBUG"}}
    prefs_path = cfgdir / "logger_prefs.json"
    prefs_path.write_text(json.dumps(prefs))

    def fake_config_manager():
        # return an object whose filepath is the directory containing prefs
        obj = SimpleNamespace()
        obj.filepath = str(prefs_path)
        def get_general_settings():
            return {}
        obj.get_general_settings = get_general_settings
        return obj

    monkeypatch.setattr(ulogger, 'ConfigManager', lambda: fake_config_manager())
    # ensure file exists next to filepath
    # call apply_saved_logger_levels should not raise
    ulogger.apply_saved_logger_levels()
    # logger level may be set
    lvl = logging.getLogger('pyucc.test').level
    assert isinstance(lvl, int)
