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.

query_test.gno

6.20 Kb · 189 lines
  1package standings
  2
  3import (
  4	"strconv"
  5	"testing"
  6
  7	"gno.land/p/nt/testutils/v0"
  8	"gno.land/p/nt/uassert/v0"
  9	"gno.land/p/nt/urequire/v0"
 10	"gno.land/r/sys/users"
 11)
 12
 13// giveName registers name for a in r/sys/users, as the users realm's genesis controller
 14// does; the registry outlives reset, so a name given once stays.
 15func giveName(cur realm, a address, name string) {
 16	if !users.ResolveAddress(a).IsDeleted() {
 17		return
 18	}
 19	testing.SetRealm(testing.NewCodeRealm("gno.land/r/sys/users/init"))
 20	if err := users.RegisterUser(cross(cur), name, a); err != nil {
 21		panic(err.Error())
 22	}
 23}
 24
 25func ranks(list []Standing) string {
 26	out := ""
 27	for _, s := range list {
 28		out += strconv.Itoa(s.Rank) + " "
 29	}
 30	return out
 31}
 32
 33func TestStandingOfUnrated(t *testing.T) {
 34	reset()
 35	s := StandingOf(carol)
 36	uassert.Equal(t, 0, s.Rank)
 37	uassert.Equal(t, carol.String(), s.Player)
 38	uassert.Equal(t, "", s.Name)
 39	uassert.Equal(t, "", s.MainClan)
 40	uassert.Equal(t, 1000, s.Rating)
 41	uassert.Equal(t, 0, s.Wins+s.Losses+s.Draws)
 42}
 43
 44func TestStandingOfNamed(cur realm, t *testing.T) {
 45	reset()
 46	giveName(cur, alice, "alice")
 47	testing.SetRealm(testing.NewCodeRealm(arenaV0))
 48	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
 49	s := StandingOf(alice)
 50	uassert.Equal(t, "alice", s.Name)
 51	uassert.Equal(t, 1, s.Rank)
 52	uassert.Equal(t, "Pickers", s.MainClan)
 53	uassert.Equal(t, 1016, s.Rating)
 54	uassert.Equal(t, 1, s.Wins)
 55	uassert.Equal(t, "", StandingOf(bob).Name, "an address without a name has none")
 56	uassert.Equal(t, 2, StandingOf(bob).Rank)
 57}
 58
 59func TestLeaderboardSharedRankAcrossPages(t *testing.T) {
 60	reset()
 61	seed(alice, 1016)
 62	seed(bob, 1016)
 63	seed(carol, 1016)
 64	seed(dave, 952)
 65	uassert.Equal(t, "1 1 ", ranks(Leaderboard(0, 2)))
 66	uassert.Equal(t, "1 4 ", ranks(Leaderboard(2, 2)), "a page that opens inside a tie keeps its shared rank")
 67	uassert.Equal(t, "1 1 1 4 ", ranks(Leaderboard(0, 50)))
 68}
 69
 70func TestLeaderboardLimits(t *testing.T) {
 71	reset()
 72	uassert.PanicsWithMessage(t, nil, "limit must be 1 to 50", func() { Leaderboard(0, 0) })
 73	uassert.PanicsWithMessage(t, nil, "limit must be 1 to 50", func() { Leaderboard(0, 51) })
 74	uassert.PanicsWithMessage(t, nil, "offset must not be negative", func() { Leaderboard(-1, 10) })
 75	list := Leaderboard(0, 50)
 76	uassert.True(t, list != nil)
 77	uassert.Equal(t, 0, len(list))
 78}
 79
 80func TestLeaderboardFiftyNamed(cur realm, t *testing.T) {
 81	reset()
 82	for i := 0; i < 50; i++ {
 83		a := testutils.TestAddress("player" + strconv.Itoa(i))
 84		giveName(cur, a, "player"+strconv.Itoa(i))
 85		seed(a, 1000+i)
 86	}
 87	list := Leaderboard(0, 50)
 88	urequire.Equal(t, 50, len(list))
 89	for i, s := range list {
 90		uassert.Equal(t, 1049-i, s.Rating)
 91		uassert.Equal(t, i+1, s.Rank)
 92		uassert.Equal(t, "player"+strconv.Itoa(49-i), s.Name)
 93	}
 94}
 95
 96func TestRatedPlayers(cur realm, t *testing.T) {
 97	reset()
 98	uassert.Equal(t, 0, RatedPlayers())
 99	testing.SetRealm(testing.NewCodeRealm(arenaV0))
100	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
101	uassert.Equal(t, 2, RatedPlayers())
102}
103
104func TestClanStandings(cur realm, t *testing.T) {
105	reset()
106	list := ClanStandings()
107	uassert.True(t, list != nil)
108	uassert.Equal(t, 0, len(list))
109
110	testing.SetRealm(testing.NewCodeRealm(arenaV0))
111	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
112	Record(cross(cur), carol, dave, "Delvers", "Delvers", "a")
113	list = ClanStandings()
114	urequire.Equal(t, 2, len(list), "a clan with only mirror matches is not listed")
115	uassert.Equal(t, "Pickers", list[0].Clan)
116	uassert.Equal(t, 1, list[0].Rank)
117	uassert.Equal(t, 1008, list[0].Rating)
118	uassert.Equal(t, 1, list[0].Wins)
119	uassert.Equal(t, "Ironhide", list[1].Clan)
120	uassert.Equal(t, 2, list[1].Rank)
121	uassert.Equal(t, 992, list[1].Rating)
122	uassert.Equal(t, 1, list[1].Losses)
123}
124
125func TestResultsPageAcrossChunks(cur realm, t *testing.T) {
126	reset()
127	testing.SetRealm(testing.NewCodeRealm(arenaV0))
128	for i := 0; i < 65; i++ {
129		Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
130	}
131	page := Results(62, 5)
132	urequire.Equal(t, 3, len(page))
133	for i, r := range page {
134		uassert.Equal(t, 63+i, r.Seq)
135	}
136	uassert.Equal(t, 65, ResultCount())
137}
138
139func TestLeaderboardAndStandingOfAgree(cur realm, t *testing.T) {
140	reset()
141	testing.SetRealm(testing.NewCodeRealm(arenaV0))
142	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
143	Record(cross(cur), carol, dave, "Delvers", "Pickers", "draw")
144	Record(cross(cur), carol, alice, "Delvers", "Pickers", "a")
145	Record(cross(cur), dave, bob, "Ironhide", "Ironhide", "b")
146	Record(cross(cur), bob, carol, "Ironhide", "Delvers", "a")
147	erin, frank, gina := testutils.TestAddress("erin"), testutils.TestAddress("frank"), testutils.TestAddress("gina")
148	Record(cross(cur), erin, alice, "Pickers", "Pickers", "a")
149	Record(cross(cur), frank, gina, "Delvers", "Ironhide", "draw")
150
151	board := Leaderboard(0, 50)
152	urequire.Equal(t, 7, len(board))
153	for _, s := range board {
154		uassert.Equal(t, s.Rank, StandingOf(address(s.Player)).Rank, s.Player)
155	}
156	for off := 0; off < len(board); off += 2 {
157		for i, s := range Leaderboard(off, 2) {
158			uassert.Equal(t, board[off+i].Rank, s.Rank, "the page at offset "+strconv.Itoa(off))
159		}
160	}
161	tied := StandingOf(frank).Rank
162	uassert.True(t, tied > 1, "frank and gina tie below first place")
163	uassert.Equal(t, tied, StandingOf(gina).Rank)
164}
165
166func TestResultsPage(cur realm, t *testing.T) {
167	reset()
168	testing.SetRealm(testing.NewCodeRealm(arenaV0))
169	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
170	Record(cross(cur), bob, carol, "Ironhide", "Delvers", "b")
171	Record(cross(cur), carol, alice, "Delvers", "Pickers", "draw")
172
173	uassert.Equal(t, 3, ResultCount())
174	page := Results(1, 2)
175	urequire.Equal(t, 2, len(page))
176	uassert.Equal(t, 2, page[0].Seq)
177	uassert.Equal(t, arenaV0, page[0].Writer)
178	uassert.Equal(t, bob.String(), page[0].Challenger)
179	uassert.Equal(t, carol.String(), page[0].Acceptor)
180	uassert.Equal(t, "Ironhide", page[0].ChallengerClan)
181	uassert.Equal(t, "Delvers", page[0].AcceptorClan)
182	uassert.Equal(t, "b", page[0].Result)
183	uassert.Equal(t, 3, page[1].Seq)
184	uassert.Equal(t, "draw", page[1].Result)
185	uassert.Equal(t, "Pickers", page[1].AcceptorClan)
186	uassert.Equal(t, 0, len(Results(3, 10)), "a page past the log is empty")
187	uassert.PanicsWithMessage(t, nil, "limit must be 1 to 50", func() { Results(0, 51) })
188	uassert.PanicsWithMessage(t, nil, "offset must not be negative", func() { Results(-1, 10) })
189}