| 1 | package chrome |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "flag" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/estilo.git/menu" |
| 14 | ) |
| 15 | |
| 16 | var update = flag.Bool("update", false, "rewrite testdata/unchanged-*.html from the current templates") |
| 17 | |
| 18 | // unchangedBars are bars with neither Menubar nor Lead set. Their markup |
| 19 | // was recorded from main at f6d5605, before the slots existed |
| 20 | // (testdata/unchanged-*.html), and must stay byte for byte what it was: |
| 21 | // the slots are opt-in, so an app that sets neither sees nothing move. |
| 22 | // |
| 23 | // The account's Appearance group is left out on purpose. It changed for |
| 24 | // every app on bar-stack (the segmented selector, switched in place), and |
| 25 | // TestAppearanceIsASegmentedSelectorSwitchedInPlace holds that. |
| 26 | func unchangedBars() map[string]any { |
| 27 | full := signedIn("x") |
| 28 | full.Account.Appearance = nil |
| 29 | sticky := signedIn("x") |
| 30 | sticky.Account.Appearance = nil |
| 31 | sticky.Sticky = true |
| 32 | dialog := signedIn("x") |
| 33 | dialog.Account.Appearance = nil |
| 34 | dialog.Actions = []Action{{Item: menu.Item{Dialog: "help", Label: "x-help"}}, {Item: menu.Item{Href: "/n", Label: "x-new"}, Primary: true}} |
| 35 | return map[string]any{ |
| 36 | "signed-in": full, |
| 37 | "sticky": sticky, |
| 38 | "dialog": dialog, |
| 39 | "public": Bar{App: App{Name: "Docs", Icon: "/i.svg"}, Context: "A document"}, |
| 40 | "signed-out": Bar{App: App{Name: "Docs", Home: "/", Icon: "/i.svg"}, SignIn: &Link{Href: "/signin?return_to=%2Fd%2Fabc", Label: "Sign in"}}, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestABarWithoutSlotsIsByteForByteUnchanged(t *testing.T) { |
| 45 | for name, bar := range unchangedBars() { |
| 46 | t.Run(name, func(t *testing.T) { |
| 47 | got := render(t, "estilo-bar", bar) |
| 48 | path := filepath.Join("testdata", "unchanged-"+name+".html") |
| 49 | if *update { |
| 50 | if err := os.MkdirAll("testdata", 0o755); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if err := os.WriteFile(path, []byte(got), 0o644); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | return |
| 57 | } |
| 58 | want, err := os.ReadFile(path) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if got != string(want) { |
| 63 | t.Errorf("a bar with no slots changed its markup.\ngot:\n%s\nwant:\n%s", got, want) |
| 64 | } |
| 65 | }) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // barStackMarker opens chrome.css's opt-in section: everything bar-stack |
| 70 | // added sits after it, and main's rules before it are untouched. |
| 71 | const barStackMarker = "\n/* ── Bar stack" |
| 72 | |
| 73 | // mainChromeCSS is the sha256 of chrome.css before the bar-stack marker, |
| 74 | // and mainMenuCSS of menu.css; both were first recorded from main at |
| 75 | // f6d5605, when bar-stack began. A change to the bar's own rules for every |
| 76 | // app is a design decision: make it deliberately, and update these with |
| 77 | // the reason in the commit. chrome.css's was last updated on |
| 78 | // feat/smart-door, for the door's rules only (the footer slot removed; |
| 79 | // the smart door and the signed-out page's Forget added), and again on |
| 80 | // feat/door-pills, for the door's rules only (the door's buttons drawn as |
| 81 | // pills, Paul 2026-11-21): no bar rule changed. Both were updated on |
| 82 | // feat/stack-rhythm (Paul 2026-11-22, "Release and start the rollout"): |
| 83 | // every margin, padding and gap on the spacing scale, the door's card a |
| 84 | // stack. The bar and its menus move by a pixel or two (the trigger's |
| 85 | // padding 0.4rem to 0.5rem, a menu row's 0.4rem to 0.25rem and 0.65rem |
| 86 | // to 0.75rem), and nothing else in their rules changed; the door's card |
| 87 | // became a stack, its rules all keyed on estilo-signin. chrome.css's |
| 88 | // again on fix/door-stack-lows, for the door's card only: its gap is the |
| 89 | // table's smallest step as in Fiesta's stack, so a hidden child leaves no |
| 90 | // relation touching, and the card takes a hint and a section hook. Both |
| 91 | // again on feat/phone-compact-bar (Paul 2026-11-28, the phone web design, |
| 92 | // approved with its six answers): on a coarse pointer every bar trigger |
| 93 | // is 44px (--estilo-trigger-size 2.75rem) and every menu row 44px, and on |
| 94 | // a touch screen that cannot hover a menu shows no shortcut hints; a menu |
| 95 | // row may carry an icon ([estilo-menu-icon]); and, after Fable's review |
| 96 | // of c56314e, the rail's account row and every menubar title are 44px on |
| 97 | // a coarse pointer too. Nothing changed for a pointer that hovers. |
| 98 | const ( |
| 99 | mainChromeCSS = "52435a1fee808fa6d20a1f32e823da9cf7eaac6a762963d45281361d316fea2c" |
| 100 | mainMenuCSS = "a76fb85446799156a2debeb263bd6bfb4a57a358183e05d441d1f90953613896" |
| 101 | ) |
| 102 | |
| 103 | // optInHooks are the attributes only a bar with a slot, a WideOnly or |
| 104 | // IconOnly action, the Appearance form or a compact bar renders. |
| 105 | var optInHooks = []string{ |
| 106 | `[estilo-bar~="stacked"]`, "[estilo-bar-stack]", "[estilo-bar-menubar]", "[estilo-bar-lead]", |
| 107 | "[estilo-bar-control", "[estilo-bar-wide]", "[estilo-appearance", "[estilo-bar-icon]", |
| 108 | `[estilo-bar~="compact"]`, "[estilo-bar-trail]", "[estilo-bar-back]", "[estilo-bar-compact-title]", "[estilo-bar-tool]", |
| 109 | } |
| 110 | |
| 111 | // A bar with no slot must look as it did, not only read as it did: main's |
| 112 | // stylesheet rules are byte for byte where they were, and every rule |
| 113 | // bar-stack added is keyed on an opt-in hook, in every selector of its |
| 114 | // list. (Appearance is the deliberate exception that every app takes: |
| 115 | // its hook is the form's own attribute.) |
| 116 | func TestEverythingAfterTheMarkerIsOptIn(t *testing.T) { |
| 117 | src := string(CSS()) |
| 118 | at := strings.Index(src, barStackMarker) |
| 119 | if at < 0 { |
| 120 | t.Fatal("chrome.css lost its bar-stack marker") |
| 121 | } |
| 122 | if sum := sha256.Sum256([]byte(src[:at])); hex.EncodeToString(sum[:]) != mainChromeCSS { |
| 123 | t.Error("main's chrome.css rules changed: every app's bar would look different") |
| 124 | } |
| 125 | if sum := sha256.Sum256(menu.CSS()); hex.EncodeToString(sum[:]) != mainMenuCSS { |
| 126 | t.Error("menu.css changed: every app's menus would look different") |
| 127 | } |
| 128 | section := regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(src[at:], "") |
| 129 | n := 0 |
| 130 | for _, m := range regexp.MustCompile(`([^{};]+)\{`).FindAllStringSubmatch(section, -1) { |
| 131 | list := strings.TrimSpace(m[1]) |
| 132 | if strings.HasPrefix(list, "@") { |
| 133 | continue |
| 134 | } |
| 135 | for _, sel := range splitSelectors(list) { |
| 136 | n++ |
| 137 | hooked := false |
| 138 | for _, h := range optInHooks { |
| 139 | hooked = hooked || strings.Contains(sel, h) |
| 140 | } |
| 141 | if !hooked { |
| 142 | t.Errorf("%q styles every bar, not only one that opted in", sel) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | if n < 20 { |
| 147 | t.Fatalf("found only %d selectors after the marker; the check is not reading the section", n) |
| 148 | } |
| 149 | } |
| 150 | |