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/nym-moultest123/x/daily/todos/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186408
  max_deposit = "5000000ugnot"

todos.gno

// Package todos is a shared, on-chain to-do board realm.
//
// Anyone can add an item; the caller's address is recorded as the author.
// Items can be toggled (open/done) or removed. Render exposes the board as
// Markdown checkboxes with open/done counts, newest item first.
package todos

import (
	"strconv"
	"strings"

	"gno.land/p/nt/avl/v0"
)

// item is one entry on the board. `address` is the uverse bech32 type — it is
// persistable (unlike a `realm` value), so it is safe to store across txs.
type item struct {
	id     int
	text   string
	done   bool
	author address
}

var (
	// items is keyed by zero-padded id string so avl iteration is ordered by id.
	items  avl.Tree
	nextID int
)

// Add appends a new item authored by the caller and returns its id.
//
// Crossing function: takes `cur realm` first (gno 0.9 interrealm convention),
// authenticates with cur.IsCurrent() before trusting cur.Previous().
func Add(cur realm, text string) int {
	if !cur.IsCurrent() {
		panic("spoofed realm")
	}
	text = strings.TrimSpace(text)
	if text == "" {
		panic("todos: text must not be empty")
	}

	author := cur.Previous().Address()

	nextID++
	id := nextID
	items.Set(idKey(id), &item{
		id:     id,
		text:   text,
		done:   false,
		author: author,
	})
	return id
}

// Toggle flips the done state of the item with the given id.
func Toggle(cur realm, id int) {
	if !cur.IsCurrent() {
		panic("spoofed realm")
	}
	v := items.Get(idKey(id))
	if v == nil {
		panic("todos: item not found: #" + strconv.Itoa(id))
	}
	it := v.(*item)
	it.done = !it.done
}

// Remove deletes the item with the given id.
func Remove(cur realm, id int) {
	if !cur.IsCurrent() {
		panic("spoofed realm")
	}
	if _, removed := items.Remove(idKey(id)); !removed {
		panic("todos: item not found: #" + strconv.Itoa(id))
	}
}

// idKey renders an id as a fixed-width, zero-padded key so avl.Tree's
// byte-lexicographic ordering matches numeric id ordering.
func idKey(id int) string {
	s := strconv.Itoa(id)
	const width = 12
	if len(s) >= width {
		return s
	}
	return strings.Repeat("0", width-len(s)) + s
}

// shortAddr renders an address as "g1abc…" (first 6 chars + ellipsis).
func shortAddr(a address) string {
	s := a.String()
	if len(s) <= 6 {
		return s
	}
	return s[:6] + "…"
}

// counts returns the number of open and done items.
func counts() (open, done int) {
	items.Iterate("", "", func(_ string, v interface{}) bool {
		if v.(*item).done {
			done++
		} else {
			open++
		}
		return false
	})
	return open, done
}

// Render returns the board as Markdown. Newest item (highest id) first.
//
// Render is NOT a crossing function (no `cur realm` param) — it is read-only
// and invoked by gnoweb, not via MsgCall.
func Render(path string) string {
	open, done := counts()

	var b strings.Builder
	b.WriteString("# Shared To-Do Board\n\n")
	b.WriteString(strconv.Itoa(open) + " open · ")
	b.WriteString(strconv.Itoa(done) + " done · ")
	b.WriteString(strconv.Itoa(open+done) + " total\n\n")

	if open+done == 0 {
		b.WriteString("_No items yet. Add one with `Add(text)`._\n")
		return b.String()
	}

	// Collect in ascending id order, then emit newest first.
	var lines []string
	items.Iterate("", "", func(_ string, v interface{}) bool {
		it := v.(*item)
		mark := " "
		if it.done {
			mark = "x"
		}
		line := "- [" + mark + "] #" + strconv.Itoa(it.id) + " " +
			it.text + " (" + shortAddr(it.author) + ")"
		lines = append(lines, line)
		return false
	})

	for i := len(lines) - 1; i >= 0; i-- {
		b.WriteString(lines[i])
		b.WriteString("\n")
	}

	return b.String()
}

todos_test.gno

package todos

