advent_test.gno
5.25 Kb · 141 lines
1package advent
2
3import (
4 "strings"
5 "testing"
6 "time"
7
8 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/tally/v0"
9 "gno.land/p/nt/testutils/v0"
10 "gno.land/p/nt/uassert/v0"
11)
12
13func reset() {
14 days = [Days]*challenge{}
15 board = tally.NewBoard()
16 oracle = organizer
17}
18
19func TestCalendarAndReviews(cur realm, t *testing.T) {
20 reset()
21 defer reset()
22 alice := testutils.TestAddress("alice")
23 bob := testutils.TestAddress("bob")
24 checker := testutils.TestAddress("oracle")
25 hash, ciphertext := strings.Repeat("a", 64), strings.Repeat("b", 512)
26 key := strings.Repeat("a", 588)
27 next := time.Now().Unix() + 3600
28
29 testing.SetRealm(testing.NewUserRealm(alice))
30 uassert.AbortsWithMessage(t, cur, "organizer only", func() {
31 Publish(cross(cur), 1, "First clue", "Follow the gnome.", key, next)
32 })
33 uassert.AbortsWithMessage(t, cur, "this door is still locked", func() {
34 Submit(cross(cur), 1, hash, ciphertext)
35 })
36
37 testing.SetRealm(testing.NewUserRealm(organizer))
38 Publish(cross(cur), 1, "The [hidden] seal", "One line\nTwo lines", key, next)
39 Publish(cross(cur), 3, "Snow", "Third clue", key, next)
40 SetOracle(cross(cur), checker)
41 uassert.AbortsWithMessage(t, cur, "invalid or already open door", func() {
42 Publish(cross(cur), 1, "New clue", "Follow the gnome.", key, next)
43 })
44 uassert.True(t, strings.Contains(Render(""), "2 / 24 doors open"), "calendar shows all opened doors")
45 uassert.True(t, strings.Contains(Render("door/1"), `The \[hidden\] seal`), "title is escaped")
46 uassert.True(t, strings.Contains(Render("door/1"), "> One line\n> Two lines"), "clue keeps line breaks")
47 uassert.True(t, strings.Contains(Render("door/2"), "Still snowed in"), "unpublished doors stay closed")
48
49 testing.SetRealm(testing.NewUserRealm(alice))
50 uassert.Equal(t, 1, Submit(cross(cur), 1, hash, ciphertext))
51 uassert.AbortsWithMessage(t, cur, "you already have a pending guess or a solve", func() {
52 Submit(cross(cur), 1, hash, ciphertext)
53 })
54 uassert.AbortsWithMessage(t, cur, "oracle only", func() {
55 Review(cross(cur), 1, 1, "1")
56 })
57 testing.SetRealm(testing.NewUserRealm(bob))
58 uassert.Equal(t, 2, Submit(cross(cur), 1, hash, ciphertext))
59 testing.SetRealm(testing.NewUserRealm(checker))
60 uassert.AbortsWithMessage(t, cur, "this case is still under seal", func() {
61 Review(cross(cur), 1, 1, "01")
62 })
63
64 doors(1).reviewFrom = time.Now().Unix()
65 uassert.AbortsWithMessage(t, cur, "verdict must be 0 or 1", func() {
66 Review(cross(cur), 1, 1, "1x")
67 })
68 uassert.Equal(t, "1", Progress(alice.String())[:1])
69 Review(cross(cur), 1, 1, "01")
70 uassert.AbortsWithMessage(t, cur, "invalid review range", func() {
71 Review(cross(cur), 1, 1, "11")
72 })
73 uassert.Equal(t, "2", Progress(alice.String())[:1])
74 uassert.Equal(t, "3", Progress(bob.String())[:1])
75 uassert.Equal(t, bob.String()+"\t1\n", Leaders())
76 uassert.True(t, strings.Contains(Attempts(1, 2), "2\t"+bob.String()+"\t"+hash+"\t"+ciphertext+"\t3"), "sealed guess is publicly readable")
77 uassert.True(t, strings.Contains(Render("seal/1/2"), hash), "individual proof shows full commitment")
78
79 testing.SetRealm(testing.NewUserRealm(alice))
80 uassert.Equal(t, 3, Submit(cross(cur), 1, hash, ciphertext))
81 testing.SetRealm(testing.NewUserRealm(checker))
82 Review(cross(cur), 1, 3, "1")
83 uassert.Equal(t, 2, doors(1).solves)
84 uassert.True(t, strings.Contains(Render("scores"), alice.String()), "late solve updates the board")
85 uassert.True(t, strings.Contains(Render("door/1"), "2 solves"), "late solve updates the stats")
86
87 testing.SetRealm(testing.NewUserRealm(bob))
88 uassert.AbortsWithMessage(t, cur, "you already have a pending guess or a solve", func() {
89 Submit(cross(cur), 1, hash, ciphertext)
90 })
91}
92
93func TestUpdateClue(cur realm, t *testing.T) {
94 reset()
95 defer reset()
96 player := testutils.TestAddress("clue-player")
97 key := strings.Repeat("a", 588)
98 now := time.Now().Unix()
99 testing.SetRealm(testing.NewUserRealm(organizer))
100 Publish(cross(cur), 1, "The original title", "Original body", key, now+3600)
101 uassert.AbortsWithMessage(t, cur, "this door is still locked", func() {
102 UpdateClue(cross(cur), 2, "Corrected body")
103 })
104
105 testing.SetRealm(testing.NewUserRealm(player))
106 Submit(cross(cur), 1, strings.Repeat("a", 64), strings.Repeat("b", 512))
107 uassert.AbortsWithMessage(t, cur, "organizer only", func() {
108 UpdateClue(cross(cur), 1, "Wrong organizer")
109 })
110
111 testing.SetRealm(testing.NewUserRealm(organizer))
112 d := doors(1)
113 d.reviewFrom = now
114 Review(cross(cur), 1, 1, "1")
115 before := Attempts(1, 1)
116 uassert.AbortsWithMessage(t, cur, "invalid clue", func() {
117 UpdateClue(cross(cur), 1, strings.Repeat("a", 2501))
118 })
119 uassert.Equal(t, "Original body", d.clue)
120
121 UpdateClue(cross(cur), 1, "The corrected body")
122 uassert.Equal(t, "The corrected body", d.clue)
123 uassert.True(t, strings.Contains(Render("door/1"), "> The corrected body"), "GnoWeb renders the corrected clue")
124 uassert.True(t, strings.Contains(Calendar(), "VGhlIGNvcnJlY3RlZCBib2R5"), "the rich app reads the corrected clue")
125 uassert.Equal(t, "The original title", d.title)
126 uassert.Equal(t, key, d.publicKey)
127 uassert.Equal(t, now, d.reviewFrom)
128 uassert.Equal(t, before, Attempts(1, 1))
129 uassert.Equal(t, player.String()+"\t1\n", Leaders())
130}
131
132func ExampleRender_scores() {
133 reset()
134 print(Render("scores"))
135 // Output:
136 // [Gnoël calendar](/r/moul/advent/v0)
137 //
138 // ## 🏆 The Nice List
139 //
140 // _Nobody has thawed the scoreboard yet._
141}