linktree_test.gno
2.07 Kb · 60 lines
1package linktree
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/testutils/v0"
8 "gno.land/p/nt/uassert/v0"
9)
10
11func TestProfileLifecycle(cur realm, t *testing.T) {
12 alice := testutils.TestAddress("alice")
13 testing.SetRealm(testing.NewUserRealm(alice))
14
15 SetBio(cross(cur), "gnome builder")
16 AddLink(cross(cur), "Home", "https://alice.example")
17 AddLink(cross(cur), "GitHub", "https://github.com/alice")
18
19 out := Render("/" + alice.String())
20 uassert.True(t, strings.Contains(out, "gnome builder"), "bio present")
21 uassert.True(t, strings.Contains(out, "[Home](https://alice.example)"), "first link present")
22 uassert.True(t, strings.Contains(out, "[GitHub](https://github.com/alice)"), "second link present")
23
24 // Remove the first link; second must survive.
25 RemoveLink(cross(cur), 0)
26 out = Render("/" + alice.String())
27 uassert.False(t, strings.Contains(out, "[Home]"), "removed link gone")
28 uassert.True(t, strings.Contains(out, "[GitHub](https://github.com/alice)"), "kept link present")
29}
30
31func TestIndexAndOwnership(cur realm, t *testing.T) {
32 alice := testutils.TestAddress("alice2")
33 bob := testutils.TestAddress("bob2")
34
35 testing.SetRealm(testing.NewUserRealm(alice))
36 AddLink(cross(cur), "A", "https://a.example")
37
38 testing.SetRealm(testing.NewUserRealm(bob))
39 AddLink(cross(cur), "B", "https://b.example")
40
41 // Root index lists both addresses.
42 idx := Render("")
43 uassert.True(t, strings.Contains(idx, alice.String()), "alice listed")
44 uassert.True(t, strings.Contains(idx, bob.String()), "bob listed")
45
46 // bob is current caller; his profile has only his link, not alice's.
47 bobOut := Render("/" + bob.String())
48 uassert.True(t, strings.Contains(bobOut, "[B](https://b.example)"), "bob has own link")
49 uassert.False(t, strings.Contains(bobOut, "[A]"), "bob does not have alice's link")
50
51 // Out-of-range removal on bob aborts.
52 uassert.AbortsWithMessage(t, cur, "index out of range", func() {
53 RemoveLink(cross(cur), 99)
54 })
55}
56
57func TestUnknownProfile(t *testing.T) {
58 out := Render("/g1doesnotexist")
59 uassert.True(t, strings.Contains(out, "No profile for this address"), "unknown profile message")
60}