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

53 lines
1.5 KiB
Go

package service
import "testing"
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("空输入应返回空")
}
}