Skip to content Search Apps Documentation Source Content Run Reset Eval Playground 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.

Playground

gnomod.toml

module = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/steward"
gno = "0.9"

[addpkg]
  creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf"
  height = 1457804
  max_deposit = "100000000ugnot"

steward.gno

// Package steward runs the evening for the Borrowed Livery challenge. It
// is the one realm the wardrobe obeys.
package steward

import (
	"strings"

	"gno.land/p/nt/markdown/sanitize/v0"
	"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/livery"
)

const (
	maxToast  = 140 // characters the steward will read
	maxToasts = 20  // toasts remembered
)

var (
	headOfHouse = address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf")
	toasts      []string
)

// Admit dresses guest in the livery. Only the head of house admits.
func Admit(cur realm, guest address) {
	if cur.Previous().Address() != headOfHouse {
		panic("steward: only the head of house admits guests")
	}
	livery.Dress(guest)
}

// Toast asks the steward to read aloud the toast your realm composes.
func Toast(cur realm, compose func() string) {
	text := compose()
	if len(text) > maxToast {
		text = text[:maxToast]
	}
	toasts = append(toasts, text)
	if len(toasts) > maxToasts {
		toasts = toasts[len(toasts)-maxToasts:]
	}
}

// Render shows the evening's toasts.
func Render(_ string) string {
	var b strings.Builder
	b.WriteString("# The Steward\n\nThe steward runs the evening for [Borrowed Livery](/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/livery). " +
		"Realms may propose a toast with `Toast`; the steward reads it aloud.\n\n## Toasts\n\n")
	if len(toasts) == 0 {
		b.WriteString("*The room is quiet.*\n")
	}
	for i := len(toasts) - 1; i >= 0; i-- {
		b.WriteString("- " + sanitize.InlineText(toasts[i]) + "\n")
	}
	return b.String()
}

steward_test.gno

package steward

import (
	"strings"
	"testing"

	"gno.land/p/nt/uassert/v0"
	"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/livery"
)

const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")

func TestOnlyHeadOfHouseAdmits(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewUserRealm(ash))
	uassert.AbortsContains(t, cur, "only the head of house", func() { Admit(cross(cur), ash) })
}

func TestAdmitDresses(cur realm, t *testing.T) {
	guest := address("g1rh0vd3xzwjpcqehrf0tpjlqeq35079wf4zklar")
	testing.SetRealm(testing.NewUserRealm(headOfHouse))
	Admit(cross(cur), guest)
	uassert.True(t, livery.InLivery(guest))
}

func TestToastIsSanitized(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewUserRealm(ash))
	Toast(cross(cur), func() string { return "to the cellar\n# hijacked" })
	uassert.False(t, strings.Contains(Render(""), "\n# hijacked"))
	uassert.True(t, strings.Contains(Render(""), "to the cellar"))
}