from app.domain.risk_engine import BaselineRiskEngine, IndicatorValue from app.main import app from fastapi.testclient import TestClient def test_preview_requires_authentication() -> None: response = TestClient(app).post( "/api/v1/risk-predictions/preview", json={ "subjectId": "customer-001", "indicators": [ {"name": "debtBurden", "value": 80, "weight": 3}, {"name": "cashflowVolatility", "value": 20, "weight": 1}, ], }, ) assert response.status_code == 401 def test_baseline_engine_calculates_weighted_score() -> None: score = BaselineRiskEngine().predict( [ IndicatorValue(name="debtBurden", value=80, weight=3), IndicatorValue(name="cashflowVolatility", value=20, weight=1), ] ) assert score.score == 35.0 assert score.level.value == "high" assert [factor.contribution for factor in score.factors] == [15.0, 20.0] def test_risk_level_boundaries_follow_prd() -> None: engine = BaselineRiskEngine() cases = ( (80, "low"), (60, "moderately_low"), (40, "medium"), (20, "high"), (19.99, "very_high"), ) for score, expected_level in cases: result = engine.predict([IndicatorValue(name="risk", value=100 - score, weight=1)]) assert result.score == score assert result.level.value == expected_level