oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "strings"
9 "testing"
10 "time"
11)
12
13// In-app reminders with team mode off (spec §7 "In-app reminders"): email
14// stays exactly what it was for anybody who never chose otherwise, and a
15// person with an address may choose in-app, or both.
16
17type plainWorld struct {
18 t *testing.T
19 app *App
20 session *http.Cookie
21 box *teamMailbox
22 person Person
23 calendar int64 // their default (personal) calendar
24}
25
26func newPlainWorld(t *testing.T) *plainWorld {
27 t.Helper()
28 app := NewForTest(t, t.TempDir())
29 w := &plainWorld{t: t, app: app, session: app.SignInForTest(t, "paul@example.test")}
30 w.box = &teamMailbox{}
31 app.h.mailer = newBoundedSender(w.box)
32 w.do("GET", "/calendars", nil) // the first page makes their calendars
33 w.person = app.PersonByAddressForTest(t, "paul@example.test")
34 id, err := app.store.DefaultCalendarID(context.Background(), w.person.ID)
35 if err != nil {
36 t.Fatal(err)
37 }
38 w.calendar = id
39 return w
40}
41
42func (w *plainWorld) do(method, path string, form url.Values) *httptest.ResponseRecorder {
43 rec := httptest.NewRecorder()
44 w.app.mux.ServeHTTP(rec, RequestForTest(w.session, method, path, form))
45 return rec
46}
47
48// event creates an event through the form a few minutes from now, so the
49// person's default reminder is made and due, and returns it.
50func (w *plainWorld) event(title string) Event {
51 w.t.Helper()
52 start, end := soon(5)
53 if rec := w.do("POST", "/events", url.Values{"calendar_id": {itoa(w.calendar)}, "title": {title}, "start": {start}, "end": {end}, "tz": {"UTC"}, "kind": {"meeting"}, "status": {"confirmed"}, "repeat": {"none"}}); rec.Code != http.StatusSeeOther {
54 w.t.Fatalf("create %q = %d %s", title, rec.Code, rec.Body.String())
55 }
56 var e Event
57 w.app.db.G.Last(&e)
58 return e
59}
60
61func (w *plainWorld) notices() int64 {
62 var n int64
63 w.app.db.G.Model(&ReminderNotice{}).Count(&n)
64 return n
65}
66
67func TestEmailRemindersAreUnchangedUntilSomebodyOptsIn(t *testing.T) {
68 w := newPlainWorld(t)
69 e := w.event("Standup by email")
70 var rule Reminder
71 w.app.db.G.Where("event_id = ?", e.ID).First(&rule)
72 if rule.Channel != ChannelEmail {
73 t.Fatalf("the default reminder's channel = %q, want email", rule.Channel)
74 }
75 if sent, _, err := w.app.h.drain(context.Background(), time.Now()); err != nil || sent != 1 {
76 t.Fatalf("drain = %d %v", sent, err)
77 }
78 page := w.do("GET", "/events/"+itoa(e.ID), nil).Body.String()
79 if strings.Contains(page, `href="/reminders"`) || strings.Contains(page, `name="channel"`) || !strings.Contains(page, "Email 10 minutes before") {
80 t.Fatal("a person who never chose in-app sees in-app reminders on the event page")
81 }
82 if len(w.box.to("paul@example.test")) != 1 || w.notices() != 0 {
83 t.Fatalf("an email reminder = %v mailed, %d notices", w.box.sent, w.notices())
84 }
85 if rec := w.do("GET", "/reminders", nil); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Open Settings") {
86 t.Fatalf("the list for somebody on email = %d", rec.Code)
87 }
88}
89
90func TestAPersonWithAnAddressChoosesInAppOrBoth(t *testing.T) {
91 w := newPlainWorld(t)
92 settings := url.Values{"zone": {"UTC"}, "week_start": {"1"}, "default_reminder_min": {"10"}, "default_all_day_min": {"0"}, "handle": {""}}
93 settings.Set("reminder_channel", "both")
94 if rec := w.do("POST", "/settings", settings); rec.Code != http.StatusSeeOther {
95 t.Fatalf("settings = %d %s", rec.Code, rec.Body.String())
96 }
97 e := w.event("Both ways")
98 var rule Reminder
99 w.app.db.G.Where("event_id = ?", e.ID).First(&rule)
100 if rule.Channel != ChannelBoth {
101 t.Fatalf("the default reminder's channel = %q, want both", rule.Channel)
102 }
103 // Both: mailed AND marked, in one pass.
104 if sent, _, err := w.app.h.drain(context.Background(), time.Now()); err != nil || sent != 1 {
105 t.Fatalf("drain = %d %v", sent, err)
106 }
107 page := w.do("GET", "/events/"+itoa(e.ID), nil).Body.String()
108 if !strings.Contains(page, `name="channel"`) || !strings.Contains(page, "Email and in Calendar 10 minutes before") || !strings.Contains(page, "Reminders (1)") {
109 t.Fatal("the event page does not offer the channel, name it, or count it in the chrome")
110 }
111 if len(w.box.to("paul@example.test")) != 1 || w.notices() != 1 {
112 t.Fatalf("both = %v mailed, %d notices", w.box.sent, w.notices())
113 }
114 // One rule on its own channel, chosen on the event page.
115 if rec := w.do("POST", "/events/"+itoa(e.ID)+"/reminders", url.Values{"offset_min": {"5"}, "channel": {"inapp"}}); rec.Code != http.StatusSeeOther {
116 t.Fatalf("add = %d", rec.Code)
117 }
118 var channels []string
119 w.app.db.G.Model(&Reminder{}).Where("event_id = ?", e.ID).Order("id").Pluck("channel", &channels)
120 if len(channels) != 2 || channels[1] != ChannelInApp {
121 t.Fatalf("channels = %v", channels)
122 }
123 // Settings without the field (an older form) keeps the choice.
124 settings.Del("reminder_channel")
125 w.do("POST", "/settings", settings)
126 if w.app.db.G.First(&w.person, w.person.ID); w.person.ReminderChannel == nil || *w.person.ReminderChannel != ChannelBoth {
127 t.Fatal("a settings save without the field changed the channel")
128 }
129}
130
131// Several rules on ONE occurrence — two people's reminders on a shared
132// meeting, or one person's two — are each owed in the same pass. The
133// drain's claimed-twice guard used to key on the occurrence alone, so the
134// second stopped the pass with an error.
135func TestTwoRulesOnOneOccurrenceAreBothDeliveredInOnePass(t *testing.T) {
136 w := newPlainWorld(t)
137 e := w.event("Twice warned")
138 if err := w.app.store.AddReminder(context.Background(), w.person.ID, e.ID, 30); err != nil {
139 t.Fatal(err)
140 }
141 if sent, _, err := w.app.h.drain(context.Background(), time.Now()); err != nil || sent != 2 {
142 t.Fatalf("drain = %d %v, want both rules delivered", sent, err)
143 }
144}
145
146// ONE rule whose next occurrence is already due inside the same pass (a
147// daily series, drained a day late) is owed both: the guard is keyed by
148// rule AND occurrence, and a key by rule alone would stop the pass here
149// (Fable #36 N1).
150func TestOneRuleTwoOccurrencesAreBothDeliveredInOnePass(t *testing.T) {
151 w := newPlainWorld(t)
152 w.app.db.G.Model(&Person{}).Where("id = ?", w.person.ID).Update("reminder_channel", ChannelInApp)
153 start, end := soon(5)
154 if rec := w.do("POST", "/events", url.Values{"calendar_id": {itoa(w.calendar)}, "title": {"Daily, late"}, "start": {start}, "end": {end}, "tz": {"UTC"}, "kind": {"meeting"}, "status": {"confirmed"}, "repeat": {"daily"}}); rec.Code != http.StatusSeeOther {
155 t.Fatalf("create = %d", rec.Code)
156 }
157 if sent, _, err := w.app.h.drain(context.Background(), time.Now().Add(24*time.Hour)); err != nil || sent != 2 {
158 t.Fatalf("drain a day late = %d %v, want both occurrences delivered", sent, err)
159 }
160}
161
162// The mark is conditional on the claim, as every completion is: a
163// reschedule that overtook the claim announces nothing.
164func TestAnOvertakenClaimMarksNothing(t *testing.T) {
165 w := newPlainWorld(t)
166 choose := ChannelInApp
167 w.app.db.G.Model(&Person{}).Where("id = ?", w.person.ID).Update("reminder_channel", choose)
168 e := w.event("Overtaken")
169 due, ok, err := w.app.store.ClaimDueReminder(context.Background(), time.Now().Unix())
170 if err != nil || !ok || due.EventID != e.ID || due.Channel != ChannelInApp {
171 t.Fatalf("claim = %+v %v %v", due, ok, err)
172 }
173 if err := w.app.store.RescheduleReminder(context.Background(), due.Queue.ReminderID); err != nil {
174 t.Fatal(err)
175 }
176 if err := w.app.store.MarkReminderNotice(context.Background(), due, time.Now().Unix()); err != nil || w.notices() != 0 {
177 t.Fatalf("an overtaken claim marked %d notices (%v)", w.notices(), err)
178 }
179}
180
181// A notice links to its occurrence, and goes when its event does.
182func TestANoticeLinksItsOccurrenceAndGoesWithItsEvent(t *testing.T) {
183 w := newPlainWorld(t)
184 w.app.db.G.Model(&Person{}).Where("id = ?", w.person.ID).Update("reminder_channel", ChannelInApp)
185 start, end := soon(5)
186 if rec := w.do("POST", "/events", url.Values{"calendar_id": {itoa(w.calendar)}, "title": {"Daily thing"}, "start": {start}, "end": {end}, "tz": {"UTC"}, "kind": {"meeting"}, "status": {"confirmed"}, "repeat": {"daily"}}); rec.Code != http.StatusSeeOther {
187 t.Fatalf("create = %d", rec.Code)
188 }
189 var e Event
190 w.app.db.G.Last(&e)
191 page := w.do("GET", "/reminders", nil).Body.String()
192 if !strings.Contains(page, `href="/events/`+itoa(e.ID)+"/"+compactLocal(e.StartLocal)+`"`) {
193 t.Fatalf("the notice does not link its occurrence:\n%s", page)
194 }
195 if rec := w.do("POST", "/events/"+itoa(e.ID)+"/delete", url.Values{}); rec.Code != http.StatusSeeOther {
196 t.Fatalf("delete = %d", rec.Code)
197 }
198 if page := w.do("GET", "/reminders", nil).Body.String(); strings.Contains(page, "Daily thing") || strings.Contains(page, "Reminders (") {
199 t.Fatal("a deleted event's notice is still shown or counted")
200 }
201}
202
203func TestNoticeWhenIsPastTenseAndAllDayIsADate(t *testing.T) {
204 now := time.Date(2026, 11, 2, 12, 0, 0, 0, time.UTC).Unix()
205 timed := NoticeView{StartUTC: now - 3600, Local: "2026-11-02T11:00"}
206 if got := noticeWhen(timed, "Europe/Dublin", now); got != "Started Monday 2 November at 11:00" {
207 t.Fatalf("a started occurrence = %q", got)
208 }
209 timed.StartUTC = now + 3600
210 if got := noticeWhen(timed, "UTC", now); got != "Starts Monday 2 November at 13:00" {
211 t.Fatalf("a coming occurrence = %q", got)
212 }
213 // An all-day occurrence is its date, wherever the reader is: its UTC
214 // instant west of Greenwich is the evening before.
215 allDay := NoticeView{AllDay: true, StartUTC: now + 86400, Local: "2026-11-03"}
216 if got := noticeWhen(allDay, "America/Los_Angeles", now); got != "Starts Tuesday 3 November" {
217 t.Fatalf("an all-day occurrence = %q", got)
218 }
219}
220
221// Home's pull sees a newer occurrence of the same rule as a new item: the
222// notice row is replaced, unread again, under a new ID.
223func TestTheSeamNamesEachOccurrenceAfresh(t *testing.T) {
224 w := newPlainWorld(t)
225 w.app.db.G.Model(&Person{}).Where("id = ?", w.person.ID).Update("reminder_channel", ChannelInApp)
226 start, end := soon(5)
227 if rec := w.do("POST", "/events", url.Values{"calendar_id": {itoa(w.calendar)}, "title": {"Every day"}, "start": {start}, "end": {end}, "tz": {"UTC"}, "kind": {"meeting"}, "status": {"confirmed"}, "repeat": {"daily"}}); rec.Code != http.StatusSeeOther {
228 t.Fatalf("create = %d", rec.Code)
229 }
230 read := func() NoticeSummary {
231 t.Helper()
232 sum, err := w.app.reminderNotices(context.Background(), w.person.ID, "", time.Now())
233 if err != nil || sum.Unread != 1 || len(sum.Items) != 1 {
234 t.Fatalf("the seam = %+v %v", sum, err)
235 }
236 return sum
237 }
238 if _, _, err := w.app.h.drain(context.Background(), time.Now()); err != nil {
239 t.Fatal(err)
240 }
241 today := read()
242 mustRedirect(t, "dismiss", w.do("POST", "/reminders/dismiss", url.Values{}))
243 if _, _, err := w.app.h.drain(context.Background(), time.Now().Add(24*time.Hour)); err != nil {
244 t.Fatal(err)
245 }
246 tomorrow := read()
247 if tomorrow.Items[0].ID == today.Items[0].ID || w.notices() != 1 {
248 t.Fatalf("the next occurrence = %q after %q (%d rows), want a new ID on the same row", tomorrow.Items[0].ID, today.Items[0].ID, w.notices())
249 }
250}
251