| 1 | //go:build browser |
| 2 | |
| 3 | package calendartest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "io" |
| 8 | "log/slog" |
| 9 | "net/http" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/harness" |
| 15 | "github.com/chromedp/chromedp" |
| 16 | |
| 17 | calendar "amadan.net/oficina/calendar/internal/calendar" |
| 18 | ) |
| 19 | |
| 20 | // TestQuickAddOnANarrowScreen: on a phone the quick-add panel stops |
| 21 | // floating and drops into the page (calendar.css, max-width 40rem), and |
| 22 | // at 390px it ran about 15px past the right edge of the screen — the |
| 23 | // textarea's and the Add button's right ends were off the side, and the |
| 24 | // page scrolled sideways. Every string check in the Go suite passes a |
| 25 | // panel like that. This opens it at 320 and 390 and fails if anything |
| 26 | // inside it reaches past the viewport, or the page grows wider than the |
| 27 | // screen. |
| 28 | func TestQuickAddOnANarrowScreen(t *testing.T) { |
| 29 | var app *calendar.App |
| 30 | rig := harness.New(t, func(origin string) http.Handler { |
| 31 | a, err := calendar.New(calendar.Config{ |
| 32 | DBPath: filepath.Join(t.TempDir(), "calendar.db"), |
| 33 | Env: "development", |
| 34 | Origin: origin, |
| 35 | Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 36 | }) |
| 37 | if err != nil { |
| 38 | t.Fatalf("calendar.New: %v", err) |
| 39 | } |
| 40 | t.Cleanup(func() { a.Close() }) |
| 41 | app = a |
| 42 | return a.Handler() |
| 43 | }) |
| 44 | session := signInOverHTTP(t, app, rig.Origin, "host@example.com") |
| 45 | |
| 46 | for _, width := range []int64{320, 390} { |
| 47 | var problems []string |
| 48 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 49 | err := chromedp.Do(ctx, |
| 50 | setCookie(rig.Origin, session), |
| 51 | deviceMetrics(width, 844), |
| 52 | chromedp.Navigate(rig.Origin+"/"), |
| 53 | chromedp.WaitVisible(chromedp.CSS(`[cal-quick] > summary`)), |
| 54 | chromedp.Click(chromedp.CSS(`[cal-quick] > summary`)), |
| 55 | chromedp.WaitVisible(chromedp.CSS(`[cal-quick-text]`)), |
| 56 | evalInto(&problems, quickAddProblems), |
| 57 | ) |
| 58 | cancel() |
| 59 | if err != nil { |
| 60 | t.Fatalf("%dpx: %v", width, err) |
| 61 | } |
| 62 | for _, p := range problems { |
| 63 | t.Errorf("%dpx, the open quick-add panel: %s", width, p) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // quickAddProblems lists every visible element of the open quick-add |
| 69 | // panel that reaches past either side of the viewport, and the page |
| 70 | // itself if it scrolls sideways. |
| 71 | const quickAddProblems = `(() => { |
| 72 | const vw = document.documentElement.clientWidth, out = [] |
| 73 | const els = [document.querySelector('[cal-quick]'), ...document.querySelectorAll('[cal-quick] *')] |
| 74 | for (const el of els) { |
| 75 | const r = el.getBoundingClientRect() |
| 76 | if (r.width === 0 && r.height === 0) continue |
| 77 | if (r.right > vw + 0.5 || r.left < -0.5) { |
| 78 | const name = el.tagName.toLowerCase() + [...el.attributes].filter(a => a.name.startsWith('cal-')).map(a => '[' + a.name + ']').join('') |
| 79 | out.push(name + ' spans ' + Math.round(r.left) + '-' + Math.round(r.right) + ' in a ' + vw + 'px viewport') |
| 80 | } |
| 81 | } |
| 82 | if (document.documentElement.scrollWidth > vw) out.push('the page is ' + document.documentElement.scrollWidth + 'px wide in a ' + vw + 'px viewport') |
| 83 | return out |
| 84 | })()` |
| 85 | |