package readings import ( "net/http" "net/http/httptest" "os" "strings" "testing" "github.com/lukaszkasprzak/lectio/internal/config" "github.com/lukaszkasprzak/lectio/internal/liturgy" ) func sectionsForFilterTests() []liturgy.Section { return []liturgy.Section{ {PartID: "pierwsze_czytanie", Heading: "1. czytanie"}, {PartID: "psalm", Heading: "Psalm"}, {PartID: "ewangelia", Heading: "Ewangelia"}, } } func TestFilterPartsGospelOnly(t *testing.T) { secs := sectionsForFilterTests() got := filterParts(secs, config.Config{}, false) if len(got) != 1 { t.Fatalf("got %d sections, want 1: %+v", len(got), got) } if got[0].PartID != "ewangelia" { t.Errorf("got PartID %q, want ewangelia", got[0].PartID) } } func TestFilterPartsAll(t *testing.T) { secs := sectionsForFilterTests() cfg := config.Config{ Lectionary: "new", Parts: map[string]map[string]bool{ "new": {"psalm": false}, }, } got := filterParts(secs, cfg, true) if len(got) != 2 { t.Fatalf("got %d sections, want 2: %+v", len(got), got) } for _, s := range got { if s.PartID == "psalm" { t.Errorf("psalm should have been filtered out: %+v", got) } } ids := map[string]bool{got[0].PartID: true, got[1].PartID: true} if !ids["pierwsze_czytanie"] || !ids["ewangelia"] { t.Errorf("got %+v, want pierwsze_czytanie and ewangelia", got) } } func TestLoadModernRoutes(t *testing.T) { html, err := os.ReadFile("../liturgy/testdata/2026-07-22.html") if err != nil { t.Fatal(err) } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write(html) })) defer srv.Close() liturgy.SetBaseURL(srv.URL + "/liturgia/%s/Ewangelia") t.Setenv("XDG_CACHE_HOME", t.TempDir()) cfg := config.Config{Lectionary: "new"} secs, err := Load(cfg, Options{Date: "2026-07-22", All: true}) if err != nil { t.Fatalf("Load: %v", err) } found := false for _, s := range secs { if s.PartID == "ewangelia" { found = true } } if !found { t.Errorf("no gospel section (PartID=ewangelia) found in %+v", secs) } } func TestLoadTraditionalOfflineErrors(t *testing.T) { cfg := config.Config{Lectionary: "traditional", TraditionalLang: "pl"} _, err := Load(cfg, Options{Date: "2026-07-22", Offline: true}) if err == nil { t.Fatal("expected error, got nil") } msg := strings.ToLower(err.Error()) if !strings.Contains(msg, "offline") || !strings.Contains(msg, "traditional") { t.Errorf("error %q should mention offline and traditional", err.Error()) } }