aboutsummaryrefslogtreecommitdiff
path: root/internal/tradlit/parse_test.go
blob: 58cc66f3d26c345e44ddc3df484276e6db2a70a5 (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
package tradlit

import (
	"os"
	"strings"
	"testing"

	"github.com/lukaszkasprzak/lectio/internal/liturgy"
)

func TestParse(t *testing.T) {
	body, err := os.ReadFile("testdata/2026-07-22.json")
	if err != nil {
		t.Fatal(err)
	}
	secs, _, err := Parse(body)
	if err != nil {
		t.Fatal(err)
	}
	var gospel, epistle bool
	for _, s := range secs {
		if s.PartID == "evangelium" {
			gospel = true
			if s.Citation != "Luke 7:36-50" {
				t.Errorf("gospel citation = %q", s.Citation)
			}
			if len(s.Paragraphs) == 0 {
				t.Error("gospel has no vernacular text")
			}
		}
		if s.PartID == "lectio" {
			epistle = true
		}
	}
	if !gospel || !epistle {
		t.Errorf("missing parts: gospel=%v epistle=%v", gospel, epistle)
	}
}

// TestParseDayInfo checks the traditional (missalemeum) day-info
// extraction against the fixture's "info" object: Name from info.title,
// Season from info.tempora, Colour from info.colors[0] ("w" -> "white").
func TestParseDayInfo(t *testing.T) {
	body, err := os.ReadFile("testdata/2026-07-22.json")
	if err != nil {
		t.Fatal(err)
	}
	_, info, err := Parse(body)
	if err != nil {
		t.Fatal(err)
	}
	if info.Name != "St. Mary Magdalene" {
		t.Errorf("Name = %q, want %q", info.Name, "St. Mary Magdalene")
	}
	if !strings.Contains(info.Season, "Pentecost") {
		t.Errorf("Season = %q, want it to contain %q", info.Season, "Pentecost")
	}
	if info.Colour != "white" {
		t.Errorf("Colour = %q, want %q", info.Colour, "white")
	}
}

// TestParseDayInfoMissingInfo checks that a response with no (or empty)
// info object yields a zero DayInfo rather than an error.
func TestParseDayInfoMissingInfo(t *testing.T) {
	_, info, err := Parse([]byte(`[{"sections":[]}]`))
	if err != nil {
		t.Fatal(err)
	}
	if info != (liturgy.DayInfo{}) {
		t.Errorf("info = %+v, want zero value for a response with no info object", info)
	}
}

// TestParseDayInfoUnknownColourCode checks an unrecognised colour code maps
// to "" rather than passing the raw code through.
func TestParseDayInfoUnknownColourCode(t *testing.T) {
	body := []byte(`[{"info":{"title":"Test","tempora":"","colors":["z"]},"sections":[]}]`)
	_, info, err := Parse(body)
	if err != nil {
		t.Fatal(err)
	}
	if info.Colour != "" {
		t.Errorf("Colour = %q, want empty for unknown code %q", info.Colour, "z")
	}
}