feat(CORE-001D3B): surface alert history degraded state
This commit is contained in:
@@ -5,6 +5,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models.alerts import AlertHistoryLoadError, AlertHistoryRepository
|
||||
from app.models.portfolio import PortfolioConfig
|
||||
from app.services.alerts import AlertService, _normalize_alert_evaluation_input, build_portfolio_alert_context
|
||||
|
||||
@@ -169,6 +170,70 @@ def test_alert_service_escalates_to_critical_and_keeps_history(alert_service: Al
|
||||
assert critical_status.history[0].ltv_ratio == pytest.approx(0.7525, rel=1e-6)
|
||||
|
||||
|
||||
def test_alert_history_repository_raises_on_corrupt_history_file(tmp_path: Path) -> None:
|
||||
history_path = tmp_path / "alert_history.json"
|
||||
history_path.write_text("{not valid json", encoding="utf-8")
|
||||
repository = AlertHistoryRepository(history_path=history_path)
|
||||
|
||||
with pytest.raises(AlertHistoryLoadError, match="Alert history is not valid JSON"):
|
||||
repository.load()
|
||||
|
||||
|
||||
def test_alert_service_marks_history_unavailable_on_corrupt_storage(alert_service: AlertService) -> None:
|
||||
alert_service.repository.history_path.write_text("{not valid json", encoding="utf-8")
|
||||
config = PortfolioConfig(
|
||||
gold_value=215_000.0,
|
||||
entry_price=215.0,
|
||||
loan_amount=151_000.0,
|
||||
ltv_warning=0.70,
|
||||
margin_threshold=0.75,
|
||||
)
|
||||
portfolio = build_portfolio_alert_context(
|
||||
config,
|
||||
spot_price=215.0,
|
||||
source="test",
|
||||
updated_at="2026-03-24T12:00:00Z",
|
||||
)
|
||||
|
||||
status = alert_service.evaluate(config, portfolio)
|
||||
|
||||
assert status.severity == "warning"
|
||||
assert status.history == []
|
||||
assert status.history_unavailable is True
|
||||
assert (
|
||||
status.history_notice
|
||||
== "Alert history is temporarily unavailable due to a storage error. New alerts are not being recorded."
|
||||
)
|
||||
assert alert_service.repository.history_path.read_text(encoding="utf-8") == "{not valid json"
|
||||
|
||||
|
||||
def test_alert_service_preview_marks_history_unavailable_on_corrupt_storage(alert_service: AlertService) -> None:
|
||||
alert_service.repository.history_path.write_text("{not valid json", encoding="utf-8")
|
||||
config = PortfolioConfig(
|
||||
gold_value=215_000.0,
|
||||
entry_price=215.0,
|
||||
loan_amount=151_000.0,
|
||||
ltv_warning=0.70,
|
||||
margin_threshold=0.75,
|
||||
)
|
||||
portfolio = build_portfolio_alert_context(
|
||||
config,
|
||||
spot_price=215.0,
|
||||
source="settings-preview",
|
||||
updated_at="",
|
||||
)
|
||||
|
||||
status = alert_service.evaluate(config, portfolio, persist=False)
|
||||
|
||||
assert status.severity == "warning"
|
||||
assert [event.severity for event in status.history] == ["warning"]
|
||||
assert status.history_unavailable is True
|
||||
assert (
|
||||
status.history_notice
|
||||
== "Alert history is temporarily unavailable due to a storage error. New alerts are not being recorded."
|
||||
)
|
||||
|
||||
|
||||
def test_alert_service_preserves_persisted_history_during_ok_evaluation(alert_service: AlertService) -> None:
|
||||
warning_config = PortfolioConfig(
|
||||
gold_value=215_000.0,
|
||||
|
||||
Reference in New Issue
Block a user