Skip to content Search Apps Documentation Source Content 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.

game_migration_test.gno

2.32 Kb · 79 lines
 1package game32
 2
 3import (
 4	"strconv"
 5	"testing"
 6	"time"
 7)
 8
 9// TestClaimBuckets_LazyMigration verifies that a player whose claimBuckets
10// entries are in the old per-rarity format (addr+":"+r) are automatically
11// migrated to the packed format on the next ClaimAndMint.
12func TestClaimBuckets_LazyMigration(cur realm, t *testing.T) {
13	setup()
14	p := ta("migrate-player")
15
16	// Write old-format per-rarity bucket entries directly.
17	nowTs := time.Now().Unix()
18	for r := 0; r < 7; r++ {
19		oldKey := p.String() + ":" + strconv.Itoa(r)
20		// Write a non-default value so we can verify it survived migration.
21		claimBuckets.Set(oldKey, "9999:"+strconv.FormatInt(nowTs-100, 10))
22	}
23
24	// Confirm old keys exist.
25	for r := 0; r < 7; r++ {
26		if !claimBuckets.Has(p.String() + ":" + strconv.Itoa(r)) {
27			t.Fatalf("setup: old key r=%d missing", r)
28		}
29	}
30
31	// Trigger a ClaimAndMint — this calls loadPackedBuckets which lazy-migrates.
32	n, e, s := voucher(t, "claim", p, `[["grin","1"]]`)
33	testing.SetRealm(testing.NewUserRealm(p))
34	ClaimAndMint(cross(cur), `[["grin","1"]]`, n, e, s)
35
36	// Old keys must be gone.
37	for r := 0; r < 7; r++ {
38		if claimBuckets.Has(p.String() + ":" + strconv.Itoa(r)) {
39			t.Errorf("old key r=%d still exists after migration", r)
40		}
41	}
42
43	// New packed key must exist.
44	if !claimBuckets.Has(p.String()) {
45		t.Error("packed key missing after migration")
46	}
47}
48
49// TestClaimBuckets_NewPlayerSingleKey verifies a brand-new player creates
50// exactly one claimBuckets key (the packed format) on first ClaimAndMint.
51func TestClaimBuckets_NewPlayerSingleKey(cur realm, t *testing.T) {
52	setup()
53	p := ta("new-player-bucket")
54
55	// Confirm no bucket state exists yet.
56	if claimBuckets.Has(p.String()) {
57		t.Fatal("setup: packed key should not exist yet")
58	}
59	for r := 0; r < 7; r++ {
60		if claimBuckets.Has(p.String() + ":" + strconv.Itoa(r)) {
61			t.Fatalf("setup: old key r=%d should not exist", r)
62		}
63	}
64
65	// First claim.
66	n, e, s := voucher(t, "claim", p, `[["grin","1"]]`)
67	testing.SetRealm(testing.NewUserRealm(p))
68	ClaimAndMint(cross(cur), `[["grin","1"]]`, n, e, s)
69
70	// Exactly one key for this player in claimBuckets.
71	if !claimBuckets.Has(p.String()) {
72		t.Error("packed key missing after first claim")
73	}
74	for r := 0; r < 7; r++ {
75		if claimBuckets.Has(p.String() + ":" + strconv.Itoa(r)) {
76			t.Errorf("old-format key r=%d unexpectedly created", r)
77		}
78	}
79}