aboutsummaryrefslogtreecommitdiff
path: root/internal/readings/readings_test.go
blob: fd355fff5469a8f8c892e5a47977f11ed79c1173 (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
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())
	}
}