package server import ( "bytes" "context" "image" "image/color" "image/png" "net/http" "net/http/httptest" "net/url" "os" "os/exec" "strings" "testing" "time" ) func TestHomePaletteSettingsAndScope(t *testing.T) { s, token, domain := configFixture(t) s.mux.HandleFunc("/", s.handleHome) saveWallpaperFixture(t, s, domain, "/wallpapers/a.jpg", "image/jpeg") if err := s.store.SetConfigInDomain(domain, "home_wallpaper_path", "/wallpapers/a.jpg"); err != nil { t.Fatal(err) } initial := s.homePalette(domain) if initial.Mode != "surface" || initial.Strength != 35 || initial.Brightness != "preserve" { t.Fatal(initial) } for _, values := range []url.Values{{"home_palette_mode": {"bad"}}, {"home_palette_strength": {"101"}}, {"home_palette_strength": {"-1"}}, {"home_palette_strength": {"bad"}}, {"home_palette_brightness": {"bad"}}} { values.Set("section", "home-appearance") values.Set("home_glass_transparency", "47") if w := configRequest(s, token, "POST", "/-/sys/config", values); w.Code != 400 { t.Fatal(w.Code, w.Body) } if s.homePalette(domain) != initial || s.homeGlassTransparency(domain) != 42 { t.Fatal("invalid request partially saved") } } values := url.Values{"section": {"home-appearance"}, "home_glass_transparency": {"47"}, "home_palette_mode": {"accent"}, "home_palette_strength": {"70"}, "home_palette_brightness": {"auto"}} if w := configRequest(s, token, "POST", "/-/sys/config", values); w.Code != http.StatusFound { t.Fatal(w.Code, w.Body) } if p := s.homePalette(domain); p.Mode != "accent" || p.Strength != 70 || p.Brightness != "auto" { t.Fatal(p) } member, err := s.store.CreatePasswordAccount(domain, "palette-reader", "a long test password", "Reader", "") if err != nil { t.Fatal(err) } memberToken, err := s.store.CreateAuthSession(domain, member.UUID) if err != nil { t.Fatal(err) } if w := configRequest(s, memberToken, "POST", "/-/sys/config", url.Values{"section": {"home-appearance"}, "home_palette_mode": {"off"}, "home_glass_transparency": {"47"}}); w.Code != http.StatusForbidden { t.Fatalf("member changed domain appearance: %d", w.Code) } if s.homePalette(domain).Mode != "accent" { t.Fatal("member overwrote settings") } for _, layout := range []string{"classic", "search", "directory"} { if err := s.store.SetConfigInDomain(domain, "home_layout", layout); err != nil { t.Fatal(err) } w := configRequest(s, token, "GET", "/", nil) for _, want := range []string{`data-home-palette`, `data-mode="accent"`, `data-strength="70"`, `data-home-appearance-open`, `/static/js/home-palette.js?v=1`} { if w.Code != 200 || !strings.Contains(w.Body.String(), want) { t.Fatal(layout, w.Code, want) } } } for _, path := range []string{"/-/sys/config?panel=home", "/missing-page"} { w := configRequest(s, token, http.MethodGet, path, nil) if strings.Contains(w.Body.String(), `data-home-palette `) || strings.Contains(w.Body.String(), `src="/static/js/home-palette.js`) { t.Fatal("theme leaked outside home", path) } } settings := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=home", nil) if settings.Code != http.StatusOK || !strings.Contains(settings.Body.String(), `name="home_palette_mode"`) || !strings.Contains(settings.Body.String(), `value="accent" selected`) { t.Fatal("settings panel did not load saved palette") } for _, section := range []string{"home", "home-appearance"} { invalid := url.Values{"section": {section}, "home_palette_strength": {"NaN"}, "home_layout": {"search"}} if w := configRequest(s, token, http.MethodPost, "/-/sys/config", invalid); w.Code != http.StatusBadRequest { t.Fatal(section, w.Code) } } // Old forms may omit the new fields without resetting their saved settings. delete(values, "home_palette_mode") delete(values, "home_palette_strength") delete(values, "home_palette_brightness") if w := configRequest(s, token, "POST", "/-/sys/config", values); w.Code != http.StatusFound { t.Fatal(w.Code) } if s.homePalette(domain).Mode != "accent" { t.Fatal("old form reset settings") } if err := s.store.SetConfigInDomain(domain, "home_wallpaper_path", ""); err != nil { t.Fatal(err) } w := configRequest(s, token, "GET", "/", nil) if strings.Contains(w.Body.String(), `src="/static/js/home-palette.js`) { t.Fatal("loaded extractor without wallpaper") } } func TestHomePaletteChromium(t *testing.T) { if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { t.Skip("opt-in Chromium") } s, token, domain := configFixture(t) s.store.BackgroundJobsDisabled = true s = New(s.store) picture := image.NewRGBA(image.Rect(0, 0, 480, 320)) for y := 0; y < 320; y++ { for x := 0; x < 480; x++ { picture.SetRGBA(x, y, color.RGBA{uint8(30 + x/4), uint8(60 + y/3), uint8(180 + y/5), 255}) } } var content bytes.Buffer if err := png.Encode(&content, picture); err != nil { t.Fatal(err) } object, err := s.store.CreatePageAtPathInDomain(domain, "/wallpapers/test.png") if err != nil { t.Fatal(err) } if _, err = s.store.SaveRevisionContent(object, content.Bytes(), "test", "und", "image/png", "test.png"); err != nil { t.Fatal(err) } for k, v := range map[string]string{"home_layout": "directory", "home_wallpaper_path": "/wallpapers/test.png", "home_wallpaper_opacity": "70", "theme": "nanoha-dark"} { if err := s.store.SetConfigInDomain(domain, k, v); err != nil { t.Fatal(err) } } script, err := os.ReadFile("internal/server/testdata/home-palette.js") if err != nil { t.Fatal(err) } handler := s.handler() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" r.Header.Set("Authorization", "Bearer "+token) if r.URL.Path == "/fixture" { w.Header().Set("Content-Type", "text/html; charset=utf-8") _, _ = w.Write([]byte("")) return } handler.ServeHTTP(w, r) })) defer server.Close() ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) defer cancel() width := os.Getenv("HOME_PALETTE_WIDTH") if width == "" { width = "1440" } args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir(), os.Getenv("HOME_PALETTE_SCREENSHOT"), width, "850"} if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil { t.Fatalf("palette: %v\n%s", err, out) } if p := s.homePalette(domain); p.Mode != "accent" || p.Strength != 70 || p.Brightness != "auto" { t.Fatalf("browser save not persisted: %+v", p) } }