|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { RGBA } from "@opentui/core" |
| 3 | +import github from "@/cli/cmd/tui/context/theme/github.json" |
| 4 | +import solarized from "@/cli/cmd/tui/context/theme/solarized.json" |
| 5 | +import flexoki from "@/cli/cmd/tui/context/theme/flexoki.json" |
| 6 | + |
| 7 | +/** |
| 8 | + * E2E tests for light-theme text visibility (issue #617). |
| 9 | + * |
| 10 | + * Root cause: the experimental <markdown> element was missing fg={theme.text}, |
| 11 | + * falling back to OpenTUI's hardcoded white default (RGBA(1,1,1,1)). Additionally, |
| 12 | + * the markup.raw / markup.raw.block syntax scopes lacked a background property, |
| 13 | + * so fenced code blocks had no background contrast on light terminals. |
| 14 | + * |
| 15 | + * These tests resolve theme JSON files through the same algorithm used in |
| 16 | + * production (resolveTheme + getSyntaxRules), then assert that: |
| 17 | + * 1. Code block scopes have a background color set |
| 18 | + * 2. Default foreground is never white on light backgrounds |
| 19 | + * 3. All foreground colors have sufficient contrast against their background |
| 20 | + * |
| 21 | + * No mocks — uses real theme JSON files and the real color resolution algorithm. |
| 22 | + */ |
| 23 | + |
| 24 | +// ─── Pure functions extracted from theme.tsx (identical logic) ────────────── |
| 25 | + |
| 26 | +type ThemeColors = Record<string, RGBA> |
| 27 | + |
| 28 | +type Theme = ThemeColors & { |
| 29 | + _hasSelectedListItemText: boolean |
| 30 | + thinkingOpacity: number |
| 31 | +} |
| 32 | + |
| 33 | +type ThemeJson = { |
| 34 | + defs?: Record<string, string> |
| 35 | + theme: Record<string, unknown> |
| 36 | +} |
| 37 | + |
| 38 | +function ansiToRgba(code: number): RGBA { |
| 39 | + if (code < 16) { |
| 40 | + const ansiColors = [ |
| 41 | + "#000000", "#800000", "#008000", "#808000", |
| 42 | + "#000080", "#800080", "#008080", "#c0c0c0", |
| 43 | + "#808080", "#ff0000", "#00ff00", "#ffff00", |
| 44 | + "#0000ff", "#ff00ff", "#00ffff", "#ffffff", |
| 45 | + ] |
| 46 | + return RGBA.fromHex(ansiColors[code] ?? "#000000") |
| 47 | + } |
| 48 | + if (code < 232) { |
| 49 | + const index = code - 16 |
| 50 | + const b = index % 6 |
| 51 | + const g = Math.floor(index / 6) % 6 |
| 52 | + const r = Math.floor(index / 36) |
| 53 | + const val = (x: number) => (x === 0 ? 0 : x * 40 + 55) |
| 54 | + return RGBA.fromInts(val(r), val(g), val(b)) |
| 55 | + } |
| 56 | + if (code < 256) { |
| 57 | + const gray = (code - 232) * 10 + 8 |
| 58 | + return RGBA.fromInts(gray, gray, gray) |
| 59 | + } |
| 60 | + return RGBA.fromInts(0, 0, 0) |
| 61 | +} |
| 62 | + |
| 63 | +function resolveTheme(theme: ThemeJson, mode: "dark" | "light"): Theme { |
| 64 | + const defs = theme.defs ?? {} |
| 65 | + type ColorValue = string | number | RGBA | { dark: string; light: string } |
| 66 | + |
| 67 | + function resolveColor(c: ColorValue): RGBA { |
| 68 | + if (c instanceof RGBA) return c |
| 69 | + if (typeof c === "string") { |
| 70 | + if (c === "transparent" || c === "none") return RGBA.fromInts(0, 0, 0, 0) |
| 71 | + if (c.startsWith("#")) return RGBA.fromHex(c) |
| 72 | + if (defs[c] != null) return resolveColor(defs[c]) |
| 73 | + if (theme.theme[c] !== undefined) return resolveColor(theme.theme[c] as ColorValue) |
| 74 | + throw new Error(`Color reference "${c}" not found in defs or theme`) |
| 75 | + } |
| 76 | + if (typeof c === "number") return ansiToRgba(c) |
| 77 | + return resolveColor(c[mode]) |
| 78 | + } |
| 79 | + |
| 80 | + const resolved: Record<string, RGBA> = {} |
| 81 | + for (const [key, value] of Object.entries(theme.theme)) { |
| 82 | + if (key === "selectedListItemText" || key === "backgroundMenu" || key === "thinkingOpacity") continue |
| 83 | + resolved[key] = resolveColor(value as ColorValue) |
| 84 | + } |
| 85 | + |
| 86 | + const hasSelectedListItemText = theme.theme.selectedListItemText !== undefined |
| 87 | + if (hasSelectedListItemText) { |
| 88 | + resolved.selectedListItemText = resolveColor(theme.theme.selectedListItemText as ColorValue) |
| 89 | + } else { |
| 90 | + resolved.selectedListItemText = resolved.background! |
| 91 | + } |
| 92 | + |
| 93 | + if (theme.theme.backgroundMenu !== undefined) { |
| 94 | + resolved.backgroundMenu = resolveColor(theme.theme.backgroundMenu as ColorValue) |
| 95 | + } else { |
| 96 | + resolved.backgroundMenu = resolved.backgroundElement! |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + ...resolved, |
| 101 | + _hasSelectedListItemText: hasSelectedListItemText, |
| 102 | + thinkingOpacity: (theme.theme.thinkingOpacity as number | undefined) ?? 0.6, |
| 103 | + } as Theme |
| 104 | +} |
| 105 | + |
| 106 | +type SyntaxRule = { |
| 107 | + scope: string[] |
| 108 | + style: { foreground?: RGBA; background?: RGBA; bold?: boolean; italic?: boolean; underline?: boolean } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Identical to getSyntaxRules in theme.tsx — including the fix under test |
| 113 | + * (background: theme.backgroundElement on markup.raw scope). |
| 114 | + */ |
| 115 | +function getSyntaxRules(theme: Theme): SyntaxRule[] { |
| 116 | + return [ |
| 117 | + { scope: ["default"], style: { foreground: theme.text } }, |
| 118 | + { scope: ["prompt"], style: { foreground: theme.accent } }, |
| 119 | + { scope: ["comment"], style: { foreground: theme.syntaxComment, italic: true } }, |
| 120 | + { scope: ["string", "symbol"], style: { foreground: theme.syntaxString } }, |
| 121 | + { scope: ["number", "boolean"], style: { foreground: theme.syntaxNumber } }, |
| 122 | + { scope: ["keyword"], style: { foreground: theme.syntaxKeyword, italic: true } }, |
| 123 | + { scope: ["variable", "variable.parameter"], style: { foreground: theme.syntaxVariable } }, |
| 124 | + { scope: ["type", "module"], style: { foreground: theme.syntaxType } }, |
| 125 | + { scope: ["punctuation", "punctuation.bracket"], style: { foreground: theme.syntaxPunctuation } }, |
| 126 | + // Markdown styles — the critical ones for the fix |
| 127 | + { scope: ["markup.heading"], style: { foreground: theme.markdownHeading, bold: true } }, |
| 128 | + { scope: ["markup.bold", "markup.strong"], style: { foreground: theme.markdownStrong, bold: true } }, |
| 129 | + { scope: ["markup.italic"], style: { foreground: theme.markdownEmph, italic: true } }, |
| 130 | + { scope: ["markup.list"], style: { foreground: theme.markdownListItem } }, |
| 131 | + { scope: ["markup.quote"], style: { foreground: theme.markdownBlockQuote, italic: true } }, |
| 132 | + { |
| 133 | + scope: ["markup.raw", "markup.raw.block"], |
| 134 | + style: { |
| 135 | + foreground: theme.markdownCode, |
| 136 | + // THE FIX: this background was missing before, causing invisible code blocks |
| 137 | + background: theme.backgroundElement, |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + scope: ["markup.raw.inline"], |
| 142 | + style: { foreground: theme.markdownCode, background: theme.background }, |
| 143 | + }, |
| 144 | + { scope: ["markup.link"], style: { foreground: theme.markdownLink, underline: true } }, |
| 145 | + { scope: ["spell", "nospell"], style: { foreground: theme.text } }, |
| 146 | + { scope: ["diff.plus"], style: { foreground: theme.diffAdded, background: theme.diffAddedBg } }, |
| 147 | + { scope: ["diff.minus"], style: { foreground: theme.diffRemoved, background: theme.diffRemovedBg } }, |
| 148 | + ] |
| 149 | +} |
| 150 | + |
| 151 | +// ─── Contrast helpers ────────────────────────────────────────────────────── |
| 152 | +// Contrast thresholds use WCAG 2.1 "large text" minimums (3:1) since terminal |
| 153 | +// text renders at effective large-text size. Lower thresholds (2:1, 2.5:1) are |
| 154 | +// used for syntax-highlighted code where some colors are decorative/secondary. |
| 155 | + |
| 156 | +const WHITE = RGBA.fromHex("#ffffff") |
| 157 | + |
| 158 | +function luminance(c: RGBA): number { |
| 159 | + const [r, g, b] = c.toInts() |
| 160 | + return (0.299 * r + 0.587 * g + 0.114 * b) / 255 |
| 161 | +} |
| 162 | + |
| 163 | +function isLightBackground(bg: RGBA): boolean { |
| 164 | + return luminance(bg) > 0.5 |
| 165 | +} |
| 166 | + |
| 167 | +function contrastRatio(fg: RGBA, bg: RGBA): number { |
| 168 | + function relLum(c: RGBA): number { |
| 169 | + const [r, g, b] = c.toInts() |
| 170 | + const srgb = [r, g, b].map((v) => { |
| 171 | + const s = v / 255 |
| 172 | + return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4) |
| 173 | + }) |
| 174 | + return 0.2126 * srgb[0]! + 0.7152 * srgb[1]! + 0.0722 * srgb[2]! |
| 175 | + } |
| 176 | + const l1 = relLum(fg) |
| 177 | + const l2 = relLum(bg) |
| 178 | + return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05) |
| 179 | +} |
| 180 | + |
| 181 | +// ─── Themes with explicit light mode support ─────────────────────────────── |
| 182 | + |
| 183 | +const LIGHT_THEMES: [string, ThemeJson][] = [ |
| 184 | + ["github", github as unknown as ThemeJson], |
| 185 | + ["solarized", solarized as unknown as ThemeJson], |
| 186 | + ["flexoki", flexoki as unknown as ThemeJson], |
| 187 | +] |
| 188 | + |
| 189 | +// ─── Tests ───────────────────────────────────────────────────────────────── |
| 190 | + |
| 191 | +describe("light theme: markup.raw code block visibility (issue #617)", () => { |
| 192 | + test.each(LIGHT_THEMES)( |
| 193 | + "%s: markup.raw scope has background set", |
| 194 | + (_name, themeJson) => { |
| 195 | + const resolved = resolveTheme(themeJson, "light") |
| 196 | + const rules = getSyntaxRules(resolved) |
| 197 | + |
| 198 | + const markupRawRule = rules.find( |
| 199 | + (r) => r.scope.includes("markup.raw") && r.scope.includes("markup.raw.block"), |
| 200 | + ) |
| 201 | + |
| 202 | + expect(markupRawRule).toBeDefined() |
| 203 | + expect(markupRawRule!.style.background).toBeDefined() |
| 204 | + expect(markupRawRule!.style.background).toBeInstanceOf(RGBA) |
| 205 | + }, |
| 206 | + ) |
| 207 | + |
| 208 | + test.each(LIGHT_THEMES)( |
| 209 | + "%s: markup.raw.block background differs from pure white", |
| 210 | + (_name, themeJson) => { |
| 211 | + const resolved = resolveTheme(themeJson, "light") |
| 212 | + const rules = getSyntaxRules(resolved) |
| 213 | + |
| 214 | + const markupRawRule = rules.find( |
| 215 | + (r) => r.scope.includes("markup.raw") && r.scope.includes("markup.raw.block"), |
| 216 | + )! |
| 217 | + |
| 218 | + // Background should NOT be pure white — that's the old invisible state |
| 219 | + expect(markupRawRule.style.background!.equals(WHITE)).toBe(false) |
| 220 | + }, |
| 221 | + ) |
| 222 | + |
| 223 | + test.each(LIGHT_THEMES)( |
| 224 | + "%s: markup.raw foreground is readable on its background", |
| 225 | + (_name, themeJson) => { |
| 226 | + const resolved = resolveTheme(themeJson, "light") |
| 227 | + const rules = getSyntaxRules(resolved) |
| 228 | + |
| 229 | + const markupRawRule = rules.find( |
| 230 | + (r) => r.scope.includes("markup.raw") && r.scope.includes("markup.raw.block"), |
| 231 | + )! |
| 232 | + |
| 233 | + const fg = markupRawRule.style.foreground! |
| 234 | + const bg = markupRawRule.style.background! |
| 235 | + |
| 236 | + const ratio = contrastRatio(fg, bg) |
| 237 | + expect(ratio).toBeGreaterThanOrEqual(2.5) |
| 238 | + }, |
| 239 | + ) |
| 240 | +}) |
| 241 | + |
| 242 | +describe("light theme: default foreground is not white (issue #617)", () => { |
| 243 | + test.each(LIGHT_THEMES)( |
| 244 | + "%s: default fg is not white", |
| 245 | + (_name, themeJson) => { |
| 246 | + const resolved = resolveTheme(themeJson, "light") |
| 247 | + |
| 248 | + if (!isLightBackground(resolved.background)) return |
| 249 | + |
| 250 | + const rules = getSyntaxRules(resolved) |
| 251 | + const defaultRule = rules.find((r) => r.scope.includes("default"))! |
| 252 | + |
| 253 | + // The fg must NOT be white — that's the hardcoded default that causes the bug |
| 254 | + expect(defaultRule.style.foreground!.equals(WHITE)).toBe(false) |
| 255 | + }, |
| 256 | + ) |
| 257 | + |
| 258 | + test.each(LIGHT_THEMES)( |
| 259 | + "%s: default fg has sufficient contrast against background", |
| 260 | + (_name, themeJson) => { |
| 261 | + const resolved = resolveTheme(themeJson, "light") |
| 262 | + |
| 263 | + if (!isLightBackground(resolved.background)) return |
| 264 | + |
| 265 | + const rules = getSyntaxRules(resolved) |
| 266 | + const defaultRule = rules.find((r) => r.scope.includes("default"))! |
| 267 | + |
| 268 | + const ratio = contrastRatio(defaultRule.style.foreground!, resolved.background) |
| 269 | + expect(ratio).toBeGreaterThanOrEqual(3) |
| 270 | + }, |
| 271 | + ) |
| 272 | +}) |
| 273 | + |
| 274 | +describe("light theme: theme.text is suitable for <markdown> fg prop", () => { |
| 275 | + test.each(LIGHT_THEMES)( |
| 276 | + "%s: theme.text is dark-colored (not white)", |
| 277 | + (_name, themeJson) => { |
| 278 | + const resolved = resolveTheme(themeJson, "light") |
| 279 | + |
| 280 | + if (!isLightBackground(resolved.background)) return |
| 281 | + |
| 282 | + // theme.text is what we pass as fg={theme.text} to the <markdown> element |
| 283 | + expect(resolved.text.equals(WHITE)).toBe(false) |
| 284 | + }, |
| 285 | + ) |
| 286 | + |
| 287 | + test.each(LIGHT_THEMES)( |
| 288 | + "%s: theme.text has >= 3:1 contrast against background", |
| 289 | + (_name, themeJson) => { |
| 290 | + const resolved = resolveTheme(themeJson, "light") |
| 291 | + |
| 292 | + if (!isLightBackground(resolved.background)) return |
| 293 | + |
| 294 | + const ratio = contrastRatio(resolved.text, resolved.background) |
| 295 | + expect(ratio).toBeGreaterThanOrEqual(3) |
| 296 | + }, |
| 297 | + ) |
| 298 | +}) |
| 299 | + |
| 300 | +describe("light theme: all syntax foregrounds are readable", () => { |
| 301 | + test.each(LIGHT_THEMES)( |
| 302 | + "%s: no syntax rule produces invisible text", |
| 303 | + (_name, themeJson) => { |
| 304 | + const resolved = resolveTheme(themeJson, "light") |
| 305 | + |
| 306 | + if (!isLightBackground(resolved.background)) return |
| 307 | + |
| 308 | + const rules = getSyntaxRules(resolved) |
| 309 | + for (const rule of rules) { |
| 310 | + if (!rule.style.foreground) continue |
| 311 | + |
| 312 | + const bg = rule.style.background ?? resolved.background |
| 313 | + const ratio = contrastRatio(rule.style.foreground, bg) |
| 314 | + |
| 315 | + expect(ratio).toBeGreaterThanOrEqual(2) |
| 316 | + } |
| 317 | + }, |
| 318 | + ) |
| 319 | +}) |
| 320 | + |
| 321 | +describe("dark theme: regression check", () => { |
| 322 | + const DARK_THEMES: [string, ThemeJson][] = [ |
| 323 | + ["github", github as unknown as ThemeJson], |
| 324 | + ["solarized", solarized as unknown as ThemeJson], |
| 325 | + ["flexoki", flexoki as unknown as ThemeJson], |
| 326 | + ] |
| 327 | + |
| 328 | + test.each(DARK_THEMES)( |
| 329 | + "%s: markup.raw scope has background set (no regression)", |
| 330 | + (_name, themeJson) => { |
| 331 | + const resolved = resolveTheme(themeJson, "dark") |
| 332 | + const rules = getSyntaxRules(resolved) |
| 333 | + |
| 334 | + const markupRawRule = rules.find( |
| 335 | + (r) => r.scope.includes("markup.raw") && r.scope.includes("markup.raw.block"), |
| 336 | + ) |
| 337 | + |
| 338 | + expect(markupRawRule).toBeDefined() |
| 339 | + expect(markupRawRule!.style.background).toBeDefined() |
| 340 | + }, |
| 341 | + ) |
| 342 | + |
| 343 | + test.each(DARK_THEMES)( |
| 344 | + "%s: default fg is set and not transparent", |
| 345 | + (_name, themeJson) => { |
| 346 | + const resolved = resolveTheme(themeJson, "dark") |
| 347 | + const rules = getSyntaxRules(resolved) |
| 348 | + |
| 349 | + const defaultRule = rules.find((r) => r.scope.includes("default"))! |
| 350 | + |
| 351 | + expect(defaultRule.style.foreground).toBeDefined() |
| 352 | + expect(defaultRule.style.foreground!.a).toBeGreaterThan(0) |
| 353 | + }, |
| 354 | + ) |
| 355 | +}) |
0 commit comments