package portbook import ( "strings" "testing" "gno.land/p/nt/testutils/v0" ) func expectPanic(t *testing.T, label string, fn func()) { t.Helper() if r := revive(fn); r == nil { t.Fatalf("%s: expected panic, got none", label) } } func TestPortBookFullLifecycle(cur realm, t *testing.T) { deployer := testutils.TestAddress("deployer") alice := testutils.TestAddress("alice") bob := testutils.TestAddress("bob") admin = deployer // 1. Validation checks testing.SetRealm(testing.NewUserRealm(alice)) expectPanic(t, "empty name", func() { RegisterPort(cross(cur), "", "Utility", "", "", "gno.land/r/nym-smelt001/x", "strict", 1, 1, 0, 0, "") }) expectPanic(t, "invalid realmPath", func() { RegisterPort(cross(cur), "Bad", "Utility", "", "", "https://evil.example/x", "strict", 1, 1, 0, 0, "") }) expectPanic(t, "invalid mode", func() { RegisterPort(cross(cur), "BadMode", "Utility", "", "", "gno.land/r/nym-smelt001/x", "magic", 1, 1, 0, 0, "") }) // 2. Register two ports id1 := RegisterPort( cross(cur), "BatchDisperse", "Utility", "https://github.com/aber-dev-crypto/smelt/blob/main/example-sol/cryptobatch.disperse.sol", "sha256:disperse001", "gno.land/r/nym-smelt001/disperse", "ai-assisted", 5, 5, 2, 0, "Batch native ugnot and token sender refined from Solidity with Smelt.", ) if id1 != 1 { t.Fatalf("expected port ID 1, got %d", id1) } testing.SetRealm(testing.NewUserRealm(bob)) id2 := RegisterPort( cross(cur), "SimpleERC20 (GRC-20)", "Token", "https://github.com/aber-dev-crypto/smelt/blob/main/testdata/contracts/SimpleERC20.sol", "sha256:erc20001", "gno.land/r/nym-smelt001/demo_grc20", "idiomatic", 7, 7, 1, 0, "ERC-20 transpiled idiomatically to native GRC-20 + Ownable.", ) if id2 != 2 { t.Fatalf("expected port ID 2, got %d", id2) } // 3. Upvote and prevent double upvote UpvotePort(cross(cur), id1) if p := GetPort(id1); p == nil || p.Upvotes != 1 { t.Fatalf("expected 1 upvote on port 1, got %+v", p) } expectPanic(t, "double upvote", func() { UpvotePort(cross(cur), id1) }) // 4. Community Request + Vote + Prevent double vote + Fulfill testing.SetRealm(testing.NewUserRealm(alice)) reqID := RequestPort( cross(cur), "WETH9", "DeFi", "https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "Wrapped native GNOT interface for DeFi composability", ) if reqID != 1 { t.Fatalf("expected request ID 1, got %d", reqID) } if r := GetRequest(reqID); r == nil || r.Votes != 1 || r.FulfilledPortID != 0 { t.Fatalf("unexpected initial request state: %+v", r) } // Requester already auto-voted expectPanic(t, "requester double vote", func() { VoteRequest(cross(cur), reqID) }) // Bob votes on the request testing.SetRealm(testing.NewUserRealm(bob)) VoteRequest(cross(cur), reqID) if r := GetRequest(reqID); r == nil || r.Votes != 2 { t.Fatalf("expected 2 votes on request 1, got %+v", r) } // Random stranger cannot fulfill stranger := testutils.TestAddress("stranger") testing.SetRealm(testing.NewUserRealm(stranger)) expectPanic(t, "unauthorized fulfill", func() { FulfillRequest(cross(cur), reqID, id2) }) // Port author (bob) fulfills the request with id2 testing.SetRealm(testing.NewUserRealm(bob)) FulfillRequest(cross(cur), reqID, id2) if r := GetRequest(reqID); r == nil || r.FulfilledPortID != id2 { t.Fatalf("expected request fulfilled by port %d, got %+v", id2, r) } // Cannot vote on fulfilled request testing.SetRealm(testing.NewUserRealm(stranger)) expectPanic(t, "vote on fulfilled request", func() { VoteRequest(cross(cur), reqID) }) // 5. Verify stats ports, reqs, openReqs, votes, uniq := Stats() if ports != 2 || reqs != 1 || openReqs != 0 || votes != 3 || uniq != 2 { t.Fatalf("unexpected stats: ports=%d reqs=%d open=%d votes=%d uniq=%d", ports, reqs, openReqs, votes, uniq) } // 6. Verify Render routes home := Render("") for _, want := range []string{"Smelt PortBook", "BatchDisperse", "SimpleERC20 (GRC-20)", "WETH9", "UpvotePort", "RegisterPort", "RequestPort"} { if !strings.Contains(home, want) { t.Errorf("home Render() missing %q:\n%s", want, home) } } if detail := Render("port/1"); !strings.Contains(detail, "BatchDisperse") || !strings.Contains(detail, "sha256:disperse001") { t.Errorf("port/1 Render() unexpected:\n%s", detail) } if reqView := Render("request/1"); !strings.Contains(reqView, "WETH9") || !strings.Contains(reqView, "Fulfilled") { t.Errorf("request/1 Render() unexpected:\n%s", reqView) } if allPorts := Render("ports"); !strings.Contains(allPorts, "SimpleERC20") { t.Errorf("ports Render() unexpected:\n%s", allPorts) } if allReqs := Render("requests"); !strings.Contains(allReqs, "WETH9") { t.Errorf("requests Render() unexpected:\n%s", allReqs) } }