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/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/todos/v1"
gno = "0.9"
private = true

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 914308
  max_deposit = "6000000ugnot"

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/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/store/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.
// It carries no id field: the id belongs to the store, which hands it back on
// lookup and iteration.
type item struct {
	text   string
	done   bool
	author address
}

// items assigns the item ids. v1 kept its own nextID plus an idKey() that
// zero-padded to width 12, which stopped ordering Render past 10^12.
var items = store.Named("todos: item")

// 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()

	return int(items.Add(&item{
		text:   text,
		done:   false,
		author: author,
	}))
}

// Toggle flips the done state of the item with the given id.
func Toggle(cur realm, id int) {
	if !cur.IsCurrent() {
		panic("spoofed realm")
	}
	it := items.MustGet(store.ID(id)).(*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(store.ID(id)); !removed {
		panic("todos: item #" + strconv.Itoa(id) + " not found")
	}
}

// counts returns the number of open and done items.
func counts() (open, done int) {
	items.Each(func(_ store.ID, v any) {
		if v.(*item).done {
			done++
		} else {
			open++
		}
	})
	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()
	}

	// The store iterates by id, so newest-first is one call rather than a
	// collect-then-walk-backwards.
	items.EachReverse(func(id store.ID, v any) {
		it := v.(*item)
		mark := " "
		if it.done {
			mark = "x"
		}
		b.WriteString("- [" + mark + "] #" + id.String() + " " +
			it.text + " (" + ui.Addr(it.author) + ")\n")
	})

	return b.String()
}

todos_test.gno

package todos

import (
	"strings"
	"testing"

	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/store/v0"
	"gno.land/p/nt/testutils/v0"
)

// reset clears package globals between tests (t.Run/subtests do not reset them).
func reset() {
	items = store.Named("todos: item")
}

// 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(store.ID(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 the store key sorts numerically. v1 asserted the
// same thing about its own idKey, which padded to width 12: that held only
// below the width, since idKey(10^12) is 13 characters and sorts before
// idKey(10^12-1). The store key is 8 bytes for every id, so there is no
// ceiling. Non-crossing: plain signature.
func TestIDKeyOrdering(t *testing.T) {
	if store.ID(2).Key() >= store.ID(10).Key() {
		t.Fatal("id 2 should sort before id 10")
	}
	if store.ID(9).Key() >= store.ID(100).Key() {
		t.Fatal("id 9 should sort before id 100")
	}
	if store.ID(999999999999).Key() >= store.ID(1000000000000).Key() {
		t.Fatal("ordering should survive one past v1's width-12 ceiling")
	}
	if len(store.ID(1).Key()) != len(store.ID(1000000000000).Key()) {
		t.Fatal("every key is the same width")
	}
}