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

policy_test.gno

7.84 Kb · 195 lines
  1package game_world
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/uassert/v0"
  7)
  8
  9func TestGameTypePoliciesStartEmptyAndAdminRegistersOpaqueKey(cur realm, t *testing.T) {
 10	setupGameWorld(cross(cur), t)
 11	uassert.False(t, HasGameTypePolicy(testGameTypeDungeon))
 12	uassert.False(t, HasGameTypePolicy(testGameTypeJump))
 13
 14	testing.SetOriginCaller(testOperator)
 15	testing.SetRealm(testing.NewUserRealm(testOperator))
 16	SetGameTypePolicy(cross(cur), "custom_type_alpha", 17, CreationModeApprovedCreator)
 17
 18	uassert.True(t, HasGameTypePolicy("custom_type_alpha"))
 19	policy := GetGameTypePolicy("custom_type_alpha")
 20	uassert.Equal(t, int64(17), policy.CreationCost)
 21	uassert.Equal(t, int(CreationModeApprovedCreator), int(policy.CreationMode))
 22}
 23
 24func TestApprovedCreatorIsManagedPerOpaqueKey(cur realm, t *testing.T) {
 25	setupGameWorld(cross(cur), t)
 26	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
 27	setPolicyForTest(cross(cur), testGameTypeRace, 0, CreationModeApprovedCreator)
 28	testing.SetOriginCaller(testOperator)
 29	testing.SetRealm(testing.NewUserRealm(testOperator))
 30
 31	AddApprovedCreator(cross(cur), testGameTypeJump, testCreator)
 32	uassert.True(t, IsApprovedCreator(testGameTypeJump, testCreator))
 33	uassert.False(t, IsApprovedCreator(testGameTypeRace, testCreator))
 34
 35	RemoveApprovedCreator(cross(cur), testGameTypeJump, testCreator)
 36	uassert.False(t, IsApprovedCreator(testGameTypeJump, testCreator))
 37}
 38
 39func TestAddAndRemoveApprovedCreator(cur realm, t *testing.T) {
 40	setupGameWorld(cross(cur), t)
 41	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
 42	testing.SetOriginCaller(testOperator)
 43	testing.SetRealm(testing.NewUserRealm(testOperator))
 44
 45	AddApprovedCreator(cross(cur), testGameTypeJump, testCreator)
 46	uassert.True(t, IsApprovedCreator(testGameTypeJump, testCreator))
 47
 48	RemoveApprovedCreator(cross(cur), testGameTypeJump, testCreator)
 49	uassert.False(t, IsApprovedCreator(testGameTypeJump, testCreator))
 50}
 51
 52func TestSetApprovedCreatorsBulk(cur realm, t *testing.T) {
 53	setupGameWorld(cross(cur), t)
 54	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
 55	testing.SetOriginCaller(testOperator)
 56	testing.SetRealm(testing.NewUserRealm(testOperator))
 57	creatorsCSV := testCreator.String() + "," + testOtherCreator.String()
 58
 59	SetApprovedCreators(cross(cur), testGameTypeJump, creatorsCSV, true)
 60	uassert.True(t, IsApprovedCreator(testGameTypeJump, testCreator))
 61	uassert.True(t, IsApprovedCreator(testGameTypeJump, testOtherCreator))
 62
 63	SetApprovedCreators(cross(cur), testGameTypeJump, creatorsCSV, false)
 64	uassert.False(t, IsApprovedCreator(testGameTypeJump, testCreator))
 65	uassert.False(t, IsApprovedCreator(testGameTypeJump, testOtherCreator))
 66}
 67
 68func TestSetApprovedCreatorsRejectsInvalidAndEmptyCSV(cur realm, t *testing.T) {
 69	setupGameWorld(cross(cur), t)
 70	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
 71	testing.SetOriginCaller(testOperator)
 72	testing.SetRealm(testing.NewUserRealm(testOperator))
 73
 74	uassert.AbortsWithMessage(t, cur, "invalid creator address", func() {
 75		SetApprovedCreators(cross(cur), testGameTypeJump, testCreator.String()+",invalid", true)
 76	})
 77
 78	setupGameWorld(cross(cur), t)
 79	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
 80	testing.SetOriginCaller(testOperator)
 81	testing.SetRealm(testing.NewUserRealm(testOperator))
 82	uassert.AbortsWithMessage(t, cur, "creator list must not be empty", func() {
 83		SetApprovedCreators(cross(cur), testGameTypeJump, "", true)
 84	})
 85}
 86
 87func TestSetApprovedCreatorsAllowsDuplicateAddresses(cur realm, t *testing.T) {
 88	setupGameWorld(cross(cur), t)
 89	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
 90	testing.SetOriginCaller(testOperator)
 91	testing.SetRealm(testing.NewUserRealm(testOperator))
 92	creatorsCSV := testCreator.String() + "," + testCreator.String()
 93
 94	SetApprovedCreators(cross(cur), testGameTypeJump, creatorsCSV, true)
 95	uassert.True(t, IsApprovedCreator(testGameTypeJump, testCreator))
 96	uassert.Equal(t, 1, GetApprovedCreatorCount(testGameTypeJump))
 97
 98	SetApprovedCreators(cross(cur), testGameTypeJump, creatorsCSV, false)
 99	uassert.False(t, IsApprovedCreator(testGameTypeJump, testCreator))
100}
101
102func TestSetApprovedCreatorsRejectsEmptyEntryAndOversizedBatch(cur realm, t *testing.T) {
103	setupGameWorld(cross(cur), t)
104	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
105	testing.SetOriginCaller(testOperator)
106	testing.SetRealm(testing.NewUserRealm(testOperator))
107
108	uassert.AbortsWithMessage(t, cur, "invalid creator address", func() {
109		SetApprovedCreators(cross(cur), testGameTypeJump, testCreator.String()+",", true)
110	})
111
112	setupGameWorld(cross(cur), t)
113	setPolicyForTest(cross(cur), testGameTypeJump, 0, CreationModeApprovedCreator)
114	testing.SetOriginCaller(testOperator)
115	testing.SetRealm(testing.NewUserRealm(testOperator))
116	creatorsCSV := testCreator.String()
117	for i := 1; i < 101; i++ {
118		creatorsCSV += "," + testCreator.String()
119	}
120	uassert.AbortsWithMessage(t, cur, "creator list exceeds limit", func() {
121		SetApprovedCreators(cross(cur), testGameTypeJump, creatorsCSV, true)
122	})
123}
124
125func TestListApprovedCreatorsSupportsManagementProjection(cur realm, t *testing.T) {
126	setupGameWorld(cross(cur), t)
127	setPolicyForTest(cross(cur), testGameTypeDrop, 0, CreationModeApprovedCreator)
128	testing.SetOriginCaller(testOperator)
129	testing.SetRealm(testing.NewUserRealm(testOperator))
130	AddApprovedCreator(cross(cur), testGameTypeDrop, testCreator)
131	AddApprovedCreator(cross(cur), testGameTypeDrop, testOtherCreator)
132
133	uassert.Equal(t, 2, GetApprovedCreatorCount(testGameTypeDrop))
134	listed := ListApprovedCreators(testGameTypeDrop, 0, 100)
135	uassert.Equal(t, 2, len(listed))
136	uassert.True(t, containsApprovedCreator(listed, testCreator))
137	uassert.True(t, containsApprovedCreator(listed, testOtherCreator))
138}
139
140func containsApprovedCreator(creators []address, target address) bool {
141	for _, creator := range creators {
142		if creator == target {
143			return true
144		}
145	}
146	return false
147}
148
149func TestSetGameTypePolicyRejectsUnauthorizedCaller(cur realm, t *testing.T) {
150	setupGameWorld(cross(cur), t)
151	testing.SetOriginCaller(testCreator)
152	testing.SetRealm(testing.NewUserRealm(testCreator))
153
154	uassert.AbortsWithMessage(t, cur, "admin or operator access required", func() {
155		SetGameTypePolicy(cross(cur), testGameTypeJump, 10, CreationModeApprovedCreator)
156	})
157}
158
159func TestSetGameTypePolicyValidatesOpaqueKeyAndPolicy(cur realm, t *testing.T) {
160	setupGameWorld(cross(cur), t)
161	testing.SetOriginCaller(testOperator)
162	testing.SetRealm(testing.NewUserRealm(testOperator))
163
164	uassert.AbortsWithMessage(t, cur, "game type key length must be between 1 and 64", func() {
165		SetGameTypePolicy(cross(cur), "", 0, CreationModeOperatorOnly)
166	})
167	uassert.AbortsWithMessage(t, cur, "game type key length must be between 1 and 64", func() {
168		SetGameTypePolicy(cross(cur), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0, CreationModeOperatorOnly)
169	})
170	uassert.AbortsWithMessage(t, cur, "invalid game type key", func() {
171		SetGameTypePolicy(cross(cur), "Jump Map", 0, CreationModeOperatorOnly)
172	})
173	uassert.AbortsWithMessage(t, cur, "creation cost must not be negative", func() {
174		SetGameTypePolicy(cross(cur), testGameTypeJump, -1, CreationModeOperatorOnly)
175	})
176	uassert.AbortsWithMessage(t, cur, "invalid creation mode", func() {
177		SetGameTypePolicy(cross(cur), testGameTypeJump, 0, CreationModeInvalid)
178	})
179}
180
181func TestUnregisteredGameTypeIsRejected(cur realm, t *testing.T) {
182	setupGameWorld(cross(cur), t)
183	testing.SetOriginCaller(testCreator)
184	testing.SetRealm(testing.NewUserRealm(testCreator))
185
186	uassert.AbortsWithMessage(t, cur, "game type policy not found: unknown_type", func() {
187		CreateGameWorld(cross(cur), "unknown_type")
188	})
189
190	testing.SetOriginCaller(testOperator)
191	testing.SetRealm(testing.NewUserRealm(testOperator))
192	uassert.AbortsWithMessage(t, cur, "game type policy not found: unknown_type", func() {
193		AddApprovedCreator(cross(cur), "unknown_type", testCreator)
194	})
195}