Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

game_world_test.gno

4.64 Kb · 112 lines
  1package game_world
  2
  3import (
  4	"chain"
  5	"testing"
  6
  7	"gno.land/p/nt/uassert/v0"
  8)
  9
 10func TestCreateGameWorldUsesCallerAsOwnerAndChargesExactCost(cur realm, t *testing.T) {
 11	setupGameWorld(cross(cur), t)
 12	setPolicyForTest(cross(cur), testGameTypeJump, 25, CreationModePublicCreator)
 13	collectorBefore := ugnotBalanceForTest(testFeeCollector)
 14
 15	id := createGameWorldForTest(cross(cur), testCreator, testGameTypeJump, chain.Coins{{"ugnot", 25}})
 16
 17	uassert.Equal(t, uint64(1), id)
 18	uassert.Equal(t, uint64(1), GetLatestGameWorldID())
 19	world := GetGameWorld(id)
 20	uassert.Equal(t, testCreator.String(), world.Owner.String())
 21	uassert.Equal(t, testGameTypeJump, world.GameType)
 22	uassert.Equal(t, int64(25), ugnotBalanceForTest(testFeeCollector)-collectorBefore)
 23	uassert.Equal(t, int64(0), ugnotBalanceForTest(testPkgAddr))
 24}
 25
 26func TestCreateGameWorldDoesNotRequirePersonalWorld(cur realm, t *testing.T) {
 27	setupGameWorld(cross(cur), t)
 28	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModePublicCreator)
 29
 30	uassert.True(t, HasGameWorld(createGameWorldForTest(cross(cur), testNoPersonal, testGameTypeJump, chain.Coins{})))
 31}
 32
 33func TestOperatorOnlyCreationAllowsOperator(cur realm, t *testing.T) {
 34	setupGameWorld(cross(cur), t)
 35	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeOperatorOnly)
 36
 37	uassert.True(t, HasGameWorld(createGameWorldForTest(cross(cur), testOperator, testGameTypeJump, chain.Coins{})))
 38}
 39
 40func TestApprovedCreatorFlowAndRevocation(cur realm, t *testing.T) {
 41	setupGameWorld(cross(cur), t)
 42	setPolicyForTest(cross(cur), testGameTypeRace, 0, CreationModeApprovedCreator)
 43
 44	uassert.AbortsWithMessage(t, cur, "creator is not approved for game type", func() {
 45		createGameWorldForTest(cross(cur), testCreator, testGameTypeRace, chain.Coins{})
 46	})
 47
 48	testing.SetOriginCaller(testOperator)
 49	testing.SetRealm(testing.NewUserRealm(testOperator))
 50	AddApprovedCreator(cross(cur), testGameTypeRace, testCreator)
 51	id := createGameWorldForTest(cross(cur), testCreator, testGameTypeRace, chain.Coins{})
 52	uassert.True(t, HasGameWorld(id))
 53
 54	testing.SetOriginCaller(testOperator)
 55	testing.SetRealm(testing.NewUserRealm(testOperator))
 56	RemoveApprovedCreator(cross(cur), testGameTypeRace, testCreator)
 57	uassert.True(t, HasGameWorld(id))
 58	uassert.AbortsWithMessage(t, cur, "creator is not approved for game type", func() {
 59		createGameWorldForTest(cross(cur), testCreator, testGameTypeRace, chain.Coins{})
 60	})
 61}
 62
 63func TestCreateGameWorldRejectsInexactPayment(cur realm, t *testing.T) {
 64	setupGameWorld(cross(cur), t)
 65	setPolicyForTest(cross(cur), testGameTypeDrop, 25, CreationModePublicCreator)
 66
 67	uassert.AbortsWithMessage(t, cur, "exact payment required: expected 25ugnot, got 24ugnot", func() {
 68		createGameWorldForTest(cross(cur), testCreator, testGameTypeDrop, chain.Coins{{"ugnot", 24}})
 69	})
 70	uassert.Equal(t, uint64(0), GetLatestGameWorldID())
 71
 72	uassert.AbortsWithMessage(t, cur, "exact payment required: expected 25ugnot, got 25ugnot,1foo", func() {
 73		createGameWorldForTest(cross(cur), testCreator, testGameTypeDrop, chain.Coins{{"ugnot", 25}, {"foo", 1}})
 74	})
 75	uassert.Equal(t, uint64(0), GetLatestGameWorldID())
 76}
 77
 78func TestApprovedCreatorModeDoesNotImplicitlyApproveOperator(cur realm, t *testing.T) {
 79	setupGameWorld(cross(cur), t)
 80	setPolicyForTest(cross(cur), testGameTypeRace, 0, CreationModeApprovedCreator)
 81
 82	uassert.AbortsWithMessage(t, cur, "creator is not approved for game type", func() {
 83		createGameWorldForTest(cross(cur), testOperator, testGameTypeRace, chain.Coins{})
 84	})
 85}
 86
 87func TestCreateGameWorldRejectsIDOverflow(cur realm, t *testing.T) {
 88	setupGameWorld(cross(cur), t)
 89	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModePublicCreator)
 90	latestGameWorldID = uint64(18446744073709551615)
 91
 92	uassert.AbortsWithMessage(t, cur, "game world id exhausted", func() {
 93		createGameWorldForTest(cross(cur), testCreator, testGameTypeJump, chain.Coins{})
 94	})
 95}
 96
 97func TestListLatestGameWorldsByOwner(cur realm, t *testing.T) {
 98	setupGameWorld(cross(cur), t)
 99	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModePublicCreator)
100	firstID := createGameWorldForTest(cross(cur), testCreator, testGameTypeJump, chain.Coins{})
101	createGameWorldForTest(cross(cur), testOtherCreator, testGameTypeJump, chain.Coins{})
102	for i := 1; i <= gameWorldListLimit; i++ {
103		createGameWorldForTest(cross(cur), testCreator, testGameTypeJump, chain.Coins{})
104	}
105
106	worlds := ListLatestGameWorldsByOwner(testCreator)
107	uassert.Equal(t, gameWorldListLimit, len(worlds))
108	uassert.Equal(t, firstID+2, worlds[0].ID)
109	uassert.Equal(t, firstID+uint64(gameWorldListLimit+1), worlds[len(worlds)-1].ID)
110	uassert.Equal(t, testGameTypeJump, worlds[0].GameType)
111	uassert.Equal(t, 0, len(ListLatestGameWorldsByOwner(address("g1missing"))))
112}