package sites
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"supermarket/internal/api/gitea"
"supermarket/internal/db"
"supermarket/internal/seo"
)
func openDB(t *testing.T) *db.DB {
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { d.Close() })
return d
}
func TestPhrase(t *testing.T) {
for in, want := range map[string]string{
"- type: hero\n title: \"**One bill** for [your church](/c/)\"\n": "One bill for your church",
"- type: stats\n items: []\n- type: text\n headline: Why it works\n": "Why it works",
"- type: hero\n subhead: no title here\n": "",
"- type: hero\n title: " + strings.Repeat("word ", 30) + "\n": strings.TrimSpace(strings.Repeat("word ", 16)),
"not yaml: [": "",
} {
if got := Phrase(in); got != want {
t.Errorf("%q: %q, want %q", in, got, want)
}
}
}
func TestCheckLive(t *testing.T) {
var page string
status := 200
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/moved/" {
http.Redirect(w, r, "/elsewhere/", http.StatusMovedPermanently)
return
}
w.WriteHeader(status)
fmt.Fprint(w, page)
}))
defer srv.Close()
f := seo.NewFetcher("test")
now := time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC)
p := &SitePage{Site: srv.URL + "/", Path: "/for-churches/", Title: "For churches", Status: PageMerged,
Sections: "- type: hero\n title: Run your church on one bill\n", MergedAt: now.Add(-time.Hour)}
status = 404
CheckLive(context.Background(), f, p, now)
if !p.LiveAt.IsZero() || p.LiveNote != "Not on the site yet (it answers 404)." || !p.CheckedAt.Equal(now) || !p.Waiting() || p.Late(now) {
t.Errorf("404: %+v", p)
}
status = 200
page = `
Old page · UsSomething else
`
CheckLive(context.Background(), f, p, now)
if !p.LiveAt.IsZero() || !strings.Contains(p.LiveNote, `Something else is at this address (its title is "Old page · Us")`) {
t.Errorf("other page: %s", p.LiveNote)
}
page = `For churches · UsFor churches
An older version.
`
CheckLive(context.Background(), f, p, now)
if !p.LiveAt.IsZero() || !strings.Contains(p.LiveNote, "not this version's words yet") {
t.Errorf("old version: %s", p.LiveNote)
}
if !p.Late(now.Add(2 * time.Hour)) {
t.Error("not late after three hours")
}
page = `ChurchesRun your church on one bill
`
CheckLive(context.Background(), f, p, now)
if !p.LiveAt.Equal(now) || p.LiveNote != `Live, but its title shows as "Churches".` || p.Waiting() {
t.Errorf("live, other title: %+v", p)
}
p.LiveAt = time.Time{}
page = `For churches · UsRun your church on one bill
`
CheckLive(context.Background(), f, p, now)
if !p.LiveAt.Equal(now) || p.LiveNote != "Live: the title and the words match." {
t.Errorf("live: %+v", p)
}
// Removed pages must be gone.
p.Status = PageRemoved
CheckLive(context.Background(), f, p, now)
if !p.GoneAt.IsZero() || p.LiveNote != "Still on the site (it answers 200)." {
t.Errorf("still there: %s", p.LiveNote)
}
status = 410
CheckLive(context.Background(), f, p, now)
if !p.GoneAt.Equal(now) || p.Waiting() {
t.Errorf("gone: %+v", p)
}
p.GoneAt, p.Path, status = time.Time{}, "/moved/", 200
CheckLive(context.Background(), f, p, now)
if p.GoneAt.IsZero() || !strings.Contains(p.LiveNote, "/elsewhere/") {
t.Errorf("redirected: %s", p.LiveNote)
}
}
type fakePulls map[int64]*gitea.Pull
func (f fakePulls) GetPull(_ context.Context, full string, n int64) (*gitea.Pull, error) {
if full != "Us/site" {
return nil, fmt.Errorf("repo %s", full)
}
return f[n], nil
}
func TestRefresh(t *testing.T) {
d := openDB(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `AHello there
`)
}))
defer srv.Close()
at := time.Date(2026, 11, 22, 9, 0, 0, 0, time.UTC)
mk := func(path, status string, pr int64) *SitePage {
p := &SitePage{Site: srv.URL + "/", Repo: "Us/site", Kind: "landing", Path: path, Title: "A", Status: status, PRNumber: pr,
Sections: "- type: hero\n title: Hello there\n"}
if err := SavePage(d, p); err != nil {
t.Fatal(err)
}
return p
}
open := mk("/open/", PagePR, 1)
merging := mk("/merging/", PagePR, 2)
closing := mk("/closing/", PagePR, 3)
draft := mk("/draft/", PageDraft, 0)
g := fakePulls{1: {State: "open"}, 2: {State: "closed", Merged: true, MergedAt: &at}, 3: {State: "closed"}}
now := at.Add(time.Hour)
if err := Refresh(context.Background(), d, g, seo.NewFetcher("test"), now); err != nil {
t.Fatal(err)
}
for _, c := range []struct {
p *SitePage
status string
live bool
}{{open, PagePR, false}, {merging, PageMerged, true}, {closing, PageClosed, false}, {draft, PageDraft, false}} {
got, _ := GetPage(d, c.p.ID)
if got.Status != c.status || got.LiveAt.IsZero() == c.live {
t.Errorf("%s: %s live=%v", c.p.Path, got.Status, !got.LiveAt.IsZero())
}
}
got, _ := GetPage(d, merging.ID)
if !got.MergedAt.Equal(at) || got.LiveNote != "Live: the title and the words match." {
t.Errorf("merged: %+v", got)
}
// Live and gone addresses are for IndexNow; seo picks them up too.
addrs, _ := LiveAddresses(d, srv.URL+"/", at)
if len(addrs) != 1 || addrs[0] != srv.URL+"/merging/" {
t.Errorf("addresses: %v", addrs)
}
newly, err := seo.NewlyLive(d, srv.URL+"/", now)
if err != nil || len(newly) != 1 || newly[0] != srv.URL+"/merging/" {
t.Errorf("newly live: %v %v", newly, err)
}
}