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 "path/filepath"
12 "testing"
13 "time"
14
15 "amadan.net/rastrillo/rastrillo/harness"
16 "github.com/chromedp/chromedp"
17
18 calendar "amadan.net/oficina/calendar/internal/calendar"
19)
20
21// TestFormFootOnANarrowScreen: rastrillo's form-foot draws its two
22// buttons at the lg size (v0.26). On a phone the booker's pair, "Hold
23// this time" and "Pick another time", no longer fit side by side, and the
24// row squeezed them instead of wrapping: two lines of label each at 390
25// wide, up to three at 320 (where the old size already broke them over
26// two). calendar.css lets that row wrap. This checks
27// at 320 and 390 that no form-foot label breaks across lines and no
28// button leaves the form, on the stranger's booking form, the one
29// most often opened on a phone.
30func TestFormFootOnANarrowScreen(t *testing.T) {
31 var app *calendar.App
32 rig := harness.New(t, func(origin string) http.Handler {
33 a, err := calendar.New(calendar.Config{
34 DBPath: filepath.Join(t.TempDir(), "calendar.db"),
35 Env: "development",
36 Origin: origin,
37 Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
38 })
39 if err != nil {
40 t.Fatalf("calendar.New: %v", err)
41 }
42 t.Cleanup(func() { a.Close() })
43 app = a
44 return a.Handler()
45 })
46 signInOverHTTP(t, app, rig.Origin, "host@example.com")
47 ctx := context.Background()
48 st := app.StoreForTest()
49 host := app.PersonByAddressForTest(t, "host@example.com")
50 if err := st.SaveHandle(ctx, host.ID, "paul"); err != nil {
51 t.Fatal(err)
52 }
53 ws, _ := calendar.ParseAvailability("every day 9-5")
54 page, err := st.CreateBookingPage(ctx, host.ID, calendar.BookingPageInput{
55 Title: "Quick chat", Description: "Half an hour.", Availability: "every day 9-5",
56 DurationMin: 30, HorizonDays: 30}, ws)
57 if err != nil {
58 t.Fatal(err)
59 }
60 if err := st.PublishBookingPage(ctx, host.ID, page.ID, true); err != nil {
61 t.Fatal(err)
62 }
63 public := "/book/paul/" + page.Code
64
65 for _, width := range []int64{320, 390} {
66 var day string
67 var problems []string
68 ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
69 err := chromedp.Do(ctx,
70 deviceMetrics(width, 844),
71 chromedp.Navigate(rig.Origin+public),
72 chromedp.WaitVisible(chromedp.CSS(`select[name="zone"]`)),
73 evalInto(&day, `(document.querySelector('a[href*="day="]') || {}).href || ''`),
74 )
75 if err == nil && day == "" {
76 err = fmt.Errorf("the booking page offers no day to pick")
77 }
78 if err == nil {
79 err = chromedp.Do(ctx,
80 chromedp.Navigate(day),
81 chromedp.WaitVisible(chromedp.CSS(`[cal-book-slot]`)),
82 chromedp.Click(chromedp.CSS(`[cal-book-slot]`)),
83 chromedp.WaitVisible(chromedp.CSS(`[cal-book-details] [rst-form-foot]`)),
84 evalInto(&problems, footProblems),
85 )
86 }
87 cancel()
88 if err != nil {
89 t.Fatalf("%dpx: %v", width, err)
90 }
91 for _, p := range problems {
92 t.Errorf("%dpx, the booker's form: %s", width, p)
93 }
94 }
95}
96
97// footProblems lists every form-foot button whose label breaks across
98// lines, or which reaches past its form's edge.
99const footProblems = `[...document.querySelectorAll('[rst-form-foot] [rst-btn]')].flatMap(b => {
100 const out = [], r = document.createRange()
101 r.selectNodeContents(b)
102 const lines = new Set([...r.getClientRects()].filter(x => x.width > 0).map(x => Math.round(x.top))).size
103 if (lines > 1) out.push('"' + b.textContent.trim() + '" breaks over ' + lines + ' lines')
104 const f = b.closest('form').getBoundingClientRect(), bb = b.getBoundingClientRect()
105 if (bb.right > f.right + 0.5 || bb.left < f.left - 0.5) out.push('"' + b.textContent.trim() + '" spans ' + Math.round(bb.left) + '-' + Math.round(bb.right) + ', its form ' + Math.round(f.left) + '-' + Math.round(f.right))
106 return out
107})`
108