Files
sundynix-pets/pets-be/internal/admin/embed_test.go
T
Blizzard 6c37785ce1
ci-cd / verify (push) Successful in 1m40s
ci-cd / deploy (push) Skipped
Complete admin dashboard with paginated user pet trees and walk relations
2026-10-04 11:45:15 +08:00

54 lines
1.4 KiB
Go

package admin
import (
"io/fs"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/sundynix/pets-be/internal/config"
"github.com/sundynix/pets-be/internal/httpapi"
)
func TestAdminAtRootAndAPIRoutes(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
httpapi.New(nil, nil, &config.Config{}).Register(r)
Register(r)
assets, err := fs.ReadDir(distFS, "dist/assets")
if err != nil {
t.Fatal(err)
}
var script string
for _, asset := range assets {
if strings.HasSuffix(asset.Name(), ".js") {
script = "/assets/" + asset.Name()
break
}
}
if script == "" {
t.Fatal("admin bundle has no JavaScript asset")
}
for _, tc := range []struct {
path string
status int
body string
}{
{path: "/", status: http.StatusOK, body: "<div id=\"root\"></div>"},
{path: script, status: http.StatusOK},
{path: "/brand-logo.png", status: http.StatusOK},
{path: "/api/ping", status: http.StatusOK, body: "pong"},
{path: "/api/missing", status: http.StatusNotFound},
{path: "/assets/missing.js", status: http.StatusNotFound},
} {
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, tc.path, nil))
if recorder.Code != tc.status || (tc.body != "" && !strings.Contains(recorder.Body.String(), tc.body)) {
t.Errorf("GET %s: got %d %q, want %d containing %q", tc.path, recorder.Code, recorder.Body.String(), tc.status, tc.body)
}
}
}