//go:build browser package mapatest import ( "context" "encoding/json" "fmt" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // Navigation that feels solid (build notes 58 and 59): a block link // lands without a jump, and no screen scrolls by a stray pixel. // longBody is forty paragraphs, ^early in the second and ^deep in the // thirtieth. func longBody() string { var paragraphs []string for number := 1; number <= 40; number++ { marker := map[int]string{2: " ^early", 30: " ^deep"}[number] paragraphs = append(paragraphs, fmt.Sprintf("Paragraph %d. %s%s", number, strings.Repeat("The quick brown fox jumps over "+ "the lazy dog. ", 3), marker)) } return strings.Join(paragraphs, "\n\n") } // landing is where the browser left the target block, and whether it // is tinted. type landing struct { ScrollY, Top, Bar, View float64 Target, Tinted bool } const readLanding = `JSON.stringify((function () { var block = document.querySelector(location.hash); var bar = document.querySelector("header[estilo-bar]"); return { scrollY: scrollY, top: block.getBoundingClientRect().top, bar: bar.getBoundingClientRect().height, view: innerHeight, target: block.matches(":target"), tinted: block.getAnimations().some(function (animation) { return animation.animationName === "mapa-arrive"; }) }; })())` // Block chips land the block without a jump: one in the first screen's // upper part scrolls nothing, a deep one lands 40% down the view below // the sticky bar, never under it, and both are tinted, with scripts // off and on (the editor redraws the block as it mounts). func TestABlockLinkLandsWithoutAJump(t *testing.T) { rig, store := browserRig(t, 0) ctx := context.Background() if _, err := store.CreateTeamPage(ctx, Bo, "Long notes", longBody()); err != nil { t.Fatal(err) } from, err := store.CreateTeamPage(ctx, Bo, "Jumper", "Early: [[Long notes#^early]]\n\nDeep: [[Long notes#^deep]]") if err != nil { t.Fatal(err) } for _, live := range []bool{false, true} { for _, size := range [][2]int64{{1280, 800}, {390, 844}} { name := fmt.Sprintf("%dx%d scripts %v", size[0], size[1], live) early := land(t, rig, mapa.PagePath(from), "early", size, live) if early.ScrollY != 0 || !early.Target || !early.Tinted { t.Errorf("%s: the early block %+v", name, early) } deep := land(t, rig, mapa.PagePath(from), "deep", size, live) want := deep.Bar + (deep.View-deep.Bar)*0.4 if deep.ScrollY == 0 || deep.Top < want-2 || deep.Top > want+2 || !deep.Target || !deep.Tinted { t.Errorf("%s: the deep block %+v, want its top at %.0f", name, deep, want) } } } } // land clicks the chip to a block and reads where the block is. func land(t *testing.T, rig *harness.Rig, from, block string, size [2]int64, live bool) landing { t.Helper() chip := `article[mapa-body] a[href$="#b-` + block + `"]` drive(t, rig, scripts(live), chromedp.EmulateViewport(size[0], size[1]), chromedp.Navigate(rig.Origin+from), chromedp.WaitVisible(chip)) if live { drive(t, rig, chromedp.WaitVisible(mounted)) } drive(t, rig, chromedp.Click(chip), chromedp.WaitVisible(`[id="b-`+block+`"]`)) if live { drive(t, rig, chromedp.WaitVisible(mounted)) } var seen landing eventually(t, "the block is the target", func() bool { raw := read(t, rig, chromedp.Evaluate[string](readLanding)) if err := json.Unmarshal([]byte(raw), &seen); err != nil { t.Fatal(err) } return seen.Target }) return seen } // overflow is the document's scroll height against what its content // needs: the window, or the body's own flow if that is taller. With // scripts on (live is true) and an editor's island on the page, it // waits for the mount, which adds the [[ and / lists' live regions. const overflow = `(function (live) { if (live && document.getElementById("mapa-editor") && !document.querySelector(".ProseMirror")) return ""; var body = document.body.getBoundingClientRect(); var needs = Math.max(innerHeight, Math.ceil(body.bottom + scrollY)); var height = document.documentElement.scrollHeight; return height <= needs ? "ok" : height + " > " + needs; })` // No screen is taller than its content: a visually hidden element (a // live region appended to ) once hung 1px below every page, so // each one scrolled by that pixel. A short page cannot scroll at all. func TestNoScreenScrollsByAStrayPixel(t *testing.T) { rig, store := browserRig(t, 0) page, err := store.CreateTeamPage(context.Background(), Bo, "Short", "One line.") if err != nil { t.Fatal(err) } path := mapa.PagePath(page) screens := []string{"/", "/d/2026-11-10", path, path + "/history", "/scratch", "/scratch/new", "/search", "/search?q=line", "/settings", "/pages"} for _, live := range []bool{false, true} { for _, width := range []int64{390, 800, 1280} { for _, screen := range screens { drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 800), chromedp.Navigate(rig.Origin+screen), chromedp.WaitReady("body")) var got string eventually(t, "the page settled", func() bool { got = read(t, rig, chromedp.Evaluate[string]( fmt.Sprintf("%s(%v)", overflow, live))) return got != "" }) if got != "ok" { t.Errorf("%s at %dpx, scripts %v: %s", screen, width, live, got) } } } } }