diff --git a/tests/infrastructure/test_error_capture.py b/tests/infrastructure/test_error_capture.py index 7b31dfc..ddf369c 100644 --- a/tests/infrastructure/test_error_capture.py +++ b/tests/infrastructure/test_error_capture.py @@ -1,6 +1,8 @@ """Tests for infrastructure.error_capture module.""" import sqlite3 +from datetime import UTC, datetime, timedelta +from unittest.mock import patch from infrastructure.error_capture import ( _dedup_cache, @@ -67,6 +69,21 @@ class TestIsDuplicate: _is_duplicate("hash_1") assert _is_duplicate("hash_2") is False + def test_stale_entries_pruned(self): + """Old entries beyond 2x the dedup window should be pruned.""" + from config import settings + + window = settings.error_dedup_window_seconds + # Seed a stale entry far in the past + stale_time = datetime.now(UTC) - timedelta(seconds=window * 3) + _dedup_cache["stale_hash"] = stale_time + + # Calling _is_duplicate on a new hash triggers pruning + _is_duplicate("fresh_hash") + + assert "stale_hash" not in _dedup_cache + assert "fresh_hash" in _dedup_cache + def teardown_method(self): _dedup_cache.clear() @@ -82,6 +99,12 @@ class TestGetGitContext: assert isinstance(ctx["branch"], str) assert isinstance(ctx["commit"], str) + def test_git_context_fallback_on_failure(self): + """When subprocess.run fails, returns 'unknown' for both fields.""" + with patch("subprocess.run", side_effect=OSError("git not found")): + ctx = _get_git_context() + assert ctx == {"branch": "unknown", "commit": "unknown"} + class TestCaptureError: """Test the main capture_error function.""" @@ -100,6 +123,23 @@ class TestCaptureError: result = capture_error(e, source="test") assert result is None + def test_returns_none_when_feedback_disabled(self): + """capture_error returns None immediately when error_feedback_enabled is False.""" + _dedup_cache.clear() + + import config + + original = config.settings.error_feedback_enabled + try: + config.settings.error_feedback_enabled = False + try: + raise RuntimeError("disabled test") + except RuntimeError as e: + result = capture_error(e, source="test") + assert result is None + finally: + config.settings.error_feedback_enabled = original + def test_capture_does_not_crash_on_missing_deps(self): """capture_error should never crash even if optional deps are missing.""" _dedup_cache.clear()