"""绿幕抠像引擎单元测试 - 配置解析等纯逻辑.""" from __future__ import annotations import pytest from video_processing.chroma_key_engine import ( CHROMA_KEY_PRESETS, ChromaKeyConfig, ) class TestChromaKeyConfigDefaults: """默认配置测试.""" def test_default_values(self): """默认值正确.""" config = ChromaKeyConfig() assert config.enabled is False assert config.key_color == "#00FF00" assert config.similarity == 0.3 assert config.blend == 0.1 assert config.spill_suppress == 0.0 class TestChromaKeyConfigFromDict: """from_dict 配置解析测试.""" def test_none_returns_disabled(self): """None 返回禁用配置.""" config = ChromaKeyConfig.from_dict(None) assert config.enabled is False def test_empty_dict_returns_disabled(self): """空字典返回禁用.""" config = ChromaKeyConfig.from_dict({}) assert config.enabled is False def test_disabled_returns_disabled(self): """enabled=False 返回禁用.""" config = ChromaKeyConfig.from_dict({"enabled": False}) assert config.enabled is False def test_enabled_default_values(self): """启用时使用默认参数.""" config = ChromaKeyConfig.from_dict({"enabled": True}) assert config.enabled is True assert config.key_color == "#00FF00" assert config.similarity == 0.3 assert config.blend == 0.1 assert config.spill_suppress == 0.0 def test_custom_key_color(self): """自定义抠像颜色.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "key_color": "#0000FF", } ) assert config.key_color == "#0000FF" def test_similarity_parsed(self): """相似度解析.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "similarity": 0.5, } ) assert config.similarity == 0.5 def test_similarity_clamped_min(self): """相似度下限钳制.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "similarity": 0.001, } ) assert config.similarity == 0.01 def test_similarity_clamped_max(self): """相似度上限钳制.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "similarity": 2.0, } ) assert config.similarity == 1.0 def test_blend_clamped_min(self): """混合度下限钳制.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "blend": -0.5, } ) assert config.blend == 0.0 def test_blend_clamped_max(self): """混合度上限钳制.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "blend": 1.5, } ) assert config.blend == 1.0 def test_spill_suppress_clamped(self): """溢色抑制钳制.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "spill_suppress": 2.0, } ) assert config.spill_suppress == 1.0 def test_invalid_similarity_falls_back(self): """无效相似度回退到默认.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "similarity": "not_a_number", } ) assert config.similarity == 0.3 def test_invalid_blend_falls_back(self): """无效混合度回退.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "blend": "high", } ) assert config.blend == 0.1 def test_key_color_stripped(self): """颜色值去除首尾空格.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "key_color": " #FF0000 ", } ) assert config.key_color == "#FF0000" def test_all_params_custom(self): """所有参数自定义.""" config = ChromaKeyConfig.from_dict( { "enabled": True, "key_color": "#0000FF", "similarity": 0.45, "blend": 0.15, "spill_suppress": 0.6, } ) assert config.enabled is True assert config.key_color == "#0000FF" assert config.similarity == 0.45 assert config.blend == 0.15 assert config.spill_suppress == 0.6 class TestHasEffect: """has_effect 方法测试.""" def test_disabled_no_effect(self): """禁用时无效果.""" config = ChromaKeyConfig(enabled=False) assert config.has_effect() is False def test_enabled_with_similarity_has_effect(self): """启用且有相似度时有效果.""" config = ChromaKeyConfig(enabled=True, similarity=0.3) assert config.has_effect() is True def test_zero_similarity_no_effect(self): """相似度为0时无效果.""" config = ChromaKeyConfig(enabled=True, similarity=0.0) assert config.has_effect() is False class TestChromaKeyPresets: """预设配置测试.""" def test_five_presets(self): """5个预设.""" assert len(CHROMA_KEY_PRESETS) == 5 def test_preset_names(self): """预设名称正确.""" assert "green_screen" in CHROMA_KEY_PRESETS assert "blue_screen" in CHROMA_KEY_PRESETS assert "red_screen" in CHROMA_KEY_PRESETS assert "precise_green" in CHROMA_KEY_PRESETS assert "soft_green" in CHROMA_KEY_PRESETS def test_presets_have_required_keys(self): """每个预设包含必要字段.""" for name, preset in CHROMA_KEY_PRESETS.items(): assert "key_color" in preset, f"{name} missing key_color" assert "similarity" in preset, f"{name} missing similarity" assert "blend" in preset, f"{name} missing blend" assert "spill_suppress" in preset, f"{name} missing spill_suppress"