Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 BUILT_IN_THEME_IDS,
4 BUILT_IN_THEMES,
5 T3_CHAT_THEME,
6 T3_CODE_LIGHT_THEME_COLORS,
7 T3_CODE_DARK_THEME_COLORS,
8 MOBILE_THEME_IDS,
9 getThemeColorsForAppearance,
10} from "@t3tools/shared/themePalettes";
11import { readDefaultMobileThemeVariables } from "./mobileTheme.test-support";
12import { getMobileThemeRuntimeVariables } from "./mobileThemeVariables";
13
14import {
15 createMobileThemePairPatch,
16 createMobileThemeSelectionPatch,
17 createMobileThemeVariables,
18 DEFAULT_MOBILE_THEME_ID,
19 flattenThemeColor,
20 getMobileThemePreviewColors,
21 getMobileThemeVariables,
22 normalizeMobileThemeId,
23 normalizeMobileThemeMode,
24 resolveMobileThemeIds,
25 themeColorWithAlpha,
26 themeColorToNativeColor,
27} from "./mobileTheme";
28
29function relativeLuminance(hex: string): number {
30 const channels = hex
31 .slice(1)
32 .match(/.{2}/g)!
33 .map((channel) => Number.parseInt(channel, 16) / 255)
34 .map((channel) => (channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4));
35 return 0.2126 * channels[0]! + 0.7152 * channels[1]! + 0.0722 * channels[2]!;
36}
37
38function contrastRatio(first: string, second: string): number {
39 const firstLuminance = relativeLuminance(first);
40 const secondLuminance = relativeLuminance(second);
41 return (
42 (Math.max(firstLuminance, secondLuminance) + 0.05) /
43 (Math.min(firstLuminance, secondLuminance) + 0.05)
44 );
45}
46
47function compositeOver(overlay: string, background: string): string {
48 const overlayMatch = /^rgba\((\d+), (\d+), (\d+), ([\d.]+)\)$/.exec(overlay)!;
49 const backgroundChannels = background
50 .slice(1)
51 .match(/.{2}/g)!
52 .map((channel) => Number.parseInt(channel, 16));
53 const alpha = Number(overlayMatch[4]);
54 const channels = [1, 2, 3].map((index) =>
55 Math.round(Number(overlayMatch[index]) * alpha + backgroundChannels[index - 1]! * (1 - alpha)),
56 );
57 return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
58}
59
60describe("mobile themes", () => {
61 it("declares every runtime theme variable in the static stylesheet", () => {
62 const generatedVariables = createMobileThemeVariables(T3_CHAT_THEME.colors, "light");
63 expect(Object.keys(readDefaultMobileThemeVariables("light")).sort()).toEqual(
64 Object.keys(generatedVariables).sort(),
65 );
66 expect(Object.keys(readDefaultMobileThemeVariables("dark")).sort()).toEqual(
67 Object.keys(generatedVariables).sort(),
68 );
69 });
70
71 it("shares all built-in desktop palettes", () => {
72 expect(BUILT_IN_THEMES.map((theme) => theme.id)).toEqual(BUILT_IN_THEME_IDS);
73 for (const themeId of BUILT_IN_THEME_IDS) {
74 expect(getMobileThemeVariables(themeId, "light")["--color-screen"]).toMatch(/^#/);
75 expect(getMobileThemeVariables(themeId, "dark")["--color-screen"]).toMatch(/^#/);
76 }
77 });
78
79 it.each(MOBILE_THEME_IDS)("uses the web color roles for %s in both appearances", (themeId) => {
80 for (const appearance of ["light", "dark"] as const) {
81 const theme = BUILT_IN_THEMES.find((candidate) => candidate.id === themeId);
82 const colors = theme
83 ? getThemeColorsForAppearance(theme, appearance)!
84 : appearance === "dark"
85 ? T3_CODE_DARK_THEME_COLORS
86 : T3_CODE_LIGHT_THEME_COLORS;
87 const variables =
88 themeId === DEFAULT_MOBILE_THEME_ID
89 ? readDefaultMobileThemeVariables(appearance)
90 : getMobileThemeVariables(themeId, appearance);
91 expect(variables["--color-screen"]).toBe(themeColorToNativeColor(colors.canvas));
92 expect(variables["--color-thread-canvas"]).toBe(variables["--color-screen"]);
93 expect(variables["--color-drawer"]).toBe(themeColorToNativeColor(colors.sidebar));
94 expect(variables["--color-thread-hover"]).toBe(
95 themeColorToNativeColor(colors.sidebarRowHover),
96 );
97 expect(variables["--color-card"]).toBe(themeColorToNativeColor(colors.surface));
98 expect(variables["--color-composer-surface"]).toBe(
99 themeColorWithAlpha(
100 themeId === DEFAULT_MOBILE_THEME_ID
101 ? variables["--color-grouped-card"]
102 : themeColorToNativeColor(colors.surface),
103 appearance === "dark" ? 0.9 : 0.94,
104 ),
105 );
106 expect(variables["--color-thread-selected"]).toBe(
107 themeColorToNativeColor(colors.sidebarRowActive),
108 );
109 expect(variables["--color-thread-selected-foreground"]).toBe(
110 themeColorToNativeColor(colors.sidebarForeground),
111 );
112 expect(variables["--color-primary"]).toBe(themeColorToNativeColor(colors.messageAction));
113 if (themeId !== DEFAULT_MOBILE_THEME_ID) {
114 expect(variables["--color-user-bubble"]).toBe(
115 themeColorToNativeColor(colors.messageSurface),
116 );
117 }
118 expect(
119 contrastRatio(variables["--color-foreground"], variables["--color-screen"]),
120 ).toBeGreaterThanOrEqual(4.5);
121 for (const [foreground, surface] of [
122 ["--color-drawer-foreground", "--color-drawer"],
123 ["--color-drawer-foreground-muted", "--color-drawer"],
124 ["--color-drawer-foreground", "--color-thread-hover"],
125 ["--color-drawer-foreground-muted", "--color-thread-hover"],
126 ["--color-thread-selected-foreground-muted", "--color-thread-selected"],
127 ["--color-primary-foreground", "--color-primary"],
128 ["--color-primary-text", "--color-screen"],
129 ["--color-primary-text", "--color-card"],
130 ["--color-primary-text", "--color-card-alt"],
131 ["--color-primary-text", "--color-sheet-solid"],
132 ["--color-foreground", "--color-sheet-solid"],
133 ["--color-foreground-muted", "--color-sheet-solid"],
134 ["--color-primary-text", "--color-grouped-card"],
135 ["--color-foreground", "--color-grouped-card"],
136 ["--color-foreground-muted", "--color-grouped-card"],
137 ["--color-placeholder", "--color-grouped-card"],
138 ["--color-secondary-foreground", "--color-secondary"],
139 ["--color-user-bubble-foreground", "--color-user-bubble"],
140 ["--color-warning-foreground", "--color-warning"],
141 ["--color-danger-foreground", "--color-danger"],
142 ["--color-md-body", "--color-screen"],
143 ["--color-md-strong", "--color-screen"],
144 ["--color-md-link", "--color-screen"],
145 ["--color-md-code-text", "--color-md-code-bg"],
146 ] as const) {
147 expect(
148 contrastRatio(variables[foreground], variables[surface]),
149 `${appearance}: ${foreground} on ${surface}`,
150 ).toBeGreaterThanOrEqual(4.5);
151 }
152 expect(
153 contrastRatio(
154 variables["--color-thread-selected-foreground"],
155 variables["--color-thread-selected"],
156 ),
157 ).toBeGreaterThanOrEqual(4.5);
158 }
159 });
160
161 it("applies palette overrides on top of the selected built-in theme", () => {
162 const variables = getMobileThemeVariables("ocean", "dark", {
163 "--color-primary": "#123456",
164 });
165
166 expect(variables["--color-primary"]).toBe("#123456");
167 expect(variables["--color-screen"]).toMatch(/^#/);
168 });
169
170 it.each(["light", "dark"] as const)(
171 "separates default settings groups from their %s background",
172 (appearance) => {
173 const variables = getMobileThemeVariables("t3-code", appearance);
174 expect(
175 contrastRatio(variables["--color-grouped-card"], variables["--color-sheet-solid"]),
176 ).toBeGreaterThanOrEqual(1.06);
177 expect(variables["--color-grouped-card"]).not.toBe(variables["--color-card"]);
178 for (const platform of ["ios", "android"]) {
179 const runtime = getMobileThemeRuntimeVariables("t3-code", appearance, platform);
180 const sidebar = flattenThemeColor(runtime["--color-drawer"], runtime["--color-screen"]);
181 const chrome = flattenThemeColor(
182 runtime[platform === "android" ? "--color-header" : "--color-drawer"],
183 runtime["--color-screen"],
184 );
185 expect(relativeLuminance(sidebar)).toBeLessThan(
186 relativeLuminance(runtime["--color-thread-canvas"]),
187 );
188 expect(contrastRatio(chrome, runtime["--color-screen"])).toBeGreaterThanOrEqual(1.06);
189 const foregroundRoles =
190 platform === "android"
191 ? (["--color-header-foreground", "--color-foreground-muted"] as const)
192 : (["--color-drawer-foreground", "--color-drawer-foreground-muted"] as const);
193 for (const role of foregroundRoles) {
194 expect(contrastRatio(runtime[role], chrome)).toBeGreaterThanOrEqual(4.5);
195 }
196 }
197 },
198 );
199
200 it.each(["light", "dark"] as const)(
201 "slightly strengthens default %s messages and separates fallback materials",
202 (appearance) => {
203 const variables = getMobileThemeVariables("t3-code", appearance);
204 const desktop =
205 appearance === "dark" ? T3_CODE_DARK_THEME_COLORS : T3_CODE_LIGHT_THEME_COLORS;
206 const bubbleContrast = contrastRatio(
207 variables["--color-user-bubble"],
208 variables["--color-screen"],
209 );
210 expect(bubbleContrast).toBeGreaterThan(contrastRatio(desktop.messageSurface, desktop.canvas));
211 expect(bubbleContrast).toBeLessThan(1.2);
212 for (const role of ["--color-composer-surface", "--color-glass-fallback"] as const) {
213 const surface = flattenThemeColor(variables[role], variables["--color-screen"]);
214 expect(contrastRatio(surface, variables["--color-screen"])).toBeGreaterThanOrEqual(1.06);
215 for (const foreground of [
216 "--color-foreground",
217 "--color-placeholder",
218 "--color-primary-text",
219 ] as const) {
220 expect(contrastRatio(variables[foreground], surface)).toBeGreaterThanOrEqual(4.5);
221 }
222 }
223 },
224 );
225
226 it("uses the same preview roles and standard artwork as desktop", () => {
227 expect(getMobileThemePreviewColors(DEFAULT_MOBILE_THEME_ID, "light")).toEqual({
228 canvas: "#fcfcfc",
229 accent: "#f4f4f5",
230 messageAction: "#4f46e5",
231 });
232 const desktopOcean = BUILT_IN_THEMES.find((theme) => theme.id === "ocean")!;
233 expect(getMobileThemePreviewColors("ocean", "light")).toEqual({
234 canvas: themeColorToNativeColor(desktopOcean.colors.canvas),
235 accent: themeColorToNativeColor(desktopOcean.colors.accent),
236 messageAction: themeColorToNativeColor(desktopOcean.colors.messageAction),
237 });
238 });
239
240 it("normalizes persisted theme preferences", () => {
241 expect(normalizeMobileThemeId("ocean")).toBe("ocean");
242 expect(normalizeMobileThemeId("missing-theme")).toBe(DEFAULT_MOBILE_THEME_ID);
243 expect(normalizeMobileThemeMode("dark")).toBe("dark");
244 expect(normalizeMobileThemeMode("sepia")).toBe("system");
245 });
246
247 it("migrates one theme choice to both appearances and preserves independent choices", () => {
248 expect(resolveMobileThemeIds({ themeId: "grove" })).toEqual({
249 light: "grove",
250 dark: "grove",
251 });
252 expect(
253 resolveMobileThemeIds({ themeId: "grove", lightThemeId: "iris", darkThemeId: "ocean" }),
254 ).toEqual({ light: "iris", dark: "ocean" });
255 expect(resolveMobileThemeIds({ themeId: "grove", lightThemeId: "missing" })).toEqual({
256 light: DEFAULT_MOBILE_THEME_ID,
257 dark: "grove",
258 });
259 });
260
261 it("changes either theme without switching the active appearance", () => {
262 const themeIds = { light: "t3-chat", dark: "grove" } as const;
263 expect(createMobileThemeSelectionPatch(themeIds, "light", "dark", "ocean")).toEqual({
264 lightThemeId: "t3-chat",
265 darkThemeId: "ocean",
266 themeId: "t3-chat",
267 });
268 expect(createMobileThemeSelectionPatch(themeIds, "light", "light", "iris")).toEqual({
269 lightThemeId: "iris",
270 darkThemeId: "grove",
271 themeId: "iris",
272 });
273 });
274
275 it("changes both appearance themes from the card action", () => {
276 expect(createMobileThemePairPatch("ember")).toEqual({
277 lightThemeId: "ember",
278 darkThemeId: "ember",
279 themeId: "ember",
280 });
281 });
282
283 it("converts OKLCH colors to React Native sRGB ColorValues", () => {
284 expect(themeColorToNativeColor("oklch(1 0 0)")).toBe("#ffffff");
285 expect(themeColorToNativeColor("oklch(0 0 0)")).toBe("#000000");
286 expect(themeColorToNativeColor("#123456")).toBe("#123456");
287 });
288
289 it("changes native palette color opacity for fades", () => {
290 expect(themeColorWithAlpha("#123456", 0)).toBe("rgba(18, 52, 86, 0)");
291 expect(themeColorWithAlpha("rgba(18, 52, 86, 0.98)", 0)).toBe("rgba(18, 52, 86, 0)");
292 });
293
294 it("maps semantic palette roles onto every mobile color variable", () => {
295 const variables = createMobileThemeVariables(T3_CHAT_THEME.colors, "light");
296 expect(variables["--color-sheet-solid"]).toBe(
297 themeColorToNativeColor(T3_CHAT_THEME.colors.chrome),
298 );
299 expect(variables["--color-warning"]).toBe(
300 themeColorToNativeColor(T3_CHAT_THEME.colors.warningSurface),
301 );
302 expect(variables["--color-warning-foreground"]).toBe(
303 themeColorToNativeColor(T3_CHAT_THEME.colors.warningForeground),
304 );
305 expect(variables["--color-primary"]).not.toBe(variables["--color-screen"]);
306 expect(variables["--color-primary-shadow"]).toBe("#000000");
307 expect(variables["--color-backdrop"]).toBe("rgba(0, 0, 0, 0.22)");
308 expect(variables["--color-drawer-shadow"]).toBe("rgba(0, 0, 0, 0.12)");
309 expect(variables["--color-user-bubble-foreground"]).toMatch(/^#/);
310 });
311
312 it("keeps every built-in shadow and backdrop black-based in dark mode", () => {
313 for (const themeId of BUILT_IN_THEME_IDS) {
314 const variables = getMobileThemeVariables(themeId, "dark");
315 expect(variables["--color-primary-shadow"]).toBe("#000000");
316 expect(variables["--color-backdrop"]).toBe("rgba(0, 0, 0, 0.48)");
317 expect(variables["--color-drawer-shadow"]).toBe("rgba(0, 0, 0, 0.32)");
318 }
319 });
320
321 it("keeps placeholders and selected-row labels readable on their mobile surfaces", () => {
322 for (const themeId of BUILT_IN_THEME_IDS) {
323 for (const appearance of ["light", "dark"] as const) {
324 const variables = getMobileThemeVariables(themeId, appearance);
325 expect(
326 contrastRatio(variables["--color-placeholder"], variables["--color-input"]),
327 ).toBeGreaterThanOrEqual(4.5);
328 }
329 }
330
331 for (const themeId of BUILT_IN_THEME_IDS) {
332 for (const appearance of ["light", "dark"] as const) {
333 const variables = getMobileThemeVariables(themeId, appearance);
334 expect(
335 contrastRatio(
336 variables["--color-user-bubble-foreground"],
337 variables["--color-user-bubble"],
338 ),
339 ).toBeGreaterThanOrEqual(4.5);
340 expect(
341 contrastRatio(
342 variables["--color-user-bubble-skill-foreground"],
343 variables["--color-user-bubble"],
344 ),
345 ).toBeGreaterThanOrEqual(4.5);
346 expect(variables["--color-user-bubble-skill-foreground"]).not.toBe(
347 variables["--color-user-bubble-foreground"],
348 );
349 const fenceSurface = compositeOver(
350 variables["--color-md-user-fence-bg"],
351 variables["--color-user-bubble"],
352 );
353 expect(fenceSurface).not.toBe(variables["--color-user-bubble"]);
354 expect(
355 contrastRatio(variables["--color-md-user-fence-text"], fenceSurface),
356 ).toBeGreaterThanOrEqual(4.5);
357 }
358 }
359 });
360
361 it("keeps the default user bubble readable in both appearances", () => {
362 for (const appearance of ["light", "dark"] as const) {
363 const variables = readDefaultMobileThemeVariables(appearance);
364 const bubble = variables["--color-user-bubble"];
365 expect(
366 contrastRatio(variables["--color-user-bubble-foreground"], bubble),
367 ).toBeGreaterThanOrEqual(4.5);
368 expect(
369 contrastRatio(variables["--color-user-bubble-skill-foreground"], bubble),
370 ).toBeGreaterThanOrEqual(4.5);
371 expect(variables["--color-user-bubble-skill-foreground"]).not.toBe(
372 variables["--color-user-bubble-foreground"],
373 );
374 const fenceSurface = compositeOver(variables["--color-md-user-fence-bg"], bubble);
375 expect(fenceSurface).not.toBe(bubble);
376 expect(
377 contrastRatio(variables["--color-md-user-fence-text"], fenceSurface),
378 ).toBeGreaterThanOrEqual(4.5);
379 const codeSurface = compositeOver(variables["--color-md-user-code-bg"], bubble);
380 expect(
381 contrastRatio(variables["--color-md-user-code-text"], codeSurface),
382 ).toBeGreaterThanOrEqual(4.5);
383 }
384 });
385});
386
387describe("flattenThemeColor", () => {
388 it("composites a translucent border over its surface", async () => {
389 const { flattenThemeColor } = await import("./mobileTheme");
390 // `--color-border` in the dark theme, over the surface a chip sits on. Native chip drawing
391 // parses opaque hex only, so this has to resolve before it crosses the bridge.
392 expect(flattenThemeColor("rgba(255, 255, 255, 0.06)", "#171717")).toBe("#252525");
393 expect(flattenThemeColor("rgba(0, 0, 0, 0.08)", "#ffffff")).toBe("#ebebeb");
394 });
395
396 it("leaves an already opaque colour alone", async () => {
397 const { flattenThemeColor } = await import("./mobileTheme");
398 expect(flattenThemeColor("#171717", "#ffffff")).toBe("#171717");
399 });
400
401 it("treats a colour with no alpha as fully opaque", async () => {
402 const { flattenThemeColor } = await import("./mobileTheme");
403 expect(flattenThemeColor("rgb(255, 0, 0)", "#000000")).toBe("#ff0000");
404 });
405});