aboutsummaryrefslogtreecommitdiff
path: root/internal/liturgy/parse_test.go
blob: 8fe2eb05c126845ee900781094a2d99f75b61f29 (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
package liturgy

import (
	"os"
	"strings"
	"testing"
)

func TestParse(t *testing.T) {
	html, _ := os.ReadFile("testdata/2026-07-22.html")
	secs, err := Parse(string(html))
	if err != nil {
		t.Fatal(err)
	}
	var gospel *Section
	var firstCzytanie *Section
	var secondCzytanie *Section
	for i := range secs {
		if strings.HasPrefix(secs[i].Heading, "Ewangelia") {
			gospel = &secs[i]
		}
		if strings.HasPrefix(secs[i].Heading, "1. czytanie") {
			if firstCzytanie == nil {
				firstCzytanie = &secs[i]
			} else if secondCzytanie == nil {
				secondCzytanie = &secs[i]
			}
		}
	}
	if gospel == nil {
		t.Fatal("no gospel section")
	}
	if gospel.Citation != "J 20, 1. 11-18" {
		t.Errorf("gospel citation = %q", gospel.Citation)
	}
	if len(gospel.Paragraphs) == 0 {
		t.Error("gospel has no paragraphs")
	}
	if gospel.PartID != "ewangelia" {
		t.Errorf("gospel PartID = %q, want %q", gospel.PartID, "ewangelia")
	}

	if firstCzytanie == nil {
		t.Fatal("no '1. czytanie' section")
	}
	if firstCzytanie.PartID != "pierwsze_czytanie" {
		t.Errorf("first '1. czytanie' PartID = %q, want %q", firstCzytanie.PartID, "pierwsze_czytanie")
	}
	// 2026-07-22 is a split-feast day with two "1. czytanie" sections in the
	// same tab; the second one must not collide with the first.
	if secondCzytanie == nil {
		t.Fatal("expected a second '1. czytanie' section (split feast fixture)")
	}
	if secondCzytanie.PartID != "drugie_czytanie" {
		t.Errorf("second '1. czytanie' PartID = %q, want %q", secondCzytanie.PartID, "drugie_czytanie")
	}
}

// TestParseDayInfo checks the modern (niedziela.pl) day-info extraction
// against the split-feast fixture: Name comes from the color-classed,
// fw-bold <p><em>...</em></p> near id="dzien" (not the earlier, plain
// fw-bold lookalike higher up the page), and Colour from "Kolor szat:
// biały" -> "white". Season is always "" for the modern lectionary.
func TestParseDayInfo(t *testing.T) {
	html, err := os.ReadFile("testdata/2026-07-22.html")
	if err != nil {
		t.Fatal(err)
	}
	info := ParseDayInfo(string(html))
	if !strings.Contains(info.Name, "Marii Magdaleny") {
		t.Errorf("Name = %q, want it to contain %q", info.Name, "Marii Magdaleny")
	}
	if info.Colour != "white" {
		t.Errorf("Colour = %q, want %q", info.Colour, "white")
	}
	if info.Season != "" {
		t.Errorf("Season = %q, want empty for the modern lectionary", info.Season)
	}
}

// TestParseDayInfoMultilineName checks the day name is cleanly joined when
// the source <em> body itself carries an embedded line break (a long
// commemoration name wrapped across lines in the page's own markup), and
// that a multi-option colour line ("zielony albo biały albo czerwony")
// maps by its first word.
func TestParseDayInfoMultilineName(t *testing.T) {
	html, err := os.ReadFile("testdata/2026-06-22.html")
	if err != nil {
		t.Fatal(err)
	}
	info := ParseDayInfo(string(html))
	if !strings.Contains(info.Name, "Dzień Powszedni") || !strings.Contains(info.Name, "Jana Fishera") {
		t.Errorf("Name = %q, want it to contain both wrapped-line fragments", info.Name)
	}
	if strings.Contains(info.Name, "\n") {
		t.Errorf("Name = %q, should not contain a raw newline", info.Name)
	}
	if info.Colour != "green" {
		t.Errorf("Colour = %q, want %q (first of \"zielony albo...\")", info.Colour, "green")
	}
}

// TestParseDayInfoNoMatch checks an unrecognised page shape yields a zero
// DayInfo rather than an error -- the header is simply omitted by callers.
func TestParseDayInfoNoMatch(t *testing.T) {
	info := ParseDayInfo("<html><body>redesigned</body></html>")
	if info != (DayInfo{}) {
		t.Errorf("ParseDayInfo(unrecognised) = %+v, want zero value", info)
	}
}

func TestParseLayoutChange(t *testing.T) {
	if _, err := Parse("<html><body>redesigned</body></html>"); err == nil {
		t.Error("expected error on missing reading tab")
	}
}

func TestParseNormalDay(t *testing.T) {
	html, err := os.ReadFile("testdata/2026-06-22.html")
	if err != nil {
		t.Fatal(err)
	}
	secs, err := Parse(string(html))
	if err != nil {
		t.Fatal(err)
	}
	if len(secs) != 4 {
		t.Fatalf("len(secs) = %d, want 4", len(secs))
	}
	want := map[string]string{
		"1. czytanie": "pierwsze_czytanie",
		"Psalm":       "psalm",
		"Aklamacja":   "aklamacja",
		"Ewangelia":   "ewangelia",
	}
	for _, s := range secs {
		for prefix, partID := range want {
			if strings.HasPrefix(s.Heading, prefix) {
				if s.PartID != partID {
					t.Errorf("heading %q: PartID = %q, want %q", s.Heading, s.PartID, partID)
				}
			}
		}
		if s.Subtitle == "" {
			t.Errorf("heading %q: empty subtitle", s.Heading)
		}
		if len(s.Paragraphs) == 0 {
			t.Errorf("heading %q: no paragraphs", s.Heading)
		}
	}
}

func TestExtractCitation(t *testing.T) {
	cases := []struct {
		heading string
		want    string
	}{
		{"Ewangelia (Mt 7, 1-5)", "Mt 7, 1-5"},
		{"1. czytanie (Pnp 8, 6-7)", "Pnp 8, 6-7"},
		{"Psalm (Ps 63 (62), 2. 3-4. 5-6. 8-9 (R.: por. 2ab))", "Ps 63 (62), 2. 3-4. 5-6. 8-9 (R.: por. 2ab)"},
	}
	for _, c := range cases {
		got, err := ExtractCitation(c.heading)
		if err != nil {
			t.Errorf("ExtractCitation(%q) error: %v", c.heading, err)
			continue
		}
		if got != c.want {
			t.Errorf("ExtractCitation(%q) = %q, want %q", c.heading, got, c.want)
		}
	}

	if _, err := ExtractCitation("no parens here"); err == nil {
		t.Error("expected error for heading with no parenthetical")
	}
}