- web/static/manifest.webmanifest: name/short_name/start_url=/dashboard/ display=standalone/theme_color/background_color + three icons (192, 512, 512-maskable with ~12% safe-zone padding) - web/static/sw.js: minimal SW — install caches /static/* shell assets, fetch is network-first with cache fallback on GETs only, skips /mcp/ and non-GETs entirely. CACHE_NAME versioned for clean activate-time prune. - cmd/icongen: stdlib-only generator that produces the three PNG icons from a stylised "p" monogram. Run once at brand-change, commit output. - web.init() registers .webmanifest → application/manifest+json with mime.AddExtensionType so Chrome accepts the manifest at all - layout.tmpl + login.tmpl: manifest link, apple-touch-icon, theme-color, apple-mobile-web-app-* metas, inline SW-register on load (silent on failure — older browsers still work) - design.md gets §"PWA install (Phase 3j)"; CLAUDE.md "Out of scope" drops the Phase-3j line and adds push/background-sync as the remaining Otto-PWA territory - 4 new tests cover manifest MIME, sw.js delivery, all 3 icons, layout meta tags
122 lines
3.3 KiB
Go
122 lines
3.3 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, "/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
|
|
}
|
|
}
|
|
}
|