//go:build browser package calendartest import ( "context" "io" "log/slog" "net" "net/http" "net/url" "os/exec" "path/filepath" "regexp" "strings" "testing" "time" calendar "amadan.net/oficina/calendar/internal/calendar" ) // TestViewSwitchingHoldsStillInARealBrowser is Paul's "views jumping" // complaint as a gate: switching Day, Week, Month and Agenda, and stepping // each with Previous and Next, moves none of the page's controls and // shifts no layout, at 1440 and 390 wide. Each hop is a full document // load, so no single-page CLS figure sees the jump; the node script // compares boxes across documents. See test/browser/views.mjs. func TestViewSwitchingHoldsStillInARealBrowser(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatalf("listen: %v", err) } origin := "http://" + ln.Addr().String() app, 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() { app.Close() }) srv := &http.Server{Handler: app.Handler()} go func() { _ = srv.Serve(ln) }() t.Cleanup(func() { _ = srv.Close() }) session := signInOverHTTP(t, app, origin, "someone@example.com") client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} do := func(method, path string, form url.Values) *http.Response { t.Helper() var body io.Reader if form != nil { body = strings.NewReader(form.Encode()) } req, _ := http.NewRequest(method, origin+path, body) if form != nil { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Origin", origin) } req.AddCookie(session) res, err := client.Do(req) if err != nil { t.Fatal(err) } return res } res := do("GET", "/events/new", nil) page, _ := io.ReadAll(res.Body) res.Body.Close() m := regexp.MustCompile(`