thread_test.gno
4.58 Kb · 175 lines
1package hub_test
2
3import (
4 "testing"
5
6 "gno.land/p/gnoland/boards"
7 "gno.land/p/nt/urequire/v0"
8
9 "gno.land/r/gnoland/boards2/v1/hub"
10)
11
12func TestThreadIterateFlags(t *testing.T) {
13 tests := []struct {
14 name string
15 flags []boards.Flag
16 }{
17 {
18 name: "no flags",
19 },
20 {
21 name: "one flag",
22 flags: []boards.Flag{
23 {User: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", Reason: "Reason 1"},
24 },
25 },
26 {
27 name: "multiple flags",
28 flags: []boards.Flag{
29 {User: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", Reason: "Reason 1"},
30 {User: "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh", Reason: "Reason 2"},
31 {User: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", Reason: "Reason 3"},
32 },
33 },
34 }
35
36 for _, tt := range tests {
37 t.Run(tt.name, func(t *testing.T) {
38 var i int
39 ref := createComment(t, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Comment")
40 for _, f := range tt.flags {
41 ref.Flags.Add(f)
42 }
43
44 comment := hub.NewSafeComment(ref)
45
46 urequire.Equal(t, len(tt.flags), comment.FlagCount, "expect number of flags to match")
47 comment.IterateFlags(0, comment.FlagCount, func(f boards.Flag) bool {
48 urequire.Equal(t, tt.flags[i].User, f.User, "expect user to match")
49 urequire.Equal(t, tt.flags[i].Reason, f.Reason, "expect reason to match")
50 i++
51 return false
52 })
53 })
54 }
55}
56
57func TestThreadIterateReposts(t *testing.T) {
58 tests := []struct {
59 name string
60 setup func(*boards.Post)
61 expected [][2]uint64 // {boardID, repostThreadID}
62 }{
63 {
64 name: "no reposts",
65 },
66 {
67 name: "one repost",
68 setup: func(thread *boards.Post) {
69 r := boards.MustNewRepost(thread, boards.New(1), "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
70 thread.Reposts.Add(r)
71 },
72 expected: [][2]uint64{
73 {1, 1},
74 },
75 },
76 {
77 name: "multiple reposts",
78 setup: func(thread *boards.Post) {
79 reposts := []*boards.Post{
80 boards.MustNewRepost(thread, boards.New(1), "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
81 boards.MustNewRepost(thread, boards.New(2), "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh"),
82 boards.MustNewRepost(thread, boards.New(5), "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
83 }
84 for _, r := range reposts {
85 thread.Reposts.Add(r)
86 }
87 },
88 expected: [][2]uint64{
89 {1, 1},
90 {2, 1},
91 {5, 1},
92 },
93 },
94 }
95
96 for _, tt := range tests {
97 t.Run(tt.name, func(t *testing.T) {
98 var i int
99 ref := createThread(t, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Title", "Body")
100 if tt.setup != nil {
101 tt.setup(ref)
102 }
103
104 thread := hub.NewSafeThread(ref)
105
106 urequire.Equal(t, ref.Reposts.Size(), thread.RepostCount, "expect number of reposts to match")
107 thread.IterateReposts(0, thread.RepostCount, func(boardID, repostThreadID uint64) bool {
108 urequire.Equal(t, tt.expected[i][0], boardID, "expect repost board ID to match")
109 urequire.Equal(t, tt.expected[i][1], repostThreadID, "expect repost thread ID to match")
110 i++
111 return false
112 })
113 })
114 }
115}
116
117func TestThreadIterateComments(t *testing.T) {
118 tests := []struct {
119 name string
120 setup func(*boards.Post)
121 }{
122 {
123 name: "no comments",
124 },
125 {
126 name: "one comment",
127 setup: func(thread *boards.Post) {
128 r := boards.MustNewReply(thread, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Body 1")
129 thread.Replies.Add(r)
130 },
131 },
132 {
133 name: "multiple comments",
134 setup: func(thread *boards.Post) {
135 replies := []*boards.Post{
136 boards.MustNewReply(thread, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Body 1"),
137 boards.MustNewReply(thread, "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh", "Body 2"),
138 boards.MustNewReply(thread, "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", "Body 3"),
139 }
140 for _, t := range replies {
141 thread.Replies.Add(t)
142 }
143 },
144 },
145 }
146
147 for _, tt := range tests {
148 t.Run(tt.name, func(t *testing.T) {
149 ref := createThread(t, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Title", "Body")
150 if tt.setup != nil {
151 tt.setup(ref)
152 }
153
154 thread := hub.NewSafeThread(ref)
155
156 urequire.Equal(t, ref.Replies.Size(), thread.CommentCount, "expect number of replies to match")
157 thread.IterateComments(0, thread.CommentCount, func(comment hub.Comment) bool {
158 id := boards.ID(comment.ID)
159 expected, found := ref.Replies.Get(id)
160
161 urequire.True(t, found, "expect comment to be found")
162 urequire.Equal(t, expected.Creator, comment.Creator, "expect creator to match")
163 urequire.Equal(t, expected.Body, comment.Body, "expect body to match")
164 return false
165 })
166 })
167 }
168}
169
170func createThread(t *testing.T, user address, title, body string) *boards.Post {
171 t.Helper()
172
173 board := boards.New(1)
174 return boards.MustNewThread(board, user, title, body)
175}