//go:build browser package calendartest import ( "context" "fmt" "io" "log/slog" "net/http" "os" "path/filepath" "regexp" "strings" "testing" "time" "amadan.net/rastrillo/fiesta/check/rhythm" "amadan.net/rastrillo/rastrillo" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" calendar "amadan.net/oficina/calendar/internal/calendar" ) // TestEveryScreenKeepsTheRhythm: each of Calendar's own screens, read by // Fiesta's rhythm audit (Paul, 2026-11-22: "Can we somehow put // something in place to avoid weird spacing issues like this in // future?"), served through the production stack. Every gap between // two of a page's blocks is a step of the spacing scale, no text rests // on a control, and every heading sits closer to its own content than // to what comes before it, at a desktop width and a phone's. // // The audit reads around the calendar grids themselves (the month's // table, the day's and week's columns of hours), whose cells are // drawn to the clock and the calendar, not to the page's rhythm. // // On the tree before
was Fiesta's stack it fails, with 15 // findings, all on the stranger's booking pages: the zone form 14px // under the page's description, and gaps the browser's own paragraph // and list margins set. Help → Open source is estilo's credits page, // left to estilo. func TestEveryScreenKeepsTheRhythm(t *testing.T) { var app *calendar.App rig := harness.New(t, func(origin string) http.Handler { a, err := calendar.New(calendar.Config{ DBPath: filepath.Join(t.TempDir(), "calendar.db"), Env: "development", Origin: origin, Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), }) if err != nil { t.Fatalf("calendar.New: %v", err) } t.Cleanup(func() { a.Close() }) app = a h, closeFn, err := rastrillo.Handler(a.ServeOptions(rastrillo.Options{Logger: slog.New(slog.NewTextHandler(io.Discard, nil))})) if err != nil { t.Fatalf("rastrillo.Handler: %v", err) } if closeFn != nil { t.Cleanup(func() { _ = closeFn() }) } return h }) session := signInOverHTTP(t, app, rig.Origin, "host@example.com") // A published booking page, so the stranger's side has a page to // draw and the host's list has a row. ctx := context.Background() st := app.StoreForTest() host := app.PersonByAddressForTest(t, "host@example.com") if err := st.SaveHandle(ctx, host.ID, "paul"); err != nil { t.Fatal(err) } ws, _ := calendar.ParseAvailability("every day 9-5") page, err := st.CreateBookingPage(ctx, host.ID, calendar.BookingPageInput{ Title: "Quick chat", Description: "Half an hour.", Availability: "every day 9-5", DurationMin: 30, HorizonDays: 30}, ws) if err != nil { t.Fatal(err) } if err := st.PublishBookingPage(ctx, host.ID, page.ID, true); err != nil { t.Fatal(err) } run := func(what string, actions ...chromedp.Action[chromedp.Void]) { t.Helper() c, cancel := context.WithTimeout(rig.Context(), 30*time.Second) defer cancel() if err := chromedp.Do(c, actions...); err != nil { t.Fatalf("%s: %v", what, err) } } // Each screen is read at a desktop width and again at a phone's. // With a path, it is loaded first, once, and must still be at that // path when read: a lost session lands on the sign-in door, whose // rhythm is not this page's. A path written "~prefix" is the page // the last step landed on, which must be under prefix. ready is a // selector only that screen draws, its main h1 must read heading // (an error page at the same address has its own), and main must // hold at least blocks elements, so a page that drew nothing cannot // pass by having nothing to measure. grids := []string{"[cal-month]", "[cal-grid]", "[cal-week-scroll]"} audit := func(name, path, ready string, blocks int, heading string) { t.Helper() landed := strings.HasPrefix(path, "~") for i, width := range []int64{1280, 390} { var found []rhythm.Finding var at, h1 string var drawn int actions := []chromedp.Action[chromedp.Void]{deviceMetrics(width, 900)} if i == 0 && !landed { actions = append(actions, chromedp.Navigate(rig.Origin+path)) } actions = append(actions, chromedp.WaitVisible(chromedp.CSS(ready)), evalInto(&at, `location.pathname + location.search`), evalInto(&h1, `(document.querySelector('main h1') || {}).textContent || ''`), evalInto(&drawn, `document.querySelectorAll('main *').length`), evalInto(&found, rhythm.Expression("main", rhythm.Options{Skip: grids}))) run(name, actions...) if landed && !strings.HasPrefix(at, path[1:]) || !landed && at != path { t.Fatalf("%s: the browser is at %s, not %s", name, at, strings.TrimPrefix(path, "~")) } if strings.TrimSpace(h1) != heading { t.Fatalf("%s: the page's heading is %q, not %q", name, h1, heading) } if drawn < blocks { t.Fatalf("%s at %dpx: main holds %d elements, want at least %d", name, width, drawn, blocks) } if len(found) > 0 { t.Errorf("%s at %dpx: %s", name, width, rhythm.Report(found)) } } } audit("the sign-in door", "/signin", `input[name="address"]`, 3, "Sign in to Calendar") run("sign in", setCookie(rig.Origin, session)) var at string run("create an event", chromedp.Navigate(rig.Origin+"/events/new?start=2026-09-02T09:30&end=2026-09-02T10:00"), chromedp.WaitVisible(chromedp.CSS(`input[name="title"]`)), chromedp.SendKeys(chromedp.CSS(`input[name="title"]`), "Standup"), chromedp.Click(chromedp.CSS(`form[action="/events"] button[type="submit"]`)), chromedp.WaitVisible(chromedp.CSS(`form[action$="/reminders"]`)), harness.Into(&at, chromedp.Location()), ) event := regexp.MustCompile(`/events/\d+`).FindString(at) if event == "" { t.Fatalf("creating an event landed on %s", at) } // A repeating one, whose page and delete offer the series. personal, err := st.DefaultCalendarID(ctx, host.ID) if err != nil { t.Fatal(err) } weekly, err := st.CreateEvent(ctx, host.ID, calendar.EventInput{CalendarID: personal, Title: "Weekly review", StartLocal: "2026-09-03T15:00", EndLocal: "2026-09-03T16:00", TZ: "UTC", RRule: "FREQ=WEEKLY"}) if err != nil { t.Fatal(err) } series := fmt.Sprintf("/events/%d", weekly.ID) for _, view := range []struct{ path, ready, heading string }{ {"/day/2026-09-02", `main [cal-grid]`, "Wednesday 2 September"}, {"/week/2026-09-02", `main [cal-week-scroll]`, "Week of 31 August 2026"}, {"/month/2026-09", `main [cal-month]`, "September 2026"}, {"/agenda?from=2026-09-02", `main[cal-view] [cal-day-chips]`, "Agenda"}, } { audit(view.path, view.path, view.ready, 40, view.heading) } audit("a new event", "/events/new", `input[name="title"]`, 40, "New event") audit("editing an event", event+"/edit", `input[name="title"]`, 40, "Edit event") audit("an event", event, `form[action$="/reminders"]`, 20, "Standup") audit("deleting an event", event+"/delete", `main form[action$="/delete"]`, 4, "Delete “Standup”?") audit("a repeating event", series, `form[action$="/reminders"]`, 20, "Weekly review") audit("deleting a repeating event", series+"/delete", `main form[action$="/delete"] fieldset`, 15, "Delete “Weekly review”?") audit("settings", "/settings", `main form`, 100, "Settings") audit("reminders", "/reminders", `main [rst-empty] a[href="/settings"], main [cal-notices]`, 6, "Reminders") audit("calendars", "/calendars", `main [cal-dot]`, 12, "Calendars") audit("sharing a calendar", "/calendars/1/share", `form[cal-feed-new]`, 12, "Team") run("make a subscription link", chromedp.Navigate(rig.Origin+"/calendars/1/share"), chromedp.WaitVisible(chromedp.CSS(`form[cal-feed-new] button`)), chromedp.Click(chromedp.CSS(`form[cal-feed-new] button`)), chromedp.WaitVisible(chromedp.CSS(`main [rst-list], main ul`))) audit("sharing a calendar, with a link", "~/calendars/1/share?token=", `form[cal-feed-new]`, 20, "Team") audit("import", "/import", `main form input[type=file]`, 20, "Import a calendar") ics := filepath.Join(t.TempDir(), "one.ics") 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 { t.Fatal(err) } run("import a file", chromedp.Navigate(rig.Origin+"/import"), chromedp.WaitVisible(chromedp.CSS(`input[type=file]`)), chromedp.SetUploadFiles(`input[type=file]`, []string{ics}), chromedp.Click(chromedp.CSS(`form[action="/import"] button[type=submit]`)), chromedp.WaitNotPresent(chromedp.CSS(`form[action="/import"] input[type=file]`))) audit("import done", "~/import", `main [rst-page-header]`, 10, "Imported") audit("booking pages", "/settings/booking", `main [cal-book-page]`, 20, "Booking pages") audit("a new booking page", "/settings/booking/new", `main form`, 30, "New booking page") audit("a page that is not there", "/events/999999", `main h1`, 4, "We can’t find that page") // The stranger's side: the page, a day's slots, a slot's details, // the hold, then the mailed link: confirm, manage, reschedule, // cancel. public := "/book/paul/" + page.Code + "?zone=UTC" audit("the booking page", public, `[cal-book-days] a`, 100, "Quick chat") var day string run("a booking day", evalInto(&day, `document.querySelector('[cal-book-days] a').getAttribute('href')`)) audit("a booking day", day, `[cal-book-slot]`, 100, "Quick chat") run("a slot", chromedp.Click(chromedp.CSS(`[cal-book-slot]`)), chromedp.WaitVisible(chromedp.CSS(`[cal-book-details] input[name="name"]`))) audit("a slot's details", "~/book/paul/"+page.Code, `[cal-book-details] input[name="name"]`, 60, "Quick chat") run("hold it", chromedp.SendKeys(chromedp.CSS(`#name`), "Doc"), chromedp.SendKeys(chromedp.CSS(`#address`), "doc@example.com"), chromedp.Click(chromedp.CSS(`[cal-book-details] button[type=submit]`)), chromedp.WaitNotPresent(chromedp.CSS(`[cal-book-details]`))) audit("the hold, sent", "~/book/paul/"+page.Code, `main [rst-page-header-sub]`, 8, "Check your mail") mail, err := app.LastMailForTest() if err != nil { t.Fatal(err) } manage := regexp.MustCompile(`/book/m/[0-9a-f]{64}`).FindString(mail) if manage == "" { t.Fatalf("no manage link in the hold mail") } audit("the hold, to confirm", manage, `form[action$="/confirm"]`, 10, "Confirm your booking") run("confirm it", chromedp.Click(chromedp.CSS(`form[action$="/confirm"] button`)), chromedp.WaitVisible(chromedp.CSS(`[cal-book-manage-actions]`))) audit("a booking", "~"+manage+"/confirm", `[cal-book-manage-actions]`, 12, "Quick chat") audit("rescheduling a booking", manage+"/reschedule", `[cal-book-days] a`, 100, "Quick chat") run("cancel it", chromedp.Navigate(rig.Origin+manage), chromedp.WaitVisible(chromedp.CSS(`form[action$="/cancel"] button`)), chromedp.Click(chromedp.CSS(`form[action$="/cancel"] button`)), chromedp.WaitNotPresent(chromedp.CSS(`form[action$="/cancel"]`))) audit("a cancelled booking", "~"+manage+"/cancel", `main p`, 6, "Quick chat") }