"""健康度计算引擎测试。""" from app.services.health_calculator import calculate_health_score, determine_trend class TestCalculateHealthScore: """健康度评分计算。""" def test_empty_data(self): """空数据应返回全 0。""" result = calculate_health_score({}) assert result["total_score"] == 0.0 assert result["financial_score"] == 0.0 def test_healthy_company(self): """健康企业:跑道充足 + 营收增长 + 烧钱下降。""" data = { "revenue": {"yoy_change": "30"}, "cash_balance": {"runway_months": 18}, "burn_rate": {"trend": "down"}, "headcount": {"new_hires": 5, "departures": 1}, "key_metrics": [{"name": "ARR", "change": "+25%"}], } result = calculate_health_score(data) assert result["total_score"] >= 69 assert result["financial_score"] > 80 def test_unhealthy_company(self): """不健康企业:跑道短 + 营收下滑 + 烧钱上升。""" data = { "revenue": {"yoy_change": "-20"}, "cash_balance": {"runway_months": 2}, "burn_rate": {"trend": "up"}, "headcount": {"new_hires": 0, "departures": 8}, } result = calculate_health_score(data) assert result["total_score"] < 50 assert result["financial_score"] < 30 def test_score_range(self): """评分应在 0-100 范围内。""" data = { "revenue": {"yoy_change": "1000"}, "cash_balance": {"runway_months": 100}, "burn_rate": {"trend": "down"}, } result = calculate_health_score(data) for key, val in result.items(): assert 0 <= val <= 100 class TestDetermineTrend: """趋势判断。""" def test_up(self): assert determine_trend(80, 60) == "up" def test_down(self): assert determine_trend(50, 70) == "down" def test_stable(self): assert determine_trend(60, 62) == "stable" def test_no_previous(self): assert determine_trend(70, None) == "stable"