Files
projax/web/theme_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

145 lines
5.2 KiB
Go

package web_test
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestThemeDefaultIsDark proves the layout boots into dark mode when no
// projax_theme cookie is present — that is Phase 4b's stated default.
func TestThemeDefaultIsDark(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
code, body := get(t, h, "/views/tree")
if code != 200 {
t.Fatalf("GET /views/tree → %d", code)
}
for _, want := range []string{
`<html lang="en" data-theme="dark">`,
`<meta name="theme-color" content="#161616">`,
`id="theme-toggle"`,
} {
if !strings.Contains(body, want) {
t.Errorf("default-theme page missing %q", want)
}
}
// The toggle's icon for dark = sun glyph (☀) so the click implies "switch
// to light". Cross-check that the inverse glyph (moon ☾) is absent in
// dark mode so we don't accidentally render both.
if !strings.Contains(body, "☀") {
t.Errorf("dark mode should show ☀ glyph (action = flip to light), got body lacking it")
}
}
// TestThemeCookieRoundTrips: setting projax_theme=light makes the next
// render flip <html data-theme=light> and the apple theme-color meta to
// the light palette colour.
func TestThemeCookieRoundTrips(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
req := httptest.NewRequest(http.MethodGet, "/views/tree", nil)
req.AddCookie(&http.Cookie{Name: "projax_theme", Value: "light"})
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
body, _ := io.ReadAll(w.Result().Body)
bodyStr := string(body)
if !strings.Contains(bodyStr, `data-theme="light"`) {
t.Errorf("expected data-theme=light when cookie set, got body:\n%s", bodyStr[:400])
}
if !strings.Contains(bodyStr, `<meta name="theme-color" content="#f0efe8">`) {
t.Errorf("light mode should set apple theme-color to light palette bg-alt")
}
// Sanity: the dark colour is NOT present anywhere in the resulting meta.
if strings.Contains(bodyStr, `<meta name="theme-color" content="#161616">`) {
t.Errorf("light render leaked the dark theme-color meta")
}
}
// TestThemeCookieUnknownFallsBackToDark proves we don't break when a stale
// or hand-typed cookie value sneaks in.
func TestThemeCookieUnknownFallsBackToDark(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
req := httptest.NewRequest(http.MethodGet, "/views/tree", nil)
req.AddCookie(&http.Cookie{Name: "projax_theme", Value: "neon-puke"})
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
body, _ := io.ReadAll(w.Result().Body)
if !strings.Contains(string(body), `data-theme="dark"`) {
t.Errorf("unknown cookie value should fall back to dark")
}
}
// TestThemeTogglePagesShareSameTheme: every chrome-bearing page renders the
// same <html data-theme=…>. Without that guarantee m would see the theme
// flicker as he navigated between pages.
func TestThemeTogglePagesShareSameTheme(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
probe := func(path, cookieValue string) string {
t.Helper()
req := httptest.NewRequest(http.MethodGet, path, nil)
if cookieValue != "" {
req.AddCookie(&http.Cookie{Name: "projax_theme", Value: cookieValue})
}
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
body, _ := io.ReadAll(w.Result().Body)
return string(body)
}
for _, path := range []string{"/views/tree", "/views/dashboard", "/views/timeline", "/views/graph", "/admin", "/admin/bulk", "/admin/classify"} {
dark := probe(path, "")
light := probe(path, "light")
if !strings.Contains(dark, `data-theme="dark"`) {
t.Errorf("GET %s default should render data-theme=dark", path)
}
if !strings.Contains(light, `data-theme="light"`) {
t.Errorf("GET %s with cookie=light should render data-theme=light", path)
}
}
}
// TestThemeToggleScriptPresent: the inline JS that writes the cookie must
// ship with every layout-bearing page so the toggle button is functional.
func TestThemeToggleScriptPresent(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body := get(t, h, "/views/dashboard")
for _, want := range []string{
"document.cookie = 'projax_theme=",
`getElementById('theme-toggle')`,
"SameSite=Lax",
} {
if !strings.Contains(body, want) {
t.Errorf("dashboard page missing theme-toggle script piece %q", want)
}
}
}
// TestThemeColorMetaHelper covers the small server-side helper directly.
// Belongs in theme_test.go even though it's a unit test — it lives next to
// the integration tests so a future hand can flip the palette in one file.
func TestThemeColorMetaHelper(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
// Indirect: render a fragment with a Theme override to confirm injection
// does not double-write the meta when caller already populates it.
req := httptest.NewRequest(http.MethodGet, "/views/dashboard", nil)
req.AddCookie(&http.Cookie{Name: "projax_theme", Value: "light"})
w := httptest.NewRecorder()
srv.Routes().ServeHTTP(w, req)
body, _ := io.ReadAll(w.Result().Body)
// Count actual <meta> tags, not the JS selector reference in the inline script.
if c := strings.Count(string(body), `<meta name="theme-color"`); c != 1 {
t.Errorf("expected exactly one <meta name=\"theme-color\"> tag, got %d", c)
}
}