weiro/providers/sitebuilder/builder_test.go
2026-02-18 22:07:18 +11:00

68 lines
1.7 KiB
Go

package sitebuilder_test
import (
"os"
"path/filepath"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"lmika.dev/lmika/weiro/models"
"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}}">{{.Meta.Title}}</a>,{{ end }}`)},
"layout_main.html": {Data: []byte(`{{ .Body }}`)},
}
site := models.Site{
Meta: models.SiteMeta{
BaseURL: "https://example.com",
},
Posts: []*models.Post{
{
Meta: models.PostMeta{
Title: "Test Post",
Slug: "/2026/02/18/test-post",
},
Content: "This is a test post",
},
{
Meta: models.PostMeta{
Title: "Another Post",
Slug: "/2026/02/20/another-post",
},
Content: "This is **another** test post",
},
},
}
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))
}
})
}