play_test.gno
15.72 Kb · 374 lines
1package arena
2
3import (
4 "strconv"
5 "strings"
6 "testing"
7
8 "gno.land/p/nym-gfanton001/gnomepit/engine/v0"
9 "gno.land/p/nt/uassert/v0"
10 "gno.land/p/nt/urequire/v0"
11)
12
13const (
14 deckA = "Pickaxe Flurry,Kneecap,Sling Stone,Dodge Roll,Chalk Dust,Double Tap,Momentum,Undermine"
15 deckB = "Stone Guard,Bulwark,Hammer Blow,Cobble Throw,Spiked Shield,Anvil Stomp,Feint,Avalanche"
16
17 salt1 = "1111111111111111111111111111111111111111111111111111111111111111"
18 salt2 = "2222222222222222222222222222222222222222222222222222222222222222"
19 salt3 = "3333333333333333333333333333333333333333333333333333333333333333"
20
21 picks = "Pick,Pick,Pick"
22 braces = "Brace,Brace,Brace"
23
24 day = 86400 / 5 // block heights in 24 hours at the test chain's 5 s per block
25)
26
27// recordOf loads match id as the realm stores it.
28func recordOf(t *testing.T, id string) record {
29 n, err := strconv.Atoi(id)
30 urequire.NoError(t, err)
31 m, ok := loadMatch(engine.ProofOfConcept(), n)
32 urequire.True(t, ok, "match "+id+" is not stored")
33 return m
34}
35
36// resolveAll plays rounds of [A plan, B plan] with the engine directly.
37func resolveAll(rounds ...[2]string) engine.Outcome {
38 rules := engine.ProofOfConcept()
39 s := rules.Start()
40 var o engine.Outcome
41 for _, r := range rounds {
42 a, _ := rules.PlanOf(strings.Split(r[0], ",")...)
43 b, _ := rules.PlanOf(strings.Split(r[1], ",")...)
44 o = rules.Resolve(s, a, b)
45 s = o.State
46 }
47 return o
48}
49
50func TestWorkedExample(cur realm, t *testing.T) {
51 reset()
52 a1, b1 := "Chalk Dust,Double Tap", "Hammer Blow,Spiked Shield"
53
54 testing.SetRealm(testing.NewUserRealm(alice))
55 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, a1), boxOf("box1"), "")
56 testing.SetRealm(testing.NewUserRealm(bob))
57 Accept(cross(cur), id, "Ironhide", deckB, b1)
58 uassert.Equal(t, phaseReveal, Match(id).Phase)
59 uassert.Equal(t, 0, len(Open(0, 50)))
60
61 testing.SetRealm(testing.NewUserRealm(alice))
62 Reveal(cross(cur), id, deckA, saltA, a1, salt1, sealOf(salt2, picks), boxOf("box2"))
63 uassert.Equal(t, phasePlay, Match(id).Phase)
64 testing.SetRealm(testing.NewUserRealm(bob))
65 Play(cross(cur), id, braces)
66 testing.SetRealm(testing.NewUserRealm(alice))
67 Reveal(cross(cur), id, "", "", picks, salt2, sealOf(salt3, picks), boxOf("box3"))
68 testing.SetRealm(testing.NewUserRealm(bob))
69 Play(cross(cur), id, braces)
70 testing.SetRealm(testing.NewUserRealm(alice))
71 Reveal(cross(cur), id, "", "", picks, salt3, "", "")
72
73 want := resolveAll([2]string{a1, b1}, [2]string{picks, braces}, [2]string{picks, braces})
74 v := Match(id)
75 uassert.Equal(t, phaseEnded, v.Phase)
76 uassert.Equal(t, resultName(want.Result), v.Result)
77 uassert.Equal(t, "hp", v.Reason)
78 urequire.Equal(t, 3, len(v.HP))
79 uassert.Equal(t, want.State.Gnomes[0].HP, v.HP[2][0])
80 uassert.Equal(t, want.State.Gnomes[1].HP, v.HP[2][1])
81 uassert.Equal(t, deckA, v.ChallengerDeck)
82 uassert.Equal(t, a1, v.Plans[0][0])
83 uassert.Equal(t, b1, v.Plans[0][1])
84}
85
86func TestKnockoutEndsTheMatchEarly(cur realm, t *testing.T) {
87 reset()
88 b1, b2 := "Pick,Anvil Stomp", "Hammer Blow,Cobble Throw"
89
90 testing.SetRealm(testing.NewUserRealm(alice))
91 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
92 testing.SetRealm(testing.NewUserRealm(bob))
93 Accept(cross(cur), id, "Ironhide", deckB, b1)
94 testing.SetRealm(testing.NewUserRealm(alice))
95 Reveal(cross(cur), id, deckA, saltA, picks, salt1, sealOf(salt2, picks), boxOf("box2"))
96 testing.SetRealm(testing.NewUserRealm(bob))
97 Play(cross(cur), id, b2)
98
99 testing.SetRealm(testing.NewUserRealm(alice))
100 uassert.AbortsWithMessage(t, cur, "unexpected next seal", func() {
101 Reveal(cross(cur), id, "", "", picks, salt2, sealOf(salt3, picks), boxOf("box3"))
102 })
103 Reveal(cross(cur), id, "", "", picks, salt2, "", "")
104
105 v := Match(id)
106 uassert.Equal(t, phaseEnded, v.Phase)
107 uassert.Equal(t, "b", v.Result)
108 uassert.Equal(t, "knockout", v.Reason)
109 uassert.Equal(t, 2, len(v.HP))
110}
111
112func TestDrawOnHP(cur realm, t *testing.T) {
113 reset()
114 testing.SetRealm(testing.NewUserRealm(alice))
115 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, braces), boxOf("box1"), "")
116 testing.SetRealm(testing.NewUserRealm(bob))
117 Accept(cross(cur), id, "Ironhide", deckB, braces)
118 testing.SetRealm(testing.NewUserRealm(alice))
119 Reveal(cross(cur), id, deckA, saltA, braces, salt1, sealOf(salt2, braces), boxOf("box2"))
120 testing.SetRealm(testing.NewUserRealm(bob))
121 Play(cross(cur), id, braces)
122 testing.SetRealm(testing.NewUserRealm(alice))
123 Reveal(cross(cur), id, "", "", braces, salt2, sealOf(salt3, braces), boxOf("box3"))
124 testing.SetRealm(testing.NewUserRealm(bob))
125 Play(cross(cur), id, braces)
126 testing.SetRealm(testing.NewUserRealm(alice))
127 Reveal(cross(cur), id, "", "", braces, salt3, "", "")
128
129 v := Match(id)
130 uassert.Equal(t, "draw", v.Result)
131 uassert.Equal(t, "hp", v.Reason)
132}
133
134func TestSelfPlay(cur realm, t *testing.T) {
135 reset()
136 testing.SetRealm(testing.NewUserRealm(alice))
137 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
138 Accept(cross(cur), id, "Ironhide", deckB, braces)
139 Reveal(cross(cur), id, deckA, saltA, picks, salt1, sealOf(salt2, picks), boxOf("box2"))
140 Play(cross(cur), id, braces)
141 Reveal(cross(cur), id, "", "", picks, salt2, sealOf(salt3, picks), boxOf("box3"))
142 Play(cross(cur), id, braces)
143 Reveal(cross(cur), id, "", "", picks, salt3, "", "")
144 uassert.Equal(t, phaseEnded, Match(id).Phase)
145 uassert.Equal(t, id, summaryIDs(Matches(alice, 0, 50)), "a match the player posted and accepted is listed once")
146}
147
148func TestAcceptRejects(cur realm, t *testing.T) {
149 reset()
150 testing.SetRealm(testing.NewUserRealm(alice))
151 openID := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
152 namedID := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), bob)
153
154 testing.SetRealm(testing.NewUserRealm(carol))
155 cases := []struct {
156 name, id, clan, deck, plan, want string
157 }{
158 {"unknown match", "99", "Ironhide", deckB, braces, "unknown match"},
159 {"not the named opponent", namedID, "Ironhide", deckB, braces, "not the named opponent"},
160 {"unknown clan", openID, "Elves", deckB, braces, "unknown clan"},
161 {"a card twice", openID, "Ironhide", strings.Replace(deckB, "Bulwark", "Stone Guard", 1), braces, "illegal deck"},
162 {"a ninth name repeating a card", openID, "Ironhide", deckB + ",Stone Guard", braces, "illegal deck"},
163 {"another clan's card", openID, "Ironhide", strings.Replace(deckB, "Bulwark", "Kneecap", 1), braces, "illegal deck"},
164 {"unknown card", openID, "Ironhide", strings.Replace(deckB, "Bulwark", "Bulwerk", 1), braces, "unknown card"},
165 {"two beats", openID, "Ironhide", deckB, "Pick,Pick", "illegal plan"},
166 {"a card outside the deck", openID, "Ironhide", deckB, "Shield Catch,Pick", "illegal plan"},
167 }
168 for _, c := range cases {
169 uassert.AbortsWithMessage(t, cur, c.want, func() {
170 Accept(cross(cur), c.id, c.clan, c.deck, c.plan)
171 }, c.name)
172 }
173
174 Accept(cross(cur), openID, "Ironhide", deckB, braces)
175 uassert.AbortsWithMessage(t, cur, "wrong phase", func() {
176 Accept(cross(cur), openID, "Ironhide", deckB, braces)
177 })
178
179 testing.SkipHeights(7*day + 1)
180 testing.SetRealm(testing.NewUserRealm(bob))
181 uassert.AbortsWithMessage(t, cur, "challenge expired", func() {
182 Accept(cross(cur), namedID, "Ironhide", deckB, braces)
183 })
184}
185
186// The named opponent is the only one who can accept, and the acceptor then names them, so a
187// copy would keep 40 bytes for good.
188func TestAcceptForgetsTheNamedOpponent(cur realm, t *testing.T) {
189 reset()
190 testing.SetRealm(testing.NewUserRealm(alice))
191 named := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), bob)
192 open := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
193 uassert.Equal(t, bob.String(), Match(named).Opponent)
194
195 testing.SetRealm(testing.NewUserRealm(bob))
196 Accept(cross(cur), named, "Ironhide", deckB, braces)
197 Accept(cross(cur), open, "Ironhide", deckB, braces)
198
199 uassert.Equal(t, "", recordOf(t, named).opponent.String())
200 stored := func(id string) string {
201 n, _ := strconv.Atoi(id)
202 return chunkRecord(stringAt(&chunks, chunkKey(n)), (n-1)%chunkSize)
203 }
204 uassert.Equal(t, len(stored(open)), len(stored(named)), "an accepted reserved match stores what an accepted open one does")
205 v := Match(named)
206 uassert.Equal(t, "", v.Opponent)
207 uassert.Equal(t, bob.String(), v.Acceptor)
208}
209
210func TestRevealRejects(cur realm, t *testing.T) {
211 reset()
212 testing.SetRealm(testing.NewUserRealm(alice))
213 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
214 badPlan := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, "Pick,Pick"), boxOf("box1"), "")
215 badDeck := Challenge(cross(cur), "Pickers", sealOf(saltA, "Pick"), sealOf(salt1, picks), boxOf("box1"), "")
216 longDeck := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA+",Kneecap"), sealOf(salt1, picks), boxOf("box1"), "")
217 testing.SetRealm(testing.NewUserRealm(bob))
218 Accept(cross(cur), id, "Ironhide", deckB, braces)
219 Accept(cross(cur), badPlan, "Ironhide", deckB, braces)
220 Accept(cross(cur), badDeck, "Ironhide", deckB, braces)
221 Accept(cross(cur), longDeck, "Ironhide", deckB, braces)
222 next := sealOf(salt2, picks)
223
224 uassert.AbortsWithMessage(t, cur, "not your move", func() {
225 Reveal(cross(cur), id, deckA, saltA, picks, salt1, next, boxOf("box2"))
226 })
227
228 testing.SetRealm(testing.NewUserRealm(alice))
229 cases := []struct {
230 name, id, deck, deckSalt, plan, planSalt, nextSeal, nextBox, want string
231 }{
232 {"no deck", id, "", "", picks, salt1, next, boxOf("box2"), "deck required"},
233 {"wrong deck salt", id, deckA, saltB, picks, salt1, next, boxOf("box2"), "seal mismatch"},
234 {"wrong plan salt", id, deckA, saltA, picks, salt2, next, boxOf("box2"), "seal mismatch"},
235 {"short salt", id, deckA, saltA[2:], picks, salt1, next, boxOf("box2"), "invalid salt"},
236 {"illegal plan", badPlan, deckA, saltA, "Pick,Pick", salt1, next, boxOf("box2"), "illegal plan"},
237 {"illegal deck", badDeck, "Pick", saltA, picks, salt1, next, boxOf("box2"), "illegal deck"},
238 {"a ninth name repeating a card", longDeck, deckA + ",Kneecap", saltA, picks, salt1, next, boxOf("box2"), "illegal deck"},
239 {"no next seal", id, deckA, saltA, picks, salt1, "", "", "next seal required"},
240 {"bad next seal", id, deckA, saltA, picks, salt1, "abc", boxOf("box2"), "invalid seal"},
241 {"no next box", id, deckA, saltA, picks, salt1, next, "", "invalid box"},
242 {"next box not base64", id, deckA, saltA, picks, salt1, next, "box", "invalid box"},
243 }
244 for _, c := range cases {
245 uassert.AbortsWithMessage(t, cur, c.want, func() {
246 Reveal(cross(cur), c.id, c.deck, c.deckSalt, c.plan, c.planSalt, c.nextSeal, c.nextBox)
247 }, c.name)
248 }
249
250 Reveal(cross(cur), id, deckA, saltA, picks, salt1, next, boxOf("box2"))
251 testing.SetRealm(testing.NewUserRealm(bob))
252 Play(cross(cur), id, braces)
253 testing.SetRealm(testing.NewUserRealm(alice))
254 uassert.AbortsWithMessage(t, cur, "deck already revealed", func() {
255 Reveal(cross(cur), id, deckA, saltA, picks, salt2, sealOf(salt3, picks), boxOf("box3"))
256 })
257
258 testing.SkipHeights(day + 1)
259 uassert.AbortsWithMessage(t, cur, "deadline passed", func() {
260 Reveal(cross(cur), id, "", "", picks, salt2, sealOf(salt3, picks), boxOf("box3"))
261 })
262}
263
264func TestPlayRejects(cur realm, t *testing.T) {
265 reset()
266 testing.SetRealm(testing.NewUserRealm(alice))
267 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
268 testing.SetRealm(testing.NewUserRealm(bob))
269 Accept(cross(cur), id, "Ironhide", deckB, braces)
270 uassert.AbortsWithMessage(t, cur, "wrong phase", func() { Play(cross(cur), id, braces) })
271
272 testing.SetRealm(testing.NewUserRealm(alice))
273 Reveal(cross(cur), id, deckA, saltA, picks, salt1, sealOf(salt2, picks), boxOf("box2"))
274 uassert.AbortsWithMessage(t, cur, "not your move", func() { Play(cross(cur), id, braces) })
275
276 testing.SetRealm(testing.NewUserRealm(bob))
277 uassert.AbortsWithMessage(t, cur, "illegal plan", func() { Play(cross(cur), id, "Kneecap,Pick,Pick") })
278 testing.SkipHeights(day + 1)
279 uassert.AbortsWithMessage(t, cur, "deadline passed", func() { Play(cross(cur), id, braces) })
280}
281
282func TestEndClearsSealAndBox(cur realm, t *testing.T) {
283 reset()
284 testing.SetRealm(testing.NewUserRealm(alice))
285 resigned := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
286 forfeited := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
287 testing.SetRealm(testing.NewUserRealm(bob))
288 Accept(cross(cur), resigned, "Ironhide", deckB, braces)
289 Accept(cross(cur), forfeited, "Ironhide", deckB, braces)
290 testing.SetRealm(testing.NewUserRealm(alice))
291 Resign(cross(cur), resigned)
292 Reveal(cross(cur), forfeited, deckA, saltA, picks, salt1, sealOf(salt2, picks), boxOf("box2"))
293 testing.SkipHeights(day + 1)
294 ClaimForfeit(cross(cur), forfeited)
295
296 for _, id := range []string{resigned, forfeited} {
297 uassert.Equal(t, "", Match(id).Box, id)
298 m := recordOf(t, id)
299 uassert.Equal(t, "", m.deckSeal, id+": a deck seal resigned before its reveal is never checked")
300 uassert.Equal(t, "", m.seal, id)
301 uassert.Equal(t, "", m.box, id)
302 }
303}
304
305func showState(s engine.State) string {
306 out := "round " + strconv.Itoa(s.Round)
307 for _, g := range s.Gnomes {
308 out += " | hp " + strconv.Itoa(g.HP) + " vulnerable " + strconv.Itoa(g.Vulnerable) +
309 " strength " + strconv.Itoa(g.Strength) + " spent " + strconv.FormatUint(g.Spent, 2)
310 }
311 return out
312}
313
314// After every round of whole matches, the state the realm rebuilds is the engine's own.
315func TestStateMatchesEngine(cur realm, t *testing.T) {
316 rules := engine.ProofOfConcept()
317 salts := []string{salt1, salt2, salt3}
318 cases := []struct {
319 name string
320 rounds [][2]string
321 }{
322 {"worked example", [][2]string{{"Chalk Dust,Double Tap", "Hammer Blow,Spiked Shield"}, {picks, braces}, {picks, braces}}},
323 {"knockout", [][2]string{{picks, "Pick,Anvil Stomp"}, {picks, "Hammer Blow,Cobble Throw"}}},
324 {"draw", [][2]string{{braces, braces}, {braces, braces}, {braces, braces}}},
325 }
326 for _, c := range cases {
327 reset()
328 testing.SetRealm(testing.NewUserRealm(alice))
329 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salts[0], c.rounds[0][0]), boxOf("box1"), "")
330 testing.SetRealm(testing.NewUserRealm(bob))
331 Accept(cross(cur), id, "Ironhide", deckB, c.rounds[0][1])
332 uassert.Equal(t, showState(rules.Start()), showState(stateOf(rules, recordOf(t, id))), c.name+" before round 1")
333
334 for r, plans := range c.rounds {
335 deck, deckSalt, next, nextBox := "", "", "", ""
336 if r == 0 {
337 deck, deckSalt = deckA, saltA
338 }
339 if r+1 < len(c.rounds) {
340 next, nextBox = sealOf(salts[r+1], c.rounds[r+1][0]), boxOf("next")
341 }
342 testing.SetRealm(testing.NewUserRealm(alice))
343 Reveal(cross(cur), id, deck, deckSalt, plans[0], salts[r], next, nextBox)
344
345 at := c.name + " after round " + strconv.Itoa(r+1)
346 want := resolveAll(c.rounds[:r+1]...).State
347 uassert.Equal(t, showState(want), showState(stateOf(rules, recordOf(t, id))), at)
348 hp := Match(id).HP
349 urequire.Equal(t, r+1, len(hp), at)
350 uassert.Equal(t, want.Gnomes[0].HP, hp[r][0], at)
351 uassert.Equal(t, want.Gnomes[1].HP, hp[r][1], at)
352
353 if next != "" {
354 testing.SetRealm(testing.NewUserRealm(bob))
355 Play(cross(cur), id, c.rounds[r+1][1])
356 }
357 }
358 }
359}
360
361func TestFirstRevealDropsDeckSeal(cur realm, t *testing.T) {
362 reset()
363 testing.SetRealm(testing.NewUserRealm(alice))
364 id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
365 uassert.Equal(t, sealOf(saltA, deckA), recordOf(t, id).deckSeal)
366 testing.SetRealm(testing.NewUserRealm(bob))
367 Accept(cross(cur), id, "Ironhide", deckB, braces)
368 uassert.Equal(t, sealOf(saltA, deckA), recordOf(t, id).deckSeal)
369
370 testing.SetRealm(testing.NewUserRealm(alice))
371 Reveal(cross(cur), id, deckA, saltA, picks, salt1, sealOf(salt2, picks), boxOf("box2"))
372 uassert.Equal(t, "", recordOf(t, id).deckSeal)
373 uassert.Equal(t, deckA, Match(id).ChallengerDeck)
374}