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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
package web
import (
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/lukaszkasprzak/lectio/internal/config"
"github.com/lukaszkasprzak/lectio/internal/liturgy"
)
// TestServer exercises NewServer's handler tree end to end via httptest,
// against the same fixture HTML/hook internal/readings uses (see
// readings_test.go TestLoadModernRoutes): no real network, no real browser.
func TestServer(t *testing.T) {
html, err := os.ReadFile("../liturgy/testdata/2026-07-22.html")
if err != nil {
t.Fatal(err)
}
fixtureServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(html)
}))
defer fixtureServer.Close()
liturgy.SetBaseURL(fixtureServer.URL + "/liturgia/%s/Ewangelia")
t.Setenv("XDG_CACHE_HOME", t.TempDir())
srv := NewServer(config.Default())
t.Run("index page", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/?date=2026-07-22&v=wuj", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
body := rec.Body.String()
if !strings.Contains(body, "Ewangelia") {
t.Errorf("body missing reading heading: %q", body)
}
if !strings.Contains(body, "htmx") {
t.Errorf("body missing htmx reference")
}
if !strings.Contains(body, `id="theme"`) {
t.Errorf("body missing theme <link>")
}
})
t.Run("readings partial", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/readings?date=2026-07-22&v=wuj&all=1", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
body := rec.Body.String()
if strings.Contains(body, "<html") {
t.Errorf("partial is not a fragment: %q", body)
}
if !strings.Contains(body, "Ewangelia") {
t.Errorf("partial missing reading heading: %q", body)
}
})
t.Run("lookup", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/lookup?ref=J+20:1&v=wuj", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
if !strings.Contains(rec.Body.String(), "20:1") {
t.Errorf("lookup result missing verse: %q", rec.Body.String())
}
})
t.Run("theme.css", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/theme.css?name=benedictines", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
if !strings.Contains(rec.Header().Get("Content-Type"), "css") {
t.Errorf("Content-Type = %q, want it to contain css", rec.Header().Get("Content-Type"))
}
})
t.Run("static", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/static/htmx.min.js", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
if rec.Body.Len() == 0 {
t.Error("static/htmx.min.js served empty body")
}
})
t.Run("theme.css unknown falls back", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/theme.css?name=does-not-exist", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200 (fallback to cfg.WebTheme/default)", rec.Code)
}
if rec.Body.Len() == 0 {
t.Error("theme.css fallback served empty body")
}
})
t.Run("readings partial interlinear", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/readings?date=2026-07-22&v=wuj&v=vul&display=interlinear", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
body := rec.Body.String()
if strings.Contains(body, "<html") {
t.Errorf("partial is not a fragment: %q", body)
}
i := strings.Index(body, `class="vnum"`)
if i == -1 {
t.Fatalf("interlinear partial missing a vnum verse label: %q", body)
}
// Bound the first ilverse block by the next vnum span (each ilverse
// carries exactly one), so the check covers every version's line
// grouped under that one key, not just the first.
block := body[i:]
if j := strings.Index(body[i+1:], `class="vnum"`); j != -1 {
block = body[i : i+1+j]
}
if !strings.Contains(block, "Wujek") || !strings.Contains(block, "Wulgata") {
t.Errorf("interlinear verse block missing both version labels grouped together: %q", block)
}
})
t.Run("readings partial vertical", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/readings?date=2026-07-22&v=wuj&v=vul&display=vertical", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
if !strings.Contains(rec.Body.String(), "display-vertical") {
t.Errorf("vertical partial missing display-vertical: %q", rec.Body.String())
}
})
t.Run("readings partial interlinear substitutes pl", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/readings?date=2026-07-22&v=pl&display=interlinear", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
body := rec.Body.String()
if !strings.Contains(body, "Wujek") {
t.Errorf("pl should be substituted with wuj in interlinear mode: %q", body)
}
if strings.Contains(body, "Polski (niedziela.pl)") {
t.Errorf("pl paragraph column should not appear in interlinear mode: %q", body)
}
})
t.Run("readings partial bad display falls back to horizontal", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/readings?date=2026-07-22&v=wuj&display=bogus", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
body := rec.Body.String()
if strings.Contains(body, "display-vertical") || strings.Contains(body, "ilverse") {
t.Errorf("bad display should fall back to horizontal, got: %q", body)
}
})
t.Run("index page seeds lookup results from ?ref=", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/?ref=J+20:1&v=wuj", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
body := rec.Body.String()
if !strings.Contains(body, `id="lookup-results"`) {
t.Fatalf("body missing lookup-results container: %q", body)
}
i := strings.Index(body, `id="lookup-results"`)
if !strings.Contains(body[i:], "20:1") {
t.Errorf("lookup-results not populated from ?ref=: %q", body[i:min(i+400, len(body))])
}
})
t.Run("index page seeds display select from cfg", func(t *testing.T) {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/?date=2026-07-22&v=wuj", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
if !strings.Contains(rec.Body.String(), `name="display"`) {
t.Errorf("body missing display select: %q", rec.Body.String())
}
})
}
// TestChooseListener exercises chooseListener's port-selection logic
// directly (no HTTP serving): port==0 prefers defaultWebPort (1099) and
// falls back to a free OS port when 1099 is taken, and a non-zero port is
// bound exactly. Sandboxed/CI environments may not permit binding 1099 (or
// may race another process for it), so those assertions t.Skip rather than
// fail the suite.
func TestChooseListener(t *testing.T) {
t.Run("zero port returns a listener with a non-zero port", func(t *testing.T) {
ln, err := chooseListener(0)
if err != nil {
t.Fatalf("chooseListener(0) error: %v", err)
}
defer ln.Close()
port := ln.Addr().(*net.TCPAddr).Port
if port == 0 {
t.Errorf("chooseListener(0) returned port 0, want non-zero")
}
})
t.Run("falls back to a free port when 1099 is taken", func(t *testing.T) {
pre, err := net.Listen("tcp", ":1099")
if err != nil {
t.Skipf("cannot bind :1099 in this environment, skipping fallback assertion: %v", err)
}
defer pre.Close()
ln, err := chooseListener(0)
if err != nil {
t.Fatalf("chooseListener(0) error while 1099 is taken: %v", err)
}
defer ln.Close()
port := ln.Addr().(*net.TCPAddr).Port
if port == 1099 {
t.Errorf("chooseListener(0) returned 1099 even though it was already taken")
}
})
t.Run("explicit non-zero port is bound exactly", func(t *testing.T) {
probe, err := net.Listen("tcp", ":0")
if err != nil {
t.Skipf("cannot bind :0 to pick a free port in this environment: %v", err)
}
want := probe.Addr().(*net.TCPAddr).Port
probe.Close()
ln, err := chooseListener(want)
if err != nil {
t.Skipf("could not bind explicit port %d (likely a race with another process): %v", want, err)
}
defer ln.Close()
got := ln.Addr().(*net.TCPAddr).Port
if got != want {
t.Errorf("chooseListener(%d) bound port %d, want exactly %d", want, got, want)
}
})
}
|