gnoblog_test.gno

4.42 Kb ยท 245 lines
  1package gnoblog
  2
  3import (
  4	"std"
  5	"strings"
  6	"testing"
  7)
  8
  9func TestPackage(t *testing.T) {
 10	std.TestSetOriginCaller(std.Address("g1manfred47kzduec920z88wfr64ylksmdcedlf5"))
 11
 12	// by default, no posts.
 13	{
 14		got := Render("")
 15		expected := `
 16# gno.land's blog
 17
 18No posts.
 19`
 20		assertMDEquals(t, got, expected)
 21	}
 22
 23	// create two posts, list post.
 24	{
 25		ModAddPost("slug1", "title1", "body1", "2022-05-20T13:17:22Z", "moul", "tag1,tag2")
 26		ModAddPost("slug2", "title2", "body2", "2022-05-20T13:17:23Z", "moul", "tag1,tag3")
 27		got := Render("")
 28		expected := `
 29	# gno.land's blog
 30
 31<div class='columns-3'><div>
 32
 33### [title2](/r/gnoland/blog:p/slug2)
 34 20 May 2022
 35</div><div>
 36
 37### [title1](/r/gnoland/blog:p/slug1)
 38 20 May 2022
 39</div></div>
 40	`
 41		assertMDEquals(t, got, expected)
 42	}
 43
 44	// view post.
 45	{
 46		got := Render("p/slug2")
 47		expected := `
 48<main class='gno-tmpl-page'>
 49
 50# title2
 51
 52body2
 53
 54---
 55
 56Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
 57
 58Written by moul on 20 May 2022
 59
 60Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
 61
 62---
 63<details><summary>Comment section</summary>
 64
 65</details>
 66</main>
 67
 68	`
 69		assertMDEquals(t, got, expected)
 70	}
 71
 72	// list by tags.
 73	{
 74		got := Render("t/invalid")
 75		expected := "# [gno.land's blog](/r/gnoland/blog:) / t / invalid\n\nNo posts."
 76		assertMDEquals(t, got, expected)
 77
 78		got = Render("t/tag2")
 79		expected = `
 80# [gno.land's blog](/r/gnoland/blog:) / t / tag2
 81
 82<div>
 83
 84### [title1](/r/gnoland/blog:p/slug1)
 85 20 May 2022
 86</div>
 87	`
 88		assertMDEquals(t, got, expected)
 89	}
 90
 91	// add comments.
 92	{
 93		AddComment("slug1", "comment1")
 94		AddComment("slug2", "comment2")
 95		AddComment("slug1", "comment3")
 96		AddComment("slug2", "comment4")
 97		AddComment("slug1", "comment5")
 98		got := Render("p/slug2")
 99		expected := `<main class='gno-tmpl-page'>
100
101# title2
102
103body2
104
105---
106
107Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
108
109Written by moul on 20 May 2022
110
111Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
112
113---
114<details><summary>Comment section</summary>
115
116<h5>comment4
117
118</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
119
120---
121
122<h5>comment2
123
124</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
125
126---
127
128</details>
129</main>
130	`
131		assertMDEquals(t, got, expected)
132	}
133
134	// edit post.
135	{
136		oldTitle := "title2"
137		oldDate := "2022-05-20T13:17:23Z"
138
139		ModEditPost("slug2", oldTitle, "body2++", oldDate, "manfred", "tag1,tag4")
140		got := Render("p/slug2")
141		expected := `<main class='gno-tmpl-page'>
142
143# title2
144
145body2++
146
147---
148
149Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag4](/r/gnoland/blog:t/tag4)
150
151Written by manfred on 20 May 2022
152
153Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
154
155---
156<details><summary>Comment section</summary>
157
158<h5>comment4
159
160</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
161
162---
163
164<h5>comment2
165
166</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
167
168---
169
170</details>
171</main>
172	`
173		assertMDEquals(t, got, expected)
174
175		home := Render("")
176
177		if strings.Count(home, oldTitle) != 1 {
178			t.Errorf("post not edited properly")
179		}
180		// Edits work everything except title, slug, and publicationDate
181		// Edits to the above will cause duplication on the blog home page
182	}
183
184	{ // Test remove functionality
185		title := "example title"
186		slug := "testSlug1"
187		ModAddPost(slug, title, "body1", "2022-05-25T13:17:22Z", "moul", "tag1,tag2")
188
189		got := Render("")
190
191		if !strings.Contains(got, title) {
192			t.Errorf("post was not added properly")
193		}
194
195		postRender := Render("p/" + slug)
196
197		if !strings.Contains(postRender, title) {
198			t.Errorf("post not rendered properly")
199		}
200
201		ModRemovePost(slug)
202		got = Render("")
203
204		if strings.Contains(got, title) {
205			t.Errorf("post was not removed")
206		}
207
208		postRender = Render("p/" + slug)
209
210		assertMDEquals(t, postRender, "404")
211	}
212
213	// TODO: pagination.
214	// TODO: ?format=...
215
216	// all 404s
217	{
218		notFoundPaths := []string{
219			"p/slug3",
220			"p",
221			"p/",
222			"x/x",
223			"t",
224			"t/",
225			"/",
226			"p/slug1/",
227		}
228		for _, notFoundPath := range notFoundPaths {
229			got := Render(notFoundPath)
230			expected := "404"
231			if got != expected {
232				t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got)
233			}
234		}
235	}
236}
237
238func assertMDEquals(t *testing.T, got, expected string) {
239	t.Helper()
240	expected = strings.TrimSpace(expected)
241	got = strings.TrimSpace(got)
242	if expected != got {
243		t.Errorf("invalid render output.\nexpected %q.\ngot      %q.", expected, got)
244	}
245}