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.

gnometavern_test.gno

6.93 Kb · 225 lines
  1package gnometavern
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/g17dvvky49vjqj6a4cjhwcvzctteqjm9stee000h/tavernlogic"
  8)
  9
 10// The two test players are the project's real testnet accounts; only
 11// their public addresses are used here.
 12var (
 13	alice = address("g17dvvky49vjqj6a4cjhwcvzctteqjm9stee000h")
 14	bob   = address("g15g63hzg36z8n5qjnzq6kxg4as5kms3vgqy42gw")
 15)
 16
 17func resetState() {
 18	gnomes = map[address]*tavernlogic.Gnome{}
 19	joinOrder = nil
 20	wall = nil
 21	totalServes = 0
 22}
 23
 24// NOTE: testing.SetRealm must be called directly in the test function
 25// body — it mutates the test frame's realm in place, so wrapping it in a
 26// helper function would mutate the helper's frame instead and lose it.
 27
 28// mustPanic asserts that f aborts. Realm functions are crossing
 29// functions, so their panics surface as aborts, which plain recover()
 30// cannot catch — the revive() builtin is the test-mode tool for this
 31// (same mechanism as p/nt/uassert's AbortsWithMessage).
 32func mustPanic(t *testing.T, what string, f func()) {
 33	t.Helper()
 34	if r := revive(f); r == nil {
 35		t.Fatalf("expected abort: %s", what)
 36	}
 37}
 38
 39func TestRegister(cur realm, t *testing.T) {
 40	resetState()
 41	testing.SetRealm(testing.NewUserRealm(alice))
 42	msg := Register(cross(cur), "Bram")
 43	if !strings.Contains(msg, "Bram") {
 44		t.Fatalf("welcome message does not name the gnome: %q", msg)
 45	}
 46	if GnomeCount() != 1 {
 47		t.Fatalf("GnomeCount = %d, want 1", GnomeCount())
 48	}
 49	if p := Profile(alice); !strings.Contains(p, "Bram") || !strings.Contains(p, "level 1") {
 50		t.Fatalf("profile wrong: %q", p)
 51	}
 52	// One gnome per address.
 53	mustPanic(t, "second gnome for same address", func() {
 54		Register(cross(cur), "Other")
 55	})
 56	// Invalid names.
 57	for _, bad := range []string{"ab", "SeventeenCharsLong", "Bad!Name", "with|pipe", ""} {
 58		testing.SetRealm(testing.NewUserRealm(bob))
 59		mustPanic(t, "invalid name "+bad, func() {
 60			Register(cross(cur), bad)
 61		})
 62	}
 63	// Name uniqueness across addresses.
 64	testing.SetRealm(testing.NewUserRealm(bob))
 65	mustPanic(t, "name already taken", func() {
 66		Register(cross(cur), "Bram")
 67	})
 68	Register(cross(cur), "Zed")
 69	if GnomeCount() != 2 {
 70		t.Fatalf("GnomeCount = %d, want 2", GnomeCount())
 71	}
 72	if p := Profile(bob); !strings.Contains(p, "No gnome") && !strings.Contains(p, "Zed") {
 73		t.Fatalf("bob profile wrong: %q", p)
 74	}
 75	if p := Profile(address("g1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq2rq73q")); !strings.Contains(p, "No gnome") {
 76		t.Fatalf("unknown profile wrong: %q", p)
 77	}
 78}
 79
 80func TestServeFlow(cur realm, t *testing.T) {
 81	resetState()
 82	// Unregistered callers cannot serve.
 83	testing.SetRealm(testing.NewUserRealm(bob))
 84	mustPanic(t, "serve while unregistered", func() {
 85		Serve(cross(cur), 0)
 86	})
 87	testing.SetRealm(testing.NewUserRealm(alice))
 88	Register(cross(cur), "Bram")
 89	out := Serve(cross(cur), 0)
 90	if !strings.Contains(out, "Barley Soup") {
 91		t.Fatalf("serve output wrong: %q", out)
 92	}
 93	if gnomes[alice].Coins != 3 || TotalServes() != 1 {
 94		t.Fatalf("after one serve: coins=%d totalServes=%d", gnomes[alice].Coins, TotalServes())
 95	}
 96	// Level gate through the realm: Honeyed Ham needs level 2.
 97	mustPanic(t, "serve above level", func() {
 98		Serve(cross(cur), 2)
 99	})
100	// Work up to level 2 with ales (3 XP each, 40 XP needed).
101	for gnomes[alice].Level() < 2 {
102		Serve(cross(cur), 1)
103	}
104	Serve(cross(cur), 2) // must not panic now
105	if gnomes[alice].ItemToday[2] != 1 {
106		t.Fatalf("ham serve not recorded")
107	}
108	// Unknown item.
109	mustPanic(t, "serve unknown item", func() {
110		Serve(cross(cur), 42)
111	})
112}
113
114func TestQuestClaimViaRealm(cur realm, t *testing.T) {
115	resetState()
116	testing.SetRealm(testing.NewUserRealm(alice))
117	Register(cross(cur), "Questor")
118	gnomes[alice].XP = 1000 // quest flow under test, not level gating
119
120	day := currentDay()
121	board := tavernlogic.QuestsForDay(day)
122	// Work the cheapest quest on today's board.
123	slot := 0
124	for i, q := range board {
125		if q.Count < board[slot].Count {
126			slot = i
127		}
128	}
129	q := board[slot]
130	item := 0
131	if q.Kind == tavernlogic.QuestItemServes {
132		item = q.ItemID
133	}
134	// Claiming before doing the work must panic.
135	mustPanic(t, "claim before work", func() {
136		ClaimQuest(cross(cur), slot)
137	})
138	for gnomes[alice].ProgressFor(day, q) < q.Count {
139		Serve(cross(cur), item)
140	}
141	coinsBefore := gnomes[alice].Coins
142	out := ClaimQuest(cross(cur), slot)
143	if !strings.Contains(out, q.Title) {
144		t.Fatalf("claim output wrong: %q", out)
145	}
146	if gnomes[alice].Coins != coinsBefore+q.CoinReward {
147		t.Fatalf("quest coins not paid: %d -> %d, reward %d",
148			coinsBefore, gnomes[alice].Coins, q.CoinReward)
149	}
150	// Claiming twice must panic.
151	mustPanic(t, "double claim", func() {
152		ClaimQuest(cross(cur), slot)
153	})
154}
155
156func TestFeastViaRealm(cur realm, t *testing.T) {
157	resetState()
158	testing.SetRealm(testing.NewUserRealm(alice))
159	Register(cross(cur), "Feaster")
160	mustPanic(t, "feast with no coins", func() {
161		Feast(cross(cur))
162	})
163	for i := 0; i < 10; i++ { // 10 ales = 50 coins
164		Serve(cross(cur), 1)
165	}
166	xpBefore := gnomes[alice].XP
167	out := Feast(cross(cur))
168	if !strings.Contains(out, "feasts") {
169		t.Fatalf("feast output wrong: %q", out)
170	}
171	if gnomes[alice].Coins != 0 || gnomes[alice].XP != xpBefore+tavernlogic.FeastXP {
172		t.Fatalf("feast accounting wrong: coins=%d xp=%d", gnomes[alice].Coins, gnomes[alice].XP)
173	}
174	mustPanic(t, "second feast same day", func() {
175		Feast(cross(cur))
176	})
177}
178
179func TestWallAndRender(cur realm, t *testing.T) {
180	resetState()
181	// Notes need a registered gnome.
182	testing.SetRealm(testing.NewUserRealm(bob))
183	mustPanic(t, "note while unregistered", func() {
184		PostNote(cross(cur), "hello?")
185	})
186	testing.SetRealm(testing.NewUserRealm(alice))
187	Register(cross(cur), "Bram")
188	PostNote(cross(cur), "First round is on me!")
189	testing.SetRealm(testing.NewUserRealm(bob))
190	Register(cross(cur), "Zed")
191	PostNote(cross(cur), "Save me a seat by the fire.")
192
193	home := Render("")
194	for _, want := range []string{"The Gnome Tavern", "Bram", "First round is on me!", "Today's quest board", "Leaderboard"} {
195		if !strings.Contains(home, want) {
196			t.Fatalf("home render missing %q", want)
197		}
198	}
199	if m := Render("menu"); !strings.Contains(m, "Dragonfire Stew") {
200		t.Fatalf("menu render incomplete")
201	}
202	if q := Render("quests"); !strings.Contains(q, "Quest Board") {
203		t.Fatalf("quests render wrong")
204	}
205	if lb := Render("leaderboard"); !strings.Contains(lb, "Bram") || !strings.Contains(lb, "Zed") {
206		t.Fatalf("leaderboard render missing gnomes")
207	}
208	if w := Render("wall"); !strings.Contains(w, "Save me a seat by the fire.") {
209		t.Fatalf("wall render missing note")
210	}
211	if card := Render("gnome/" + alice.String()); !strings.Contains(card, "Bram") {
212		t.Fatalf("gnome card wrong: %q", card)
213	}
214	if card := Render("gnome/g1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq2rq73q"); !strings.Contains(card, "No gnome") {
215		t.Fatalf("unknown gnome card wrong: %q", card)
216	}
217	if u := Render("cellar"); !strings.Contains(u, "Unknown page") {
218		t.Fatalf("unknown page handling wrong: %q", u)
219	}
220	// Multi-line notes are refused.
221	testing.SetRealm(testing.NewUserRealm(alice))
222	mustPanic(t, "multi-line note", func() {
223		PostNote(cross(cur), "line one\nline two")
224	})
225}