# -*- coding: utf-8 -*-
"""配置管理测试"""
import pytest
from config.settings import AppConfig
from config.defaults import DEFAULTS

def test_config_load():
    """测试配置加载"""
    config = AppConfig.load({"app": {"store_id": 1}})
    assert config.get("app.timezone") == DEFAULTS["app"]["timezone"]

def test_config_override():
    """测试配置覆盖"""
    overrides = {
        "app": {"store_id": 12345}
    }
    config = AppConfig.load(overrides)
    assert config.get("app.store_id") == 12345

def test_config_get_nested():
    """测试嵌套配置获取"""
    config = AppConfig.load({"app": {"store_id": 1}})
    assert config.get("db.batch_size") == 1000
    assert config.get("nonexistent.key", "default") == "default"


def test_business_day_start_hour_default():
    """默认值 8 应正常加载"""
    config = AppConfig.load({"app": {"store_id": 1}})
    assert config.get("app.business_day_start_hour") == 8


def test_business_day_start_hour_valid_range():
    """0–23 范围内的整数应正常加载"""
    for h in (0, 12, 23):
        config = AppConfig.load({"app": {"store_id": 1, "business_day_start_hour": h}})
        assert config.get("app.business_day_start_hour") == h


def test_business_day_start_hour_out_of_range():
    """超出 0–23 范围应抛出 SystemExit"""
    with pytest.raises(SystemExit, match="business_day_start_hour"):
        AppConfig.load({"app": {"store_id": 1, "business_day_start_hour": 24}})
    with pytest.raises(SystemExit, match="business_day_start_hour"):
        AppConfig.load({"app": {"store_id": 1, "business_day_start_hour": -1}})


def test_business_day_start_hour_non_int():
    """非整数类型应抛出 SystemExit"""
    with pytest.raises(SystemExit, match="business_day_start_hour"):
        AppConfig.load({"app": {"store_id": 1, "business_day_start_hour": "8"}})
    with pytest.raises(SystemExit, match="business_day_start_hour"):
        AppConfig.load({"app": {"store_id": 1, "business_day_start_hour": 8.0}})
