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.

hearthbounty_test.gno

13.62 Kb · 329 lines
  1package hearthbounty
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"strings"
  7	"testing"
  8
  9	"gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
 10	"gno.land/p/nt/avl/v0"
 11	"gno.land/p/nt/testutils/v0"
 12	"gno.land/p/nt/uassert/v0"
 13)
 14
 15func resetState(initialArbiter address) {
 16	arbiter = initialArbiter
 17	realmWebBase = "/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v0"
 18	bountySeq = 0
 19	bounties = avl.NewTree()
 20	profiles = avl.NewTree()
 21	openCount = 0
 22	claimedCount = 0
 23	submittedCount = 0
 24	disputedCount = 0
 25	settledCount = 0
 26	cancelledCount = 0
 27	agentCount = 0
 28	totalEscrowUgnot = 0
 29	totalSettledUgnot = 0
 30	totalRefundUgnot = 0
 31	initRouter()
 32}
 33
 34func assertEscrowInvariant(t *testing.T, realmAddr address, initialRealmBal int64) {
 35	t.Helper()
 36	bnk := banker.NewReadonlyBanker()
 37	bal := bnk.GetCoin(realmAddr, hearthcore.Denom)
 38	uassert.Equal(t, initialRealmBal+totalEscrowUgnot, bal)
 39}
 40
 41func TestFullBountyLifecycleAndReputation(cur realm, t *testing.T) {
 42	arb := testutils.TestAddress("arbiter")
 43	sponsor := testutils.TestAddress("sponsor")
 44	agent := testutils.TestAddress("agent_hunter")
 45	donor := testutils.TestAddress("crowdfund_donor")
 46	realmAddr := chain.PackageAddress("gno.land/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v0")
 47
 48	testing.SetRealm(testing.NewUserRealm(arb))
 49	resetState(arb)
 50	baseRealmBal := banker.NewReadonlyBanker().GetCoin(realmAddr, hearthcore.Denom)
 51
 52	// Register an AI agent profile.
 53	testing.SetRealm(testing.NewUserRealm(agent))
 54	RegisterProfile(cross(cur), "anvil-ai", "Autonomous Gno realm builder", "gno,audit,mcp", "mcp://anvil.local")
 55	prof, ok := GetProfile(agent)
 56	uassert.True(t, ok)
 57	uassert.True(t, prof.IsAgent)
 58	uassert.Equal(t, "anvil-ai", prof.Handle)
 59	uassert.Equal(t, 1, GetStats().AgentCount)
 60
 61	// Sponsor posts a 2.00 GNOT bounty.
 62	testing.SetOriginCaller(sponsor)
 63	testing.SetRealm(testing.NewUserRealm(sponsor))
 64	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 2000000}})
 65	testing.IssueCoins(realmAddr, chain.Coins{{Denom: "ugnot", Amount: 2000000}})
 66	id := PostBounty(cross(cur), "Build AVL Leaderboard Helper", "Provide a deterministic AVL sorting helper with 100% unit test coverage.", "realm", 200)
 67	uassert.Equal(t, uint64(1), id)
 68	uassert.Equal(t, int64(2000000), GetStats().TotalEscrowUgnot)
 69	assertEscrowInvariant(t, realmAddr, baseRealmBal)
 70
 71	// Supporter tops up +0.50 GNOT.
 72	testing.SetOriginCaller(donor)
 73	testing.SetRealm(testing.NewUserRealm(donor))
 74	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 500000}})
 75	testing.IssueCoins(realmAddr, chain.Coins{{Denom: "ugnot", Amount: 500000}})
 76	newEscrow := TopUpBounty(cross(cur), id)
 77	uassert.Equal(t, int64(2500000), newEscrow)
 78	assertEscrowInvariant(t, realmAddr, baseRealmBal)
 79
 80	// Sponsor cannot claim own bounty.
 81	testing.SetOriginCaller(sponsor)
 82	testing.SetRealm(testing.NewUserRealm(sponsor))
 83	uassert.AbortsWithMessage(t, cur, "hearthbounty: sponsor cannot claim own bounty", func() {
 84		ClaimBounty(cross(cur), id, "self claim")
 85	})
 86
 87	// Agent claims bounty.
 88	testing.SetOriginCaller(agent)
 89	testing.SetRealm(testing.NewUserRealm(agent))
 90	ClaimBounty(cross(cur), id, "Claiming via hearthbounty-mcp; ETA 40 blocks.")
 91	b, ok := GetBounty(id)
 92	uassert.True(t, ok)
 93	uassert.Equal(t, string(hearthcore.StatusClaimed), string(b.Status))
 94	uassert.Equal(t, agent, b.Claimant)
 95
 96	// Agent submits proof.
 97	SubmitProof(cross(cur), id, "https://github.com/hearthbounty/demo/pull/42", "sha256:9f86d081884c7d659a2feaa0c55ad015", "Implemented AVL leaderboard helper and passed all 18 tests.")
 98	b, _ = GetBounty(id)
 99	uassert.Equal(t, string(hearthcore.StatusSubmitted), string(b.Status))
