//go:build browser package calendartest import ( "context" "io" "log/slog" "net/http" "path/filepath" "testing" "time" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" calendar "amadan.net/oficina/calendar/internal/calendar" ) // TestQuickAddOnANarrowScreen: on a phone the quick-add panel stops // floating and drops into the page (calendar.css, max-width 40rem), and // at 390px it ran about 15px past the right edge of the screen — the // textarea's and the Add button's right ends were off the side, and the // page scrolled sideways. Every string check in the Go suite passes a // panel like that. This opens it at 320 and 390 and fails if anything // inside it reaches past the viewport, or the page grows wider than the // screen. func TestQuickAddOnANarrowScreen(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 return a.Handler() }) session := signInOverHTTP(t, app, rig.Origin, "host@example.com") for _, width := range []int64{320, 390} { var problems []string ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) err := chromedp.Run(ctx, network.SetCookie(session.Name, session.Value).WithURL(rig.Origin+"/"). WithHTTPOnly(session.HttpOnly).WithSameSite(network.CookieSameSiteLax), emulation.SetDeviceMetricsOverride(width, 844, 1, true), chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(`[cal-quick] > summary`, chromedp.ByQuery), chromedp.Click(`[cal-quick] > summary`, chromedp.ByQuery), chromedp.WaitVisible(`[cal-quick-text]`, chromedp.ByQuery), chromedp.Evaluate(quickAddProblems, &problems), ) cancel() if err != nil { t.Fatalf("%dpx: %v", width, err) } for _, p := range problems { t.Errorf("%dpx, the open quick-add panel: %s", width, p) } } } // quickAddProblems lists every visible element of the open quick-add // panel that reaches past either side of the viewport, and the page // itself if it scrolls sideways. const quickAddProblems = `(() => { const vw = document.documentElement.clientWidth, out = [] const els = [document.querySelector('[cal-quick]'), ...document.querySelectorAll('[cal-quick] *')] for (const el of els) { const r = el.getBoundingClientRect() if (r.width === 0 && r.height === 0) continue if (r.right > vw + 0.5 || r.left < -0.5) { const name = el.tagName.toLowerCase() + [...el.attributes].filter(a => a.name.startsWith('cal-')).map(a => '[' + a.name + ']').join('') out.push(name + ' spans ' + Math.round(r.left) + '-' + Math.round(r.right) + ' in a ' + vw + 'px viewport') } } if (document.documentElement.scrollWidth > vw) out.push('the page is ' + document.documentElement.scrollWidth + 'px wide in a ' + vw + 'px viewport') return out })()`