"""BGM工具函数领域层单元测试."""

import pytest

from packages.domain.bgm_utils import merge_bgm_config


class TestMergeBgmConfig:
    """merge_bgm_config 函数测试."""

    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 == template
        # 确保是副本不是引用
        result["volume"] = 0.9
        assert template["volume"] == 0.5

    def test_template_empty_returns_user_copy(self):
        """模板配置为空，返回用户配置的拷贝."""
        user = {"enabled": False, "volume": 0.8}
        result = merge_bgm_config({}, user)
        assert result == user
        # 确保是副本
        result["volume"] = 0.1
        assert user["volume"] == 0.8

    def test_user_overrides_template(self):
        """用户配置覆盖模板配置."""
        template = {"enabled": True, "volume": 0.5, "track": "default"}
        user = {"volume": 0.8}
        result = merge_bgm_config(template, user)
        assert result["volume"] == 0.8
        assert result["track"] == "default"

    def test_enabled_special_handling_user_not_set(self):
        """enabled 特殊处理：用户没传就保留模板的."""
        template = {"enabled": True, "volume": 0.5}
        user = {"volume": 0.8}  # 没传 enabled
        result = merge_bgm_config(template, user)
        # 用户没传 enabled，保留模板的 True
        assert result["enabled"] is True
        assert result["volume"] == 0.8

    def test_enabled_user_explicit_false(self):
        """用户显式传 enabled=False，应该覆盖模板."""
        template = {"enabled": True, "volume": 0.5}
        user = {"enabled": False}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is False

    def test_enabled_user_explicit_true(self):
        """用户显式传 enabled=True，覆盖模板的 False."""
        template = {"enabled": False, "volume": 0.5}
        user = {"enabled": True}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is True

    def test_full_override(self):
        """用户完全覆盖模板."""
        template = {"enabled": True, "volume": 0.3, "track": "piano"}
        user = {"enabled": False, "volume": 0.9, "track": "guitar"}
        result = merge_bgm_config(template, user)
        assert result == user

    def test_partial_override_keep_rest(self):
        """部分覆盖，其余保留模板值."""
        template = {
            "enabled": True,
            "volume": 0.5,
            "fade_in": 1.0,
            "fade_out": 1.0,
            "track": "default",
        }
        user = {"volume": 0.7, "fade_in": 2.0}
        result = merge_bgm_config(template, user)
        assert result["volume"] == 0.7
        assert result["fade_in"] == 2.0
        assert result["fade_out"] == 1.0
        assert result["track"] == "default"
        assert result["enabled"] is True  # 用户没传，保留模板

    def test_user_none(self):
        """user_bgm 为 None 的情况."""
        template = {"enabled": True, "volume": 0.5}
        result = merge_bgm_config(template, None)  # type: ignore
        assert result == template

    def test_template_none(self):
        """template_bgm 为 None 的情况."""
        user = {"enabled": False, "volume": 0.8}
        result = merge_bgm_config(None, user)  # type: ignore
        assert result == user

    def test_preserves_extra_fields(self):
        """保留模板中的额外字段（用户没覆盖的）."""
        template = {"enabled": True, "volume": 0.5, "custom_field": "value"}
        user = {"volume": 0.6}
        result = merge_bgm_config(template, user)
        assert result["custom_field"] == "value"

    def test_user_adds_new_fields(self):
        """用户可以添加模板中没有的新字段."""
        template = {"enabled": True, "volume": 0.5}
        user = {"loop": True, "start_time": 5.0}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is True
        assert result["volume"] == 0.5
        assert result["loop"] is True
        assert result["start_time"] == 5.0

    def test_nested_dict_behavior(self):
        """嵌套字典的合并行为（简单替换，不深度合并）."""
        template = {"enabled": True, "effects": {"fade": True, "reverb": False}}
        user = {"effects": {"reverb": True}}
        result = merge_bgm_config(template, user)
        # 简单合并，用户的 effects 整体覆盖模板的
        assert result["effects"] == {"reverb": True}

    def test_enabled_in_template_only(self):
        """只有模板有 enabled，用户没有."""
        template = {"enabled": False, "volume": 0.5}
        user = {"volume": 0.7}
        result = merge_bgm_config(template, user)
        # 用户没传 enabled，保留模板的 False
        assert result["enabled"] is False

    def test_both_have_enabled_false(self):
        """两边都有 enabled 且都是 False."""
        template = {"enabled": False, "volume": 0.5}
        user = {"enabled": False}
        result = merge_bgm_config(template, user)
        assert result["enabled"] is False

    def test_return_type_is_dict(self):
        """返回类型是 dict."""
        result = merge_bgm_config({"a": 1}, {"b": 2})
        assert isinstance(result, dict)

    def test_does_not_mutate_template(self):
        """不修改原始模板字典."""
        template = {"enabled": True, "volume": 0.5}
        template_copy = template.copy()
        user = {"volume": 0.9}
        merge_bgm_config(template, user)
        assert template == template_copy

    def test_does_not_mutate_user(self):
        """不修改原始用户字典."""
        template = {"enabled": True, "volume": 0.5}
        user = {"volume": 0.9}
        user_copy = user.copy()
        merge_bgm_config(template, user)
        assert user == user_copy
