package gnoblog
import (
"std"
"strings"
"testing"
)
func TestPackage(t *testing.T) {
testing.SetOriginCaller(std.Address("g1manfred47kzduec920z88wfr64ylksmdcedlf5"))
// by default, no posts.
{
got := Render("")
expected := `
# gno.land's blog
No posts.
`
assertMDEquals(t, got, expected)
}
// create two posts, list post.
{
ModAddPost("slug1", "title1", "body1", "2022-05-20T13:17:22Z", "moul", "tag1,tag2")
ModAddPost("slug2", "title2", "body2", "2022-05-20T13:17:23Z", "moul", "tag1,tag3")
got := Render("")
expected := `
# gno.land's blog
### [title2](/r/gnoland/blog:p/slug2)
20 May 2022
|||
### [title1](/r/gnoland/blog:p/slug1)
20 May 2022
`
assertMDEquals(t, got, expected)
}
// view post.
{
got := Render("p/slug2")
expected := `
# title2
body2
---
Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
Written by moul on 20 May 2022
Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
---
Comment section
`
assertMDEquals(t, got, expected)
}
// list by tags.
{
got := Render("t/invalid")
expected := "# [gno.land's blog](/r/gnoland/blog:) / t / invalid\n\nNo posts."
assertMDEquals(t, got, expected)
got = Render("t/tag2")
expected = `
# [gno.land's blog](/r/gnoland/blog:) / t / tag2
### [title1](/r/gnoland/blog:p/slug1)
20 May 2022
`
assertMDEquals(t, got, expected)
}
// add comments.
{
AddComment("slug1", "comment1")
AddComment("slug2", "comment2")
AddComment("slug1", "comment3")
AddComment("slug2", "comment4")
AddComment("slug1", "comment5")
got := Render("p/slug2")
expected := `
# title2
body2
---
Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
Written by moul on 20 May 2022
Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
---
Comment section
comment4
by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC
---
comment2
by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC
---
`
assertMDEquals(t, got, expected)
}
// edit post.
{
oldTitle := "title2"
oldDate := "2022-05-20T13:17:23Z"
ModEditPost("slug2", oldTitle, "body2++", oldDate, "manfred", "tag1,tag4")
got := Render("p/slug2")
expected := `
# title2
body2++
---
Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag4](/r/gnoland/blog:t/tag4)
Written by manfred on 20 May 2022
Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
---
Comment section
comment4
by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC
---
comment2
by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC
---
`
assertMDEquals(t, got, expected)
home := Render("")
if strings.Count(home, oldTitle) != 1 {
t.Errorf("post not edited properly")
}
// Edits work everything except title, slug, and publicationDate
// Edits to the above will cause duplication on the blog home page
}
//
{ // Test remove functionality
title := "example title"
slug := "testSlug1"
ModAddPost(slug, title, "body1", "2022-05-25T13:17:22Z", "moul", "tag1,tag2")
got := Render("")
if !strings.Contains(got, title) {
t.Errorf("post was not added properly")
}
postRender := Render("p/" + slug)
if !strings.Contains(postRender, title) {
t.Errorf("post not rendered properly")
}
ModRemovePost(slug)
got = Render("")
if strings.Contains(got, title) {
t.Errorf("post was not removed")
}
postRender = Render("p/" + slug)
assertMDEquals(t, postRender, "404")
}
//
// // TODO: pagination.
// // TODO: ?format=...
//
// all 404s
{
notFoundPaths := []string{
"p/slug3",
"p",
"p/",
"x/x",
"t",
"t/",
"/",
"p/slug1/",
}
for _, notFoundPath := range notFoundPaths {
got := Render(notFoundPath)
expected := "404"
if got != expected {
t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got)
}
}
}
}
func assertMDEquals(t *testing.T, got, expected string) {
t.Helper()
expected = strings.TrimSpace(expected)
got = strings.TrimSpace(got)
if expected != got {
t.Errorf("invalid render output.\nexpected %q.\ngot %q.", expected, got)
}
}