reactions_test.gno
2.46 Kb Β· 91 lines
1package reactions
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/testutils/v0"
8 "gno.land/p/nt/uassert/v0"
9)
10
11func TestReactAndRender(cur realm, t *testing.T) {
12 alice := testutils.TestAddress("alice")
13 bob := testutils.TestAddress("bob")
14
15 testing.SetRealm(testing.NewUserRealm(alice))
16 React(cross(cur),"launch", "π")
17
18 testing.SetRealm(testing.NewUserRealm(bob))
19 React(cross(cur),"launch", "β€οΈ")
20
21 if got := TopicCount(); got != 1 {
22 t.Fatalf("expected 1 topic, got %d", got)
23 }
24
25 out := Render("launch")
26 if !strings.Contains(out, "π 1") {
27 t.Fatalf("expected 'π 1' in render, got:\n%s", out)
28 }
29 if !strings.Contains(out, "β€οΈ 1") {
30 t.Fatalf("expected 'β€οΈ 1' in render, got:\n%s", out)
31 }
32 if !strings.Contains(out, "Total reactions:** 2") {
33 t.Fatalf("expected total 2 in render, got:\n%s", out)
34 }
35}
36
37func TestOneReactionPerCaller(cur realm, t *testing.T) {
38 alice := testutils.TestAddress("alice")
39
40 testing.SetRealm(testing.NewUserRealm(alice))
41 React(cross(cur),"topicA", "π")
42
43 // Second reaction by same caller to same topic must abort.
44 uassert.AbortsWithMessage(t, cur, "caller already reacted to this topic: topicA", func() {
45 React(cross(cur),"topicA", "π")
46 })
47}
48
49func TestCreateTopicAndDuplicate(cur realm, t *testing.T) {
50 dave := testutils.TestAddress("dave")
51 testing.SetRealm(testing.NewUserRealm(dave))
52
53 CreateTopic(cross(cur),"poll")
54 if !topics.Has("poll") {
55 t.Fatalf("expected topic 'poll' to exist")
56 }
57
58 uassert.AbortsWithMessage(t, cur, "topic already exists: poll", func() {
59 CreateTopic(cross(cur),"poll")
60 })
61
62 // A freshly created topic renders as having no reactions.
63 out := Render("poll")
64 if !strings.Contains(out, "no reactions yet") {
65 t.Fatalf("expected empty-topic notice, got:\n%s", out)
66 }
67}
68
69func TestTallyRowOrdering(cur realm, t *testing.T) {
70 a := testutils.TestAddress("u1")
71 b := testutils.TestAddress("u2")
72 c := testutils.TestAddress("u3")
73
74 testing.SetRealm(testing.NewUserRealm(a))
75 React(cross(cur),"party", "π")
76 testing.SetRealm(testing.NewUserRealm(b))
77 React(cross(cur),"party", "π")
78 testing.SetRealm(testing.NewUserRealm(c))
79 React(cross(cur),"party", "π")
80
81 out := Render("party")
82 // π has 2, π has 1 β π must come first (count desc).
83 iParty := strings.Index(out, "π 2")
84 iThumb := strings.Index(out, "π 1")
85 if iParty < 0 || iThumb < 0 {
86 t.Fatalf("expected both tallies in render, got:\n%s", out)
87 }
88 if iParty > iThumb {
89 t.Fatalf("expected π before π by count, got:\n%s", out)
90 }
91}