Files
silk/server-go/internal/service/health_profile_test.go

86 lines
2.6 KiB
Go

package service
import (
"testing"
"time"
)
func TestComputeHealthScore(t *testing.T) {
if s, g := ComputeHealthScore(HealthInput{}); s != 100 || g != "优" {
t.Errorf("空输入应 100/优,实际 %.1f/%s", s, g)
}
bad := HealthInput{
RecentRiskLevels: []string{"red", "red"},
LampPositive: 1,
LampTotal: 2,
ConsultationCount: 1,
TraceCount: 1,
}
s, g := ComputeHealthScore(bad)
if s >= 80 {
t.Errorf("风险输入应显著扣分,实际 %.1f", s)
}
if g == "优" {
t.Errorf("风险输入不应评级为优,实际 %s", g)
}
}
func TestComputeHealthScoreClamp(t *testing.T) {
s, _ := ComputeHealthScore(HealthInput{RecentRiskLevels: []string{"red", "red", "red", "red", "red"}})
if s < 0 {
t.Errorf("分数不应为负,实际 %.1f", s)
}
}
func TestAggregateMonthlyStats(t *testing.T) {
entries := []MonthDiseaseEntry{
{Month: "2026-07", Disease: "白僵病"},
{Month: "2026-07", Disease: "白僵病"},
{Month: "2026-07", Disease: "软化病"},
{Month: "2026-08", Disease: "白僵病"},
}
stats := AggregateMonthlyStats(entries)
if len(stats) != 2 {
t.Fatalf("月份数 = %d, want 2", len(stats))
}
if stats[0].Month != "2026-07" || stats[0].Total != 3 {
t.Errorf("2026-07 应 total=3,实际 %+v", stats[0])
}
if stats[0].Diseases["白僵病"] != 2 || stats[0].Diseases["软化病"] != 1 {
t.Errorf("2026-07 病种分布不正确: %+v", stats[0].Diseases)
}
if len(AggregateMonthlyStats(nil)) != 0 {
t.Error("空输入应返回空")
}
}
func TestEvaluateControlEffectComparesWindows(t *testing.T) {
window := EffectWindow{
BeforeStart: time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC),
BeforeEnd: time.Date(2026, 8, 7, 0, 0, 0, 0, time.UTC),
AfterStart: time.Date(2026, 8, 8, 0, 0, 0, 0, time.UTC),
AfterEnd: time.Date(2026, 8, 14, 0, 0, 0, 0, time.UTC),
}
report := EvaluateControlEffect("event-1", window, EffectMetrics{
RiskSamples: []float64{80}, LampPositive: 2, LampTotal: 3,
}, EffectMetrics{
RiskSamples: []float64{40}, LampPositive: 1, LampTotal: 4,
})
if report.Conclusion != "改善" {
t.Fatalf("结论 = %s, want 改善", report.Conclusion)
}
if report.BeforeRiskAvg == nil || *report.BeforeRiskAvg != 80 {
t.Fatalf("before risk = %+v", report.BeforeRiskAvg)
}
}
func TestEvaluateControlEffectReportsMissing(t *testing.T) {
report := EvaluateControlEffect("event-1", EffectWindow{}, EffectMetrics{}, EffectMetrics{})
if report.Conclusion != "证据不足,无法判断效果" {
t.Fatalf("无数据结论应为证据不足,实际 %s", report.Conclusion)
}
if len(report.Missing) < 4 {
t.Fatalf("缺失维度应单独列出: %v", report.Missing)
}
}