Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

arena_test.gno

6.91 Kb · 204 lines
  1package arena
  2
  3import (
  4	"crypto/sha256"
  5	"encoding/base64"
  6	"encoding/hex"
  7	"strconv"
  8	"strings"
  9	"testing"
 10
 11	"gno.land/p/nt/avl/v0"
 12	"gno.land/p/nt/testutils/v0"
 13	"gno.land/p/nt/uassert/v0"
 14	"gno.land/p/nt/urequire/v0"
 15)
 16
 17var (
 18	alice = testutils.TestAddress("alice")
 19	bob   = testutils.TestAddress("bob")
 20	carol = testutils.TestAddress("carol")
 21)
 22
 23const (
 24	saltA = "0101010101010101010101010101010101010101010101010101010101010101"
 25	saltB = "0202020202020202020202020202020202020202020202020202020202020202"
 26)
 27
 28// reset empties the realm's state between tests; t.Run shares package globals.
 29func reset() {
 30	lastID = 0
 31	chunks = avl.Tree{}
 32	players = avl.Tree{}
 33	openList = ""
 34	retired = false
 35}
 36
 37func sealOf(salt, text string) string {
 38	b, err := hex.DecodeString(salt)
 39	if err != nil {
 40		panic(err)
 41	}
 42	sum := sha256.Sum256(append(b, text...))
 43	return hex.EncodeToString(sum[:])
 44}
 45
 46// boxOf is s as a box argument, which the page sends in base64.
 47func boxOf(s string) string {
 48	return base64.StdEncoding.EncodeToString([]byte(s))
 49}
 50
 51func TestChallengeOpensAMatch(cur realm, t *testing.T) {
 52	reset()
 53	testing.SetRealm(testing.NewUserRealm(alice))
 54	id := Challenge(cross(cur), "Pickers", sealOf(saltA, "deck"), sealOf(saltB, "plan"), boxOf("box"), "")
 55	uassert.Equal(t, "1", id)
 56
 57	list := Open(0, 50)
 58	urequire.Equal(t, 1, len(list))
 59	c := list[0]
 60	uassert.Equal(t, "1", c.ID)
 61	uassert.Equal(t, alice.String(), c.Challenger)
 62	uassert.Equal(t, "Pickers", c.Clan)
 63	uassert.Equal(t, "", c.Opponent)
 64	uassert.Equal(t, c.Posted+604800, c.Expires)
 65}
 66
 67func TestChallengeNamesAnOpponent(cur realm, t *testing.T) {
 68	reset()
 69	testing.SetRealm(testing.NewUserRealm(alice))
 70	Challenge(cross(cur), "Ironhide", sealOf(saltA, "deck"), sealOf(saltB, "plan"), boxOf("box"), bob)
 71	uassert.Equal(t, bob.String(), Open(0, 50)[0].Opponent)
 72	uassert.Equal(t, bob.String(), Matches(alice, 0, 50)[0].Opponent)
 73}
 74
 75func TestChallengeRejects(cur realm, t *testing.T) {
 76	reset()
 77	testing.SetRealm(testing.NewUserRealm(alice))
 78	seal := sealOf(saltA, "deck")
 79	cases := []struct {
 80		name, clan, deckSeal, planSeal, box string
 81		opponent                            address
 82		want                                string
 83	}{
 84		{"unknown clan", "Elves", seal, seal, boxOf("box"), "", "unknown clan"},
 85		{"short seal", "Pickers", seal[1:], seal, boxOf("box"), "", "invalid seal"},
 86		{"uppercase seal", "Pickers", strings.ToUpper(seal), seal, boxOf("box"), "", "invalid seal"},
 87		{"non-hex seal", "Pickers", seal, "z" + seal[1:], boxOf("box"), "", "invalid seal"},
 88		{"empty box", "Pickers", seal, seal, "", "", "invalid box"},
 89		{"long box", "Pickers", seal, seal, boxOf(strings.Repeat("x", 1025)), "", "invalid box"},
 90		{"bad opponent", "Pickers", seal, seal, boxOf("box"), address("g1nope"), "invalid opponent"},
 91		{"uppercase opponent", "Pickers", seal, seal, boxOf("box"), address(strings.ToUpper(bob.String())), "invalid opponent"},
 92	}
 93	for _, c := range cases {
 94		uassert.AbortsWithMessage(t, cur, c.want, func() {
 95			Challenge(cross(cur), c.clan, c.deckSeal, c.planSeal, c.box, c.opponent)
 96		}, c.name)
 97	}
 98	uassert.Equal(t, 0, len(Open(0, 50)))
 99}
