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.

quillbounty_test.gno

6.73 Kb · 180 lines
  1package quillbounty
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"strings"
  7	"testing"
  8
  9	"gno.land/p/nt/avl/v0"
 10	"gno.land/p/nt/testutils/v0"
 11	"gno.land/p/nt/uassert/v0"
 12	"gno.land/p/nt/urequire/v0"
 13)
 14
 15const testPkgPath = "gno.land/r/g1ypczwgczq2rkmgxc2t898tpsv5n8ecq3gj9fsv/quillbounty"
 16
 17func resetState() {
 18	bounties = avl.NewTree()
 19	builders = avl.NewTree()
 20	nextBountyID = 1
 21	totalFunded = 0
 22	totalPaidOut = 0
 23	totalCompleted = 0
 24}
 25
 26func TestFullLifecycle_CreateTopUpSubmitSplitPayout(cur realm, t *testing.T) {
 27	defer resetState()
 28
 29	pkgAddr := chain.PackageAddress(testPkgPath)
 30	sponsor := testutils.TestAddress("sponsor")
 31	reviewer := testutils.TestAddress("reviewer")
 32	builderA := testutils.TestAddress("builderA")
 33	builderB := testutils.TestAddress("builderB")
 34
 35	// Seed realm vault to mirror ugnot credited by tx send-envelope in production
 36	testing.IssueCoins(pkgAddr, chain.Coins{{Denom: DenomUgnot, Amount: 2_000_000}})
 37
 38	// 1. Sponsor creates a 1.5 GNOT bounty with an independent reviewer
 39	testing.SetRealm(testing.NewUserRealm(sponsor))
 40	testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 1_500_000}})
 41	bountyID := CreateBounty(
 42		cross(cur),
 43		"Build Gno MCP Indexer",
 44		"ai",
 45		"Deliver an MCP server querying Onyx realms with automated tests.",
 46		reviewer.String(),
 47		100,
 48	)
 49	uassert.Equal(t, uint64(1), bountyID)
 50
 51	// 2. Another funder tops up +0.5 GNOT
 52	testing.SetRealm(testing.NewUserRealm(reviewer))
 53	testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 500_000}})
 54	TopUpBounty(cross(cur), bountyID)
 55
 56	b := GetBounty(bountyID)
 57	urequire.NotEqual(t, nil, b)
 58	uassert.Equal(t, int64(2_000_000), b.TotalFunded)
 59	uassert.Equal(t, int64(2_000_000), b.RemainingUgnot)
 60
 61	// 3. Builder A & Builder B submit verifiable work
 62	testing.SetRealm(testing.NewUserRealm(builderA))
 63	subA := SubmitWork(cross(cur), bountyID, "https://github.com/minorCoder/quillbounty", "abc1234", "Delivered MCP bridge + tests")
 64	uassert.Equal(t, uint64(1), subA)
 65
 66	testing.SetRealm(testing.NewUserRealm(builderB))
 67	subB := SubmitWork(cross(cur), bountyID, "gno.land/r/demo/helper", "def5678", "Added benchmark suite")
 68	uassert.Equal(t, uint64(2), subB)
 69
 70	// 4. Reviewer requests revision on Builder B, then approves split payouts (1.2 GNOT + remaining 0.8 GNOT)
 71	testing.SetRealm(testing.NewUserRealm(reviewer))
 72	RequestRevision(cross(cur), bountyID, subB, "Please include edge-case assertions")
 73	uassert.Equal(t, string(SubNeedsRevision), string(GetBounty(bountyID).Submissions[1].Status))
 74
 75	ApproveSubmission(cross(cur), bountyID, subA, 1_200_000, "Clean primary implementation")
 76	uassert.Equal(t, string(StatusOpen), string(GetBounty(bountyID).Status))
 77	uassert.Equal(t, int64(800_000), GetBounty(bountyID).RemainingUgnot)
 78
 79	// payoutUgnot == 0 releases entire remaining balance (800_000 ugnot) and closes bounty
 80	ApproveSubmission(cross(cur), bountyID, subB, 0, "Great benchmark additions")
 81
 82	bFinal := GetBounty(bountyID)
 83	uassert.Equal(t, string(StatusCompleted), string(bFinal.Status))
 84	uassert.Equal(t, int64(0), bFinal.RemainingUgnot)
 85
 86	// Verify real on-chain balances received
 87	ro := banker.NewReadonlyBanker()
 88	uassert.Equal(t, int64(1_200_000), ro.GetCoin(builderA, DenomUgnot))
 89	uassert.Equal(t, int64(800_000), ro.GetCoin(builderB, DenomUgnot))
 90
 91	// Verify Builder Reputation Ledger
 92	profA := GetBuilderProfile(builderA)
 93	urequire.NotEqual(t, nil, profA)
 94	uassert.Equal(t, 1, profA.CompletedCount)
 95	uassert.Equal(t, int64(1_200_000), profA.TotalEarnedUgnot)
 96
 97	// Verify Render routes (Home, Detail, Builders, JSON API)
 98	home := Render("")
 99	uassert.True(t, strings.Contains(home, "QuillBounty"))
