diff options
Diffstat (limited to 'internal/web/web_test.go')
| -rw-r--r-- | internal/web/web_test.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/internal/web/web_test.go b/internal/web/web_test.go index aa9b78e..a13697c 100644 --- a/internal/web/web_test.go +++ b/internal/web/web_test.go @@ -635,3 +635,48 @@ func TestNormalizeSection(t *testing.T) { t.Errorf("half-valid placement: %+v", sec) } } + +func TestPostSummary(t *testing.T) { + md := func(s string) *store.Post { + return &store.Post{Format: store.FormatMarkdown, BodyMD: s, BodyHTML: renderBody(store.FormatMarkdown, s)} + } + // the marker wins, whatever the length + p := md("first\n\n<!--more-->\n\nsecond") + if html, more := postSummary(p); !more || !strings.Contains(html, "first") || strings.Contains(html, "second") { + t.Errorf("marker: got %q, %v", html, more) + } + // short posts are shown whole + if html, more := postSummary(md("just a line")); more || html != p.BodyHTML && !strings.Contains(html, "just a line") { + t.Errorf("short: got %q, %v", html, more) + } + // a long one is cut at a block boundary after summaryWords words + long := strings.Repeat("word ", 50) + "\n\n" + strings.Repeat("word ", 50) + "\n\nlast block" + if html, more := postSummary(md(long)); !more || strings.Contains(html, "last block") || strings.Count(html, "word") != 100 { + t.Errorf("auto cut: got %d words, more=%v", strings.Count(html, "word"), more) + } + // never inside a code fence + fenced := "intro\n\n```\n" + strings.Repeat("code ", 80) + "\n\nstill code\n```\n\nafter" + if html, more := postSummary(md(fenced)); !more || !strings.Contains(html, "still code") || strings.Contains(html, "after") { + t.Errorf("fence: got %q, %v", html, more) + } + // HTML posts are only cut at the marker + h := &store.Post{Format: store.FormatHTML, BodyMD: "<div>" + strings.Repeat("w ", 200) + "</div>"} + h.BodyHTML = h.BodyMD + if html, more := postSummary(h); more || html != h.BodyHTML { + t.Errorf("html: cut without a marker") + } + h.BodyMD = "<p>a</p><!--more--><p>b</p>" + if html, more := postSummary(h); !more || html != "<p>a</p>" { + t.Errorf("html marker: got %q, %v", html, more) + } + // fillExcerpts follows the theme + posts := []store.Post{*md(long)} + fillExcerpts(Theme{ListStyle: "full"}, posts) + if posts[0].HasMore || posts[0].Excerpt != posts[0].BodyHTML { + t.Error("full: excerpt should be the whole body") + } + fillExcerpts(Theme{ListStyle: "summary"}, posts) + if !posts[0].HasMore { + t.Error("summary: expected a cut") + } +} |
