Implements the Pansy v1 scaffold (epic #20, phase 0). #1 Backend: Go module (pure-Go, builds with CGO_ENABLED=0), env config, gin server with slog + recovery + /api/v1/healthz, modernc SQLite store with WAL/busy_timeout/foreign_keys pragmas, embedded numbered-migration runner, full initial schema (users, sessions, gardens, garden_shares, garden_objects, plants, plantings), domain structs + sentinel errors, graceful shutdown. #2 Frontend: Vite + React 19 + TS (strict) + Tailwind 4 + TanStack Router/Query, typed /api/v1 fetch wrapper (ApiError carries status + body so later issues can read 409 conflict rows), dev proxy /api -> :8080, responsive app shell with stub pages for all five routes. #3 Single binary: //go:embed of the web build with a committed placeholder, SPA fallback (deep links, immutable asset caching, JSON 404 for unmatched /api and missing assets), Makefile (web/build/dev/test), README quickstart + env var table. Verified: go build/vet/test clean (CGO off); binary migrates idempotently and serves healthz; web tsc + build clean; integrated binary serves the SPA, deep links, and correct cache headers; app mounts and navigates all five routes at desktop and 375px widths. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func testEngine() *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
// A real API route so unmatched /api/* paths reach NoRoute (as in production).
|
|
r.GET("/api/v1/healthz", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"ok": true}) })
|
|
|
|
dist := fstest.MapFS{
|
|
"index.html": &fstest.MapFile{Data: []byte(`<!doctype html><div id="root"></div>`)},
|
|
"assets/app-abc123.js": &fstest.MapFile{Data: []byte(`console.log(1)`)},
|
|
}
|
|
RegisterSPA(r, dist)
|
|
return r
|
|
}
|
|
|
|
func do(t *testing.T, r *gin.Engine, method, path string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest(method, path, nil))
|
|
return w
|
|
}
|
|
|
|
func TestSPAServesHashedAssetImmutable(t *testing.T) {
|
|
w := do(t, testEngine(), http.MethodGet, "/assets/app-abc123.js")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", w.Code)
|
|
}
|
|
if cc := w.Header().Get("Cache-Control"); !strings.Contains(cc, "immutable") {
|
|
t.Errorf("Cache-Control = %q, want immutable", cc)
|
|
}
|
|
if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "javascript") {
|
|
t.Errorf("Content-Type = %q, want javascript", ct)
|
|
}
|
|
}
|
|
|
|
func TestSPAServesIndexNoCache(t *testing.T) {
|
|
w := do(t, testEngine(), http.MethodGet, "/")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", w.Code)
|
|
}
|
|
if cc := w.Header().Get("Cache-Control"); cc != "no-cache" {
|
|
t.Errorf("Cache-Control = %q, want no-cache", cc)
|
|
}
|
|
}
|
|
|
|
func TestSPAFallbackForClientRoute(t *testing.T) {
|
|
// A deep link with no matching file returns index.html so client routing works.
|
|
w := do(t, testEngine(), http.MethodGet, "/gardens/42")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", w.Code)
|
|
}
|
|
if !strings.Contains(w.Body.String(), `id="root"`) {
|
|
t.Errorf("expected index.html body, got %q", w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSPAMissingAssetIs404(t *testing.T) {
|
|
// A missing asset must NOT fall back to index.html (which would be a 200 of
|
|
// HTML for a .js request — a MIME-type error in the browser).
|
|
w := do(t, testEngine(), http.MethodGet, "/assets/missing-xyz.js")
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want 404", w.Code)
|
|
}
|
|
if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "json") {
|
|
t.Errorf("Content-Type = %q, want json", ct)
|
|
}
|
|
}
|
|
|
|
func TestSPAUnmatchedAPIIs404JSON(t *testing.T) {
|
|
w := do(t, testEngine(), http.MethodGet, "/api/v1/nope")
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want 404", w.Code)
|
|
}
|
|
if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "json") {
|
|
t.Errorf("Content-Type = %q, want json (not the SPA HTML)", ct)
|
|
}
|
|
}
|
|
|
|
func TestSPARejectsNonGET(t *testing.T) {
|
|
w := do(t, testEngine(), http.MethodPost, "/gardens")
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("status = %d, want 405", w.Code)
|
|
}
|
|
}
|