From cd16b014da0ff38db81bc76cbdfbb7123ebb5f29 Mon Sep 17 00:00:00 2001 From: Virgil Date: Tue, 31 Mar 2026 19:53:19 +0000 Subject: [PATCH] fix(api): include health-check reason payload Co-Authored-By: Virgil --- pkg/api/provider.go | 6 ++++++ pkg/api/provider_test.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pkg/api/provider.go b/pkg/api/provider.go index 0f2e4aa..ad89ad1 100644 --- a/pkg/api/provider.go +++ b/pkg/api/provider.go @@ -236,10 +236,15 @@ func (p *ProcessProvider) healthCheck(c *gin.Context) { } healthy := process.WaitForHealth(entry.Health, 2000) + reason := "" + if !healthy { + reason = "health endpoint did not report healthy" + } result := map[string]any{ "healthy": healthy, "address": entry.Health, + "reason": reason, } // Emit health event @@ -247,6 +252,7 @@ func (p *ProcessProvider) healthCheck(c *gin.Context) { "code": code, "daemon": daemon, "healthy": healthy, + "reason": reason, }) statusCode := http.StatusOK diff --git a/pkg/api/provider_test.go b/pkg/api/provider_test.go index ec06882..625c9a1 100644 --- a/pkg/api/provider_test.go +++ b/pkg/api/provider_test.go @@ -5,6 +5,7 @@ package api_test import ( "net/http" "net/http/httptest" + "os" "testing" process "dappco.re/go/core/process" @@ -89,6 +90,27 @@ func TestProcessProvider_GetDaemon_Bad(t *testing.T) { assert.Equal(t, http.StatusNotFound, w.Code) } +func TestProcessProvider_HealthCheck_NoEndpoint_Good(t *testing.T) { + dir := t.TempDir() + registry := newTestRegistry(dir) + require.NoError(t, registry.Register(process.DaemonEntry{ + Code: "test", + Daemon: "nohealth", + PID: os.Getpid(), + })) + + p := processapi.NewProvider(registry, nil) + + r := setupRouter(p) + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/api/process/daemons/test/nohealth/health", nil) + r.ServeHTTP(w, req) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Contains(t, w.Body.String(), "no health endpoint configured") + assert.Contains(t, w.Body.String(), "\"reason\"") +} + func TestProcessProvider_RegistersAsRouteGroup_Good(t *testing.T) { p := processapi.NewProvider(nil, nil)