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

passport_test.gno

2.41 Kb · 81 lines
 1package passport
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils/v0"
 8	"gno.land/p/nt/uassert/v0"
 9)
10
11var (
12	alice = testutils.TestAddress("alice")
13	bob   = testutils.TestAddress("bob")
14	carol = testutils.TestAddress("carol")
15)
16
17func TestLifecycle(cur realm, t *testing.T) {
18	testing.SetRealm(testing.NewUserRealm(alice))
19	Register(cross(cur), "percy", "claude-opus-4-8@abc123")
20
21	uassert.True(t, Exists("percy"))
22	uassert.True(t, IsActive("percy"))
23	uassert.True(t, IsOperator("percy", alice))
24	uassert.False(t, IsOperator("percy", bob))
25	uassert.Equal(t, 1, Count())
26
27	// Owner adds an operator; the operator now passes the gate.
28	AddOperator(cross(cur), "percy", bob)
29	uassert.True(t, IsOperator("percy", bob))
30
31	// A runtime change is recorded as drift.
32	SetRuntime(cross(cur), "percy", "claude-opus-4-8@def456")
33	a := Get("percy")
34	uassert.Equal(t, "claude-opus-4-8@def456", a.Runtime)
35
36	// Suspending drops the operator gate for everyone.
37	Suspend(cross(cur), "percy")
38	uassert.False(t, IsActive("percy"))
39	uassert.False(t, IsOperator("percy", alice))
40	Reactivate(cross(cur), "percy")
41	uassert.True(t, IsActive("percy"))
42
43	// Render must not panic and must mention the agent.
44	uassert.True(t, len(Render("")) > 0)
45	uassert.True(t, strings.Contains(Render("percy"), "percy"))
46}
47
48func TestOwnershipGate(cur realm, t *testing.T) {
49	testing.SetRealm(testing.NewUserRealm(alice))
50	Register(cross(cur), "gate", "rt")
51
52	// bob is not the owner and cannot mutate.
53	testing.SetRealm(testing.NewUserRealm(bob))
54	uassert.AbortsWithMessage(t, cur, "caller is not the owner of gate", func(cur realm) {
55		SetRuntime(cur, "gate", "hacked")
56	})
57
58	// Transfer to carol, then carol controls it.
59	testing.SetRealm(testing.NewUserRealm(alice))
60	Transfer(cross(cur), "gate", carol)
61	testing.SetRealm(testing.NewUserRealm(carol))
62	SetRuntime(cross(cur), "gate", "carol-rt")
63	uassert.Equal(t, "carol-rt", Get("gate").Runtime)
64}
65
66func TestDuplicateRegister(cur realm, t *testing.T) {
67	testing.SetRealm(testing.NewUserRealm(alice))
68	Register(cross(cur), "dup", "rt")
69	uassert.AbortsWithMessage(t, cur, "agent id already registered: dup", func(cur realm) {
70		Register(cur, "dup", "rt")
71	})
72}
73
74func TestRetireIsTerminal(cur realm, t *testing.T) {
75	testing.SetRealm(testing.NewUserRealm(alice))
76	Register(cross(cur), "term", "rt")
77	Retire(cross(cur), "term")
78	uassert.AbortsWithMessage(t, cur, "retired agents cannot be reactivated", func(cur realm) {
79		Reactivate(cur, "term")
80	})
81}