blog_test.gno
13.44 Kb · 426 lines
1package blog
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/lou/blog"
8)
9
10func clearState(t *testing.T) {
11 t.Helper()
12 var err error
13
14 admin = address("g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs")
15 myBlog, err = blog.NewBlog(
16 "Lou's Blog",
17 admin,
18 blog.WithUserResolver(myResolver),
19 )
20 if err != nil {
21 t.Errorf(err.Error())
22 }
23 // inPause = false
24}
25
26func TestPackage(cur realm, t *testing.T) {
27 clearState(t)
28
29 testing.SetOriginCaller(admin)
30 testing.SetRealm(testing.NewUserRealm(admin))
31
32 {
33 println("Creating posts and rendering previews...")
34 CreatePost(cross, "slug1", "title1", "body1", "2022-05-20T13:17:22Z", "lou", "tag1 tag2")
35 CreatePost(cross, "slug2", "title2", "body2", "2022-05-20T13:17:23Z", "lou", "tag1 tag3")
36 got := Render("")
37 expected := `# Lou's Blog
38
39[⊞ grid](/r/lou/blog:?mode=grid) | [≔ list](/r/lou/blog:?mode=list) | [⧖ relative](/r/lou/blog:?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:?order=asc&sort=update) | [past year](/r/lou/blog:?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
40
41<gno-columns>
42<gno-columns>
43## [title2](/r/lou/blog:posts/slug2)
44
45
46##### */slug2*
47
48
49just now
50
51**tags:** ` + "`" + `tag1` + "`" + `, ` + "`" + `tag3` + "`" + `
52
53**[comments \(0](/r/lou/blog:posts/slug2#comments)**) | ❤️ (0)
54
55[Like](/r/lou/blog$help&func=LikePostBySlug&slug=slug2)
56
57
58
59---
60|||
61## [title1](/r/lou/blog:posts/slug1)
62
63
64##### */slug1*
65
66
67just now
68
69**tags:** ` + "`" + `tag1` + "`" + `, ` + "`" + `tag2` + "`" + `
70
71**[comments \(0](/r/lou/blog:posts/slug1#comments)**) | ❤️ (0)
72
73[Like](/r/lou/blog$help&func=LikePostBySlug&slug=slug1)
74
75
76
77---
78|||
79</gno-columns>`
80 assertMDEquals(t, got, expected)
81 }
82
83 {
84 println("Viewing one of the post...")
85 got := Render("posts/slug2")
86 expected := `# title2
87
88
89*Author(s):* [lou](/r/lou/blog:authors/lou)
90
91
92body2
93
94---
95**Created on:** May 20, 2022 at 1:17 PM
96
97**Published on:** February 13, 2009 at 11:31 PM
98
99**Publisher:** [g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/u/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)
100
101**Tags:** [#tag1](/r/lou/blog:tags/tag1), [#tag3](/r/lou/blog:tags/tag3)
102
103
104❤️ 0
105
106---
107### Comments (0)
108
109
110No comments yet.`
111 assertMDEquals(t, got, expected)
112 }
113
114 {
115 println("Listing of tags...")
116 println("Listing")
117 got := Render("authors")
118 expected := `## [/r/lou/blog](/r/lou/blog)/authors
119[↕ alphabetical \(A\-Z\)](/r/lou/blog:authors?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:authors?order=asc&sort=recent) | [↕ most common](/r/lou/blog:authors?order=asc&sort=common) | [past year](/r/lou/blog:authors?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:authors?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:authors?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
120
121### [lou](/r/lou/blog:authors/lou) (2)`
122 assertMDEquals(t, got, expected)
123
124 println("Invalid tag")
125 got = Render("tags/invalid")
126 expected = `## [/r/lou/blog](/r/lou/blog)/[tags](/r/lou/blog:tags)/invalid
127[⊞ grid](/r/lou/blog:tags/invalid?mode=grid) | [≔ list](/r/lou/blog:tags/invalid?mode=list) | [⧖ relative](/r/lou/blog:tags/invalid?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:tags/invalid?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:tags/invalid?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:tags/invalid?order=asc&sort=update) | [past year](/r/lou/blog:tags/invalid?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:tags/invalid?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:tags/invalid?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
128
129No posts found for this tag.<gno-columns>`
130 assertMDEquals(t, got, expected)
131
132 println("Valid tag")
133 got = Render("tags/tag2")
134 expected = `## [/r/lou/blog](/r/lou/blog)/[tags](/r/lou/blog:tags)/tag2
135[⊞ grid](/r/lou/blog:tags/tag2?mode=grid) | [≔ list](/r/lou/blog:tags/tag2?mode=list) | [⧖ relative](/r/lou/blog:tags/tag2?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:tags/tag2?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:tags/tag2?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:tags/tag2?order=asc&sort=update) | [past year](/r/lou/blog:tags/tag2?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:tags/tag2?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:tags/tag2?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
136
137<gno-columns>
138<gno-columns>
139## [title1](/r/lou/blog:posts/slug1)
140
141
142##### */slug1*
143
144
145just now
146
147**author(s):** lou
148
149**tags:** ` + "`tag1`, `tag2`" + `
150
151**[comments \(0](/r/lou/blog:posts/slug1#comments)**) | ❤️ (0)
152
153[Like](/r/lou/blog$help&func=LikePostBySlug&slug=slug1)
154
155
156
157---
158
159</gno-columns>`
160 assertMDEquals(t, got, expected)
161 }
162
163 {
164 println("Listing of authors...")
165 println("Listing")
166 got := Render("authors")
167 expected := `## [/r/lou/blog](/r/lou/blog)/authors
168[↕ alphabetical \(A\-Z\)](/r/lou/blog:authors?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:authors?order=asc&sort=recent) | [↕ most common](/r/lou/blog:authors?order=asc&sort=common) | [past year](/r/lou/blog:authors?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:authors?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:authors?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
169
170### [lou](/r/lou/blog:authors/lou) (2)`
171 assertMDEquals(t, got, expected)
172
173 println("Invalid author")
174 got = Render("authors/invalid")
175 expected = `## [/r/lou/blog](/r/lou/blog)/[authors](/r/lou/blog:authors)/invalid
176#### [@invalid](/u/invalid)'s profile
177[⊞ grid](/r/lou/blog:authors/invalid?mode=grid) | [≔ list](/r/lou/blog:authors/invalid?mode=list) | [⧖ relative](/r/lou/blog:authors/invalid?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:authors/invalid?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:authors/invalid?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:authors/invalid?order=asc&sort=update) | [past year](/r/lou/blog:authors/invalid?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:authors/invalid?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:authors/invalid?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
178
179No posts found for this author.<gno-columns>`
180 assertMDEquals(t, got, expected)
181
182 println("Valid author")
183 got = Render("authors/lou")
184 expected = `## [/r/lou/blog](/r/lou/blog)/[authors](/r/lou/blog:authors)/lou
185#### [@lou](/u/lou)'s profile
186[⊞ grid](/r/lou/blog:authors/lou?mode=grid) | [≔ list](/r/lou/blog:authors/lou?mode=list) | [⧖ relative](/r/lou/blog:authors/lou?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:authors/lou?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:authors/lou?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:authors/lou?order=asc&sort=update) | [past year](/r/lou/blog:authors/lou?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:authors/lou?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:authors/lou?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
187
188<gno-columns>
189<gno-columns>
190## [title2](/r/lou/blog:posts/slug2)
191
192
193##### */slug2*
194
195
196just now
197
198**author(s):** lou
199
200**tags:** ` + "`tag1`, `tag3`" + `
201
202**[comments \(0](/r/lou/blog:posts/slug2#comments)**) | ❤️ (0)
203
204[Like](/r/lou/blog$help&func=LikePostBySlug&slug=slug2)
205
206
207
208---
209
210## [title1](/r/lou/blog:posts/slug1)
211
212
213##### */slug1*
214
215
216just now
217
218**author(s):** lou
219
220**tags:** ` + "`tag1`, `tag2`" + `
221
222**[comments \(0](/r/lou/blog:posts/slug1#comments)**) | ❤️ (0)
223
224[Like](/r/lou/blog$help&func=LikePostBySlug&slug=slug1)
225
226
227
228---
229
230</gno-columns>`
231 assertMDEquals(t, got, expected)
232 }
233
234 {
235 println("Adding comments on a post...")
236 AddCommentToPostBySlug(cross, "slug1", "comment1")
237 AddCommentToPostBySlug(cross, "slug2", "comment2")
238 AddCommentToPostBySlug(cross, "slug1", "comment3")
239 AddCommentToPostBySlug(cross, "slug2", "comment4")
240 AddCommentToPostBySlug(cross, "slug1", "comment5")
241 got := Render("posts/slug2")
242 expected := `# title2
243
244
245[2 Comment\(s\)](#comments)
246
247*Author(s):* [lou](/r/lou/blog:authors/lou)
248
249
250body2
251
252---
253**Created on:** May 20, 2022 at 1:17 PM
254
255**Published on:** February 13, 2009 at 11:31 PM
256
257**Publisher:** [g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/u/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)
258
259**Tags:** [#tag1](/r/lou/blog:tags/tag1), [#tag3](/r/lou/blog:tags/tag3)
260
261
262❤️ 0
263
264---
265### Comments (2)
266
267
268**[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)** *just now*
269
270comment4
271❤️ 0
272
273
274
275**[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)** *just now*
276
277comment2
278❤️ 0`
279 assertMDEquals(t, got, expected)
280 }
281
282 {
283 println("Listing of commenters...")
284 println("Listing invalid")
285 got := Render("commenters")
286 expected := `Commenter slug is required.`
287 assertMDEquals(t, got, expected)
288
289 println("Invalid commenter")
290 got = Render("commenters/invalid")
291 expected = `## [/r/lou/blog](/r/lou/blog)/commenters/invalid
292#### [@invalid](/u/invalid)'s profile
293[⊞ grid](/r/lou/blog:commenters/invalid?mode=grid) | [≔ list](/r/lou/blog:commenters/invalid?mode=list) | [⧖ relative](/r/lou/blog:commenters/invalid?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:commenters/invalid?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:commenters/invalid?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:commenters/invalid?order=asc&sort=update) | [past year](/r/lou/blog:commenters/invalid?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:commenters/invalid?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:commenters/invalid?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
294
295No posts found for this commenter.<gno-columns>`
296 assertMDEquals(t, got, expected)
297
298 println("Valid commenter")
299 got = Render("commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs")
300 expected = `## [/r/lou/blog](/r/lou/blog)/commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs
301#### [@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/u/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)'s profile
302[⊞ grid](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?mode=grid) | [≔ list](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?mode=list) | [⧖ relative](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?time=short) | [↕ alphabetical \(A\-Z\)](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?order=asc&sort=alpha) | [↕ recent](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?order=asc&sort=recent) | [↕ last updated](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?order=asc&sort=update) | [past year](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?end=2009-02-13&start=2008-02-13), [this year](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?end=2009-02-13&start=2009-01-01), [last 30 days](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?end=2009-02-13&start=2009-01-14) | [⟳ reset](/r/lou/blog)
303
304<gno-columns>
305<gno-columns>
306#### **[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)**
307
308
309comment1
310
311*just now*
312
313in [title1](/r/lou/blog:posts/slug1)
314
315---
316#### **[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)**
317
318
319comment3
320
321*just now*
322
323in [title1](/r/lou/blog:posts/slug1)
324
325---
326#### **[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)**
327
328
329comment5
330
331*just now*
332
333in [title1](/r/lou/blog:posts/slug1)
334
335---
336</gno-columns>
337**1** | [2](?page=2)`
338 assertMDEquals(t, got, expected)
339 }
340
341 {
342 println("Invalid paths...")
343 notFoundPaths := []string{
344 "p/slug3",
345 "p",
346 "p/",
347 "x/x",
348 "t",
349 "t/",
350 "/",
351 "p/slug1/",
352 }
353 for _, notFoundPath := range notFoundPaths {
354 got := Render(notFoundPath)
355 expected := "404"
356 if got != expected {
357 t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got)
358 } else {
359 println("PASSED")
360 }
361 }
362 }
363
364 // {
365 // println("Updating a post...")
366 // newTitle := "title3"
367 // newDate := "2022-05-20T13:17:23Z"
368
369 // CreatePost(cross, "slug1", "title1", "body1", "2022-05-20T13:17:22Z", "lou", "tag1 tag2")
370 // UpdatePostBySlug(cur, "slug2", newTitle, "body2++", newDate, "lou", "tag1 tag4")
371 // got := Render("posts/slug2")
372 // expected := ``
373 // assertMDEquals(t, got, expected)
374
375 // home := Render("")
376
377 // if strings.Count(home, newTitle) != 1 {
378 // t.Errorf("post not edited properly")
379 // }
380 // }
381
382 // {
383 // // fix
384 // println("Deleting a post...")
385 // title := "example title"
386 // slug := "testSlug1"
387 // CreatePost(cross, slug, title, "body1", "2022-05-25T13:17:22Z", "lou", "tag1,tag2")
388
389 // got := Render("")
390
391 // if !strings.Contains(got, title) {
392 // t.Errorf("post was not added properly")
393 // }
394
395 // postRender := Render("p/" + slug)
396
397 // if !strings.Contains(postRender, title) {
398 // t.Errorf("post not rendered properly")
399 // }
400
401 // DeletePost(cur, slug)
402 // got = Render("")
403
404 // if strings.Contains(got, title) {
405 // t.Errorf("post was not removed")
406 // }
407
408 // postRender = Render("p/" + slug)
409
410 // assertMDEquals(t, postRender, "404")
411 // }
412 //
413 // // TODO: ?mode=...
414 //
415}
416
417func assertMDEquals(t *testing.T, got, expected string) {
418 t.Helper()
419 expected = strings.TrimSpace(expected)
420 got = strings.TrimSpace(got)
421 if expected != got {
422 t.Errorf("invalid render output.\nexpected %q.\ngot %q.", expected, got)
423 } else {
424 println("PASSED")
425 }
426}