1package events
2
3import (
4 "std"
5 "strings"
6 "testing"
7 "time"
8
9 "gno.land/p/demo/uassert"
10 "gno.land/p/demo/urequire"
11)
12
13var (
14 suRealm = std.NewUserRealm(su)
15
16 now = "2009-02-13T23:31:30Z" // time.Now() is hardcoded to this value in the gno test machine currently
17 parsedTimeNow, _ = time.Parse(time.RFC3339, now)
18)
19
20func TestAddEvent(t *testing.T) {
21 std.TestSetOrigCaller(su)
22 std.TestSetRealm(suRealm)
23
24 e1Start := parsedTimeNow.Add(time.Hour * 24 * 5)
25 e1End := e1Start.Add(time.Hour * 4)
26
27 AddEvent("Event 1", "this event is upcoming", "gno.land", "gnome land", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))
28
29 got := renderHome(false)
30
31 if !strings.Contains(got, "Event 1") {
32 t.Fatalf("Expected to find Event 1 in render")
33 }
34
35 e2Start := parsedTimeNow.Add(-time.Hour * 24 * 5)
36 e2End := e2Start.Add(time.Hour * 4)
37
38 AddEvent("Event 2", "this event is in the past", "gno.land", "gnome land", e2Start.Format(time.RFC3339), e2End.Format(time.RFC3339))
39
40 got = renderHome(false)
41
42 upcomingPos := strings.Index(got, "## Upcoming events")
43 pastPos := strings.Index(got, "## Past events")
44
45 e1Pos := strings.Index(got, "Event 1")
46 e2Pos := strings.Index(got, "Event 2")
47
48 // expected index ordering: upcoming < e1 < past < e2
49 if e1Pos < upcomingPos || e1Pos > pastPos {
50 t.Fatalf("Expected to find Event 1 in Upcoming events")
51 }
52
53 if e2Pos < upcomingPos || e2Pos < pastPos || e2Pos < e1Pos {
54 t.Fatalf("Expected to find Event 2 on auth different pos")
55 }
56
57 // larger index => smaller startTime (future => past)
58 if events[0].startTime.Unix() < events[1].startTime.Unix() {
59 t.Fatalf("expected ordering to be different")
60 }
61}
62
63func TestAddEventErrors(t *testing.T) {
64 std.TestSetOrigCaller(su)
65 std.TestSetRealm(suRealm)
66
67 _, err := AddEvent("", "sample desc", "gno.land", "gnome land", "2009-02-13T23:31:31Z", "2009-02-13T23:33:31Z")
68 uassert.ErrorIs(t, err, ErrEmptyName)
69
70 _, err = AddEvent("sample name", "sample desc", "gno.land", "gnome land", "", "2009-02-13T23:33:31Z")
71 uassert.ErrorContains(t, err, ErrInvalidStartTime.Error())
72
73 _, err = AddEvent("sample name", "sample desc", "gno.land", "gnome land", "2009-02-13T23:31:31Z", "")
74 uassert.ErrorContains(t, err, ErrInvalidEndTime.Error())
75
76 _, err = AddEvent("sample name", "sample desc", "gno.land", "gnome land", "2009-02-13T23:31:31Z", "2009-02-13T23:30:31Z")
77 uassert.ErrorIs(t, err, ErrEndBeforeStart)
78
79 _, err = AddEvent("sample name", "sample desc", "gno.land", "gnome land", "2009-02-13T23:31:31+06:00", "2009-02-13T23:33:31+02:00")
80 uassert.ErrorIs(t, err, ErrStartEndTimezonemMismatch)
81
82 tooLongDesc := `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean ma`
83 _, err = AddEvent("sample name", tooLongDesc, "gno.land", "gnome land", "2009-02-13T23:31:31Z", "2009-02-13T23:33:31Z")
84 uassert.ErrorContains(t, err, ErrDescriptionTooLong.Error())
85}
86
87func TestDeleteEvent(t *testing.T) {
88 std.TestSetOrigCaller(su)
89 std.TestSetRealm(suRealm)
90
91 e1Start := parsedTimeNow.Add(time.Hour * 24 * 5)
92 e1End := e1Start.Add(time.Hour * 4)
93
94 id, _ := AddEvent("ToDelete", "description", "gno.land", "gnome land", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))
95
96 got := renderHome(false)
97
98 if !strings.Contains(got, "ToDelete") {
99 t.Fatalf("Expected to find ToDelete event in render")
100 }
101
102 DeleteEvent(id)
103 got = renderHome(false)
104
105 if strings.Contains(got, "ToDelete") {
106 t.Fatalf("Did not expect to find ToDelete event in render")
107 }
108}
109
110func TestEditEvent(t *testing.T) {
111 std.TestSetOrigCaller(su)
112 std.TestSetRealm(suRealm)
113
114 e1Start := parsedTimeNow.Add(time.Hour * 24 * 5)
115 e1End := e1Start.Add(time.Hour * 4)
116 loc := "gnome land"
117
118 id, _ := AddEvent("ToDelete", "description", "gno.land", loc, e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))
119
120 newName := "New Name"
121 newDesc := "Normal description"
122 newLink := "new Link"
123 newST := e1Start.Add(time.Hour)
124 newET := newST.Add(time.Hour)
125
126 EditEvent(id, newName, newDesc, newLink, "", newST.Format(time.RFC3339), newET.Format(time.RFC3339))
127 edited, _, _ := GetEventByID(id)
128
129 // Check updated values
130 uassert.Equal(t, edited.name, newName)
131 uassert.Equal(t, edited.description, newDesc)
132 uassert.Equal(t, edited.link, newLink)
133 uassert.True(t, edited.startTime.Equal(newST))
134 uassert.True(t, edited.endTime.Equal(newET))
135
136 // Check if the old values are the same
137 uassert.Equal(t, edited.location, loc)
138}
139
140func TestInvalidEdit(t *testing.T) {
141 std.TestSetOrigCaller(su)
142 std.TestSetRealm(suRealm)
143
144 uassert.PanicsWithMessage(t, ErrNoSuchID.Error(), func() {
145 EditEvent("123123", "", "", "", "", "", "")
146 })
147}
148
149func TestParseTimes(t *testing.T) {
150 // times not provided
151 // end time before start time
152 // timezone Missmatch
153
154 _, _, err := parseTimes("", "")
155 uassert.ErrorContains(t, err, ErrInvalidStartTime.Error())
156
157 _, _, err = parseTimes(now, "")
158 uassert.ErrorContains(t, err, ErrInvalidEndTime.Error())
159
160 _, _, err = parseTimes("2009-02-13T23:30:30Z", "2009-02-13T21:30:30Z")
161 uassert.ErrorContains(t, err, ErrEndBeforeStart.Error())
162
163 _, _, err = parseTimes("2009-02-10T23:30:30+02:00", "2009-02-13T21:30:33+05:00")
164 uassert.ErrorContains(t, err, ErrStartEndTimezonemMismatch.Error())
165}
166
167func TestRenderEventWidget(t *testing.T) {
168 std.TestSetOrigCaller(su)
169 std.TestSetRealm(suRealm)
170
171 // No events yet
172 events = nil
173 out, err := RenderEventWidget(1)
174 uassert.NoError(t, err)
175 uassert.Equal(t, out, "No events.")
176
177 // Too many events
178 out, err = RenderEventWidget(MaxWidgetSize + 1)
179 uassert.ErrorIs(t, err, ErrMaxWidgetSize)
180
181 // Too little events
182 out, err = RenderEventWidget(0)
183 uassert.ErrorIs(t, err, ErrMinWidgetSize)
184
185 // Ordering & if requested amt is larger than the num of events that exist
186 e1Start := parsedTimeNow.Add(time.Hour * 24 * 5)
187 e1End := e1Start.Add(time.Hour * 4)
188
189 e2Start := parsedTimeNow.Add(time.Hour * 24 * 10) // event 2 is after event 1
190 e2End := e2Start.Add(time.Hour * 4)
191
192 _, err = AddEvent("Event 1", "description", "gno.land", "loc", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))
193 urequire.NoError(t, err)
194
195 _, err = AddEvent("Event 2", "description", "gno.land", "loc", e2Start.Format(time.RFC3339), e2End.Format(time.RFC3339))
196 urequire.NoError(t, err)
197
198 out, err = RenderEventWidget(MaxWidgetSize)
199 urequire.NoError(t, err)
200
201 uniqueSequence := "- [" // sequence that is displayed once per each event as per the RenderEventWidget function
202 uassert.Equal(t, 2, strings.Count(out, uniqueSequence))
203
204 uassert.True(t, strings.Index(out, "Event 1") > strings.Index(out, "Event 2"))
205}
events_test.gno
6.40 Kb ยท 205 lines