package handlers import ( "net/http" "net/http/httptest" "testing" "gitea.leeworks.dev/0xwheatyz/gitea-mobile/internal/config" giteaclient "gitea.leeworks.dev/0xwheatyz/gitea-mobile/internal/gitea" ) func newTestHandler() *Handler { cfg := &config.Config{ GiteaURL: "https://gitea.example.com", SessionSecret: "test-secret-that-is-at-least-32-chars-long", ListenAddr: ":8080", } client := giteaclient.NewClient(cfg.GiteaURL) return NewHandler(cfg, client) } func TestHealth(t *testing.T) { h := newTestHandler() req := httptest.NewRequest(http.MethodGet, "/health", nil) w := httptest.NewRecorder() h.Health(w, req) if w.Code != http.StatusOK { t.Errorf("status = %d, want %d", w.Code, http.StatusOK) } if body := w.Body.String(); body != "ok\n" { t.Errorf("body = %q, want %q", body, "ok\n") } } func TestDashboard_NoToken(t *testing.T) { h := newTestHandler() req := httptest.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() h.Dashboard(w, req) if w.Code != http.StatusOK { t.Errorf("status = %d, want %d", w.Code, http.StatusOK) } // Without a token in context, should show "No organizations found." if body := w.Body.String(); body == "" { t.Error("expected non-empty response body") } } func TestDashboard_HTMX(t *testing.T) { h := newTestHandler() req := httptest.NewRequest(http.MethodGet, "/", nil) req.Header.Set("HX-Request", "true") w := httptest.NewRecorder() h.Dashboard(w, req) // HTMX request should not include full HTML page wrapper. body := w.Body.String() if body == "" { t.Error("expected non-empty response body") } // Should NOT contain DOCTYPE for HTMX fragment. if contains(body, "= len(substr) && searchString(s, substr) } func searchString(s, substr string) bool { for i := 0; i <= len(s)-len(substr); i++ { if s[i:i+len(substr)] == substr { return true } } return false }