deeds_test.gno
6.65 Kb · 196 lines
1package deeds
2
3import (
4 "crypto/bech32"
5 "strings"
6 "testing"
7)
8
9// testAddress builds a deterministic test address from a short name.
10// Same construction as gno.land/p/nt/testutils/v0.TestAddress, inlined
11// here so the test suite has no external package dependency (the gno
12// toolchain cannot fetch gno.land packages from this VM — its HTTP
13// client bypasses the egress proxy).
14func testAddress(name string) address {
15 if len(name) > 20 {
16 panic("address name cannot be greater than 20 bytes")
17 }
18 addr := []byte("____________________")
19 copy(addr[:], name)
20 converted, err := bech32.ConvertBits(addr, 8, 5, true)
21 if err != nil {
22 panic(err)
23 }
24 enc, err := bech32.Encode("g", converted)
25 if err != nil {
26 panic(err)
27 }
28 return address(enc)
29}
30
31// expectPanic runs f and fails the test if f does not panic.
32func expectPanic(t *testing.T, f func()) {
33 defer func() {
34 if r := recover(); r == nil {
35 t.Fatal("expected panic, got none")
36 }
37 }()
38 f()
39}
40
41func TestRegisterAndLogDeed(t *testing.T) {
42 // NOTE: gno test shares realm state across tests in one run, so
43 // assert deltas, not absolute totals.
44 agentsBefore := len(agentList)
45 deedsBefore := len(deedList)
46 alice := testAddress("alice-reg")
47 registerAgent(alice, "AliceBot", "Research agent")
48 if len(agentList) != agentsBefore+1 {
49 t.Fatalf("expected %d agents, got %d", agentsBefore+1, len(agentList))
50 }
51 id := logDeed(alice, "Wrote market brief", "sha256:abc123")
52 if id != deedsBefore+1 {
53 t.Fatalf("expected deed id %d, got %d", deedsBefore+1, id)
54 }
55 if agents[alice].DeedCount != 1 {
56 t.Fatalf("expected deed count 1, got %d", agents[alice].DeedCount)
57 }
58 got := Render("")
59 for _, want := range []string{"AliceBot", "Wrote market brief", "sha256:abc123", alice.String(), stats()} {
60 if !strings.Contains(got, want) {
61 t.Fatalf("expected render to contain %q", want)
62 }
63 }
64}
65
66func TestDuplicateRegisterPanics(t *testing.T) {
67 alice := testAddress("alice-dup")
68 registerAgent(alice, "AliceBot", "Research agent")
69 expectPanic(t, func() {
70 registerAgent(alice, "AliceBot2", "Second try")
71 })
72}
73
74func TestInvalidInputsPanic(t *testing.T) {
75 alice := testAddress("alice-bad")
76 expectPanic(t, func() { registerAgent(alice, "", "empty name") })
77 expectPanic(t, func() { registerAgent(alice, strings.Repeat("x", 65), "name too long") })
78 registerAgent(alice, "AliceBot", "ok")
79 expectPanic(t, func() { logDeed(alice, "", "sha256:abc") })
80 expectPanic(t, func() { logDeed(alice, "title", "") })
81}
82
83func TestLogDeedUnregisteredPanics(t *testing.T) {
84 ghost := testAddress("ghost")
85 expectPanic(t, func() { logDeed(ghost, "I do not exist", "sha256:abc") })
86}
87
88func TestAttestFlow(t *testing.T) {
89 attBefore := len(attestations)
90 alice := testAddress("alice-att")
91 bob := testAddress("bob-att")
92 registerAgent(alice, "AliceBot", "Research agent")
93 registerAgent(bob, "BobBot", "Review agent")
94 id := logDeed(alice, "Wrote market brief", "sha256:abc123")
95
96 attest(bob, id, "Verified the brief against sources")
97 if deeds[id].Attestations != 1 {
98 t.Fatalf("expected 1 attestation on deed, got %d", deeds[id].Attestations)
99 }
100 if agents[alice].Attestations != 1 {
101 t.Fatalf("expected 1 attestation for alice, got %d", agents[alice].Attestations)
102 }
103
104 // Same attestor cannot attest the same deed twice.
105 expectPanic(t, func() { attest(bob, id, "again") })
106 // Cannot attest your own deed.
107 expectPanic(t, func() { attest(alice, id, "self praise") })
108 // Unknown deed.
109 expectPanic(t, func() { attest(bob, 999, "ghost deed") })
110 // Unregistered attestor.
111 ghost := testAddress("ghost-att")
112 expectPanic(t, func() { attest(ghost, id, "who am I") })
113
114 if len(attestations) != attBefore+1 {
115 t.Fatalf("expected %d attestations total, got %d", attBefore+1, len(attestations))
116 }
117 got := Render("")
118 for _, want := range []string{"AliceBot", "BobBot", "Wrote market brief", stats()} {
119 if !strings.Contains(got, want) {
120 t.Fatalf("expected render to contain %q, got:\n%s", want, got)
121 }
122 }
123}
124
125func TestTopAgentsOrdering(t *testing.T) {
126 alice := testAddress("alice-top")
127 bob := testAddress("bob-top")
128 carol := testAddress("carol-top")
129 dave := testAddress("dave-top")
130 registerAgent(alice, "AliceBot", "Research agent")
131 registerAgent(bob, "BobBot", "Review agent")
132 registerAgent(carol, "CarolBot", "Checker agent")
133 registerAgent(dave, "DaveBot", "Checker agent")
134 d1 := logDeed(alice, "Brief one", "sha256:111")
135 d2 := logDeed(alice, "Brief two", "sha256:222")
136 d3 := logDeed(bob, "Review one", "sha256:333")
137 attest(carol, d1, "good")
138 attest(dave, d1, "good")
139 attest(carol, d3, "good")
140 attest(dave, d2, "good")
141 // Alice: 3 attestations, 2 deeds. Bob: 1 attestation, 1 deed.
142 top := topAgents()
143 ia := strings.Index(top, "AliceBot")
144 ib := strings.Index(top, "BobBot")
145 if ia < 0 || ib < 0 || ia > ib {
146 t.Fatalf("expected AliceBot before BobBot in top list, got:\n%s", top)
147 }
148 if !strings.Contains(top, "3 attestations") {
149 t.Fatalf("expected AliceBot with 3 attestations, got:\n%s", top)
150 }
151}
152
153func TestAttestationsFor(t *testing.T) {
154 alice := testAddress("alice-atf")
155 bob := testAddress("bob-atf")
156 carol := testAddress("carol-atf")
157 registerAgent(alice, "AliceBot", "Research agent")
158 registerAgent(bob, "BobBot", "Review agent")
159 registerAgent(carol, "CarolBot", "Checker agent")
160 id := logDeed(alice, "Brief for attestations", "sha256:atf")
161 attest(bob, id, "Verified the brief against sources")
162 attest(carol, id, "Checked the numbers twice")
163 got := attestationsFor(id)
164 for _, want := range []string{bob.String(), carol.String(), "Verified the brief against sources", "Checked the numbers twice"} {
165 if !strings.Contains(got, want) {
166 t.Fatalf("expected AttestationsFor to contain %q, got:\n%s", want, got)
167 }
168 }
169 // Unknown deed and deed without attestations return "".
170 if attestationsFor(999999) != "" {
171 t.Fatalf("expected empty result for unknown deed")
172 }
173 id2 := logDeed(alice, "Unattested brief", "sha256:atf2")
174 if attestationsFor(id2) != "" {
175 t.Fatalf("expected empty result for deed without attestations")
176 }
177}
178
179func TestCrossingCallerWiring(cur realm, t *testing.T) {
180 // Proves the crossing functions pick up the real caller via
181 // unsafe.PreviousRealm(): the test sets the caller realm to zoe.
182 zoe := testAddress("zoe-cross")
183 testing.SetRealm(testing.NewUserRealm(zoe))
184 RegisterAgent(cross(cur), "ZoeBot", "Crossing test agent")
185 got := Render("")
186 if !strings.Contains(got, "ZoeBot") || !strings.Contains(got, zoe.String()) {
187 t.Fatalf("expected render to contain ZoeBot and her address, got:\n%s", got)
188 }
189 id := LogDeed(cross(cur), "Crossing deed", "sha256:cross")
190 if id <= 0 {
191 t.Fatalf("expected a positive deed id via crossing call, got %d", id)
192 }
193 if deeds[id].Title != "Crossing deed" {
194 t.Fatalf("expected deed title %q, got %q", "Crossing deed", deeds[id].Title)
195 }
196}