forked from Rockachopa/Timmy-time-dashboard
test: push event system coverage to ≥80% on all three modules
Add 3 targeted tests for infrastructure/error_capture.py: - test_stale_entries_pruned: exercises dedup cache pruning (line 61) - test_git_context_fallback_on_failure: exercises exception path (lines 90-91) - test_returns_none_when_feedback_disabled: exercises early return (line 112) Coverage results (63 tests, all passing): - error_capture.py: 75.6% → 80.0% - broadcaster.py: 93.9% (unchanged) - bus.py: 92.9% (unchanged) - Total: 88.1% → 89.4% Closes #45
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user