advent_test.gno
3.73 Kb · 102 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}
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 ExampleRender_scores() {
94 reset()
95 print(Render("scores"))
96 // Output:
97 // [Gnoël calendar](/r/moul/advent/v0)
98 //
99 // ## 🏆 The Nice List
100 //
101 // _Nobody has thawed the scoreboard yet._
102}