Download tests/test_error_handler.py from HonzaH/pruvodce: direct link, hf CLI and curl.
- Browser
- Download file 8.32 kB
-
https://huggingface.co/spaces/HonzaH/pruvodce/resolve/main/tests/test_error_handler.py
- Command line
-
hf download hf://spaces/HonzaH/pruvodce/tests/test_error_handler.py
-
curl -L -o test_error_handler.py https://huggingface.co/spaces/HonzaH/pruvodce/resolve/main/tests/test_error_handler.py
8.32 kB
| """ | |
| Tests for ErrorHandler utility module | |
| """ | |
| import pytest | |
| import asyncio | |
| from unittest.mock import Mock, patch | |
| from backend.mcp.orchestrator.error_handler import ErrorHandler, CircuitBreaker | |
| class TestErrorHandler: | |
| """Test ErrorHandler class""" | |
| def test_handle_llm_error_basic(self): | |
| """Test basic error handling with localized fallback""" | |
| exception = Exception("Test error") | |
| agent_name = "coach" | |
| provider = Mock() | |
| provider.model = "test-model" | |
| result = ErrorHandler.handle_llm_error(exception, agent_name, provider, locale='cs') | |
| assert isinstance(result, str) | |
| assert len(result) > 0 # Should return fallback message | |
| def test_handle_llm_error_with_none_provider(self): | |
| """Test error handling when provider is None""" | |
| exception = Exception("Test error") | |
| agent_name = "planner" | |
| result = ErrorHandler.handle_llm_error(exception, agent_name, None, locale='cs') | |
| assert isinstance(result, str) | |
| assert len(result) > 0 | |
| def test_handle_llm_error_different_locales(self): | |
| """Test error handling with different locales""" | |
| exception = Exception("Test error") | |
| agent_name = "guardian" | |
| provider = Mock() | |
| result_cs = ErrorHandler.handle_llm_error(exception, agent_name, provider, locale='cs') | |
| result_en = ErrorHandler.handle_llm_error(exception, agent_name, provider, locale='en') | |
| assert isinstance(result_cs, str) | |
| assert isinstance(result_en, str) | |
| # Both should return valid strings (may be same fallback) | |
| assert len(result_cs) > 0 | |
| assert len(result_en) > 0 | |
| def test_log_error_verbose_enabled(self, capsys): | |
| """Test verbose error logging when enabled""" | |
| exception = ValueError("Test verbose error") | |
| context = {'agent': 'coach', 'provider': 'test-provider', 'operation': 'test_op'} | |
| ErrorHandler.log_error_verbose(exception, context) | |
| captured = capsys.readouterr() | |
| assert 'ERROR DETAILS' in captured.out | |
| assert 'AGENT: coach' in captured.out | |
| assert 'PROVIDER: test-provider' in captured.out | |
| def test_log_error_verbose_disabled(self, capsys): | |
| """Test verbose error logging when disabled""" | |
| exception = ValueError("Test error") | |
| context = {'agent': 'coach'} | |
| ErrorHandler.log_error_verbose(exception, context) | |
| captured = capsys.readouterr() | |
| assert captured.out == '' # No output when disabled | |
| async def test_retry_on_failure_success_first_try(self): | |
| """Test retry decorator with successful first attempt""" | |
| call_count = 0 | |
| async def successful_function(): | |
| nonlocal call_count | |
| call_count += 1 | |
| return "success" | |
| result = await successful_function() | |
| assert result == "success" | |
| assert call_count == 1 # Should succeed on first try | |
| async def test_retry_on_failure_success_after_retries(self): | |
| """Test retry decorator succeeding after failures""" | |
| call_count = 0 | |
| async def flaky_function(): | |
| nonlocal call_count | |
| call_count += 1 | |
| if call_count < 3: | |
| raise ValueError("Temporary failure") | |
| return "success" | |
| result = await flaky_function() | |
| assert result == "success" | |
| assert call_count == 3 # Should succeed on third try | |
| async def test_retry_on_failure_exhausted(self): | |
| """Test retry decorator when all retries exhausted""" | |
| call_count = 0 | |
| async def always_failing_function(): | |
| nonlocal call_count | |
| call_count += 1 | |
| raise ValueError("Always fails") | |
| with pytest.raises(ValueError, match="Always fails"): | |
| await always_failing_function() | |
| assert call_count == 3 # Initial + 2 retries | |
| async def test_retry_on_failure_exponential_backoff(self): | |
| """Test retry decorator uses exponential backoff""" | |
| call_times = [] | |
| async def timing_function(): | |
| import time | |
| call_times.append(time.time()) | |
| if len(call_times) < 3: | |
| raise ValueError("Fail") | |
| return "success" | |
| await timing_function() | |
| # Check that delays increase exponentially | |
| assert len(call_times) == 3 | |
| delay1 = call_times[1] - call_times[0] | |
| delay2 = call_times[2] - call_times[1] | |
| # Second delay should be roughly 2x first delay (allowing for timing variance) | |
| assert delay2 > delay1 * 1.5 | |
| class TestCircuitBreaker: | |
| """Test CircuitBreaker class""" | |
| def test_circuit_breaker_starts_closed(self): | |
| """Test circuit breaker starts in closed state""" | |
| breaker = CircuitBreaker(failure_threshold=3, timeout=1.0) | |
| assert breaker.state == "closed" | |
| assert not breaker.is_open() | |
| def test_circuit_breaker_opens_after_threshold(self): | |
| """Test circuit opens after failure threshold""" | |
| breaker = CircuitBreaker(failure_threshold=3, timeout=1.0) | |
| # Record failures | |
| breaker.record_failure() | |
| assert not breaker.is_open() # Still closed | |
| breaker.record_failure() | |
| assert not breaker.is_open() # Still closed | |
| breaker.record_failure() | |
| assert breaker.is_open() # Should open now | |
| def test_circuit_breaker_closes_after_success(self): | |
| """Test circuit closes after successful call in half-open state""" | |
| breaker = CircuitBreaker(failure_threshold=2, timeout=0.1) | |
| # Open the circuit | |
| breaker.record_failure() | |
| breaker.record_failure() | |
| assert breaker.is_open() | |
| # Wait for timeout | |
| import time | |
| time.sleep(0.15) | |
| # Should transition to half-open | |
| assert not breaker.is_open() | |
| # Record success - should close | |
| breaker.record_success() | |
| assert breaker.state == "closed" | |
| async def test_circuit_breaker_decorator_blocks_when_open(self): | |
| """Test circuit breaker decorator blocks calls when open""" | |
| breaker = CircuitBreaker(failure_threshold=2, timeout=1.0, name="test") | |
| async def test_function(): | |
| raise ValueError("Test error") | |
| # Trigger failures to open circuit | |
| with pytest.raises(ValueError): | |
| await test_function() | |
| with pytest.raises(ValueError): | |
| await test_function() | |
| assert breaker.is_open() | |
| # Next call should be blocked | |
| with pytest.raises(Exception, match="Circuit breaker test is open"): | |
| await test_function() | |
| async def test_circuit_breaker_decorator_allows_after_timeout(self): | |
| """Test circuit breaker allows calls after timeout""" | |
| breaker = CircuitBreaker(failure_threshold=2, timeout=0.1, name="test") | |
| call_count = 0 | |
| async def test_function(): | |
| nonlocal call_count | |
| call_count += 1 | |
| if call_count < 3: | |
| raise ValueError("Fail") | |
| return "success" | |
| # Open circuit | |
| with pytest.raises(ValueError): | |
| await test_function() | |
| with pytest.raises(ValueError): | |
| await test_function() | |
| assert breaker.is_open() | |
| # Wait for timeout | |
| import time | |
| time.sleep(0.15) | |
| # Should allow call now | |
| result = await test_function() | |
| assert result == "success" | |
| assert breaker.state == "closed" | |