100
101func TestOpenNewestFirstAndUnexpired(cur realm, t *testing.T) {
102	reset()
103	testing.SetRealm(testing.NewUserRealm(alice))
104	seal := sealOf(saltA, "deck")
105	Challenge(cross(cur), "Pickers", seal, seal, boxOf("box"), "")
106	Challenge(cross(cur), "Pickers", seal, seal, boxOf("box"), "")
107	testing.SkipHeights(120000) // 600,000 s
108	Challenge(cross(cur), "Ironhide", seal, seal, boxOf("box"), "")
109
110	ids := func(list []OpenChallenge) string {
111		var out []string
112		for _, c := range list {
113			out = append(out, c.ID)
114		}
115		return strings.Join(out, ",")
116	}
117	uassert.Equal(t, "3,2,1", ids(Open(0, 50)))
118	uassert.Equal(t, "2", ids(Open(1, 1)))
119
120	testing.SkipHeights(1000) // the first two are now 605,000 s old
121	uassert.Equal(t, "3", ids(Open(0, 50)))
122}
123
124func TestOpenLimit(t *testing.T) {
125	uassert.PanicsWithMessage(t, nil, "limit must be 1 to 50", func() { Open(0, 0) })
126	uassert.PanicsWithMessage(t, nil, "limit must be 1 to 50", func() { Open(0, 51) })
127	uassert.PanicsWithMessage(t, nil, "offset must not be negative", func() { Open(-1, 10) })
128}
129
130func TestExpiredChallengeLeavesTheBoardBeforeClearing(cur realm, t *testing.T) {
131	reset()
132	testing.SetRealm(testing.NewUserRealm(alice))
133	id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
134	testing.SkipHeights(7 * day)
135	uassert.Equal(t, 1, len(Open(0, 50)), "a challenge is open until the end of its expiry second")
136
137	testing.SkipHeights(1)
138	uassert.Equal(t, 0, len(Open(0, 50)))
139	uassert.Equal(t, phaseExpired, Match(id).Phase)
140	list := Matches(alice, 0, 50)
141	urequire.Equal(t, 1, len(list))
142	uassert.Equal(t, phaseExpired, list[0].Phase)
143}
144
145func TestClearingAtMostEight(cur realm, t *testing.T) {
146	reset()
147	testing.SetRealm(testing.NewUserRealm(alice))
148	for i := 0; i < 10; i++ {
149		Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
150	}
151	testing.SkipHeights(7*day + 1)
152
153	Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
154	uassert.Equal(t, "11,10,9", summaryIDs(Matches(alice, 0, 50)))
155	uassert.Equal(t, "[9 10 11]", showIDs(loadPlayer(alice).ids))
156	uassert.PanicsWithMessage(t, nil, "unknown match", func() { Match("8") })
157	uassert.Equal(t, phaseExpired, Match("9").Phase)
158
159	Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
160	uassert.Equal(t, "12,11", summaryIDs(Matches(alice, 0, 50)))
161	uassert.Equal(t, "[11 12]", showIDs(loadPlayer(alice).ids))
162	uassert.Equal(t, 2, len(Open(0, 50)))
163}
164
165// A challenge whose match is gone must not block the clearing, or every later Challenge.
166func TestClearingSkipsMissingMatch(cur realm, t *testing.T) {
167	reset()
168	testing.SetRealm(testing.NewUserRealm(alice))
169	Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
170	dropMatch(1)
171	testing.SkipHeights(7*day + 1)
172
173	id := Challenge(cross(cur), "Pickers", sealOf(saltA, deckA), sealOf(salt1, picks), boxOf("box1"), "")
174	entries := openEntries(openList)
175	urequire.Equal(t, 1, len(entries))
176	uassert.Equal(t, id, strconv.Itoa(entries[0].id))
177}
178
179func TestBoxText(cur realm, t *testing.T) {
180	reset()
181	testing.SetRealm(testing.NewUserRealm(alice))
182	seal := sealOf(saltA, deckA)
183	full := boxOf(strings.Repeat("\x00\xff\x07\x80", 26))
184	urequire.Equal(t, 140, len(full))
185	cases := []struct{ name, box string }{
186		{"over 140 characters", strings.Repeat("A", 144)},
187		{"141 characters of 104 bytes", full + "\n"},
188		{"140 characters of 105 bytes", strings.Repeat("A", 140)},
189		{"unpadded", "box"},
190		{"outside the alphabet", "b*x1"},
191		{"with a newline", "Ym94\n"},
192		{"stray low bits", "YR=="},
193	}
194	for _, c := range cases {
195		uassert.AbortsWithMessage(t, cur, "invalid box", func() {
196			Challenge(cross(cur), "Pickers", seal, seal, c.box, "")
197		}, c.name)
198	}
199
200	for _, box := range []string{boxOf("box"), full} {
201		id := Challenge(cross(cur), "Pickers", seal, seal, box, "")
202		uassert.Equal(t, box, Match(id).Box)
203	}
204}