"""bgm_utils 单元测试."""

from __future__ import annotations

from domain.bgm_utils import merge_bgm_config


class TestMergeBgmConfigEmptyInputs:
    """空输入测试."""

    def test_both_empty(self):
        result = merge_bgm_config({}, {})
        assert result == {}

    def test_user_empty_returns_template_copy(self):
        template = {"enabled": True, "volume": 0.5}
        result = merge_bgm_config(template, {})
        assert result == {"enabled": True, "volume": 0.5}
        # 返回的是副本不是同一个对象
        assert result is not template

    def test_template_empty_returns_user_copy(self):
        user = {"enabled": False, "volume": 0.8}
        result = merge_bgm_config({}, user)
        assert result == {"enabled": False, "volume": 0.8}
        assert result is not user

    def test_user_none_returns_template(self):
        template = {"enabled": True}
        result = merge_bgm_config(template, None)  # type: ignore[arg-type]
        assert result == template

    def test_template_none_returns_user(self):
        user = {"enabled": True}
        result = merge_bgm_config(None, user)  # type: ignore[arg-type]
        assert result == user


class TestMergeBgmConfigBasicMerge:
    """基础合并测试."""

    def test_user_overrides_template_field(self):
        template = {"volume": 0.5, "fade_in": 1.0}
        user = {"volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["volume"] == 0.8
        assert result["fade_in"] == 1.0

    def test_user_adds_new_field(self):
        template = {"volume": 0.5}
        user = {"fade_out": 2.0}
        result = merge_bgm_config(template, user)
        assert result["volume"] == 0.5
        assert result["fade_out"] == 2.0

    def test_all_fields_overridden(self):
        template = {"enabled": True, "volume": 0.5, "track_id": "t1"}
        user = {"enabled": False, "volume": 1.0, "track_id": "t2"}
        result = merge_bgm_config(template, user)
        assert result == {"enabled": False, "volume": 1.0, "track_id": "t2"}


class TestMergeBgmConfigEnabledSpecial:
    """enabled 特殊处理测试."""

    def test_user_no_enabled_keeps_template_enabled_true(self):
        template = {"enabled": True, "volume": 0.5}
        user = {"volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is True
        assert result["volume"] == 0.8

    def test_user_no_enabled_keeps_template_enabled_false(self):
        template = {"enabled": False, "volume": 0.5}
        user = {"volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is False
        assert result["volume"] == 0.8

    def test_user_explicit_enabled_true_overrides_template_false(self):
        template = {"enabled": False, "volume": 0.5}
        user = {"enabled": True, "volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is True
        assert result["volume"] == 0.8

    def test_user_explicit_enabled_false_overrides_template_true(self):
        template = {"enabled": True, "volume": 0.5}
        user = {"enabled": False}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is False

    def test_template_no_enabled_user_no_enabled(self):
        template = {"volume": 0.5}
        user = {"volume": 0.8}
        result = merge_bgm_config(template, user)
        assert "enabled" not in result
        assert result["volume"] == 0.8

    def test_template_no_enabled_user_has_enabled(self):
        template = {"volume": 0.5}
        user = {"enabled": True, "volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is True


class TestMergeBgmConfigDoesNotMutate:
    """不修改原字典测试."""

    def test_template_not_mutated(self):
        template = {"enabled": True, "volume": 0.5}
        original = dict(template)
        user = {"volume": 0.8, "fade": 1.0}
        merge_bgm_config(template, user)
        assert template == original

    def test_user_not_mutated(self):
        template = {"enabled": True, "volume": 0.5}
        user = {"volume": 0.8}
        original = dict(user)
        merge_bgm_config(template, user)
        assert user == original


class TestMergeBgmConfigNestedDict:
    """嵌套字典合并测试（简单合并，非深合并）."""

    def test_nested_dict_user_overrides(self):
        template = {"effects": {"fade_in": 1.0, "fade_out": 1.0}}
        user = {"effects": {"fade_in": 2.0}}
        result = merge_bgm_config(template, user)
        # 简单合并，用户effects整个覆盖模板的
        assert result["effects"] == {"fade_in": 2.0}

    def test_nested_dict_preserved_when_no_user_override(self):
        template = {"effects": {"fade_in": 1.0}}
        user = {"volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["effects"] == {"fade_in": 1.0}
