timecapsule_test.gno
2.06 Kb · 78 lines
1package timecapsule
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
12func resetState() {
13 capsules = avl.Tree{}
14 nextID = 0
15}
16
17func TestLeaveAndReveal(cur realm, t *testing.T) {
18 resetState()
19 alice := testutils.TestAddress("alice")
20 testing.SetRealm(testing.NewUserRealm(alice))
21
22 id := Leave(cross(cur), "hello, future", 5)
23 uassert.Equal(t, int64(0), id)
24
25 if got := Render(""); !strings.Contains(got, "1 still sealed") {
26 t.Fatalf("expected capsule to still be sealed, got: %s", got)
27 }
28 if got := Render("sealed"); !strings.Contains(got, "capsule/0") {
29 t.Fatalf("expected capsule 0 listed as sealed, got: %s", got)
30 }
31
32 testing.SkipHeights(5)
33
34 if got := Render(""); !strings.Contains(got, "hello, future") {
35 t.Fatalf("expected capsule to be revealed, got: %s", got)
36 }
37 if got := Render("capsule/0"); !strings.Contains(got, "hello, future") {
38 t.Fatalf("expected capsule detail to reveal message, got: %s", got)
39 }
40}
41
42func TestLeaveRejectsEmptyMessage(cur realm, t *testing.T) {
43 resetState()
44 alice := testutils.TestAddress("alice")
45 testing.SetRealm(testing.NewUserRealm(alice))
46
47 uassert.AbortsWithMessage(t, cur, "message must not be empty", func() {
48 Leave(cross(cur), " ", 5)
49 })
50}
51
52func TestLeaveRejectsBadDelay(cur realm, t *testing.T) {
53 resetState()
54 alice := testutils.TestAddress("alice")
55 testing.SetRealm(testing.NewUserRealm(alice))
56
57 uassert.AbortsWithMessage(t, cur, "delayBlocks out of range", func() {
58 Leave(cross(cur), "hi", 0)
59 })
60}
61
62// Regression: an intermediate realm (not a direct EOA call) must never be
63// able to seal a capsule as if it were a user.
64func TestLeaveRejectsCodeRealm(cur realm, t *testing.T) {
65 resetState()
66 testing.SetRealm(testing.NewCodeRealm("gno.land/r/some/attacker"))
67
68 uassert.AbortsWithMessage(t, cur, "only direct user calls can leave a capsule", func() {
69 Leave(cross(cur), "hi", 5)
70 })
71}
72
73func TestRenderNotFound(t *testing.T) {
74 resetState()
75 if got := Render("nope"); !strings.Contains(got, "not found") {
76 t.Fatalf("expected 404-ish message, got: %s", got)
77 }
78}