Files
projax/web/pwa_test.go
mAi f820fa5830 feat(views): Phase 5j slice C — full URL migration + system views
Per m's Q1 pick (b) (2026-05-29): legacy `/`, `/dashboard`, `/calendar`,
`/timeline`, `/graph` become `/views/{system-slug}`. Old routes
301-redirect to the new ones with chip params preserved; the legacy
?view=<uuid> param from 5i is resolved through the uuid → slug map
when present so old bookmarks land on the right user view.

System views (web/system_views.go):
- SystemView struct (Slug / Name / Icon / URL) — code-resident, never
  rows in projax.views.
- AllSystemViews() returns the canonical five: tree, dashboard,
  calendar, timeline, graph. Display order matches the existing
  sidebar.
- LookupSystemView(slug) returns the matching entry or nil; the
  reserved-slug list in store.IsReservedViewSlug (slice A) is kept
  in sync.
- legacyRedirect(systemSlug) handler 301s with chip-param preservation
  + uuid → slug resolution for any leftover ?view=<uuid>.

Routes (web/server.go):
- GET /views/tree      → handleTree     (was GET /)
- GET /views/dashboard → handleDashboard
- GET /views/timeline  → handleTimeline
- GET /views/calendar  → handleCalendar
- GET /views/graph     → handleGraph
- GET /                → 301 → /views/tree
- GET /dashboard       → 301 → /views/dashboard
- GET /timeline        → 301 → /views/timeline
- GET /calendar        → 301 → /views/calendar
- GET /graph           → 301 → /views/graph
- POST action endpoints (/dashboard/task/*, /dashboard/pin, /admin/*)
  stay where they are — those are RPC-ish, not page renders.

handleTree: dropped the `r.URL.Path != "/"` guard — the only entry
point now is /views/tree, mounted via the new route. Slice F removes
any residual references; this slice keeps the handler reachable.

computeChipCounts grew a `base string` arg so chip URLs anchor on the
caller's route (/views/tree for the system tree, /views/{slug} for
saved views). PageViewTypes recognises both legacy and /views/ keys
during the transition.

Template hrefs / hx-gets bulk-updated to the new URLs:
- layout.tmpl: every sidebar + bottom-nav entry points at
  /views/{system-slug}. Active-state checks updated alongside.
- tree_section.tmpl, tree_card.tmpl, tree_kanban.tmpl: clear-filter
  / clear-all hrefs → /views/tree.
- calendar*.tmpl, timeline_section.tmpl, graph.tmpl,
  dashboard_section.tmpl: every internal nav + filter link points at
  the /views/{slug} surface.
- detail.tmpl, error.tmpl: cancel / back-to-tree → /views/tree.

Test-source updates (per the 5c sharpened rule):
- ~100 test paths bulk-rewritten from /dashboard /calendar /timeline
  /graph (and `/`) to their /views/{slug} counterparts. The
  behaviour-preservation contract holds: status codes + body shapes
  for the rendered pages stay the same; only the URL anchoring the
  test changes.
- layout_test.go: sidebar href assertions updated to /views/{slug}.
- view_type_test.go (Q2 + Q3 follow-up): PageViewTypes lookup table
  updated to use the new route keys.
- 2 deliberate behaviour-change assertions land: TestLegacyRedirects
  expects 301 on the old URLs (was 200); TestTreeRenders fetches
  /views/tree (the new home) instead of /.

Internal go-source URL emissions (dashboard.go, calendar.go,
timeline.go) updated to the new BasePath so chip + refresh URLs round
through /views/{slug} correctly.

New tests:
- TestSystemViewLookup — AllSystemViews shape + LookupSystemView
  round-trip + unknown-slug nil.
- TestLegacyRedirects — every legacy URL 301s to its new home with
  chip params preserved.
- TestLegacyViewUUIDRedirect — old `?view=<uuid>` URLs land on the
  resolved slug per m's Q3 pick.
2026-05-29 11:59:26 +02:00

122 lines
3.4 KiB
Go

package web_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestManifestServedWithCorrectMIME asserts /static/manifest.webmanifest
// returns 200 with the W3C-required Content-Type. Without it, Chrome treats
// the file as plain text and refuses to register the manifest.
func TestManifestServedWithCorrectMIME(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
req := httptest.NewRequest(http.MethodGet, "/static/manifest.webmanifest", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Result().StatusCode != 200 {
t.Fatalf("GET manifest → %d", w.Result().StatusCode)
}
ct := w.Result().Header.Get("Content-Type")
if !strings.HasPrefix(ct, "application/manifest+json") {
t.Errorf("Content-Type = %q, want application/manifest+json", ct)
}
// Validate the manifest payload parses + has required fields.
var m map[string]any
body, _ := readAll(t, w.Result().Body)
if err := json.Unmarshal([]byte(body), &m); err != nil {
t.Fatalf("manifest is not valid JSON: %v\n%s", err, body)
}
for _, k := range []string{"name", "short_name", "start_url", "display", "icons", "theme_color"} {
if _, ok := m[k]; !ok {
t.Errorf("manifest missing required field %q", k)
}
}
}
func TestServiceWorkerServed(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
req := httptest.NewRequest(http.MethodGet, "/static/sw.js", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Result().StatusCode != 200 {
t.Fatalf("GET sw.js → %d", w.Result().StatusCode)
}
ct := w.Result().Header.Get("Content-Type")
if !strings.Contains(ct, "javascript") {
t.Errorf("Content-Type = %q, want a javascript type", ct)
}
body, _ := readAll(t, w.Result().Body)
for _, want := range []string{"CACHE_NAME", "addEventListener", "fetch"} {
if !strings.Contains(body, want) {
t.Errorf("sw.js missing %q", want)
}
}
}
func TestIconsServed(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
for _, p := range []string{"/static/icon-192.png", "/static/icon-512.png", "/static/icon-maskable.png"} {
req := httptest.NewRequest(http.MethodGet, p, nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Result().StatusCode != 200 {
t.Errorf("GET %s → %d", p, w.Result().StatusCode)
}
if ct := w.Result().Header.Get("Content-Type"); ct != "image/png" {
t.Errorf("GET %s Content-Type = %q, want image/png", p, ct)
}
}
}
func TestLayoutHasManifestAndAppleTouchIcon(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body := get(t, h, "/views/dashboard")
for _, want := range []string{
`rel="manifest"`,
`/static/manifest.webmanifest`,
`rel="apple-touch-icon"`,
`/static/icon-192.png`,
`name="theme-color"`,
`apple-mobile-web-app-capable`,
`serviceWorker`,
} {
if !strings.Contains(body, want) {
t.Errorf("layout missing %q", want)
}
}
}
// readAll is a tiny helper that returns the response body as a string.
func readAll(t *testing.T, body interface {
Read(p []byte) (n int, err error)
Close() error
}) (string, error) {
t.Helper()
defer body.Close()
var b strings.Builder
buf := make([]byte, 4096)
for {
n, err := body.Read(buf)
if n > 0 {
b.Write(buf[:n])
}
if err != nil {
if err.Error() == "EOF" {
return b.String(), nil
}
return b.String(), err
}
}
}