diff --git a/pyrit/converter/audio_echo_converter.py b/pyrit/converter/audio_echo_converter.py index a96c4d87c9..b2f86f3a16 100644 --- a/pyrit/converter/audio_echo_converter.py +++ b/pyrit/converter/audio_echo_converter.py @@ -3,6 +3,7 @@ import io import logging +import math from typing import Any, Literal import numpy as np @@ -43,19 +44,19 @@ def __init__( Args: output_format (str): The format of the audio file, defaults to "wav". - delay (float): The echo delay in seconds. Must be greater than 0. Defaults to 0.3. + delay (float): The echo delay in seconds. Must be finite and greater than 0. Defaults to 0.3. decay (float): The decay factor for the echo (0.0 to 1.0). A value of 0.0 means no echo, 1.0 means the echo is as loud as - the original. Must be between 0 and 1 (exclusive of both). + the original. Must be finite and between 0 and 1 (exclusive of both). Defaults to 0.5. Raises: - ValueError: If delay is not positive or decay is not in (0, 1). + ValueError: If delay is not finite and positive, or decay is not finite and in (0, 1). """ - if delay <= 0: - raise ValueError("delay must be greater than 0.") - if decay <= 0 or decay >= 1: - raise ValueError("decay must be between 0 and 1 (exclusive).") + if not math.isfinite(delay) or delay <= 0: + raise ValueError("delay must be a finite number greater than 0.") + if not math.isfinite(decay) or decay <= 0 or decay >= 1: + raise ValueError("decay must be a finite number between 0 and 1 (exclusive).") self._output_format = output_format self._delay = delay self._decay = decay diff --git a/tests/unit/converter/test_audio_echo_converter.py b/tests/unit/converter/test_audio_echo_converter.py index 3235782ff9..de1298bb85 100644 --- a/tests/unit/converter/test_audio_echo_converter.py +++ b/tests/unit/converter/test_audio_echo_converter.py @@ -96,34 +96,46 @@ async def test_echo_file_not_found(): def test_echo_invalid_delay_zero(): """delay of 0 should raise ValueError.""" - with pytest.raises(ValueError, match="delay must be greater than 0"): + with pytest.raises(ValueError, match="delay must be a finite number greater than 0"): AudioEchoConverter(delay=0, decay=0.5) def test_echo_invalid_delay_negative(): """Negative delay should raise ValueError.""" - with pytest.raises(ValueError, match="delay must be greater than 0"): + with pytest.raises(ValueError, match="delay must be a finite number greater than 0"): AudioEchoConverter(delay=-0.5, decay=0.5) +@pytest.mark.parametrize("delay", [float("nan"), float("inf"), float("-inf")]) +def test_echo_invalid_delay_non_finite(delay: float): + with pytest.raises(ValueError, match="delay must be a finite number greater than 0"): + AudioEchoConverter(delay=delay, decay=0.5) + + def test_echo_invalid_decay_zero(): """decay of 0 should raise ValueError.""" - with pytest.raises(ValueError, match="decay must be between 0 and 1"): + with pytest.raises(ValueError, match="decay must be a finite number between 0 and 1"): AudioEchoConverter(delay=0.3, decay=0) def test_echo_invalid_decay_one(): """decay of 1 should raise ValueError.""" - with pytest.raises(ValueError, match="decay must be between 0 and 1"): + with pytest.raises(ValueError, match="decay must be a finite number between 0 and 1"): AudioEchoConverter(delay=0.3, decay=1.0) def test_echo_invalid_decay_above_one(): """decay > 1 should raise ValueError.""" - with pytest.raises(ValueError, match="decay must be between 0 and 1"): + with pytest.raises(ValueError, match="decay must be a finite number between 0 and 1"): AudioEchoConverter(delay=0.3, decay=1.5) +@pytest.mark.parametrize("decay", [float("nan"), float("inf"), float("-inf")]) +def test_echo_invalid_decay_non_finite(decay: float): + with pytest.raises(ValueError, match="decay must be a finite number between 0 and 1"): + AudioEchoConverter(delay=0.3, decay=decay) + + async def test_echo_unsupported_input_type(sqlite_instance): """Passing an unsupported input_type should raise ValueError.""" converter = AudioEchoConverter(delay=0.3, decay=0.5)