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.

render_test.gno

16.80 Kb · 365 lines
  1package questmaster
  2
  3import (
  4	"chain/runtime"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/escrow"
  9	"gno.land/p/nt/testutils/v0"
 10	"gno.land/p/nt/uassert/v0"
 11)
 12
 13const base = "/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/questmaster"
 14
 15func has(t *testing.T, page, want, what string) {
 16	t.Helper()
 17	if !strings.Contains(page, want) {
 18		t.Fatalf("%s: page does not contain %q\n--- page ---\n%s", what, want, page)
 19	}
 20}
 21
 22func hasNot(t *testing.T, page, bad, what string) {
 23	t.Helper()
 24	if strings.Contains(page, bad) {
 25		t.Fatalf("%s: page must not contain %q\n--- page ---\n%s", what, bad, page)
 26	}
 27}
 28
 29// renders calls Render and fails the test (instead of crashing) on a panic.
 30func renders(t *testing.T, path string) string {
 31	t.Helper()
 32	var out string
 33	if r := revive(func() { out = Render(path) }); r != nil {
 34		t.Fatalf("Render(%q) panicked: %v", path, r)
 35	}
 36	return out
 37}
 38
 39var allPaths = []string{
 40	"", "?page=2", "help", "paid", "paid?page=2",
 41	"quest/1", "quest/2", "quest/3", "quest/4", "quest/5", "quest/6",
 42	"poster/" + alice.String(), "builder/" + bob.String(), "poster/" + bob.String() + "?page=2",
 43}
 44
 45func TestRenderEmptyBoard(cur realm, t *testing.T) {
 46	resetState()
 47	for _, p := range allPaths {
 48		renders(t, p)
 49	}
 50	home := renders(t, "")
 51	has(t, home, "# QuestMaster", "home title")
 52	has(t, home, "| 0 | 0 | 0 | 0 |", "empty totals")
 53	has(t, home, "No open quests right now.", "empty list")
 54	has(t, home, "[Post the first one]("+base+"$help&func=PostQuest&.send=1000000ugnot)", "post link with send")
 55	has(t, renders(t, "paid"), "No quest has been paid yet.", "empty paid")
 56	has(t, renders(t, "poster/"+alice.String()), "No quests yet.", "empty poster")
 57	has(t, renders(t, "quest/1"), "# Not found", "no quest 1 yet")
 58}
 59
 60// board builds a board with quests in every state and more than one page of
 61// open quests. Known numbers:
 62//
 63//	1 Paid (approved, 2 GNOT)       2 Paid (timeout, 3 GNOT)
 64//	3 Cancelled (1.5 GNOT)          4 Claimed by bob
 65//	5 Submitted by bob              6 Open after one reject
 66//	7..31 Open (25 quests, 1 GNOT each)
 67func board(cur realm) {
 68	resetState()
 69	a := submitted(cur, 2*gnot)
 70	testing.SetRealm(testing.NewUserRealm(alice))
 71	testing.SetOriginSend(nil)
 72	ApproveWork(cross(cur), a)
 73
 74	b := submitted(cur, 3*gnot)
 75	testing.SkipHeights(escrow.ReviewBlocks + 1)
 76	testing.SetRealm(testing.NewUserRealm(bob))
 77	testing.SetOriginSend(nil)
 78	ClaimPayout(cross(cur), b)
 79
 80	c := post(cur, alice, 1_500_000, "Cancelled quest")
 81	testing.SetRealm(testing.NewUserRealm(alice))
 82	testing.SetOriginSend(nil)
 83	CancelQuest(cross(cur), c)
 84
 85	claimed(cur, 4*gnot)
 86	submitted(cur, 5*gnot)
 87
 88	f := submitted(cur, 6*gnot)
 89	testing.SetRealm(testing.NewUserRealm(alice))
 90	testing.SetOriginSend(nil)
 91	RejectWork(cross(cur), f, "Please add tests")
 92
 93	for i := 0; i < 25; i++ {
 94		post(cur, carol, gnot, "Small task")
 95	}
 96}
 97
 98func TestRenderFullBoard(cur realm, t *testing.T) {
 99	board(cur)
100	for _, p := range allPaths {
101		renders(t, p)
102	}
103	home := renders(t, "")
104	// open: 6 + 25 = 26; locked: 4+5+6+25 = 40; paid 2+3 = 5; 2 quests paid
105	has(t, home, "| 26 | 40 | 5 | 2 |", "totals")
106	uassert.Equal(t, PageSize, strings.Count(home, "](/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/questmaster:quest/"), "first page holds PageSize quests")
107	has(t, home, "| 31 | [Small task]("+base+":quest/31) | 1 GNOT | 2,000 (about 117 minutes) | 0 |", "newest first")
108	has(t, home, "Page 1 of 2 · [Older →]("+base+"?page=2)", "next link")
109	hasNot(t, home, "Newer", "no prev link on page 1")
110
111	p2 := renders(t, "?page=2")
112	uassert.Equal(t, 6, strings.Count(p2, "](/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/questmaster:quest/"), "second page")
113	has(t, p2, "| 6 | [claimed quest]("+base+":quest/6) | 6 GNOT | 2,000 (about 117 minutes) | 1 |", "rejected quest shows count")
114	has(t, p2, "Page 2 of 2 · [← Newer]("+base+"?page=1)", "prev link")
115	has(t, renders(t, "?page=3"), "This page is empty.", "past the end")
116
117	paid := renders(t, "paid")
118	has(t, paid, "2 quests, 5 GNOT in total", "paid summary")
119	has(t, paid, "| 2 | [claimed quest]("+base+":quest/2) | 3 GNOT | "+userLink(bob, false)+" | 0 |", "paid row")
120
121	poster := renders(t, "poster/"+alice.String())
122	has(t, poster, "| 3 | [Cancelled quest]("+base+":quest/3) | 1.5 GNOT | Cancelled (refunded) | - |", "cancelled row")
123	has(t, poster, "| 4 | [claimed quest]("+base+":quest/4) | 4 GNOT | Claimed (work in progress) |", "claimed row")
124	builder := renders(t, "builder/"+bob.String())
125	// builder history keeps quest 6 although bob was rejected
126	has(t, builder, "| 6 | [claimed quest]("+base+":quest/6) | 6 GNOT | Open |", "history row")
127	has(t, builder, "| 1 | [claimed quest]("+base+":quest/1) | 2 GNOT | Paid |", "paid in history")
128	has(t, renders(t, "poster/"+carol.String()+"?page=2"), "Page 2 of 2", "poster page 2")
129}
130
131func TestRenderQuestStates(cur realm, t *testing.T) {
132	board(cur)
133	open := renders(t, "quest/7")
134	has(t, open, "| State | **Open** |", "open state")
135	has(t, open, "**Claim**", "claim step")
136	has(t, open, "[Open in gnoweb]("+base+"$help&func=ClaimQuest&id=7)", "claim link")
137	has(t, open, "[Open in gnoweb]("+base+"$help&func=CancelQuest&id=7)", "cancel link")
138	has(t, open, `-pkgpath "gno.land/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/questmaster" -func "ClaimQuest" -args "7"`, "gnokey claim")
139	has(t, open, "-chainid onyx-1 -remote https://rpc.onyx.testnets.gno.land:443 KEY", "onyx pinned")
140	hasNot(t, open, "ApproveWork", "no approve when open")
141
142	claimedPage := renders(t, "quest/4")
143	has(t, claimedPage, "| State | **Claimed (work in progress)** |", "claimed state")
144	has(t, claimedPage, "func=SubmitWork&id=4", "submit link")
145	has(t, claimedPage, "func=UnclaimQuest&id=4", "unclaim link")
146	hasNot(t, claimedPage, "func=ReleaseQuest", "no release before deadline")
147	has(t, claimedPage, "anyone can release the quest from block", "release explained")
148	has(t, claimedPage, "| Work deadline | block ", "work deadline row")
149	has(t, claimedPage, " left) |", "time left")
150
151	sub := renders(t, "quest/5")
152	has(t, sub, "| State | **Submitted (waiting for review)** |", "submitted")
153	has(t, sub, "func=ApproveWork&id=5", "approve link")
154	has(t, sub, "func=RejectWork&id=5", "reject link")
155	has(t, sub, "| Work | [https://github\\.com/example/repo/pull/1](https://github.com/example/repo/pull/1) |", "work link")
156	hasNot(t, sub, "func=ClaimPayout", "no payout before deadline")
157
158	rejected := renders(t, "quest/6")
159	has(t, rejected, "## Last reject reason", "reason heading")
160	has(t, rejected, "> Please add tests  ", "reason text")
161	has(t, rejected, "| Times rejected | 1 |", "reject count")
162
163	has(t, renders(t, "quest/1"), "Nothing: this quest is finished and the builder was paid 2 GNOT.", "paid quest")
164	has(t, renders(t, "quest/3"), "Nothing: this quest was cancelled and the reward went back to the poster", "cancelled quest")
165
166	// after the deadlines the late moves appear and the early ones go
167	testing.SkipHeights(escrow.ReviewBlocks + 1)
168	late := renders(t, "quest/5")
169	has(t, late, "func=ClaimPayout&id=5", "payout link after deadline")
170	hasNot(t, late, "func=RejectWork", "no reject after deadline")
171	has(t, late, "(passed) |", "deadline passed")
172	lateClaim := renders(t, "quest/4")
173	has(t, lateClaim, "func=ReleaseQuest&id=4", "release link after deadline")
174	hasNot(t, lateClaim, "func=SubmitWork", "no submit after deadline")
175}
176
177// User text must never become Markdown structure. These asserts pin the exact
178// escaped output for every dangerous character.
179func TestRenderInjection(cur realm, t *testing.T) {
180	resetState()
181	title := "# A | `c` [x](y) ![i](z) <script> * _ ) @alice"
182	details := "Line one\n# not a heading\n- not a list\n| a | b |\n|---|---|\n<script>alert(1)</script>\n![img](https://e.vil/i.png) [link](https://e.vil)\nsee g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 and @bob\n===\n\n\n    indented"
183	id := postD(cur, alice, gnot, title, details)
184	testing.SetRealm(testing.NewUserRealm(bob))
185	testing.SetOriginSend(nil)
186	ClaimQuest(cross(cur), id)
187	SubmitWork(cross(cur), id, "https://e.vil/a_(b)<c>`d`|e")
188	testing.SetRealm(testing.NewUserRealm(alice))
189	testing.SetOriginSend(nil)
190	RejectWork(cross(cur), id, "Bad\n## heading\n[x](https://e.vil) <b>")
191
192	// second quest keeps the hostile link visible
193	id2 := postD(cur, alice, gnot, "Link test", "")
194	testing.SetRealm(testing.NewUserRealm(bob))
195	testing.SetOriginSend(nil)
196	ClaimQuest(cross(cur), id2)
197	SubmitWork(cross(cur), id2, "https://e.vil/a_(b)<c>`d`|e")
198
199	escTitle := "\\# A | \\`c\\` \\[x\\]\\(y\\) \\!\\[i\\]\\(z\\) \\<script\\> \\* \\_ \\) \\@alice"
200	escCellTitle := strings.ReplaceAll(escTitle, "|", "\\|")
201
202	q := renders(t, "quest/1")
203	has(t, q, "# Quest 1: "+escTitle+"\n", "title in heading")
204	has(t, q, "> Line one  \n> \\# not a heading  \n> \\- not a list  \n> \\| a \\| b \\|  \n> \\|\\-\\-\\-\\|\\-\\-\\-\\|  \n"+
205		"> \\<script\\>alert\\(1\\)\\</script\\>  \n> \\!\\[img\\]\\(https://e\\.vil/i\\.png\\) \\[link\\]\\(https://e\\.vil\\)  \n"+
206		"> see&#32;g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 and \\@bob  \n> \\===  \n>\n> indented  \n", "details block")
207	has(t, q, "> Bad  \n> \\#\\# heading  \n> \\[x\\]\\(https://e\\.vil\\) \\<b\\>  \n", "reason block")
208
209	home := renders(t, "")
210	has(t, renders(t, "poster/"+alice.String()), "| 2 | [Link test]("+base+":quest/2) |", "plain title")
211	has(t, home, "| 1 | ["+escCellTitle+"]("+base+":quest/1) |", "title in table cell")
212
213	q2 := renders(t, "quest/2")
214	// ( ) < > ` | are percent-encoded in the target; the visible text is the
215	// escaped URL itself
216	has(t, q2, "| Work | [https://e\\.vil/a\\_\\(b\\)\\<c\\>\\`d\\`\\|e](https://e.vil/a_%28b%29%3Cc%3E%60d%60%7Ce) |", "hostile link made safe")
217	hasNot(t, q2, "](https://e.vil/a_(", "no raw parenthesis in a link target")
218	has(t, q2, "_No details given._", "empty details")
219
220	for _, page := range []string{q, q2, home} {
221		hasNot(t, page, "<script>", "raw script tag")
222		hasNot(t, page, "![", "unescaped image")
223		hasNot(t, page, "\n#  ", "stray heading")
224		hasNot(t, page, " @alice", "raw mention")
225		hasNot(t, page, " g1jg8m", "raw address mention")
226	}
227}
228
229func TestRenderBadPaths(cur realm, t *testing.T) {
230	board(cur)
231	bad := []string{
232		"nope", "quest", "quest/", "quest/abc", "quest/0", "quest/999", "quest/-1", "quest/1/2", "quest/+1",
233		"quest/99999999999999999999999", "quest/1e3", "poster/", "poster/notanaddress",
234		"poster/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf6", "builder/<script>", "builder/" + strings.Repeat("g", 500),
235		"paid/x", "HELP", "../quest/1", "quest/1#x", "%00", "quest/1\n# injected",
236	}
237	for _, p := range bad {
238		out := renders(t, p)
239		has(t, out, "# Not found", "bad path "+p)
240		has(t, out, "[Go to the QuestMaster board]("+base+")", "home link "+p)
241		hasNot(t, out, "script", "path not echoed "+p)
242		hasNot(t, out, "injected", "path not echoed "+p)
243	}
244	// bad page numbers fall back to page 1 or an empty page, never a panic
245	for _, p := range []string{"?page=-5", "?page=0", "?page=abc", "?page=", "?page=1.5", "?%zz", "?page=99999999999999999999"} {
246		has(t, renders(t, p), "Page 1 of 2", "page fallback "+p)
247	}
248	for _, p := range []string{"?page=1000000", "?page=999999999", "paid?page=999", "poster/" + carol.String() + "?page=50"} {
249		has(t, renders(t, p), "This page is empty.", "huge page "+p)
250	}
251	// a valid address with no quests
252	has(t, renders(t, "builder/"+testutils.TestAddress("nobody").String()), "No quests yet.", "unknown user")
253	// query on a quest page is ignored
254	has(t, renders(t, "quest/1?page=2"), "# Quest 1:", "quest with query")
255}
256
257func TestRenderHelp(cur realm, t *testing.T) {
258	resetState()
259	h := renders(t, "help")
260	has(t, h, "**Onyx test network** (`onyx-1`)", "testnet note")
261	has(t, h, "test GNOT", "test GNOT")
262	for _, fn := range []string{"PostQuest", "ClaimQuest", "SubmitWork", "ApproveWork", "RejectWork", "CancelQuest", "UnclaimQuest", "ReleaseQuest", "ClaimPayout"} {
263		has(t, h, `-func "`+fn+`"`, "command for "+fn)
264	}
265	has(t, h, `-func "PostQuest" -args "Fix a typo in the docs" -args "Details of the task" -args "20000" -send "5000000ugnot"`, "post with send")
266	hasNot(t, h, "gnoland-1", "no mainnet")
267	hasNot(t, h, "rpc.gno.land", "no mainnet rpc")
268	hasNot(t, h, "rpc.latest", "no moving alias")
269	uassert.Equal(t, 9, strings.Count(h, "-chainid onyx-1 -remote https://rpc.onyx.testnets.gno.land:443 KEY"), "every command pinned to Onyx")
270}
271
272func TestFormatHelpers(t *testing.T) {
273	cases := map[int64]string{0: "0", 1: "0.000001", 10: "0.00001", 1_000_000: "1", 1_500_000: "1.5", 123_456_789: "123.456789", 5_250_000: "5.25", -2_500_000: "-2.5"}
274	for in, want := range cases {
275		uassert.Equal(t, want, gnotStr(in))
276	}
277	uassert.Equal(t, "0", blocksStr(0))
278	uassert.Equal(t, "999", blocksStr(999))
279	uassert.Equal(t, "1,000", blocksStr(1000))
280	uassert.Equal(t, "2,200,000", blocksStr(2_200_000))
281	uassert.Equal(t, "now", approx(0))
282	uassert.Equal(t, "about a minute", approx(10))
283	uassert.Equal(t, "about 58 minutes", approx(1_000))
284	uassert.Equal(t, "about 3 days", approx(75_000))
285	uassert.Equal(t, "about 7 days", approx(escrow.ReviewBlocks))
286	uassert.Equal(t, "about 89 days", approx(2_200_000))
287	uassert.Equal(t, "passed", left(10, 11))
288	uassert.Equal(t, "about a minute left", left(10, 10))
289}
290
291func TestRenderClosing(cur realm, t *testing.T) {
292	resetState()
293	cl := claimed(cur, 2*gnot)
294	sub := submitted(cur, 3*gnot)
295	// before closing: the poster sees "Close quest" on claimed and submitted quests
296	has(t, renders(t, "quest/1"), "**Close quest**", "close offered on claimed")
297	has(t, renders(t, "quest/2"), "**Close quest**", "close offered on submitted")
298	has(t, renders(t, "quest/1"), "func=CancelQuest&id=1", "close link")
299
300	testing.SetRealm(testing.NewUserRealm(alice))
301	testing.SetOriginSend(nil)
302	CancelQuest(cross(cur), cl)
303	CancelQuest(cross(cur), sub)
304
305	c := renders(t, "quest/1")
306	has(t, c, "| State | **Claimed (work in progress), closing** |", "closing state")
307	has(t, c, "> **Closing**: the reward returns to the poster when the current builder's turn ends, unless the work is approved.", "closing note")
308	has(t, c, "It is closing, so it ends and the reward goes back to the poster.", "unclaim text while closing")
309	has(t, c, "anyone can end the quest from block", "release text while closing")
310	hasNot(t, c, "**Close quest**", "no second close")
311	s := renders(t, "quest/2")
312	has(t, s, "The quest is closing, so it ends and the reward goes back to the poster.", "reject text while closing")
313	hasNot(t, s, "**Close quest**", "no second close")
314	has(t, renders(t, "poster/"+alice.String()), "Claimed (work in progress), closing", "closing mark in table")
315	has(t, renders(t, ""), "No open quests right now.", "closing quests are not open")
316
317	testing.SkipHeights(GetQuest(cl).WorkDeadline - runtime.ChainHeight() + 1)
318	has(t, renders(t, "quest/1"), "so anyone can end the quest; the reward goes back to the poster.", "release while closing")
319}
320
321func TestRenderReviewWarningAndFooter(cur realm, t *testing.T) {
322	board(cur)
323	warn := "**Posters: check your quest.** If you do not approve or reject a submission within the review window (about 7 days), the builder is paid automatically."
324	has(t, renders(t, ""), warn, "warning on home")
325	has(t, renders(t, "help"), warn, "warning on help")
326	has(t, renders(t, "help"), "Anyone except you can claim your quest while it is open; once it is claimed, closing it (below) stops new claims.", "claim note on help")
327	foot := "\n---\n\nBuilt on [gno.land](https://gno.land) · [source](https://github.com/elangrr/questmaster)\n"
328	for _, p := range append(allPaths, "nope", "quest/999") {
329		out := renders(t, p)
330		if !strings.HasSuffix(out, foot) {
331			t.Fatalf("page %q does not end with the footer", p)
332		}
333	}
334}
335
336// Math ($...$) and mention links at the start of user text are neutralised.
337func TestRenderMathAndLeadingAddress(cur realm, t *testing.T) {
338	resetState()
339	addr := "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
340	id := postD(cur, alice, gnot, addr+" pays $x$", "$$y$$ and $z$\n"+addr+" line two")
341	testing.SetRealm(testing.NewUserRealm(bob))
342	testing.SetOriginSend(nil)
343	ClaimQuest(cross(cur), id)
344	SubmitWork(cross(cur), id, "https://e.vil/$x$")
345	testing.SetRealm(testing.NewUserRealm(alice))
346	testing.SetOriginSend(nil)
347	RejectWork(cross(cur), id, addr+" no $$m$$")
348
349	id2 := postD(cur, alice, gnot, "Link with dollars", "")
350	testing.SetRealm(testing.NewUserRealm(bob))
351	testing.SetOriginSend(nil)
352	ClaimQuest(cross(cur), id2)
353	SubmitWork(cross(cur), id2, "https://e.vil/$x$")
354
355	q := renders(t, "quest/1")
356	has(t, q, "# Quest 1: &#103;1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 pays \\$x\\$\n", "title in heading")
357	has(t, q, "> \\$\\$y\\$\\$ and \\$z\\$  \n> &#103;1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 line two  \n", "details lines")
358	has(t, q, "> &#103;1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 no \\$\\$m\\$\\$  \n", "reason line")
359	has(t, renders(t, "poster/"+alice.String()), "| 1 | [&#103;1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 pays \\$x\\$]("+base+":quest/1) |", "title in a table cell")
360	has(t, renders(t, "quest/2"), "| Work | [https://e\\.vil/\\$x\\$](https://e.vil/$x$) |", "dollar in the link text")
361	for _, p := range []string{q, renders(t, "quest/2")} {
362		hasNot(t, p, " "+addr, "raw address after a space")
363		hasNot(t, p, " $", "raw dollar after a space")
364	}
365}