aboutsummaryrefslogtreecommitdiff
path: root/internal/tradlit/tradlit_test.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-07-24 09:15:40 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-07-24 09:15:40 +0200
commitb93de95dd24ff2a1d3126e6d7d00cd77530a03bf (patch)
tree483a0901a20274152ba3f03f272f8098be4f49c0 /internal/tradlit/tradlit_test.go
parentc1b954ad517cef00bf480d5229b253a8c6524be7 (diff)
downloadlectio-b93de95dd24ff2a1d3126e6d7d00cd77530a03bf.tar.gz
lectio-b93de95dd24ff2a1d3126e6d7d00cd77530a03bf.zip
tradlit: offline caching + read; update pre-caches traditional; --clean prunes it; v0.2.0
Diffstat (limited to 'internal/tradlit/tradlit_test.go')
-rw-r--r--internal/tradlit/tradlit_test.go121
1 files changed, 121 insertions, 0 deletions
diff --git a/internal/tradlit/tradlit_test.go b/internal/tradlit/tradlit_test.go
new file mode 100644
index 0000000..cd2cc83
--- /dev/null
+++ b/internal/tradlit/tradlit_test.go
@@ -0,0 +1,121 @@
+package tradlit
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/lukaszkasprzak/lectio/internal/liturgy"
+)
+
+// TestLoadOnlineCachesRawBody exercises Load's online path: a successful
+// fetch is cached verbatim (the raw response body, not the parsed
+// sections) at tradlit's cache path, and parses the same way a cached read
+// would.
+func TestLoadOnlineCachesRawBody(t *testing.T) {
+ body, err := os.ReadFile("testdata/2026-07-22.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ hits := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ hits++
+ w.Write(body)
+ }))
+ defer srv.Close()
+
+ orig := baseURL
+ baseURL = srv.URL + "/%s/api/v5/proper/%s"
+ defer func() { baseURL = orig }()
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+
+ secs, err := Load("2026-07-22", "en", false)
+ if err != nil {
+ t.Fatalf("Load: %v", err)
+ }
+ if len(secs) == 0 {
+ t.Fatal("Load returned no sections")
+ }
+ if hits != 1 {
+ t.Errorf("server hit %d times, want 1", hits)
+ }
+
+ cached, err := os.ReadFile(filepath.Join(liturgy.CacheDir(), "2026-07-22.trad.en.json"))
+ if err != nil {
+ t.Fatalf("cache file not written: %v", err)
+ }
+ if string(cached) != string(body) {
+ t.Error("cached content does not match the raw response body")
+ }
+}
+
+// TestLoadOnline404WritesNoCache checks the documented invariant: a 404 (no
+// propers published for that date) returns an error and leaves the cache
+// directory untouched.
+func TestLoadOnline404WritesNoCache(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ defer srv.Close()
+
+ orig := baseURL
+ baseURL = srv.URL + "/%s/api/v5/proper/%s"
+ defer func() { baseURL = orig }()
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+
+ _, err := Load("2026-07-22", "en", false)
+ if err == nil {
+ t.Fatal("expected error on 404, got nil")
+ }
+
+ cachePath := filepath.Join(liturgy.CacheDir(), "2026-07-22.trad.en.json")
+ if _, statErr := os.Stat(cachePath); !os.IsNotExist(statErr) {
+ t.Errorf("cache file should not exist after a 404 (stat err = %v)", statErr)
+ }
+}
+
+// TestLoadOfflineReadsCache exercises Load's offline path against a
+// pre-written cache file (as an earlier online Load, or 'lectio update',
+// would have left behind) -- no network access at all.
+func TestLoadOfflineReadsCache(t *testing.T) {
+ body, err := os.ReadFile("testdata/2026-07-22.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ dir := t.TempDir()
+ t.Setenv("XDG_CACHE_HOME", dir)
+ cacheDir := filepath.Join(dir, "lectio")
+ if err := os.MkdirAll(cacheDir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(cacheDir, "2026-07-22.trad.pl.json"), body, 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ secs, err := Load("2026-07-22", "pl", true)
+ if err != nil {
+ t.Fatalf("Load offline: %v", err)
+ }
+ if len(secs) == 0 {
+ t.Fatal("Load offline returned no sections")
+ }
+}
+
+// TestLoadOfflineMissingCacheErrors checks Load's offline path errors
+// clearly (mentioning the missing cache) rather than trying the network,
+// when nothing has been cached yet for that (date, lang).
+func TestLoadOfflineMissingCacheErrors(t *testing.T) {
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+
+ _, err := Load("2026-07-22", "pl", true)
+ if err == nil {
+ t.Fatal("expected error for missing cache, got nil")
+ }
+ msg := strings.ToLower(err.Error())
+ if !strings.Contains(msg, "no cached") {
+ t.Errorf("error %q should mention no cached propers", err.Error())
+ }
+}