package chrome import ( "html/template" "regexp" "strings" "testing" "amadan.net/oficina/estilo.git/menu" ) // What Docs puts in the compact slots on a phone: its Aa and ••• at the // inline end, and its inline title (shown once its large title has // scrolled away). Markup it rendered itself. const ( testTrail = template.HTML(`
`) testTitle = template.HTML(`x-inline-title`) ) func compact() Bar { b := stacked() b.Lead = testLead b.Compact = &Compact{Trail: testTrail} b.Nav = nil // Docs has none; a compact bar with nav keeps a sheet for it (TestACompactBarWithNavKeepsItsSheet) return b } // narrowQuery is where a compact bar is the phone's one row: below 704px, // or a phone held sideways. bandQuery is where a compact bar, and only a // compact bar, is already the 56px desktop header that every other // stacked bar starts at 800px (Paul, 2026-11-28: the desktop header from // 704px for an iPad in portrait; Fable on c56314e: Mapa and Fichas stack // too, and their own layouts break at 800px). In px, as the 800px // breakpoint is, so the two never part at a reader's larger default font. // (bandQuery is chrome.go's.) const narrowQuery = "@media (max-width: 703.98px), (max-height: 480px) and (pointer: coarse)" // The Compact slot marks the bar compact and adds only what a compact bar // needs: the Back chevron in the brand, the app's trail in the end group // before the switcher and the account, and no Menu sheet, since the trail // carries the app's own overflow. func TestTheCompactSlotMarksTheBar(t *testing.T) { b := compact() html := render(t, "estilo-bar", b) if got := header(html); got != `
` { t.Errorf("a compact stacked bar opens %s", got) } b.Sticky = true if got := header(render(t, "estilo-bar", b)); got != `
` { t.Errorf("a sticky compact stacked bar opens %s", got) } plain := signedIn("x") plain.Compact = &Compact{Trail: testTrail} if got := header(render(t, "estilo-bar", plain)); got != `
` { t.Errorf("a compact one-row bar opens %s", got) } brand := html[strings.Index(html, "")] if !strings.Contains(brand, `
"+string(testTrail)+"
") { t.Errorf("the trail is not drawn verbatim:\n%s", html) } order := []string{"
", "
", "
", "
"+string(testTitle)+"
") if at < 0 || at < strings.Index(html, "
") || at > strings.Index(html, "
") { t.Errorf("the compact title is not between the stack and the end group:\n%s", html) } // A public page's brand is plain text: no Back. pub := compact() pub.App.Home = "" if strings.Contains(render(t, "estilo-bar", pub), "estilo-bar-back") { t.Error("a brand that links nowhere drew a Back chevron") } } // Every rule a compact bar adds is keyed on a compact hook, and the // hooks are in the opt-in list (TestEverythingAfterTheMarkerIsOptIn). func TestTheCompactHooksAreOptIn(t *testing.T) { for _, h := range []string{`[estilo-bar~="compact"]`, "[estilo-bar-trail]", "[estilo-bar-back]", "[estilo-bar-compact-title]", "[estilo-bar-tool]"} { found := false for _, o := range optInHooks { found = found || o == h } if !found { t.Errorf("%s is not an opt-in hook", h) } } } // Below 44rem a compact bar is the phone's one 52px row: Back, the // title, the lead and the trail. The menus, the actions, the switcher and // the account go (the trail and the app's other screens carry them), and // the published block size is the row's. func TestTheCompactBarIsOneRowBelow704px(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, barStackMarker):] at := strings.Index(src, "/* ── Compact") if at < 0 { t.Fatal("chrome.css has no compact section") } section := src[at:] narrow := section[strings.Index(section, narrowQuery):] narrow = narrow[:strings.Index(narrow, "\n}\n")] for _, want := range []string{ `:root:has([estilo-bar~="compact"]) { --estilo-bar-block-size: 3.25rem; }`, `[estilo-bar~="compact"] [estilo-bar-menubar],`, `[estilo-bar~="compact"] [estilo-bar-actions],`, `[estilo-bar~="compact"] [estilo-switcher],`, `[estilo-bar~="compact"] [estilo-account] { display: none; }`, `[estilo-bar~="compact"] [estilo-bar-trail] { display: flex;`, `[estilo-bar~="compact"] [estilo-bar-compact-title] { display: block;`, `[estilo-bar~="compact"] [estilo-bar-back] { display: block;`, } { if !strings.Contains(narrow, want) { t.Errorf("the compact narrow block lacks %s", want) } } // The trail's panel is a drop-down from the inline end (Paul's answer // 3), sized for a finger: 44px rows, no shortcut hints. for _, re := range []string{ `\[estilo-bar-trail\] \[estilo-menu\] \{[^}]*position: absolute;[^}]*inset-inline-end: var\(--rst-sp-2\);`, `\[estilo-bar-trail\] \[estilo-menu-item\] \{[^}]*min-block-size: 44px;`, `\[estilo-bar-trail\] \[estilo-menu-key\] \{ display: none; \}`, `\[estilo-bar-tool\] \{[^}]*min-inline-size: 44px;[^}]*block-size: 44px;`, } { if !regexp.MustCompile(re).MatchString(section) { t.Errorf("the compact section lacks %s", re) } } // The editable title fills its room and ends in an ellipsis there, // instead of being cut mid-letter (audit finding 2). if !regexp.MustCompile(`\[estilo-bar~="compact"\] \[estilo-bar-title-input\] \{[^}]*inline-size: 100%;[^}]*text-overflow: ellipsis;`).MatchString(narrow) { t.Error("the compact title field does not fill its room with an ellipsis") } // From 44rem the trail and the compact title are gone. if !regexp.MustCompile(`\n:is\(\[estilo-bar-trail\], \[estilo-bar-compact-title\], \[estilo-bar-back\]\) \{ display: none; \}`).MatchString(section) { t.Error("the trail, compact title and Back are not hidden by default") } } // chrome.js leaves a compact title unfitted where the compact bar is // narrow, on the CSS's own query, and publishes a stacked bar's narrow // height below 800px, as it always has. func TestTheScriptUsesTheCSSBreakpoints(t *testing.T) { js := string(JS()) q := strings.TrimPrefix(narrowQuery, "@media ") if !strings.Contains(js, "matchMedia('"+q+"')") { t.Errorf("chrome.js does not read the compact query %q", q) } if !strings.Contains(js, "matchMedia('(max-width: 799.98px)')") { t.Error("chrome.js no longer publishes the stacked bar's height below 800px") } if !strings.Contains(js, `(bar) => (' ' + bar.getAttribute('estilo-bar') + ' ').indexOf(' compact ') < 0)`) { t.Error("chrome.js publishes a compact bar's measured height, which holds it at 52px past 704px") } css := string(CSS()) if !strings.Contains(css, narrowQuery+" {") || !strings.Contains(css, bandQuery+" {") { t.Error("chrome.css does not use the compact pair") } if strings.Contains(css, "43.99rem") || strings.Contains(css, "(min-width: 44rem)") { t.Error("a rem breakpoint is left; at a larger default font it parts from 800px") } } // Touch: every bar trigger is a 44px target on a coarse pointer, in every // app (audit finding 9). The stacked header keeps its 32px look and takes // 44px of hit area. func TestTouchTargetsAre44px(t *testing.T) { src := string(CSS()) main := src[:strings.Index(src, barStackMarker)] if !regexp.MustCompile(`@media \(pointer: coarse\) \{\s*:root \{ --estilo-trigger-size: 2\.75rem; \}`).MatchString(main) { t.Error("the bar's triggers are not 44px on a coarse pointer") } stack := src[strings.Index(src, barStackMarker):] wide := stack[strings.Index(stack, "@media (min-width: 800px) {"):] coarse := wide[strings.Index(wide, "@media (pointer: coarse) {"):] coarse = coarse[:strings.Index(coarse, "\n }\n")] for _, want := range []string{ `[estilo-bar~="stacked"] [estilo-menubar-title]::before { content: ""; position: absolute; inset-inline: 0; inset-block: -16px -4px; }`, `[estilo-bar~="stacked"] [estilo-menu-item] { min-block-size: 44px; }`, `:is([estilo-bar-control] > a, [estilo-bar-control] > button, [estilo-bar-control] > form > button)::before { content: ""; position: absolute; inset: -6px -4px; }`, } { if !strings.Contains(coarse, want) { t.Errorf("the stacked header's coarse rules lack %s", want) } } } // Only a compact bar moves to 704px. Every other stacked bar (Mapa and // Fichas set Menubar, Lead and Nav, and their layouts break at 800px) // keeps main's rules: the 56px header from 800px, the two-row bar and its // Menu sheet below. Between 704px and 800px a compact bar gets the very // same 56px header, a copy of that block CSS() makes at init with every // selector scoped to a compact bar, so the two can never drift. func TestOnlyACompactBarMovesTo704px(t *testing.T) { src := string(CSS()) at := strings.Index(src, bandQuery+" {") if at < 0 { t.Fatal("no 704px band for a compact bar") } if strings.Contains(string(css), `[estilo-bar~="stacked"][estilo-bar~="compact"]) { --estilo-bar-block-size: 56px; }`) { t.Error("the band's copy is written by hand in chrome.css; it must be the generated one") } wide := regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(wideBlock(string(css)), "") band := block(src, at+len(bandQuery)+2) rules := func(block string) []string { var out []string for _, m := range regexp.MustCompile(`([^{};]+)\{`).FindAllStringSubmatch(block, -1) { if l := strings.TrimSpace(m[1]); !strings.HasPrefix(l, "@") { out = append(out, l) } } return out } w, b := rules(wide), rules(band) if len(w) < 40 || len(w) != len(b) { t.Fatalf("the band has %d rules for the wide block's %d", len(b), len(w)) } for i, list := range b { for _, sel := range splitSelectors(list) { if !strings.Contains(sel, `[estilo-bar~="compact"]`) { t.Errorf("band rule %d, %q, reaches a bar that is not compact (from %q)", i, sel, w[i]) } } } // The declarations are the wide block's, verbatim. decls := func(block string) string { return regexp.MustCompile(`[^{};]+\{`).ReplaceAllString(block, "{") } if strings.Join(strings.Fields(decls(wide)), " ") != strings.Join(strings.Fields(decls(band)), " ") { t.Error("the band's declarations are not the wide block's") } // The scope adds no specificity (:where), so the cascade around the // copy is the original's; a pseudo-element stays outside :is(). if strings.Contains(band, `):is([estilo-bar~="compact"]`) { t.Error("the band's scope adds specificity; the forced-colors border would lose to the controls' border: 0") } if !strings.Contains(band, `:is([estilo-bar~="stacked"] [estilo-menubar-title]):where([estilo-bar~="compact"], [estilo-bar~="compact"] *)::before`) { t.Error("the band does not keep ::before outside :is()") } if !strings.Contains(band, `:root:has([estilo-bar~="stacked"][estilo-bar~="compact"]) { --estilo-bar-block-size: 56px; }`) { t.Error("the band does not publish the compact bar's 56px") } // Nothing written by hand for the band reaches a bar without Compact. hand := src[strings.Index(src, "/* Between 704px and 800px"):] hand = regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(block(hand, strings.Index(hand, "{")+1), "") for _, list := range rules(hand) { for _, sel := range splitSelectors(list) { if !strings.Contains(sel, `[estilo-bar~="compact"]`) { t.Errorf("%q, in the band, reaches a bar that is not compact", sel) } } } // The Rename pill stays in the title row in the band, where the narrow // rule would float it under the title. pill := regexp.MustCompile(`\[estilo-bar~="stacked"\] \[estilo-bar-title\]:focus-within \[estilo-bar-title-submit\] \{[^}]*\}`).FindString(wide) if !strings.Contains(pill, "position: static;") { t.Errorf("the wide Rename pill can still float under the title:\n%s", pill) } } // A compact bar with Nav draws the Menu sheet (with the nav and the // actions) and shows it in the compact row, so the nav is never // unreachable; without Nav the trail is the only overflow. func TestACompactBarWithNavKeepsItsSheet(t *testing.T) { b := compact() b.Nav = []Link{{Href: "/places", Label: "x-place"}} html := render(t, "estilo-bar", b) sheet := html[strings.Index(html, "
")] if !strings.Contains(sheet, "x-place") || !strings.Contains(sheet, "x-share") { t.Errorf("a compact bar with nav has no sheet holding it:\n%s", html) } if strings.Index(html, "
strings.Index(html, "
") { t.Error("the sheet is not before the trail") } b.Nav = nil if strings.Contains(render(t, "estilo-bar", b), "estilo-bar-more") { t.Error("a compact bar without nav drew the sheet") } src := string(CSS()) narrow := src[strings.Index(src, "/* ── Compact"):] narrow = narrow[strings.Index(narrow, narrowQuery):] if !strings.Contains(narrow[:strings.Index(narrow, "\n}\n")], `[estilo-bar~="compact"] [estilo-bar-more] { display: block; }`) { t.Error("the compact row does not show a compact bar's sheet") } } // Folded, Back is its chevron alone, still a 44px target (Fable, item 2). func TestFoldedBackIsA44pxTarget(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, "/* ── Compact"):] if !regexp.MustCompile(`\[estilo-bar~="compact"\]\[estilo-bar-folded\]:has\(> \[estilo-bar-compact-title\]\) \[estilo-bar-brand\] \{[^}]*min-inline-size: 44px;[^}]*justify-content: center;`).MatchString(src) { t.Error("folded Back is not a centred 44px target") } } // A public page's brand is plain text, not a link: it is not drawn in the // link's accent (Fable, item 4). func TestOnlyALinkedBrandTakesTheAccent(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, "/* ── Compact"):] for _, m := range regexp.MustCompile(`([^{};]+)\{([^}]*)\}`).FindAllStringSubmatch(src, -1) { if !strings.Contains(m[2], "var(--rst-accent)") || !strings.Contains(m[1], "[estilo-bar-brand]") { continue } for _, sel := range splitSelectors(strings.TrimSpace(m[1])) { if strings.HasSuffix(sel, "[estilo-bar-brand]") && !strings.HasSuffix(sel, "a[estilo-bar-brand]") { t.Errorf("%q draws a plain-text brand in the accent", sel) } } } } // On a coarse pointer the rail's account row and every menubar title // outside the wide stacked header are 44px too (Fable, item 5), and the // narrow stacked bar publishes the taller menu row. func TestTheRemainingTouchTargetsAre44px(t *testing.T) { src := string(CSS()) main := src[:strings.Index(src, barStackMarker)] if !regexp.MustCompile(`@media \(pointer: coarse\) \{[^}]*\[estilo-account~="rail"\] > \[estilo-bar-trigger\] \{ min-block-size: 2\.75rem; \}`).MatchString(main) { t.Error("the rail's account row is not 44px on touch") } if !regexp.MustCompile(`@media \(pointer: coarse\) \{[^@]*\[estilo-menubar-title\] \{ min-block-size: 2\.75rem; \}`).MatchString(string(menu.CSS())) { t.Error("menubar titles are not 44px on touch") } if !strings.Contains(src, "@media (max-width: 799.98px) and (pointer: coarse) {\n :root:has([estilo-bar~=\"stacked\"]) { --estilo-bar-block-size: calc(3.25rem + 2.75rem + 1px); }") { t.Error("the narrow stacked bar does not publish its 44px menu row on touch") } } // A compact bar fits 360px with room for the title: Back, the lead and a // two-button trail are 300px in the mock-up, and the bar's own padding // and gaps are the small steps. func TestTheCompactBarUsesTheSmallSteps(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, "/* ── Compact"):] if !regexp.MustCompile(`\[estilo-bar~="compact"\] \{[^}]*gap: var\(--rst-sp-1\);[^}]*padding-inline: var\(--rst-sp-2\);`).MatchString(src) { t.Error("the compact bar is not laid out on the small steps") } } // The menu's Icon is drawn in the compact trail's rows (and any menu that // passes one); a row without one is byte for byte what it was. func TestTheCompactTrailCanDrawIconRows(t *testing.T) { got := render(t, "estilo-menu-item", menu.Item{Href: "/s", Label: "x-share", Icon: ``}) if got != `x-share` { t.Errorf("an icon row: %s", got) } } // With Compact.Title the bar is the large-title idiom: the compact title // shows only once the app marks the bar folded, and then Back is its // chevron, its name visually hidden but still its accessible name. func TestACompactTitleShowsOnlyWhenFolded(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, "/* ── Compact"):] for _, want := range []string{ `[estilo-bar~="compact"] [estilo-bar-compact-title] { opacity: 0; visibility: hidden;`, `[estilo-bar~="compact"][estilo-bar-folded] [estilo-bar-compact-title] { opacity: 1; visibility: visible;`, `[estilo-bar~="compact"][estilo-bar-folded]:has(> [estilo-bar-compact-title]) [estilo-bar-name] { position: absolute; clip-path: inset(50%);`, `[estilo-bar~="compact"]:has(> [estilo-bar-compact-title]) { border-block-end-color: transparent; }`, } { if !strings.Contains(src, want) { t.Errorf("the compact section lacks %s", want) } } if strings.Contains(src, `[estilo-bar-folded]:has(> [estilo-bar-compact-title]) [estilo-bar-name] { display: none`) { t.Error("folded, Back would lose its accessible name") } } // block is the body of the block whose opening brace is just before // start, up to its matching closing brace. func block(src string, start int) string { depth := 1 for i := start; i < len(src); i++ { switch src[i] { case '{': depth++ case '}': if depth--; depth == 0 { return src[start:i] } } } return src[start:] }