Files
mcdsl/health/health_test.go
Kyle Isom ebe2079a83 Migrate module path from kyle/ to mc/ org
All import paths updated from git.wntrmute.dev/kyle/mcdsl to
git.wntrmute.dev/mc/mcdsl to match the Gitea organization.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 02:03:45 -07:00

98 lines
2.2 KiB
Go

package health
import (
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"google.golang.org/grpc"
"git.wntrmute.dev/mc/mcdsl/db"
)
func TestHandlerHealthy(t *testing.T) {
database := openTestDB(t)
handler := Handler(database)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
var body map[string]string
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode: %v", err)
}
if body["status"] != "ok" {
t.Fatalf("status = %q, want %q", body["status"], "ok")
}
}
func TestHandlerUnhealthy(t *testing.T) {
database := openTestDB(t)
// Close the database to simulate unhealthy state.
_ = database.Close()
handler := Handler(database)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusServiceUnavailable)
}
var body map[string]string
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode: %v", err)
}
if body["status"] != "unhealthy" {
t.Fatalf("status = %q, want %q", body["status"], "unhealthy")
}
if body["error"] == "" {
t.Fatal("expected error message")
}
}
func TestHandlerContentType(t *testing.T) {
database := openTestDB(t)
handler := Handler(database)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
handler.ServeHTTP(rec, req)
if ct := rec.Header().Get("Content-Type"); ct != "application/json" {
t.Fatalf("Content-Type = %q, want %q", ct, "application/json")
}
}
func TestRegisterGRPC(t *testing.T) {
srv := grpc.NewServer()
// Should not panic.
RegisterGRPC(srv)
info := srv.GetServiceInfo()
if _, ok := info["grpc.health.v1.Health"]; !ok {
t.Fatal("health service not registered")
}
}
func openTestDB(t *testing.T) *sql.DB {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "test.db")
database, err := db.Open(path)
if err != nil {
t.Fatalf("db.Open: %v", err)
}
t.Cleanup(func() { _ = database.Close() })
return database
}