feat(CORE-001D3A): normalize alerts and settings service boundaries

This commit is contained in:
Bu5hm4nn
2026-03-26 13:10:30 +01:00
parent 91f67cd414
commit bb557009c7
7 changed files with 231 additions and 27 deletions

View File

@@ -1,11 +1,12 @@
from __future__ import annotations
from decimal import Decimal
from pathlib import Path
import pytest
from app.models.portfolio import PortfolioConfig
from app.services.alerts import AlertService, build_portfolio_alert_context
from app.services.alerts import AlertService, _normalize_alert_evaluation_input, build_portfolio_alert_context
@pytest.fixture
@@ -13,6 +14,39 @@ def alert_service(tmp_path: Path) -> AlertService:
return AlertService(history_path=tmp_path / "alert_history.json")
def test_normalize_alert_evaluation_input_coerces_numeric_boundary_values() -> None:
config = PortfolioConfig(gold_value=215_000.0, entry_price=215.0, loan_amount=151_000.0)
normalized = _normalize_alert_evaluation_input(
config,
{
"ltv_ratio": "0.7023",
"spot_price": "215.0",
"quote_updated_at": 123,
},
)
assert normalized.ltv_ratio == Decimal("0.7023")
assert normalized.spot_price == Decimal("215.0")
assert normalized.updated_at == "123"
assert normalized.warning_threshold == Decimal("0.7")
assert normalized.critical_threshold == Decimal("0.75")
def test_normalize_alert_evaluation_input_rejects_bool_values() -> None:
config = PortfolioConfig(gold_value=215_000.0, entry_price=215.0, loan_amount=151_000.0)
with pytest.raises(TypeError, match="portfolio.ltv_ratio must be numeric, got bool"):
_normalize_alert_evaluation_input(
config,
{
"ltv_ratio": True,
"spot_price": 215.0,
"quote_updated_at": "2026-03-24T12:00:00Z",
},
)
def test_alert_service_reports_ok_when_ltv_is_below_warning(alert_service: AlertService) -> None:
config = PortfolioConfig(gold_value=215_000.0, entry_price=215.0, loan_amount=120_000.0)
portfolio = build_portfolio_alert_context(