nomaq_test.gno
4.28 Kb · 162 lines
1package nomaq
2
3import (
4 "chain"
5 "strings"
6 "testing"
7
8 "gno.land/p/nt/avl/v0"
9 "gno.land/p/nt/testutils/v0"
10)
11
12const perk2 = "gno.land/r/g1n4pl5uc4yt5r96m9w6fmdznx3x0jyg8l6arhmt/bazaar/perk2"
13
14func resetAll() {
15 bySlug = avl.Tree{}
16 byPkg = avl.Tree{}
17 reserved = avl.Tree{}
18 holderDisc = avl.Tree{}
19 inited = false
20 launchFee = 0
21 protocolBps = 0
22 genesisPkg = ""
23 genesisShareBps = 0
24 discountPusher = ""
25 var zero address
26 admin = zero
27 self = zero
28 protocolSink = zero
29 genesisSink = zero
30}
31
32func TestGenesisShareDefault(cur realm, t *testing.T) {
33 resetAll()
34 alice := testutils.TestAddress("alice")
35 testing.SetRealm(testing.NewUserRealm(alice))
36 Init(cross(cur))
37 if GenesisShareBps() != 0 {
38 t.Fatalf("share before genesis %d", GenesisShareBps())
39 }
40 if ProtocolBps() != defaultProtocolBps {
41 t.Fatalf("protocol %d", ProtocolBps())
42 }
43 SetGenesis(cross(cur), perk2)
44 if GenesisPkg() != perk2 {
45 t.Fatalf("pkg %q", GenesisPkg())
46 }
47 if GenesisSink() != chain.PackageAddress(perk2) {
48 t.Fatalf("sink %s", GenesisSink())
49 }
50 if GenesisShareBps() != defaultGenesisShareBps {
51 t.Fatalf("share %d", GenesisShareBps())
52 }
53 if GenesisShareBps() == 1100 {
54 t.Fatalf("v5 fallback must not apply")
55 }
56 if !strings.Contains(Render(""), "holder share: 2500 bps") {
57 t.Fatalf("render %q", Render(""))
58 }
59}
60
61func TestSetGenesisShareBps(cur realm, t *testing.T) {
62 resetAll()
63 alice := testutils.TestAddress("alice")
64 testing.SetRealm(testing.NewUserRealm(alice))
65 Init(cross(cur))
66 SetGenesisShareBps(cross(cur), 3000)
67 if GenesisShareBps() != 0 {
68 t.Fatalf("share before genesis %d", GenesisShareBps())
69 }
70 SetGenesis(cross(cur), perk2)
71 if GenesisShareBps() != 3000 {
72 t.Fatalf("kept setter %d", GenesisShareBps())
73 }
74 SetGenesisShareBps(cross(cur), 0)
75 if GenesisShareBps() != 0 {
76 t.Fatalf("zero share %d", GenesisShareBps())
77 }
78 SetGenesisShareBps(cross(cur), maxGenesisShareBps)
79 if GenesisShareBps() != 10000 {
80 t.Fatalf("full share %d", GenesisShareBps())
81 }
82 SetGenesisShareBps(cross(cur), 2500)
83 if GenesisShareBps() != 2500 {
84 t.Fatalf("restore %d", GenesisShareBps())
85 }
86}
87
88func TestSetGenesisDoesNotResetShare(cur realm, t *testing.T) {
89 resetAll()
90 alice := testutils.TestAddress("alice")
91 testing.SetRealm(testing.NewUserRealm(alice))
92 Init(cross(cur))
93 SetGenesisShareBps(cross(cur), 0)
94 SetGenesis(cross(cur), perk2)
95 if GenesisShareBps() != 0 {
96 t.Fatalf("set genesis clobbered zero %d", GenesisShareBps())
97 }
98 SetGenesis(cross(cur), perk2)
99 if GenesisShareBps() != 0 {
100 t.Fatalf("second set genesis %d", GenesisShareBps())
101 }
102}
103
104func TestSetGenesisShareRejected(cur realm, t *testing.T) {
105 resetAll()
106 alice := testutils.TestAddress("alice")
107 bob := testutils.TestAddress("bob")
108 testing.SetRealm(testing.NewUserRealm(alice))
109 Init(cross(cur))
110 SetGenesis(cross(cur), perk2)
111
112 got := revive(func() { SetGenesisShareBps(cross(cur), -1) })
113 if got == nil {
114 t.Fatalf("expected negative reject")
115 }
116 got = revive(func() { SetGenesisShareBps(cross(cur), maxGenesisShareBps+1) })
117 if got == nil {
118 t.Fatalf("expected above max reject")
119 }
120 if GenesisShareBps() != defaultGenesisShareBps {
121 t.Fatalf("rejected call changed share %d", GenesisShareBps())
122 }
123
124 testing.SetRealm(testing.NewUserRealm(bob))
125 got = revive(func() { SetGenesisShareBps(cross(cur), 2500) })
126 if got == nil {
127 t.Fatalf("expected not admin")
128 }
129
130 testing.SetRealm(testing.NewCodeRealm(perk2))
131 got = revive(func() { SetGenesisShareBps(cross(cur), 2500) })
132 if got == nil {
133 t.Fatalf("expected EOA only")
134 }
135 if GenesisShareBps() != defaultGenesisShareBps {
136 t.Fatalf("rejected caller changed share %d", GenesisShareBps())
137 }
138}
139
140func TestAttachGenesisKeepsShare(cur realm, t *testing.T) {
141 resetAll()
142 alice := testutils.TestAddress("alice")
143 testing.SetRealm(testing.NewUserRealm(alice))
144 Init(cross(cur))
145 SetGenesis(cross(cur), perk2)
146 testing.SetRealm(testing.NewUserRealm(alice))
147 got := revive(func() { AttachGenesis(cross(cur)) })
148 if got == nil {
149 t.Fatalf("expected genesis realm")
150 }
151 testing.SetRealm(testing.NewCodeRealm("gno.land/r/g1n4pl5uc4yt5r96m9w6fmdznx3x0jyg8l6arhmt/bazaar/other"))
152 got = revive(func() { AttachGenesis(cross(cur)) })
153 if got == nil {
154 t.Fatalf("expected not admin")
155 }
156 if GenesisPkg() != perk2 {
157 t.Fatalf("pkg changed %q", GenesisPkg())
158 }
159 if GenesisShareBps() != defaultGenesisShareBps {
160 t.Fatalf("share changed %d", GenesisShareBps())
161 }
162}