Files
bookhoard/templates/utils_test.go
T
John O'Keefe bbedbfb13f feat(facets+ui): clickable facet values everywhere, genre prettify, active-filter chips
Frontend half of the faceted-navigation Phase 2. 'Everything filterable
should be clickable': every displayed facet value links to the bookshelf
pre-filtered by its raw stored value; display forms are prettified at
render, never in the DB.

- templates/facet_links.templ: FacetLink (inline links) + FacetBadge
  (badge links) to /bookshelf?<facet>_filter=<value>; the raw value
  rides the param while the shown text is prettified, so matching and
  display can never disagree. Series/tags keep their dedicated pages
  by design; the card media-type icon stays unlinked (a derived kind,
  not a column value — deep-linking it would send wrong values).
- book_detail.templ: publisher, language, edition, genre, imprint
  cells + narrators line + age-rating/story-arc badges are now links.
- prettifyGenre (templates/utils.go + unit tests): title-cases only
  entirely-lowercase values; small words stay down except first
  position; & preserved. TS port for chip labels documents the twin.
- bookshelf.templ + bookshelf.ts: active-filter chips bar — Alpine
  renders chips from client-local form state (sanctioned island);
  one bubbled htmx:afterRequest hook keeps chips + shareable URL in
  sync; removeFilterChip clears the facet and re-applies. URL deep
  links now accept all 18 facet params (hidden inputs where the
  drawer has no field).

Pins changelog: Batch B/C entry in docs/contract_pins.md. Rendering
ownership guard (make lint-rendering) green.
2026-10-19 18:59:36 -04:00

122 lines
3.9 KiB
Go

package templates
import (
"testing"
"bookhoard/internal/database"
)
func TestGroupTunableSettings(t *testing.T) {
entries := []SettingEntry{
{Key: "session_duration_seconds", Group: "Session", RequiresRestart: false},
{Key: "password_min_length", Group: "Password Quality", RequiresRestart: false},
{Key: "password_require_upper", Group: "Password Quality", RequiresRestart: false},
{Key: "opds_default_page_size", Group: "OPDS Catalog", RequiresRestart: false},
{Key: "auth_rate_limit_per_min", Group: "Auth Rate Limiting", RequiresRestart: true},
{Key: "login_max_attempts", Group: "Login Lockout", RequiresRestart: true},
{Key: "login_lockout_minutes", Group: "Login Lockout", RequiresRestart: true},
}
live, restart := GroupTunableSettings(entries)
if len(live) != 3 {
t.Fatalf("expected 3 live groups, got %d", len(live))
}
if live[0].Name != "Session" || len(live[0].Entries) != 1 {
t.Errorf("live[0] = %+v", live[0])
}
if live[1].Name != "Password Quality" || len(live[1].Entries) != 2 {
t.Errorf("live[1] = %+v", live[1])
}
if live[2].Name != "OPDS Catalog" || len(live[2].Entries) != 1 {
t.Errorf("live[2] = %+v", live[2])
}
if len(restart) != 2 {
t.Fatalf("expected 2 restart groups, got %d", len(restart))
}
if restart[0].Name != "Auth Rate Limiting" || len(restart[0].Entries) != 1 {
t.Errorf("restart[0] = %+v", restart[0])
}
if restart[1].Name != "Login Lockout" || len(restart[1].Entries) != 2 {
t.Errorf("restart[1] = %+v", restart[1])
}
}
func TestGroupTunableSettingsEmpty(t *testing.T) {
live, restart := GroupTunableSettings(nil)
if len(live) != 0 || len(restart) != 0 {
t.Errorf("expected empty groups, got live=%d restart=%d", len(live), len(restart))
}
}
func TestThemeCheckClass(t *testing.T) {
if got := themeCheckClass("dracula", "dracula"); got != "" {
t.Errorf("active theme should be visible, got %q", got)
}
if got := themeCheckClass("nord", "dracula"); got != " hidden" {
t.Errorf("inactive theme should be hidden, got %q", got)
}
}
// The library-type dropdown renders from DB rows; ordering and labels are
// presentation policy (OrderedLibraryTypes / LibraryTypeDisplayName).
func TestOrderedLibraryTypes(t *testing.T) {
// The DB query returns ORDER BY name; the dropdown must not reshuffle
// the historical UI order, and unknown (future) types append by name.
types := []database.LibraryTypes{
{Name: "audiobooks"}, {Name: "comics"}, {Name: "ebooks"},
{Name: "manga"}, {Name: "videos"},
}
got := OrderedLibraryTypes(types)
want := []string{"ebooks", "comics", "manga", "audiobooks", "videos"}
if len(got) != len(want) {
t.Fatalf("got %d types, want %d", len(got), len(want))
}
for i, name := range want {
if got[i].Name != name {
t.Errorf("position %d = %q, want %q", i, got[i].Name, name)
}
}
}
func TestLibraryTypeDisplayName(t *testing.T) {
cases := map[string]string{
"ebooks": "Ebooks",
"comics": "Comics",
"manga": "Manga",
"audiobooks": "Audiobooks",
"videos": "Videos", // unknown type: capitalized fallback
"": "",
}
for name, want := range cases {
if got := LibraryTypeDisplayName(name); got != want {
t.Errorf("LibraryTypeDisplayName(%q) = %q, want %q", name, got, want)
}
}
}
func TestPrettifyGenre(t *testing.T) {
cases := []struct{ in, want string }{
{"science fiction", "Science Fiction"},
{"sword & sorcery", "Sword & Sorcery"},
{"action & adventure", "Action & Adventure"},
{"war & military", "War & Military"},
{"historical fiction", "Historical Fiction"},
{"children's", "Children's"},
{"middle grade", "Middle Grade"},
{"fiction", "Fiction"},
// Small words stay down except in first position.
{"towers of midnight", "Towers of Midnight"},
// Mixed case = user-authored; verbatim.
{"Science Fiction", "Science Fiction"},
{"HardSF", "HardSF"},
{"", ""},
}
for _, c := range cases {
if got := prettifyGenre(c.in); got != c.want {
t.Errorf("prettifyGenre(%q) = %q, want %q", c.in, got, c.want)
}
}
}