Files
sm-sitemap-plugin/sitemap_index_test.go
Jakub Zych 3359f1a196 feat: sitemap index and per-type files from a slot table (tracer)
- contract package: Filter, Listing and their types (D-95)
- golem15_sitemap_urls slot table and the new settings columns
- Rebuild over signpost Records, stable slot assignment, rebuild job
- GET /sitemap.xml index and GET /_sitemap/files/{file} from the table
2026-11-04 10:58:05 +01:00

134 lines
5.1 KiB
Go

package sitemap_test
import (
"context"
"net/http"
"regexp"
"strings"
"testing"
"time"
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
"git.golem15.com/golem15/summercms/modules/pact"
)
// bootIndex boots the fake content plugin and the sitemap on a fresh
// database with an empty content store.
func bootIndex(t *testing.T, extra ...string) *sitemapEnv {
t.Helper()
contentStore.reset(t)
return bootPlugins(t, append([]string{contentPluginID, "golem15.sitemap"}, extra...))
}
// rebuild runs the plugin's rebuild job once, as a worker would.
func (e *sitemapEnv) rebuild(t *testing.T) {
t.Helper()
for _, p := range e.plugins {
hj, ok := p.(pact.HasJobs)
if !ok || p.ID() != "golem15.sitemap" {
continue
}
for _, j := range hj.Jobs() {
if err := j.Work(context.Background(), classes.RebuildArgs{}); err != nil {
t.Fatalf("rebuild job: %v", err)
}
return
}
}
t.Fatal("the sitemap registers no rebuild job")
}
// setFileSize stores the file size setting.
func (e *sitemapEnv) setFileSize(t *testing.T, n int) {
t.Helper()
if err := e.gdb.Exec(`UPDATE golem15_sitemap_settings SET file_size = ? WHERE id = 1`, n).Error; err != nil {
t.Fatal(err)
}
}
// locs lists the <loc> values of a sitemap body in order.
func locs(body string) []string {
var out []string
for _, m := range regexp.MustCompile(`<loc>([^<]*)</loc>`).FindAllStringSubmatch(body, -1) {
out = append(out, m[1])
}
return out
}
// TestSitemapIndex is the tracer of D-30: a content type's published
// records appear, after a rebuild job, in an index at /sitemap.xml that
// links per-group files of file_size addresses with absolute <loc> and a
// real <lastmod>; a draft is absent, no priority or change frequency is
// written, values are XML-escaped (T-33-09) and an unknown file is a 404.
func TestSitemapIndex(t *testing.T) {
e := bootIndex(t)
e.setFileSize(t, 2)
base := time.Date(2026, 10, 1, 8, 0, 0, 0, time.UTC)
for i, slug := range []string{"a", "b", "c", "d"} {
contentStore.put(pageType, page1(i+1, slug, base.Add(time.Duration(i)*time.Hour)))
}
contentStore.put(pageType, fakeRecord{ID: 5, Published: true, LastMod: base, Locales: []pact.ContentLocale{{Locale: "en", URL: `/pages/fish&chips?x=<1>"`, Title: `Fish & "chips" <b>`}}})
contentStore.put(pageType, fakeRecord{ID: 6, Published: false, LastMod: base, Locales: []pact.ContentLocale{{Locale: "en", URL: "/pages/draft", Title: "Draft"}}})
if rec := e.get(t, "/sitemap.xml"); rec.Code != http.StatusOK || len(locs(rec.Body.String())) != 0 {
t.Fatalf("index before the first rebuild: %d %s", rec.Code, rec.Body.String())
}
e.rebuild(t)
rec := e.get(t, "/sitemap.xml")
body := rec.Body.String()
if rec.Code != http.StatusOK || rec.Header().Get("Content-Type") != "application/xml" {
t.Fatalf("GET /sitemap.xml: %d %q %s", rec.Code, rec.Header().Get("Content-Type"), body)
}
if !strings.HasPrefix(body, `<?xml version="1.0" encoding="UTF-8"?>`) || !strings.Contains(body, `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`) {
t.Fatalf("index head:\n%s", body)
}
want := []string{
"https://api.example.test/sitemap-pages-1.xml",
"https://api.example.test/sitemap-pages-2.xml",
"https://api.example.test/sitemap-pages-3.xml",
}
if got := locs(body); strings.Join(got, " ") != strings.Join(want, " ") {
t.Fatalf("index locs = %v, want %v\n%s", got, want, body)
}
if n := strings.Count(body, "<lastmod>"); n != 3 {
t.Fatalf("index has %d lastmod elements:\n%s", n, body)
}
if !strings.Contains(body, "<sitemap><loc>https://api.example.test/sitemap-pages-1.xml</loc><lastmod>2026-10-01T09:00:00Z</lastmod></sitemap>") {
t.Fatalf("file 1 lastmod is not the newest of its addresses:\n%s", body)
}
rec = e.get(t, "/_sitemap/files/sitemap-pages-1.xml")
file := rec.Body.String()
if rec.Code != http.StatusOK || rec.Header().Get("Content-Type") != "application/xml" || rec.Header().Get("X-Robots-Tag") != "noindex" {
t.Fatalf("file 1: %d %v %s", rec.Code, rec.Header(), file)
}
if got := locs(file); strings.Join(got, " ") != "https://api.example.test/pages/a https://api.example.test/pages/b" {
t.Fatalf("file 1 locs = %v\n%s", got, file)
}
if !strings.Contains(file, "<url><loc>https://api.example.test/pages/a</loc><lastmod>2026-10-01T08:00:00Z</lastmod></url>") {
t.Fatalf("file 1 lacks a real lastmod:\n%s", file)
}
file3 := e.get(t, "/_sitemap/files/sitemap-pages-3.xml").Body.String()
if !strings.Contains(file3, "<loc>https://api.example.test/pages/fish&amp;chips?x=&lt;1&gt;&#34;</loc>") {
t.Fatalf("file 3 does not escape the address:\n%s", file3)
}
all := body + file + e.get(t, "/_sitemap/files/sitemap-pages-2.xml").Body.String() + file3
for _, banned := range []string{"<priority>", "<changefreq>", "/pages/draft", "xml-stylesheet", "xmlns:xhtml"} {
if strings.Contains(all, banned) {
t.Errorf("the sitemap contains %q:\n%s", banned, all)
}
}
for _, path := range []string{
"/_sitemap/files/sitemap-pages-4.xml",
"/_sitemap/files/sitemap-posts-1.xml",
"/_sitemap/files/sitemap-pages-0.xml",
"/_sitemap/files/other.xml",
"/sitemap.xsl",
} {
if rec := e.get(t, path); rec.Code != http.StatusNotFound {
t.Errorf("GET %s: %d, want 404", path, rec.Code)
}
}
}