import (
	"strings"
	"testing"

	"gno.land/p/nt/avl/v0"
	"gno.land/p/nt/testutils/v0"
)

// reset clears package globals between tests (t.Run/subtests do not reset them).
func reset() {
	items = avl.Tree{}
	nextID = 0
}

// TestAddToggleRemove exercises the full lifecycle through the crossing API.
// Crossing test: takes `cur realm` and calls via cross(cur).
func TestAddToggleRemove(cur realm, t *testing.T) {
	reset()
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	id1 := Add(cross(cur), "buy milk")
	if id1 != 1 {
		t.Fatalf("Add first id = %d, want 1", id1)
	}
	id2 := Add(cross(cur), "walk dog")
	if id2 != 2 {
		t.Fatalf("Add second id = %d, want 2", id2)
	}

	open, done := counts()
	if open != 2 || done != 0 {
		t.Fatalf("after adds: open=%d done=%d, want 2/0", open, done)
	}

	// Toggle #1 done.
	Toggle(cross(cur), 1)
	open, done = counts()
	if open != 1 || done != 1 {
		t.Fatalf("after toggle: open=%d done=%d, want 1/1", open, done)
	}

	// Toggle #1 back to open.
	Toggle(cross(cur), 1)
	open, done = counts()
	if open != 2 || done != 0 {
		t.Fatalf("after re-toggle: open=%d done=%d, want 2/0", open, done)
	}

	// Remove #1.
	Remove(cross(cur), 1)
	open, done = counts()
	if open != 1 || done != 0 {
		t.Fatalf("after remove: open=%d done=%d, want 1/0", open, done)
	}
	if items.Has(idKey(1)) {
		t.Fatalf("item #1 still present after Remove")
	}
}

// TestRender checks the Markdown output shape and counts. Render is not a
// crossing function, so this is a plain test — but the Add calls that seed
// state still need a realm, so it also takes `cur realm`.
func TestRender(cur realm, t *testing.T) {
	reset()

	// Empty board.
	empty := Render("")
	if !strings.Contains(empty, "0 open · 0 done · 0 total") {
		t.Fatalf("empty render missing zero counts:\n%s", empty)
	}
	if !strings.Contains(empty, "No items yet") {
		t.Fatalf("empty render missing placeholder:\n%s", empty)
	}

	bob := testutils.TestAddress("bob")
	testing.SetRealm(testing.NewUserRealm(bob))

	Add(cross(cur), "first")
	Add(cross(cur), "second")
	Add(cross(cur), "third")
	Toggle(cross(cur), 2) // mark #2 done

	out := Render("")

	// Counts line: 2 open, 1 done, 3 total.
	if !strings.Contains(out, "2 open · 1 done · 3 total") {
		t.Fatalf("render counts wrong:\n%s", out)
	}
	// Done checkbox for #2.
	if !strings.Contains(out, "- [x] #2 second") {
		t.Fatalf("render missing done item #2:\n%s", out)
	}
	// Open checkbox for #1.
	if !strings.Contains(out, "- [ ] #1 first") {
		t.Fatalf("render missing open item #1:\n%s", out)
	}
	// Newest first: #3 must appear before #1.
	if strings.Index(out, "#3 third") > strings.Index(out, "#1 first") {
		t.Fatalf("render not newest-first:\n%s", out)
	}
	// Author short form present.
	if !strings.Contains(out, "(g1") {
		t.Fatalf("render missing author short address:\n%s", out)
	}
}

// TestIDKeyOrdering verifies idKey produces lexicographically-ordered keys so
// avl iteration matches numeric id order. Non-crossing: plain signature.
func TestIDKeyOrdering(t *testing.T) {
	if idKey(2) >= idKey(10) {
		t.Fatalf("idKey(2)=%q should sort before idKey(10)=%q", idKey(2), idKey(10))
	}
	if idKey(9) >= idKey(100) {
		t.Fatalf("idKey(9)=%q should sort before idKey(100)=%q", idKey(9), idKey(100))
	}
	if len(idKey(1)) != 12 {
		t.Fatalf("idKey width = %d, want 12", len(idKey(1)))
	}
}