gnoblog_test.gno

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