From 417f62710651cd40102777ef44e284216dd0fcd8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 May 2026 09:58:31 +0800 Subject: [PATCH] =?UTF-8?q?test(P0-4):=20=E8=A1=A5=E9=BD=90=20health=20pla?= =?UTF-8?q?tform=20=E6=B5=8B=E8=AF=95=20-=20Evaluate=20=E8=A6=86=E7=9B=96?= =?UTF-8?q?=20100%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扩展 internal/platform/health/health_test.go(+5 个测试): - TestEvaluate_NoCheckers_ReturnsTrue: nil checkers → true - TestEvaluate_EmptyCheckers_ReturnsTrue: empty checkers → true - TestEvaluate_AllCheckersPass_ReturnsTrue: 全通过 → true - TestEvaluate_SomeCheckersFail_ReturnsFalse: 部分失败 → false - TestEvaluate_NilChecker_Skipped: nil checker 跳过不 panic - TestEvaluate_AllCheckersFail_ReturnsFalse: 全失败 → false **覆盖率提升**: - internal/platform/health: 38.1% → **100.0%** (+61.9%) ✅ - 整体覆盖率: 73.6% → **74.8%** (+1.2%) Ref: test/PHASE2_TEST_PLAN.md P0-4 --- internal/platform/health/health_test.go | 113 ++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/internal/platform/health/health_test.go b/internal/platform/health/health_test.go index 9defd44..16656b3 100644 --- a/internal/platform/health/health_test.go +++ b/internal/platform/health/health_test.go @@ -1,6 +1,8 @@ package health import ( + "context" + "errors" "testing" ) @@ -61,3 +63,114 @@ func TestProbe_SetReady_IsReady(t *testing.T) { }) } } + +func TestEvaluate_NoCheckers_ReturnsTrue(t *testing.T) { + ctx := context.Background() + healthy, results := Evaluate(ctx, nil) + if !healthy { + t.Errorf("Evaluate(nil) healthy = %v, want true", healthy) + } + if results != nil { + t.Errorf("Evaluate(nil) results = %v, want nil", results) + } +} + +func TestEvaluate_EmptyCheckers_ReturnsTrue(t *testing.T) { + ctx := context.Background() + healthy, results := Evaluate(ctx, []Checker{}) + if !healthy { + t.Errorf("Evaluate([]) healthy = %v, want true", healthy) + } + if results != nil { + t.Errorf("Evaluate([]) results = %v, want nil", results) + } +} + +func TestEvaluate_AllCheckersPass_ReturnsTrue(t *testing.T) { + ctx := context.Background() + checkers := []Checker{ + stubChecker{name: "db", err: nil}, + stubChecker{name: "redis", err: nil}, + } + healthy, results := Evaluate(ctx, checkers) + if !healthy { + t.Errorf("Evaluate() healthy = %v, want true", healthy) + } + if len(results) != 2 { + t.Errorf("len(results) = %d, want 2", len(results)) + } + for _, r := range results { + if r.Status != "UP" { + t.Errorf("result %s status = %s, want UP", r.Name, r.Status) + } + } +} + +func TestEvaluate_SomeCheckersFail_ReturnsFalse(t *testing.T) { + ctx := context.Background() + checkers := []Checker{ + stubChecker{name: "db", err: nil}, + stubChecker{name: "redis", err: errors.New("connection refused")}, + } + healthy, results := Evaluate(ctx, checkers) + if healthy { + t.Errorf("Evaluate() healthy = %v, want false", healthy) + } + if len(results) != 2 { + t.Errorf("len(results) = %d, want 2", len(results)) + } + for _, r := range results { + if r.Name == "redis" && r.Status != "DOWN" { + t.Errorf("redis result status = %s, want DOWN", r.Status) + } + if r.Name == "db" && r.Status != "UP" { + t.Errorf("db result status = %s, want UP", r.Status) + } + } +} + +func TestEvaluate_NilChecker_Skipped(t *testing.T) { + ctx := context.Background() + checkers := []Checker{ + stubChecker{name: "db", err: nil}, + nil, + stubChecker{name: "cache", err: nil}, + } + healthy, results := Evaluate(ctx, checkers) + if !healthy { + t.Errorf("Evaluate() healthy = %v, want true (nil skipped)", healthy) + } + if len(results) != 2 { + t.Errorf("len(results) = %d, want 2 (nil skipped)", len(results)) + } +} + +func TestEvaluate_AllCheckersFail_ReturnsFalse(t *testing.T) { + ctx := context.Background() + checkers := []Checker{ + stubChecker{name: "db", err: errors.New("db down")}, + stubChecker{name: "redis", err: errors.New("redis down")}, + } + healthy, results := Evaluate(ctx, checkers) + if healthy { + t.Errorf("Evaluate() healthy = %v, want false", healthy) + } + if len(results) != 2 { + t.Errorf("len(results) = %d, want 2", len(results)) + } + for _, r := range results { + if r.Status != "DOWN" { + t.Errorf("result %s status = %s, want DOWN", r.Name, r.Status) + } + } +} + +// stubChecker is a test double for Checker interface. +type stubChecker struct { + name string + err error +} + +func (s stubChecker) Name() string { return s.name } + +func (s stubChecker) Check(_ context.Context) error { return s.err }