| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/rastrillo/rastrillo/harness" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/oficina/mapa/internal/mapa" |
| 16 | ) |
| 17 | |
| 18 | // Navigation that feels solid (build notes 58 and 59): a block link |
| 19 | // lands without a jump, and no screen scrolls by a stray pixel. |
| 20 | |
| 21 | // longBody is forty paragraphs, ^early in the second and ^deep in the |
| 22 | // thirtieth. |
| 23 | func longBody() string { |
| 24 | var paragraphs []string |
| 25 | for number := 1; number <= 40; number++ { |
| 26 | marker := map[int]string{2: " ^early", 30: " ^deep"}[number] |
| 27 | paragraphs = append(paragraphs, fmt.Sprintf("Paragraph %d. %s%s", |
| 28 | number, strings.Repeat("The quick brown fox jumps over "+ |
| 29 | "the lazy dog. ", 3), marker)) |
| 30 | } |
| 31 | return strings.Join(paragraphs, "\n\n") |
| 32 | } |
| 33 | |
| 34 | // landing is where the browser left the target block, and whether it |
| 35 | // is tinted. |
| 36 | type landing struct { |
| 37 | ScrollY, Top, Bar, View float64 |
| 38 | Target, Tinted bool |
| 39 | } |
| 40 | |
| 41 | const readLanding = `JSON.stringify((function () { |
| 42 | var block = document.querySelector(location.hash); |
| 43 | var bar = document.querySelector("header[estilo-bar]"); |
| 44 | return { scrollY: scrollY, top: block.getBoundingClientRect().top, |
| 45 | bar: bar.getBoundingClientRect().height, view: innerHeight, |
| 46 | target: block.matches(":target"), |
| 47 | tinted: block.getAnimations().some(function (animation) { |
| 48 | return animation.animationName === "mapa-arrive"; }) }; |
| 49 | })())` |
| 50 | |
| 51 | // Block chips land the block without a jump: one in the first screen's |
| 52 | // upper part scrolls nothing, a deep one lands 40% down the view below |
| 53 | // the sticky bar, never under it, and both are tinted, with scripts |
| 54 | // off and on (the editor redraws the block as it mounts). |
| 55 | func TestABlockLinkLandsWithoutAJump(t *testing.T) { |
| 56 | rig, store := browserRig(t, 0) |
| 57 | ctx := context.Background() |
| 58 | if _, err := store.CreateTeamPage(ctx, Bo, "Long notes", |
| 59 | longBody()); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | from, err := store.CreateTeamPage(ctx, Bo, "Jumper", |
| 63 | "Early: [[Long notes#^early]]\n\nDeep: [[Long notes#^deep]]") |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | for _, live := range []bool{false, true} { |
| 68 | for _, size := range [][2]int64{{1280, 800}, {390, 844}} { |
| 69 | name := fmt.Sprintf("%dx%d scripts %v", size[0], size[1], live) |
| 70 | early := land(t, rig, mapa.PagePath(from), "early", size, live) |
| 71 | if early.ScrollY != 0 || !early.Target || !early.Tinted { |
| 72 | t.Errorf("%s: the early block %+v", name, early) |
| 73 | } |
| 74 | deep := land(t, rig, mapa.PagePath(from), "deep", size, live) |
| 75 | want := deep.Bar + (deep.View-deep.Bar)*0.4 |
| 76 | if deep.ScrollY == 0 || deep.Top < want-2 || deep.Top > want+2 || |
| 77 | !deep.Target || !deep.Tinted { |
| 78 | t.Errorf("%s: the deep block %+v, want its top at %.0f", |
| 79 | name, deep, want) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // land clicks the chip to a block and reads where the block is. |
| 86 | func land(t *testing.T, rig *harness.Rig, from, block string, |
| 87 | size [2]int64, live bool) landing { |
| 88 | t.Helper() |
| 89 | chip := `article[mapa-body] a[href$="#b-` + block + `"]` |
| 90 | drive(t, rig, scripts(live), chromedp.EmulateViewport(size[0], size[1]), |
| 91 | chromedp.Navigate(rig.Origin+from), chromedp.WaitVisible(chip)) |
| 92 | if live { |
| 93 | drive(t, rig, chromedp.WaitVisible(mounted)) |
| 94 | } |
| 95 | drive(t, rig, chromedp.Click(chip), |
| 96 | chromedp.WaitVisible(`[id="b-`+block+`"]`)) |
| 97 | if live { |
| 98 | drive(t, rig, chromedp.WaitVisible(mounted)) |
| 99 | } |
| 100 | var seen landing |
| 101 | eventually(t, "the block is the target", func() bool { |
| 102 | raw := read(t, rig, chromedp.Evaluate[string](readLanding)) |
| 103 | if err := json.Unmarshal([]byte(raw), &seen); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | return seen.Target |
| 107 | }) |
| 108 | return seen |
| 109 | } |
| 110 | |
| 111 | // overflow is the document's scroll height against what its content |
| 112 | // needs: the window, or the body's own flow if that is taller. With |
| 113 | // scripts on (live is true) and an editor's island on the page, it |
| 114 | // waits for the mount, which adds the [[ and / lists' live regions. |
| 115 | const overflow = `(function (live) { |
| 116 | if (live && document.getElementById("mapa-editor") && |
| 117 | !document.querySelector(".ProseMirror")) return ""; |
| 118 | var body = document.body.getBoundingClientRect(); |
| 119 | var needs = Math.max(innerHeight, Math.ceil(body.bottom + scrollY)); |
| 120 | var height = document.documentElement.scrollHeight; |
| 121 | return height <= needs ? "ok" : height + " > " + needs; |
| 122 | })` |
| 123 | |
| 124 | // No screen is taller than its content: a visually hidden element (a |
| 125 | // live region appended to <body>) once hung 1px below every page, so |
| 126 | // each one scrolled by that pixel. A short page cannot scroll at all. |
| 127 | func TestNoScreenScrollsByAStrayPixel(t *testing.T) { |
| 128 | rig, store := browserRig(t, 0) |
| 129 | page, err := store.CreateTeamPage(context.Background(), Bo, "Short", |
| 130 | "One line.") |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | path := mapa.PagePath(page) |
| 135 | screens := []string{"/", "/d/2026-11-10", path, path + "/history", |
| 136 | "/scratch", "/scratch/new", "/search", "/search?q=line", |
| 137 | "/settings", "/pages"} |
| 138 | for _, live := range []bool{false, true} { |
| 139 | for _, width := range []int64{390, 800, 1280} { |
| 140 | for _, screen := range screens { |
| 141 | drive(t, rig, scripts(live), chromedp.EmulateViewport(width, |
| 142 | 800), chromedp.Navigate(rig.Origin+screen), |
| 143 | chromedp.WaitReady("body")) |
| 144 | var got string |
| 145 | eventually(t, "the page settled", func() bool { |
| 146 | got = read(t, rig, chromedp.Evaluate[string]( |
| 147 | fmt.Sprintf("%s(%v)", overflow, live))) |
| 148 | return got != "" |
| 149 | }) |
| 150 | if got != "ok" { |
| 151 | t.Errorf("%s at %dpx, scripts %v: %s", screen, width, |
| 152 | live, got) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |