Files
khosra/internal/web/slug_test.go
T
Claude Opus 5andbdeshi 9100ce4876 notice content changes and rebuild without a restart
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.
2026-07-31 14:00:53 +06:00

157 lines
6.4 KiB
Go

package web
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/fstest"
"khosra/internal/content"
"khosra/internal/render"
)
func slugHandler(t *testing.T, fsys fstest.MapFS) http.Handler {
t.Helper()
bundles, err := content.Scan(fsys)
if err != nil {
t.Fatal(err)
}
r, err := render.New(nil, content.Settings{}, nil)
if err != nil {
t.Fatal(err)
}
return Handler(Fixed(content.NewSite(bundles)), r, fsys, nil, content.Settings{})
}
func TestASlugRenamesTheAddressInEveryLanguage(t *testing.T) {
h := slugHandler(t, fstest.MapFS{
"content/posts/hello-world.md": {Data: []byte("---\ntitle: Hello\nslug: ekti-post\n---\nEnglish.\n")},
"content/posts/hello-world.bn.md": {Data: []byte("---\ntitle: একটি পোস্ট\n---\nবাংলা।\n")},
})
for _, path := range []string{"/posts/ekti-post/", "/bn/posts/ekti-post/"} {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
if rec.Code != http.StatusOK {
t.Errorf("GET %s = %d, want 200 — a slug renames every language (ADR-0035)", path, rec.Code)
}
}
// The old path is gone: the engine serves the new one only, and an author who wants both writes an alias.
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/posts/hello-world/", nil))
if rec.Code != http.StatusNotFound {
t.Errorf("the derived path = %d, want 404", rec.Code)
}
// Canonical and hreflang must name the new address, not the key.
rec = httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/bn/posts/ekti-post/", nil))
body := rec.Body.String()
if !strings.Contains(body, `rel="canonical" href="/bn/posts/ekti-post/"`) {
t.Errorf("canonical should name the served address:\n%s", body)
}
if strings.Contains(body, "hello-world") {
t.Errorf("nothing the engine emits should still say hello-world:\n%s", body)
}
}
func TestAnAliasCanKeepTheOldPathWorking(t *testing.T) {
h := slugHandler(t, fstest.MapFS{
"content/posts/hello-world.md": {Data: []byte("---\ntitle: Hello\nslug: ekti-post\naliases: [posts/hello-world]\n---\nx\n")},
})
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/posts/hello-world/", nil))
if rec.Code != http.StatusMovedPermanently {
t.Fatalf("got %d, want 301 — this is how a rename keeps its promise (ADR-0008)", rec.Code)
}
if loc := rec.Header().Get("Location"); loc != "/posts/ekti-post/" {
t.Errorf("Location = %q, want the new address", loc)
}
}
func TestListingsAndSitemapsUseTheSluggedAddress(t *testing.T) {
fsys := fstest.MapFS{
"content/posts/hello-world.md": {Data: []byte("---\ntitle: Hello\ndate: 2026-03-01\nslug: ekti-post\n---\nx\n")},
"content/posts/plain.md": {Data: []byte("---\ntitle: Plain\ndate: 2026-02-01\n---\nx\n")},
}
bundles, err := content.Scan(fsys)
if err != nil {
t.Fatal(err)
}
settings := content.Settings{Base: "https://khosra.example"}
r, err := render.New(nil, settings, nil)
if err != nil {
t.Fatal(err)
}
h := Handler(Fixed(content.NewSite(bundles)), r, fsys, nil, settings)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/posts/", nil))
if body := rec.Body.String(); !strings.Contains(body, `href="/posts/ekti-post/"`) || strings.Contains(body, "hello-world") {
t.Errorf("a listing must link the address, not the key:\n%s", body)
}
rec = httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/sitemap.xml", nil))
if body := rec.Body.String(); !strings.Contains(body, "/posts/ekti-post/") || strings.Contains(body, "hello-world") {
t.Errorf("a sitemap that disagrees with what is served is worse than none:\n%s", body)
}
}
func TestAmbiguousOrCollidingSlugsAreDropped(t *testing.T) {
// Both cases keep the derived path rather than picking a winner, the same rule aliases follow.
h := slugHandler(t, fstest.MapFS{
// Variants disagree.
"content/posts/a.md": {Data: []byte("---\ntitle: A\nslug: one\n---\nx\n")},
"content/posts/a.bn.md": {Data: []byte("---\ntitle: ক\nslug: two\n---\nx\n")},
// Slug lands on a bundle that already exists.
"content/posts/b.md": {Data: []byte("---\ntitle: B\nslug: taken\n---\nx\n")},
"content/posts/taken.md": {Data: []byte("---\ntitle: Taken\n---\nx\n")},
})
for path, want := range map[string]int{
"/posts/a/": http.StatusOK, // kept its derived path
"/posts/one/": http.StatusNotFound, // neither declared slug won
"/posts/two/": http.StatusNotFound,
"/posts/b/": http.StatusOK,
"/posts/taken/": http.StatusOK, // the bundle already there keeps its URL
} {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
if rec.Code != want {
t.Errorf("GET %s = %d, want %d", path, rec.Code, want)
}
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/posts/taken/", nil))
if body := rec.Body.String(); !strings.Contains(body, "<h1>Taken</h1>") {
t.Errorf("the bundle already there must still be the one served:\n%s", body)
}
}
func TestASeriesSurvivesItsLandingBeingSlugged(t *testing.T) {
// The reason a slug does not touch the key: membership is the directory (ADR-0033), so renaming the
// landing page's address must not orphan its chapters.
h := slugHandler(t, fstest.MapFS{
"content/comics/the-long-monsoon/_index.md": {Data: []byte("---\ntitle: The Long Monsoon\nslug: monsoon\n---\nx\n")},
"content/comics/the-long-monsoon/first-rain.md": {Data: []byte("---\ntitle: First Rain\norder: 10\n---\nx\n")},
"content/comics/the-long-monsoon/the-flood.md": {Data: []byte("---\ntitle: The Flood\norder: 20\n---\nx\n")},
})
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/comics/monsoon/", nil))
if rec.Code != http.StatusOK {
t.Fatalf("the landing page = %d, want 200 at its new address", rec.Code)
}
body := rec.Body.String()
for _, want := range []string{"First Rain", "The Flood"} {
if !strings.Contains(body, want) {
t.Errorf("the archive lost %q — a slug must not orphan a series:\n%s", want, body)
}
}
rec = httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/comics/the-long-monsoon/the-flood/", nil))
if rec.Code != http.StatusOK {
t.Errorf("a chapter = %d, want 200: chapters keep their own addresses", rec.Code)
}
if body := rec.Body.String(); !strings.Contains(body, `href="/comics/monsoon/"`) {
t.Errorf("the chapter should link its series at the slugged address:\n%s", body)
}
}