| 1 | //go:build browser |
| 2 | |
| 3 | // The no-flash check for the chrome's typeface, in real engines. Not |
| 4 | // 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`. The gate |
| 8 | // vets this file with the tag (make test-vet), so it cannot stop |
| 9 | // compiling unnoticed. An engine that will not launch fails the test; it |
| 10 | // never skips to a pass. |
| 11 | // |
| 12 | // Paul, 2026-10-24: "There should never be a flash of unstyled text. |
| 13 | // Ever." A visible stretch of invisible text counts too. So this looks |
| 14 | // at pixels, not at what the CSS asked for or what layout reported: |
| 15 | // every picture of the chrome text from navigation on must be either |
| 16 | // "nothing painted yet" or the final picture, in that order. A fallback |
| 17 | // face, invisible text, or text that changes after it appears, is a |
| 18 | // third picture, and fails. And the final picture must be the system |
| 19 | // UI face (AGENTS.md row 9), visibly. |
| 20 | // |
| 21 | // The detector is checked against itself every run: two pages that do |
| 22 | // flash (v1's Onest with font-display: swap, and the chrome with its |
| 23 | // text hidden for a second) must be caught, or the run fails as blind. |
| 24 | package theme |
| 25 | |
| 26 | import ( |
| 27 | "encoding/json" |
| 28 | "fmt" |
| 29 | "net/http" |
| 30 | "net/http/httptest" |
| 31 | "os" |
| 32 | "os/exec" |
| 33 | "strings" |
| 34 | "sync/atomic" |
| 35 | "testing" |
| 36 | "time" |
| 37 | ) |
| 38 | |
| 39 | // probe is chrome text as the bar and its nav draw it, in --rst-font at |
| 40 | // the bar's weights, on a tinted page so "painted, text invisible" can |
| 41 | // never look like the blank page. |
| 42 | const probe = `<!doctype html> |
| 43 | <html lang="en"> |
| 44 | <head> |
| 45 | <meta charset="utf-8"> |
| 46 | <title>Chrome font probe</title> |
| 47 | %s |
| 48 | <link rel="stylesheet" href="/theme.css"> |
| 49 | <style> |
| 50 | body { margin: 0; background: #dfe8f5; color: #15171c; font-family: var(--rst-font); font-size: 18px; line-height: 28px; } |
| 51 | #bar { font-weight: 600; } %s |
| 52 | </style> |
| 53 | </head> |
| 54 | <body> |
| 55 | <div id="probe"><div id="bar">Oficina Docs</div><div>Files · Shared · Settings · Sign out</div></div> |
| 56 | </body> |
| 57 | </html> |
| 58 | ` |
| 59 | |
| 60 | // production is rastrillo's default CSP, unchanged: the system face |
| 61 | // needs nothing from it. |
| 62 | const production = "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:" |
| 63 | |
| 64 | // systemStack is --rst-font spelled out, for the reference picture. |
| 65 | const systemStack = `system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif` |
| 66 | |
| 67 | type page struct { |
| 68 | head, css string |
| 69 | cssDelay time.Duration // theme.css held back this long |
| 70 | fontDelay time.Duration // v1's woff2 held back this long |
| 71 | fetched *atomic.Int32 // v1 woff2 requests |
| 72 | } |
| 73 | |
| 74 | func serve(t *testing.T, p page) string { |
| 75 | fonts := FontHandler() |
| 76 | if p.fetched == nil { |
| 77 | p.fetched = new(atomic.Int32) |
| 78 | } |
| 79 | mux := http.NewServeMux() |
| 80 | mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { |
| 81 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 82 | w.Header().Set("Cache-Control", "no-store") |
| 83 | w.Header().Set("Content-Security-Policy", production) |
| 84 | fmt.Fprintf(w, probe, p.head, p.css) |
| 85 | }) |
| 86 | mux.HandleFunc("GET /theme.css", func(w http.ResponseWriter, r *http.Request) { |
| 87 | select { |
| 88 | case <-time.After(p.cssDelay): |
| 89 | case <-r.Context().Done(): |
| 90 | return |
| 91 | } |
| 92 | w.Header().Set("Content-Type", "text/css; charset=utf-8") |
| 93 | _, _ = w.Write(CSS()) |
| 94 | }) |
| 95 | mux.Handle("GET "+FontPrefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 96 | if strings.HasSuffix(r.URL.Path, ".woff2") { |
| 97 | p.fetched.Add(1) |
| 98 | select { |
| 99 | case <-time.After(p.fontDelay): |
| 100 | case <-r.Context().Done(): |
| 101 | return |
| 102 | } |
| 103 | } |
| 104 | fonts.ServeHTTP(w, r) |
| 105 | })) |
| 106 | srv := httptest.NewServer(mux) |
| 107 | t.Cleanup(srv.Close) |
| 108 | return srv.URL + "/" |
| 109 | } |
| 110 | |
| 111 | type scenario struct { |
| 112 | Name string `json:"name"` |
| 113 | URL string `json:"url"` |
| 114 | WatchMs int `json:"watchMs"` |
| 115 | |
| 116 | flashes bool // a control: the detector must catch it |
| 117 | fetched *atomic.Int32 // must stay 0 for the design: nothing is downloaded |
| 118 | } |
| 119 | |
| 120 | type observed struct { |
| 121 | Engine string `json:"engine"` |
| 122 | Version string `json:"version"` |
| 123 | Error string `json:"error"` |
| 124 | Scenarios []struct { |
| 125 | Name string `json:"name"` |
| 126 | Blank string `json:"blank"` |
| 127 | Final string `json:"final"` |
| 128 | Samples []struct { |
| 129 | T int `json:"t"` |
| 130 | H string `json:"h"` |
| 131 | } `json:"samples"` |
| 132 | Refs map[string]string `json:"refs"` |
| 133 | } `json:"scenarios"` |
| 134 | } |
| 135 | |
| 136 | func TestChromeTextNeverFlashes(t *testing.T) { |
| 137 | if _, err := exec.LookPath("node"); err != nil { |
| 138 | t.Fatal("node is required (Node 22+)") |
| 139 | } |
| 140 | engines := strings.Split(os.Getenv("ESTILO_BROWSERS"), ",") |
| 141 | if engines[0] == "" { |
| 142 | engines = []string{"chromium", "webkit"} |
| 143 | } |
| 144 | v1 := `<link rel="stylesheet" href="` + FontPrefix + `fonts.css">` |
| 145 | hide := `#probe { animation: hide 1s step-end; } @keyframes hide { from { color: transparent; } to { color: inherit; } }` |
| 146 | ms := func(d time.Duration) int { return int(d / time.Millisecond) } |
| 147 | var scs []*scenario |
| 148 | add := func(name string, p page, watch time.Duration, sc scenario) { |
| 149 | p.fetched = new(atomic.Int32) |
| 150 | sc.Name, sc.URL, sc.WatchMs, sc.fetched = name, serve(t, p), ms(watch), p.fetched |
| 151 | scs = append(scs, &sc) |
| 152 | } |
| 153 | // The design: the page with theme.css prompt, slower than optional's |
| 154 | // 100 ms block period, and slower than block's 3 s. |
| 155 | for _, d := range []time.Duration{0, 400 * time.Millisecond, 3500 * time.Millisecond} { |
| 156 | add(fmt.Sprintf("system UI, theme.css +%dms", ms(d)), page{cssDelay: d}, d+1500*time.Millisecond, scenario{}) |
| 157 | } |
| 158 | // An app not yet adopted: its layout still links v1's fonts.css. The |
| 159 | // face is declared and nothing uses it, so it is never fetched. |
| 160 | add("system UI, page still linking v1 fonts.css", page{head: v1, fontDelay: time.Second}, 2500*time.Millisecond, scenario{}) |
| 161 | // Controls, which do flash. |
| 162 | add("control: v1 Onest (swap), font +1000ms", page{head: v1, fontDelay: time.Second, css: `#probe { font-family: "Onest", var(--rst-font); }`}, 2500*time.Millisecond, scenario{flashes: true}) |
| 163 | add("control: text hidden 1s", page{css: hide}, 2500*time.Millisecond, scenario{flashes: true}) |
| 164 | |
| 165 | refs := map[string]string{ |
| 166 | "system": serve(t, page{css: `#probe { font-family: ` + systemStack + `; }`}), // the stack, spelled out |
| 167 | "hidden": serve(t, page{css: `#probe { color: transparent; }`}), // painted, text invisible |
| 168 | } |
| 169 | job, _ := json.Marshal(map[string]any{ |
| 170 | "engines": engines, |
| 171 | "viewport": map[string]int{"width": 480, "height": 120}, |
| 172 | "clip": map[string]int{"x": 0, "y": 0, "width": 480, "height": 56}, |
| 173 | "scenarios": scs, |
| 174 | "refs": refs, |
| 175 | }) |
| 176 | cmd := exec.Command("node", "fontflash.browser.mjs", string(job)) |
| 177 | cmd.Stderr = os.Stderr |
| 178 | raw, err := cmd.Output() |
| 179 | if err != nil { |
| 180 | t.Fatalf("node fontflash.browser.mjs: %v", err) |
| 181 | } |
| 182 | var results []observed |
| 183 | if err := json.Unmarshal(raw, &results); err != nil { |
| 184 | t.Fatalf("driver output: %v\n%s", err, raw) |
| 185 | } |
| 186 | |
| 187 | for _, r := range results { |
| 188 | if r.Error != "" { |
| 189 | t.Errorf("%s did not launch: %s", r.Engine, r.Error) |
| 190 | continue |
| 191 | } |
| 192 | for i, s := range r.Scenarios { |
| 193 | sc, where := scs[i], r.Engine+" "+r.Version+", "+s.Name |
| 194 | // A flash is any picture that is neither "not painted yet" |
| 195 | // nor the final one, or "not painted" again after the final |
| 196 | // one has shown. |
| 197 | var flash []string |
| 198 | painted, before := false, 0 |
| 199 | for _, p := range s.Samples { |
| 200 | switch p.H { |
| 201 | case s.Blank, "none": |
| 202 | before++ |
| 203 | if painted { |
| 204 | flash = append(flash, fmt.Sprintf("%dms blank again", p.T)) |
| 205 | } |
| 206 | case s.Final: |
| 207 | painted = true |
| 208 | default: |
| 209 | flash = append(flash, fmt.Sprintf("%dms %s", p.T, p.H)) |
| 210 | } |
| 211 | } |
| 212 | if len(s.Samples) < 10 { |
| 213 | t.Errorf("%s: only %d pictures; the watch did not see the page", where, len(s.Samples)) |
| 214 | continue |
| 215 | } |
| 216 | if !painted { |
| 217 | t.Errorf("%s: the final picture never appeared during the watch", where) |
| 218 | } |
| 219 | if s.Final == s.Refs["hidden"] || s.Final == s.Blank { |
| 220 | t.Errorf("%s: the final picture has no visible text", where) |
| 221 | } |
| 222 | if !sc.flashes { |
| 223 | if s.Final != s.Refs["system"] { |
| 224 | t.Errorf("%s: the final picture is not the system UI face", where) |
| 225 | } |
| 226 | if n := sc.fetched.Load(); n != 0 { |
| 227 | t.Errorf("%s: a font was downloaded %d times; the chrome downloads none", where, n) |
| 228 | } |
| 229 | } |
| 230 | switch { |
| 231 | case sc.flashes && len(flash) == 0: |
| 232 | t.Errorf("%s: a page that flashes was not caught; the detector is blind", where) |
| 233 | case !sc.flashes && len(flash) > 0: |
| 234 | t.Errorf("%s: FLASH: %d of %d pictures were neither blank nor final: %s", where, len(flash), len(s.Samples), strings.Join(first(flash, 6), ", ")) |
| 235 | } |
| 236 | t.Logf("%s: timeline %s", where, timeline(s.Samples, map[string]string{s.Blank: "blank", "none": "none", s.Final: "FINAL", s.Refs["system"]: "SYSTEM", s.Refs["hidden"]: "HIDDEN"})) |
| 237 | t.Logf("%s: %d pictures (%d before first paint, every %dms), %d off-final", where, len(s.Samples), before, s.Samples[len(s.Samples)-1].T/len(s.Samples), len(flash)) |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // timeline is the run-length sequence of pictures: "blank 0-7ms, FINAL |
| 243 | // 8-1500ms". |
| 244 | func timeline(samples []struct { |
| 245 | T int `json:"t"` |
| 246 | H string `json:"h"` |
| 247 | }, names map[string]string) string { |
| 248 | var out []string |
| 249 | for i := 0; i < len(samples); { |
| 250 | j := i |
| 251 | for j+1 < len(samples) && samples[j+1].H == samples[i].H { |
| 252 | j++ |
| 253 | } |
| 254 | name := names[samples[i].H] |
| 255 | if name == "" { |
| 256 | name = samples[i].H |
| 257 | } |
| 258 | out = append(out, fmt.Sprintf("%s %d-%dms", name, samples[i].T, samples[j].T)) |
| 259 | i = j + 1 |
| 260 | } |
| 261 | return strings.Join(first(out, 12), ", ") |
| 262 | } |
| 263 | |
| 264 | func first(s []string, n int) []string { |
| 265 | if len(s) > n { |
| 266 | return append(s[:n:n], fmt.Sprintf("... %d more", len(s)-n)) |
| 267 | } |
| 268 | return s |
| 269 | } |
| 270 | |