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.

timecapsule_test.gno

2.45 Kb · 84 lines
 1package timecapsule
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/store/v0"
 8	"gno.land/p/nt/testutils/v0"
 9	"gno.land/p/nt/uassert/v0"
10)
11
12func resetState() {
13	capsules = store.Named("capsule")
14}
15
16func TestLeaveAndReveal(cur realm, t *testing.T) {
17	resetState()
18	alice := testutils.TestAddress("alice")
19	testing.SetRealm(testing.NewUserRealm(alice))
20
21	// v1 ids start at 1: the store never assigns 0, so the zero id stays
22	// usable as "absent". v0 handed out 0 for the first capsule.
23	id := Leave(cross(cur), "hello, future", 5)
24	uassert.Equal(t, int64(1), id)
25
26	if got := Render(""); !strings.Contains(got, "1 still sealed") {
27		t.Fatalf("expected capsule to still be sealed, got: %s", got)
28	}
29	if got := Render("sealed"); !strings.Contains(got, "capsule/1") {
30		t.Fatalf("expected capsule 1 listed as sealed, got: %s", got)
31	}
32
33	testing.SkipHeights(5)
34
35	if got := Render(""); !strings.Contains(got, "hello, future") {
36		t.Fatalf("expected capsule to be revealed, got: %s", got)
37	}
38	if got := Render("capsule/1"); !strings.Contains(got, "hello, future") {
39		t.Fatalf("expected capsule detail to reveal message, got: %s", got)
40	}
41
42	// Id 0 is never assigned, so the path is rejected rather than looked up.
43	if got := Render("capsule/0"); !strings.Contains(got, "Invalid capsule id") {
44		t.Fatalf("expected capsule/0 to be rejected, got: %s", got)
45	}
46}
47
48func TestLeaveRejectsEmptyMessage(cur realm, t *testing.T) {
49	resetState()
50	alice := testutils.TestAddress("alice")
51	testing.SetRealm(testing.NewUserRealm(alice))
52
53	uassert.AbortsWithMessage(t, cur, "message must not be empty", func() {
54		Leave(cross(cur), "   ", 5)
55	})
56}
57
58func TestLeaveRejectsBadDelay(cur realm, t *testing.T) {
59	resetState()
60	alice := testutils.TestAddress("alice")
61	testing.SetRealm(testing.NewUserRealm(alice))
62
63	uassert.AbortsWithMessage(t, cur, "delayBlocks out of range", func() {
64		Leave(cross(cur), "hi", 0)
65	})
66}
67
68// Regression: an intermediate realm (not a direct EOA call) must never be
69// able to seal a capsule as if it were a user.
70func TestLeaveRejectsCodeRealm(cur realm, t *testing.T) {
71	resetState()
72	testing.SetRealm(testing.NewCodeRealm("gno.land/r/some/attacker"))
73
74	uassert.AbortsWithMessage(t, cur, "only direct user calls can leave a capsule", func() {
75		Leave(cross(cur), "hi", 5)
76	})
77}
78
79func TestRenderNotFound(t *testing.T) {
80	resetState()
81	if got := Render("nope"); !strings.Contains(got, "not found") {
82		t.Fatalf("expected 404-ish message, got: %s", got)
83	}
84}