comment_test.gno
3.56 Kb · 90 lines
1package hub_test
2
3import (
4 "testing"
5
6 "gno.land/p/gnoland/boards/exts/hub/v0"
7 "gno.land/p/gnoland/boards/v0"
8 "gno.land/p/nt/urequire/v0"
9)
10
11// Addresses and the fixed time the test VM reports for time.Now().
12const (
13 alice address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
14 bob address = "g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx"
15 testTime int64 = 1234567890
16)
17
18func TestNewSafeComment(t *testing.T) {
19 board := boards.New(1)
20 thread := boards.MustNewThread(board, alice, "Title", "Body")
21 ref := boards.MustNewReply(thread, bob, "Comment")
22 ref.Flags.Add(boards.Flag{User: alice, Reason: "Spam"})
23
24 comment := hub.NewSafeComment(ref)
25
26 urequire.Equal(t, uint64(ref.ID), comment.ID(), "expect ID to match")
27 urequire.Equal(t, uint64(1), comment.BoardID(), "expect board ID to match")
28 urequire.Equal(t, uint64(thread.ID), comment.ThreadID(), "expect thread ID to match")
29 urequire.Equal(t, "Comment", comment.Body(), "expect body to match")
30 urequire.Equal(t, bob, comment.Creator(), "expect creator to match")
31 urequire.False(t, comment.Hidden(), "expect comment not to be hidden")
32 urequire.Equal(t, 0, comment.ReplyCount(), "expect no replies")
33 urequire.Equal(t, 1, comment.FlagCount(), "expect one flag")
34 urequire.Equal(t, testTime, comment.CreatedAt(), "expect creation time to match")
35 urequire.Equal(t, int64(0), comment.UpdatedAt(), "expect zero update time to map to zero")
36}
37
38// TestNewSafeCommentParentID documents that a top level comment reports its
39// thread as parent rather than a zero parent, so callers must compare
40// ParentID against ThreadID to tell comments and replies apart.
41func TestNewSafeCommentParentID(t *testing.T) {
42 board := boards.New(1)
43 thread := boards.MustNewThread(board, alice, "Title", "Body")
44 commentRef := boards.MustNewReply(thread, bob, "Comment")
45 replyRef := boards.MustNewReply(commentRef, alice, "Reply")
46
47 comment := hub.NewSafeComment(commentRef)
48 reply := hub.NewSafeComment(replyRef)
49
50 urequire.Equal(t, comment.ThreadID(), comment.ParentID(), "expect a comment's parent to be its thread")
51 urequire.Equal(t, comment.ID(), reply.ParentID(), "expect a reply's parent to be its comment")
52 urequire.Equal(t, comment.ThreadID(), reply.ThreadID(), "expect a reply to keep the thread ID")
53}
54
55func TestNewSafeCommentReplyCount(t *testing.T) {
56 board := boards.New(1)
57 thread := boards.MustNewThread(board, alice, "Title", "Body")
58 ref := boards.MustNewReply(thread, bob, "Comment")
59 ref.Replies.Add(boards.MustNewReply(ref, alice, "Reply 1"))
60 ref.Replies.Add(boards.MustNewReply(ref, alice, "Reply 2"))
61
62 comment := hub.NewSafeComment(ref)
63
64 urequire.Equal(t, 2, comment.ReplyCount(), "expect reply count to match")
65}
66
67// TestNewSafeCommentNilStorages covers the branches that skip counting when a
68// post carries no reply or flag storage.
69func TestNewSafeCommentNilStorages(t *testing.T) {
70 ref := &boards.Post{ID: 2, ParentID: 1, ThreadID: 1, Board: boards.New(1)}
71
72 comment := hub.NewSafeComment(ref)
73
74 urequire.Equal(t, 0, comment.ReplyCount(), "expect no replies")
75 urequire.Equal(t, 0, comment.FlagCount(), "expect no flags")
76 urequire.Equal(t, int64(0), comment.CreatedAt(), "expect zero creation time to map to zero")
77}
78
79func TestNewSafeCommentRejectsInvalidRefs(cur realm, t *testing.T) {
80 board := boards.New(1)
81 thread := boards.MustNewThread(board, alice, "Title", "Body")
82
83 urequire.PanicsWithMessage(t, cur, "post reference is nil", func() {
84 hub.NewSafeComment(nil)
85 }, "expect a nil post reference to be rejected")
86
87 urequire.PanicsWithMessage(t, cur, "post is not a comment or reply", func() {
88 hub.NewSafeComment(thread)
89 }, "expect a thread to be rejected")
90}