Extend the publishing pipeline to generate category index pages, per-category archive pages, per-category RSS/JSON feeds, and display categories on individual post pages and post lists. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package sitebuilder_test
|
|
|
|
import (
|
|
"context"
|
|
"iter"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"lmika.dev/lmika/weiro/models"
|
|
"lmika.dev/lmika/weiro/models/pubmodel"
|
|
"lmika.dev/lmika/weiro/providers/sitebuilder"
|
|
)
|
|
|
|
func TestBuilder_BuildSite(t *testing.T) {
|
|
t.Run("build site", func(t *testing.T) {
|
|
tmpls := fstest.MapFS{
|
|
"posts_single.html": {Data: []byte(`{{ .HTML }}`)},
|
|
"posts_list.html": {Data: []byte(`{{ range .Posts}}<a href="{{url_abs .Path}}">{{.Post.Title}}</a>,{{ end }}`)},
|
|
"layout_main.html": {Data: []byte(`{{ .Body }}`)},
|
|
"categories_list.html": {Data: []byte(`{{ range .Categories}}<a href="{{url_abs .Path}}">{{.Name}}</a>,{{ end }}`)},
|
|
"categories_single.html": {Data: []byte(`<h2>{{.Category.Name}}</h2>`)},
|
|
}
|
|
|
|
posts := []*models.Post{
|
|
{
|
|
Title: "Test Post",
|
|
Slug: "/2026/02/18/test-post",
|
|
Body: "This is a test post",
|
|
},
|
|
{
|
|
Title: "Another Post",
|
|
Slug: "/2026/02/20/another-post",
|
|
Body: "This is **another** test post",
|
|
},
|
|
}
|
|
|
|
site := pubmodel.Site{
|
|
BaseURL: "https://example.com",
|
|
PostIter: func(ctx context.Context) iter.Seq[models.Maybe[*models.Post]] {
|
|
return func(yield func(models.Maybe[*models.Post]) bool) {
|
|
for _, p := range posts {
|
|
if !yield(models.Maybe[*models.Post]{Value: p}) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}
|
|
wantFiles := map[string]string{
|
|
"2026/02/18/test-post/index.html": "<p>This is a test post</p>\n",
|
|
"2026/02/20/another-post/index.html": "<p>This is <strong>another</strong> test post</p>\n",
|
|
"index.html": "<a href=\"https://example.com/2026/02/18/test-post\">Test Post</a>,<a href=\"https://example.com/2026/02/20/another-post\">Another Post</a>,",
|
|
}
|
|
|
|
outDir := t.TempDir()
|
|
|
|
b, err := sitebuilder.New(site, sitebuilder.Options{
|
|
TemplatesFS: tmpls,
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
err = b.BuildSite(outDir)
|
|
assert.NoError(t, err)
|
|
|
|
for file, content := range wantFiles {
|
|
filePath := filepath.Join(outDir, file)
|
|
fileContent, err := os.ReadFile(filePath)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, content, string(fileContent))
|
|
}
|
|
})
|
|
}
|