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.

erratabank_test.gno

10.09 Kb · 221 lines
  1package erratabank
  2
  3import (
  4	"chain"
  5	"chain/runtime"
  6	"strconv"
  7	"strings"
  8	"testing"
  9
 10	"gno.land/p/nt/markdown/sanitize/v0"
 11)
 12
 13const (
 14	testOwner  = address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0")
 15	testReader = address("g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c")
 16	testOther  = address("g1juz2yxmdsa6audkp6ep9vfv80c8p5u76e03vvh")
 17	testLink   = "https://example.org/book"
 18)
 19
 20func assertPanic(t *testing.T, got any, want string) {
 21	t.Helper()
 22	if got != want {
 23		t.Fatalf("panic = %v, want %q", got, want)
 24	}
 25}
 26
 27func wantText(t *testing.T, got string, parts ...string) {
 28	t.Helper()
 29	for _, part := range parts {
 30		if !strings.Contains(got, part) {
 31			t.Errorf("expected %q in %q", part, got)
 32		}
 33	}
 34}
 35
 36func TestBookValidationAndSubmission(cur realm, t *testing.T) {
 37	testing.SetRealm(testing.NewUserRealm(testOwner))
 38	assertPanic(t, revive(func() { CreateBook(cross(cur), "", "First", testLink, 0) }),
 39		"title must contain 1 to 120 characters")
 40	assertPanic(t, revive(func() { CreateBook(cross(cur), "Book", "", testLink, 0) }),
 41		"edition must contain 1 to 120 characters")
 42	assertPanic(t, revive(func() { CreateBook(cross(cur), "Book", "First", strings.Repeat("a", 301), 0) }),
 43		"link must contain 1 to 300 characters")
 44	assertPanic(t, revive(func() { CreateBook(cross(cur), "Book", "First", "javascript:bad", 0) }),
 45		"unsupported book link")
 46	assertPanic(t, revive(func() { CreateBook(cross(cur), "Book", "First", testLink, -1) }),
 47		"reward cannot be negative")
 48
 49	id := CreateBook(cross(cur), "Book", "First", testLink, 0)
 50	assertPanic(t, revive(func() { Submit(cross(cur), id, "Page 1", "Fix") }), errSelfSubmit)
 51
 52	testing.SetRealm(testing.NewUserRealm(testReader))
 53	assertPanic(t, revive(func() { Submit(cross(cur), id, " ", "Fix") }),
 54		"location must contain 1 to 120 characters")
 55	assertPanic(t, revive(func() { Submit(cross(cur), id, strings.Repeat("é", 121), "Fix") }),
 56		"location must contain 1 to 120 characters")
 57	assertPanic(t, revive(func() { Submit(cross(cur), id, "Page 1", "") }),
 58		"text must contain 1 to 1000 characters")
 59	assertPanic(t, revive(func() { Submit(cross(cur), id, "Page 1", strings.Repeat("a", 1001)) }),
 60		"text must contain 1 to 1000 characters")
 61
 62	testing.SetRealm(testing.NewUserRealm(testOwner))
 63	SetBookOpen(cross(cur), id, false)
 64	testing.SetRealm(testing.NewUserRealm(testReader))
 65	assertPanic(t, revive(func() { Submit(cross(cur), id, "Page 1", "Fix") }), errClosed)
 66	testing.SetRealm(testing.NewUserRealm(testOwner))
 67	SetBookOpen(cross(cur), id, true)
 68
 69	testing.SkipHeights(2)
 70	testing.SetRealm(testing.NewUserRealm(testReader))
 71	reportID := Submit(cross(cur), id, strings.Repeat("é", 120), strings.Repeat("a", 1000))
 72	report, ok := GetErratum(reportID)
 73	if !ok || report.Finder != testReader || report.SubmittedAt != runtime.ChainHeight() || report.Status != Pending {
 74		t.Fatalf("unexpected report %#v", report)
 75	}
 76	book, ok := GetBook(id)
 77	if !ok || book.Owner != testOwner || !book.Open {
 78		t.Fatalf("unexpected book %#v", book)
 79	}
 80
 81	// Cross-realm consumers get detached value data.
 82	book.Title = "modified"
 83	report.Status = Accepted
 84	again, _ := GetBook(id)
 85	againReport, _ := GetErratum(reportID)
 86	if again.Title != "Book" || againReport.Status != Pending {
 87		t.Fatal("editing an exported copy affected realm state")
 88	}
 89}
 90
 91func TestFirstFinderAndPermissions(cur realm, t *testing.T) {
 92	testing.SetRealm(testing.NewUserRealm(testOwner))
 93	bookID := CreateBook(cross(cur), "Duplicate test", "First", testLink, 0)
 94	otherBookID := CreateBook(cross(cur), "Another book", "First", testLink, 0)
 95	testing.SetRealm(testing.NewUserRealm(testReader))
 96	first := Submit(cross(cur), bookID, "Page 2", "Fix the number.")
 97	testing.SetRealm(testing.NewUserRealm(testOther))
 98	second := Submit(cross(cur), bookID, "Page 2", "Fix the number.")
 99	other := Submit(cross(cur), otherBookID, "Page 2", "Fix the number.")
100	rejected := Submit(cross(cur), bookID, "Page 3", "Suggested wording.")
101	late := Submit(cross(cur), bookID, "Page 4", "Another suggestion.")
102	latest := Submit(cross(cur), bookID, "Page 4", "A later correction.")
103	assertPanic(t, revive(func() { Accept(cross(cur), first, "") }), errOwnerOnly)
104	assertPanic(t, revive(func() { Reject(cross(cur), first, "") }), errOwnerOnly)
105	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), second, first) }), errOwnerOnly)
106	assertPanic(t, revive(func() { Withdraw(cross(cur), bookID, 1) }), errOwnerOnly)
107	assertPanic(t, revive(func() { SetBookOpen(cross(cur), bookID, false) }), errOwnerOnly)
108
109	original, _ := GetErratum(first)
110	later, _ := GetErratum(second)
111	if original.SubmittedAt != later.SubmittedAt || first >= second {
112		t.Fatal("same-height submissions must retain transaction order")
113	}
114	testing.SetRealm(testing.NewUserRealm(testOwner))
115	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), second, first) }), errDuplicate)
116	Accept(cross(cur), first, "Confirmed")
117	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), other, first) }), errDuplicate)
118	MarkDuplicate(cross(cur), second, first)
119	Reject(cross(cur), rejected, "Editorial preference")
120	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), late, rejected) }), errDuplicate)
121	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), late, second) }), errDuplicate)
122	assertPanic(t, revive(func() { Accept(cross(cur), second, "") }), errPendingOnly)
123	assertPanic(t, revive(func() { Accept(cross(cur), first, "") }), errPendingOnly)
124	assertPanic(t, revive(func() { Reject(cross(cur), rejected, "") }), errPendingOnly)
125
126	duplicate, _ := GetErratum(second)
127	entries := AcceptedEntries(bookID)
128	count, paid := FinderStats(testReader)
129	otherCount, _ := FinderStats(testOther)
130	if duplicate.DuplicateOf != first || duplicate.Status != Duplicate ||
131		len(entries) != 1 || count != 1 || paid != 0 || otherCount != 0 {
132		t.Fatal("duplicate status or finder credit is wrong")
133	}
134	duplicates := ListErrata(bookID, Duplicate, 0, 10)
135	if len(duplicates) != 1 || duplicates[0].ID != second {
136		t.Fatal("duplicate index is wrong")
137	}
138
139	Accept(cross(cur), latest, "Confirmed")
140	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), late, latest) }), errDuplicate)
141}
142
143func TestPayableGuards(cur realm, t *testing.T) {
144	testing.SetRealm(testing.NewUserRealm(testOwner))
145	id := CreateBook(cross(cur), "Payment test", "First", testLink, 100)
146	assertPanic(t, revive(func() { FundBook(cross(cur), id) }), errPayment)
147
148	testing.SetOriginSend(chain.Coins{{Denom: "utest", Amount: 100}})
149	assertPanic(t, revive(func() { FundBook(cross(cur), id) }), errPaymentDenom)
150	assertPanic(t, revive(func() { CreateBook(cross(cur), "Payment test", "First", testLink, 100) }), errPaymentDenom)
151
152	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 0}})
153	assertPanic(t, revive(func() { FundBook(cross(cur), id) }), errPayment)
154	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: -10}})
155	assertPanic(t, revive(func() { FundBook(cross(cur), id) }), errPayment)
156	testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: 100}})
157	testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/paymentcaller"))
158	assertPanic(t, revive(func() { FundBook(cross(cur), id) }), errDirectCall)
159	assertPanic(t, revive(func() { CreateBook(cross(cur), "Payment test", "First", testLink, 100) }), errDirectCall)
160	book, _ := GetBook(id)
161	if book.Escrow != 0 {
162		t.Fatal("invalid funding was credited")
163	}
164	testing.SetOriginSend(nil)
165}
166
167func TestNonpayableCallsRejectCoins(cur realm, t *testing.T) {
168	testing.SetRealm(testing.NewUserRealm(testOwner))
169	bookID := CreateBook(cross(cur), "Nonpayable calls", "First", testLink, 0)
170	testing.SetRealm(testing.NewUserRealm(testReader))
171	first := Submit(cross(cur), bookID, "Page 1", "First correction.")
172	pending := Submit(cross(cur), bookID, "Page 2", "Second correction.")
173	testing.SetRealm(testing.NewUserRealm(testOwner))
174	Accept(cross(cur), first, "Confirmed")
175
176	testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 1)))
177	testing.SetRealm(testing.NewUserRealm(testReader))
178	assertPanic(t, revive(func() { Submit(cross(cur), bookID, "Page 3", "Third correction.") }), errRejectCoins)
179
180	testing.SetRealm(testing.NewUserRealm(testOwner))
181	assertPanic(t, revive(func() { Accept(cross(cur), pending, "") }), errRejectCoins)
182	assertPanic(t, revive(func() { Reject(cross(cur), pending, "") }), errRejectCoins)
183	assertPanic(t, revive(func() { MarkDuplicate(cross(cur), pending, first) }), errRejectCoins)
184	assertPanic(t, revive(func() { SetBookOpen(cross(cur), bookID, false) }), errRejectCoins)
185	assertPanic(t, revive(func() { Withdraw(cross(cur), bookID, 1) }), errRejectCoins)
186
187	// A crossing from another realm does not receive the caller's origin send.
188	testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/nonpayablecaller"))
189	Submit(cross(cur), bookID, "Page 3", "Third correction.")
190	testing.SetOriginSend(nil)
191}
192
193func TestRenderAndPagination(cur realm, t *testing.T) {
194	hostile := "[x](javascript:bad) *fix* | <img>\n# line"
195	testing.SetRealm(testing.NewUserRealm(testOwner))
196	bookID := CreateBook(cross(cur), hostile, "First edition", testLink, 0)
197	bookPath := "book/" + strconv.Itoa(bookID)
198
199	testing.SetRealm(testing.NewUserRealm(testReader))
200	first := Submit(cross(cur), bookID, "Page 1", hostile)
201	for i := 2; i <= pageSize+1; i++ {
202		Submit(cross(cur), bookID, "Page "+strconv.Itoa(i), "Fix "+strconv.Itoa(i))
203	}
204	wantText(t, Render(bookPath+"/pending?page=2"), "Page 2 of 2", "Page 21")
205	wantText(t, Render(bookPath+"/all?page=2"), "Page 2 of 2", "Page 21")
206
207	testing.SetRealm(testing.NewUserRealm(testOwner))
208	Accept(cross(cur), first, hostile)
209	wantText(t, Render(""), "# Erratabank", bookURL(bookID), "Started 23 Nov 2026 for Gnome Tavern.")
210	out := Render(bookPath)
211	wantText(t, out, "## Accepted errata", finderURL(testReader), "Pending (20)", sanitize.TableCell(hostile))
212	wantText(t, Render("erratum/"+strconv.Itoa(first)), "Status history", "Pending (submitted)", "Accepted",
213		sanitize.InlineText(hostile))
214	wantText(t, Render("finder/"+testReader.String()), "Finder account", "Accepted finds:", bookURL(bookID))
215	wantText(t, Render("finder/invalid"), "Finder not found")
216	wantText(t, Render("unknown"), "Page not found")
217	wantText(t, Render(bookPath+"/all?page=3"), "Page not found")
218	if strings.Contains(out, "](javascript:") || strings.Contains(out, "<img>") {
219		t.Fatalf("unsafe Markdown in book page: %s", out)
220	}
221}