summaryrefslogtreecommitdiff
path: root/internal/web/render_test.go
blob: 94d5e6e098fc54ead52112c5181b995af8d288c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
// "<p class="dayinfo">" 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) {
	// An attacker-controlled version code (?v=... reaches RenderReadings
	// unfiltered) flows to render.GatherVersion as both the column label and the
	// "(not in %s)" note. RenderReadings must HTML-escape it: it renders through
	// html/template, never wrapping untrusted text in template.HTML.
	const evil = "<script>alert(1)</script>"
	secs := []liturgy.Section{{
		Heading:  "Ewangelia",
		Citation: "J 20, 1. 11-18",
		Ref:      "John 20:1,11-18",
		PartID:   "pierwsze_czytanie",
	}}
	html := string(RenderReadings(secs, []string{evil}, "new", "horizontal", "pl", liturgy.DayInfo{}))
	if strings.Contains(html, evil) {
		t.Errorf("raw <script> leaked into rendered output: %q", html)
	}
	if !strings.Contains(html, "&lt;script&gt;alert(1)&lt;/script&gt;") {
		t.Errorf("expected escaped script tag in output: %q", html)
	}
}

func TestRenderReadingsVertical(t *testing.T) {
	secs := []liturgy.Section{{Heading: "Ewangelia (J 20, 1. 11-18)", PartID: "ewangelia"}}
	html := string(RenderReadings(secs, []string{"wuj", "vul"}, "new", "vertical", "pl", liturgy.DayInfo{}))
	if !strings.Contains(html, "display-vertical") {
		t.Errorf("vertical output missing display-vertical container: %q", html[:min(300, len(html))])
	}
	if !strings.Contains(html, "vcol") {
		t.Errorf("vertical output missing vcol columns: %q", html[:min(300, len(html))])
	}
	if !strings.Contains(html, "Wujek") || !strings.Contains(html, "Wulgata") {
		t.Errorf("vertical output missing both version labels: %q", html[:min(300, len(html))])
	}
}

func TestRenderReadingsInterlinear(t *testing.T) {
	secs := []liturgy.Section{{Heading: "Ewangelia", Citation: "J 20, 1. 11-18", Ref: "John 20:1,11-18", PartID: "ewangelia"}}
	html := string(RenderReadings(secs, []string{"wuj", "vul"}, "new", "interlinear", "pl", liturgy.DayInfo{}))
	if !strings.Contains(html, "ilverse") {
		t.Errorf("interlinear output missing ilverse: %q", html[:min(300, len(html))])
	}
	if !strings.Contains(html, `class="vnum"`) {
		t.Errorf("interlinear output missing vnum: %q", html[:min(300, len(html))])
	}
	// The first ilverse block should group both version labels under one key.
	// Bound it by the next vnum span (each ilverse carries exactly one). The
	// Wujek line appears only when the optional wuj corpus is embedded and
	// contributes versified text to the alignment.
	t.Run("both version labels", func(t *testing.T) {
		requireCorpus(t, "wuj")
		i := strings.Index(html, `class="vnum"`)
		block := html[i:]
		if j := strings.Index(html[i+1:], `class="vnum"`); j != -1 {
			block = html[i : i+1+j]
		}
		if !strings.Contains(block, "Wujek") || !strings.Contains(block, "Wulgata") {
			t.Errorf("first interlinear verse block missing both version labels: %q", block)
		}
	})
}

func TestRenderReadingsInterlinearExcludesBT(t *testing.T) {
	secs := []liturgy.Section{{Heading: "Ewangelia", Citation: "J 20, 1. 11-18", Ref: "John 20:1,11-18", PartID: "ewangelia"}}
	html := string(RenderReadings(secs, []string{"bt", "vul"}, "new", "interlinear", "pl", liturgy.DayInfo{}))
	// bt is dropped before rendering regardless, so its own label never shows.
	if strings.Contains(html, "Biblia Tysiąclecia (niedziela.pl)") {
		t.Errorf("interlinear output should substitute wuj for bt, not carry bt's label: %q", html[:min(300, len(html))])
	}
	// bt maps to wuj, but the Wujek label only renders when wuj is embedded and
	// supplies versified text to the alignment.
	t.Run("wuj substituted", func(t *testing.T) {
		requireCorpus(t, "wuj")
		if !strings.Contains(html, "Wujek") {
			t.Errorf("interlinear output should substitute wuj for bt: %q", html[:min(300, len(html))])
		}
	})
}

