package service

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestHealthEndpoint(t *testing.T) {
	previous := backendStore
	backendStore = newTestGORMStore(t)
	defer func() { backendStore = previous }()

	handler := NewHandler(Config{Name: "Test Service", Domain: "test", Capabilities: []string{"health"}})
	recorder := httptest.NewRecorder()
	request := httptest.NewRequest(http.MethodGet, "/health", nil)

	handler.ServeHTTP(recorder, request)

	if recorder.Code != http.StatusOK {
		t.Fatalf("expected status %d, got %d", http.StatusOK, recorder.Code)
	}

	var body response
	if err := json.NewDecoder(recorder.Body).Decode(&body); err != nil {
		t.Fatalf("decode response: %v", err)
	}
	if body.Status != "ok" || body.Domain != "test" {
		t.Fatalf("unexpected body: %+v", body)
	}
}
