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
...
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 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("redesigned") if info != (DayInfo{}) { t.Errorf("ParseDayInfo(unrecognised) = %+v, want zero value", info) } } func TestParseLayoutChange(t *testing.T) { if _, err := Parse("redesigned"); 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") } }