Polling lives in internal/ext/watch, per the human's call to keep core under its ceiling rather than raise it a second time — which is what ADR-0041 said a second raise would mean. It is a poller, deletable without trace, and core stayed at 2671/2800. A settled change calls the same `rebuilder` that startup calls, because a reload path that differs from the startup path is a reload path that drifts. The index is an atomic.Pointer swapped whole, so a request reads the site that was current when it arrived instead of one being rebuilt underneath it — the alternative, mutating in place, is a data race with every in-flight request. Names, sizes and modification times, not contents: reading every file to detect a change costs more than the rebuild it triggers. Editor droppings are excluded, because saving in vim writes a swap file, a backup and the number 4913, and each would otherwise look like a change. A change must hold still for a moment first, since one save is often several operations. Verified against the running binary: a page 404s, the file appears, and five seconds later it serves — one "site root changed" in the log. Then three droppings written at once produced no rebuild at all. Two warnings fired and were fixed rather than silenced: `runServe` gave up the rebuild closure to `rebuilder`, and the fingerprint walk gave up its body to `record`, where three exclusions read as a list instead of as nesting. The Dockerfile ships the binary alone. The site root arrives as a volume and is never copied in — it is somebody's content repository with its own history (ADR-0011), so the image is the same for every site.
102 lines
3.7 KiB
Go
102 lines
3.7 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"khosra/internal/content"
|
|
"khosra/internal/render"
|
|
)
|
|
|
|
func crawlerHandler(t *testing.T, settings content.Settings, extra fstest.MapFS) http.Handler {
|
|
t.Helper()
|
|
fsys := fstest.MapFS{
|
|
"content/posts/hello.md": {Data: []byte("---\ntitle: Hello\ndate: 2026-03-08\n---\nx\n")},
|
|
"content/posts/hello.bn.md": {Data: []byte("---\ntitle: হ্যালো\ndate: 2026-03-08\n---\nx\n")},
|
|
"content/pages/about.md": {Data: []byte("---\ntitle: About\n---\nx\n")},
|
|
}
|
|
for name, file := range extra {
|
|
fsys[name] = file
|
|
}
|
|
bundles, err := content.Scan(fsys)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := render.New(nil, settings, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return Handler(Fixed(content.NewSite(bundles)), r, fsys, nil, settings)
|
|
}
|
|
|
|
func TestSitemapListsEveryVariantAbsolutely(t *testing.T) {
|
|
h := crawlerHandler(t, content.Settings{Base: "https://khosra.example"}, nil)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/sitemap.xml", nil))
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("got %d, want 200", rec.Code)
|
|
}
|
|
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/xml") {
|
|
t.Errorf("content-type = %q — a sitemap served as HTML is a sitemap nothing reads", ct)
|
|
}
|
|
body := rec.Body.String()
|
|
for _, want := range []string{
|
|
"<loc>https://khosra.example/posts/hello/</loc>",
|
|
"<loc>https://khosra.example/bn/posts/hello/</loc>", // each language is its own URL
|
|
"<loc>https://khosra.example/pages/about/</loc>",
|
|
"<lastmod>2026-03-08</lastmod>",
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("missing %q:\n%s", want, body)
|
|
}
|
|
}
|
|
if strings.Contains(body, "<lastmod></lastmod>") {
|
|
t.Error("an undated bundle should carry no lastmod at all")
|
|
}
|
|
}
|
|
|
|
func TestNoBaseMeansNoSitemap(t *testing.T) {
|
|
// The format has no room for a relative URL, so the honest answer is that the file does not exist
|
|
// (ADR-0039) rather than one full of paths no crawler can resolve.
|
|
h := crawlerHandler(t, content.Settings{}, nil)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/sitemap.xml", nil))
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Errorf("got %d, want 404", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestRobotsIsGeneratedOrTheSitesOwn(t *testing.T) {
|
|
h := crawlerHandler(t, content.Settings{Base: "https://khosra.example"}, nil)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/robots.txt", nil))
|
|
body := rec.Body.String()
|
|
if !strings.Contains(body, "User-agent: *") || !strings.Contains(body, "Sitemap: https://khosra.example/sitemap.xml") {
|
|
t.Errorf("generated robots should point at the sitemap:\n%s", body)
|
|
}
|
|
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
|
|
t.Errorf("content-type = %q", ct)
|
|
}
|
|
|
|
// A site that ships its own has said something deliberate.
|
|
h = crawlerHandler(t, content.Settings{Base: "https://khosra.example"},
|
|
fstest.MapFS{"robots.txt": {Data: []byte("User-agent: *\nDisallow: /drafts/\n")}})
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/robots.txt", nil))
|
|
if got := rec.Body.String(); !strings.Contains(got, "Disallow: /drafts/") || strings.Contains(got, "Sitemap:") {
|
|
t.Errorf("the site's own robots.txt should be served verbatim:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func TestRobotsWithoutABaseOmitsTheSitemapLine(t *testing.T) {
|
|
h := crawlerHandler(t, content.Settings{}, nil)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/robots.txt", nil))
|
|
if got := rec.Body.String(); strings.Contains(got, "Sitemap:") {
|
|
t.Errorf("a relative sitemap reference is not something a crawler accepts:\n%s", got)
|
|
}
|
|
}
|