Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/home/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186407 max_deposit = "5000000ugnot"
home.gno
package home
import "chain/runtime/unsafe"
// admin is the only address allowed to mutate the dashboard state.
const admin = address("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
var (
todos []string
status string
memeImgURL string
)
func init() {
todos = append(todos, "fill this todo list...")
status = "Online" // Initial status set to "Online"
memeImgURL = "https://i.imgflip.com/7ze8dc.jpg"
}
func assertIsAdmin() {
if unsafe.PreviousRealm().Address() != admin {
panic("restricted to admin")
}
}
func Render(path string) string {
content := "# Manfred's (gn)home Dashboard\n\n"
content += "## Meme\n"
content += "\n\n"
content += "## Status\n"
content += status + "\n\n"
content += "## Personal ToDo List\n"
for _, todo := range todos {
content += "- [ ] " + todo + "\n"
}
content += "\n"
// TODO: Implement a feature to list replies on r/boards on my posts
// TODO: Maybe integrate a calendar feature for upcoming events?
return content
}
func AddNewTodo(cur realm, todo string) {
assertIsAdmin()
todos = append(todos, todo)
}
func DeleteTodo(cur realm, todoIndex int) {
assertIsAdmin()
if todoIndex >= 0 && todoIndex < len(todos) {
// Remove the todo from the list by merging slices from before and after the todo
todos = append(todos[:todoIndex], todos[todoIndex+1:]...)
} else {
panic("Invalid todo index")
}
}
func UpdateStatus(cur realm, newStatus string) {
assertIsAdmin()
status = newStatus
}
home_test.gno
package home
import (
"strings"
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
var notAdmin = testutils.TestAddress("mallory")
// reset restores the globals to what init() sets, so assertions do not depend
// on which tests ran before. Realm globals persist for the whole test binary.
func reset() {
todos = []string{"fill this todo list..."}
status = "Online"
memeImgURL = "https://i.imgflip.com/7ze8dc.jpg"
}
// ExampleRender pins the dashboard's default output. It resets state first so
// the example is deterministic regardless of test ordering (Go runs examples
// after tests, and the tests below mutate these globals).
func ExampleRender() {
reset()
print(Render(""))
// Output:
// # Manfred's (gn)home Dashboard
//
// ## Meme
// 
//
// ## Status
// Online
//
// ## Personal ToDo List
// - [ ] fill this todo list...
}
// TestRenderIgnoresPath documents that Render serves the same dashboard for
// any path — it takes no sub-routes.
func TestRenderIgnoresPath(t *testing.T) {
reset()
uassert.Equal(t, Render(""), Render("/anything"))
}
// TestRenderReflectsState checks Render tracks the admin-mutable fields.
func TestRenderReflectsState(cur realm, t *testing.T) {
reset()
testing.SetRealm(testing.NewUserRealm(admin))
UpdateStatus(cross(cur), "Away")
AddNewTodo(cross(cur), "ship the thing")
out := Render("")
uassert.True(t, strings.Contains(out, "## Status\nAway\n"), "status should show Away")
uassert.True(t, strings.Contains(out, "- [ ] ship the thing\n"), "new todo should be listed")
uassert.True(t, strings.Contains(out, "- [ ] fill this todo list...\n"), "original todo should remain")
}
// TestDeleteTodo removes by index and keeps the rest.
func TestDeleteTodo(cur realm, t *testing.T) {
reset()
testing.SetRealm(testing.NewUserRealm(admin))
AddNewTodo(cross(cur), "first")
AddNewTodo(cross(cur), "second")
DeleteTodo(cross(cur), 1) // drop "first"
out := Render("")
uassert.False(t, strings.Contains(out, "- [ ] first\n"), "deleted todo should be gone")
uassert.True(t, strings.Contains(out, "- [ ] second\n"), "other todo should remain")
}
// TestDeleteTodoOutOfRange covers both bounds of the index check.
func TestDeleteTodoOutOfRange(cur realm, t *testing.T) {
reset()
testing.SetRealm(testing.NewUserRealm(admin))
uassert.AbortsWithMessage(t, cur, "Invalid todo index", func() {
DeleteTodo(cross(cur), -1)
})
uassert.AbortsWithMessage(t, cur, "Invalid todo index", func() {
DeleteTodo(cross(cur), len(todos))
})
}
// TestAdminGating checks every mutator rejects a non-admin caller.
func TestAdminGating(cur realm, t *testing.T) {
reset()
testing.SetRealm(testing.NewUserRealm(notAdmin))
uassert.AbortsWithMessage(t, cur, "restricted to admin", func() {
AddNewTodo(cross(cur), "nope")
})
uassert.AbortsWithMessage(t, cur, "restricted to admin", func() {
DeleteTodo(cross(cur), 0)
})
uassert.AbortsWithMessage(t, cur, "restricted to admin", func() {
UpdateStatus(cross(cur), "hacked")
})
// State must be untouched.
uassert.Equal(t, "Online", status)
uassert.Equal(t, 1, len(todos))
}