| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo/harness" |
| 11 | "github.com/chromedp/chromedp" |
| 12 | "github.com/chromedp/chromedp/kb" |
| 13 | ) |
| 14 | |
| 15 | // The header has no Search (Paul, 2026-11-18: "I think we can just lose |
| 16 | // the search icon in the header"). Search is a place: the sidebar's row |
| 17 | // with ⌘K, the rail's icon with "Search" its tooltip, the Menu sheet's |
| 18 | // row below 800px, once, and ⌘K or Ctrl+K anywhere. Each opens ⌘K with |
| 19 | // scripts and the search page without. |
| 20 | |
| 21 | // barSearch is anything in the bar that is a Search: a link to the |
| 22 | // search page or an icon-only action, outside the Menu sheet. |
| 23 | const barSearch = `Array.prototype.filter.call( |
| 24 | document.querySelectorAll('[estilo-bar] a[href="/search"], ' + |
| 25 | '[estilo-bar] [estilo-bar-icon], [estilo-bar] [aria-keyshortcuts]'), |
| 26 | function (el) { |
| 27 | return !el.closest('[estilo-menu="sheet"]') && el.checkVisibility(); |
| 28 | }).length` |
| 29 | |
| 30 | // paletteOpen waits for ⌘K's dialog. |
| 31 | var paletteOpen = chromedp.WaitVisible("dialog[mapa-palette] input") |
| 32 | |
| 33 | func TestTheBarHasNoSearch(t *testing.T) { |
| 34 | rig, _ := browserRig(t, 1) |
| 35 | path := firstTeamPage(t, rig) |
| 36 | for _, live := range []bool{false, true} { |
| 37 | for _, width := range []int64{1440, 1000, 800, 390} { |
| 38 | for _, at := range []string{"/", path, path + "/history", |
| 39 | "/search?q=line"} { |
| 40 | drive(t, rig, scripts(live), |
| 41 | chromedp.EmulateViewport(width, 900), |
| 42 | chromedp.Navigate(rig.Origin+at), |
| 43 | chromedp.WaitVisible("[estilo-bar]")) |
| 44 | if got := read(t, rig, chromedp.Evaluate[int]( |
| 45 | barSearch)); got != 0 { |
| 46 | t.Errorf("%s at %dpx, scripts %v: the bar shows %d "+ |
| 47 | "Search controls", at, width, live, got) |
| 48 | } |
| 49 | if html := read(t, rig, chromedp.Evaluate[string]( |
| 50 | `document.documentElement.outerHTML`)); strings.Contains( |
| 51 | html, "Search ⌘K") { |
| 52 | t.Errorf("%s at %dpx, scripts %v: \"Search ⌘K\" is "+ |
| 53 | "still drawn", at, width, live) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | for _, width := range []int64{1440, 1000, 800, 390} { |
| 59 | drive(t, rig, scripts(true), chromedp.EmulateViewport(width, 900), |
| 60 | chromedp.Navigate(rig.Origin+path), |
| 61 | chromedp.WaitVisible("[estilo-bar]")) |
| 62 | if found := axeSerious(t, rig); found != "" { |
| 63 | t.Errorf("%dpx: axe:\n%s", width, found) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // openSearch presses the Search at selector: ⌘K with scripts, the |
| 69 | // search page without. |
| 70 | func openSearch(t *testing.T, rig *harness.Rig, name, selector string, |
| 71 | live bool, before ...chromedp.Action[chromedp.Void]) { |
| 72 | t.Helper() |
| 73 | steps := append(before, press(selector)) |
| 74 | if live { |
| 75 | drive(t, rig, append(steps, paletteOpen)...) |
| 76 | return |
| 77 | } |
| 78 | drive(t, rig, append(steps, |
| 79 | chromedp.WaitVisible(`main input[name="q"]`))...) |
| 80 | if at := read(t, rig, chromedp.Evaluate[string]( |
| 81 | `location.pathname`)); at != "/search" { |
| 82 | t.Errorf("%s with scripts off went to %s", name, at) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // The sidebar's row, open and in the rail, scripts on and off. |
| 87 | func TestTheSidebarsSearchOpensSearch(t *testing.T) { |
| 88 | rig, _ := browserRig(t, 1, harness.WithHover()) |
| 89 | path := firstTeamPage(t, rig) |
| 90 | for _, side := range sideCases { |
| 91 | for _, live := range []bool{false, true} { |
| 92 | name := fmt.Sprintf("%dpx, %s (cookie %q), scripts %v", |
| 93 | side.width, side.show, side.cookie, live) |
| 94 | drive(t, rig, scripts(live), sideCookie(rig, side.cookie), |
| 95 | chromedp.EmulateViewport(side.width, 877), |
| 96 | chromedp.Navigate(rig.Origin+path), |
| 97 | chromedp.WaitVisible(sideSearch)) |
| 98 | label := read(t, rig, chromedp.Evaluate[string]( |
| 99 | `document.querySelector('`+sideSearch+ |
| 100 | ` [mapa-side-label]').textContent`)) |
| 101 | if label != "Search" { |
| 102 | t.Errorf("%s: the sidebar's Search is labelled %q", |
| 103 | name, label) |
| 104 | } |
| 105 | if side.show == "rail" { |
| 106 | // The label is the rail's tooltip: hidden at rest, shown |
| 107 | // on hover. |
| 108 | const shown = `getComputedStyle(document.querySelector('` + |
| 109 | sideSearch + ` [mapa-side-label]')).opacity` |
| 110 | rested := read(t, rig, chromedp.Evaluate[string](shown)) |
| 111 | drive(t, rig, rest(sideSearch), chromedp.Sleep(200e6)) |
| 112 | hovered := read(t, rig, chromedp.Evaluate[string](shown)) |
| 113 | if rested != "0" || hovered != "1" { |
| 114 | t.Errorf("%s: the rail's tooltip is at opacity %s at "+ |
| 115 | "rest and %s on hover", name, rested, hovered) |
| 116 | } |
| 117 | drive(t, rig, rest("main")) |
| 118 | } else if kbd := read(t, rig, chromedp.Evaluate[string]( |
| 119 | `document.querySelector('`+sideSearch+ |
| 120 | ` kbd').textContent`)); kbd != "⌘K" { |
| 121 | t.Errorf("%s: the sidebar's Search shows %q", name, kbd) |
| 122 | } |
| 123 | openSearch(t, rig, name, sideSearch, live) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Below 800px Search is the Menu sheet's row, once. |
| 129 | func TestTheMenuSheetHasSearchOnce(t *testing.T) { |
| 130 | rig, _ := browserRig(t, 1) |
| 131 | path := firstTeamPage(t, rig) |
| 132 | const sheetSearch = `[estilo-menu="sheet"] a[href="/search"]` |
| 133 | for _, live := range []bool{false, true} { |
| 134 | name := fmt.Sprintf("the Menu sheet's Search, scripts %v", live) |
| 135 | drive(t, rig, scripts(live), chromedp.EmulateViewport(390, 844), |
| 136 | chromedp.Navigate(rig.Origin+path), |
| 137 | chromedp.Click("[estilo-bar-more] > summary"), |
| 138 | chromedp.WaitVisible(sheetSearch)) |
| 139 | rows := read(t, rig, chromedp.Evaluate[string]( |
| 140 | `Array.prototype.map.call(document.querySelectorAll( |
| 141 | '[estilo-menu="sheet"] [estilo-menu-item]'), |
| 142 | function (row) { return row.textContent.trim(); }).join("|")`)) |
| 143 | if strings.Count("|"+rows+"|", "|Search|") != 1 || |
| 144 | strings.Contains(rows, "⌘K") { |
| 145 | t.Errorf("scripts %v: the Menu sheet's rows are %q", live, rows) |
| 146 | } |
| 147 | openSearch(t, rig, name, sheetSearch, live) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // blurAll takes focus off whatever a page focused on load. |
| 152 | const blurAll = `document.activeElement && document.activeElement.blur()` |
| 153 | |
| 154 | // ⌘K and Ctrl+K open Search from any page, at any width. |
| 155 | func TestTheShortcutOpensSearchAnywhere(t *testing.T) { |
| 156 | rig, _ := browserRig(t, 1) |
| 157 | path := firstTeamPage(t, rig) |
| 158 | for _, width := range []int64{1440, 900, 390} { |
| 159 | for _, at := range []string{"/", "/pages", "/scratch", path, |
| 160 | path + "/history", "/search"} { |
| 161 | for _, modifier := range []chromedp.Modifier{ |
| 162 | chromedp.ModifierMeta, chromedp.ModifierCtrl} { |
| 163 | drive(t, rig, scripts(true), |
| 164 | chromedp.EmulateViewport(width, 900), |
| 165 | chromedp.Navigate(rig.Origin+at), |
| 166 | chromedp.WaitVisible("[estilo-bar]"), |
| 167 | chromedp.Evaluate[chromedp.Void](blurAll), |
| 168 | chromedp.KeyEvent("k", chromedp.KeyModifiers(modifier)), |
| 169 | paletteOpen, |
| 170 | chromedp.KeyEvent(kb.Escape), |
| 171 | chromedp.WaitNotVisible("dialog[mapa-palette]")) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // L5 for the header: every control in it is where the server drew it |
| 178 | // (scripts off) once the page has mounted, and no layout shift is |
| 179 | // recorded from the start of the load, light and dark. |
| 180 | func TestTheBarMovesNothing(t *testing.T) { |
| 181 | rig, _ := browserRig(t, 1) |
| 182 | path := firstTeamPage(t, rig) |
| 183 | drive(t, rig, onNewDocument(shiftRecorder)) |
| 184 | const bar = `JSON.stringify(Array.prototype.map.call( |
| 185 | document.querySelectorAll("[estilo-bar-end] > *, " + |
| 186 | "[estilo-bar-actions] a, [estilo-bar-actions] button"), |
| 187 | function (el) { var b = el.getBoundingClientRect(); |
| 188 | return [el.nodeName, b.left, b.top, b.width, b.height].join(" "); }))` |
| 189 | for _, dark := range []bool{false, true} { |
| 190 | for _, width := range []int64{1440, 1000} { |
| 191 | load := func(live bool) string { |
| 192 | drive(t, rig, scripts(live), |
| 193 | chromedp.EmulateViewport(width, 900), colorScheme(dark), |
| 194 | chromedp.Navigate(rig.Origin+path), |
| 195 | chromedp.WaitVisible("[estilo-bar-end]"), |
| 196 | chromedp.Sleep(300e6)) |
| 197 | return read(t, rig, chromedp.Evaluate[string](bar)) |
| 198 | } |
| 199 | off, on := load(false), load(true) |
| 200 | if off != on { |
| 201 | t.Errorf("%dpx dark=%v: the bar moved once scripts "+ |
| 202 | "ran:\noff %s\non %s", width, dark, off, on) |
| 203 | } |
| 204 | if shifts := read(t, rig, chromedp.Evaluate[string]( |
| 205 | `JSON.stringify(window.__shifts || [])`)); shifts != "[]" { |
| 206 | t.Errorf("%dpx dark=%v: layout shifts %s", width, dark, shifts) |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |