package httpserver_test import ( "io" "net/http" "net/http/httptest" "testing" "testing/fstest" "rickub.com/bots-garden/ori/internal/httpserver" ) // builtUI mimics a Vite production build. var builtUI = fstest.MapFS{ "index.html": {Data: []byte("ori")}, "assets/app.js": {Data: []byte("console.log('ori')")}, "assets/app.css": {Data: []byte("body{}")}, "vite.svg": {Data: []byte("")}, } func get(t *testing.T, handler http.Handler, path string) *httptest.ResponseRecorder { t.Helper() rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil)) return rec } func TestHealthz(t *testing.T) { rec := get(t, httpserver.Handler(builtUI, nil), "/healthz") if rec.Code != http.StatusOK { t.Fatalf("GET /healthz status = %d, want 200", rec.Code) } if body := rec.Body.String(); body != `{"status":"ok"}` { t.Errorf("GET /healthz body = %q", body) } } func TestServesIndex(t *testing.T) { rec := get(t, httpserver.Handler(builtUI, nil), "/") if rec.Code != http.StatusOK { t.Fatalf("GET / status = %d, want 200", rec.Code) } if body := rec.Body.String(); body != "ori" { t.Errorf("GET / body = %q, want index.html content", body) } } func TestServesStaticAsset(t *testing.T) { rec := get(t, httpserver.Handler(builtUI, nil), "/assets/app.js") if rec.Code != http.StatusOK { t.Fatalf("GET /assets/app.js status = %d, want 200", rec.Code) } if body := rec.Body.String(); body != "console.log('ori')" { t.Errorf("GET /assets/app.js body = %q", body) } } func TestUnknownPathFallsBackToIndex(t *testing.T) { rec := get(t, httpserver.Handler(builtUI, nil), "/some/client/route") if rec.Code != http.StatusOK { t.Fatalf("GET /some/client/route status = %d, want 200 (SPA fallback)", rec.Code) } if body := rec.Body.String(); body != "ori" { t.Errorf("SPA fallback body = %q, want index.html content", body) } } func TestMissingBuildAnswers503(t *testing.T) { empty := fstest.MapFS{".gitkeep": {Data: nil}} rec := get(t, httpserver.Handler(empty, nil), "/") if rec.Code != http.StatusServiceUnavailable { t.Fatalf("GET / without a UI build status = %d, want 503", rec.Code) } } func TestExtraRouteIsMounted(t *testing.T) { extra := map[string]http.Handler{ "GET /ws": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "ws-here") }), } rec := get(t, httpserver.Handler(builtUI, extra), "/ws") if rec.Code != http.StatusOK || rec.Body.String() != "ws-here" { t.Fatalf("GET /ws = %d %q, want 200 \"ws-here\"", rec.Code, rec.Body.String()) } }