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

toolbox_test.gno

3.54 Kb · 117 lines
  1package toolbox
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	tb "gno.land/p/moul/toolbox/v0"
  8	"gno.land/p/nt/uassert/v0"
  9)
 10
 11// TestEveryToolIsNested is the check that makes the compiled-in catalog safe:
 12// a copy-pasted entry that kept the wrong path would publish a dead link, and
 13// buildCatalog panics on it at deploy rather than at read.
 14func TestEveryToolIsNested(t *testing.T) {
 15	uassert.True(t, len(tools) > 0)
 16	for _, tool := range tools {
 17		uassert.NoError(t, tool.Validate())
 18		uassert.Equal(t, tool.Slug, tb.SlugOf(hubPath, tool.Path))
 19	}
 20}
 21
 22func TestReadAPI(t *testing.T) {
 23	uassert.Equal(t, len(tools), Count())
 24	uassert.True(t, Has("usernames"))
 25	uassert.False(t, Has("nope"))
 26	uassert.Equal(t, "gno.land/r/moul/toolbox/usernames", PathOf("usernames"))
 27	uassert.Equal(t, "Usernames", TitleOf("usernames"))
 28	uassert.True(t, strings.Contains(DescOf("usernames"), "address"))
 29
 30	uassert.Equal(t, "", PathOf("nope"))
 31	uassert.Equal(t, "", TitleOf("nope"))
 32	uassert.Equal(t, "", DescOf("nope"))
 33}
 34
 35func TestSlugsAreSorted(t *testing.T) {
 36	got := Slugs()
 37	uassert.Equal(t, Count(), len(got))
 38	for i := 1; i < len(got); i++ {
 39		uassert.True(t, got[i-1] < got[i])
 40	}
 41}
 42
 43func TestTags(t *testing.T) {
 44	got := Tags()
 45	uassert.True(t, len(got) > 0)
 46	found := false
 47	for _, tag := range got {
 48		if tag == "identity" {
 49			found = true
 50		}
 51	}
 52	uassert.True(t, found)
 53}
 54
 55func TestRenderIndex(t *testing.T) {
 56	got := Render("")
 57	uassert.True(t, strings.Contains(got, "# Toolbox"))
 58	uassert.True(t, strings.Contains(got, "[Usernames](/r/moul/toolbox/usernames)"))
 59	uassert.True(t, strings.Contains(got, "Tags:"))
 60}
 61
 62func TestRenderTagFilter(t *testing.T) {
 63	kept := Render("?tag=identity")
 64	uassert.True(t, strings.Contains(kept, "[Usernames](/r/moul/toolbox/usernames)"))
 65	uassert.True(t, strings.Contains(kept, "Show all"))
 66
 67	empty := Render("?tag=nope")
 68	uassert.True(t, strings.Contains(empty, "No tools yet."))
 69	uassert.False(t, strings.Contains(empty, "[Usernames]"))
 70}
 71
 72// TestTagLinkIsURLEscaped: a tag is an arbitrary string on Tool, and
 73// concatenating one into a query string unescaped turns "a&b" into two
 74// parameters and "a#b" into a fragment, so the link filters by something
 75// nobody asked for. Values copied from what QueryEscape actually emits.
 76func TestTagLinkIsURLEscaped(t *testing.T) {
 77	var c tb.Catalog
 78	uassert.NoError(t, c.Set(tb.Tool{
 79		Slug:  "weird",
 80		Path:  "gno.land/r/moul/toolbox/weird",
 81		Title: "Weird",
 82		Tags:  []string{"a&b c"},
 83	}))
 84
 85	got := tagLinks(&c, hubURL)
 86	uassert.True(t, strings.Contains(got, "?tag=a%26b+c"))
 87	uassert.False(t, strings.Contains(got, "?tag=a&b c"))
 88}
 89
 90func TestTagLinksEmpty(t *testing.T) {
 91	var c tb.Catalog
 92	uassert.Equal(t, "", tagLinks(&c, hubURL))
 93}
 94
 95func TestRenderTool(t *testing.T) {
 96	got := Render("usernames")
 97	uassert.True(t, strings.Contains(got, "[toolbox](/r/moul/toolbox)"))
 98	uassert.True(t, strings.Contains(got, "## Usernames"))
 99	uassert.True(t, strings.Contains(got, "`gno.land/r/moul/toolbox/usernames`"))
100}
101
102func TestRenderUnknown(t *testing.T) {
103	for _, path := range []string{"nope", "a/b", "usernames/extra"} {
104		got := Render(path)
105		uassert.True(t, strings.Contains(got, "# Not found"))
106		uassert.True(t, strings.Contains(got, "Back to the toolbox"))
107	}
108}
109
110// TestRenderNeverEmitsTwoBlankLines: gno collapses them in an Output block, so
111// a Render that produces one can never be pinned by an example. Checked here
112// rather than discovered later.
113func TestRenderNeverEmitsTwoBlankLines(t *testing.T) {
114	for _, path := range []string{"", "?tag=identity", "usernames", "nope"} {
115		uassert.False(t, strings.Contains(Render(path), "\n\n\n"))
116	}
117}