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}}{{.Post.Title}},{{ end }}`)}, "layout_main.html": {Data: []byte(`{{ .Body }}`)}, "categories_list.html": {Data: []byte(`{{ range .Categories}}{{.Name}},{{ end }}`)}, "categories_single.html": {Data: []byte(`

{{.Category.Name}}

`)}, "pages_single.html": {Data: []byte(`{{ if .Page.Title }}

{{ .Page.Title }}

{{ end }}{{ .HTML }}`)}, } 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{ Site: models.Site{PostsPerPage: 10}, 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 } } } }, Pages: []*models.Page{ {Title: "About", Slug: "about", Body: "About this site"}, }, } wantFiles := map[string]string{ "2026/02/18/test-post/index.html": "

This is a test post

\n", "2026/02/20/another-post/index.html": "

This is another test post

\n", "index.html": "Test Post,Another Post,", "about/index.html": "

About

About this site

\n", } 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)) } }) }