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) } } }