"""降噪引擎单元测试 - 配置解析等纯逻辑.""" from __future__ import annotations import pytest from video_processing.noise_reduction_engine import ( NoiseReductionConfig, NoiseReductionLevel, ) class TestNoiseReductionLevel: """降噪等级枚举测试.""" def test_level_values(self): """等级枚举值正确.""" assert NoiseReductionLevel.LOW.value == "low" assert NoiseReductionLevel.MEDIUM.value == "medium" assert NoiseReductionLevel.HIGH.value == "high" assert NoiseReductionLevel.CUSTOM.value == "custom" def test_from_string(self): """从字符串创建.""" assert NoiseReductionLevel("low") == NoiseReductionLevel.LOW assert NoiseReductionLevel("medium") == NoiseReductionLevel.MEDIUM assert NoiseReductionLevel("high") == NoiseReductionLevel.HIGH assert NoiseReductionLevel("custom") == NoiseReductionLevel.CUSTOM def test_invalid_string_raises(self): """无效字符串抛异常.""" with pytest.raises(ValueError): NoiseReductionLevel("invalid") class TestNoiseReductionConfigDefaults: """默认配置测试.""" def test_default_values(self): """默认值正确.""" config = NoiseReductionConfig() assert config.enabled is False assert config.level == NoiseReductionLevel.MEDIUM assert config.noise_floor == -25.0 assert config.voice_enhance is False class TestNoiseReductionConfigFromDict: """from_dict 配置解析测试.""" def test_none_returns_disabled(self): """None 返回禁用配置.""" config = NoiseReductionConfig.from_dict(None) assert config.enabled is False def test_empty_dict_returns_disabled(self): """空字典返回禁用配置.""" config = NoiseReductionConfig.from_dict({}) assert config.enabled is False def test_disabled_returns_disabled(self): """enabled=False 返回禁用.""" config = NoiseReductionConfig.from_dict({"enabled": False}) assert config.enabled is False def test_enabled_default_level(self): """启用时默认等级为 medium.""" config = NoiseReductionConfig.from_dict({"enabled": True}) assert config.enabled is True assert config.level == NoiseReductionLevel.MEDIUM def test_level_low(self): """low 等级.""" config = NoiseReductionConfig.from_dict({"enabled": True, "level": "low"}) assert config.level == NoiseReductionLevel.LOW def test_level_high(self): """high 等级.""" config = NoiseReductionConfig.from_dict({"enabled": True, "level": "high"}) assert config.level == NoiseReductionLevel.HIGH def test_level_custom(self): """custom 等级.""" config = NoiseReductionConfig.from_dict({"enabled": True, "level": "custom"}) assert config.level == NoiseReductionLevel.CUSTOM def test_level_case_insensitive(self): """等级大小写不敏感.""" config = NoiseReductionConfig.from_dict({"enabled": True, "level": "HIGH"}) assert config.level == NoiseReductionLevel.HIGH def test_invalid_level_falls_back_to_medium(self): """无效等级 fallback 到 medium.""" config = NoiseReductionConfig.from_dict({"enabled": True, "level": "ultra"}) assert config.level == NoiseReductionLevel.MEDIUM def test_noise_floor_parsed(self): """噪音阈值解析.""" config = NoiseReductionConfig.from_dict( { "enabled": True, "level": "custom", "noise_floor": -30.0, } ) assert config.noise_floor == -30.0 def test_noise_floor_clamped_min(self): """噪音阈值下限钳制 (-60).""" config = NoiseReductionConfig.from_dict( { "enabled": True, "level": "custom", "noise_floor": -100.0, } ) assert config.noise_floor == -60.0 def test_noise_floor_clamped_max(self): """噪音阈值上限钳制 (-5).""" config = NoiseReductionConfig.from_dict( { "enabled": True, "level": "custom", "noise_floor": 0.0, } ) assert config.noise_floor == -5.0 def test_noise_floor_boundary_low(self): """噪音阈值边界值 -60.""" config = NoiseReductionConfig.from_dict( { "enabled": True, "level": "custom", "noise_floor": -60.0, } ) assert config.noise_floor == -60.0 def test_noise_floor_boundary_high(self): """噪音阈值边界值 -5.""" config = NoiseReductionConfig.from_dict( { "enabled": True, "level": "custom", "noise_floor": -5.0, } ) assert config.noise_floor == -5.0 def test_invalid_noise_floor_falls_back(self): """无效噪音阈值 fallback 到默认值.""" config = NoiseReductionConfig.from_dict( { "enabled": True, "level": "custom", "noise_floor": "not_a_number", } ) assert config.noise_floor == -25.0 def test_voice_enhance_enabled(self): """人声增强启用.""" config = NoiseReductionConfig.from_dict( { "enabled": True, "voice_enhance": True, } ) assert config.voice_enhance is True def test_voice_enhance_disabled_default(self): """人声增强默认禁用.""" config = NoiseReductionConfig.from_dict({"enabled": True}) assert config.voice_enhance is False class TestHasEffect: """has_effect 方法测试.""" def test_disabled_no_effect(self): """禁用时无效果.""" config = NoiseReductionConfig(enabled=False) assert config.has_effect() is False def test_enabled_has_effect(self): """启用时有效果.""" config = NoiseReductionConfig(enabled=True) assert config.has_effect() is True class TestGetEffectiveNoiseFloor: """get_effective_noise_floor 方法测试.""" def test_custom_level_returns_noise_floor(self): """custom 等级返回配置的 noise_floor.""" config = NoiseReductionConfig( enabled=True, level=NoiseReductionLevel.CUSTOM, noise_floor=-35.0, ) assert config.get_effective_noise_floor() == -35.0 def test_low_level_returns_params(self): """low 等级返回对应参数值.""" config = NoiseReductionConfig( enabled=True, level=NoiseReductionLevel.LOW, ) result = config.get_effective_noise_floor() assert isinstance(result, float) assert result < 0 # dB值为负数 def test_medium_level_returns_params(self): """medium 等级返回对应参数值.""" config = NoiseReductionConfig( enabled=True, level=NoiseReductionLevel.MEDIUM, ) result = config.get_effective_noise_floor() assert isinstance(result, float) assert result < 0 def test_high_level_returns_params(self): """high 等级返回对应参数值.""" config = NoiseReductionConfig( enabled=True, level=NoiseReductionLevel.HIGH, ) result = config.get_effective_noise_floor() assert isinstance(result, float) assert result < 0