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.

store_test.gno

3.33 Kb · 139 lines
  1package arena
  2
  3import (
  4	"strconv"
  5	"testing"
  6
  7	"gno.land/p/nym-gfanton001/gnomepit/engine/v0"
  8	"gno.land/p/nt/uassert/v0"
  9	"gno.land/p/nt/urequire/v0"
 10)
 11
 12func openRecord(who address, deadline int64) record {
 13	return record{phase: phaseOpen, challenger: who, deadline: deadline}
 14}
 15
 16func showIDs(ids []int) string {
 17	s := "["
 18	for i, id := range ids {
 19		if i > 0 {
 20			s += " "
 21		}
 22		s += strconv.Itoa(id)
 23	}
 24	return s + "]"
 25}
 26
 27func TestMatchesAcrossChunks(cur realm, t *testing.T) {
 28	reset()
 29	rules := engine.ProofOfConcept()
 30	lastID = 130
 31	for _, id := range []int{1, 64, 65, 130} {
 32		saveMatch(rules, id, openRecord(alice, int64(id)))
 33	}
 34	for _, id := range []int{1, 64, 65, 130} {
 35		m, ok := loadMatch(rules, id)
 36		urequire.True(t, ok, "match missing")
 37		uassert.Equal(t, int64(id), m.deadline)
 38		uassert.Equal(t, alice, m.challenger)
 39	}
 40	for _, id := range []int{66, 0, 131} {
 41		_, ok := loadMatch(rules, id)
 42		uassert.False(t, ok)
 43	}
 44	uassert.Equal(t, 3, chunks.Size())
 45}
 46
 47func TestDropMatch(cur realm, t *testing.T) {
 48	reset()
 49	rules := engine.ProofOfConcept()
 50	lastID = 64
 51	saveMatch(rules, 1, openRecord(alice, 1))
 52	saveMatch(rules, 64, openRecord(bob, 64))
 53	dropMatch(64)
 54	_, ok := loadMatch(rules, 64)
 55	uassert.False(t, ok)
 56	_, ok = loadMatch(rules, 1)
 57	uassert.True(t, ok)
 58	dropMatch(64)
 59	dropMatch(500)
 60}
 61
 62func TestPlayerIDs(cur realm, t *testing.T) {
 63	reset()
 64	uassert.Equal(t, 0, len(loadPlayer(alice).ids))
 65	p := loadPlayer(alice)
 66	p.add(1)
 67	p.add(5)
 68	p.add(9)
 69	p.add(5)
 70	savePlayer(alice, p)
 71
 72	p = loadPlayer(alice)
 73	uassert.Equal(t, "[9 5]", showIDs(p.newest(0, 2)))
 74	uassert.Equal(t, "[1]", showIDs(p.newest(2, 2)))
 75	uassert.Equal(t, "[]", showIDs(p.newest(5, 2)))
 76	p.remove(5)
 77	p.remove(77)
 78	uassert.Equal(t, "[9 1]", showIDs(p.newest(0, 50)))
 79}
 80
 81func TestPlayerKeepsLocks(cur realm, t *testing.T) {
 82	reset()
 83	p := loadPlayer(alice)
 84	p.add(1)
 85	p.locks = []sessionLock{{session: bob, id: 1, expires: 99}}
 86	savePlayer(alice, p)
 87	got := loadPlayer(alice)
 88	uassert.Equal(t, 1, len(got.locks))
 89	uassert.Equal(t, bob, got.locks[0].session)
 90	uassert.Equal(t, 1, len(got.ids))
 91}
 92
 93func TestOpenList(cur realm, t *testing.T) {
 94	reset()
 95	const now = int64(1000)
 96	openAdd(1, now+10)
 97	openAdd(2, now+20)
 98	openAdd(3, now+30)
 99	uassert.Equal(t, "[3 2]", showIDs(openNewest(now+15, 0, 50)))
100	uassert.Equal(t, "[2]", showIDs(openNewest(now+15, 1, 1)))
101	uassert.Equal(t, "[1 2]", showIDs(openClear(now+25, 8)))
102	uassert.Equal(t, "[3]", showIDs(openNewest(now, 0, 50)))
103	openRemove(9)
104	openRemove(3)
105	uassert.Equal(t, "", openList)
106}
107
108func TestOpenClearStopsAtMax(cur realm, t *testing.T) {
109	reset()
110	openAdd(1, 10)
111	openAdd(2, 20)
112	openAdd(3, 30)
113	uassert.Equal(t, "[1]", showIDs(openClear(100, 1)))
114	uassert.Equal(t, 2, len(openEntries(openList)))
115}
116
117func TestDropAbsentMatchLeavesChunk(cur realm, t *testing.T) {
118	reset()
119	rules := engine.ProofOfConcept()
120	lastID = 66
121	saveMatch(rules, 65, openRecord(alice, 65))
122	before := stringAt(&chunks, chunkKey(65))
123
124	dropMatch(66)
125	uassert.Equal(t, before, stringAt(&chunks, chunkKey(65)))
126
127	dropMatch(65)
128	dropped := stringAt(&chunks, chunkKey(65))
129	dropMatch(65)
130	uassert.Equal(t, dropped, stringAt(&chunks, chunkKey(65)))
131}
132
133func TestOpenExpiryBoundary(cur realm, t *testing.T) {
134	reset()
135	openAdd(1, 50)
136	uassert.Equal(t, "[1]", showIDs(openNewest(50, 0, 50)))
137	uassert.Equal(t, "[]", showIDs(openClear(50, 8)))
138	uassert.Equal(t, "[1]", showIDs(openClear(51, 8)))
139}