100	uassert.Equal(t, uint64(1), b.SubmissionCount)
101
102	// Sponsor requests a revision.
103	testing.SetOriginCaller(sponsor)
104	testing.SetRealm(testing.NewUserRealm(sponsor))
105	RequestRevision(cross(cur), id, "Please add edge-case tests for empty trees.")
106	b, _ = GetBounty(id)
107	uassert.Equal(t, string(hearthcore.StatusClaimed), string(b.Status))
108	uassert.Equal(t, "Please add edge-case tests for empty trees.", b.RevisionFeedback)
109
110	// Agent resubmits revised proof.
111	testing.SetOriginCaller(agent)
112	testing.SetRealm(testing.NewUserRealm(agent))
113	SubmitProof(cross(cur), id, "https://github.com/hearthbounty/demo/pull/42", "sha256:4b227777d4dd1fc61c6f884f48641d02", "Added empty-tree and tie-breaker unit tests.")
114	b, _ = GetBounty(id)
115	uassert.Equal(t, uint64(2), b.SubmissionCount)
116
117	// Non-sponsor cannot settle.
118	uassert.AbortsWithMessage(t, cur, "hearthbounty: only the sponsor may approve and settle", func() {
119		ApproveAndSettle(cross(cur), id, 5, "unauthorized")
120	})
121
122	// Sponsor approves & settles; verify exact ugnot transfer and reputation update.
123	agentBalBefore := banker.NewReadonlyBanker().GetCoin(agent, hearthcore.Denom)
124	testing.SetOriginCaller(sponsor)
125	testing.SetRealm(testing.NewUserRealm(sponsor))
126	ApproveAndSettle(cross(cur), id, 5, "Clean idiomatic Gno code and crisp tests!")
127
128	agentBalAfter := banker.NewReadonlyBanker().GetCoin(agent, hearthcore.Denom)
129	uassert.Equal(t, agentBalBefore+2500000, agentBalAfter)
130	assertEscrowInvariant(t, realmAddr, baseRealmBal)
131
132	b, _ = GetBounty(id)
133	uassert.Equal(t, string(hearthcore.StatusSettled), string(b.Status))
134	uassert.Equal(t, int64(0), b.EscrowUgnot)
135	uassert.Equal(t, int64(2500000), b.PayoutUgnot)
136	uassert.Equal(t, uint8(5), b.Rating)
137
138	prof, _ = GetProfile(agent)
139	uassert.Equal(t, uint64(1), prof.SettledCount)
140	uassert.Equal(t, int64(2500000), prof.EarnedUgnot)
141	uassert.Equal(t, uint64(5), prof.RatingSum)
142	uassert.Equal(t, uint64(1), prof.RatingCount)
143}
144
145func TestCancelAndExpiredClaimRelease(cur realm, t *testing.T) {
146	arb := testutils.TestAddress("arbiter2")
147	sponsor := testutils.TestAddress("sponsor2")
148	hunter := testutils.TestAddress("hunter2")
149	bystander := testutils.TestAddress("bystander2")
150	realmAddr := chain.PackageAddress("gno.land/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v0")
151
152	testing.SetRealm(testing.NewUserRealm(arb))
153	resetState(arb)
154	baseRealmBal := banker.NewReadonlyBanker().GetCoin(realmAddr, hearthcore.Denom)
155
156	// Post a bounty with short 20-block claim window.
157	testing.SetOriginCaller(sponsor)
158	testing.SetRealm(testing.NewUserRealm(sponsor))
159	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 1000000}})
160	testing.IssueCoins(realmAddr, chain.Coins{{Denom: "ugnot", Amount: 1000000}})
161	id := PostBounty(cross(cur), "Write gnoweb SVG Guide", "Document deterministic SVG rendering in gnoweb v1.8.", "docs", 20)
162
163	// Hunter claims it.
164	testing.SetOriginCaller(hunter)
165	testing.SetRealm(testing.NewUserRealm(hunter))
166	ClaimBounty(cross(cur), id, "On it")
167
168	// Sponsor cannot cancel while claimed.
169	testing.SetOriginCaller(sponsor)
170	testing.SetRealm(testing.NewUserRealm(sponsor))
171	uassert.AbortsWithMessage(t, cur, "hearthbounty: only open bounties can be cancelled", func() {
172		CancelBounty(cross(cur), id)
173	})
174
175	// Bystander cannot release before deadline.
176	testing.SetOriginCaller(bystander)
177	testing.SetRealm(testing.NewUserRealm(bystander))
178	uassert.AbortsWithMessage(t, cur, "hearthbounty: claim deadline has not expired yet", func() {
179		ReleaseExpiredClaim(cross(cur), id)
180	})
181
182	// Advance chain height past deadline -> bystander releases stale claim.
183	testing.SkipHeights(25)
184	ReleaseExpiredClaim(cross(cur), id)
185	b, _ := GetBounty(id)
186	uassert.Equal(t, string(hearthcore.StatusOpen), string(b.Status))
187	uassert.Equal(t, address(""), b.Claimant)
188
189	// Now sponsor cancels and recovers full 1.00 GNOT escrow.
190	sponsorBalBefore := banker.NewReadonlyBanker().GetCoin(sponsor, hearthcore.Denom)
191	testing.SetOriginCaller(sponsor)
192	testing.SetRealm(testing.NewUserRealm(sponsor))
193	CancelBounty(cross(cur), id)
194
195	sponsorBalAfter := banker.NewReadonlyBanker().GetCoin(sponsor, hearthcore.Denom)
196	uassert.Equal(t, sponsorBalBefore+1000000, sponsorBalAfter)
197	assertEscrowInvariant(t, realmAddr, baseRealmBal)
198	uassert.Equal(t, int64(1000000), GetStats().TotalRefundUgnot)
199}
200
201func TestDisputeSplitResolutionAndArbiterTransfer(cur realm, t *testing.T) {
202	arb := testutils.TestAddress("arbiter3")
203	arb2 := testutils.TestAddress("arbiter3_new")
204	sponsor := testutils.TestAddress("sponsor3")
205	hunter := testutils.TestAddress("hunter3")
206	realmAddr := chain.PackageAddress("gno.land/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v0")
207
208	testing.SetRealm(testing.NewUserRealm(arb))
209	resetState(arb)
210	baseRealmBal := banker.NewReadonlyBanker().GetCoin(realmAddr, hearthcore.Denom)
211
212	// Transfer arbiter role.
213	SetArbiter(cross(cur), arb2)
214	uassert.Equal(t, arb2, Arbiter())
215
216	// Post, claim, submit.
217	testing.SetOriginCaller(sponsor)
218	testing.SetRealm(testing.NewUserRealm(sponsor))
219	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 1000000}})
220	testing.IssueCoins(realmAddr, chain.Coins{{Denom: "ugnot", Amount: 1000000}})
221	id := PostBounty(cross(cur), "Audit Interrealm Payment Guard", "Check all crossing guards and produce formal report.", "audit", 100)
222
223	testing.SetOriginCaller(hunter)
224	testing.SetRealm(testing.NewUserRealm(hunter))
225	ClaimBounty(cross(cur), id, "Auditing now")
226	SubmitProof(cross(cur), id, "https://github.com/hearthbounty/audit/blob/main/report.md", "sha256:abcdef0123456789", "Delivered partial audit covering 70% of scope.")
227
228	// Hunter opens dispute.
229	OpenDispute(cross(cur), id, "Core findings delivered; requesting 70/30 partial settlement.")
230	b, _ := GetBounty(id)
231	uassert.Equal(t, string(hearthcore.StatusDisputed), string(b.Status))
232
233	// Old arbiter cannot resolve.
234	testing.SetOriginCaller(arb)
235	testing.SetRealm(testing.NewUserRealm(arb))
236	uassert.AbortsWithMessage(t, cur, "hearthbounty: only the arbiter may resolve disputes", func() {
237		ResolveDispute(cross(cur), id, 7000, "unauthorized")
238	})
239
240	// New arbiter resolves 70% hunter / 30% sponsor refund.
241	hunterBalBefore := banker.NewReadonlyBanker().GetCoin(hunter, hearthcore.Denom)
242	sponsorBalBefore := banker.NewReadonlyBanker().GetCoin(sponsor, hearthcore.Denom)
243
244	testing.SetOriginCaller(arb2)
245	testing.SetRealm(testing.NewUserRealm(arb2))
246	ResolveDispute(cross(cur), id, 7000, "70% of specification verified; 30% refunded to sponsor.")
247
248	uassert.Equal(t, hunterBalBefore+700000, banker.NewReadonlyBanker().GetCoin(hunter, hearthcore.Denom))
249	uassert.Equal(t, sponsorBalBefore+300000, banker.NewReadonlyBanker().GetCoin(sponsor, hearthcore.Denom))
250	assertEscrowInvariant(t, realmAddr, baseRealmBal)
251
252	b, _ = GetBounty(id)
253	uassert.Equal(t, string(hearthcore.StatusSettled), string(b.Status))
254	uassert.Equal(t, int64(700000), b.PayoutUgnot)
255	uassert.Equal(t, int64(300000), b.RefundUgnot)
256}
257
258func TestSecurityGuardsAndSanitizedRender(cur realm, t *testing.T) {
259	arb := testutils.TestAddress("arbiter4")
260	sponsor := testutils.TestAddress("sponsor4")
261	realmAddr := chain.PackageAddress("gno.land/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v0")
262
263	testing.SetRealm(testing.NewUserRealm(arb))
264	resetState(arb)
265
266	// Cross-realm wrapper calls must be rejected by runtime.AssertOriginCall().
267	testing.SetOriginCaller(sponsor)
268	testing.SetRealm(testing.NewUserRealm(sponsor))
269	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 1000000}})
270	uassert.AbortsWithMessage(t, cur, "invalid non-origin call", func() {
271		PostBounty(cross(cur), "Cross Call", "Should abort because called through uassert closure.", "tool", 100)
272	})
273
274	// Reject zero coins, dust below 0.10 GNOT, and unsupported denominations.
275	testing.SetOriginSend(chain.Coins{})
276	uassert.PanicsWithMessage(t, cur, "hearthbounty: must attach ugnot escrow coins", func() {
277		requireSingleUgnotSend(hearthcore.MinRewardUgnot)
278	})
279
280	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 50000}})
281	uassert.PanicsWithMessage(t, cur, "hearthbounty: attached ugnot is below minimum required amount", func() {
282		requireSingleUgnotSend(hearthcore.MinRewardUgnot)
283	})
284
285	testing.SetOriginSend(chain.Coins{{Denom: "uatom", Amount: 1000000}})
286	uassert.PanicsWithMessage(t, cur, "hearthbounty: only ugnot coins are accepted", func() {
287		requireSingleUgnotSend(hearthcore.MinRewardUgnot)
288	})
289
290	// Reject markdown button/form/math injection in rendered output.
291	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 1500000}})
292	testing.IssueCoins(realmAddr, chain.Coins{{Denom: "ugnot", Amount: 1500000}})
293	id := PostBounty(
294		cross(cur),
295		`Test <gno-button href="/r/evil" label="Steal" /> $GNOT/$ATOM`,
296		"Normal spec with $100 and <gno-form exec=\"Drain\"> attempt.\n<gno-form exec=\"Drain2\">\n</gno-form>",
297		"agent",
298		100,
299	)
300
301	home := Render("")
302	uassert.True(t, strings.Contains(home, "HearthBounty"))
303	uassert.True(t, strings.Contains(home, "data:image/svg+xml;base64,"))
304	uassert.False(t, strings.Contains(home, `<gno-button href="/r/evil"`))
305	uassert.True(t, strings.Contains(home, `\$GNOT/\$ATOM`))
306
307	detail := Render("bounty/1")
308	uassert.True(t, strings.Contains(detail, "Bounty #1:"))
309	uassert.True(t, strings.Contains(detail, `<gno-form exec="ClaimBounty">`))
310	uassert.False(t, strings.Contains(detail, `<gno-form exec="Drain">`))
311
312	// Verify all other routes render cleanly.
313	uassert.True(t, strings.Contains(Render("post"), `<gno-form exec="PostBounty">`))
314	uassert.True(t, strings.Contains(Render("leaderboard"), "Guild Reputation Leaderboard"))
315	uassert.True(t, strings.Contains(Render("agents"), "AI Agent"))
316	uassert.True(t, strings.Contains(Render("category/agent"), "AI Agent Bounties"))
317	uassert.True(t, strings.Contains(Render("profile/"+string(sponsor)), string(sponsor)))
318	uassert.True(t, strings.Contains(Render("unknown/path"), "Route Not Found"))
319
320	// Verify JSON export endpoints.
321	statsJSON := ExportStatsJSON()
322	uassert.True(t, strings.Contains(statsJSON, `"total_bounties":1`))
323	bountyJSON := ExportBountyJSON(id)
324	uassert.True(t, strings.Contains(bountyJSON, `"found":true`))
325	listJSON := ExportBountiesJSON("open", "agent", 1000000, 10)
326	uassert.True(t, strings.Contains(listJSON, `"count":1`))
327	lbJSON := ExportLeaderboardJSON(10)
328	uassert.True(t, strings.Contains(lbJSON, `"count":1`))
329}