oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package calendartest
4
5import (
6 "context"
7 "fmt"
8 "io"
9 "log/slog"
10 "net/http"
11 "os"
12 "path/filepath"
13 "regexp"
14 "strings"
15 "testing"
16 "time"
17
18 "amadan.net/rastrillo/fiesta/check/rhythm"
19 "amadan.net/rastrillo/rastrillo"
20 "amadan.net/rastrillo/rastrillo/harness"
21 "github.com/chromedp/chromedp"
22
23 calendar "amadan.net/oficina/calendar/internal/calendar"
24)
25
26// TestEveryScreenKeepsTheRhythm: each of Calendar's own screens, read by
27// Fiesta's rhythm audit (Paul, 2026-11-22: "Can we somehow put
28// something in place to avoid weird spacing issues like this in
29// future?"), served through the production stack. Every gap between
30// two of a page's blocks is a step of the spacing scale, no text rests
31// on a control, and every heading sits closer to its own content than
32// to what comes before it, at a desktop width and a phone's.
33//
34// The audit reads around the calendar grids themselves (the month's
35// table, the day's and week's columns of hours), whose cells are
36// drawn to the clock and the calendar, not to the page's rhythm.
37//
38// On the tree before <main> was Fiesta's stack it fails, with 15
39// findings, all on the stranger's booking pages: the zone form 14px
40// under the page's description, and gaps the browser's own paragraph
41// and list margins set. Help → Open source is estilo's credits page,
42// left to estilo.
43func TestEveryScreenKeepsTheRhythm(t *testing.T) {
44 var app *calendar.App
45 rig := harness.New(t, func(origin string) http.Handler {
46 a, err := calendar.New(calendar.Config{
47 DBPath: filepath.Join(t.TempDir(), "calendar.db"),
48 Env: "development",
49 Origin: origin,
50 Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
51 })
52 if err != nil {
53 t.Fatalf("calendar.New: %v", err)
54 }
55 t.Cleanup(func() { a.Close() })
56 app = a
57 h, closeFn, err := rastrillo.Handler(a.ServeOptions(rastrillo.Options{Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}))
58 if err != nil {
59 t.Fatalf("rastrillo.Handler: %v", err)
60 }
61 if closeFn != nil {
62 t.Cleanup(func() { _ = closeFn() })
63 }
64 return h
65 })
66 session := signInOverHTTP(t, app, rig.Origin, "host@example.com")
67
68 // A published booking page, so the stranger's side has a page to
69 // draw and the host's list has a row.
70 ctx := context.Background()
71 st := app.StoreForTest()
72 host := app.PersonByAddressForTest(t, "host@example.com")
73 if err := st.SaveHandle(ctx, host.ID, "paul"); err != nil {
74 t.Fatal(err)
75 }
76 ws, _ := calendar.ParseAvailability("every day 9-5")
77 page, err := st.CreateBookingPage(ctx, host.ID, calendar.BookingPageInput{
78 Title: "Quick chat", Description: "Half an hour.", Availability: "every day 9-5",
79 DurationMin: 30, HorizonDays: 30}, ws)
80 if err != nil {
81 t.Fatal(err)
82 }
83 if err := st.PublishBookingPage(ctx, host.ID, page.ID, true); err != nil {
84 t.Fatal(err)
85 }
86
87 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
88 t.Helper()
89 c, cancel := context.WithTimeout(rig.Context(), 30*time.Second)
90 defer cancel()
91 if err := chromedp.Do(c, actions...); err != nil {
92 t.Fatalf("%s: %v", what, err)
93 }
94 }
95 // Each screen is read at a desktop width and again at a phone's.
96 // With a path, it is loaded first, once, and must still be at that
97 // path when read: a lost session lands on the sign-in door, whose
98 // rhythm is not this page's. A path written "~prefix" is the page
99 // the last step landed on, which must be under prefix. ready is a
100 // selector only that screen draws, its main h1 must read heading
101 // (an error page at the same address has its own), and main must
102 // hold at least blocks elements, so a page that drew nothing cannot
103 // pass by having nothing to measure.
104 grids := []string{"[cal-month]", "[cal-grid]", "[cal-week-scroll]"}
105 audit := func(name, path, ready string, blocks int, heading string) {
106 t.Helper()
107 landed := strings.HasPrefix(path, "~")
108 for i, width := range []int64{1280, 390} {
109 var found []rhythm.Finding
110 var at, h1 string
111 var drawn int
112 actions := []chromedp.Action[chromedp.Void]{deviceMetrics(width, 900)}
113 if i == 0 && !landed {
114 actions = append(actions, chromedp.Navigate(rig.Origin+path))
115 }
116 actions = append(actions,
117 chromedp.WaitVisible(chromedp.CSS(ready)),
118 evalInto(&at, `location.pathname + location.search`),
119 evalInto(&h1, `(document.querySelector('main h1') || {}).textContent || ''`),
120 evalInto(&drawn, `document.querySelectorAll('main *').length`),
121 evalInto(&found, rhythm.Expression("main", rhythm.Options{Skip: grids})))
122 run(name, actions...)
123 if landed && !strings.HasPrefix(at, path[1:]) || !landed && at != path {
124 t.Fatalf("%s: the browser is at %s, not %s", name, at, strings.TrimPrefix(path, "~"))
125 }
126 if strings.TrimSpace(h1) != heading {
127 t.Fatalf("%s: the page's heading is %q, not %q", name, h1, heading)
128 }
129 if drawn < blocks {
130 t.Fatalf("%s at %dpx: main holds %d elements, want at least %d", name, width, drawn, blocks)
131 }
132 if len(found) > 0 {
133 t.Errorf("%s at %dpx: %s", name, width, rhythm.Report(found))
134 }
135 }
136 }
137
138 audit("the sign-in door", "/signin", `input[name="address"]`, 3, "Sign in to Calendar")
139
140 run("sign in", setCookie(rig.Origin, session))
141
142 var at string
143 run("create an event",
144 chromedp.Navigate(rig.Origin+"/events/new?start=2026-09-02T09:30&end=2026-09-02T10:00"),
145 chromedp.WaitVisible(chromedp.CSS(`input[name="title"]`)),
146 chromedp.SendKeys(chromedp.CSS(`input[name="title"]`), "Standup"),
147 chromedp.Click(chromedp.CSS(`form[action="/events"] button[type="submit"]`)),
148 chromedp.WaitVisible(chromedp.CSS(`form[action$="/reminders"]`)),
149 harness.Into(&at, chromedp.Location()),
150 )
151 event := regexp.MustCompile(`/events/\d+`).FindString(at)
152 if event == "" {
153 t.Fatalf("creating an event landed on %s", at)
154 }
155 // A repeating one, whose page and delete offer the series.
156 personal, err := st.DefaultCalendarID(ctx, host.ID)
157 if err != nil {
158 t.Fatal(err)
159 }
160 weekly, err := st.CreateEvent(ctx, host.ID, calendar.EventInput{CalendarID: personal, Title: "Weekly review",
161 StartLocal: "2026-09-03T15:00", EndLocal: "2026-09-03T16:00", TZ: "UTC", RRule: "FREQ=WEEKLY"})
162 if err != nil {
163 t.Fatal(err)
164 }
165 series := fmt.Sprintf("/events/%d", weekly.ID)
166
167 for _, view := range []struct{ path, ready, heading string }{
168 {"/day/2026-09-02", `main [cal-grid]`, "Wednesday 2 September"},
169 {"/week/2026-09-02", `main [cal-week-scroll]`, "Week of 31 August 2026"},
170 {"/month/2026-09", `main [cal-month]`, "September 2026"},
171 {"/agenda?from=2026-09-02", `main[cal-view] [cal-day-chips]`, "Agenda"},
172 } {
173 audit(view.path, view.path, view.ready, 40, view.heading)
174 }
175 audit("a new event", "/events/new", `input[name="title"]`, 40, "New event")
176 audit("editing an event", event+"/edit", `input[name="title"]`, 40, "Edit event")
177 audit("an event", event, `form[action$="/reminders"]`, 20, "Standup")
178 audit("deleting an event", event+"/delete", `main form[action$="/delete"]`, 4, "Delete “Standup”?")
179 audit("a repeating event", series, `form[action$="/reminders"]`, 20, "Weekly review")
180 audit("deleting a repeating event", series+"/delete", `main form[action$="/delete"] fieldset`, 15, "Delete “Weekly review”?")
181 audit("settings", "/settings", `main form`, 100, "Settings")
182 audit("reminders", "/reminders", `main [rst-empty] a[href="/settings"], main [cal-notices]`, 6, "Reminders")
183 audit("calendars", "/calendars", `main [cal-dot]`, 12, "Calendars")
184 audit("sharing a calendar", "/calendars/1/share", `form[cal-feed-new]`, 12, "Team")
185 run("make a subscription link",
186 chromedp.Navigate(rig.Origin+"/calendars/1/share"),
187 chromedp.WaitVisible(chromedp.CSS(`form[cal-feed-new] button`)),
188 chromedp.Click(chromedp.CSS(`form[cal-feed-new] button`)),
189 chromedp.WaitVisible(chromedp.CSS(`main [rst-list], main ul`)))
190 audit("sharing a calendar, with a link", "~/calendars/1/share?token=", `form[cal-feed-new]`, 20, "Team")
191 audit("import", "/import", `main form input[type=file]`, 20, "Import a calendar")
192 ics := filepath.Join(t.TempDir(), "one.ics")
193 if err := os.WriteFile(ics, []byte("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//test//EN\r\nBEGIN:VEVENT\r\nUID:one@test\r\nDTSTAMP:20260901T000000Z\r\nDTSTART:20260904T100000Z\r\nDTEND:20260904T110000Z\r\nSUMMARY:Imported\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"), 0o600); err != nil {
194 t.Fatal(err)
195 }
196 run("import a file",
197 chromedp.Navigate(rig.Origin+"/import"),
198 chromedp.WaitVisible(chromedp.CSS(`input[type=file]`)),
199 chromedp.SetUploadFiles(`input[type=file]`, []string{ics}),
200 chromedp.Click(chromedp.CSS(`form[action="/import"] button[type=submit]`)),
201 chromedp.WaitNotPresent(chromedp.CSS(`form[action="/import"] input[type=file]`)))
202 audit("import done", "~/import", `main [rst-page-header]`, 10, "Imported")
203 audit("booking pages", "/settings/booking", `main [cal-book-page]`, 20, "Booking pages")
204 audit("a new booking page", "/settings/booking/new", `main form`, 30, "New booking page")
205 audit("a page that is not there", "/events/999999", `main h1`, 4, "We can’t find that page")
206
207 // The stranger's side: the page, a day's slots, a slot's details,
208 // the hold, then the mailed link: confirm, manage, reschedule,
209 // cancel.
210 public := "/book/paul/" + page.Code + "?zone=UTC"
211 audit("the booking page", public, `[cal-book-days] a`, 100, "Quick chat")
212 var day string
213 run("a booking day", evalInto(&day, `document.querySelector('[cal-book-days] a').getAttribute('href')`))
214 audit("a booking day", day, `[cal-book-slot]`, 100, "Quick chat")
215 run("a slot", chromedp.Click(chromedp.CSS(`[cal-book-slot]`)), chromedp.WaitVisible(chromedp.CSS(`[cal-book-details] input[name="name"]`)))
216 audit("a slot's details", "~/book/paul/"+page.Code, `[cal-book-details] input[name="name"]`, 60, "Quick chat")
217 run("hold it",
218 chromedp.SendKeys(chromedp.CSS(`#name`), "Doc"),
219 chromedp.SendKeys(chromedp.CSS(`#address`), "doc@example.com"),
220 chromedp.Click(chromedp.CSS(`[cal-book-details] button[type=submit]`)),
221 chromedp.WaitNotPresent(chromedp.CSS(`[cal-book-details]`)))
222 audit("the hold, sent", "~/book/paul/"+page.Code, `main [rst-page-header-sub]`, 8, "Check your mail")
223 mail, err := app.LastMailForTest()
224 if err != nil {
225 t.Fatal(err)
226 }
227 manage := regexp.MustCompile(`/book/m/[0-9a-f]{64}`).FindString(mail)
228 if manage == "" {
229 t.Fatalf("no manage link in the hold mail")
230 }
231 audit("the hold, to confirm", manage, `form[action$="/confirm"]`, 10, "Confirm your booking")
232 run("confirm it",
233 chromedp.Click(chromedp.CSS(`form[action$="/confirm"] button`)),
234 chromedp.WaitVisible(chromedp.CSS(`[cal-book-manage-actions]`)))
235 audit("a booking", "~"+manage+"/confirm", `[cal-book-manage-actions]`, 12, "Quick chat")
236 audit("rescheduling a booking", manage+"/reschedule", `[cal-book-days] a`, 100, "Quick chat")
237 run("cancel it",
238 chromedp.Navigate(rig.Origin+manage),
239 chromedp.WaitVisible(chromedp.CSS(`form[action$="/cancel"] button`)),
240 chromedp.Click(chromedp.CSS(`form[action$="/cancel"] button`)),
241 chromedp.WaitNotPresent(chromedp.CSS(`form[action$="/cancel"]`)))
242 audit("a cancelled booking", "~"+manage+"/cancel", `main p`, 6, "Quick chat")
243}
244