100	uassert.True(t, strings.Contains(home, "Build Gno MCP Indexer"))
101
102	detail := Render("bounty/1")
103	uassert.True(t, strings.Contains(detail, "COMPLETED"))
104	uassert.True(t, strings.Contains(detail, "abc1234"))
105
106	board := Render("builders")
107	uassert.True(t, strings.Contains(board, builderA.String()))
108
109	apiJSON := Render("api/bounties")
110	uassert.True(t, strings.Contains(apiJSON, `"protocol":"QuillBounty"`))
111	uassert.True(t, strings.Contains(apiJSON, `"totalPaidOutUgnot":2000000`))
112
113	detailJSON := Render("api/bounty/1")
114	uassert.True(t, strings.Contains(detailJSON, `"submissions":[`))
115}
116
117func TestExpiryRefundSafetyControl(cur realm, t *testing.T) {
118	defer resetState()
119
120	pkgAddr := chain.PackageAddress(testPkgPath)
121	sponsor := testutils.TestAddress("sponsor_exp")
122	other := testutils.TestAddress("stranger")
123
124	testing.IssueCoins(pkgAddr, chain.Coins{{Denom: DenomUgnot, Amount: 1_000_000}})
125	testing.SetRealm(testing.NewUserRealm(sponsor))
126	testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 1_000_000}})
127	id := CreateBounty(cross(cur), "Expired Task Test", "security", "Audit state transitions before launch.", "", 20)
128
129	// Refund before deadline must panic
130	uassert.AbortsWithMessage(t, cur, "RefundExpired: deadline height not reached yet", func(cur realm) {
131		RefundExpired(cur, id)
132	})
133
134	// Advance chain past deadline
135	testing.SkipHeights(25)
136
137	// Non-sponsor cannot reclaim funds
138	testing.SetRealm(testing.NewUserRealm(other))
139	uassert.AbortsWithMessage(t, cur, "RefundExpired: only sponsor can reclaim expired funds", func(cur realm) {
140		RefundExpired(cur, id)
141	})
142
143	// Sponsor reclaims full escrow safely
144	testing.SetRealm(testing.NewUserRealm(sponsor))
145	refunded := RefundExpired(cross(cur), id)
146	uassert.Equal(t, int64(1_000_000), refunded)
147	uassert.Equal(t, string(StatusExpired), string(GetBounty(id).Status))
148	uassert.Equal(t, int64(1_000_000), banker.NewReadonlyBanker().GetCoin(sponsor, DenomUgnot))
149}
150
151func TestAuthorizationAndValidationGuards(cur realm, t *testing.T) {
152	defer resetState()
153
154	sponsor := testutils.TestAddress("sponsor_sec")
155	attacker := testutils.TestAddress("attacker")
156	builder := testutils.TestAddress("honest_builder")
157
158	testing.SetRealm(testing.NewUserRealm(sponsor))
159	testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 500_000}})
160	id := CreateBounty(cross(cur), "Security Guard Test", "security", "Verify caller identity and input validation.", "", 50)
161
162	// Sponsor cannot self-submit
163	uassert.AbortsWithMessage(t, cur, "SubmitWork: sponsor cannot submit to own bounty", func(cur realm) {
164		SubmitWork(cur, id, "https://github.com/test/repo", "111", "self submit attempt")
165	})
166
167	// Invalid proof URL rejected
168	testing.SetRealm(testing.NewUserRealm(builder))
169	uassert.AbortsWithMessage(t, cur, "SubmitWork: proofURL must start with https:// or gno.land/", func(cur realm) {
170		SubmitWork(cur, id, "javascript:alert(1)", "111", "bad url scheme")
171	})
172
173	subID := SubmitWork(cross(cur), id, "https://github.com/minorCoder/quillbounty", "999", "Valid PR submission")
174
175	// Unauthorized attacker cannot approve payout
176	testing.SetRealm(testing.NewUserRealm(attacker))
177	uassert.AbortsWithMessage(t, cur, "ApproveSubmission: unauthorized reviewer", func(cur realm) {
178		ApproveSubmission(cur, id, subID, 0, "steal funds")
179	})
180}