| 1 | //go:build browser |
| 2 | |
| 3 | // The stacked bar's title field, fitted by chrome.js, in real engines. |
| 4 | // Not part of `make test`: it needs Node 22+ and Playwright with its |
| 5 | // browsers (ESTILO_PLAYWRIGHT names the playwright package directory |
| 6 | // when it is not resolvable from here; ESTILO_BROWSERS picks engines, |
| 7 | // default "chromium,webkit"). Run it with `make test-browser`. An engine |
| 8 | // that will not launch fails the test; it never skips to a pass. |
| 9 | // |
| 10 | // 2026-11-14: in Chromium the fitted field cut the title's last letter |
| 11 | // off ("Onboardin", "Brightwater Lab"), 12.8px short, both paddings, |
| 12 | // because Chromium measures an input's scroll width in its content box |
| 13 | // and WebKit in its padding box, and the node test's fake field was |
| 14 | // WebKit's. This is the check that would have caught it. |
| 15 | package chrome |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "encoding/json" |
| 20 | "fmt" |
| 21 | "net/http" |
| 22 | "net/http/httptest" |
| 23 | "os" |
| 24 | "os/exec" |
| 25 | "strings" |
| 26 | "testing" |
| 27 | |
| 28 | "amadan.net/oficina/estilo.git/base" |
| 29 | "amadan.net/oficina/estilo.git/menu" |
| 30 | "amadan.net/oficina/estilo.git/theme" |
| 31 | "github.com/carlosframework/rastrillo/ui" |
| 32 | ) |
| 33 | |
| 34 | // The page: the stacked bar as an app draws it, with the suite's |
| 35 | // stylesheets in their fixed order and chrome.js. ?title= is the title. |
| 36 | const titlePage = `<!doctype html> |
| 37 | <html lang="en"> |
| 38 | <head> |
| 39 | <meta charset="utf-8"> |
| 40 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 41 | <link rel="stylesheet" href="/tokens.css"> |
| 42 | <link rel="stylesheet" href="/theme.css"> |
| 43 | <link rel="stylesheet" href="/base.css"> |
| 44 | <link rel="stylesheet" href="/menu.css"> |
| 45 | <link rel="stylesheet" href="/chrome.css"> |
| 46 | <script defer src="/chrome.js"></script> |
| 47 | </head> |
| 48 | <body> |
| 49 | <div estilo-shell> |
| 50 | %s |
| 51 | <main id="main"></main> |
| 52 | </div> |
| 53 | </body> |
| 54 | </html> |
| 55 | ` |
| 56 | |
| 57 | type titleFit struct { |
| 58 | Engine string `json:"engine"` |
| 59 | Version string `json:"version"` |
| 60 | Error string `json:"error"` |
| 61 | Fields []struct { |
| 62 | Title string `json:"title"` |
| 63 | Viewport int `json:"viewport"` |
| 64 | Inline string `json:"inline"` // style.inlineSize, as chrome.js set it |
| 65 | Width float64 `json:"width"` // the field's border box |
| 66 | Content float64 `json:"content"` // its content box: the room the text has |
| 67 | Text float64 `json:"text"` // the title in the field's font, as a span draws it |
| 68 | Scroll int `json:"scroll"` // scrollWidth |
| 69 | Client int `json:"client"` // clientWidth |
| 70 | Capped bool `json:"capped"` // the bar's room, not the title, set the width |
| 71 | Narrowed bool `json:"narrowed"` // the control: the same field 12.8px (both paddings) narrower |
| 72 | Zero struct { |
| 73 | Scroll, Client, Offset int |
| 74 | } `json:"zero"` // what chrome.js reads, at zero width |
| 75 | } `json:"fields"` |
| 76 | } |
| 77 | |
| 78 | func TestStackedTitleFitsItsTitle(t *testing.T) { |
| 79 | if _, err := exec.LookPath("node"); err != nil { |
| 80 | t.Fatal("node is required (Node 22+)") |
| 81 | } |
| 82 | view := parse(t) |
| 83 | mux := http.NewServeMux() |
| 84 | mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { |
| 85 | b := stacked() |
| 86 | b.ContextEdit.Value = r.URL.Query().Get("title") |
| 87 | var bar bytes.Buffer |
| 88 | if err := view.ExecuteTemplate(&bar, "estilo-bar", b); err != nil { |
| 89 | t.Error(err) |
| 90 | } |
| 91 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 92 | w.Header().Set("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:") |
| 93 | fmt.Fprintf(w, titlePage, bar.String()) |
| 94 | }) |
| 95 | assets := map[string][]byte{"/tokens.css": ui.TokensCSS(), "/theme.css": theme.CSS(), "/base.css": base.CSS(), "/menu.css": menu.CSS(), "/chrome.css": CSS(), "/chrome.js": JS()} |
| 96 | for path, body := range assets { |
| 97 | mux.HandleFunc("GET "+path, func(w http.ResponseWriter, r *http.Request) { |
| 98 | ct := "text/css; charset=utf-8" |
| 99 | if strings.HasSuffix(path, ".js") { |
| 100 | ct = "text/javascript; charset=utf-8" |
| 101 | } |
| 102 | w.Header().Set("Content-Type", ct) |
| 103 | _, _ = w.Write(body) |
| 104 | }) |
| 105 | } |
| 106 | srv := httptest.NewServer(mux) |
| 107 | t.Cleanup(srv.Close) |
| 108 | |
| 109 | engines := strings.Split(os.Getenv("ESTILO_BROWSERS"), ",") |
| 110 | if engines[0] == "" { |
| 111 | engines = []string{"chromium", "webkit"} |
| 112 | } |
| 113 | job, _ := json.Marshal(map[string]any{ |
| 114 | "origin": srv.URL, |
| 115 | "engines": engines, |
| 116 | "viewports": []int{1440, 390}, |
| 117 | // Short, the two titles the bug was found on, a sentence, wide |
| 118 | // and narrow Latin, CJK, and Latin with accents and a dash. |
| 119 | "titles": []string{"Q3", "Onboarding", "Brightwater Labs", "Untitled document", "WWWWWWWWWW", "illiterate fill", "A considerably longer document title for the quarter", "四半期の計画書", "Résumé — draft"}, |
| 120 | }) |
| 121 | cmd := exec.Command("node", "titlefit.browser.mjs", string(job)) |
| 122 | cmd.Stderr = os.Stderr |
| 123 | raw, err := cmd.Output() |
| 124 | if err != nil { |
| 125 | t.Fatalf("node titlefit.browser.mjs: %v", err) |
| 126 | } |
| 127 | var results []titleFit |
| 128 | if err := json.Unmarshal(raw, &results); err != nil { |
| 129 | t.Fatalf("driver output: %v\n%s", err, raw) |
| 130 | } |
| 131 | if len(results) != len(engines) { |
| 132 | t.Fatalf("%d engines asked for, %d reported", len(engines), len(results)) |
| 133 | } |
| 134 | for _, r := range results { |
| 135 | if r.Error != "" { |
| 136 | t.Errorf("%s did not launch: %s", r.Engine, r.Error) |
| 137 | continue |
| 138 | } |
| 139 | fitted := 0 |
| 140 | for _, f := range r.Fields { |
| 141 | where := fmt.Sprintf("%s %s, %dpx, %q", r.Engine, r.Version, f.Viewport, f.Title) |
| 142 | t.Logf("%s: %s; content %.2f for text %.2f; scroll %d, client %d; at zero width scroll %d, client %d, offset %d; capped %v", where, f.Inline, f.Content, f.Text, f.Scroll, f.Client, f.Zero.Scroll, f.Zero.Client, f.Zero.Offset, f.Capped) |
| 143 | if f.Inline == "" { |
| 144 | t.Errorf("%s: chrome.js did not fit the field", where) |
| 145 | continue |
| 146 | } |
| 147 | // The detector must see a field that is short by exactly what |
| 148 | // the bug cost, or a pass means nothing. |
| 149 | if !f.Narrowed { |
| 150 | t.Errorf("%s: the same field 12.8px narrower was not seen as cut off; the check is blind", where) |
| 151 | } |
| 152 | if f.Capped { |
| 153 | continue // a title longer than the room scrolls inside the field, by design |
| 154 | } |
| 155 | fitted++ |
| 156 | // The engine's own verdict: nothing to scroll to, so nothing cut off. |
| 157 | if f.Scroll > f.Client { |
| 158 | t.Errorf("%s: the title overflows its field (scroll width %d, client width %d): its end is cut off", where, f.Scroll, f.Client) |
| 159 | } |
| 160 | // And the text's own measure fits the room the field gives it. |
| 161 | if f.Content+0.01 < f.Text { |
| 162 | t.Errorf("%s: the field's content box is %.2fpx for a title %.2fpx wide: its end is cut off", where, f.Content, f.Text) |
| 163 | } |
| 164 | // And as wide as its title, not the bar's room: a caret and |
| 165 | // the rounding over the text, never a field's worth. |
| 166 | if f.Content > f.Text+24 { |
| 167 | t.Errorf("%s: the field's content box is %.2fpx for a title %.2fpx wide: not fitted to it", where, f.Content, f.Text) |
| 168 | } |
| 169 | } |
| 170 | if fitted < len(r.Fields)/2 { |
| 171 | t.Errorf("%s: only %d of %d fields were fitted to their title rather than capped", r.Engine, fitted, len(r.Fields)) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |