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 "errors"
6 "net/http"
7 "net/url"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/home/dashboard"
13 "amadan.net/oficina/home/roster"
14)
15
16func TestTeamFeedStandsOnItsMinter(t *testing.T) {
17 f := newTeamFixture(t)
18 subject := teamSubject('f')
19 p := f.member(subject, "member")
20 team, _ := f.calendarsOf(p)
21 f.event(p, subject, team, "Feed title", "2026-11-02T10:00", "2026-11-02T11:00")
22 loc := f.post("/calendars/"+itoa(team)+"/feeds", "").Header().Get("Location")
23 u, _ := url.Parse(loc)
24 token := u.Query().Get("token")
25 if token == "" {
26 t.Fatalf("no token in %q", loc)
27 }
28 feed := func() (int, string, http.Header) {
29 w := f.do("GET", "/feed/"+token+".ics", "")
30 return w.Code, w.Body.String(), w.Header()
31 }
32 if code, body, _ := feed(); code != http.StatusOK || !strings.Contains(body, "Feed title") {
33 t.Fatalf("member's feed = %d", code)
34 }
35 f.expired()
36 if code, body, h := feed(); code != http.StatusServiceUnavailable || h.Get("Retry-After") == "" || !strings.Contains(h.Get("Cache-Control"), "no-store") || strings.Contains(body, "VCALENDAR") {
37 t.Fatalf("feed under Unknown = %d %q %q, want a retryable no-store 503 and no calendar", code, h.Get("Retry-After"), h.Get("Cache-Control"))
38 }
39 f.dir.Set(subject, "")
40 f.refreshed()
41 if code, body, _ := feed(); code != http.StatusNotFound || strings.Contains(body, "Feed title") {
42 t.Fatalf("a removed minter's feed = %d", code)
43 }
44 f.dir.Set(subject, "member")
45 f.refreshed()
46 if code, body, _ := feed(); code != http.StatusOK || !strings.Contains(body, "Feed title") {
47 t.Fatalf("a re-added minter's feed = %d", code)
48 }
49 // Never a short calendar: whenever the lease runs out mid-read, the
50 // answer is the whole calendar or a 503.
51 sweep(t, func() (*teamFixture, func() string) {
52 f := newTeamFixture(t)
53 p := f.member(subject, "member")
54 team, _ := f.calendarsOf(p)
55 f.event(p, subject, team, "Feed title", "2026-11-02T10:00", "2026-11-02T11:00")
56 u, _ := url.Parse(f.post("/calendars/"+itoa(team)+"/feeds", "").Header().Get("Location"))
57 token := u.Query().Get("token")
58 return f, func() string {
59 w := f.do("GET", "/feed/"+token+".ics", "")
60 switch {
61 case w.Code == http.StatusOK && strings.Contains(w.Body.String(), "Feed title"):
62 return "whole"
63 case w.Code == http.StatusServiceUnavailable:
64 return "retry"
65 }
66 t.Fatalf("feed during a lapse = %d, with the event %v", w.Code, strings.Contains(w.Body.String(), "Feed title"))
67 return ""
68 }
69 }, "whole", "retry")
70}
71
72func TestTeamDashboardFeedFollowsTheRoster(t *testing.T) {
73 f := newTeamFixture(t)
74 subject := teamSubject('d')
75 p := f.member(subject, "member")
76 team, _ := f.calendarsOf(p)
77 start := time.Now().UTC().Add(26 * time.Hour)
78 f.event(p, subject, team, "Dashboard item", start.Format("2006-01-02T15:04"), start.Add(time.Hour).Format("2006-01-02T15:04"))
79 read := func() (dashboard.Response, error) {
80 return f.app.dashboardEvents(context.Background(), p.ID, subject, time.Now())
81 }
82 if got, err := read(); err != nil || len(got.Items) != 1 || got.Items[0].Title != "Dashboard item" {
83 t.Fatalf("member's dashboard = %+v %v", got, err)
84 }
85 f.expired()
86 if _, err := read(); !errors.Is(err, dashboard.ErrUnavailable) {
87 t.Fatalf("dashboard under Unknown = %v, want unavailable (never short)", err)
88 }
89 f.dir.Set(subject, "")
90 f.refreshed()
91 if got, err := read(); err != nil || len(got.Items) != 0 {
92 t.Fatalf("removed member's dashboard = %+v %v, want empty", got, err)
93 }
94 sweep(t, func() (*teamFixture, func() string) {
95 f := newTeamFixture(t)
96 p := f.member(subject, "member")
97 team, _ := f.calendarsOf(p)
98 f.event(p, subject, team, "Dashboard item", start.Format("2006-01-02T15:04"), start.Add(time.Hour).Format("2006-01-02T15:04"))
99 return f, func() string {
100 got, err := f.app.dashboardEvents(context.Background(), p.ID, subject, time.Now())
101 switch {
102 case err == nil && len(got.Items) == 1:
103 return "whole"
104 case errors.Is(err, dashboard.ErrUnavailable):
105 return "retry"
106 }
107 t.Fatalf("dashboard during a lapse = %+v %v", got, err)
108 return ""
109 }
110 }, "whole", "retry")
111}
112
113func TestTeamListsDuringALapseAreWholeOrSayPartial(t *testing.T) {
114 subject := teamSubject('s')
115 world := func() *teamFixture {
116 f := newTeamFixture(t)
117 p := f.member(subject, "member")
118 team, _ := f.calendarsOf(p)
119 f.event(p, subject, team, "Swept", "2026-11-02T10:00", "2026-11-02T11:00")
120 return f
121 }
122 sweep(t, func() (*teamFixture, func() string) {
123 f := world()
124 return f, func() string {
125 w := f.get("/day/2026-11-02")
126 body := w.Body.String()
127 has, partial := strings.Contains(body, "Swept"), strings.Contains(body, "data-partial")
128 switch {
129 case w.Code == http.StatusOK && has && !partial:
130 return "whole"
131 case w.Code == http.StatusOK && partial:
132 return "partial"
133 }
134 t.Fatalf("day during a lapse = %d, event %v, partial %v: short without a notice", w.Code, has, partial)
135 return ""
136 }
137 }, "whole", "partial")
138 sweep(t, func() (*teamFixture, func() string) {
139 f := world()
140 return f, func() string {
141 w := f.get("/api/native/events?from=2026-11-01&to=2026-11-08")
142 switch {
143 case w.Code == http.StatusOK && strings.Contains(w.Body.String(), "Swept"):
144 return "whole"
145 case w.Code == http.StatusServiceUnavailable:
146 return "retry"
147 }
148 t.Fatalf("native list during a lapse = %d, short", w.Code)
149 return ""
150 }
151 }, "whole", "retry")
152 sweep(t, func() (*teamFixture, func() string) {
153 f := world()
154 team, _ := f.calendarsOf(f.personFor(subject))
155 return f, func() string {
156 w := f.get("/calendars/" + itoa(team) + ".ics")
157 switch {
158 case w.Code == http.StatusOK && strings.Contains(w.Body.String(), "Swept"):
159 return "whole"
160 case w.Code == http.StatusServiceUnavailable:
161 return "retry"
162 }
163 t.Fatalf("export during a lapse = %d, short", w.Code)
164 return ""
165 }
166 }, "whole", "retry")
167 // An item page is the event or a retry: never "not found" because
168 // Home was slow, never our 500.
169 sweep(t, func() (*teamFixture, func() string) {
170 f := world()
171 var e Event
172 f.app.db.G.First(&e)
173 return f, func() string {
174 w := f.get("/events/" + itoa(e.ID))
175 switch {
176 case w.Code == http.StatusOK && strings.Contains(w.Body.String(), "Swept"):
177 return "whole"
178 case w.Code == http.StatusServiceUnavailable:
179 return "retry"
180 }
181 t.Fatalf("event page during a lapse = %d", w.Code)
182 return ""
183 }
184 }, "whole", "retry")
185 // So is the native editor's save: saved, or a retry.
186 sweep(t, func() (*teamFixture, func() string) {
187 f := world()
188 var e Event
189 f.app.db.G.First(&e)
190 return f, func() string {
191 w := f.post("/api/native/events/"+itoa(e.ID), `{"title":"Swept again","start_local":"2026-11-02T10:00","end_local":"2026-11-02T11:00","tz":"UTC"}`)
192 switch w.Code {
193 case http.StatusOK:
194 return "whole"
195 case http.StatusServiceUnavailable:
196 return "retry"
197 }
198 t.Fatalf("native save during a lapse = %d", w.Code)
199 return ""
200 }
201 }, "whole", "retry")
202}
203
204// sweep runs read at the lease's edge — directory down, the clock k ms
205// short of the lease, 1 ms passing at every reading — for k from 1 to 40,
206// each on a fresh fixture from build, so the lease runs out at every point
207// inside one request; it fails unless every outcome in want was seen.
208func sweep(t *testing.T, build func() (*teamFixture, func() string), want ...string) {
209 t.Helper()
210 seen := map[string]bool{}
211 for k := 1; k <= 40; k++ {
212 f, read := build()
213 f.dir.SetDown(true)
214 f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond)
215 f.clock.SetTick(time.Millisecond)
216 seen[read()] = true
217 }
218 for _, w := range want {
219 if !seen[w] {
220 t.Fatalf("the sweep never saw %q (saw %v)", w, seen)
221 }
222 }
223}
224