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