advent_test.gno
7.80 Kb · 212 lines
1package advent
2
3import (
4 "strings"
5 "testing"
6 "time"
7
8 "gno.land/p/moul/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 publisher = organizer
18 carryingOver = false
19}
20
21func TestCalendarAndReviews(cur realm, t *testing.T) {
22 reset()
23 defer reset()
24 alice := testutils.TestAddress("alice")
25 bob := testutils.TestAddress("bob")
26 checker := testutils.TestAddress("oracle")
27 hash, ciphertext := strings.Repeat("a", 64), strings.Repeat("b", 512)
28 key := strings.Repeat("a", 588)
29 next := time.Now().Unix() + 3600
30
31 testing.SetRealm(testing.NewUserRealm(alice))
32 uassert.AbortsWithMessage(t, cur, "publisher only", func() {
33 Publish(cross(cur), 1, "First clue", "Follow the gnome.", key, next)
34 })
35 uassert.AbortsWithMessage(t, cur, "this door is still locked", func() {
36 Submit(cross(cur), 1, hash, ciphertext)
37 })
38
39 testing.SetRealm(testing.NewUserRealm(organizer))
40 Publish(cross(cur), 1, "The [hidden] seal", "One line\nTwo lines", key, next)
41 Publish(cross(cur), 3, "Snow", "Third clue", key, next)
42 SetOracle(cross(cur), checker)
43 uassert.AbortsWithMessage(t, cur, "invalid or already open door", func() {
44 Publish(cross(cur), 1, "New clue", "Follow the gnome.", key, next)
45 })
46 uassert.True(t, strings.Contains(Render(""), "2 / 24 doors open"), "calendar shows all opened doors")
47 uassert.True(t, strings.Contains(Render("door/1"), `The \[hidden\] seal`), "title is escaped")
48 uassert.True(t, strings.Contains(Render("door/1"), "> One line\n> Two lines"), "clue keeps line breaks")
49 uassert.True(t, strings.Contains(Render("door/2"), "Still snowed in"), "unpublished doors stay closed")
50
51 testing.SetRealm(testing.NewUserRealm(alice))
52 uassert.Equal(t, 1, Submit(cross(cur), 1, hash, ciphertext))
53 uassert.AbortsWithMessage(t, cur, "you already have a pending guess or a solve", func() {
54 Submit(cross(cur), 1, hash, ciphertext)
55 })
56 uassert.AbortsWithMessage(t, cur, "oracle only", func() {
57 Review(cross(cur), 1, 1, "1", "")
58 })
59 testing.SetRealm(testing.NewUserRealm(bob))
60 uassert.Equal(t, 2, Submit(cross(cur), 1, hash, ciphertext))
61 testing.SetRealm(testing.NewUserRealm(checker))
62 uassert.AbortsWithMessage(t, cur, "this case is still under seal", func() {
63 Review(cross(cur), 1, 1, "01", "")
64 })
65
66 doors(1).reviewFrom = time.Now().Unix()
67 uassert.AbortsWithMessage(t, cur, "verdict must be 0 or 1", func() {
68 Review(cross(cur), 1, 1, "1x", "")
69 })
70 uassert.Equal(t, "1", Progress(alice.String())[:1])
71 Review(cross(cur), 1, 1, "01", doors(1).snapshot(1, 2))
72 uassert.AbortsWithMessage(t, cur, "invalid review range", func() {
73 Review(cross(cur), 1, 1, "11", "")
74 })
75 uassert.Equal(t, "2", Progress(alice.String())[:1])
76 uassert.Equal(t, "3", Progress(bob.String())[:1])
77 uassert.Equal(t, bob.String()+"\t1\n", Leaders())
78 uassert.True(t, strings.Contains(Attempts(1, 2), "2\t"+bob.String()+"\t"+hash+"\t"+ciphertext+"\t3"), "sealed guess is publicly readable")
79 uassert.True(t, strings.Contains(Render("seal/1/2"), hash), "individual proof shows full commitment")
80
81 testing.SetRealm(testing.NewUserRealm(alice))
82 uassert.Equal(t, 3, Submit(cross(cur), 1, hash, ciphertext))
83 testing.SetRealm(testing.NewUserRealm(checker))
84 Review(cross(cur), 1, 3, "1", doors(1).snapshot(3, 1))
85 uassert.Equal(t, 2, doors(1).solves)
86 uassert.True(t, strings.Contains(Render("scores"), alice.String()), "late solve updates the board")
87 uassert.True(t, strings.Contains(Render("door/1"), "2 solves"), "late solve updates the stats")
88
89 testing.SetRealm(testing.NewUserRealm(bob))
90 uassert.AbortsWithMessage(t, cur, "you already have a pending guess or a solve", func() {
91 Submit(cross(cur), 1, hash, ciphertext)
92 })
93}
94
95func TestUpdateClue(cur realm, t *testing.T) {
96 reset()
97 defer reset()
98 player := testutils.TestAddress("clue-player")
99 key := strings.Repeat("a", 588)
100 now := time.Now().Unix()
101 testing.SetRealm(testing.NewUserRealm(organizer))
102 Publish(cross(cur), 1, "The original title", "Original body", key, now+3600)
103 uassert.AbortsWithMessage(t, cur, "this door is still locked", func() {
104 UpdateClue(cross(cur), 2, "Corrected body")
105 })
106
107 testing.SetRealm(testing.NewUserRealm(player))
108 Submit(cross(cur), 1, strings.Repeat("a", 64), strings.Repeat("b", 512))
109 uassert.AbortsWithMessage(t, cur, "publisher only", func() {
110 UpdateClue(cross(cur), 1, "Wrong organizer")
111 })
112
113 testing.SetRealm(testing.NewUserRealm(organizer))
114 d := doors(1)
115 d.reviewFrom = now
116 Review(cross(cur), 1, 1, "1", d.snapshot(1, 1))
117 before := Attempts(1, 1)
118 uassert.AbortsWithMessage(t, cur, "invalid clue", func() {
119 UpdateClue(cross(cur), 1, strings.Repeat("a", 2501))
120 })
121 uassert.Equal(t, "Original body", d.clue)
122
123 UpdateClue(cross(cur), 1, "The corrected body")
124 uassert.Equal(t, "The corrected body", d.clue)
125 uassert.True(t, strings.Contains(Render("door/1"), "> The corrected body"), "GnoWeb renders the corrected clue")
126 uassert.True(t, strings.Contains(Calendar(), "VGhlIGNvcnJlY3RlZCBib2R5"), "the rich app reads the corrected clue")
127 uassert.Equal(t, "The original title", d.title)
128 uassert.Equal(t, key, d.publicKey)
129 uassert.Equal(t, now, d.reviewFrom)
130 uassert.Equal(t, before, Attempts(1, 1))
131 uassert.Equal(t, player.String()+"\t1\n", Leaders())
132}
133
134func TestPublisherDelegation(cur realm, t *testing.T) {
135 reset()
136 defer reset()
137 agent := testutils.TestAddress("publisher")
138 key := strings.Repeat("a", 588)
139 next := time.Now().Unix() + 3600
140
141 testing.SetRealm(testing.NewUserRealm(agent))
142 uassert.AbortsWithMessage(t, cur, "organizer only", func() {
143 SetPublisher(cross(cur), agent)
144 })
145
146 testing.SetRealm(testing.NewUserRealm(organizer))
147 SetPublisher(cross(cur), agent)
148 uassert.Equal(t, agent, Publisher())
149 uassert.Equal(t, organizer, oracle)
150 uassert.AbortsWithMessage(t, cur, "publisher only", func() {
151 Publish(cross(cur), 2, "Old publisher", "Wrong account", key, next)
152 })
153
154 testing.SetRealm(testing.NewUserRealm(agent))
155 Publish(cross(cur), 2, "Agent’s door", "Original body", key, next)
156 UpdateClue(cross(cur), 2, "Corrected body")
157 uassert.Equal(t, "Corrected body", doors(2).clue)
158}
159
160func TestReplaceCarriedSeal(cur realm, t *testing.T) {
161 reset()
162 defer reset()
163 player := testutils.TestAddress("legacy-player")
164 hash, ciphertext := strings.Repeat("a", 64), strings.Repeat("b", 512)
165 newHash := strings.Repeat("c", 64)
166 now := time.Now().Unix()
167 testing.SetRealm(testing.NewUserRealm(organizer))
168 Publish(cross(cur), 1, "The first door", "Clue", strings.Repeat("a", 588), now+3600)
169 carryingOver = true
170 CarryOver(cross(cur), player, hash, ciphertext)
171 uassert.True(t, strings.Contains(Attempts(1, 1), "/advent/v0"), "legacy seals keep their original commitment domain")
172
173 testing.SetRealm(testing.NewUserRealm(player))
174 uassert.AbortsWithMessage(t, cur, "the original seals are being carried over", func() {
175 ReplaceGuess(cross(cur), 1, newHash, ciphertext)
176 })
177
178 testing.SetRealm(testing.NewUserRealm(organizer))
179 FinishCarryOver(cross(cur))
180 d := doors(1)
181 d.reviewFrom = now
182 oldSnapshot := d.snapshot(1, 1)
183
184 testing.SetRealm(testing.NewUserRealm(player))
185 uassert.Equal(t, 1, ReplaceGuess(cross(cur), 1, newHash, ciphertext))
186 uassert.Equal(t, 1, d.submitted)
187 uassert.Equal(t, "", d.attempt(1).origin)
188 uassert.Equal(t, newHash, d.attempt(1).commitment)
189
190 testing.SetRealm(testing.NewUserRealm(organizer))
191 uassert.AbortsWithMessage(t, cur, "a seal changed; read the attempts again", func() {
192 Review(cross(cur), 1, 1, "1", oldSnapshot)
193 })
194 Review(cross(cur), 1, 1, "0", d.snapshot(1, 1))
195
196 testing.SetRealm(testing.NewUserRealm(player))
197 uassert.AbortsWithMessage(t, cur, "only a pending guess can be replaced", func() {
198 ReplaceGuess(cross(cur), 1, hash, ciphertext)
199 })
200 uassert.Equal(t, 2, Submit(cross(cur), 1, hash, ciphertext))
201}
202
203func ExampleRender_scores() {
204 reset()
205 print(Render("scores"))
206 // Output:
207 // [Gnoël calendar](/r/moul/advent/v1)
208 //
209 // ## 🏆 The Nice List
210 //
211 // _Nobody has thawed the scoreboard yet._
212}