weiro/providers/sitebuilder/builder_test.go

63 lines
1.7 KiB
Go
Raw Permalink Normal View History

2026-02-18 11:07:18 +00:00
package sitebuilder_test
import (
"os"
"path/filepath"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"lmika.dev/lmika/weiro/models"
2026-02-19 10:21:27 +00:00
"lmika.dev/lmika/weiro/models/pubmodel"
2026-02-18 11:07:18 +00:00
"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 }}`)},
2026-02-19 11:29:44 +00:00
"posts_list.html": {Data: []byte(`{{ range .Posts}}<a href="{{url_abs .Path}}">{{.Post.Title}}</a>,{{ end }}`)},
2026-02-18 11:07:18 +00:00
"layout_main.html": {Data: []byte(`{{ .Body }}`)},
}
2026-02-19 10:21:27 +00:00
site := pubmodel.Site{
BaseURL: "https://example.com",
2026-02-18 11:07:18 +00:00
Posts: []*models.Post{
{
2026-02-19 10:21:27 +00:00
Title: "Test Post",
Slug: "/2026/02/18/test-post",
Body: "This is a test post",
2026-02-18 11:07:18 +00:00
},
{
2026-02-19 10:21:27 +00:00
Title: "Another Post",
Slug: "/2026/02/20/another-post",
Body: "This is **another** test post",
2026-02-18 11:07:18 +00:00
},
},
}
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))
}
})
2026-02-19 11:29:44 +00:00
}