erratabank_test.gno
10.64 Kb · 233 lines
1package erratabank
2
3import (
4 "chain"
5 "strconv"
6 "strings"
7 "testing"
8
9 "gno.land/p/nt/markdown/sanitize/v0"
10)
11
12const (
13 testOwner = address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0")
14 testReader = address("g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c")
15 testSponsor = address("g1juz2yxmdsa6audkp6ep9vfv80c8p5u76e03vvh")
16 testLink = "https://example.org/guide"
17)
18
19func assertPanic(t *testing.T, got any, want string) {
20 t.Helper()
21 if got != want {
22 t.Fatalf("panic = %v, want %q", got, want)
23 }
24}
25
26func TestReviewOrderAndPermissions(cur realm, t *testing.T) {
27 testing.SetRealm(testing.NewUserRealm(testOwner))
28 book := CreateBook(cross(cur), "Review order", "First edition", testLink, 0)
29 otherBook := CreateBook(cross(cur), "Other book", "First edition", testLink, 0)
30 testing.SetRealm(testing.NewUserRealm(testReader))
31 first := Submit(cross(cur), book, " Section 1 ", "The first correction.")
32 other := Submit(cross(cur), otherBook, "Section 1", "This location is in another book.")
33 testing.SetRealm(testing.NewUserRealm(testSponsor))
34 later := Submit(cross(cur), book, "Section 1", "The second correction.")
35 assertPanic(t, revive(func() { Accept(cross(cur), first, "") }), errOwnerOnly)
36 assertPanic(t, revive(func() { Reject(cross(cur), first, "") }), errOwnerOnly)
37 assertPanic(t, revive(func() { MarkDuplicate(cross(cur), later, first) }), errOwnerOnly)
38 assertPanic(t, revive(func() { Withdraw(cross(cur), book, 1) }), errOwnerOnly)
39
40 testing.SetRealm(testing.NewUserRealm(testOwner))
41 assertPanic(t, revive(func() { Accept(cross(cur), later, "") }), errEarlierPending)
42 assertPanic(t, revive(func() { MarkDuplicate(cross(cur), first, later) }), errDuplicate)
43 assertPanic(t, revive(func() { MarkDuplicate(cross(cur), later, other) }), errDuplicate)
44 Accept(cross(cur), other, "")
45 Accept(cross(cur), first, "")
46 MarkDuplicate(cross(cur), later, first)
47 duplicate, _ := GetErratum(later)
48 if duplicate.Status != Duplicate || duplicate.DuplicateOf != first {
49 t.Fatalf("wrong duplicate record: %+v", duplicate)
50 }
51
52 testing.SetRealm(testing.NewUserRealm(testReader))
53 original := Submit(cross(cur), book, "Alternate section", "Report before Section 2.")
54 firstAtLocation := Submit(cross(cur), book, "Section 2", "First at this location.")
55 secondAtLocation := Submit(cross(cur), book, "Section 2", "Second at this location.")
56 rejected := Submit(cross(cur), book, "Section 3", "Unconfirmed report.")
57 confirmed := Submit(cross(cur), book, "Section 3", "Confirmed report.")
58 testing.SetRealm(testing.NewUserRealm(testOwner))
59 MarkDuplicate(cross(cur), firstAtLocation, original)
60 Accept(cross(cur), secondAtLocation, "")
61 Reject(cross(cur), rejected, "")
62 assertPanic(t, revive(func() { MarkDuplicate(cross(cur), confirmed, rejected) }), errDuplicate)
63 Accept(cross(cur), confirmed, "")
64 pending, _ := GetErratum(original)
65 if pending.Status != Pending {
66 t.Fatalf("marking a duplicate changed the original status: %s", pending.Status)
67 }
68 assertPanic(t, revive(func() { Accept(cross(cur), first, "") }), errPendingOnly)
69}
70
71func TestFinderReputationAndOwed(cur realm, t *testing.T) {
72 author := chain.PackageAddress("gno.land/r/test/reputationauthor")
73 finder := chain.PackageAddress("gno.land/r/test/reputationfinder")
74 testing.SetRealm(testing.NewUserRealm(author))
75 rewarded := CreateBook(cross(cur), "Rewarded book", "First edition", testLink, 1000)
76 creditOnly := CreateBook(cross(cur), "Credit-only book", "First edition", testLink, 0)
77 self := Submit(cross(cur), rewarded, "Owner note", "A maintainer's own correction.")
78 testing.SetRealm(testing.NewUserRealm(finder))
79 eligible := Submit(cross(cur), rewarded, "Rewarded location", "A reader's correction.")
80 unpaid := Submit(cross(cur), creditOnly, "Volunteer note", "A voluntary correction.")
81 testing.SetRealm(testing.NewUserRealm(author))
82 Accept(cross(cur), self, "")
83 Accept(cross(cur), eligible, "")
84 Accept(cross(cur), unpaid, "")
85
86 owner := FinderStats(author)
87 if owner.Credits != 0 || owner.UnpaidCredits != 1 || owner.Paid != 0 || owner.Owed != 0 {
88 t.Fatalf("self-owned correction counted as a reward: %+v", owner)
89 }
90 reader := FinderStats(finder)
91 if reader.Credits != 1 || reader.UnpaidCredits != 1 || reader.Paid != 0 || reader.Owed != 1000 {
92 t.Fatalf("wrong reader reputation or reward ledger: %+v", reader)
93 }
94 path := "finder/" + finder.String()
95 main := Render(path)
96 if !strings.Contains(main, "Reward-eligible credits: 1") ||
97 !strings.Contains(main, "Owed: 0.001 GNOT") ||
98 !strings.Contains(main, "Paid: 0 GNOT") ||
99 !strings.Contains(main, "Rewarded location") || strings.Contains(main, "Volunteer note") {
100 t.Fatalf("wrong finder view: %s", main)
101 }
102 unpaidPage := Render(path + "/unpaid")
103 if !strings.Contains(unpaidPage, "## Unpaid credits") ||
104 !strings.Contains(unpaidPage, "Volunteer note") || strings.Contains(unpaidPage, "Rewarded location") {
105 t.Fatalf("wrong unpaid page: %s", unpaidPage)
106 }
107}
108
109func TestFundingAndClosureGuards(cur realm, t *testing.T) {
110 testing.SetRealm(testing.NewUserRealm(testOwner))
111 book := CreateBook(cross(cur), "Funding guards", "First edition", testLink, 1000)
112 assertPanic(t, revive(func() { FundBook(cross(cur), book) }), errPayment)
113 testing.SetOriginSend(chain.Coins{{Denom: "utest", Amount: 100}})
114 assertPanic(t, revive(func() { FundBook(cross(cur), book) }), errPaymentDenom)
115 testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 500)))
116 testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/fundingcaller"))
117 assertPanic(t, revive(func() { FundBook(cross(cur), book) }), errDirectCall)
118
119 testing.SetRealm(testing.NewUserRealm(testSponsor))
120 FundBook(cross(cur), book)
121 assertPanic(t, revive(func() { RefundSponsor(cross(cur), book) }), errRejectCoins)
122 testing.SetOriginSend(nil)
123 assertPanic(t, revive(func() { RefundSponsor(cross(cur), book) }), errRefundUnavailable)
124 report := Submit(cross(cur), book, "Section 1", "A sponsored report.")
125 testing.SetRealm(testing.NewUserRealm(testOwner))
126 assertPanic(t, revive(func() { Withdraw(cross(cur), book, 1) }), errWithdraw)
127 SetBookOpen(cross(cur), book, false)
128 assertPanic(t, revive(func() { Accept(cross(cur), report, "") }), errClosedAccept)
129 assertPanic(t, revive(func() { SetBookOpen(cross(cur), book, true) }), errSponsorRefundsPending)
130 assertPanic(t, revive(func() { RefundSponsor(cross(cur), book) }), errNoSponsorFunds)
131
132 testing.SetRealm(testing.NewUserRealm(testReader))
133 testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 1)))
134 assertPanic(t, revive(func() { FundBook(cross(cur), book) }), errClosedSponsor)
135 assertPanic(t, revive(func() { SettleOwed(cross(cur), book) }), errRejectCoins)
136 testing.SetOriginSend(nil)
137 if SponsorBalance(book, testSponsor) != 500 {
138 t.Fatal("a blocked action changed the sponsor balance")
139 }
140}
141
142func TestCountersSurviveRemovals(cur realm, t *testing.T) {
143 testing.SetRealm(testing.NewUserRealm(testOwner))
144 first := CreateBook(cross(cur), "First book", "First edition", testLink, 0)
145 second := CreateBook(cross(cur), "Second book", "First edition", testLink, 0)
146 books.Remove(idKey(first))
147 third := CreateBook(cross(cur), "Third book", "First edition", testLink, 0)
148 if third != second+1 {
149 t.Fatalf("book ID %d did not advance from %d", third, second)
150 }
151 a := Submit(cross(cur), third, "Section A", "A correction.")
152 b := Submit(cross(cur), third, "Section B", "Another correction.")
153 errataByID.Remove(idKey(a))
154 c := Submit(cross(cur), third, "Section C", "A third correction.")
155 if c != b+1 {
156 t.Fatalf("report ID %d did not advance from %d", c, b)
157 }
158}
159
160func TestBoundedLists(cur realm, t *testing.T) {
161 testing.SetRealm(testing.NewUserRealm(testOwner))
162 book := CreateBook(cross(cur), "Pagination", "First edition", testLink, 0)
163 for i := 0; i < maxListLimit+1; i++ {
164 CreateBook(cross(cur), "Book "+strconv.Itoa(i), "First edition", testLink, 0)
165 id := Submit(cross(cur), book, "Location "+strconv.Itoa(i), "A correction.")
166 Accept(cross(cur), id, "")
167 }
168 if got := len(ListBooks(0, int(maxAmount))); got != 50 {
169 t.Fatalf("book limit = %d, want 50", got)
170 }
171 if len(ListBooks(books.Size(), 1)) != 0 || len(ListBooks(0, 0)) != 0 || len(ListBooks(-1, 1)) != 0 {
172 t.Fatal("book list accepted an out-of-range offset or empty limit")
173 }
174 if got := len(ListErrata(book, Accepted, 0, int(maxAmount))); got != 50 {
175 t.Fatalf("report limit = %d, want 50", got)
176 }
177 if len(ListErrata(book, Accepted, 52, 5)) != 0 || len(ListErrata(book, Accepted, 0, 0)) != 0 ||
178 len(ListErrata(book, Accepted, -1, 1)) != 0 || len(ListErrata(book, "unknown", 0, 1)) != 0 {
179 t.Fatal("report list accepted invalid pagination or a status")
180 }
181 if got := len(AcceptedEntries(book)); got != 50 {
182 t.Fatalf("default entries = %d, want 50", got)
183 }
184 last := AcceptedEntriesPage(book, 50, 200)
185 if len(last) != 1 || last[0].Location != "Location 50" {
186 t.Fatalf("unexpected last page: %+v", last)
187 }
188}
189
190func TestModeratedRendering(cur realm, t *testing.T) {
191 testing.SetRealm(testing.NewUserRealm(testOwner))
192 book := CreateBook(cross(cur), "Moderation", "First edition", testLink, 0)
193 acceptedText := "Accepted marker | <b>safe</b>"
194 rejectedText := "Rejected marker | <script>unsafe</script>"
195 accepted := Submit(cross(cur), book, "Section A", acceptedText)
196 rejected := Submit(cross(cur), book, "Section B", rejectedText)
197 Accept(cross(cur), accepted, "")
198 Reject(cross(cur), rejected, "Rejected note marker")
199 bookPath := "book/" + strconv.Itoa(book)
200 bookPage := Render(bookPath)
201 for _, want := range []string{"Owner funds:", "Sponsor funds:", "(locked)", "Owed:", "The owner decides", sanitize.TableCell(acceptedText)} {
202 if !strings.Contains(bookPage, want) {
203 t.Errorf("book page missing %q", want)
204 }
205 }
206 for _, route := range []string{
207 "", bookPath, bookPath + "/pending",
208 "erratum/" + strconv.Itoa(rejected), "finder/" + testOwner.String() + "/unpaid",
209 } {
210 got := Render(route)
211 if strings.Contains(got, "Rejected marker") || strings.Contains(got, "Rejected note marker") {
212 t.Errorf("rejected correction or note leaked into %q", route)
213 }
214 }
215 all := Render(bookPath + "/all")
216 if !strings.Contains(all, sanitize.TableCell(rejectedText)) ||
217 !strings.Contains(all, "Rejected note marker") || strings.Contains(all, "<script>") {
218 t.Fatalf("all reports should escape and show the rejected correction: %s", all)
219 }
220}
221
222func TestPublicReportNoticeOnHomeAndBook(cur realm, t *testing.T) {
223 const notice = "Reports are public and permanent on-chain."
224 testing.SetRealm(testing.NewUserRealm(testOwner))
225 book := CreateBook(cross(cur), "Public report notice", "First edition", testLink, 0)
226
227 if !strings.Contains(Render(""), notice+"\n\nUse the Actions tab") {
228 t.Error("home page must show the notice directly before the report instructions")
229 }
230 if !strings.Contains(Render("book/"+strconv.Itoa(book)), notice+"\n\n[Pending (") {
231 t.Error("book page must show the notice directly before the pending report link")
232 }
233}