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
3// The iOS zoom-on-tap check, in a real Chromium through chromedp. Not
4// part of `make ci`, because it needs a Chromium (on PATH, via
5// RASTRILLO_CHROME, or in a Playwright cache):
6//
7// make browser
8//
9// `make vet` vets this package with the tag, so it cannot stop
10// compiling unnoticed.
11package calendartest
12
13import (
14 "context"
15 "fmt"
16 "io"
17 "log/slog"
18 "net/http"
19 "path/filepath"
20 "regexp"
21 "strings"
22 "testing"
23 "time"
24
25 "amadan.net/rastrillo/rastrillo/harness"
26 "github.com/chromedp/cdproto/emulation"
27 "github.com/chromedp/chromedp"
28
29 calendar "amadan.net/oficina/calendar/internal/calendar"
30)
31
32// textControls is every control a tap puts a caret in. A checkbox, a
33// radio, a file picker or a button raises no keyboard, so iOS never
34// zooms for them.
35const textControls = `input:not([type=checkbox]):not([type=radio]):not([type=hidden]):not([type=file]):not([type=submit]):not([type=button]):not([type=color]):not([type=range]), select, textarea`
36
37// TestNoZoomOnTap: iOS Safari zooms the whole page into any text control
38// whose type is under 16px, and rastrillo's base type is 14px. Its
39// tokens.css brings text controls up to 16px where the pointer is coarse
40// ("Touch: the two zooms nobody asked for"); an app on a tokens.css
41// without that block, or with a rule of its own that sets a control
42// smaller, zooms on the first tap of every field.
43//
44// Emulated as pointer: coarse, which is how the CSS decides. Every page
45// with a field is measured: the door, the views (and the quick-add box,
46// which only shows once its disclosure is opened), the event form with
47// its enhanced date fields, the event page's reminders, settings,
48// sharing, import, the booking-page form, and a stranger's booking
49// form, which is the one most often opened on a phone.
50func TestNoZoomOnTap(t *testing.T) {
51 var app *calendar.App
52 rig := harness.New(t, func(origin string) http.Handler {
53 a, err := calendar.New(calendar.Config{
54 DBPath: filepath.Join(t.TempDir(), "calendar.db"),
55 Env: "development",
56 Origin: origin,
57 Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
58 })
59 if err != nil {
60 t.Fatalf("calendar.New: %v", err)
61 }
62 t.Cleanup(func() { a.Close() })
63 app = a
64 return a.Handler()
65 })
66 session := signInOverHTTP(t, app, rig.Origin, "host@example.com")
67
68 // A published booking page, through the store, so the stranger's
69 // form has something to book.
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 // The zone named in the URL, so the page does not submit its own zone
87 // guess on load (booking.js setupZone): that self-navigation raced
88 // measure's reload, which chromedp v0.21 reports as an aborted load.
89 public := "/book/paul/" + page.Code + "?zone=UTC"
90
91 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
92 t.Helper()
93 ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
94 defer cancel()
95 if err := chromedp.Do(ctx, actions...); err != nil {
96 var at string
97 _ = chromedp.Do(rig.Context(), harness.Into(&at, chromedp.Location()))
98 t.Fatalf("%s: %v (the browser is at %s)", what, err, at)
99 }
100 }
101 // measure loads path on a touch screen and lists its small controls,
102 // after the optional actions (which open what only interaction shows).
103 measure := func(path, ready string, then ...chromedp.Action[chromedp.Void]) {
104 t.Helper()
105 var coarse bool
106 var small []string
107 actions := []chromedp.Action[chromedp.Void]{
108 chromedp.Navigate(rig.Origin + path),
109 chromedp.Reload(),
110 chromedp.WaitVisible(chromedp.CSS(ready)),
111 evalInto(&coarse, `matchMedia('(pointer: coarse)').matches`),
112 }
113 actions = append(actions, then...)
114 actions = append(actions, evalInto(&small, smallControls(textControls)))
115 run(path, actions...)
116 if !coarse {
117 t.Fatalf("%s: the touch-screen emulation did not apply; this would measure desktop styles", path)
118 }
119 if len(small) > 0 {
120 t.Errorf("%s: iOS zooms when these are tapped (under 16px on a touch screen): %s", path, strings.Join(small, "; "))
121 }
122 }
123
124 run("touch emulation",
125 // Touch emulation, then a reload per page so it re-evaluates its
126 // media queries. SetEmulatedMedia's "pointer" feature is not
127 // emulated by this Chromium; the coarse check in measure is what
128 // proves the emulation took.
129 command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}),
130 )
131
132 // Signed out first: the door is the field every visit starts on.
133 measure("/signin", `input[name="address"]`)
134
135 // The session the real magic-link flow minted, handed to the browser.
136 run("sign in", setCookie(rig.Origin, session))
137
138 // An event, made through the form as a person makes one.
139 var at string
140 run("create an event",
141 chromedp.Navigate(rig.Origin+"/events/new?start=2026-09-02T09:30&end=2026-09-02T10:00"),
142 chromedp.WaitVisible(chromedp.CSS(`input[name="title"]`)),
143 chromedp.SendKeys(chromedp.CSS(`input[name="title"]`), "Standup"),
144 chromedp.Click(chromedp.CSS(`form[action="/events"] button[type="submit"]`)),
145 chromedp.WaitVisible(chromedp.CSS(`form[action$="/reminders"]`)),
146 harness.Into(&at, chromedp.Location()),
147 )
148 event := regexp.MustCompile(`/events/\d+`).FindString(at)
149 if event == "" {
150 t.Fatalf("creating an event landed on %s", at)
151 }
152
153 // The quick-add box is a closed <details> until it is tapped.
154 openQuick := evalOnly(`document.querySelectorAll('details[cal-quick]').forEach(d => d.open = true)`)
155 for _, view := range []string{"/day/2026-09-02", "/week/2026-09-02", "/month/2026-09", "/agenda?from=2026-09-02"} {
156 measure(view, `main`, openQuick)
157 }
158 measure("/events/new", `input[name="title"]`)
159 measure(event+"/edit", `input[name="title"]`)
160 measure(event, `form[action$="/reminders"]`)
161 measure("/settings", `main form`)
162 measure("/calendars", `main`)
163 measure("/calendars/1/share", `main`)
164 measure("/import", `main form`)
165 measure("/settings/booking", `main`)
166 measure("/settings/booking/new", `main form`)
167
168 // The stranger's side: the zone picker, then a day, then a slot,
169 // which is where the name, address and note fields are.
170 measure(public, `select[name="zone"][data-chosen]`)
171 var day string
172 run("a booking day",
173 chromedp.Navigate(rig.Origin+public),
174 chromedp.WaitVisible(chromedp.CSS(`select[name="zone"]`)),
175 evalInto(&day, `(document.querySelector('a[href*="day="]') || {}).href || ''`),
176 )
177 if day == "" {
178 t.Fatal("the booking page offers no day to pick")
179 }
180 measure(strings.TrimPrefix(day, rig.Origin), `[cal-book-slot]`)
181 run("a booking slot",
182 chromedp.Click(chromedp.CSS(`[cal-book-slot]`)),
183 chromedp.WaitVisible(chromedp.CSS(`[cal-book-details] input[name="name"]`)),
184 )
185 var coarse bool
186 var small []string
187 run("the booker's details",
188 evalInto(&coarse, `matchMedia('(pointer: coarse)').matches`),
189 evalInto(&small, smallControls(textControls)),
190 )
191 if !coarse {
192 t.Fatal("the booker's details: the touch-screen emulation did not apply")
193 }
194 if len(small) > 0 {
195 t.Errorf("the booker's details: iOS zooms when these are tapped (under 16px on a touch screen): %s", strings.Join(small, "; "))
196 }
197}
198
199// smallControls is a script listing each visible control matching sel
200// whose computed font-size is under 16px, described well enough to find.
201func smallControls(sel string) string {
202 return fmt.Sprintf(`[...document.querySelectorAll(%q)]
203 .filter(e => e.getClientRects().length > 0)
204 .filter(e => parseFloat(getComputedStyle(e).fontSize) < 16)
205 .map(e => e.tagName.toLowerCase() + (e.name ? '[name=' + e.name + ']' : '') + (e.id ? '#' + e.id : '') + ' ' + getComputedStyle(e).fontSize)`, sel)
206}
207