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