//go:build browser package fichastest import ( "context" "encoding/json" "fmt" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/network" "github.com/chromedp/cdproto/page" "github.com/chromedp/chromedp" ) // The header has no Search (Paul, 2026-11-18: "I think we can just lose // the search icon in the header"). Search is the rail's first row, with // ⌘K beside it while the rail is open and "Search" its tooltip when // folded; the Menu sheet's first row below 800px, once, so a phone has // it on a card and on History, which have no Find box; and ⌘K or Ctrl+K // anywhere but inside a field. Each goes to the list's Find box, and // with scripts off links to #find. // railSearch is the rail's Search. const railSearch = `[fichas-rail] a[data-fichas-search]` // barSearch counts what in the bar, outside the Menu sheet, is a Search // on screen: a link to the Find box, an icon-only action, or a control // with a shortcut. const barSearch = `Array.prototype.filter.call( document.querySelectorAll('[estilo-bar] a[href$="#find"], [estilo-bar] [estilo-bar-icon], [estilo-bar] [aria-keyshortcuts]'), function (el) { return !el.closest('[estilo-menu="sheet"]') && el.checkVisibility(); }).length` // whereAt is the page's path and fragment. const whereAt = `location.pathname + location.hash` // findFocused is whether the list's Find box has focus. const findFocused = `document.activeElement === document.querySelector('[data-live-search] input[name="q"]')` func scriptsOn(on bool) chromedp.Action[chromedp.Void] { return send(emulation.SetScriptExecutionDisabled, emulation.SetScriptExecutionDisabledParams{Value: !on}) } func viewport(width int64) chromedp.Action[chromedp.Void] { return send(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1}) } // railCookie sets the rail's fold, "" for none (the width decides). func railCookie(rig *harness.Rig, state string) chromedp.Action[chromedp.Void] { if state == "" { return send(network.DeleteCookies, network.DeleteCookiesParams{Name: "fichas-rail", URL: rig.Origin + "/"}) } return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: "fichas-rail", Value: state, URL: rig.Origin + "/"}) return err }) } func TestTheBarHasNoSearch(t *testing.T) { s := seedCards(t, 3) card := cardPath(s.ids[0]) for _, live := range []bool{false, true} { for _, width := range []int64{1440, 1000, 800, 390} { for _, path := range []string{"/t/t_customer", card, card + "/history", "/t/t_customer/fields"} { var shown int var html string s.rig.Run(scriptsOn(live), viewport(width), chromedp.Navigate(s.rig.Origin+path), chromedp.WaitReady(`[estilo-bar]`), harness.Into(&shown, chromedp.Evaluate[int](barSearch)), harness.Into(&html, chromedp.Evaluate[string](`document.documentElement.outerHTML`))) if shown != 0 { t.Errorf("%s at %dpx, scripts %v: the bar shows %d Search controls", path, width, live, shown) } if strings.Contains(html, "Search ⌘K") { t.Errorf("%s at %dpx, scripts %v: \"Search ⌘K\" is still drawn", path, width, live) } } } } s.rig.Run(scriptsOn(true)) for _, path := range []string{"/t/t_customer", card} { for _, width := range []int64{1440, 1000, 800, 390} { axeCheck(t, s.rig, path, width, "") } } } // The rail's Search, open and folded (by width, and by choice), scripts // on and off, from the list and from a card. func TestTheRailsSearchGoesToFind(t *testing.T) { s := seedCards(t, 3) card := cardPath(s.ids[0]) cases := []struct { width int64 cookie string open bool }{{1440, "", true}, {1000, "", false}, {1440, "rail", false}, {1000, "open", true}} for _, c := range cases { for _, live := range []bool{false, true} { name := fmt.Sprintf("%dpx, cookie %q, scripts %v", c.width, c.cookie, live) var facts struct { Label, Title, Keys, Kbd string LabelShown, KbdShown bool } var raw string s.rig.Run(scriptsOn(live), railCookie(s.rig, c.cookie), viewport(c.width), chromedp.Navigate(s.rig.Origin+"/t/t_customer"), chromedp.WaitVisible(railSearch), harness.Into(&raw, chromedp.Evaluate[string](`(function () { var a = document.querySelector('`+railSearch+`'); var label = a.querySelector('[fichas-rail-label]'), kbd = a.querySelector('kbd'); // Folded, the rail keeps the words for screen readers and clips them // to nothing. var seen = function (el) { var b = el.getBoundingClientRect(); return b.width > 1 && b.height > 1 && getComputedStyle(el).clipPath === 'none'; }; return JSON.stringify({Label: label.textContent, Title: a.title, Keys: a.getAttribute('aria-keyshortcuts'), Kbd: kbd.textContent, LabelShown: seen(label), KbdShown: seen(kbd)}); })()`))) if err := json.Unmarshal([]byte(raw), &facts); err != nil { t.Fatal(err) } if facts.Label != "Search" || facts.Title != "Search" || facts.Keys != "Meta+K Control+K" || facts.Kbd != "⌘K" { t.Errorf("%s: the rail's Search is %+v", name, facts) } if facts.LabelShown != c.open || facts.KbdShown != c.open { t.Errorf("%s: open %v, but the label shows %v and ⌘K %v", name, c.open, facts.LabelShown, facts.KbdShown) } // From the list: the Find box (with scripts, focus; without, // the link to #find). var at string var focused bool s.rig.Run(chromedp.Click(railSearch), chromedp.Sleep(300*time.Millisecond), harness.Into(&at, chromedp.Evaluate[string](whereAt)), harness.Into(&focused, chromedp.Evaluate[bool](findFocused))) if live && !focused { t.Errorf("%s: from the list the rail's Search did not focus the Find box (at %s)", name, at) } if !live && at != "/t/t_customer#find" { t.Errorf("%s: from the list the rail's Search went to %s", name, at) } // From a card, which has no Find box: the list's. s.rig.Run(chromedp.Navigate(s.rig.Origin+card), chromedp.WaitVisible(railSearch), chromedp.Click(railSearch), chromedp.WaitReady(`[data-live-search] input[name="q"]`), harness.Into(&at, chromedp.Evaluate[string](whereAt))) if at != "/t/t_customer#find" { t.Errorf("%s: from a card the rail's Search went to %s", name, at) } } } s.rig.Run(scriptsOn(true), railCookie(s.rig, "")) } // Below 800px Search is the Menu sheet's first row, once, on the list, // a card and History. func TestTheMenuSheetHasSearch(t *testing.T) { s := seedCards(t, 3) card := cardPath(s.ids[0]) const sheetSearch = `[estilo-menu="sheet"] a[href$="#find"]` for _, live := range []bool{false, true} { for _, path := range []string{"/t/t_customer", card, card + "/history"} { name := fmt.Sprintf("%s, scripts %v", path, live) var rows, at string var focused bool s.rig.Run(scriptsOn(live), viewport(390), chromedp.Navigate(s.rig.Origin+path), chromedp.WaitVisible("[estilo-bar-more] > summary"), chromedp.Click("[estilo-bar-more] > summary"), chromedp.WaitVisible(sheetSearch), harness.Into(&rows, chromedp.Evaluate[string](`Array.prototype.map.call(document.querySelectorAll('[estilo-menu="sheet"] [estilo-menu-item]'), function (row) { return row.textContent.trim(); }).join("|")`)), chromedp.Click(sheetSearch), chromedp.Sleep(300*time.Millisecond), chromedp.WaitReady(`[data-live-search] input[name="q"]`), harness.Into(&at, chromedp.Evaluate[string](whereAt)), harness.Into(&focused, chromedp.Evaluate[bool](findFocused))) if !strings.HasPrefix(rows, "Search|") || strings.Count("|"+rows+"|", "|Search|") != 1 { t.Errorf("%s: the Menu sheet's rows are %q", name, rows) } if live && path == "/t/t_customer" { if !focused { t.Errorf("%s: the sheet's Search did not focus the Find box", name) } } else if at != "/t/t_customer#find" { t.Errorf("%s: the sheet's Search went to %s", name, at) } } } s.rig.Run(scriptsOn(true)) } // ⌘K and Ctrl+K: the Find box on the list; from a card or History, the // list's Find box, at any width (below 800px too, where no rail is // drawn); and nothing from inside a field. func TestTheShortcutGoesToFind(t *testing.T) { s := seedCards(t, 3) card := cardPath(s.ids[0]) for _, mod := range []chromedp.Modifier{chromedp.ModifierMeta, chromedp.ModifierCtrl} { for _, width := range []int64{1440, 1000, 390} { name := fmt.Sprintf("%dpx, modifier %v", width, mod) var focused bool s.rig.Run(viewport(width), chromedp.Navigate(s.rig.Origin+"/t/t_customer"), chromedp.WaitVisible(`[data-live-search] input[name="q"]`), chromedp.Evaluate[chromedp.Void](`document.activeElement && document.activeElement.blur()`), chromedp.KeyEvent("k", chromedp.KeyModifiers(mod)), chromedp.Sleep(200*time.Millisecond), harness.Into(&focused, chromedp.Evaluate[bool](findFocused))) if !focused { t.Errorf("%s: on the list the shortcut did not focus the Find box", name) } for _, path := range []string{card, card + "/history"} { var at string s.rig.Run(chromedp.Navigate(s.rig.Origin+path), chromedp.WaitReady(`[fichas-main]`), chromedp.Evaluate[chromedp.Void](`document.activeElement && document.activeElement.blur()`), chromedp.KeyEvent("k", chromedp.KeyModifiers(mod)), chromedp.WaitVisible(`[data-live-search] input[name="q"]`), harness.Into(&at, chromedp.Evaluate[string](whereAt))) if at != "/t/t_customer#find" { t.Errorf("%s: the shortcut on %s went to %s", name, path, at) } } } // Inside a field it does nothing: typing is never left unsaved. var at string var stayed bool s.rig.Run(wide(), chromedp.Navigate(s.rig.Origin+card+"/edit"), chromedp.WaitReady(`[fichas-main] form`), chromedp.Evaluate[chromedp.Void](`Array.prototype.find.call(document.querySelectorAll('[fichas-main] input, [fichas-main] textarea'), function (el) { return !/^(hidden|radio|checkbox|submit)$/.test(el.type) && el.checkVisibility(); }).focus()`), chromedp.KeyEvent("k", chromedp.KeyModifiers(mod)), chromedp.Sleep(400*time.Millisecond), harness.Into(&at, chromedp.Evaluate[string](`location.pathname`)), harness.Into(&stayed, chromedp.Evaluate[bool](`document.activeElement.matches('[fichas-main] input, [fichas-main] textarea')`))) if at != card+"/edit" || !stayed { t.Errorf("modifier %v: the shortcut inside a field went to %s (focus kept: %v)", mod, at, stayed) } } } // shiftRecorder keeps every layout shift from the start of the load // that moved something in the bar (this gate's subject; the page body // is not), naming what moved. const shiftRecorder = `(function () { window.__shifts = []; try { new PerformanceObserver(function (list) { list.getEntries().forEach(function (entry) { (entry.sources || []).forEach(function (source) { var el = source.node && (source.node.nodeType === 1 ? source.node : source.node.parentElement); if (el && el.closest("[estilo-bar]")) __shifts.push(el.outerHTML.slice(0, 80) + " " + entry.value); }); }); }).observe({ type: "layout-shift", buffered: true }); } catch (error) {} })()` // No shift (Mapa's L5, for the bar): every control in the bar is where // the server drew it (scripts off) once the page's scripts have run, and // no layout shift moves anything in the bar from the start of the load, // on the list and on a card. func TestTheBarMovesNothing(t *testing.T) { s := seedCards(t, 3) 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(" "); }))` s.rig.Run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, page.AddScriptToEvaluateOnNewDocument, page.AddScriptToEvaluateOnNewDocumentParams{Source: shiftRecorder}) return err })) for _, path := range []string{"/t/t_customer", cardPath(s.ids[0])} { for _, width := range []int64{1440, 1000} { load := func(live bool) string { var boxes string s.rig.Run(scriptsOn(live), viewport(width), chromedp.Navigate(s.rig.Origin+path), chromedp.WaitReady(`[estilo-bar-end]`), chromedp.Sleep(300*time.Millisecond), harness.Into(&boxes, chromedp.Evaluate[string](bar))) return boxes } off, on := load(false), load(true) if off != on { t.Errorf("%s at %dpx: the bar moved once scripts ran:\noff %s\non %s", path, width, off, on) } var shifts string s.rig.Run(harness.Into(&shifts, chromedp.Evaluate[string](`JSON.stringify(window.__shifts || [])`))) if shifts != "[]" { t.Errorf("%s at %dpx: the bar shifted: %s", path, width, shifts) } } } s.rig.Run(scriptsOn(true)) }