podium_test.gno
7.70 Kb · 186 lines
1package podium
2
3import (
4 "chain"
5 "chain/banker"
6 "strings"
7 "testing"
8
9 "gno.land/p/nt/testutils/v0"
10 "gno.land/p/nt/uassert/v0"
11)
12
13var (
14 pkgAddr = chain.PackageAddress("gno.land/r/g1ac3v8vr4ddr2m7g68gkksz3f463v9x95w05pnl/podium")
15 creator = testutils.TestAddress("creator")
16 judge = testutils.TestAddress("judge")
17 alice = testutils.TestAddress("alice")
18 bob = testutils.TestAddress("bob")
19 carol = testutils.TestAddress("carol")
20 dave = testutils.TestAddress("dave")
21)
22
23func minutes(n int64) { testing.SkipHeights(n * 12) } // 5s blocks
24
25func bal(cur realm, a address) int64 {
26 return int64(banker.NewReadonlyBanker().GetCoins(a).AmountOf("ugnot"))
27}
28
29func newBounty(cur realm, pot int64, splits string, higher bool, judgeAddr string) int {
30 testing.IssueCoins(pkgAddr, chain.Coins{{"ugnot", pot}})
31 testing.SetRealm(testing.NewUserRealm(creator))
32 testing.SetOriginSend(chain.Coins{{"ugnot", pot}})
33 id := CreateBounty(cross(cur), "Fastest JSON parser", "https://example.com/spec", "ms per op", higher, splits, 60, 60, judgeAddr)
34 testing.SetOriginSend(nil)
35 return id
36}
37
38func register(cur realm, a address, name string) {
39 testing.SetRealm(testing.NewUserRealm(a))
40 Register(cross(cur), name, "https://example.com/"+name+"/agent.json", "an agent")
41}
42
43func TestRankedPayout(cur realm, t *testing.T) {
44 for _, a := range []address{alice, bob, carol, dave} {
45 register(cur, a, strings.ToUpper(a.String()[:4]))
46 }
47 register(cur, alice, "alice-bot")
48 id := newBounty(cur, 10_000_000, "50,30,20", false, judge.String())
49 uassert.Equal(t, "open", Phase(id))
50
51 testing.SetRealm(testing.NewUserRealm(alice))
52 Submit(cross(cur), id, "https://github.com/a/p", "abc1234", "simd", 120)
53 testing.SetRealm(testing.NewUserRealm(bob))
54 Submit(cross(cur), id, "https://github.com/b/p", "def5678", "", 90)
55 testing.SetRealm(testing.NewUserRealm(carol))
56 Submit(cross(cur), id, "https://github.com/c/p", "aaa0001", "", 150)
57 testing.SetRealm(testing.NewUserRealm(dave))
58 Submit(cross(cur), id, "https://github.com/d/p", "bbb0002", "", 10)
59
60 // judge cannot act before the deadline
61 testing.SetRealm(testing.NewUserRealm(judge))
62 uassert.AbortsWithMessage(t, cur, "bounty is not in judging (wait for the deadline)", func() { Finalize(cross(cur), id) })
63
64 minutes(61)
65 uassert.Equal(t, "judging", Phase(id))
66 // late submissions are rejected
67 testing.SetRealm(testing.NewUserRealm(bob))
68 uassert.AbortsWithMessage(t, cur, "submissions are closed", func() { Submit(cross(cur), id, "https://github.com/b/p", "zzz9999", "", 1) })
69 // only the judge can score
70 uassert.AbortsWithMessage(t, cur, "only the judge can do this", func() { ScoreEntry(cross(cur), id, bob.String(), 1) })
71
72 testing.SetRealm(testing.NewUserRealm(judge))
73 n := AcceptClaimed(cross(cur), id)
74 uassert.Equal(t, 4, n)
75 ScoreEntry(cross(cur), id, bob.String(), 95) // judge re-measured bob
76 Disqualify(cross(cur), id, dave.String()) // dave's 10ms not reproducible
77
78 before := bal(cur, creator)
79 Finalize(cross(cur), id)
80 uassert.Equal(t, "paid", Phase(id))
81
82 // lower is better: bob 95 (1st), alice 120 (2nd), carol 150 (3rd)
83 uassert.Equal(t, int64(5_000_000), bal(cur, bob))
84 uassert.Equal(t, int64(3_000_000), bal(cur, alice))
85 uassert.Equal(t, int64(2_000_000), bal(cur, carol))
86 uassert.Equal(t, int64(0), bal(cur, dave))
87 uassert.Equal(t, before, bal(cur, creator))
88 uassert.Equal(t, 1, agentOf(bob).Wins)
89 uassert.Equal(t, int64(3_000_000), agentOf(alice).Earned)
90
91 out := Render("bounty/" + itoa(id))
92 uassert.True(t, strings.Contains(out, "🥇 1st"), out)
93 uassert.True(t, strings.Contains(out, "🚫 DQ"), out)
94 uassert.True(t, strings.Contains(Render(""), "Fastest JSON parser"))
95 uassert.True(t, strings.Contains(BountyJSON(id), `"phase":"paid"`))
96
97 // cannot finalize twice
98 uassert.AbortsWithMessage(t, cur, "bounty is not in judging (wait for the deadline)", func() { Finalize(cross(cur), id) })
99}
100
101func TestUnfilledPlacesReturnToCreator(cur realm, t *testing.T) {
102 register(cur, alice, "alice-bot")
103 id := newBounty(cur, 3_000_001, "50,30,20", true, "")
104 testing.SetRealm(testing.NewUserRealm(alice))
105 Submit(cross(cur), id, "https://github.com/a/q", "c0ffee1", "", 7)
106 minutes(61)
107 before := bal(cur, creator)
108 beforeAlice := bal(cur, alice)
109 testing.SetRealm(testing.NewUserRealm(creator)) // creator judges when judge is empty
110 AcceptClaimed(cross(cur), id)
111 Finalize(cross(cur), id)
112 // 50% + rounding dust (1) to alice, the other 50% back to the creator
113 uassert.Equal(t, beforeAlice+1_500_001, bal(cur, alice))
114 uassert.Equal(t, before+1_500_000, bal(cur, creator))
115}
116
117func TestFallbackWhenJudgeSilent(cur realm, t *testing.T) {
118 register(cur, alice, "alice-bot")
119 register(cur, bob, "bob-bot")
120 id := newBounty(cur, 2_000_001, "100", true, judge.String())
121 testing.SetRealm(testing.NewUserRealm(alice))
122 Submit(cross(cur), id, "https://github.com/a/r", "1111111", "", 1)
123 testing.SetRealm(testing.NewUserRealm(bob))
124 Submit(cross(cur), id, "https://github.com/b/r", "2222222", "", 2)
125
126 minutes(61)
127 testing.SetRealm(testing.NewUserRealm(carol))
128 uassert.AbortsWithMessage(t, cur, "fallback only after the judging window, on an unfinalized bounty", func() { Fallback(cross(cur), id) })
129 minutes(61)
130 uassert.Equal(t, "awaiting-fallback", Phase(id))
131 // judge is too late now
132 testing.SetRealm(testing.NewUserRealm(judge))
133 uassert.AbortsWithMessage(t, cur, "judging window is over; use Fallback", func() { Finalize(cross(cur), id) })
134
135 a0, b0 := bal(cur, alice), bal(cur, bob)
136 testing.SetRealm(testing.NewUserRealm(carol)) // anyone can trigger it
137 Fallback(cross(cur), id)
138 uassert.Equal(t, a0+1_000_001, bal(cur, alice)) // first submitter gets the dust
139 uassert.Equal(t, b0+1_000_000, bal(cur, bob))
140 uassert.Equal(t, "fallback", Phase(id))
141}
142
143func TestCancelAndRules(cur realm, t *testing.T) {
144 register(cur, alice, "alice-bot")
145 id := newBounty(cur, 1_000_000, "60,40", true, "")
146 // non-creator cannot cancel
147 testing.SetRealm(testing.NewUserRealm(alice))
148 uassert.AbortsWithMessage(t, cur, "only the creator can cancel", func() { Cancel(cross(cur), id) })
149 // creator cannot enter own bounty
150 register(cur, creator, "creator-bot")
151 uassert.AbortsWithMessage(t, cur, "creator and judge cannot compete in their own bounty", func() {
152 Submit(cross(cur), id, "https://github.com/x/y", "abcdef0", "", 1)
153 })
154 before := bal(cur, creator)
155 testing.SetRealm(testing.NewUserRealm(creator))
156 Cancel(cross(cur), id)
157 uassert.Equal(t, before+1_000_000, bal(cur, creator))
158 uassert.Equal(t, "cancelled", Phase(id))
159
160 // unregistered agents cannot submit
161 id2 := newBounty(cur, 1_000_000, "100", true, "")
162 testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("stranger")))
163 uassert.AbortsWithMessage(t, cur, "register your agent first (Register)", func() {
164 Submit(cross(cur), id2, "https://github.com/x/y", "abcdef0", "", 1)
165 })
166 // creator cannot cancel once an entry exists
167 testing.SetRealm(testing.NewUserRealm(alice))
168 Submit(cross(cur), id2, "https://github.com/a/z", "abcdef0", "", 1)
169 testing.SetRealm(testing.NewUserRealm(creator))
170 uassert.AbortsWithMessage(t, cur, "cannot cancel: bounty has entries or is closed", func() { Cancel(cross(cur), id2) })
171}
172
173func TestValidation(cur realm, t *testing.T) {
174 uassert.PanicsWithMessage(t, cur, "splits must sum to 100", func() { parseSplits("50,30") })
175 uassert.PanicsWithMessage(t, cur, "splits: a lower place cannot pay more than a higher one", func() { parseSplits("30,70") })
176 uassert.PanicsWithMessage(t, cur, "splits: each place must be a positive integer percent", func() { parseSplits("50,x,50") })
177 uassert.Equal(t, 3, len(parseSplits("50, 30, 20")))
178 uassert.Equal(t, "1.5 GNOT", gnot(1_500_000))
179 uassert.Equal(t, "2 GNOT", gnot(2_000_000))
180 uassert.Equal(t, `\[x\]\(javascript:\)`, esc("[x](javascript:)"))
181 uassert.Equal(t, `evil\]\(x\)`, link("evil](x)"))
182}
183
184func itoa(i int) string {
185 return strings.TrimLeft(key(i), "0")
186}