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.

tavern2048_test.gno

4.22 Kb · 161 lines
  1package tavern2048
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/nt/avl/v0"
  8	"gno.land/p/nt/testutils/v0"
  9	"gno.land/p/nt/uassert/v0"
 10)
 11
 12var (
 13	alice = testutils.TestAddress("alice")
 14	bob   = testutils.TestAddress("bob")
 15)
 16
 17func resetState() {
 18	games = avl.NewTree()
 19	best = avl.NewTree()
 20	totalGames = 0
 21	totalMoves = 0
 22}
 23
 24func countTiles(b Board) int {
 25	n := 0
 26	for i := 0; i < N; i++ {
 27		for j := 0; j < N; j++ {
 28			if b.Cells[i][j] != 0 {
 29				n++
 30			}
 31		}
 32	}
 33	return n
 34}
 35
 36// moveAny tries every direction until one changes the board.
 37func moveAny(cur realm, g *Game) bool {
 38	for _, d := range []string{"left", "up", "right", "down"} {
 39		before := g.Board.Moves
 40		Move(cross(cur), d)
 41		if g.Board.Moves > before {
 42			return true
 43		}
 44	}
 45	return false
 46}
 47
 48func TestNewGameAndMove(cur realm, t *testing.T) {
 49	resetState()
 50	testing.SetRealm(testing.NewUserRealm(alice))
 51	NewGame(cross(cur))
 52
 53	v := games.Get(alice.String())
 54	if v == nil {
 55		t.Fatal("alice should have a game keyed by her address")
 56	}
 57	g := v.(*Game)
 58	if countTiles(g.Board) != 2 {
 59		t.Fatalf("new board must have 2 tiles, got %d", countTiles(g.Board))
 60	}
 61	if !moveAny(cur, g) {
 62		t.Fatal("a fresh board must allow a move")
 63	}
 64	if g.Board.Moves != 1 || totalMoves != 1 {
 65		t.Fatalf("moves=%d total=%d, want 1/1", g.Board.Moves, totalMoves)
 66	}
 67	if countTiles(g.Board) < 2 {
 68		t.Fatal("a move must spawn a tile")
 69	}
 70}
 71
 72func TestPlayersAreIsolated(cur realm, t *testing.T) {
 73	resetState()
 74	testing.SetRealm(testing.NewUserRealm(alice))
 75	NewGame(cross(cur))
 76	ga := games.Get(alice.String()).(*Game)
 77	moveAny(cur, ga)
 78
 79	testing.SetRealm(testing.NewUserRealm(bob))
 80	uassert.AbortsWithMessage(t, cur, "no game yet: call NewGame first", func() {
 81		Move(cross(cur), "left")
 82	})
 83	NewGame(cross(cur))
 84	if games.Size() != 2 {
 85		t.Fatalf("want 2 games, got %d", games.Size())
 86	}
 87	if ga.Board.Moves != 1 {
 88		t.Fatal("bob must not touch alice's board")
 89	}
 90}
 91
 92func TestInvalidInput(cur realm, t *testing.T) {
 93	resetState()
 94	testing.SetRealm(testing.NewUserRealm(alice))
 95	NewGame(cross(cur))
 96	uassert.AbortsWithMessage(t, cur, "invalid direction diagonal: use up, down, left or right", func() {
 97		Move(cross(cur), "diagonal")
 98	})
 99	uassert.AbortsWithMessage(t, cur, "invalid move x: use only u, d, l, r", func() {
100		Play(cross(cur), "lrx")
101	})
102	uassert.AbortsWithMessage(t, cur, "no moves given: e.g. lldrul", func() {
103		Play(cross(cur), " , ")
104	})
105	uassert.AbortsWithMessage(t, cur, "too many moves: at most 64 per call", func() {
106		Play(cross(cur), strings.Repeat("l", 65))
107	})
108}
109
110func TestPlaySequenceAndBest(cur realm, t *testing.T) {
111	resetState()
112	testing.SetRealm(testing.NewUserRealm(alice))
113	NewGame(cross(cur))
114	g := games.Get(alice.String()).(*Game)
115	for i := 0; i < 20 && !g.Board.Over; i++ {
116		testing.SkipHeights(1)
117		Play(cross(cur), "ldru ldru ldru ldru")
118	}
119	if g.Board.Moves < 10 {
120		t.Fatalf("expected many moves, got %d", g.Board.Moves)
121	}
122	if BestOf(alice) != g.Board.Score || ScoreOf(alice) != g.Board.Score {
123		t.Fatalf("best=%d score=%d current=%d", BestOf(alice), ScoreOf(alice), g.Board.Score)
124	}
125	top := g.Board.Score
126	// a new game resets the board but keeps the best score
127	NewGame(cross(cur))
128	if ScoreOf(alice) != 0 || BestOf(alice) != top {
129		t.Fatalf("after NewGame: score=%d best=%d want 0/%d", ScoreOf(alice), BestOf(alice), top)
130	}
131}
132
133func TestRender(cur realm, t *testing.T) {
134	resetState()
135	home := Render("")
136	if !strings.Contains(home, "Tavern2048") || !strings.Contains(home, "No scores yet") {
137		t.Fatalf("empty home render wrong:\n%s", home)
138	}
139	testing.SetRealm(testing.NewUserRealm(alice))
140	NewGame(cross(cur))
141	g := games.Get(alice.String()).(*Game)
142	moveAny(cur, g)
143
144	home = Render("")
145	if !strings.Contains(home, "Leaderboard") || !strings.Contains(home, realmPath+":"+alice.String()) {
146		t.Fatalf("home must link alice's board:\n%s", home)
147	}
148	page := Render(alice.String())
149	if !strings.Contains(page, "data:image/svg+xml;base64,") {
150		t.Fatalf("player page must embed the SVG board:\n%s", page)
151	}
152	if !strings.Contains(page, "<gno-form exec=\"Play\">") {
153		t.Fatal("player page must offer the Play form")
154	}
155	if !strings.Contains(Render(bob.String()), "has not started a game") {
156		t.Fatal("unknown player page wrong")
157	}
158	if !strings.Contains(Render("nope"), "Not found") {
159		t.Fatal("bad path must render Not found")
160	}
161}