realmwatch_test.gno
7.27 Kb · 200 lines
1package realmwatch
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/testutils/v0"
8 "gno.land/p/nt/urequire/v0"
9)
10
11// Test actors. In the test harness the deploy-time origin capture
12// is empty, so the test owner claims ownership explicitly in the
13// first test (the same path a deployer would use if the capture
14// ever came back empty on-chain).
15var (
16 ownerTestAddr = testutils.TestAddress("rw-owner")
17 agentAddr = testutils.TestAddress("rw-agent")
18 randoAddr = testutils.TestAddress("rw-rando")
19)
20
21// NOTE: testing.SetRealm is frame-scoped — it must be called
22// directly in the test body, not via a helper, or it is lost when
23// the helper returns. Hence the longhand SetRealm calls below.
24
25// Ownership is claimable exactly once, by whoever calls first while
26// it is unclaimed; afterwards the claim path is closed forever.
27func TestClaimOwnership(cur realm, t *testing.T) {
28 urequire.Equal(t, Owner(), "")
29 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
30 ClaimOwnership(cross(cur))
31 urequire.Equal(t, Owner(), string(ownerTestAddr))
32
33 testing.SetRealm(testing.NewUserRealm(randoAddr))
34 urequire.AbortsContains(t, cur, "already claimed", func() {
35 ClaimOwnership(cross(cur))
36 })
37}
38
39// The realm is born STOPPED: before the owner arms it, even a
40// correctly registered agent cannot post.
41func TestBornStopped(cur realm, t *testing.T) {
42 urequire.True(t, IsStopped(), "realm must be born stopped")
43 urequire.Equal(t, LogCount(), uint64(0))
44
45 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
46 SetAgent(cross(cur), agentAddr)
47 AddWatch(cross(cur), "w1", "realm", "gno.land/r/demo/counter", "demo counter realm")
48
49 testing.SetRealm(testing.NewUserRealm(agentAddr))
50 urequire.AbortsContains(t, cur, "STOP is set", func() {
51 PostAlert(cross(cur), "BASELINE", "INFO", "w1", "baseline fp=abc")
52 })
53 urequire.Equal(t, LogCount(), uint64(0))
54}
55
56// Configuration is owner-only, and the owner is not the agent:
57// holding the leash does not let you write the log.
58func TestOwnerOnly(cur realm, t *testing.T) {
59 testing.SetRealm(testing.NewUserRealm(randoAddr))
60 urequire.AbortsContains(t, cur, "owner only", func() {
61 SetAgent(cross(cur), randoAddr)
62 })
63 urequire.AbortsContains(t, cur, "owner only", func() {
64 SetStopped(cross(cur), false)
65 })
66 urequire.AbortsContains(t, cur, "owner only", func() {
67 AddWatch(cross(cur), "evil", "realm", "gno.land/r/demo/counter", "x")
68 })
69
70 testing.SetRealm(testing.NewUserRealm(agentAddr))
71 urequire.AbortsContains(t, cur, "owner only", func() {
72 SetCap(cross(cur), 999, 100)
73 })
74}
75
76// Happy path: owner arms the realm, the agent posts, the log and
77// the cap accounting move exactly as specified.
78func TestArmAndPost(cur realm, t *testing.T) {
79 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
80 SetStopped(cross(cur), false)
81 urequire.False(t, IsStopped())
82
83 before := RemainingActions()
84 testing.SetRealm(testing.NewUserRealm(agentAddr))
85 seqStr := PostAlert(cross(cur), "BASELINE", "INFO", "w1", "baseline fp=abc")
86 urequire.Equal(t, seqStr, "1")
87 urequire.Equal(t, LogCount(), uint64(1))
88 urequire.Equal(t, RemainingActions(), before-1)
89
90 entry := EntryAt(1)
91 urequire.True(t, strings.Contains(entry, "|BASELINE|INFO|w1|baseline fp=abc"),
92 "entry round-trip mismatch: "+entry)
93 urequire.Equal(t, EntryAt(0), "")
94 urequire.Equal(t, EntryAt(99), "")
95 urequire.Equal(t, WatchTarget("w1"), "realm|gno.land/r/demo/counter|1")
96 urequire.Equal(t, WatchIDs(), "w1")
97}
98
99// Only the registered agent may write — not a stranger, and not
100// even the owner.
101func TestAgentOnly(cur realm, t *testing.T) {
102 testing.SetRealm(testing.NewUserRealm(randoAddr))
103 urequire.AbortsContains(t, cur, "agent only", func() {
104 PostAlert(cross(cur), "NOTE", "INFO", "", "intrusion attempt")
105 })
106 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
107 urequire.AbortsContains(t, cur, "agent only", func() {
108 PostAlert(cross(cur), "NOTE", "INFO", "", "owner self-post")
109 })
110 urequire.Equal(t, LogCount(), uint64(1))
111}
112
113// Malformed posts are rejected before anything is written.
114func TestValidation(cur realm, t *testing.T) {
115 testing.SetRealm(testing.NewUserRealm(agentAddr))
116 urequire.AbortsContains(t, cur, "bad entry kind", func() {
117 PostAlert(cross(cur), "EXPLODE", "INFO", "w1", "boom")
118 })
119 urequire.AbortsContains(t, cur, "bad severity", func() {
120 PostAlert(cross(cur), "NOTE", "MEH", "w1", "meh")
121 })
122 urequire.AbortsContains(t, cur, "one line", func() {
123 PostAlert(cross(cur), "NOTE", "INFO", "w1", "two\nlines")
124 })
125 urequire.AbortsContains(t, cur, "no such watch", func() {
126 PostAlert(cross(cur), "NOTE", "INFO", "ghost", "boo")
127 })
128 urequire.AbortsContains(t, cur, "1..280", func() {
129 PostAlert(cross(cur), "NOTE", "INFO", "w1", "")
130 })
131 urequire.Equal(t, LogCount(), uint64(1))
132}
133
134// A paused watch rejects posts; a system entry (no watch) is fine.
135func TestPausedWatch(cur realm, t *testing.T) {
136 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
137 AddWatch(cross(cur), "w2", "balance", string(randoAddr), "rando balance")
138 SetWatchActive(cross(cur), "w2", false)
139 urequire.Equal(t, WatchTarget("w2"), "balance|"+string(randoAddr)+"|0")
140
141 testing.SetRealm(testing.NewUserRealm(agentAddr))
142 urequire.AbortsContains(t, cur, "watch is paused", func() {
143 PostAlert(cross(cur), "CHANGE", "WARN", "w2", "balance moved")
144 })
145 PostAlert(cross(cur), "NOTE", "INFO", "", "system note: watch w2 paused by owner")
146 urequire.Equal(t, LogCount(), uint64(2))
147}
148
149// The cap is enforced by the realm: the (cap+1)-th post in an epoch
150// reverts, and RemainingActions reads 0.
151func TestCapExhaustion(cur realm, t *testing.T) {
152 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
153 SetCap(cross(cur), 2, 100000) // long epoch: no roll during the test
154 urequire.Equal(t, RemainingActions(), uint64(2))
155
156 testing.SetRealm(testing.NewUserRealm(agentAddr))
157 PostAlert(cross(cur), "HEARTBEAT", "INFO", "", "hb 1")
158 PostAlert(cross(cur), "HEARTBEAT", "INFO", "", "hb 2")
159 urequire.Equal(t, RemainingActions(), uint64(0))
160 urequire.AbortsContains(t, cur, "cap exhausted", func() {
161 PostAlert(cross(cur), "HEARTBEAT", "INFO", "", "hb 3 — one too many")
162 })
163
164 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
165 SetCap(cross(cur), 12, 100) // restore defaults for later tests
166 urequire.Equal(t, RemainingActions(), uint64(12))
167}
168
169// The agent can arrest itself; only the owner can release it.
170func TestTripStop(cur realm, t *testing.T) {
171 testing.SetRealm(testing.NewUserRealm(agentAddr))
172 TripStop(cross(cur))
173 urequire.True(t, IsStopped())
174 urequire.AbortsContains(t, cur, "STOP is set", func() {
175 PostAlert(cross(cur), "NOTE", "INFO", "", "posting while arrested")
176 })
177 urequire.AbortsContains(t, cur, "owner only", func() {
178 SetStopped(cross(cur), false)
179 })
180
181 testing.SetRealm(testing.NewUserRealm(randoAddr))
182 urequire.AbortsContains(t, cur, "owner or agent only", func() {
183 TripStop(cross(cur))
184 })
185
186 testing.SetRealm(testing.NewUserRealm(ownerTestAddr))
187 SetStopped(cross(cur), false)
188 urequire.False(t, IsStopped())
189 testing.SetRealm(testing.NewUserRealm(agentAddr))
190 PostAlert(cross(cur), "NOTE", "INFO", "", "released by owner; resuming")
191}
192
193// Render shows the leash state to any human with a browser.
194func TestRender(cur realm, t *testing.T) {
195 out := Render("")
196 urequire.True(t, strings.Contains(out, "STATUS: RUNNING"))
197 urequire.True(t, strings.Contains(out, "## Watches"))
198 urequire.True(t, strings.Contains(out, "w1"))
199 urequire.True(t, strings.Contains(out, "Read its leash"))
200}