The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { converter } from "culori/fn"; |
| 3 | import { BUILT_IN_THEMES } from "@t3tools/shared/themePalettes"; |
| 4 | |
| 5 | import { CONTEXT_CHIP_ACCENTS, contextChipColorVariable } from "./lib/contextChipAccents"; |
| 6 | import { appAtomRegistry } from "./rpc/atomRegistry"; |
| 7 | |
| 8 | import { |
| 9 | applyThemePalette, |
| 10 | canonicalThemePreference, |
| 11 | contextChipVariables, |
| 12 | getThemeDefinition, |
| 13 | getThemeColorsForMode, |
| 14 | getThemeModes, |
| 15 | getCustomThemes, |
| 16 | getStandardThemeColors, |
| 17 | getStoredCustomThemeCollection, |
| 18 | invalidateCustomThemes, |
| 19 | installCustomTheme, |
| 20 | parseThemeFile, |
| 21 | parseThemeHalves, |
| 22 | publishedThemeDefinitions, |
| 23 | removeCustomTheme, |
| 24 | replaceCustomThemeCollection, |
| 25 | resolveDesktopTheme, |
| 26 | resolveThemeAppearance, |
| 27 | serializeThemeFile, |
| 28 | T3_CHAT_THEME, |
| 29 | GROVE_THEME, |
| 30 | updateCustomTheme, |
| 31 | updateThemeColorFamily, |
| 32 | CUSTOM_THEMES_STORAGE_KEY, |
| 33 | createVividThemeColors, |
| 34 | getDefaultThemeColors, |
| 35 | themeColorToHex, |
| 36 | toCanonicalThemeColor, |
| 37 | THEME_FILE_VERSION, |
| 38 | } from "./themePalette"; |
| 39 | |
| 40 | function asHex(value: string): string { |
| 41 | const hex = themeColorToHex(value); |
| 42 | if (!hex) throw new Error(`Expected a theme color, received ${value}`); |
| 43 | return hex.slice(0, 7); |
| 44 | } |
| 45 | |
| 46 | function canonical(value: string): string { |
| 47 | const color = toCanonicalThemeColor(value); |
| 48 | if (!color) throw new Error(`Expected a theme color, received ${value}`); |
| 49 | return color; |
| 50 | } |
| 51 | |
| 52 | function contrastRatio(first: string, second: string): number { |
| 53 | const toRgb = (value: string) => { |
| 54 | const hex = asHex(value).slice(1); |
| 55 | return [0, 1, 2].map( |
| 56 | (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255, |
| 57 | ); |
| 58 | }; |
| 59 | const luminance = (value: string) => |
| 60 | toRgb(value) |
| 61 | .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4)) |
| 62 | .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0); |
| 63 | const lighter = Math.max(luminance(first), luminance(second)); |
| 64 | const darker = Math.min(luminance(first), luminance(second)); |
| 65 | return (lighter + 0.05) / (darker + 0.05); |
| 66 | } |
| 67 | |
| 68 | describe("theme files", () => { |
| 69 | it("gives non-Latin theme names distinct stable ids", () => { |
| 70 | const file = { |
| 71 | version: THEME_FILE_VERSION, |
| 72 | appearance: "dark", |
| 73 | colors: { canvas: "#101820" }, |
| 74 | }; |
| 75 | const night = parseThemeFile({ ...file, name: "夜" }); |
| 76 | expect(parseThemeFile({ ...file, name: "朝" }).id).not.toBe(night.id); |
| 77 | expect(parseThemeFile({ ...file, name: " 夜 " }).id).toBe(night.id); |
| 78 | }); |
| 79 | |
| 80 | it("keeps built-in and standard search pairs readable and distinct from inline code", () => { |
| 81 | const toOklab = converter("oklab"); |
| 82 | const distance = (first: string, second: string) => { |
| 83 | const a = toOklab(first)!; |
| 84 | const b = toOklab(second)!; |
| 85 | return Math.hypot(a.l - b.l, a.a - b.a, a.b - b.b); |
| 86 | }; |
| 87 | const palettes = [ |
| 88 | getStandardThemeColors("light"), |
| 89 | getStandardThemeColors("dark"), |
| 90 | ...BUILT_IN_THEMES.flatMap((theme) => [theme.colors, ...Object.values(theme.variants ?? {})]), |
| 91 | ]; |
| 92 | for (const colors of palettes) { |
| 93 | expect( |
| 94 | contrastRatio(colors.searchMatchForeground, colors.searchMatchBackground), |
| 95 | ).toBeGreaterThanOrEqual(4.5); |
| 96 | expect( |
| 97 | contrastRatio(colors.searchMatchActiveForeground, colors.searchMatchActiveBackground), |
| 98 | ).toBeGreaterThanOrEqual(4.5); |
| 99 | expect( |
| 100 | distance(colors.searchMatchBackground, colors.searchMatchActiveBackground), |
| 101 | ).toBeGreaterThan(0.14); |
| 102 | for (const background of [colors.muted, colors.messageSurface, colors.codeBackground]) { |
| 103 | expect(distance(colors.searchMatchBackground, background)).toBeGreaterThan(0.08); |
| 104 | expect(distance(colors.searchMatchActiveBackground, background)).toBeGreaterThan(0.08); |
| 105 | } |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | it("derives an imported palette from top-level canvas and accent like a published theme", () => { |
| 110 | const theme = parseThemeFile({ |
| 111 | version: THEME_FILE_VERSION, |
| 112 | name: "Teal", |
| 113 | appearance: "dark", |
| 114 | canvas: "#0b1a1e", |
| 115 | accent: "#f08a2c", |
| 116 | colors: { sidebar: "#0f2126" }, |
| 117 | }); |
| 118 | |
| 119 | expect(theme.colors.canvas).toBe(canonical("#0b1a1e")); |
| 120 | expect(theme.colors.accent).toBe(canonical("#f08a2c")); |
| 121 | expect(theme.colors.sidebar).toBe(canonical("#0f2126")); |
| 122 | }); |
| 123 | |
| 124 | it("round-trips custom search colours and fills them in for older theme files", () => { |
| 125 | const legacy = parseThemeFile({ |
| 126 | version: THEME_FILE_VERSION, |
| 127 | name: "Legacy", |
| 128 | appearance: "dark", |
| 129 | colors: { canvas: "#101010" }, |
| 130 | }); |
| 131 | expect(legacy.colors.searchMatchBackground).toBe( |
| 132 | getDefaultThemeColors("dark").searchMatchBackground, |
| 133 | ); |
| 134 | const theme = parseThemeFile({ |
| 135 | version: THEME_FILE_VERSION, |
| 136 | name: "Search", |
| 137 | appearance: "dark", |
| 138 | colors: { |
| 139 | searchMatchBackground: "#224466", |
| 140 | searchMatchForeground: "#ffffff", |
| 141 | searchMatchActiveBackground: "#aaddff", |
| 142 | searchMatchActiveForeground: "#112233", |
| 143 | }, |
| 144 | variants: { light: { searchMatchBackground: "#ddeeff", searchMatchForeground: "#112233" } }, |
| 145 | }); |
| 146 | const restored = parseThemeFile(JSON.parse(serializeThemeFile(theme))); |
| 147 | expect(restored.colors).toEqual(theme.colors); |
| 148 | expect(restored.variants).toEqual(theme.variants); |
| 149 | }); |
| 150 | |
| 151 | it.each(["searchMatchBackground", "searchMatchActiveBackground"] as const)( |
| 152 | "editing %s derives readable text without changing the other match or warning colours", |
| 153 | (role) => { |
| 154 | const original = getDefaultThemeColors("dark"); |
| 155 | const foreground = |
| 156 | role === "searchMatchBackground" ? "searchMatchForeground" : "searchMatchActiveForeground"; |
| 157 | for (const value of ["#ffffff", "#000000", "#33669980"]) { |
| 158 | const updated = updateThemeColorFamily("dark", original, role, value); |
| 159 | expect(contrastRatio(updated[foreground], updated[role])).toBeGreaterThanOrEqual(4.5); |
| 160 | for (const key of Object.keys(original) as Array<keyof typeof original>) { |
| 161 | if (key !== role && key !== foreground) expect(updated[key]).toBe(original[key]); |
| 162 | } |
| 163 | } |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | it("derives readable, distinctive vivid palettes from exact seeds", () => { |
| 168 | const seeds: ReadonlyArray<["light" | "dark", string, string]> = [ |
| 169 | ["light", "#f4f9f2", "#1d8a4e"], |
| 170 | ["dark", "#101a2c", "#4f8fe8"], |
| 171 | ["dark", "#211a23", "#df5398"], |
| 172 | ["light", "#fdf6ec", "#c2571b"], |
| 173 | ["light", "#111827", "#8ab4f8"], |
| 174 | ["dark", "#f5ecf5", "#a84370"], |
| 175 | ]; |
| 176 | const channels = (value: string) => |
| 177 | [1, 3, 5].map((index) => Number.parseInt(asHex(value).slice(index, index + 2), 16)) as [ |
| 178 | number, |
| 179 | number, |
| 180 | number, |
| 181 | ]; |
| 182 | for (const [appearance, canvas, accent] of seeds) { |
| 183 | const colors = createVividThemeColors(appearance, canvas, accent); |
| 184 | expect(colors.canvas).toMatch(/^oklch\(/); |
| 185 | expect(colors.accent).toMatch(/^oklch\(/); |
| 186 | expect(asHex(colors.canvas)).toBe(canvas); |
| 187 | expect(asHex(colors.accent)).toBe(accent); |
| 188 | expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(7); |
| 189 | expect(contrastRatio(colors.textMuted, colors.canvas)).toBeGreaterThanOrEqual(4.5); |
| 190 | expect(contrastRatio(colors.textMuted, colors.canvas)).toBeLessThan(5.5); |
| 191 | expect(contrastRatio(colors.mutedForeground, colors.muted)).toBeGreaterThanOrEqual(4.5); |
| 192 | expect(contrastRatio(colors.mutedForeground, colors.muted)).toBeLessThan( |
| 193 | contrastRatio(colors.text, colors.muted), |
| 194 | ); |
| 195 | expect(contrastRatio(colors.placeholder, colors.surfaceRaised)).toBeGreaterThanOrEqual(4.5); |
| 196 | expect(contrastRatio(colors.placeholder, colors.surfaceRaised)).toBeLessThan( |
| 197 | contrastRatio(colors.text, colors.surfaceRaised), |
| 198 | ); |
| 199 | expect(colors.secondaryLabel).toBe(colors.textMuted); |
| 200 | expect(contrastRatio(colors.accentForeground, colors.accent)).toBeGreaterThanOrEqual(4.5); |
| 201 | expect( |
| 202 | contrastRatio(colors.searchMatchForeground, colors.searchMatchBackground), |
| 203 | ).toBeGreaterThanOrEqual(4.5); |
| 204 | expect( |
| 205 | contrastRatio(colors.searchMatchActiveForeground, colors.searchMatchActiveBackground), |
| 206 | ).toBeGreaterThanOrEqual(4.5); |
| 207 | expect( |
| 208 | contrastRatio(colors.messageActionForeground, colors.messageAction), |
| 209 | ).toBeGreaterThanOrEqual(4.5); |
| 210 | expect(contrastRatio(colors.messageForeground, colors.messageSurface)).toBeGreaterThanOrEqual( |
| 211 | 4.5, |
| 212 | ); |
| 213 | expect(contrastRatio(colors.secondaryForeground, colors.secondary)).toBeGreaterThanOrEqual( |
| 214 | 4.5, |
| 215 | ); |
| 216 | expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5); |
| 217 | expect(colors.messageAction).not.toBe(colors.accent); |
| 218 | expect(asHex(colors.update)).toBe(accent); |
| 219 | const [errorRed, errorGreen, errorBlue] = channels(colors.error); |
| 220 | expect(errorRed).toBeGreaterThan(errorGreen * 2); |
| 221 | expect(errorRed).toBeGreaterThan(errorBlue * 2); |
| 222 | expect(contrastRatio(colors.error, "#ffffff")).toBeGreaterThanOrEqual(2.5); |
| 223 | expect(contrastRatio(colors.errorForeground, colors.errorSurface)).toBeGreaterThanOrEqual( |
| 224 | 4.5, |
| 225 | ); |
| 226 | const [warnRed, warnGreen, warnBlue] = channels(colors.warning); |
| 227 | expect(warnRed).toBeGreaterThan(warnBlue); |
| 228 | expect(warnGreen).toBeGreaterThan(warnBlue); |
| 229 | } |
| 230 | }); |
| 231 | |
| 232 | it("decodes literal CSS color formats into OKLCH without dropping alpha", () => { |
| 233 | const theme = parseThemeFile({ |
| 234 | version: THEME_FILE_VERSION, |
| 235 | name: "Translucent", |
| 236 | appearance: "light", |
| 237 | colors: { |
| 238 | canvas: "oklch(62% 0.2 280deg / 50%)", |
| 239 | accent: "#abcd", |
| 240 | focus: "rgb(10 20 30 / 50%)", |
| 241 | error: "hsl(350 80% 50%)", |
| 242 | warning: "hwb(45 10% 20%)", |
| 243 | update: "lab(60% 40 30)", |
| 244 | messageAction: "lch(60% 50 120)", |
| 245 | sidebar: "oklab(0.6 0.1 -0.1)", |
| 246 | terminalCursor: "color(display-p3 0.8 0.2 0.3)", |
| 247 | terminalSelection: "rebeccapurple", |
| 248 | terminalScrollbar: "transparent", |
| 249 | terminalScrollbarHover: "rgb(10 20 30 / none)", |
| 250 | }, |
| 251 | }); |
| 252 | |
| 253 | expect(theme.colors.canvas).toBe("oklch(0.62 0.2 280 / 0.5)"); |
| 254 | expect(theme.colors.accent).toBe(canonical("#abcd")); |
| 255 | expect(themeColorToHex(theme.colors.accent)).toBe("#aabbccdd"); |
| 256 | expect(themeColorToHex(theme.colors.focus)).toBe("#0a141e80"); |
| 257 | expect(themeColorToHex(theme.colors.terminalSelection)).toBe("#663399"); |
| 258 | expect(theme.colors.terminalScrollbar).toBe("oklch(0 0 0 / 0)"); |
| 259 | expect(themeColorToHex(theme.colors.terminalScrollbarHover)).toBe("#0a141e00"); |
| 260 | for (const role of [ |
| 261 | "error", |
| 262 | "warning", |
| 263 | "update", |
| 264 | "messageAction", |
| 265 | "sidebar", |
| 266 | "terminalCursor", |
| 267 | ] as const) { |
| 268 | expect(theme.colors[role]).toMatch(/^oklch\(/); |
| 269 | } |
| 270 | }); |
| 271 | |
| 272 | it("gamut maps extreme finite OKLCH chroma from theme files", () => { |
| 273 | const theme = parseThemeFile({ |
| 274 | version: THEME_FILE_VERSION, |
| 275 | name: "Extreme chroma", |
| 276 | appearance: "light", |
| 277 | colors: { accent: "oklch(0.5 1e303 0)" }, |
| 278 | }); |
| 279 | |
| 280 | expect(theme.colors.accent).toBe("oklch(0.5 1e+303 0)"); |
| 281 | expect(themeColorToHex(theme.colors.accent)).toBe("#b5005e"); |
| 282 | }); |
| 283 | |
| 284 | it("keeps optional light and dark palettes under one theme id", () => { |
| 285 | const theme = parseThemeFile({ |
| 286 | version: THEME_FILE_VERSION, |
| 287 | id: "aurora", |
| 288 | name: "Aurora", |
| 289 | appearance: "light", |
| 290 | colors: { canvas: "#f8fbff", text: "#10243d" }, |
| 291 | variants: { |
| 292 | dark: { canvas: "#101827", text: "#eef5ff" }, |
| 293 | }, |
| 294 | css: "[data-chat-header] { border-bottom: 1px solid red; }", |
| 295 | }); |
| 296 | |
| 297 | expect(JSON.parse(serializeThemeFile(theme)).css).toBe(theme.css); |
| 298 | expect(getThemeModes(theme)).toEqual(["light", "dark"]); |
| 299 | expect(getThemeColorsForMode(theme, "dark")).toMatchObject({ |
| 300 | canvas: canonical("#101827"), |
| 301 | text: canonical("#eef5ff"), |
| 302 | }); |
| 303 | expect(getThemeModes(T3_CHAT_THEME)).toEqual(["light", "dark"]); |
| 304 | expect(resolveThemeAppearance(T3_CHAT_THEME.id, true, true)).toBe("dark"); |
| 305 | expect(resolveDesktopTheme(T3_CHAT_THEME.id, true)).toBe("system"); |
| 306 | expect(resolveThemeAppearance(T3_CHAT_THEME.id, false, false, "dark")).toBe("dark"); |
| 307 | expect(resolveDesktopTheme(T3_CHAT_THEME.id, false, "dark")).toBe("dark"); |
| 308 | expect(JSON.parse(serializeThemeFile(theme)).variants.dark).toMatchObject({ |
| 309 | canvas: canonical("#101827"), |
| 310 | text: canonical("#eef5ff"), |
| 311 | }); |
| 312 | }); |
| 313 | |
| 314 | it("preserves valid imported-theme collections and drops malformed metadata", () => { |
| 315 | vi.stubGlobal("window", { |
| 316 | localStorage: { |
| 317 | getItem: (key: string) => |
| 318 | key === CUSTOM_THEMES_STORAGE_KEY |
| 319 | ? JSON.stringify([ |
| 320 | { |
| 321 | id: "github-dark", |
| 322 | label: "GitHub Dark", |
| 323 | appearance: "dark", |
| 324 | colors: { canvas: "#0d1117" }, |
| 325 | collection: { id: "open-vsx:github.github-vscode-theme", label: "GitHub Theme" }, |
| 326 | }, |
| 327 | { |
| 328 | id: "github-light", |
| 329 | label: "GitHub Light", |
| 330 | appearance: "light", |
| 331 | colors: { canvas: "#ffffff" }, |
| 332 | collection: { id: "bad collection id", label: "GitHub Theme" }, |
| 333 | }, |
| 334 | ]) |
| 335 | : null, |
| 336 | }, |
| 337 | }); |
| 338 | |
| 339 | invalidateCustomThemes(); |
| 340 | expect(getCustomThemes()).toMatchObject([ |
| 341 | { collection: { id: "open-vsx:github.github-vscode-theme", label: "GitHub Theme" } }, |
| 342 | { id: "github-light" }, |
| 343 | ]); |
| 344 | expect(getCustomThemes()[1]).not.toHaveProperty("collection"); |
| 345 | |
| 346 | vi.unstubAllGlobals(); |
| 347 | invalidateCustomThemes(); |
| 348 | }); |
| 349 | |
| 350 | it("atomically replaces an imported collection and removes stale variants", () => { |
| 351 | const collection = { id: "open-vsx:demo.theme", label: "Demo Theme" }; |
| 352 | const personalTheme = { |
| 353 | id: "personal", |
| 354 | label: "Personal", |
| 355 | appearance: "dark", |
| 356 | colors: { canvas: "#111111", futureRole: "hsl(10 20% 30%)" }, |
| 357 | futureMetadata: { version: 2 }, |
| 358 | }; |
| 359 | const stored = new Map<string, string>([ |
| 360 | [ |
| 361 | CUSTOM_THEMES_STORAGE_KEY, |
| 362 | JSON.stringify([ |
| 363 | personalTheme, |
| 364 | { |
| 365 | id: "old-light", |
| 366 | label: "Old Light", |
| 367 | appearance: "light", |
| 368 | colors: { canvas: "#ffffff" }, |
| 369 | collection, |
| 370 | }, |
| 371 | { |
| 372 | id: "removed-dark", |
| 373 | label: "Removed Dark", |
| 374 | appearance: "dark", |
| 375 | colors: { canvas: "#000000" }, |
| 376 | collection, |
| 377 | }, |
| 378 | ]), |
| 379 | ], |
| 380 | ]); |
| 381 | const setItem = vi.fn((key: string, value: string) => stored.set(key, value)); |
| 382 | vi.stubGlobal("window", { |
| 383 | localStorage: { |
| 384 | getItem: (key: string) => stored.get(key) ?? null, |
| 385 | setItem, |
| 386 | }, |
| 387 | }); |
| 388 | |
| 389 | invalidateCustomThemes(); |
| 390 | const expectedCollection = getStoredCustomThemeCollection(collection.id); |
| 391 | getCustomThemes(); |
| 392 | const concurrentlyAddedTheme = { |
| 393 | id: "other-tab", |
| 394 | label: "Other Tab", |
| 395 | appearance: "dark", |
| 396 | colors: { canvas: "#222222" }, |
| 397 | }; |
| 398 | stored.set( |
| 399 | CUSTOM_THEMES_STORAGE_KEY, |
| 400 | JSON.stringify([ |
| 401 | ...JSON.parse(stored.get(CUSTOM_THEMES_STORAGE_KEY) ?? "[]"), |
| 402 | concurrentlyAddedTheme, |
| 403 | ]), |
| 404 | ); |
| 405 | const replacement = [ |
| 406 | { |
| 407 | ...parseThemeFile({ |
| 408 | version: THEME_FILE_VERSION, |
| 409 | id: "old-light", |
| 410 | name: "New Light", |
| 411 | appearance: "light", |
| 412 | colors: { canvas: "#fafafa" }, |
| 413 | }), |
| 414 | collection, |
| 415 | }, |
| 416 | { |
| 417 | ...parseThemeFile({ |
| 418 | version: THEME_FILE_VERSION, |
| 419 | id: "new-dark", |
| 420 | name: "New Dark", |
| 421 | appearance: "dark", |
| 422 | colors: { canvas: "#101010" }, |
| 423 | }), |
| 424 | collection, |
| 425 | }, |
| 426 | ]; |
| 427 | |
| 428 | expect( |
| 429 | replaceCustomThemeCollection(collection.id, replacement, { expectedCollection }), |
| 430 | ).toEqual(replacement); |
| 431 | expect(getCustomThemes().map((theme) => theme.id)).toEqual([ |
| 432 | "personal", |
| 433 | "old-light", |
| 434 | "new-dark", |
| 435 | "other-tab", |
| 436 | ]); |
| 437 | expect(setItem).toHaveBeenCalledTimes(1); |
| 438 | expect(JSON.parse(stored.get(CUSTOM_THEMES_STORAGE_KEY) ?? "[]")[0]).toEqual(personalTheme); |
| 439 | |
| 440 | vi.unstubAllGlobals(); |
| 441 | invalidateCustomThemes(); |
| 442 | }); |
| 443 | |
| 444 | it("canonicalizes explicit writes without migrating untouched themes", () => { |
| 445 | const stored = new Map<string, string>(); |
| 446 | const untouchedTheme = { |
| 447 | id: "legacy", |
| 448 | label: "Legacy", |
| 449 | appearance: "dark", |
| 450 | colors: { accent: "#5b6cff", futureRole: "hsl(10 20% 30%)" }, |
| 451 | futureMetadata: { version: 2 }, |
| 452 | }; |
| 453 | stored.set(CUSTOM_THEMES_STORAGE_KEY, JSON.stringify([untouchedTheme])); |
| 454 | vi.stubGlobal("window", { |
| 455 | localStorage: { |
| 456 | getItem: (key: string) => stored.get(key) ?? null, |
| 457 | setItem: (key: string, value: string) => stored.set(key, value), |
| 458 | }, |
| 459 | }); |
| 460 | |
| 461 | invalidateCustomThemes(); |
| 462 | const createdTheme = installCustomTheme( |
| 463 | parseThemeFile({ |
| 464 | version: THEME_FILE_VERSION, |
| 465 | id: "aurora", |
| 466 | name: "Aurora", |
| 467 | appearance: "light", |
| 468 | colors: { canvas: "#f8fbff", accent: "#5b6cff" }, |
| 469 | sidebarArtwork: true, |
| 470 | }), |
| 471 | ); |
| 472 | const updatedTheme = updateCustomTheme({ |
| 473 | ...createdTheme, |
| 474 | label: "Aurora Night", |
| 475 | colors: { ...createdTheme.colors, accent: "hsl(263 70% 58%)" }, |
| 476 | }); |
| 477 | |
| 478 | expect(updatedTheme).toMatchObject({ |
| 479 | id: "aurora", |
| 480 | label: "Aurora Night", |
| 481 | colors: { accent: canonical("hsl(263 70% 58%)") }, |
| 482 | }); |
| 483 | expect(updatedTheme).not.toHaveProperty("sidebarArtwork"); |
| 484 | const storedThemes = JSON.parse(stored.get(CUSTOM_THEMES_STORAGE_KEY) ?? "[]"); |
| 485 | expect(storedThemes[0]).toEqual(untouchedTheme); |
| 486 | expect(storedThemes[1]).toMatchObject({ |
| 487 | id: "aurora", |
| 488 | label: "Aurora Night", |
| 489 | colors: { accent: canonical("hsl(263 70% 58%)") }, |
| 490 | }); |
| 491 | expect(storedThemes[1]).not.toHaveProperty("sidebarArtwork"); |
| 492 | invalidateCustomThemes(); |
| 493 | expect(getCustomThemes().find((theme) => theme.id === "aurora")).toMatchObject({ |
| 494 | id: "aurora", |
| 495 | colors: { accent: canonical("hsl(263 70% 58%)") }, |
| 496 | }); |
| 497 | removeCustomTheme("aurora"); |
| 498 | expect(JSON.parse(stored.get(CUSTOM_THEMES_STORAGE_KEY) ?? "[]")).toEqual([untouchedTheme]); |
| 499 | |
| 500 | vi.unstubAllGlobals(); |
| 501 | invalidateCustomThemes(); |
| 502 | }); |
| 503 | |
| 504 | it("writes from the cached raw snapshot without risking a destructive reread", () => { |
| 505 | const legacyTheme = { |
| 506 | id: "legacy", |
| 507 | label: "Legacy", |
| 508 | appearance: "dark", |
| 509 | colors: { accent: "#5b6cff" }, |
| 510 | futureMetadata: true, |
| 511 | }; |
| 512 | let storedThemes = JSON.stringify([legacyTheme]); |
| 513 | let readCount = 0; |
| 514 | const setItem = vi.fn((_key: string, value: string) => { |
| 515 | storedThemes = value; |
| 516 | }); |
| 517 | vi.stubGlobal("window", { |
| 518 | localStorage: { |
| 519 | getItem: () => { |
| 520 | readCount += 1; |
| 521 | if (readCount > 1) throw new Error("transient read failure"); |
| 522 | return storedThemes; |
| 523 | }, |
| 524 | setItem, |
| 525 | }, |
| 526 | }); |
| 527 | |
| 528 | invalidateCustomThemes(); |
| 529 | expect(getCustomThemes()).toHaveLength(1); |
| 530 | installCustomTheme( |
| 531 | parseThemeFile({ |
| 532 | version: THEME_FILE_VERSION, |
| 533 | id: "aurora", |
| 534 | name: "Aurora", |
| 535 | appearance: "light", |
| 536 | colors: { accent: "hsl(263 70% 58%)" }, |
| 537 | }), |
| 538 | ); |
| 539 | |
| 540 | expect(readCount).toBe(1); |
| 541 | expect(setItem).toHaveBeenCalledOnce(); |
| 542 | expect(JSON.parse(storedThemes)[0]).toEqual(legacyTheme); |
| 543 | |
| 544 | vi.unstubAllGlobals(); |
| 545 | invalidateCustomThemes(); |
| 546 | }); |
| 547 | |
| 548 | it("refuses to overwrite a theme library that could not be read", () => { |
| 549 | const setItem = vi.fn(); |
| 550 | vi.stubGlobal("window", { |
| 551 | localStorage: { |
| 552 | getItem: () => { |
| 553 | throw new Error("storage unavailable"); |
| 554 | }, |
| 555 | setItem, |
| 556 | }, |
| 557 | }); |
| 558 | |
| 559 | invalidateCustomThemes(); |
| 560 | expect(getCustomThemes()).toEqual([]); |
| 561 | expect(() => |
| 562 | installCustomTheme( |
| 563 | parseThemeFile({ |
| 564 | version: THEME_FILE_VERSION, |
| 565 | id: "aurora", |
| 566 | name: "Aurora", |
| 567 | appearance: "light", |
| 568 | colors: { accent: "#5b6cff" }, |
| 569 | }), |
| 570 | ), |
| 571 | ).toThrow(`Failed to read the theme library from ${CUSTOM_THEMES_STORAGE_KEY}.`); |
| 572 | expect(setItem).not.toHaveBeenCalled(); |
| 573 | |
| 574 | vi.unstubAllGlobals(); |
| 575 | invalidateCustomThemes(); |
| 576 | }); |
| 577 | }); |
| 578 | |
| 579 | describe("context chip colors", () => { |
| 580 | it.each([ |
| 581 | ["#ffffff", 5], |
| 582 | ["#777777", 4.5], |
| 583 | ] as const)( |
| 584 | "chooses readable chip lightness on %s when the text is medium gray", |
| 585 | (surface, minimum) => { |
| 586 | const colors = { |
| 587 | ...getStandardThemeColors("light"), |
| 588 | canvas: surface, |
| 589 | surface, |
| 590 | surfaceRaised: surface, |
| 591 | messageSurface: surface, |
| 592 | text: "#767676", |
| 593 | messageForeground: "#767676", |
| 594 | }; |
| 595 | for (const color of Object.values(contextChipVariables(colors))) { |
| 596 | expect(contrastRatio(color, surface)).toBeGreaterThanOrEqual(minimum); |
| 597 | } |
| 598 | }, |
| 599 | ); |
| 600 | |
| 601 | const toOklch = converter("oklch"); |
| 602 | const toRgb = converter("rgb"); |
| 603 | // Surfaces stay unrounded, as CSS paints them; the chip is the 8-bit hex the solver returns. |
| 604 | const paintedContrastRatio = (chip: string, surface: string, canvas: string) => { |
| 605 | const channels = (value: string) => { |
| 606 | const rgb = toRgb(value)!; |
| 607 | return [rgb.r, rgb.g, rgb.b].map((channel) => Math.min(1, Math.max(0, channel))); |
| 608 | }; |
| 609 | const back = channels(canvas); |
| 610 | const alpha = toRgb(surface)!.alpha ?? 1; |
| 611 | const painted = channels(surface).map( |
| 612 | (channel, index) => back[index]! * (1 - alpha) + channel * alpha, |
| 613 | ); |
| 614 | const luminance = (rgb: number[]) => |
| 615 | rgb |
| 616 | .map((channel) => |
| 617 | channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4, |
| 618 | ) |
| 619 | .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0); |
| 620 | const first = luminance(channels(asHex(chip))); |
| 621 | const second = luminance(painted); |
| 622 | return (Math.max(first, second) + 0.05) / (Math.min(first, second) + 0.05); |
| 623 | }; |
| 624 | const palettes = [ |
| 625 | getStandardThemeColors("light"), |
| 626 | getStandardThemeColors("dark"), |
| 627 | ...BUILT_IN_THEMES.flatMap((theme) => |
| 628 | getThemeModes(theme).map((mode) => getThemeColorsForMode(theme, mode)!), |
| 629 | ), |
| 630 | ]; |
| 631 | |
| 632 | it("reads at 5:1 on every chip surface of every built-in theme and keeps each hue", () => { |
| 633 | for (const colors of palettes) { |
| 634 | const variables = contextChipVariables(colors); |
| 635 | expect(variables["--context-chip-message"]).toBeUndefined(); |
| 636 | for (const [kind, accent] of Object.entries(CONTEXT_CHIP_ACCENTS)) { |
| 637 | const chip = |
| 638 | variables[contextChipColorVariable(kind as keyof typeof CONTEXT_CHIP_ACCENTS)]!; |
| 639 | for (const surface of [ |
| 640 | colors.canvas, |
| 641 | colors.surface, |
| 642 | colors.surfaceRaised, |
| 643 | colors.messageSurface, |
| 644 | ]) { |
| 645 | expect(paintedContrastRatio(chip, surface, colors.canvas)).toBeGreaterThanOrEqual(5); |
| 646 | } |
| 647 | const solved = toOklch(chip)!; |
| 648 | if ((solved.c ?? 0) > 0.04) { |
| 649 | const drift = Math.abs((((solved.h ?? 0) - toOklch(accent)!.h! + 540) % 360) - 180); |
| 650 | expect(drift).toBeLessThan(4); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | }); |
| 655 | |
| 656 | it("reads at 5:1 on the standard surfaces as index.css paints them", () => { |
| 657 | const painted = { |
| 658 | light: ["#fcfcfc", "#ffffff", "#ebebed"], |
| 659 | dark: ["#000000", "#0a0a0a", "#111111", "#141414", "#1e1e1e"], |
| 660 | } as const; |
| 661 | const properties = new Map<string, string>(); |
| 662 | vi.stubGlobal("window", { localStorage: { getItem: () => null } }); |
| 663 | vi.stubGlobal("document", { |
| 664 | documentElement: { |
| 665 | dataset: {}, |
| 666 | style: { |
| 667 | setProperty: (name: string, value: string) => properties.set(name, value), |
| 668 | removeProperty: (name: string) => properties.delete(name), |
| 669 | }, |
| 670 | }, |
| 671 | }); |
| 672 | invalidateCustomThemes(); |
| 673 | try { |
| 674 | for (const appearance of ["light", "dark"] as const) { |
| 675 | applyThemePalette("system", appearance); |
| 676 | for (const kind of Object.keys(CONTEXT_CHIP_ACCENTS) as Array< |
| 677 | keyof typeof CONTEXT_CHIP_ACCENTS |
| 678 | >) { |
| 679 | for (const surface of painted[appearance]) { |
| 680 | expect( |
| 681 | contrastRatio(properties.get(contextChipColorVariable(kind))!, surface), |
| 682 | ).toBeGreaterThanOrEqual(5); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } finally { |
| 687 | vi.unstubAllGlobals(); |
| 688 | invalidateCustomThemes(); |
| 689 | } |
| 690 | }); |
| 691 | |
| 692 | it("solves the invalid state's error color to 5:1 on every chip surface", () => { |
| 693 | for (const colors of palettes) { |
| 694 | const invalid = contextChipVariables(colors)["--context-chip-invalid"]; |
| 695 | expect(invalid).toBeDefined(); |
| 696 | for (const surface of [ |
| 697 | colors.canvas, |
| 698 | colors.surface, |
| 699 | colors.surfaceRaised, |
| 700 | colors.messageSurface, |
| 701 | ]) { |
| 702 | expect(paintedContrastRatio(invalid!, surface, colors.canvas)).toBeGreaterThanOrEqual(5); |
| 703 | } |
| 704 | } |
| 705 | }); |
| 706 | |
| 707 | it("leaves a bubble with opposite-shade text out of the shared solve", () => { |
| 708 | const colors = { |
| 709 | ...getStandardThemeColors("light"), |
| 710 | messageSurface: "#1b4ed8", |
| 711 | messageForeground: "#ffffff", |
| 712 | }; |
| 713 | const variables = contextChipVariables(colors); |
| 714 | expect(variables["--context-chip-message"]).toBe("#ffffff"); |
| 715 | for (const kind of Object.keys(CONTEXT_CHIP_ACCENTS) as Array< |
| 716 | keyof typeof CONTEXT_CHIP_ACCENTS |
| 717 | >) { |
| 718 | expect( |
| 719 | contrastRatio(variables[contextChipColorVariable(kind)]!, colors.canvas), |
| 720 | ).toBeGreaterThanOrEqual(5); |
| 721 | } |
| 722 | expect(toOklch(variables[contextChipColorVariable("file")])!.c).toBeGreaterThan(0.08); |
| 723 | }); |
| 724 | }); |
| 725 | |
| 726 | describe("stored theme preferences", () => { |
| 727 | it("lets built-in and published halves unlock an appearance the base theme lacks", () => { |
| 728 | const lightOnly = parseThemeFile({ |
| 729 | version: THEME_FILE_VERSION, |
| 730 | id: "paper", |
| 731 | name: "Paper", |
| 732 | appearance: "light", |
| 733 | colors: { canvas: "#f8fbff" }, |
| 734 | }); |
| 735 | const [publishedTheme] = publishedThemeDefinitions([ |
| 736 | { |
| 737 | id: "constructor", |
| 738 | name: "Constructor", |
| 739 | appearance: "dark", |
| 740 | canvas: "#1a1b26", |
| 741 | accent: "#7aa2f7", |
| 742 | }, |
| 743 | ]); |
| 744 | expect(publishedTheme).toBeDefined(); |
| 745 | using _environmentThemes = vi.spyOn(appAtomRegistry, "get").mockReturnValue([publishedTheme]); |
| 746 | vi.stubGlobal("window", { |
| 747 | localStorage: { |
| 748 | getItem: (key: string) => |
| 749 | key === CUSTOM_THEMES_STORAGE_KEY ? JSON.stringify([lightOnly]) : null, |
| 750 | }, |
| 751 | }); |
| 752 | invalidateCustomThemes(); |
| 753 | try { |
| 754 | expect(resolveThemeAppearance("paper", true, true)).toBe("light"); |
| 755 | const halves = { dark: GROVE_THEME.id }; |
| 756 | expect(resolveThemeAppearance("paper", true, true, undefined, halves)).toBe("dark"); |
| 757 | expect(resolveThemeAppearance("paper", false, false, "dark", halves)).toBe("dark"); |
| 758 | expect(resolveDesktopTheme("paper", true, undefined, halves)).toBe("system"); |
| 759 | |
| 760 | expect(getThemeDefinition("constructor")).toEqual(publishedTheme); |
| 761 | expect(canonicalThemePreference("constructor")).toBe("constructor"); |
| 762 | const publishedHalves = parseThemeHalves(JSON.stringify({ dark: "constructor" })); |
| 763 | expect(publishedHalves).toEqual({ dark: "constructor" }); |
| 764 | expect(resolveThemeAppearance("paper", true, true, undefined, publishedHalves)).toBe("dark"); |
| 765 | } finally { |
| 766 | vi.unstubAllGlobals(); |
| 767 | invalidateCustomThemes(); |
| 768 | } |
| 769 | }); |
| 770 | |
| 771 | it("decodes stored colors in memory without writing during reads", () => { |
| 772 | const storedThemes = JSON.stringify([ |
| 773 | { |
| 774 | id: "aurora", |
| 775 | label: "Aurora", |
| 776 | appearance: "light", |
| 777 | colors: { canvas: "#f8fbff", futureRole: "#123456", accent: "not-a-color" }, |
| 778 | variants: { dark: { canvas: "rgb(16 24 39)" } }, |
| 779 | }, |
| 780 | { id: "light", label: "Reserved", appearance: "light", colors: {} }, |
| 781 | { id: "aurora", label: "Duplicate", appearance: "dark", colors: {} }, |
| 782 | ]); |
| 783 | const setItem = vi.fn(); |
| 784 | vi.stubGlobal("window", { |
| 785 | localStorage: { |
| 786 | getItem: (key: string) => (key === CUSTOM_THEMES_STORAGE_KEY ? storedThemes : null), |
| 787 | setItem, |
| 788 | }, |
| 789 | }); |
| 790 | invalidateCustomThemes(); |
| 791 | |
| 792 | const themes = getCustomThemes(); |
| 793 | expect(themes).toHaveLength(1); |
| 794 | expect(themes[0]).toMatchObject({ |
| 795 | id: "aurora", |
| 796 | colors: { canvas: canonical("#f8fbff"), accent: getDefaultThemeColors("light").accent }, |
| 797 | }); |
| 798 | expect(getThemeModes(themes[0]!)).toEqual(["light", "dark"]); |
| 799 | expect(getThemeColorsForMode(themes[0]!, "dark")?.canvas).toBe(canonical("rgb(16 24 39)")); |
| 800 | expect(setItem).not.toHaveBeenCalled(); |
| 801 | expect(JSON.parse(storedThemes)[0].colors).toEqual({ |
| 802 | canvas: "#f8fbff", |
| 803 | futureRole: "#123456", |
| 804 | accent: "not-a-color", |
| 805 | }); |
| 806 | |
| 807 | vi.unstubAllGlobals(); |
| 808 | invalidateCustomThemes(); |
| 809 | }); |
| 810 | }); |