Skip to content Search Apps Documentation Source Content 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.

escrow_test.gno

2.37 Kb · 91 lines
 1package escrow
 2
 3import (
 4	"strconv"
 5	"strings"
 6	"testing"
 7
 8	"gno.land/p/nt/avl/v0"
 9	"gno.land/p/nt/testutils/v0"
10	"gno.land/p/nt/uassert/v0"
11)
12
13var (
14	alice = testutils.TestAddress("alice")
15	bob   = testutils.TestAddress("bob")
16	carol = testutils.TestAddress("carol")
17)
18
19func resetState() {
20	deals = avl.NewTree()
21	nextID = 0
22}
23
24func TestCreateAndConfirmSettles(cur realm, t *testing.T) {
25	resetState()
26
27	// Alice creates a deal with Bob.
28	testing.SetRealm(testing.NewUserRealm(alice))
29	id := Create(cross(cur), bob, "swap NFT for 100 GNOT")
30	uassert.Equal(t, 0, id)
31
32	// Alice confirms (1/2, still open).
33	Confirm(cross(cur), id)
34	out := Render("0")
35	uassert.True(t, strings.Contains(out, "State:** Open"), "expected still open after one confirm")
36
37	// Bob confirms -> settles.
38	testing.SetRealm(testing.NewUserRealm(bob))
39	Confirm(cross(cur), id)
40
41	out = Render("0")
42	uassert.True(t, strings.Contains(out, "Settled"), "expected Settled state")
43	uassert.True(t, strings.Contains(out, "swap NFT for 100 GNOT"), "expected terms")
44}
45
46func TestCancelByCreator(cur realm, t *testing.T) {
47	resetState()
48
49	testing.SetRealm(testing.NewUserRealm(alice))
50	id := Create(cross(cur), bob, "rent payment")
51
52	Cancel(cross(cur), id)
53
54	out := Render("")
55	uassert.True(t, strings.Contains(out, "Canceled"), "expected Canceled state")
56}
57
58func TestNonPartyCannotConfirm(cur realm, t *testing.T) {
59	resetState()
60
61	testing.SetRealm(testing.NewUserRealm(alice))
62	id := Create(cross(cur), bob, "consulting")
63
64	// Carol is not a party.
65	testing.SetRealm(testing.NewUserRealm(carol))
66	uassert.AbortsWithMessage(t, cur, "escrow: caller is not a party to this deal", func() {
67		Confirm(cross(cur), id)
68	})
69}
70
71func TestOnlyCreatorCancels(cur realm, t *testing.T) {
72	resetState()
73
74	testing.SetRealm(testing.NewUserRealm(alice))
75	id := Create(cross(cur), bob, "loan")
76
77	testing.SetRealm(testing.NewUserRealm(bob))
78	uassert.AbortsWithMessage(t, cur, "escrow: only the creator can cancel", func() {
79		Cancel(cross(cur), id)
80	})
81}
82
83// The list view escaped Terms and the deal page did not, so a caller's terms
84// rendered as live markdown on the realm's own page.
85func TestDealPageEscapesTerms(cur realm, t *testing.T) {
86	resetState()
87	testing.SetRealm(testing.NewUserRealm(alice))
88	id := Create(cross(cur), bob, "[claim](https://evil.example)")
89	out := Render(strconv.Itoa(id))
90	uassert.False(t, strings.Contains(out, "[claim](https://evil.example)"), out)
91}