snowchive-server/tests/prefs_matrix_test.go
2026-11-19 04:40:59 +02:00

153 lines
7.1 KiB
Go

package main
// prefs_matrix_test.go - every preference, asserted against the markup it
// is supposed to change. The cookies are documented in prefs.go and their
// effects are spread across four templates; before this, nothing checked
// that setting one did anything at all.
import (
"net/http"
"strings"
"testing"
)
// prefCase is one preference: the cookie that turns it on, a page that
// shows it, and a marker that must be present in exactly one of the two
// states. present says which state the marker belongs to.
type prefCase struct {
name string
cookie string
value string
path string
marker string
present bool // true: the marker appears when the cookie is SET
}
func TestPreferenceMatrix(t *testing.T) {
buildFixture(t)
cases := []prefCase{
// --- card display switches (browse lists, search results) -------
{"hide thumbnails", "pref_hide_thumbs", "1", "/browse", `class="thumb-container"`, false},
{"hide thumbnails (search)", "pref_hide_thumbs", "1", "/search?q=prism&do_search=1", `class="result-thumb"`, false},
{"hide summaries", "pref_hide_summary", "1", "/browse", `class="card-summary"`, false},
{"hide classification", "pref_hide_classification", "1", "/browse", `class="doc-classification"`, false},
{"hide programs", "pref_hide_programs", "1", "/browse", `class="doc-programs"`, false},
{"hide people", "pref_hide_people", "1", "/browse", `class="doc-people"`, false},
{"hide journalists", "pref_hide_journalists", "1", "/browse", `class="doc-journalist`, false},
{"page preview hook", "pref_page_preview", "1", "/browse", "data-pp-id=", true},
// --- category tables --------------------------------------------
{"collection columns", "pref_show_coll_cols", "1", "/browse/program", `id="coll-cols" class="coll-cols-checkbox" checked`, true},
// --- the document page ------------------------------------------
{"info sidebar", "pref_info_position", "beside", "/doc/prism-slides", `class="doc-layout info-sidebar"`, true},
{"scroll panes", "pref_scroll_doc", "1", "/doc/prism-slides", "scroll-panes", true},
{"expand more info", "pref_open_info", "1", "/doc/prism-slides", `id="meta-toggle" class="meta-checkbox" checked`, true},
{"programs one per line", "pref_programs_list", "1", "/doc/prism-slides", `<ul class="meta-value-list">`, true},
{"tags one per line", "pref_tags_list", "1", "/doc/prism-slides", `<ul class="meta-value-list">`, true},
{"collapse published-together", "pref_collapse_pub", "1", "/doc/prism-slides", `<details class="collapse-section" open>`, false},
// --- the viewport picker ----------------------------------------
{"default view: text", "pref_doc_view", "text", "/doc/prism-slides", `id="view-text" aria-controls="doc-viewport" checked`, true},
{"default view: split", "pref_doc_view", "split", "/doc/prism-slides", `id="view-split" aria-controls="doc-viewport" checked`, true},
// --- theme -------------------------------------------------------
{"dark theme", "pref_theme", "dark", "/", `data-theme="dark"`, true},
{"light theme", "pref_theme", "light", "/browse", `data-theme="light"`, true},
}
for _, c := range cases {
off := get(t, c.path).Body.String()
on := get(t, c.path, &http.Cookie{Name: c.cookie, Value: c.value}).Body.String()
hasOff, hasOn := strings.Contains(off, c.marker), strings.Contains(on, c.marker)
wantOn, wantOff := c.present, !c.present
if hasOn != wantOn || hasOff != wantOff {
t.Errorf("%s (%s=%s on %s): marker %q present without the cookie=%v, with it=%v; wanted %v / %v",
c.name, c.cookie, c.value, c.path, c.marker, hasOff, hasOn, wantOff, wantOn)
}
}
}
// Text-only mode is the archive's promise to readers on Tor and metered
// links: NO page image, thumbnail or preview, anywhere, whatever else is
// set. It also overrides the thumbnail preference rather than being
// overridden by it.
func TestPreferenceTextOnly(t *testing.T) {
buildFixture(t)
textOnly := &http.Cookie{Name: "pref_text_only", Value: "1"}
thumbsOn := &http.Cookie{Name: "pref_hide_thumbs", Value: "0"}
preview := &http.Cookie{Name: "pref_page_preview", Value: "1"}
for _, p := range []string{"/", "/browse", "/browse?collection=sidtoday-archive&view=card",
"/doc/prism-slides", "/doc/prism-slides/pages", "/doc/prism-slides/versions/lemonde",
"/search?q=prism&do_search=1", "/pinned", "/articles"} {
body := get(t, p, textOnly, thumbsOn, preview).Body.String()
if i := strings.Index(body, "<img"); i >= 0 {
t.Errorf("%s in text-only mode emits an image: %.80s", p, body[i:])
}
if strings.Contains(body, "data-pp-imgs") {
t.Errorf("%s in text-only mode emits the page-preview payload", p)
}
}
// The document page says so and offers the pages as links instead.
doc := get(t, "/doc/prism-slides", textOnly).Body.String()
for _, want := range []string{"Text-only mode is on", `class="page-link-list"`} {
if !strings.Contains(doc, want) {
t.Errorf("the document page in text-only mode lacks %q", want)
}
}
// Without it, that same page does show images.
if !strings.Contains(get(t, "/doc/prism-slides").Body.String(), "<img") {
t.Error("the document page shows no images even with images on")
}
}
// Documents per page drives the list length and the pagination, on browse
// and on search alike.
func TestPreferencePerPage(t *testing.T) {
buildFixture(t)
count := func(body string) int { return strings.Count(body, `class="doc-card"`) }
all := count(get(t, "/browse", &http.Cookie{Name: "pref_per_page", Value: "all"}).Body.String())
if all < 2 {
t.Fatalf("the fixture's news collection has %d cards; the test needs at least 2", all)
}
one := get(t, "/browse", &http.Cookie{Name: "pref_per_page", Value: "10"}).Body.String()
if n := count(one); n > 10 {
t.Errorf("per_page=10 rendered %d cards", n)
}
// An unknown value falls back to the default rather than to everything.
def := count(get(t, "/browse", &http.Cookie{Name: "pref_per_page", Value: "7"}).Body.String())
if def > 20 {
t.Errorf("an invalid per_page rendered %d cards, want at most the default 20", def)
}
}
// The two history preferences are opt-in: nothing is recorded until they
// are on, and turning one off deletes what it recorded.
func TestPreferenceHistoryOptIn(t *testing.T) {
buildFixture(t)
// Off by default: viewing a document sets no history cookie.
w := get(t, "/doc/prism-slides")
for _, c := range w.Result().Cookies() {
if c.Name == recentDocsCookie && c.Value != "" {
t.Errorf("a document view recorded history without the preference: %v", c)
}
}
// On: the view is recorded.
w = get(t, "/doc/prism-slides", &http.Cookie{Name: "pref_track_recent", Value: "1"})
found := false
for _, c := range w.Result().Cookies() {
if c.Name == recentDocsCookie && strings.Contains(c.Value, "prism-slides") {
found = true
}
}
if !found {
t.Error("with tracking on, a document view was not recorded")
}
// A page showing history must never be stored by a shared cache.
hist := get(t, "/", &http.Cookie{Name: "pref_track_recent", Value: "1"},
&http.Cookie{Name: recentDocsCookie, Value: "prism-slides"})
if cc := hist.Header().Get("Cache-Control"); !strings.Contains(cc, "no-store") {
t.Errorf("the home page showing history: Cache-Control %q", cc)
}
}