func TestRenderReadingsInterlinearNoVersifiedNote(t *testing.T) {
	secs := []liturgy.Section{{Heading: "Bez odwołania", PartID: "ewangelia"}}
	html := string(RenderReadings(secs, []string{"wuj"}, "new", "interlinear", "pl", liturgy.DayInfo{}))
	if strings.Contains(html, "ilverse") {
		t.Errorf("expected no ilverse blocks when nothing resolves: %q", html)
	}
	if !strings.Contains(html, "brak wersetów do zestawienia interlinearnego") {
		t.Errorf("pl no-verses note missing/wrong: %q", html)
	}
}

// TestRenderReadingsInterlinearNoVersifiedNoteLang checks the interlinear
// "no verses to align" note (finding §1) follows lang, not always Polish.
func TestRenderReadingsInterlinearNoVersifiedNoteLang(t *testing.T) {
	secs := []liturgy.Section{{Heading: "Bez odwołania", PartID: "ewangelia"}}
	html := string(RenderReadings(secs, []string{"wuj"}, "new", "interlinear", "en", liturgy.DayInfo{}))
	if !strings.Contains(html, "(no verses to align)") {
		t.Errorf("en no-verses note missing/wrong: %q", html)
	}
	if strings.Contains(html, "brak wersetów") {
		t.Errorf("en output should not carry the Polish no-verses note: %q", html)
	}
}

// TestRenderReadingsLocalizesEN checks that lang="en" localises both the
// version-column label (via render.GatherVersion) and the section heading's
// part-label word (via render.LocalizeHeading, brief §3b), while the
// citation stays exactly as scraped.
func TestRenderReadingsLocalizesEN(t *testing.T) {
	secs := []liturgy.Section{{Heading: "Ewangelia (J 20, 1. 11-18)", PartID: "ewangelia"}}
	html := string(RenderReadings(secs, []string{"vul"}, "new", "horizontal", "en", liturgy.DayInfo{}))
	if !strings.Contains(html, "Gospel (J 20, 1. 11-18)") {
		t.Errorf("en output missing localised heading: %q", html[:min(300, len(html))])
	}
	if !strings.Contains(html, "Vulgate (Latin)") {
		t.Errorf("en output missing localised version label: %q", html[:min(300, len(html))])
	}
	if strings.Contains(html, "Ewangelia") {
		t.Errorf("en output should not carry the Polish heading label: %q", html[:min(300, len(html))])
	}
}

func TestRenderPassageColumns(t *testing.T) {
	html := string(RenderPassage("John", "John", 3, []string{"wuj"}, "vertical", "en"))
	if !strings.Contains(html, "John 3") { // passage heading, independent of the corpus
		t.Errorf("passage columns missing heading:\n%s", html)
	}
	t.Run("verse", func(t *testing.T) {
		requireCorpus(t, "wuj") // the verse text comes from the optional wuj corpus
		if !strings.Contains(html, "3:16") {
			t.Errorf("passage columns missing verse:\n%s", html)
		}
	})
}

func TestRenderPassageInterlinear(t *testing.T) {
	html := string(RenderPassage("John", "John", 3, []string{"wuj", "vul"}, "interlinear", "en"))
	if !strings.Contains(html, "3:16") {
		t.Errorf("interlinear missing verse:\n%s", html)
	}
}

func TestRenderPassageMissingVersion(t *testing.T) {
	// A non-existent chapter yields no verses: the column must carry the
	// "(not in wuj)" note, never a panic or an empty column.
	html := string(RenderPassage("John", "John", 999, []string{"wuj"}, "vertical", "en"))
	if !strings.Contains(html, "wuj") {
		t.Errorf("missing-passage note absent:\n%s", html)
	}
}

func TestUserTheme(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", dir)
	td := filepath.Join(dir, "lectio", "themes")
	os.MkdirAll(td, 0o755)
	os.WriteFile(filepath.Join(td, "mine.css"), []byte(".heading{color:#f00}"), 0o644)
	found := false
	for _, n := range Themes() {
		if n == "mine" {
			found = true
		}
	}
	if !found {
		t.Error("user theme 'mine' not listed by Themes()")
	}
	if b, err := themeCSS("mine"); err != nil || len(b) == 0 {
		t.Errorf("user theme not read: %v", err)
	}
}