| 1 | package theme |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "flag" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "regexp" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/carlosframework/rastrillo/ui" |
| 14 | ) |
| 15 | |
| 16 | // Estilo stopped being day on 2026-09-29 (AGENTS.md, decisions table, |
| 17 | // row 8): Paul chose Calma. The byte-for-byte gate against day is gone |
| 18 | // with that ruling. What replaces it holds Calma to the same bars day was |
| 19 | // held to, on its own values: |
| 20 | // |
| 21 | // - TestDeclaresRastrilloTokens: the same token NAMES as rastrillo's |
| 22 | // day, the pinned one and the newest an app pins (a snapshot), so |
| 23 | // rastrillo's tokens.css never meets an undefined var(--rst-*), and a |
| 24 | // rastrillo upgrade that adds or drops a token still fails here. |
| 25 | // - TestContrastMeetsWCAG: every pair rastrillo's own gate checks, in |
| 26 | // both schemes, for estilo.css and for every app's accent. |
| 27 | // - TestAccentGoldens: each app's accent block against testdata, so no |
| 28 | // colour moves without the diff saying so. Regenerate deliberately |
| 29 | // with `go test ./theme -run Goldens -update` and read the diff. |
| 30 | |
| 31 | var update = flag.Bool("update", false, "rewrite theme/testdata goldens") |
| 32 | |
| 33 | // The next three are copied verbatim from rastrillo's |
| 34 | // ui/contrast_test.go (v0.23.0), per AGENTS.md's "contrast arithmetic |
| 35 | // copied verbatim, never reimplemented". The arithmetic itself is |
| 36 | // rastrillo's exported ui.ContrastRatio. |
| 37 | |
| 38 | var declPattern = regexp.MustCompile(`(--rst-[a-z0-9-]+)\s*:\s*([^;]+);`) |
| 39 | |
| 40 | func parseTokens(body string) map[string]string { |
| 41 | out := map[string]string{} |
| 42 | for _, m := range declPattern.FindAllStringSubmatch(body, -1) { |
| 43 | out[m[1]] = strings.TrimSpace(m[2]) |
| 44 | } |
| 45 | return out |
| 46 | } |
| 47 | |
| 48 | // splitLightDark evaluates a whole-value light-dark(<light>, <dark>) |
| 49 | // call, returning the two halves. It reports false for anything else — |
| 50 | // including a value that merely *contains* a light-dark() somewhere |
| 51 | // inside it, which is exactly what the shadow tokens look like |
| 52 | // ("0 8px 24px light-dark(rgba(…), rgba(…))"): those are not colours in |
| 53 | // their own right, are not in the pair table, and are left to both |
| 54 | // schemes verbatim rather than half-parsed. |
| 55 | // |
| 56 | // The split is paren-aware because both halves are usually rgba() calls |
| 57 | // full of commas of their own. |
| 58 | func splitLightDark(v string) (light, dark string, ok bool) { |
| 59 | const prefix = "light-dark(" |
| 60 | if !strings.HasPrefix(v, prefix) || !strings.HasSuffix(v, ")") { |
| 61 | return "", "", false |
| 62 | } |
| 63 | inner := v[len(prefix) : len(v)-1] |
| 64 | depth, comma := 0, -1 |
| 65 | for i := 0; i < len(inner); i++ { |
| 66 | switch inner[i] { |
| 67 | case '(': |
| 68 | depth++ |
| 69 | case ')': |
| 70 | depth-- |
| 71 | if depth < 0 { |
| 72 | return "", "", false // the closing ")" we trimmed was not ours |
| 73 | } |
| 74 | case ',': |
| 75 | if depth == 0 { |
| 76 | if comma >= 0 { |
| 77 | return "", "", false // three arguments is not light-dark() |
| 78 | } |
| 79 | comma = i |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | if depth != 0 || comma < 0 { |
| 84 | return "", "", false |
| 85 | } |
| 86 | return strings.TrimSpace(inner[:comma]), strings.TrimSpace(inner[comma+1:]), true |
| 87 | } |
| 88 | |
| 89 | // tokensIn parses every :root block in css, later declarations winning, |
| 90 | // and splits each value into its two schemes. ForApp's output has two |
| 91 | // :root blocks (estilo.css, then the app's accent), exactly as a browser |
| 92 | // would cascade them. |
| 93 | func tokensIn(css string) map[string]map[string]string { |
| 94 | out := map[string]map[string]string{"light": {}, "dark": {}} |
| 95 | // Comments first: a comment quoting a declaration ("was --rst-accent: |
| 96 | // #123;") must never win the cascade the way it would in this parse. |
| 97 | css = regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(css, "") |
| 98 | for name, v := range parseTokens(css) { |
| 99 | if l, d, ok := splitLightDark(v); ok { |
| 100 | out["light"][name], out["dark"][name] = l, d |
| 101 | continue |
| 102 | } |
| 103 | out["light"][name], out["dark"][name] = v, v |
| 104 | } |
| 105 | return out |
| 106 | } |
| 107 | |
| 108 | func names(css []byte) []string { |
| 109 | var out []string |
| 110 | for name := range parseTokens(string(css)) { |
| 111 | out = append(out, name) |
| 112 | } |
| 113 | sort.Strings(out) |
| 114 | return out |
| 115 | } |
| 116 | |
| 117 | // TestDeclaresRastrilloTokens: estilo.css declares exactly the token |
| 118 | // names rastrillo's day declares, so rastrillo's tokens.css never meets |
| 119 | // an undefined var(--rst-*) on a Calma page. Two sources, because estilo |
| 120 | // pins rastrillo v0.23.0 while its apps run newer ones: |
| 121 | // |
| 122 | // - day at the pinned v0.23.0, read live through ui.ThemeCSS; |
| 123 | // - day at the newest rastrillo an app pins (v0.38.0 today), from the |
| 124 | // snapshot testdata/rastrillo-day-tokens.txt. Estilo cannot import |
| 125 | // that rastrillo: requiring it here would pull every app up to it. |
| 126 | // |
| 127 | // The snapshot must cover the live set, and estilo.css must declare |
| 128 | // exactly the snapshot: a token added upstream fails here once the |
| 129 | // snapshot is refreshed, and a stray one fails always. |
| 130 | func TestDeclaresRastrilloTokens(t *testing.T) { |
| 131 | day, ok := ui.ThemeCSS("day") |
| 132 | if !ok { |
| 133 | t.Fatal(`ui.ThemeCSS("day") returned false -- rastrillo no longer ships a theme named "day"`) |
| 134 | } |
| 135 | raw, err := os.ReadFile(filepath.Join("testdata", "rastrillo-day-tokens.txt")) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | var snapshot []string |
| 140 | inSnapshot := map[string]bool{} |
| 141 | for _, line := range strings.Split(string(raw), "\n") { |
| 142 | if line = strings.TrimSpace(line); line != "" && !strings.HasPrefix(line, "#") { |
| 143 | snapshot = append(snapshot, line) |
| 144 | inSnapshot[line] = true |
| 145 | } |
| 146 | } |
| 147 | sort.Strings(snapshot) |
| 148 | for _, name := range names(day) { |
| 149 | if !inSnapshot[name] { |
| 150 | t.Errorf("the pinned rastrillo's day declares %s, which testdata/rastrillo-day-tokens.txt lacks; regenerate the snapshot", name) |
| 151 | } |
| 152 | } |
| 153 | if got := names(css); strings.Join(snapshot, " ") != strings.Join(got, " ") { |
| 154 | t.Fatalf("estilo.css must declare exactly rastrillo day's token names (tokens.css uses them):\n day: %v\n estilo: %v", snapshot, got) |
| 155 | } |
| 156 | // The toggle, too: it is the only way an explicit Appearance choice |
| 157 | // beats the OS. |
| 158 | for _, rule := range []string{`:root[data-theme="light"] { color-scheme: light; }`, `:root[data-theme="dark"] { color-scheme: dark; }`} { |
| 159 | if !bytes.Contains(css, []byte(rule)) { |
| 160 | t.Errorf("estilo.css lost the scheme toggle %q", rule) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestContrastMeetsWCAG(t *testing.T) { |
| 166 | type pair struct { |
| 167 | fg, bg string |
| 168 | min float64 |
| 169 | why string |
| 170 | } |
| 171 | // Transcribed verbatim from rastrillo's TestThemeTokenContrastMeetsWCAG. |
| 172 | pairs := []pair{ |
| 173 | {"--rst-text", "--rst-surface", 4.5, "body text on a card"}, |
| 174 | {"--rst-text", "--rst-bg", 4.5, "body text"}, |
| 175 | {"--rst-text", "--rst-surface-2", 4.5, "body text on a card"}, |
| 176 | {"--rst-text", "--rst-accent-soft", 4.5, "body text on an accent-tinted surface"}, |
| 177 | {"--rst-text-muted", "--rst-surface", 4.5, "muted text on a card"}, |
| 178 | {"--rst-text-muted", "--rst-bg", 4.5, "muted text"}, |
| 179 | {"--rst-text-muted", "--rst-surface-2", 4.5, "muted text on a card"}, |
| 180 | {"--rst-text-muted", "--rst-accent-soft", 4.5, "muted text on an accent-tinted surface"}, |
| 181 | {"--rst-text-faint", "--rst-surface", 4.5, "faint text on a card ([rst-field-hint])"}, |
| 182 | {"--rst-text-faint", "--rst-bg", 4.5, "faint text"}, |
| 183 | {"--rst-text-faint", "--rst-surface-2", 4.5, "faint text on a card"}, |
| 184 | {"--rst-text-faint", "--rst-accent-soft", 4.5, "faint text on an accent-tinted surface"}, |
| 185 | {"--rst-accent", "--rst-surface", 4.5, "text + focus ring"}, |
| 186 | {"--rst-accent", "--rst-bg", 4.5, "text + focus ring"}, |
| 187 | {"--rst-accent", "--rst-surface-2", 4.5, "text"}, |
| 188 | {"--rst-accent", "--rst-accent-soft", 4.5, "text"}, |
| 189 | {"--rst-on-accent", "--rst-accent", 4.5, "primary button label"}, |
| 190 | {"--rst-on-accent", "--rst-accent-strong", 4.5, "primary button label, hover"}, |
| 191 | {"--rst-line-strong", "--rst-surface", 3.0, "control border (1.4.11 boundary, not text)"}, |
| 192 | {"--rst-line-strong", "--rst-bg", 3.0, "control border"}, |
| 193 | {"--rst-line-strong", "--rst-surface-2", 3.0, "control border"}, |
| 194 | {"--rst-tone-neutral-fg", "--rst-tone-neutral-bg", 4.5, "status pill text"}, |
| 195 | {"--rst-tone-positive-fg", "--rst-tone-positive-bg", 4.5, "status pill text"}, |
| 196 | {"--rst-tone-warning-fg", "--rst-tone-warning-bg", 4.5, "status pill text"}, |
| 197 | {"--rst-tone-negative-fg", "--rst-tone-negative-bg", 4.5, "status pill text"}, |
| 198 | // Not in the main header tables — documented separately in |
| 199 | // tokens.css's [rst-btn~="danger"] comment, which reuses |
| 200 | // --rst-tone-negative-fg as a solid fill rather than declaring a |
| 201 | // dedicated --rst-danger-* pair. This is the real pair the danger |
| 202 | // button's label renders against, so it belongs in the gate anyway. |
| 203 | {"--rst-on-accent", "--rst-tone-negative-fg", 4.5, "danger button label"}, |
| 204 | } |
| 205 | |
| 206 | themes := map[string]string{"estilo.css": string(css)} |
| 207 | for _, a := range Apps() { |
| 208 | full, _ := ForApp(a.Slug) |
| 209 | themes[a.Slug] = string(full) |
| 210 | } |
| 211 | for name, body := range themes { |
| 212 | tokens := tokensIn(body) |
| 213 | for _, scheme := range []string{"light", "dark"} { |
| 214 | t.Run(name+"/"+scheme, func(t *testing.T) { |
| 215 | for _, p := range pairs { |
| 216 | fg, bg := tokens[scheme][p.fg], tokens[scheme][p.bg] |
| 217 | if fg == "" || bg == "" { |
| 218 | t.Errorf("%s or %s is not declared", p.fg, p.bg) |
| 219 | continue |
| 220 | } |
| 221 | ratio, err := ui.ContrastRatio(fg, bg) |
| 222 | if err != nil { |
| 223 | t.Errorf("%s (%s) on %s (%s): %v", p.fg, fg, p.bg, bg, err) |
| 224 | continue |
| 225 | } |
| 226 | if ratio < p.min { |
| 227 | t.Errorf("%s (%s) on %s (%s) = %.2f:1, want >= %.1f:1 (%s)", p.fg, fg, p.bg, bg, ratio, p.min, p.why) |
| 228 | } |
| 229 | } |
| 230 | }) |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // estilo.css is written out by hand, so it can carry its own comments; |
| 236 | // its accent is Oficina's, and must stay what the ring says Oficina is. |
| 237 | func TestSuiteAccentIsOficinas(t *testing.T) { |
| 238 | block, _ := AccentCSS("oficina") |
| 239 | want, got := parseTokens(string(block)), parseTokens(string(css)) |
| 240 | for name, v := range want { |
| 241 | if got[name] != v { |
| 242 | t.Errorf("estilo.css %s = %q, but the ring gives Oficina %q", name, got[name], v) |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | func TestAccentGoldens(t *testing.T) { |
| 248 | for _, a := range Apps() { |
| 249 | block, _ := AccentCSS(a.Slug) |
| 250 | path := filepath.Join("testdata", a.Slug+".css") |
| 251 | if *update { |
| 252 | if err := os.WriteFile(path, block, 0o644); err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | continue |
| 256 | } |
| 257 | want, err := os.ReadFile(path) |
| 258 | if err != nil { |
| 259 | t.Fatalf("%v (run with -update to create it)", err) |
| 260 | } |
| 261 | if !bytes.Equal(want, block) { |
| 262 | t.Errorf("%s's accent changed:\n want %s\n got %s", a.Name, want, block) |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func TestTheRingIsClosed(t *testing.T) { |
| 268 | for _, slug := range []string{"", "day", "Docs", "docs ", "../docs", "meet"} { |
| 269 | if _, ok := ForApp(slug); ok { |
| 270 | t.Errorf("ForApp(%q) accepted a slug outside the ring", slug) |
| 271 | } |
| 272 | } |
| 273 | seen := map[string]bool{} |
| 274 | for _, a := range Apps() { |
| 275 | if seen[a.Slug] || !regexp.MustCompile(`^[a-z]+$`).MatchString(a.Slug) { |
| 276 | t.Errorf("ring slug %q is repeated or not a plain word", a.Slug) |
| 277 | } |
| 278 | seen[a.Slug] = true |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // On touch a switch is the size a finger expects: iOS's 51x31 with a |
| 283 | // 27px knob, as Fiesta's Day theme has it. It was the pointer size, |
| 284 | // 34x19 (Docs design/phone-web, finding 5; slice P3). |
| 285 | func TestTouchSwitchesAreFingerSized(t *testing.T) { |
| 286 | got := parseTokens(string(css)) |
| 287 | for name, want := range map[string]string{ |
| 288 | "--rst-switch-touch-w": "51px", |
| 289 | "--rst-switch-touch-h": "31px", |
| 290 | "--rst-switch-touch-knob": "27px", |
| 291 | } { |
| 292 | if got[name] != want { |
| 293 | t.Errorf("estilo.css %s = %q, want %q", name, got[name], want) |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |