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.

portbook_test.gno

4.70 Kb · 149 lines
  1package portbook
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/nt/testutils/v0"
  8)
  9
 10func expectPanic(t *testing.T, label string, fn func()) {
 11	t.Helper()
 12	if r := revive(fn); r == nil {
 13		t.Fatalf("%s: expected panic, got none", label)
 14	}
 15}
 16
 17func TestPortBookFullLifecycle(cur realm, t *testing.T) {
 18	deployer := testutils.TestAddress("deployer")
 19	alice := testutils.TestAddress("alice")
 20	bob := testutils.TestAddress("bob")
 21	admin = deployer
 22
 23	// 1. Validation checks
 24	testing.SetRealm(testing.NewUserRealm(alice))
 25	expectPanic(t, "empty name", func() {
 26		RegisterPort(cross(cur), "", "Utility", "", "", "gno.land/r/nym-smelt001/x", "strict", 1, 1, 0, 0, "")
 27	})
 28	expectPanic(t, "invalid realmPath", func() {
 29		RegisterPort(cross(cur), "Bad", "Utility", "", "", "https://evil.example/x", "strict", 1, 1, 0, 0, "")
 30	})
 31	expectPanic(t, "invalid mode", func() {
 32		RegisterPort(cross(cur), "BadMode", "Utility", "", "", "gno.land/r/nym-smelt001/x", "magic", 1, 1, 0, 0, "")
 33	})
 34
 35	// 2. Register two ports
 36	id1 := RegisterPort(
 37		cross(cur),
 38		"BatchDisperse",
 39		"Utility",
 40		"https://github.com/aber-dev-crypto/smelt/blob/main/example-sol/cryptobatch.disperse.sol",
 41		"sha256:disperse001",
 42		"gno.land/r/nym-smelt001/disperse",
 43		"ai-assisted",
 44		5, 5, 2, 0,
 45		"Batch native ugnot and token sender refined from Solidity with Smelt.",
 46	)
 47	if id1 != 1 {
 48		t.Fatalf("expected port ID 1, got %d", id1)
 49	}
 50
 51	testing.SetRealm(testing.NewUserRealm(bob))
 52	id2 := RegisterPort(
 53		cross(cur),
 54		"SimpleERC20 (GRC-20)",
 55		"Token",
 56		"https://github.com/aber-dev-crypto/smelt/blob/main/testdata/contracts/SimpleERC20.sol",
 57		"sha256:erc20001",
 58		"gno.land/r/nym-smelt001/demo_grc20",
 59		"idiomatic",
 60		7, 7, 1, 0,
 61		"ERC-20 transpiled idiomatically to native GRC-20 + Ownable.",
 62	)
 63	if id2 != 2 {
 64		t.Fatalf("expected port ID 2, got %d", id2)
 65	}
 66
 67	// 3. Upvote and prevent double upvote
 68	UpvotePort(cross(cur), id1)
 69	if p := GetPort(id1); p == nil || p.Upvotes != 1 {
 70		t.Fatalf("expected 1 upvote on port 1, got %+v", p)
 71	}
 72	expectPanic(t, "double upvote", func() {
 73		UpvotePort(cross(cur), id1)
 74	})
 75
 76	// 4. Community Request + Vote + Prevent double vote + Fulfill
 77	testing.SetRealm(testing.NewUserRealm(alice))
 78	reqID := RequestPort(
 79		cross(cur),
 80		"WETH9",
 81		"DeFi",
 82		"https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
 83		"Wrapped native GNOT interface for DeFi composability",
 84	)
 85	if reqID != 1 {
 86		t.Fatalf("expected request ID 1, got %d", reqID)
 87	}
 88	if r := GetRequest(reqID); r == nil || r.Votes != 1 || r.FulfilledPortID != 0 {
 89		t.Fatalf("unexpected initial request state: %+v", r)
 90	}
 91
 92	// Requester already auto-voted
 93	expectPanic(t, "requester double vote", func() {
 94		VoteRequest(cross(cur), reqID)
 95	})
 96
 97	// Bob votes on the request
 98	testing.SetRealm(testing.NewUserRealm(bob))
 99	VoteRequest(cross(cur), reqID)
100	if r := GetRequest(reqID); r == nil || r.Votes != 2 {
101		t.Fatalf("expected 2 votes on request 1, got %+v", r)
102	}
103
104	// Random stranger cannot fulfill
105	stranger := testutils.TestAddress("stranger")
106	testing.SetRealm(testing.NewUserRealm(stranger))
107	expectPanic(t, "unauthorized fulfill", func() {
108		FulfillRequest(cross(cur), reqID, id2)
109	})
110
111	// Port author (bob) fulfills the request with id2
112	testing.SetRealm(testing.NewUserRealm(bob))
113	FulfillRequest(cross(cur), reqID, id2)
114	if r := GetRequest(reqID); r == nil || r.FulfilledPortID != id2 {
115		t.Fatalf("expected request fulfilled by port %d, got %+v", id2, r)
116	}
117
118	// Cannot vote on fulfilled request
119	testing.SetRealm(testing.NewUserRealm(stranger))
120	expectPanic(t, "vote on fulfilled request", func() {
121		VoteRequest(cross(cur), reqID)
122	})
123
124	// 5. Verify stats
125	ports, reqs, openReqs, votes, uniq := Stats()
126	if ports != 2 || reqs != 1 || openReqs != 0 || votes != 3 || uniq != 2 {
127		t.Fatalf("unexpected stats: ports=%d reqs=%d open=%d votes=%d uniq=%d", ports, reqs, openReqs, votes, uniq)
128	}
129
130	// 6. Verify Render routes
131	home := Render("")
132	for _, want := range []string{"Smelt PortBook", "BatchDisperse", "SimpleERC20 (GRC-20)", "WETH9", "UpvotePort", "RegisterPort", "RequestPort"} {
133		if !strings.Contains(home, want) {
134			t.Errorf("home Render() missing %q:\n%s", want, home)
135		}
136	}
137	if detail := Render("port/1"); !strings.Contains(detail, "BatchDisperse") || !strings.Contains(detail, "sha256:disperse001") {
138		t.Errorf("port/1 Render() unexpected:\n%s", detail)
139	}
140	if reqView := Render("request/1"); !strings.Contains(reqView, "WETH9") || !strings.Contains(reqView, "Fulfilled") {
141		t.Errorf("request/1 Render() unexpected:\n%s", reqView)
142	}
143	if allPorts := Render("ports"); !strings.Contains(allPorts, "SimpleERC20") {
144		t.Errorf("ports Render() unexpected:\n%s", allPorts)
145	}
146	if allReqs := Render("requests"); !strings.Contains(allReqs, "WETH9") {
147		t.Errorf("requests Render() unexpected:\n%s", allReqs)
148	}
149}