package chrome import ( "crypto/sha256" "encoding/hex" "flag" "os" "path/filepath" "regexp" "strings" "testing" "amadan.net/oficina/estilo.git/menu" ) var update = flag.Bool("update", false, "rewrite testdata/unchanged-*.html from the current templates") // unchangedBars are bars with neither Menubar nor Lead set. Their markup // was recorded from main at f6d5605, before the slots existed // (testdata/unchanged-*.html), and must stay byte for byte what it was: // the slots are opt-in, so an app that sets neither sees nothing move. // // The account's Appearance group is left out on purpose. It changed for // every app on bar-stack (the segmented selector, switched in place), and // TestAppearanceIsASegmentedSelectorSwitchedInPlace holds that. func unchangedBars() map[string]any { full := signedIn("x") full.Account.Appearance = nil sticky := signedIn("x") sticky.Account.Appearance = nil sticky.Sticky = true dialog := signedIn("x") dialog.Account.Appearance = nil dialog.Actions = []Action{{Item: menu.Item{Dialog: "help", Label: "x-help"}}, {Item: menu.Item{Href: "/n", Label: "x-new"}, Primary: true}} return map[string]any{ "signed-in": full, "sticky": sticky, "dialog": dialog, "public": Bar{App: App{Name: "Docs", Icon: "/i.svg"}, Context: "A document"}, "signed-out": Bar{App: App{Name: "Docs", Home: "/", Icon: "/i.svg"}, SignIn: &Link{Href: "/signin?return_to=%2Fd%2Fabc", Label: "Sign in"}}, } } func TestABarWithoutSlotsIsByteForByteUnchanged(t *testing.T) { for name, bar := range unchangedBars() { t.Run(name, func(t *testing.T) { got := render(t, "estilo-bar", bar) path := filepath.Join("testdata", "unchanged-"+name+".html") if *update { if err := os.MkdirAll("testdata", 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(got), 0o644); err != nil { t.Fatal(err) } return } want, err := os.ReadFile(path) if err != nil { t.Fatal(err) } if got != string(want) { t.Errorf("a bar with no slots changed its markup.\ngot:\n%s\nwant:\n%s", got, want) } }) } } // barStackMarker opens chrome.css's opt-in section: everything bar-stack // added sits after it, and main's rules before it are untouched. const barStackMarker = "\n/* ── Bar stack" // mainChromeCSS is the sha256 of chrome.css before the bar-stack marker, // and mainMenuCSS of menu.css; both were first recorded from main at // f6d5605, when bar-stack began. A change to the bar's own rules for every // app is a design decision: make it deliberately, and update these with // the reason in the commit. chrome.css's was last updated on // feat/smart-door, for the door's rules only (the footer slot removed; // the smart door and the signed-out page's Forget added), and again on // feat/door-pills, for the door's rules only (the door's buttons drawn as // pills, Paul 2026-11-21): no bar rule changed. Both were updated on // feat/stack-rhythm (Paul 2026-11-22, "Release and start the rollout"): // every margin, padding and gap on the spacing scale, the door's card a // stack. The bar and its menus move by a pixel or two (the trigger's // padding 0.4rem to 0.5rem, a menu row's 0.4rem to 0.25rem and 0.65rem // to 0.75rem), and nothing else in their rules changed; the door's card // became a stack, its rules all keyed on estilo-signin. chrome.css's // again on fix/door-stack-lows, for the door's card only: its gap is the // table's smallest step as in Fiesta's stack, so a hidden child leaves no // relation touching, and the card takes a hint and a section hook. Both // again on feat/phone-compact-bar (Paul 2026-11-28, the phone web design, // approved with its six answers): on a coarse pointer every bar trigger // is 44px (--estilo-trigger-size 2.75rem) and every menu row 44px, and on // a touch screen that cannot hover a menu shows no shortcut hints; a menu // row may carry an icon ([estilo-menu-icon]); and, after Fable's review // of c56314e, the rail's account row and every menubar title are 44px on // a coarse pointer too. Nothing changed for a pointer that hovers. const ( mainChromeCSS = "52435a1fee808fa6d20a1f32e823da9cf7eaac6a762963d45281361d316fea2c" mainMenuCSS = "a76fb85446799156a2debeb263bd6bfb4a57a358183e05d441d1f90953613896" ) // optInHooks are the attributes only a bar with a slot, a WideOnly or // IconOnly action, the Appearance form or a compact bar renders. var optInHooks = []string{ `[estilo-bar~="stacked"]`, "[estilo-bar-stack]", "[estilo-bar-menubar]", "[estilo-bar-lead]", "[estilo-bar-control", "[estilo-bar-wide]", "[estilo-appearance", "[estilo-bar-icon]", `[estilo-bar~="compact"]`, "[estilo-bar-trail]", "[estilo-bar-back]", "[estilo-bar-compact-title]", "[estilo-bar-tool]", } // A bar with no slot must look as it did, not only read as it did: main's // stylesheet rules are byte for byte where they were, and every rule // bar-stack added is keyed on an opt-in hook, in every selector of its // list. (Appearance is the deliberate exception that every app takes: // its hook is the form's own attribute.) func TestEverythingAfterTheMarkerIsOptIn(t *testing.T) { src := string(CSS()) at := strings.Index(src, barStackMarker) if at < 0 { t.Fatal("chrome.css lost its bar-stack marker") } if sum := sha256.Sum256([]byte(src[:at])); hex.EncodeToString(sum[:]) != mainChromeCSS { t.Error("main's chrome.css rules changed: every app's bar would look different") } if sum := sha256.Sum256(menu.CSS()); hex.EncodeToString(sum[:]) != mainMenuCSS { t.Error("menu.css changed: every app's menus would look different") } section := regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(src[at:], "") n := 0 for _, m := range regexp.MustCompile(`([^{};]+)\{`).FindAllStringSubmatch(section, -1) { list := strings.TrimSpace(m[1]) if strings.HasPrefix(list, "@") { continue } for _, sel := range splitSelectors(list) { n++ hooked := false for _, h := range optInHooks { hooked = hooked || strings.Contains(sel, h) } if !hooked { t.Errorf("%q styles every bar, not only one that opted in", sel) } } } if n < 20 { t.Fatalf("found only %d selectors after the marker; the check is not reading the section", n) } }