Files
bookhoard/templates/utils_test.go
T
John O'Keefe 9e831085ef fix(ui): derive Library Type dropdowns from library_types — audiobooks was missing
The audiobook library type shipped backend-complete (LibraryTypeAudiobooks
constant, allowed extensions, MIME map, schema seed row, GET
/api/libraries/types) but neither create-library form offered it: the
admin Create Library modal and the setup wizard's add-library step both
hardcoded their <option> lists to the original three types
(ebooks/comics/manga), so an audiobook library could not be created from
the UI.

Both dropdowns now render from the same DB-backed library_types table
the create API resolves types against — a future type appears without
template edits (the exact drift the rendering-ownership rule warns
about):

- templates/utils.go: OrderedLibraryTypes pins the historical dropdown
  order (ebooks, comics, manga, audiobooks) and appends unknown future
  types by name; LibraryTypeDisplayName renders labels with a
  capitalize-the-name fallback for types added without a label
- templates/admin_library.templ: new LibraryTypeOptions fragment used
  by the modal's select; AdminLibrary takes the types list
- templates/setup.templ: Setup takes the types list, same fragment
- internal/router/helpers.go: libraryTypeOptionsFor fetches via
  LibraryService.GetLibraryTypes, falling back to the built-in type
  names on DB failure so the forms stay usable (CreateLibrary still
  resolves the name against the DB, so the fallback is never
  authoritative)
- internal/router/frontend.go, internal/router/setup.go: pass the list
  into the two pages

Tests:
- templates/utils_test.go: pure unit tests for ordering + labels
- cmd/server/tests/admin_library_page_test.go: SSR regression —
  /admin/library must offer all four library types, audiobooks exactly
  once (options come from the DB loop, not a hardcode)
2026-10-18 16:38:03 -04:00

98 lines
3.2 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)
}
}
}