package quillbounty import ( "chain" "chain/banker" "strings" "testing" "gno.land/p/nt/avl/v0" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" "gno.land/p/nt/urequire/v0" ) const testPkgPath = "gno.land/r/g1ypczwgczq2rkmgxc2t898tpsv5n8ecq3gj9fsv/quillbounty" func resetState() { bounties = avl.NewTree() builders = avl.NewTree() nextBountyID = 1 totalFunded = 0 totalPaidOut = 0 totalCompleted = 0 } func TestFullLifecycle_CreateTopUpSubmitSplitPayout(cur realm, t *testing.T) { defer resetState() pkgAddr := chain.PackageAddress(testPkgPath) sponsor := testutils.TestAddress("sponsor") reviewer := testutils.TestAddress("reviewer") builderA := testutils.TestAddress("builderA") builderB := testutils.TestAddress("builderB") // Seed realm vault to mirror ugnot credited by tx send-envelope in production testing.IssueCoins(pkgAddr, chain.Coins{{Denom: DenomUgnot, Amount: 2_000_000}}) // 1. Sponsor creates a 1.5 GNOT bounty with an independent reviewer testing.SetRealm(testing.NewUserRealm(sponsor)) testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 1_500_000}}) bountyID := CreateBounty( cross(cur), "Build Gno MCP Indexer", "ai", "Deliver an MCP server querying Onyx realms with automated tests.", reviewer.String(), 100, ) uassert.Equal(t, uint64(1), bountyID) // 2. Another funder tops up +0.5 GNOT testing.SetRealm(testing.NewUserRealm(reviewer)) testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 500_000}}) TopUpBounty(cross(cur), bountyID) b := GetBounty(bountyID) urequire.NotEqual(t, nil, b) uassert.Equal(t, int64(2_000_000), b.TotalFunded) uassert.Equal(t, int64(2_000_000), b.RemainingUgnot) // 3. Builder A & Builder B submit verifiable work testing.SetRealm(testing.NewUserRealm(builderA)) subA := SubmitWork(cross(cur), bountyID, "https://github.com/minorCoder/quillbounty", "abc1234", "Delivered MCP bridge + tests") uassert.Equal(t, uint64(1), subA) testing.SetRealm(testing.NewUserRealm(builderB)) subB := SubmitWork(cross(cur), bountyID, "gno.land/r/demo/helper", "def5678", "Added benchmark suite") uassert.Equal(t, uint64(2), subB) // 4. Reviewer requests revision on Builder B, then approves split payouts (1.2 GNOT + remaining 0.8 GNOT) testing.SetRealm(testing.NewUserRealm(reviewer)) RequestRevision(cross(cur), bountyID, subB, "Please include edge-case assertions") uassert.Equal(t, string(SubNeedsRevision), string(GetBounty(bountyID).Submissions[1].Status)) ApproveSubmission(cross(cur), bountyID, subA, 1_200_000, "Clean primary implementation") uassert.Equal(t, string(StatusOpen), string(GetBounty(bountyID).Status)) uassert.Equal(t, int64(800_000), GetBounty(bountyID).RemainingUgnot) // payoutUgnot == 0 releases entire remaining balance (800_000 ugnot) and closes bounty ApproveSubmission(cross(cur), bountyID, subB, 0, "Great benchmark additions") bFinal := GetBounty(bountyID) uassert.Equal(t, string(StatusCompleted), string(bFinal.Status)) uassert.Equal(t, int64(0), bFinal.RemainingUgnot) // Verify real on-chain balances received ro := banker.NewReadonlyBanker() uassert.Equal(t, int64(1_200_000), ro.GetCoin(builderA, DenomUgnot)) uassert.Equal(t, int64(800_000), ro.GetCoin(builderB, DenomUgnot)) // Verify Builder Reputation Ledger profA := GetBuilderProfile(builderA) urequire.NotEqual(t, nil, profA) uassert.Equal(t, 1, profA.CompletedCount) uassert.Equal(t, int64(1_200_000), profA.TotalEarnedUgnot) // Verify Render routes (Home, Detail, Builders, JSON API) home := Render("") uassert.True(t, strings.Contains(home, "QuillBounty")) uassert.True(t, strings.Contains(home, "Build Gno MCP Indexer")) detail := Render("bounty/1") uassert.True(t, strings.Contains(detail, "COMPLETED")) uassert.True(t, strings.Contains(detail, "abc1234")) board := Render("builders") uassert.True(t, strings.Contains(board, builderA.String())) apiJSON := Render("api/bounties") uassert.True(t, strings.Contains(apiJSON, `"protocol":"QuillBounty"`)) uassert.True(t, strings.Contains(apiJSON, `"totalPaidOutUgnot":2000000`)) detailJSON := Render("api/bounty/1") uassert.True(t, strings.Contains(detailJSON, `"submissions":[`)) } func TestExpiryRefundSafetyControl(cur realm, t *testing.T) { defer resetState() pkgAddr := chain.PackageAddress(testPkgPath) sponsor := testutils.TestAddress("sponsor_exp") other := testutils.TestAddress("stranger") testing.IssueCoins(pkgAddr, chain.Coins{{Denom: DenomUgnot, Amount: 1_000_000}}) testing.SetRealm(testing.NewUserRealm(sponsor)) testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 1_000_000}}) id := CreateBounty(cross(cur), "Expired Task Test", "security", "Audit state transitions before launch.", "", 20) // Refund before deadline must panic uassert.AbortsWithMessage(t, cur, "RefundExpired: deadline height not reached yet", func(cur realm) { RefundExpired(cur, id) }) // Advance chain past deadline testing.SkipHeights(25) // Non-sponsor cannot reclaim funds testing.SetRealm(testing.NewUserRealm(other)) uassert.AbortsWithMessage(t, cur, "RefundExpired: only sponsor can reclaim expired funds", func(cur realm) { RefundExpired(cur, id) }) // Sponsor reclaims full escrow safely testing.SetRealm(testing.NewUserRealm(sponsor)) refunded := RefundExpired(cross(cur), id) uassert.Equal(t, int64(1_000_000), refunded) uassert.Equal(t, string(StatusExpired), string(GetBounty(id).Status)) uassert.Equal(t, int64(1_000_000), banker.NewReadonlyBanker().GetCoin(sponsor, DenomUgnot)) } func TestAuthorizationAndValidationGuards(cur realm, t *testing.T) { defer resetState() sponsor := testutils.TestAddress("sponsor_sec") attacker := testutils.TestAddress("attacker") builder := testutils.TestAddress("honest_builder") testing.SetRealm(testing.NewUserRealm(sponsor)) testing.SetOriginSend(chain.Coins{{Denom: DenomUgnot, Amount: 500_000}}) id := CreateBounty(cross(cur), "Security Guard Test", "security", "Verify caller identity and input validation.", "", 50) // Sponsor cannot self-submit uassert.AbortsWithMessage(t, cur, "SubmitWork: sponsor cannot submit to own bounty", func(cur realm) { SubmitWork(cur, id, "https://github.com/test/repo", "111", "self submit attempt") }) // Invalid proof URL rejected testing.SetRealm(testing.NewUserRealm(builder)) uassert.AbortsWithMessage(t, cur, "SubmitWork: proofURL must start with https:// or gno.land/", func(cur realm) { SubmitWork(cur, id, "javascript:alert(1)", "111", "bad url scheme") }) subID := SubmitWork(cross(cur), id, "https://github.com/minorCoder/quillbounty", "999", "Valid PR submission") // Unauthorized attacker cannot approve payout testing.SetRealm(testing.NewUserRealm(attacker)) uassert.AbortsWithMessage(t, cur, "ApproveSubmission: unauthorized reviewer", func(cur realm) { ApproveSubmission(cur, id, subID, 0, "steal funds") }) }