Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

thread_test.gno

2.88 Kb · 76 lines
 1package hub_test
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/gnoland/boards"
 7	"gno.land/p/gnoland/boards/exts/hub"
 8	"gno.land/p/nt/urequire/v0"
 9)
10
11func TestNewSafeThread(t *testing.T) {
12	board := boards.New(1)
13	ref := boards.MustNewThread(board, alice, "Title", "Body")
14	ref.Flags.Add(boards.Flag{User: bob, Reason: "Spam"})
15
16	thread := hub.NewSafeThread(ref)
17
18	urequire.Equal(t, uint64(ref.ID), thread.ID(), "expect ID to match")
19	urequire.Equal(t, uint64(1), thread.BoardID(), "expect board ID to match")
20	urequire.Equal(t, "Title", thread.Title(), "expect title to match")
21	urequire.Equal(t, "Body", thread.Body(), "expect body to match")
22	urequire.Equal(t, alice, thread.Creator(), "expect creator to match")
23	urequire.False(t, thread.Hidden(), "expect thread not to be hidden")
24	urequire.False(t, thread.Readonly(), "expect thread not to be readonly")
25	urequire.Equal(t, 0, thread.CommentCount(), "expect no comments")
26	urequire.Equal(t, 0, thread.RepostCount(), "expect no reposts")
27	urequire.Equal(t, 1, thread.FlagCount(), "expect one flag")
28	urequire.Equal(t, testTime, thread.CreatedAt(), "expect creation time to match")
29	urequire.Equal(t, int64(0), thread.UpdatedAt(), "expect zero update time to map to zero")
30}
31
32func TestNewSafeThreadCounts(t *testing.T) {
33	board := boards.New(1)
34	ref := boards.MustNewThread(board, alice, "Title", "Body")
35	ref.Replies.Add(boards.MustNewReply(ref, bob, "Comment 1"))
36	ref.Replies.Add(boards.MustNewReply(ref, bob, "Comment 2"))
37
38	thread := hub.NewSafeThread(ref)
39
40	urequire.Equal(t, 2, thread.CommentCount(), "expect comment count to match")
41	urequire.Equal(t, 0, thread.FlagCount(), "expect no flags")
42}
43
44// TestNewSafeThreadNilStorages covers the branches that skip counting when a
45// post carries no reply, repost or flag storage.
46func TestNewSafeThreadNilStorages(t *testing.T) {
47	ref := &boards.Post{ID: 1, ThreadID: 1, Board: boards.New(1)}
48
49	thread := hub.NewSafeThread(ref)
50
51	urequire.Equal(t, 0, thread.CommentCount(), "expect no comments")
52	urequire.Equal(t, 0, thread.RepostCount(), "expect no reposts")
53	urequire.Equal(t, 0, thread.FlagCount(), "expect no flags")
54	urequire.Equal(t, int64(0), thread.CreatedAt(), "expect zero creation time to map to zero")
55}
56
57func TestNewSafeThreadRejectsInvalidRefs(cur realm, t *testing.T) {
58	board := boards.New(1)
59	thread := boards.MustNewThread(board, alice, "Title", "Body")
60	comment := boards.MustNewReply(thread, alice, "Comment")
61
62	urequire.PanicsWithMessage(t, cur, "post reference is nil", func() {
63		hub.NewSafeThread(nil)
64	}, "expect a nil post reference to be rejected")
65
66	urequire.PanicsWithMessage(t, cur, "post is not a thread", func() {
67		hub.NewSafeThread(comment)
68	}, "expect a comment to be rejected")
69}
70
71func TestNewSafeFlag(t *testing.T) {
72	flag := hub.NewSafeFlag(boards.Flag{User: alice, Reason: "Spam"})
73
74	urequire.Equal(t, alice, flag.User(), "expect user to match")
75	urequire.Equal(t, "Spam", flag.Reason(), "expect reason to match")
76}