reputation_test.gno
13.23 Kb · 327 lines
1package agentmarketplace
2
3import (
4 "strconv"
5 "strings"
6 "testing"
7 "time"
8
9 "gno.land/p/nt/uassert/v0"
10 "gno.land/p/nt/urequire/v0"
11)
12
13const halfLifeHeights = int64(halfLifeDays * 24 * 3600 / 5) // 45 days at 5s / height
14
15func TestRateAccessControl(cur realm, t *testing.T) {
16 reset()
17 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
18 id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
19
20 urequire.AbortsWithMessage(t, cur, "only a settled, completed task can be rated", func() {
21 rate(cur, alice, id, 5, "too early")
22 })
23
24 bidID := bid(cur, carol, id, 2000000, "ready")
25 accept(cur, alice, id, bidID)
26 urequire.AbortsWithMessage(t, cur, "only a settled, completed task can be rated", func() {
27 rate(cur, alice, id, 5, "still early")
28 })
29
30 complete(cur, carol, id)
31
32 urequire.AbortsWithMessage(t, cur, "only the task poster can rate this job", func() {
33 rate(cur, bob, id, 5, "not my job")
34 })
35 urequire.AbortsWithMessage(t, cur, "only the task poster can rate this job", func() {
36 rate(cur, carol, id, 5, "self?")
37 })
38 urequire.AbortsWithMessage(t, cur, "score must be an integer from 1 to 5", func() {
39 rate(cur, alice, id, 0, "")
40 })
41 urequire.AbortsWithMessage(t, cur, "score must be an integer from 1 to 5", func() {
42 rate(cur, alice, id, 6, "")
43 })
44
45 rid := rate(cur, alice, id, 5, "Excellent summary")
46 uassert.Equal(t, 1, rid)
47 uassert.Equal(t, 1, RatingCount())
48
49 urequire.AbortsWithMessage(t, cur, "this task has already been rated", func() {
50 rate(cur, alice, id, 4, "again")
51 })
52 uassert.Equal(t, 1, RatingCount())
53}
54
55func TestCannotRateOwnAgent(cur realm, t *testing.T) {
56 reset()
57 // The usual bid path already refuses a poster bidding on their own task,
58 // so poke a settled self-deal into state to cover the Rate guard.
59 tasks.Set(padID(1), &Task{
60 ID: 1,
61 Poster: alice,
62 Title: "Self deal",
63 Description: "Should not be rateable by the winner.",
64 Specialty: "research",
65 BudgetUgnot: 1000000,
66 Status: StatusCompleted,
67 Settled: true,
68 Winner: alice,
69 })
70 nextTaskID = 2
71 urequire.AbortsWithMessage(t, cur, "you cannot rate your own agent", func() {
72 rate(cur, alice, 1, 5, "nice")
73 })
74 uassert.Equal(t, 0, RatingCount())
75}
76
77func TestCannotRateCancelled(cur realm, t *testing.T) {
78 reset()
79 id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
80 cancel(cur, alice, id)
81 urequire.AbortsWithMessage(t, cur, "only a settled, completed task can be rated", func() {
82 rate(cur, alice, id, 5, "cancelled")
83 })
84}
85
86func TestScoreOfUnratedAndProvisional(cur realm, t *testing.T) {
87 reset()
88 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
89 count, trust, raw, prov := ScoreOf(carol)
90 uassert.Equal(t, 0, count)
91 uassert.Equal(t, int64(0), trust)
92 uassert.Equal(t, int64(0), raw)
93 uassert.True(t, prov)
94 uassert.Equal(t, "", ReputationSummary(carol))
95
96 // Three 4-star jobs from the same poster: still 4.00, flips to trusted at 3.
97 for i := 0; i < 3; i++ {
98 title := "Research job " + strconv.Itoa(i+1)
99 id := settleJob(cur, alice, carol, title, "A short research brief for this job.", "research", 2000000)
100 rate(cur, alice, id, 4, "Solid work")
101 count, trust, raw, prov = ScoreOf(carol)
102 uassert.Equal(t, i+1, count)
103 uassert.Equal(t, int64(4000), trust)
104 uassert.Equal(t, int64(4000), raw)
105 if i < 2 {
106 uassert.True(t, prov, "still provisional before 3 jobs")
107 uassert.True(t, strings.Contains(ReputationSummary(carol), "provisional"))
108 } else {
109 uassert.False(t, prov)
110 uassert.True(t, strings.Contains(ReputationSummary(carol), "trusted"))
111 }
112 }
113}
114
115func TestBudgetWeighting(cur realm, t *testing.T) {
116 reset()
117 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
118 // Two posters, same first-job reviewer shape, but 5 GNOT at 5 stars vs 1 GNOT at 1 star.
119 // Without the 20% cap this would sit near 4.33; the cap is tested separately.
120 idBig := settleJob(cur, alice, carol, "Large research brief", "Write a long research brief for a 5 GNOT budget.", "research", 5000000)
121 rate(cur, alice, idBig, 5, "Worth it")
122 idSmall := settleJob(cur, dave, carol, "Tiny research note", "Write a one-line research note for a 1 GNOT budget.", "research", 1000000)
123 rate(cur, dave, idSmall, 1, "Thin")
124
125 count, trust, raw, prov := ScoreOf(carol)
126 uassert.Equal(t, 2, count)
127 uassert.True(t, prov)
128 uassert.Equal(t, int64(3000), raw, "unweighted average of 5 and 1")
129 // 5 GNOT at 5 stars vs 1 GNOT at 1 star should sit between the raw
130 // average (3.00) and the uncapped budget mix (~4.33), because the
131 // large poster is clipped to 20% of the pre-cap weight mass.
132 uassert.True(t, trust != raw)
133 uassert.True(t, trust > 3000, "the larger 5-star still counts more")
134 uassert.True(t, trust < 4300, "the 20% cap should pull this below the uncapped mix")
135}
136
137func TestPosterCapTwentyPercent(cur realm, t *testing.T) {
138 reset()
139 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
140 // Five posters: one 10 GNOT 5-star and four 1 GNOT 1-stars. The cap is
141 // 20% (N>=5), so the large poster is clipped to 20% of the pre-cap
142 // mass and cannot bury the rest. Uncapped would be ~3.86; capped ~2.65.
143 idBig := settleJob(cur, alice, carol, "Large research brief", "Write a long research brief for a 10 GNOT budget.", "research", 10000000)
144 rate(cur, alice, idBig, 5, "Loved it")
145 smallPosters := []address{dave, eve, frank, bob}
146 for i, p := range smallPosters {
147 id := settleJob(cur, p, carol, "Tiny research note "+strconv.Itoa(i+1), "Write a one-line research note for 1 GNOT.", "research", 1000000)
148 rate(cur, p, id, 1, "Nope")
149 }
150
151 count, trust, raw, _ := ScoreOf(carol)
152 uassert.Equal(t, 5, count)
153 uassert.Equal(t, int64(1800), raw, "unweighted average of 5,1,1,1,1")
154 uassert.True(t, trust >= 2550 && trust <= 2800, "capped trust should be near 2.65, got "+fmtScore(trust))
155 uassert.True(t, trust > raw, "budget still gives the 5-star more weight than the raw average")
156}
157
158func TestSolePosterUncapped(cur realm, t *testing.T) {
159 reset()
160 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
161 id := settleJob(cur, alice, carol, "Solo research brief", "Only one poster rates this agent.", "research", 10000000)
162 rate(cur, alice, id, 5, "Only review")
163 _, trust, raw, prov := ScoreOf(carol)
164 uassert.Equal(t, int64(5000), trust)
165 uassert.Equal(t, int64(5000), raw)
166 uassert.True(t, prov)
167}
168
169func TestHalfLifeDecay(cur realm, t *testing.T) {
170 reset()
171 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
172 // Same poster, same budget: a 5-star ages one half-life, then a fresh
173 // 1-star arrives. One poster is uncapped, so decay alone should give
174 // (0.5*5 + 1*1) / 1.5 = 2.333.
175 idOld := settleJob(cur, alice, carol, "Older research brief", "This 5-star should lose half its weight.", "research", 2000000)
176 rate(cur, alice, idOld, 5, "Strong start")
177 _, trust, _, _ := ScoreOf(carol)
178 uassert.Equal(t, int64(5000), trust)
179
180 testing.SkipHeights(halfLifeHeights)
181 idNew := settleJob(cur, alice, carol, "Newer research brief", "This 1-star is fresh and should weigh twice the old 5.", "research", 2000000)
182 rate(cur, alice, idNew, 1, "Weak follow-up")
183
184 count, trust, raw, _ := ScoreOf(carol)
185 uassert.Equal(t, 2, count)
186 uassert.Equal(t, int64(3000), raw)
187 // (0.5*5 + 1*1) / 1.5 = 2.333...; allow a little slack if reviewer
188 // weights differ by a few GNOT of settled volume.
189 uassert.True(t, trust >= 2300 && trust <= 2400, "decayed mix should be near 2.33, got "+fmtScore(trust))
190}
191
192func TestDecayDoesNotChangeSingleRatingAverage(cur realm, t *testing.T) {
193 reset()
194 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
195 id := settleJob(cur, alice, carol, "Solo research brief", "A single rating should stay 5.00 as it decays.", "research", 2000000)
196 rate(cur, alice, id, 5, "Still five")
197 testing.SkipHeights(halfLifeHeights)
198 _, trust, raw, _ := ScoreOf(carol)
199 uassert.Equal(t, int64(5000), trust, "weight cancels in a single-rating average")
200 uassert.Equal(t, int64(5000), raw)
201}
202
203func TestRefreshScore(cur realm, t *testing.T) {
204 reset()
205 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
206 idOld := settleJob(cur, alice, carol, "Older research brief", "This 5-star will age one half-life.", "research", 2000000)
207 rate(cur, alice, idOld, 5, "Strong start")
208 testing.SkipHeights(halfLifeHeights)
209 idNew := settleJob(cur, alice, carol, "Newer research brief", "Fresh 1-star after the skip.", "research", 2000000)
210 rate(cur, alice, idNew, 1, "Weak follow-up")
211
212 _, live, _, _ := ScoreOf(carol)
213 refreshScore(cur, bob, carol) // anyone may refresh
214 cached := scores.Get(carol.String()).(*AgentScore)
215 uassert.Equal(t, live, cached.TrustMilli)
216 uassert.True(t, live >= 2300 && live <= 2400)
217}
218
219func TestSockPuppetBarelyMovesScore(cur realm, t *testing.T) {
220 reset()
221 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
222 // Trusted 4.00 from three 2 GNOT jobs, then a 1 ugnot 1-star smear.
223 for i := 0; i < 3; i++ {
224 id := settleJob(cur, alice, carol, "Baseline job "+strconv.Itoa(i+1), "A 2 GNOT research brief for the baseline.", "research", 2000000)
225 rate(cur, alice, id, 4, "Baseline")
226 }
227 _, before, _, prov := ScoreOf(carol)
228 uassert.Equal(t, int64(4000), before)
229 uassert.False(t, prov)
230
231 idSock := settleJob(cur, dave, carol, "Sock puppet job", "A 1 ugnot smear that should barely move the score.", "research", 1)
232 rate(cur, dave, idSock, 1, "Smear")
233 count, after, raw, stillTrusted := ScoreOf(carol)
234 uassert.Equal(t, 4, count)
235 uassert.False(t, stillTrusted)
236 uassert.True(t, raw < 4000, "raw average should dip")
237 // Trust must stay very close to 4.00 — well above 3.90.
238 uassert.True(t, after >= 3900 && after <= 4000, "1 ugnot smear should barely move 4.00, got "+fmtScore(after))
239}
240
241func TestFiveStarClimbsLeaderboard(cur realm, t *testing.T) {
242 reset()
243 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
244 register(cur, bob, "Code Agent", "code, review", RateFlat, 2000000)
245 register(cur, eve, "Writing Bot", "writing", RateFlat, 1500000)
246 register(cur, frank, "Data Agent", "data, research", RateHourly, 800000)
247
248 for i := 0; i < 3; i++ {
249 id := settleJob(cur, alice, carol, "Baseline job "+strconv.Itoa(i+1), "A 2 GNOT research brief for the baseline.", "research", 2000000)
250 rate(cur, alice, id, 4, "Baseline")
251 }
252 board := Render("leaderboard")
253 uassert.True(t, strings.Contains(board, "Research Bot"))
254 uassert.True(t, indexOf(board, "Research Bot") < indexOf(board, "Code Agent") || !strings.Contains(board, "Code Agent"))
255
256 id := settleJob(cur, alice, bob, "Review a realm", "Review an escrow realm and write up the findings.", "review", 3000000)
257 rate(cur, alice, id, 5, "Outstanding")
258
259 _, bobTrust, _, bobProv := ScoreOf(bob)
260 _, carolTrust, _, carolProv := ScoreOf(carol)
261 uassert.Equal(t, int64(5000), bobTrust)
262 uassert.True(t, bobProv, "one job stays provisional")
263 uassert.Equal(t, int64(4000), carolTrust)
264 uassert.False(t, carolProv)
265 uassert.True(t, bobTrust > carolTrust)
266
267 board = Render("leaderboard")
268 uassert.True(t, indexOf(board, "Code Agent") < indexOf(board, "Research Bot"), "5-star agent should rank above the trusted 4.00")
269 uassert.True(t, strings.Contains(board, "provisional"))
270 uassert.True(t, strings.Contains(board, "trusted"))
271}
272
273func TestScoreOfAndSummaryLookup(cur realm, t *testing.T) {
274 reset()
275 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
276 id := settleJob(cur, alice, carol, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
277 rate(cur, alice, id, 5, "Nice")
278 count, trust, raw, prov := ScoreOf(carol)
279 uassert.Equal(t, 1, count)
280 uassert.Equal(t, int64(5000), trust)
281 uassert.Equal(t, int64(5000), raw)
282 uassert.True(t, prov)
283 sum := ReputationSummary(carol)
284 uassert.True(t, strings.Contains(sum, "5.00"))
285 uassert.True(t, strings.Contains(sum, "provisional"))
286 uassert.True(t, strings.Contains(sum, "1 job"))
287}
288
289func TestDecayPPM(t *testing.T) {
290 half := time.Duration(halfLifeDays) * 24 * time.Hour
291 uassert.Equal(t, int64(decayScale), decayPPM(0))
292 uassert.Equal(t, int64(decayScale), decayPPM(-time.Hour))
293 uassert.Equal(t, int64(500000), decayPPM(half))
294 uassert.Equal(t, int64(250000), decayPPM(2*half))
295 uassert.Equal(t, int64(0), decayPPM(40*half))
296 mid := decayPPM(half / 2)
297 uassert.True(t, mid >= 700000 && mid <= 710000, "sqrt(1/2) is about 0.707, got "+strconv.Itoa(int(mid)))
298}
299
300func TestFmtScore(t *testing.T) {
301 uassert.Equal(t, "0.00", fmtScore(0))
302 uassert.Equal(t, "4.00", fmtScore(4000))
303 uassert.Equal(t, "5.00", fmtScore(5000))
304 uassert.Equal(t, "2.33", fmtScore(2333))
305 uassert.Equal(t, "1.80", fmtScore(1800))
306}
307
308func TestPosterStatsRecordedOnSettle(cur realm, t *testing.T) {
309 reset()
310 register(cur, carol, "Research Bot", "research", RateHourly, 500000)
311 register(cur, bob, "Code Agent", "code, review", RateFlat, 1000000)
312 settleJob(cur, alice, carol, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
313 settleJob(cur, alice, bob, "Review a realm", "Look over an escrow realm for footguns please.", "review", 3000000)
314 ps := posterStats.Get(alice.String()).(*PosterStats)
315 uassert.Equal(t, 2, ps.SettledJobs)
316 uassert.Equal(t, int64(5000000), ps.SettledUgnot)
317 uassert.Equal(t, 2, ps.DistinctAgents)
318 uassert.True(t, currentReviewerWeight(alice) > weightUnit)
319}
320
321func indexOf(s, sub string) int {
322 i := strings.Index(s, sub)
323 if i < 0 {
324 return 1 << 30
325 }
326 return i
327}