Membership is a publication date, not a type declaration (ADR-0043). The parked
feed shape said "every type declared primary", which would have made feeds wait on
declared types a third time — but the thing that distinguishes a feed item is
already on disk. Pages and section landings drop out because they have no date,
which is the right reason. The parked idea stays parked with a sharper trigger:
someone wanting a *dated* bundle kept out.
Built with encoding/xml from typed structs, never a template: XML in html/template
is escaping for the wrong grammar, and that is a correctness trap rather than a
matter of taste.
A bug the evidence found, older than feeds: content.URL("", lang) built "//", so a
whole-site feed's id and alternate link were https://khosra.example// — every
entry identity wrong in every reader. The root is "/" now, with a test, and the
hand-built "/" the resolver carried for the same reason can follow later.
Two counters re-scoped rather than incremented, the same way transforms was:
Views now counts *per-bundle selection* — the thing architecture.md means by the
View layer, still at zero consumers. Output formats are not it: HTML, sitemap XML
and Atom are three functions with nothing to share, so an interface over them
would have one member and no leverage.
Effects stays at 1. A feed is generated per request like the sitemap, so it is not
a second Effect and the runner is not yet due — the next thing that writes files
off the request path is.
155 lines
5.6 KiB
Go
155 lines
5.6 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"khosra/internal/content"
|
|
"khosra/internal/render"
|
|
)
|
|
|
|
func feedHandler(t *testing.T, settings content.Settings) http.Handler {
|
|
t.Helper()
|
|
fsys := fstest.MapFS{
|
|
"content/posts/newer.md": {Data: []byte("---\ntitle: Newer\ndate: 2026-03-08\ntags: [monsoon]\n---\nx\n")},
|
|
"content/posts/older.md": {Data: []byte("---\ntitle: Older\ndate: 2026-02-01\n---\nx\n")},
|
|
"content/posts/newer.bn.md": {Data: []byte("---\ntitle: নতুন\ndate: 2026-03-08\n---\nx\n")},
|
|
"content/comics/strip.md": {Data: []byte("---\ntitle: Strip\ndate: 2026-01-15\ntags: [monsoon]\n---\nx\n")},
|
|
"content/pages/colophon.md": {Data: []byte("---\ntitle: Colophon\n---\nno date at all\n")},
|
|
"content/posts/renamed.md": {Data: []byte("---\ntitle: Renamed\ndate: 2026-03-01\nslug: ekti\n---\nx\n")},
|
|
}
|
|
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(content.NewSite(bundles), r, fsys, nil, settings)
|
|
}
|
|
|
|
func fetchFeed(t *testing.T, h http.Handler, path string) (*httptest.ResponseRecorder, atom) {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
|
|
var doc atom
|
|
if rec.Code == http.StatusOK {
|
|
if err := xml.Unmarshal(rec.Body.Bytes(), &doc); err != nil {
|
|
t.Fatalf("GET %s did not produce parseable XML: %v\n%s", path, err, rec.Body.String())
|
|
}
|
|
}
|
|
return rec, doc
|
|
}
|
|
|
|
func TestTheFeedCarriesDatedBundlesNewestFirst(t *testing.T) {
|
|
h := feedHandler(t, content.Settings{Base: "https://khosra.example", Title: "Khosra"})
|
|
rec, doc := fetchFeed(t, h, "/feed.xml")
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("got %d, want 200", rec.Code)
|
|
}
|
|
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/atom+xml") {
|
|
t.Errorf("content-type = %q", ct)
|
|
}
|
|
var titles []string
|
|
for _, e := range doc.Entries {
|
|
titles = append(titles, e.Title)
|
|
}
|
|
want := []string{"Newer", "Renamed", "Older", "Strip"}
|
|
if strings.Join(titles, ",") != strings.Join(want, ",") {
|
|
t.Errorf("entries = %v, want %v (newest first, every section)", titles, want)
|
|
}
|
|
// Undated bundles are not feed items, and drop out because they have no date rather than by declaration.
|
|
if strings.Contains(rec.Body.String(), "Colophon") {
|
|
t.Error("an undated bundle must not appear in a feed")
|
|
}
|
|
// Identity has to mean something in a reader that has never seen the site.
|
|
for _, e := range doc.Entries {
|
|
if !strings.HasPrefix(e.ID, "https://khosra.example/") {
|
|
t.Errorf("entry id %q is not absolute", e.ID)
|
|
}
|
|
}
|
|
// A slugged bundle appears at its address, not its key.
|
|
if !strings.Contains(rec.Body.String(), "https://khosra.example/posts/ekti/") {
|
|
t.Errorf("a renamed bundle must be linked at its route:\n%s", rec.Body.String())
|
|
}
|
|
if doc.Title != "Khosra" {
|
|
t.Errorf("feed title = %q", doc.Title)
|
|
}
|
|
// Asserted on the bytes, because a namespaced attribute does not round-trip through the same struct tag
|
|
// that marshals it — and the bytes are what a reader sees.
|
|
body := rec.Body.String()
|
|
if !strings.Contains(body, `xml:lang="en"`) {
|
|
t.Errorf("the feed should declare its language:\n%s", body)
|
|
}
|
|
if strings.Contains(body, "example//") {
|
|
t.Errorf("a doubled slash makes every identity wrong:\n%s", body)
|
|
}
|
|
}
|
|
|
|
func TestFeedsNarrowBySectionTagAndLanguage(t *testing.T) {
|
|
h := feedHandler(t, content.Settings{Base: "https://khosra.example", Title: "Khosra"})
|
|
for path, want := range map[string][]string{
|
|
"/comics/feed.xml": {"Strip"},
|
|
"/tags/monsoon/feed.xml": {"Newer", "Strip"},
|
|
"/posts/tags/monsoon/feed.xml": {"Newer"},
|
|
"/bn/feed.xml": {"নতুন", "Renamed", "Older", "Strip"}, // bn where it exists, fallback elsewhere
|
|
} {
|
|
rec, doc := fetchFeed(t, h, path)
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("GET %s = %d, want 200", path, rec.Code)
|
|
continue
|
|
}
|
|
var titles []string
|
|
for _, e := range doc.Entries {
|
|
titles = append(titles, e.Title)
|
|
}
|
|
if strings.Join(titles, ",") != strings.Join(want, ",") {
|
|
t.Errorf("GET %s = %v, want %v", path, titles, want)
|
|
}
|
|
}
|
|
// The self link names the feed that was actually asked for.
|
|
_, doc := fetchFeed(t, h, "/comics/feed.xml")
|
|
var self string
|
|
for _, l := range doc.Links {
|
|
if l.Rel == "self" {
|
|
self = l.Href
|
|
}
|
|
}
|
|
if self != "https://khosra.example/comics/feed.xml" {
|
|
t.Errorf("self link = %q", self)
|
|
}
|
|
}
|
|
|
|
func TestAFeedNeedsABaseAndSomethingToCarry(t *testing.T) {
|
|
// Without a base an entry's identity would be a path no reader can resolve, so the file does not exist.
|
|
h := feedHandler(t, content.Settings{})
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/feed.xml", nil))
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Errorf("no base: got %d, want 404", rec.Code)
|
|
}
|
|
h = feedHandler(t, content.Settings{Base: "https://khosra.example"})
|
|
for _, path := range []string{"/pages/feed.xml", "/tags/nothing/feed.xml", "/nosuch/feed.xml"} {
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Errorf("GET %s = %d, want 404: a feed for nothing is nothing", path, rec.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAFeedPathIsAFileNotAPage(t *testing.T) {
|
|
// No trailing-slash canonicalisation: /feed.xml/ is not the feed, and /feed.xml must not redirect.
|
|
h := feedHandler(t, content.Settings{Base: "https://khosra.example"})
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/feed.xml", nil))
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("got %d, want 200 without a redirect", rec.Code)
|
|
}
|
|
}
|