package health

import (
	"context"
	"testing"

	"hyapp/pkg/grpchealth"
	"hyapp/pkg/servicekit/grpcserver"
)

func TestNewRegistersSharedChecker(t *testing.T) {
	server := grpcserver.New("test-service")
	health, healthHTTP, err := New(server, Config{
		ServiceName: "test-service",
		NodeID:      "node-1",
		Dependencies: []grpchealth.Dependency{{
			Name:  "mysql",
			Check: func(context.Context) error { return nil },
		}},
	})
	if err != nil {
		t.Fatalf("new health failed: %v", err)
	}
	if health == nil {
		t.Fatalf("health checker is required")
	}
	if healthHTTP != nil {
		t.Fatalf("empty HTTP addr should disable health HTTP server")
	}
	health.MarkServing()
	if !grpchealth.Healthy(health.Ready(context.Background())) {
		t.Fatalf("health should be ready after serving mark and passing dependency")
	}
	grpcserver.GracefulStop(server, 0)
}

func TestNewHTTPCreatesCheckerWithoutGRPC(t *testing.T) {
	health, healthHTTP, err := NewHTTP(Config{ServiceName: "statistics-service", NodeID: "node-1"})
	if err != nil {
		t.Fatalf("new HTTP health failed: %v", err)
	}
	if health == nil {
		t.Fatalf("health checker is required")
	}
	if healthHTTP != nil {
		t.Fatalf("empty HTTP addr should disable health HTTP server")
	}
}
