//go:build browser
// The stacked bar's title field, fitted by chrome.js, in real engines.
// Not part of `make test`: it needs Node 22+ and Playwright with its
// browsers (ESTILO_PLAYWRIGHT names the playwright package directory
// when it is not resolvable from here; ESTILO_BROWSERS picks engines,
// default "chromium,webkit"). Run it with `make test-browser`. An engine
// that will not launch fails the test; it never skips to a pass.
//
// 2026-11-14: in Chromium the fitted field cut the title's last letter
// off ("Onboardin", "Brightwater Lab"), 12.8px short, both paddings,
// because Chromium measures an input's scroll width in its content box
// and WebKit in its padding box, and the node test's fake field was
// WebKit's. This is the check that would have caught it.
package chrome
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"amadan.net/oficina/estilo.git/base"
"amadan.net/oficina/estilo.git/menu"
"amadan.net/oficina/estilo.git/theme"
"github.com/carlosframework/rastrillo/ui"
)
// The page: the stacked bar as an app draws it, with the suite's
// stylesheets in their fixed order and chrome.js. ?title= is the title.
const titlePage = `
%s
`
type titleFit struct {
Engine string `json:"engine"`
Version string `json:"version"`
Error string `json:"error"`
Fields []struct {
Title string `json:"title"`
Viewport int `json:"viewport"`
Inline string `json:"inline"` // style.inlineSize, as chrome.js set it
Width float64 `json:"width"` // the field's border box
Content float64 `json:"content"` // its content box: the room the text has
Text float64 `json:"text"` // the title in the field's font, as a span draws it
Scroll int `json:"scroll"` // scrollWidth
Client int `json:"client"` // clientWidth
Capped bool `json:"capped"` // the bar's room, not the title, set the width
Narrowed bool `json:"narrowed"` // the control: the same field 12.8px (both paddings) narrower
Zero struct {
Scroll, Client, Offset int
} `json:"zero"` // what chrome.js reads, at zero width
} `json:"fields"`
}
func TestStackedTitleFitsItsTitle(t *testing.T) {
if _, err := exec.LookPath("node"); err != nil {
t.Fatal("node is required (Node 22+)")
}
view := parse(t)
mux := http.NewServeMux()
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
b := stacked()
b.ContextEdit.Value = r.URL.Query().Get("title")
var bar bytes.Buffer
if err := view.ExecuteTemplate(&bar, "estilo-bar", b); err != nil {
t.Error(err)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:")
fmt.Fprintf(w, titlePage, bar.String())
})
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()}
for path, body := range assets {
mux.HandleFunc("GET "+path, func(w http.ResponseWriter, r *http.Request) {
ct := "text/css; charset=utf-8"
if strings.HasSuffix(path, ".js") {
ct = "text/javascript; charset=utf-8"
}
w.Header().Set("Content-Type", ct)
_, _ = w.Write(body)
})
}
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
engines := strings.Split(os.Getenv("ESTILO_BROWSERS"), ",")
if engines[0] == "" {
engines = []string{"chromium", "webkit"}
}
job, _ := json.Marshal(map[string]any{
"origin": srv.URL,
"engines": engines,
"viewports": []int{1440, 390},
// Short, the two titles the bug was found on, a sentence, wide
// and narrow Latin, CJK, and Latin with accents and a dash.
"titles": []string{"Q3", "Onboarding", "Brightwater Labs", "Untitled document", "WWWWWWWWWW", "illiterate fill", "A considerably longer document title for the quarter", "四半期の計画書", "Résumé — draft"},
})
cmd := exec.Command("node", "titlefit.browser.mjs", string(job))
cmd.Stderr = os.Stderr
raw, err := cmd.Output()
if err != nil {
t.Fatalf("node titlefit.browser.mjs: %v", err)
}
var results []titleFit
if err := json.Unmarshal(raw, &results); err != nil {
t.Fatalf("driver output: %v\n%s", err, raw)
}
if len(results) != len(engines) {
t.Fatalf("%d engines asked for, %d reported", len(engines), len(results))
}
for _, r := range results {
if r.Error != "" {
t.Errorf("%s did not launch: %s", r.Engine, r.Error)
continue
}
fitted := 0
for _, f := range r.Fields {
where := fmt.Sprintf("%s %s, %dpx, %q", r.Engine, r.Version, f.Viewport, f.Title)
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)
if f.Inline == "" {
t.Errorf("%s: chrome.js did not fit the field", where)
continue
}
// The detector must see a field that is short by exactly what
// the bug cost, or a pass means nothing.
if !f.Narrowed {
t.Errorf("%s: the same field 12.8px narrower was not seen as cut off; the check is blind", where)
}
if f.Capped {
continue // a title longer than the room scrolls inside the field, by design
}
fitted++
// The engine's own verdict: nothing to scroll to, so nothing cut off.
if f.Scroll > f.Client {
t.Errorf("%s: the title overflows its field (scroll width %d, client width %d): its end is cut off", where, f.Scroll, f.Client)
}
// And the text's own measure fits the room the field gives it.
if f.Content+0.01 < f.Text {
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)
}
// And as wide as its title, not the bar's room: a caret and
// the rounding over the text, never a field's worth.
if f.Content > f.Text+24 {
t.Errorf("%s: the field's content box is %.2fpx for a title %.2fpx wide: not fitted to it", where, f.Content, f.Text)
}
}
if fitted < len(r.Fields)/2 {
t.Errorf("%s: only %d of %d fields were fitted to their title rather than capped", r.Engine, fitted, len(r.Fields))
}
}
}