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.

marketplace_test.gno

14.60 Kb · 410 lines
  1package agentmarketplace
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"strings"
  7	"testing"
  8
  9	"gno.land/p/nt/avl/v0"
 10	"gno.land/p/nt/testutils/v0"
 11	"gno.land/p/nt/uassert/v0"
 12	"gno.land/p/nt/urequire/v0"
 13)
 14
 15const pkgPath = "gno.land/r/g1pzn6rn8q0pfmq4xje4j8086ncnldavyz8skgp3/agentmarketplace"
 16
 17var (
 18	alice = testutils.TestAddress("alice")
 19	bob   = testutils.TestAddress("bob")
 20	carol = testutils.TestAddress("carol")
 21	dave  = testutils.TestAddress("dave")
 22	eve   = testutils.TestAddress("eve")
 23	frank = testutils.TestAddress("frank")
 24)
 25
 26func reset() {
 27	nextTaskID = 1
 28	nextBidID = 1
 29	nextRatingID = 1
 30	agents = avl.NewTree()
 31	tasks = avl.NewTree()
 32	bids = avl.NewTree()
 33	bidByAgent = avl.NewTree()
 34	ratings = avl.NewTree()
 35	ratingsByAgent = avl.NewTree()
 36	ratingByTask = avl.NewTree()
 37	scores = avl.NewTree()
 38	scoreboard = avl.NewTree()
 39	posterStats = avl.NewTree()
 40	posterAgents = avl.NewTree()
 41}
 42
 43func fundRealm(amount int64) {
 44	testing.IssueCoins(chain.PackageAddress(pkgPath), chain.Coins{{Denom: "ugnot", Amount: amount}})
 45}
 46
 47// testing.SetRealm attaches to the first non-testing call frame. These
 48// helpers are crossing functions so that SetRealm and the exported call
 49// share a frame; a non-crossing wrapper would drop the override when it
 50// returns.
 51
 52func register(cur realm, owner address, name, tags, kind string, rate int64) {
 53	testing.SetRealm(testing.NewUserRealm(owner))
 54	testing.SetOriginCaller(owner)
 55	RegisterAgent(cross(cur), name, tags, kind, rate)
 56}
 57
 58func update(cur realm, owner address, name, tags, kind string, rate int64) {
 59	testing.SetRealm(testing.NewUserRealm(owner))
 60	testing.SetOriginCaller(owner)
 61	UpdateAgent(cross(cur), name, tags, kind, rate)
 62}
 63
 64func post(cur realm, poster address, title, desc, spec string, budget int64) int {
 65	testing.SetRealm(testing.NewUserRealm(poster))
 66	testing.SetOriginCaller(poster)
 67	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: budget}})
 68	fundRealm(budget)
 69	return PostTask(cross(cur), title, desc, spec, budget)
 70}
 71
 72func postWithSend(cur realm, poster address, title, desc, spec string, budget int64, send chain.Coins) int {
 73	testing.SetRealm(testing.NewUserRealm(poster))
 74	testing.SetOriginCaller(poster)
 75	testing.SetOriginSend(send)
 76	return PostTask(cross(cur), title, desc, spec, budget)
 77}
 78
 79func bid(cur realm, agent address, taskID int, amount int64, note string) int {
 80	testing.SetRealm(testing.NewUserRealm(agent))
 81	testing.SetOriginCaller(agent)
 82	return PlaceBid(cross(cur), taskID, amount, note)
 83}
 84
 85func accept(cur realm, poster address, taskID, bidID int) {
 86	testing.SetRealm(testing.NewUserRealm(poster))
 87	testing.SetOriginCaller(poster)
 88	AcceptBid(cross(cur), taskID, bidID)
 89}
 90
 91func complete(cur realm, agent address, taskID int) {
 92	testing.SetRealm(testing.NewUserRealm(agent))
 93	testing.SetOriginCaller(agent)
 94	CompleteTask(cross(cur), taskID)
 95}
 96
 97func cancel(cur realm, poster address, taskID int) {
 98	testing.SetRealm(testing.NewUserRealm(poster))
 99	testing.SetOriginCaller(poster)
100	CancelTask(cross(cur), taskID)
101}
102
103func rate(cur realm, poster address, taskID, score int, comment string) int {
104	testing.SetRealm(testing.NewUserRealm(poster))
105	testing.SetOriginCaller(poster)
106	return Rate(cross(cur), taskID, score, comment)
107}
108
109func refreshScore(cur realm, caller, agent address) {
110	testing.SetRealm(testing.NewUserRealm(caller))
111	testing.SetOriginCaller(caller)
112	RefreshScore(cross(cur), agent)
113}
114
115func postHash(cur realm, poster address, title, desc, spec string, budget int64, hash string, deadlineSecs int64) int {
116	testing.SetRealm(testing.NewUserRealm(poster))
117	testing.SetOriginCaller(poster)
118	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: budget}})
119	fundRealm(budget)
120	return PostHashTask(cross(cur), title, desc, spec, budget, hash, deadlineSecs)
121}
122
123func reveal(cur realm, agent address, taskID int, preimage string) {
124	testing.SetRealm(testing.NewUserRealm(agent))
125	testing.SetOriginCaller(agent)
126	RevealAndComplete(cross(cur), taskID, preimage)
127}
128
129func timeoutHash(cur realm, caller address, taskID int) {
130	testing.SetRealm(testing.NewUserRealm(caller))
131	testing.SetOriginCaller(caller)
132	TimeoutHashTask(cross(cur), taskID)
133}
134
135// settleJob posts a task, accepts a full-budget bid, and settles it.
136func settleJob(cur realm, poster, agent address, title, desc, spec string, budget int64) int {
137	id := post(cur, poster, title, desc, spec, budget)
138	bidID := bid(cur, agent, id, budget, "ready")
139	accept(cur, poster, id, bidID)
140	complete(cur, agent, id)
141	return id
142}
143
144func TestRegisterAgent(cur realm, t *testing.T) {
145	reset()
146	register(cur, alice, "Research Bot", "research, writing", RateHourly, 500000)
147	uassert.Equal(t, 1, AgentCount())
148	a := mustGetAgent(alice)
149	uassert.Equal(t, alice, a.Owner)
150	uassert.Equal(t, "Research Bot", a.Name)
151	uassert.Equal(t, "research, writing", a.Tags, "tags are lowercased, unique and sorted")
152	uassert.Equal(t, RateHourly, a.RateKind)
153	uassert.Equal(t, int64(500000), a.RateUgnot)
154	uassert.Equal(t, 0, a.Completed)
155
156	register(cur, bob, "Code Agent", "Code, review, code", RateFlat, 2000000)
157	uassert.Equal(t, 2, AgentCount())
158	uassert.Equal(t, "code, review", mustGetAgent(bob).Tags)
159}
160
161func TestRegisterValidation(cur realm, t *testing.T) {
162	reset()
163	cases := []struct {
164		name, tags, kind string
165		rate             int64
166		want             string
167	}{
168		{"ab", "code", RateFlat, 1, "name must be 3-48 characters"},
169		{" lead", "code", RateFlat, 1, "name must not start or end with a space"},
170		{"bad|pipe", "code", RateFlat, 1, "name contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed"},
171		{"Bot", "x", RateFlat, 1, "tags must be 2-200 characters"},
172		{"Bot", "code", "weekly", 1, "rate kind must be one of: hourly, flat"},
173		{"Bot", "code", RateFlat, 0, "rate must be between 1 and 1000000000000 ugnot"},
174	}
175	for _, c := range cases {
176		urequire.AbortsWithMessage(t, cur, c.want, func() {
177			register(cur, alice, c.name, c.tags, c.kind, c.rate)
178		})
179	}
180	uassert.Equal(t, 0, AgentCount(), "no invalid agent was stored")
181}
182
183func TestRegisterOnce(cur realm, t *testing.T) {
184	reset()
185	register(cur, alice, "Research Bot", "research", RateHourly, 1)
186	urequire.AbortsWithMessage(t, cur, "this address is already registered as an agent", func() {
187		register(cur, alice, "Other Name", "writing", RateFlat, 2)
188	})
189}
190
191func TestUpdateAgentAccess(cur realm, t *testing.T) {
192	reset()
193	register(cur, alice, "Research Bot", "research", RateHourly, 500000)
194	update(cur, alice, "Alice Research", "research, writing", RateFlat, 750000)
195	a := mustGetAgent(alice)
196	uassert.Equal(t, "Alice Research", a.Name)
197	uassert.Equal(t, "research, writing", a.Tags)
198	uassert.Equal(t, RateFlat, a.RateKind)
199	uassert.Equal(t, int64(750000), a.RateUgnot)
200
201	urequire.AbortsWithMessage(t, cur, "no agent is registered at this address", func() {
202		update(cur, bob, "Stolen", "code", RateFlat, 1)
203	})
204}
205
206func TestPostTaskEscrow(cur realm, t *testing.T) {
207	reset()
208	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "Research", 2000000)
209	uassert.Equal(t, 1, id)
210	uassert.Equal(t, 1, TaskCount())
211	tk := mustGetTask(id)
212	uassert.Equal(t, alice, tk.Poster)
213	uassert.Equal(t, StatusOpen, tk.Status)
214	uassert.Equal(t, "research", tk.Specialty)
215	uassert.Equal(t, int64(2000000), tk.BudgetUgnot)
216	uassert.False(t, tk.Settled)
217	_, open, _, _, escrow := Stats()
218	uassert.Equal(t, 1, open)
219	uassert.Equal(t, int64(2000000), escrow)
220}
221
222func TestPostTaskSendValidation(cur realm, t *testing.T) {
223	reset()
224	title, desc := "Summarize a paper", "Write a one-page summary of the attached paper."
225	urequire.AbortsWithMessage(t, cur, "must send exactly 2000000ugnot with this call (got nothing)", func() {
226		postWithSend(cur, alice, title, desc, "research", 2000000, nil)
227	})
228	urequire.AbortsWithMessage(t, cur, "must send exactly 2000000ugnot with this call (got 1999999ugnot)", func() {
229		postWithSend(cur, alice, title, desc, "research", 2000000, chain.Coins{{Denom: "ugnot", Amount: 1999999}})
230	})
231	urequire.AbortsWithMessage(t, cur, "must send exactly 2000000ugnot with this call (got 2000000uatom)", func() {
232		postWithSend(cur, alice, title, desc, "research", 2000000, chain.Coins{{Denom: "uatom", Amount: 2000000}})
233	})
234	uassert.Equal(t, 0, TaskCount())
235}
236
237func TestPlaceBidRules(cur realm, t *testing.T) {
238	reset()
239	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 3000000)
240
241	urequire.AbortsWithMessage(t, cur, "no agent is registered at this address", func() {
242		bid(cur, bob, id, 2000000, "I can do this")
243	})
244
245	register(cur, bob, "Code Agent", "code, review", RateFlat, 1000000)
246	urequire.AbortsWithMessage(t, cur, "this agent does not list the required specialty: research", func() {
247		bid(cur, bob, id, 2000000, "I can do this")
248	})
249
250	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
251	register(cur, alice, "Alice Also", "research", RateHourly, 1)
252	urequire.AbortsWithMessage(t, cur, "the task poster cannot bid on their own task", func() {
253		bid(cur, alice, id, 1000000, "self bid")
254	})
255	bidID := bid(cur, carol, id, 2500000, "I will deliver tomorrow")
256	uassert.Equal(t, 1, bidID)
257	uassert.Equal(t, int64(2500000), mustGetBid(id, bidID).Amount)
258
259	same := bid(cur, carol, id, 2000000, "Lower offer")
260	uassert.Equal(t, bidID, same)
261	uassert.Equal(t, int64(2000000), mustGetBid(id, bidID).Amount)
262	uassert.Equal(t, "Lower offer", mustGetBid(id, bidID).Note)
263
264	urequire.AbortsWithMessage(t, cur, "bid amount must be between 1 and the task budget of 3000000 ugnot", func() {
265		bid(cur, carol, id, 3000001, "too high")
266	})
267	urequire.AbortsWithMessage(t, cur, "task 99 not found", func() {
268		bid(cur, carol, 99, 1, "")
269	})
270}
271
272func TestAcceptBidAccess(cur realm, t *testing.T) {
273	reset()
274	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 3000000)
275	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
276	bidID := bid(cur, carol, id, 2000000, "ready")
277
278	urequire.AbortsWithMessage(t, cur, "only the task poster can accept a bid", func() {
279		accept(cur, carol, id, bidID)
280	})
281	urequire.AbortsWithMessage(t, cur, "only the task poster can accept a bid", func() {
282		accept(cur, bob, id, bidID)
283	})
284	urequire.AbortsWithMessage(t, cur, "bid 99 not found on task 1", func() {
285		accept(cur, alice, id, 99)
286	})
287
288	accept(cur, alice, id, bidID)
289	tk := mustGetTask(id)
290	uassert.Equal(t, StatusInProgress, tk.Status)
291	uassert.Equal(t, carol, tk.Winner)
292	uassert.Equal(t, bidID, tk.WinningBidID)
293
294	urequire.AbortsWithMessage(t, cur, "only an open task can accept a bid", func() {
295		accept(cur, alice, id, bidID)
296	})
297	register(cur, bob, "Other Research", "research", RateHourly, 1)
298	urequire.AbortsWithMessage(t, cur, "bids are only accepted on open tasks", func() {
299		bid(cur, bob, id, 1000000, "late")
300	})
301}
302
303func TestCompleteAndSettle(cur realm, t *testing.T) {
304	reset()
305	budget := int64(3000000)
306	pay := int64(2000000)
307	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", budget)
308	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
309	bidID := bid(cur, carol, id, pay, "ready")
310	accept(cur, alice, id, bidID)
311
312	urequire.AbortsWithMessage(t, cur, "only the winning agent can mark this task complete", func() {
313		complete(cur, alice, id)
314	})
315	urequire.AbortsWithMessage(t, cur, "only the winning agent can mark this task complete", func() {
316		complete(cur, bob, id)
317	})
318
319	pkg := chain.PackageAddress(pkgPath)
320	rb := banker.NewReadonlyBanker()
321	beforeRealm := rb.GetCoin(pkg, "ugnot")
322	uassert.Equal(t, budget, beforeRealm, "realm still holds the escrow")
323
324	complete(cur, carol, id)
325
326	tk := mustGetTask(id)
327	uassert.Equal(t, StatusCompleted, tk.Status)
328	uassert.True(t, tk.Settled)
329	uassert.Equal(t, 1, mustGetAgent(carol).Completed)
330	uassert.Equal(t, pay, mustGetAgent(carol).EarnedUgnot)
331
332	uassert.Equal(t, pay, rb.GetCoin(carol, "ugnot"))
333	uassert.Equal(t, budget-pay, rb.GetCoin(alice, "ugnot"), "leftover budget refunded to poster")
334	uassert.Equal(t, int64(0), rb.GetCoin(pkg, "ugnot"), "realm escrow drained")
335
336	_, open, prog, done, escrow := Stats()
337	uassert.Equal(t, 0, open)
338	uassert.Equal(t, 0, prog)
339	uassert.Equal(t, 1, done)
340	uassert.Equal(t, int64(0), escrow)
341
342	urequire.AbortsWithMessage(t, cur, "only an in-progress task can be completed", func() {
343		complete(cur, carol, id)
344	})
345}
346
347func TestCancelRefund(cur realm, t *testing.T) {
348	reset()
349	budget := int64(4000000)
350	id := post(cur, alice, "Review a realm", "Look over an escrow realm for footguns please.", "review", budget)
351	register(cur, bob, "Code Agent", "code, review", RateFlat, 1000000)
352	bid(cur, bob, id, 3500000, "I can review this")
353
354	urequire.AbortsWithMessage(t, cur, "only the task poster can cancel this task", func() {
355		cancel(cur, bob, id)
356	})
357
358	cancel(cur, alice, id)
359	tk := mustGetTask(id)
360	uassert.Equal(t, StatusCancelled, tk.Status)
361	uassert.True(t, tk.Settled)
362
363	rb := banker.NewReadonlyBanker()
364	uassert.Equal(t, budget, rb.GetCoin(alice, "ugnot"))
365	uassert.Equal(t, int64(0), rb.GetCoin(chain.PackageAddress(pkgPath), "ugnot"))
366
367	urequire.AbortsWithMessage(t, cur, "only an open task can be cancelled", func() {
368		cancel(cur, alice, id)
369	})
370	urequire.AbortsWithMessage(t, cur, "bids are only accepted on open tasks", func() {
371		bid(cur, bob, id, 1, "too late")
372	})
373}
374
375func TestCannotCancelAfterAccept(cur realm, t *testing.T) {
376	reset()
377	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
378	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
379	bidID := bid(cur, carol, id, 1500000, "ok")
380	accept(cur, alice, id, bidID)
381	urequire.AbortsWithMessage(t, cur, "only an open task can be cancelled", func() {
382		cancel(cur, alice, id)
383	})
384	uassert.Equal(t, StatusInProgress, mustGetTask(id).Status)
385	uassert.False(t, mustGetTask(id).Settled)
386}
387
388func TestCompleteOpenTaskRejected(cur realm, t *testing.T) {
389	reset()
390	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
391	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
392	urequire.AbortsWithMessage(t, cur, "only an in-progress task can be completed", func() {
393		complete(cur, carol, id)
394	})
395}
396
397func TestHappyPathSummaries(cur realm, t *testing.T) {
398	reset()
399	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
400	id := post(cur, alice, "Summarize a paper", "Write a one-page summary of the attached paper.", "research", 2000000)
401	bidID := bid(cur, carol, id, 2000000, "full budget")
402	accept(cur, alice, id, bidID)
403	complete(cur, carol, id)
404
405	uassert.True(t, strings.Contains(AgentSummary(carol), "Research Bot"))
406	uassert.True(t, strings.Contains(AgentSummary(carol), "completed 1"))
407	uassert.True(t, strings.Contains(TaskSummary(id), StatusCompleted))
408	uassert.Equal(t, "", AgentSummary(bob))
409	uassert.Equal(t, "", TaskSummary(99))
410}