//go:build browser package mapatest import ( "fmt" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // The header has no Search (Paul, 2026-11-18: "I think we can just lose // the search icon in the header"). Search is a place: the sidebar's row // with ⌘K, the rail's icon with "Search" its tooltip, the Menu sheet's // row below 800px, once, and ⌘K or Ctrl+K anywhere. Each opens ⌘K with // scripts and the search page without. // barSearch is anything in the bar that is a Search: a link to the // search page or an icon-only action, outside the Menu sheet. const barSearch = `Array.prototype.filter.call( document.querySelectorAll('[estilo-bar] a[href="/search"], ' + '[estilo-bar] [estilo-bar-icon], [estilo-bar] [aria-keyshortcuts]'), function (el) { return !el.closest('[estilo-menu="sheet"]') && el.checkVisibility(); }).length` // paletteOpen waits for ⌘K's dialog. var paletteOpen = chromedp.WaitVisible("dialog[mapa-palette] input") func TestTheBarHasNoSearch(t *testing.T) { rig, _ := browserRig(t, 1) path := firstTeamPage(t, rig) for _, live := range []bool{false, true} { for _, width := range []int64{1440, 1000, 800, 390} { for _, at := range []string{"/", path, path + "/history", "/search?q=line"} { drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+at), chromedp.WaitVisible("[estilo-bar]")) if got := read(t, rig, chromedp.Evaluate[int]( barSearch)); got != 0 { t.Errorf("%s at %dpx, scripts %v: the bar shows %d "+ "Search controls", at, width, live, got) } if html := read(t, rig, chromedp.Evaluate[string]( `document.documentElement.outerHTML`)); strings.Contains( html, "Search ⌘K") { t.Errorf("%s at %dpx, scripts %v: \"Search ⌘K\" is "+ "still drawn", at, width, live) } } } } for _, width := range []int64{1440, 1000, 800, 390} { drive(t, rig, scripts(true), chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible("[estilo-bar]")) if found := axeSerious(t, rig); found != "" { t.Errorf("%dpx: axe:\n%s", width, found) } } } // openSearch presses the Search at selector: ⌘K with scripts, the // search page without. func openSearch(t *testing.T, rig *harness.Rig, name, selector string, live bool, before ...chromedp.Action[chromedp.Void]) { t.Helper() steps := append(before, press(selector)) if live { drive(t, rig, append(steps, paletteOpen)...) return } drive(t, rig, append(steps, chromedp.WaitVisible(`main input[name="q"]`))...) if at := read(t, rig, chromedp.Evaluate[string]( `location.pathname`)); at != "/search" { t.Errorf("%s with scripts off went to %s", name, at) } } // The sidebar's row, open and in the rail, scripts on and off. func TestTheSidebarsSearchOpensSearch(t *testing.T) { rig, _ := browserRig(t, 1, harness.WithHover()) path := firstTeamPage(t, rig) for _, side := range sideCases { for _, live := range []bool{false, true} { name := fmt.Sprintf("%dpx, %s (cookie %q), scripts %v", side.width, side.show, side.cookie, live) drive(t, rig, scripts(live), sideCookie(rig, side.cookie), chromedp.EmulateViewport(side.width, 877), chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible(sideSearch)) label := read(t, rig, chromedp.Evaluate[string]( `document.querySelector('`+sideSearch+ ` [mapa-side-label]').textContent`)) if label != "Search" { t.Errorf("%s: the sidebar's Search is labelled %q", name, label) } if side.show == "rail" { // The label is the rail's tooltip: hidden at rest, shown // on hover. const shown = `getComputedStyle(document.querySelector('` + sideSearch + ` [mapa-side-label]')).opacity` rested := read(t, rig, chromedp.Evaluate[string](shown)) drive(t, rig, rest(sideSearch), chromedp.Sleep(200e6)) hovered := read(t, rig, chromedp.Evaluate[string](shown)) if rested != "0" || hovered != "1" { t.Errorf("%s: the rail's tooltip is at opacity %s at "+ "rest and %s on hover", name, rested, hovered) } drive(t, rig, rest("main")) } else if kbd := read(t, rig, chromedp.Evaluate[string]( `document.querySelector('`+sideSearch+ ` kbd').textContent`)); kbd != "⌘K" { t.Errorf("%s: the sidebar's Search shows %q", name, kbd) } openSearch(t, rig, name, sideSearch, live) } } } // Below 800px Search is the Menu sheet's row, once. func TestTheMenuSheetHasSearchOnce(t *testing.T) { rig, _ := browserRig(t, 1) path := firstTeamPage(t, rig) const sheetSearch = `[estilo-menu="sheet"] a[href="/search"]` for _, live := range []bool{false, true} { name := fmt.Sprintf("the Menu sheet's Search, scripts %v", live) drive(t, rig, scripts(live), chromedp.EmulateViewport(390, 844), chromedp.Navigate(rig.Origin+path), chromedp.Click("[estilo-bar-more] > summary"), chromedp.WaitVisible(sheetSearch)) rows := read(t, rig, chromedp.Evaluate[string]( `Array.prototype.map.call(document.querySelectorAll( '[estilo-menu="sheet"] [estilo-menu-item]'), function (row) { return row.textContent.trim(); }).join("|")`)) if strings.Count("|"+rows+"|", "|Search|") != 1 || strings.Contains(rows, "⌘K") { t.Errorf("scripts %v: the Menu sheet's rows are %q", live, rows) } openSearch(t, rig, name, sheetSearch, live) } } // blurAll takes focus off whatever a page focused on load. const blurAll = `document.activeElement && document.activeElement.blur()` // ⌘K and Ctrl+K open Search from any page, at any width. func TestTheShortcutOpensSearchAnywhere(t *testing.T) { rig, _ := browserRig(t, 1) path := firstTeamPage(t, rig) for _, width := range []int64{1440, 900, 390} { for _, at := range []string{"/", "/pages", "/scratch", path, path + "/history", "/search"} { for _, modifier := range []chromedp.Modifier{ chromedp.ModifierMeta, chromedp.ModifierCtrl} { drive(t, rig, scripts(true), chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+at), chromedp.WaitVisible("[estilo-bar]"), chromedp.Evaluate[chromedp.Void](blurAll), chromedp.KeyEvent("k", chromedp.KeyModifiers(modifier)), paletteOpen, chromedp.KeyEvent(kb.Escape), chromedp.WaitNotVisible("dialog[mapa-palette]")) } } } } // L5 for the header: every control in it is where the server drew it // (scripts off) once the page has mounted, and no layout shift is // recorded from the start of the load, light and dark. func TestTheBarMovesNothing(t *testing.T) { rig, _ := browserRig(t, 1) path := firstTeamPage(t, rig) drive(t, rig, onNewDocument(shiftRecorder)) const bar = `JSON.stringify(Array.prototype.map.call( document.querySelectorAll("[estilo-bar-end] > *, " + "[estilo-bar-actions] a, [estilo-bar-actions] button"), function (el) { var b = el.getBoundingClientRect(); return [el.nodeName, b.left, b.top, b.width, b.height].join(" "); }))` for _, dark := range []bool{false, true} { for _, width := range []int64{1440, 1000} { load := func(live bool) string { drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 900), colorScheme(dark), chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible("[estilo-bar-end]"), chromedp.Sleep(300e6)) return read(t, rig, chromedp.Evaluate[string](bar)) } off, on := load(false), load(true) if off != on { t.Errorf("%dpx dark=%v: the bar moved once scripts "+ "ran:\noff %s\non %s", width, dark, off, on) } if shifts := read(t, rig, chromedp.Evaluate[string]( `JSON.stringify(window.__shifts || [])`)); shifts != "[]" { t.Errorf("%dpx dark=%v: layout shifts %s", width, dark, shifts) } } } }