package modules import ( "bytes" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/spf13/viper" ) func TestAgentHeartbeatRequiresConfiguredSecret(t *testing.T) { gin.SetMode(gin.TestMode) vp := viper.New() vp.Set("agent.shared_secret", "test-secret") router := gin.New() NewAgentModule(vp).RegisterRoutes(router) body := []byte(`{"status":"running","last_message_seq":42}`) unauthorized := httptest.NewRequest(http.MethodPost, "/agents/agent_hermes_main/heartbeat", bytes.NewReader(body)) unauthorized.Header.Set("Content-Type", "application/json") unauthorizedResult := httptest.NewRecorder() router.ServeHTTP(unauthorizedResult, unauthorized) if unauthorizedResult.Code != http.StatusUnauthorized { t.Fatalf("unauthorized status = %d, want %d", unauthorizedResult.Code, http.StatusUnauthorized) } heartbeat := httptest.NewRequest(http.MethodPost, "/agents/agent_hermes_main/heartbeat", bytes.NewReader(body)) heartbeat.Header.Set("Content-Type", "application/json") heartbeat.Header.Set("X-LineUp-Agent-Secret", "test-secret") heartbeatResult := httptest.NewRecorder() router.ServeHTTP(heartbeatResult, heartbeat) if heartbeatResult.Code != http.StatusOK { t.Fatalf("heartbeat status = %d, body = %s", heartbeatResult.Code, heartbeatResult.Body.String()) } health := httptest.NewRequest(http.MethodGet, "/agents/agent_hermes_main/health", nil) healthResult := httptest.NewRecorder() router.ServeHTTP(healthResult, health) if healthResult.Code != http.StatusOK { t.Fatalf("health status = %d, body = %s", healthResult.Code, healthResult.Body.String()) } if !bytes.Contains(healthResult.Body.Bytes(), []byte(`"online":true`)) { t.Fatalf("health body = %s, want online=true", healthResult.Body.String()) } }