Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

home_test.gno

2.46 Kb · 73 lines
 1package home
 2
 3import (
 4	"strings"
 5	"testing"
 6	"time"
 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	mallory = testutils.TestAddress("mallory")
16)
17
18// setDay moves the block time to noon on the given day since 1970-01-01 UTC.
19func setDay(day int64) {
20	ctx := testing.GetContext()
21	ctx.Time = time.Unix(day*86400+43200, 0)
22	testing.SetContext(ctx)
23}
24
25func TestDefaultPage(t *testing.T) {
26	setDay(launchDay)
27	page := Render("")
28	for _, want := range []string{
29		"![David G., developer engineer at Samouraï Coop](data:image/svg+xml;base64,", "I'm a developer engineer at [Samouraï Coop](https://www.samourai.world/), in [samcrew](/u/samcrew)", "## Gno core", "## Projects", "## Peer Dev",
30		"## Gnordle", "[Play puzzle #1](" + home + ":wordle)", "[GnoVM, explained](",
31		"## Hearts", "**0** so far.", "$help&func=Heart",
32	} {
33		uassert.True(t, strings.Contains(page, want), "missing: "+want)
34	}
35}
36
37func TestSetGetDelete(cur realm, t *testing.T) {
38	testing.SetRealm(testing.NewUserRealm(admin))
39	Set(cross(cur), "bio", "Building on gno.")
40	uassert.Equal(t, "Building on gno.", Get("bio"))
41	Set(cross(cur), "order", "bio")
42	uassert.True(t, strings.HasPrefix(Render(""), "Building on gno."))
43
44	Delete(cross(cur), "bio")
45	uassert.Equal(t, "", Get("bio"))
46	uassert.AbortsWithMessage(t, cur, "no such section: bio", func() { Delete(cross(cur), "bio") })
47	uassert.AbortsWithMessage(t, cur, "reserved section: game", func() { Set(cross(cur), "game", "x") })
48	uassert.AbortsWithMessage(t, cur, "invalid section name", func() { Set(cross(cur), "a,b", "x") })
49
50	testing.SetRealm(testing.NewUserRealm(mallory))
51	uassert.AbortsWithMessage(t, cur, "unauthorized", func() { Set(cross(cur), "header", "pwned") })
52	uassert.AbortsWithMessage(t, cur, "unauthorized", func() { Delete(cross(cur), "header") })
53
54	for _, s := range defaultSlots {
55		slots.Set(s.name, s.body)
56	}
57}
58
59func TestHeart(cur realm, t *testing.T) {
60	hearts = avl.NewTree()
61	testing.SetRealm(testing.NewUserRealm(alice))
62	Heart(cross(cur))
63	uassert.Equal(t, 1, Hearts())
64	uassert.True(t, HasHeart(alice))
65	uassert.False(t, HasHeart(mallory))
66	uassert.AbortsWithMessage(t, cur, "already sent: one heart per address", func() { Heart(cross(cur)) })
67	uassert.True(t, strings.Contains(Render("hearts"), alice.String()))
68	uassert.True(t, strings.Contains(Render(""), "**1** so far."))
69}
70
71func TestNotFound(t *testing.T) {
72	uassert.True(t, strings.HasPrefix(Render("nope"), "# Not found"))
73}