package web
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/lukaszkasprzak/lectio/internal/liturgy"
)
func TestRenderReadings(t *testing.T) {
secs := []liturgy.Section{{Heading: "Ewangelia (J 20, 1. 11-18)", PartID: "ewangelia"}}
html := string(RenderReadings(secs, []string{"wuj"}, "new", "horizontal", "pl", liturgy.DayInfo{}))
if !strings.Contains(html, "Ewangelia") || !strings.Contains(html, "class=") {
t.Errorf("reading pane missing heading/classes: %q", html[:min(200, len(html))])
}
}
// TestRenderReadingsDayInfoHeader checks RenderReadings prepends a
// "
" header carrying the celebration Name (heading role)
// and Season (citation role, muted) ahead of the reading sections, and that
// it reuses the existing role classes rather than inventing new ones.
func TestRenderReadingsDayInfoHeader(t *testing.T) {
secs := []liturgy.Section{{Heading: "Ewangelia (J 20, 1. 11-18)", PartID: "ewangelia"}}
info := liturgy.DayInfo{Name: "Święto św. Marii Magdaleny", Colour: "white"}
html := string(RenderReadings(secs, []string{"wuj"}, "new", "horizontal", "pl", info))
if !strings.Contains(html, `class="dayinfo"`) {
t.Errorf("missing dayinfo header: %q", html[:min(300, len(html))])
}
if !strings.Contains(html, "Święto św. Marii Magdaleny") {
t.Errorf("dayinfo header missing the day name: %q", html[:min(300, len(html))])
}
if i := strings.Index(html, `class="dayinfo"`); i > strings.Index(html, "reading-section") && strings.Index(html, "reading-section") != -1 {
t.Errorf("dayinfo header should come before the reading sections: %q", html[:min(400, len(html))])
}
}
// TestRenderReadingsDayInfoSeason checks the Season (traditional lectionary
// only) renders as a muted citation-role span alongside Name.
func TestRenderReadingsDayInfoSeason(t *testing.T) {
secs := []liturgy.Section{{Heading: "Gospel", PartID: "evangelium"}}
info := liturgy.DayInfo{Name: "St. Mary Magdalene", Season: "Feria IV after VIII Sunday after Pentecost", Colour: "white"}
html := string(RenderReadings(secs, []string{"wuj"}, "traditional", "horizontal", "en", info))
if !strings.Contains(html, "Feria IV after VIII Sunday after Pentecost") {
t.Errorf("dayinfo header missing Season: %q", html[:min(400, len(html))])
}
}
// TestRenderReadingsNoDayInfoHeaderWhenEmpty checks the header is entirely
// omitted (never an empty/error stub) when the source yielded no DayInfo.
func TestRenderReadingsNoDayInfoHeaderWhenEmpty(t *testing.T) {
secs := []liturgy.Section{{Heading: "Ewangelia (J 20, 1. 11-18)", PartID: "ewangelia"}}
html := string(RenderReadings(secs, []string{"wuj"}, "new", "horizontal", "pl", liturgy.DayInfo{}))
if strings.Contains(html, `class="dayinfo"`) {
t.Errorf("dayinfo header should be omitted when Name is empty: %q", html)
}
}
func TestBuiltinThemes(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // no user themes
for _, name := range []string{
"transfiguration", "desert_fathers", "benedictines", "franciscans",
"memento_mori", "dominicans", "advent", "nativity", "lent",
"easter", "pentecost", "ordinary", "epiphany", "marian",
} {
if b, err := themeCSS(name); err != nil || len(b) == 0 {
t.Errorf("built-in theme %s not embedded: %v", name, err)
}
}
}
func TestThemeCSSGuardRejectsInvalidNames(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // no user themes
for _, name := range []string{"../../etc/passwd", "..", "a/b", ""} {
if _, err := themeCSS(name); err == nil {
t.Errorf("themeCSS(%q): expected error, got nil", name)
}
}
}
func TestRenderReadingsEscapesScriptText(t *testing.T) {
secs := []liturgy.Section{{
Heading: "Test",
PartID: "pierwsze_czytanie",
Paragraphs: [][]string{{""}},
}}
html := string(RenderReadings(secs, []string{"bt"}, "new", "horizontal", "pl", liturgy.DayInfo{}))
if strings.Contains(html, "") {
t.Errorf("raw