rpsduel_test.gno
5.36 Kb · 208 lines
1package rpsduel
2
3import (
4 "testing"
5
6 "gno.land/p/nt/avl/v0"
7 "gno.land/p/nt/testutils/v0"
8)
9
10func resetState() {
11 duels = avl.Tree{}
12 players = avl.Tree{}
13 nextID = 0
14 totalResolved = 0
15 var zero address
16 champion = zero
17 championWins = 0
18}
19
20func TestComputeCommitDeterministicAndSalted(t *testing.T) {
21 a := ComputeCommit("rock", "salt1")
22 b := ComputeCommit("rock", "salt1")
23 if a != b {
24 t.Fatal("ComputeCommit should be deterministic for the same move+salt")
25 }
26 if a == ComputeCommit("rock", "salt2") {
27 t.Fatal("different salts should produce different commits")
28 }
29 if a == ComputeCommit("paper", "salt1") {
30 t.Fatal("different moves should produce different commits")
31 }
32 if len(a) != 64 {
33 t.Fatalf("commit length = %d, want 64 (hex sha256)", len(a))
34 }
35}
36
37// Crossing: Open/Accept/Reveal mutate realm state, so the test needs `cur realm`.
38func TestFullDuelChallengerWins(cur realm, t *testing.T) {
39 resetState()
40 alice := testutils.TestAddress("alice")
41 bob := testutils.TestAddress("bob")
42
43 commit := ComputeCommit("paper", "s3cr3t")
44
45 testing.SetRealm(testing.NewUserRealm(alice))
46 Open(cross(cur), bob.String(), commit)
47
48 v := duels.Get("1")
49 if v == nil {
50 t.Fatal("expected duel #1 to exist")
51 }
52 d := v.(*duel)
53 if d.Status != statusPending {
54 t.Fatalf("Status = %v, want pending", d.Status)
55 }
56
57 testing.SetRealm(testing.NewUserRealm(bob))
58 Accept(cross(cur), "1", "rock")
59
60 if d.Status != statusAwaitingReveal {
61 t.Fatalf("Status = %v, want awaiting reveal", d.Status)
62 }
63
64 testing.SetRealm(testing.NewUserRealm(alice))
65 Reveal(cross(cur), "1", "paper", "s3cr3t")
66
67 if d.Status != statusResolved {
68 t.Fatalf("Status = %v, want resolved", d.Status)
69 }
70 if d.Result != "challenger" {
71 t.Fatalf("Result = %q, want challenger (paper beats rock)", d.Result)
72 }
73 if d.Winner != alice {
74 t.Fatal("expected alice to win")
75 }
76 if totalResolved != 1 {
77 t.Fatalf("totalResolved = %d, want 1", totalResolved)
78 }
79
80 aliceStats := getOrCreate(alice)
81 if aliceStats.Wins != 1 {
82 t.Fatalf("alice.Wins = %d, want 1", aliceStats.Wins)
83 }
84 bobStats := getOrCreate(bob)
85 if bobStats.Losses != 1 {
86 t.Fatalf("bob.Losses = %d, want 1", bobStats.Losses)
87 }
88 if !champion.IsValid() || champion != alice {
89 t.Fatal("champion should be alice")
90 }
91}
92
93func TestRevealMismatchPanics(cur realm, t *testing.T) {
94 resetState()
95 alice := testutils.TestAddress("alice")
96 bob := testutils.TestAddress("bob")
97
98 testing.SetRealm(testing.NewUserRealm(alice))
99 Open(cross(cur), bob.String(), ComputeCommit("rock", "s3cr3t"))
100
101 testing.SetRealm(testing.NewUserRealm(bob))
102 Accept(cross(cur), "1", "scissors")
103
104 testing.SetRealm(testing.NewUserRealm(alice))
105 rec := revive(func() { Reveal(cross(cur), "1", "paper", "s3cr3t") })
106 if rec == nil {
107 t.Fatal("expected panic when revealed move doesn't match the commitment")
108 }
109}
110
111func TestOnlyOpponentCanAccept(cur realm, t *testing.T) {
112 resetState()
113 alice := testutils.TestAddress("alice")
114 bob := testutils.TestAddress("bob")
115 mallory := testutils.TestAddress("mallory")
116
117 testing.SetRealm(testing.NewUserRealm(alice))
118 Open(cross(cur), bob.String(), ComputeCommit("rock", "s3cr3t"))
119
120 testing.SetRealm(testing.NewUserRealm(mallory))
121 rec := revive(func() { Accept(cross(cur), "1", "paper") })
122 if rec == nil {
123 t.Fatal("expected panic when a non-opponent accepts")
124 }
125}
126
127func TestClaimForfeitBeforeDeadlinePanics(cur realm, t *testing.T) {
128 resetState()
129 alice := testutils.TestAddress("alice")
130 bob := testutils.TestAddress("bob")
131
132 testing.SetRealm(testing.NewUserRealm(alice))
133 Open(cross(cur), bob.String(), ComputeCommit("rock", "s3cr3t"))
134
135 testing.SetRealm(testing.NewUserRealm(bob))
136 Accept(cross(cur), "1", "paper")
137
138 rec := revive(func() { ClaimForfeit(cross(cur), "1") })
139 if rec == nil {
140 t.Fatal("expected panic when claiming a forfeit before the reveal window expires")
141 }
142}
143
144func TestClaimForfeitAfterDeadline(cur realm, t *testing.T) {
145 resetState()
146 alice := testutils.TestAddress("alice")
147 bob := testutils.TestAddress("bob")
148
149 testing.SetRealm(testing.NewUserRealm(alice))
150 Open(cross(cur), bob.String(), ComputeCommit("rock", "s3cr3t"))
151
152 testing.SetRealm(testing.NewUserRealm(bob))
153 Accept(cross(cur), "1", "paper")
154
155 testing.SkipHeights(revealWindow + 1)
156
157 ClaimForfeit(cross(cur), "1")
158
159 v := duels.Get("1")
160 d := v.(*duel)
161 if d.Status != statusForfeited {
162 t.Fatalf("Status = %v, want forfeited", d.Status)
163 }
164 if d.Winner != bob {
165 t.Fatal("expected bob to win by forfeit")
166 }
167
168 aliceStats := getOrCreate(alice)
169 if aliceStats.Forfeits != 1 {
170 t.Fatalf("alice.Forfeits = %d, want 1", aliceStats.Forfeits)
171 }
172}
173
174func TestCancel(cur realm, t *testing.T) {
175 resetState()
176 alice := testutils.TestAddress("alice")
177 bob := testutils.TestAddress("bob")
178
179 testing.SetRealm(testing.NewUserRealm(alice))
180 Open(cross(cur), bob.String(), ComputeCommit("rock", "s3cr3t"))
181 Cancel(cross(cur), "1")
182
183 v := duels.Get("1")
184 d := v.(*duel)
185 if d.Status != statusCancelled {
186 t.Fatalf("Status = %v, want cancelled", d.Status)
187 }
188}
189
190func TestOpenSelfRejected(cur realm, t *testing.T) {
191 resetState()
192 alice := testutils.TestAddress("alice")
193 testing.SetRealm(testing.NewUserRealm(alice))
194
195 rec := revive(func() { Open(cross(cur), alice.String(), ComputeCommit("rock", "s3cr3t")) })
196 if rec == nil {
197 t.Fatal("expected panic when challenging yourself")
198 }
199}
200
201// Non-crossing: Render is a plain read.
202func TestRenderHome(t *testing.T) {
203 resetState()
204 out := Render("")
205 if out == "" {
206 t.Fatal(`Render("") should not be empty`)
207 }
208}