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.

codec_test.gno

1.32 Kb · 44 lines
 1package arena
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
 7	"gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/game"
 8)
 9
10func TestCodecRoundTrip(t *testing.T) {
11	g := game.New(77)
12	for i := 0; i < 23; i++ {
13		g.Step(bot.Action(i%7), bot.Action((i+3)%7))
14	}
15	m := &Match{
16		ID: 42, BotA: "alpha-1", BotB: "zeta", Status: Stepping, Game: *g,
17		Challenger: address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
18		Created:    123456789012, Rated: true, Waiting: 1, Held: bot.Defend, Deadline: 123456789112,
19		Winner: 1, Forfeit: true, Reason: "zeta missed its move deadline on turn 24", Delta: -17, Settled: 99,
20	}
21	got := decode(42, m.encode())
22	if got.encode() != m.encode() {
23		t.Fatal("round trip changed the record")
24	}
25	if got.Game.Moves != g.Moves || got.Game.Coins != g.Coins || got.Game.Turn != 23 {
26		t.Fatal("game state lost")
27	}
28	if got.Challenger != m.Challenger || got.Held != bot.Defend || got.Delta != -17 || !got.Rated || got.Created != 123456789012 {
29		t.Fatalf("fields lost: %+v", got)
30	}
31	// the replay rebuilt from the decoded record matches the live game
32	if got.Game.At(23).Pos != g.At(23).Pos {
33		t.Fatal("replay differs after decoding")
34	}
35}
36
37func TestCodecRejectsCorruptRecords(t *testing.T) {
38	defer func() {
39		if recover() == nil {
40			t.Fatal("expected a panic")
41		}
42	}()
43	decode(1, "garbage")
44}