Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { BUILT_IN_THEME_IDS, BUILT_IN_THEMES, T3_CHAT_THEME } from "@t3tools/shared/themePalettes";
3import { readDefaultMobileThemeVariables } from "./mobileTheme.test-support";
4
5import {
6 createMobileThemePairPatch,
7 createMobileThemeSelectionPatch,
8 createMobileThemeVariables,
9 DEFAULT_MOBILE_THEME_ID,
10 getMobileThemePreviewColors,
11 getMobileThemeVariables,
12 normalizeMobileThemeId,
13 normalizeMobileThemeMode,
14 resolveMobileThemeIds,
15 themeColorWithAlpha,
16 themeColorToNativeColor,
17} from "./mobileTheme";
18
19function relativeLuminance(hex: string): number {
20 const channels = hex
21 .slice(1)
22 .match(/.{2}/g)!
23 .map((channel) => Number.parseInt(channel, 16) / 255)
24 .map((channel) => (channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4));
25 return 0.2126 * channels[0]! + 0.7152 * channels[1]! + 0.0722 * channels[2]!;
26}
27
28function contrastRatio(first: string, second: string): number {
29 const firstLuminance = relativeLuminance(first);
30 const secondLuminance = relativeLuminance(second);
31 return (
32 (Math.max(firstLuminance, secondLuminance) + 0.05) /
33 (Math.min(firstLuminance, secondLuminance) + 0.05)
34 );
35}
36
37function compositeOver(overlay: string, background: string): string {
38 const overlayMatch = /^rgba\((\d+), (\d+), (\d+), ([\d.]+)\)$/.exec(overlay)!;
39 const backgroundChannels = background
40 .slice(1)
41 .match(/.{2}/g)!
42 .map((channel) => Number.parseInt(channel, 16));
43 const alpha = Number(overlayMatch[4]);
44 const channels = [1, 2, 3].map((index) =>
45 Math.round(Number(overlayMatch[index]) * alpha + backgroundChannels[index - 1]! * (1 - alpha)),
46 );
47 return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
48}
49
50describe("mobile themes", () => {
51 it("declares every runtime theme variable in the static stylesheet", () => {
52 const generatedVariables = createMobileThemeVariables(T3_CHAT_THEME.colors, "light");
53 expect(Object.keys(readDefaultMobileThemeVariables("light")).sort()).toEqual(
54 Object.keys(generatedVariables).sort(),
55 );
56 expect(Object.keys(readDefaultMobileThemeVariables("dark")).sort()).toEqual(
57 Object.keys(generatedVariables).sort(),
58 );
59 });
60
61 it("shares all built-in desktop palettes", () => {
62 expect(BUILT_IN_THEMES.map((theme) => theme.id)).toEqual(BUILT_IN_THEME_IDS);
63 for (const themeId of BUILT_IN_THEME_IDS) {
64 expect(getMobileThemeVariables(themeId, "light")["--color-screen"]).toMatch(/^#/);
65 expect(getMobileThemeVariables(themeId, "dark")["--color-screen"]).toMatch(/^#/);
66 }
67 });
68
69 it("preserves the existing mobile palette as the default", () => {
70 expect(readDefaultMobileThemeVariables("light")["--color-screen"]).toBe("#f2f2f7");
71 expect(readDefaultMobileThemeVariables("dark")["--color-screen"]).toBe("#0a0a0a");
72 expect(readDefaultMobileThemeVariables("light")["--color-user-bubble-skill-foreground"]).toBe(
73 "#2563eb",
74 );
75 });
76
77 it("applies palette overrides on top of the selected built-in theme", () => {
78 const variables = getMobileThemeVariables("ocean", "dark", {
79 "--color-primary": "#123456",
80 });
81
82 expect(variables["--color-primary"]).toBe("#123456");
83 expect(variables["--color-screen"]).toMatch(/^#/);
84 });
85
86 it("uses the same preview roles and standard artwork as desktop", () => {
87 expect(getMobileThemePreviewColors(DEFAULT_MOBILE_THEME_ID, "light")).toEqual({
88 canvas: "#fcfcfc",
89 accent: "#f4f4f5",
90 messageAction: "#4f46e5",
91 });
92 const desktopOcean = BUILT_IN_THEMES.find((theme) => theme.id === "ocean")!;
93 expect(getMobileThemePreviewColors("ocean", "light")).toEqual({
94 canvas: themeColorToNativeColor(desktopOcean.colors.canvas),
95 accent: themeColorToNativeColor(desktopOcean.colors.accent),
96 messageAction: themeColorToNativeColor(desktopOcean.colors.messageAction),
97 });
98 });
99
100 it("normalizes persisted theme preferences", () => {
101 expect(normalizeMobileThemeId("ocean")).toBe("ocean");
102 expect(normalizeMobileThemeId("missing-theme")).toBe(DEFAULT_MOBILE_THEME_ID);
103 expect(normalizeMobileThemeMode("dark")).toBe("dark");
104 expect(normalizeMobileThemeMode("sepia")).toBe("system");
105 });
106
107 it("migrates one theme choice to both appearances and preserves independent choices", () => {
108 expect(resolveMobileThemeIds({ themeId: "grove" })).toEqual({
109 light: "grove",
110 dark: "grove",
111 });
112 expect(
113 resolveMobileThemeIds({ themeId: "grove", lightThemeId: "iris", darkThemeId: "ocean" }),
114 ).toEqual({ light: "iris", dark: "ocean" });
115 expect(resolveMobileThemeIds({ themeId: "grove", lightThemeId: "missing" })).toEqual({
116 light: DEFAULT_MOBILE_THEME_ID,
117 dark: "grove",
118 });
119 });
120
121 it("changes either theme without switching the active appearance", () => {
122 const themeIds = { light: "t3-chat", dark: "grove" } as const;
123 expect(createMobileThemeSelectionPatch(themeIds, "light", "dark", "ocean")).toEqual({
124 lightThemeId: "t3-chat",
125 darkThemeId: "ocean",
126 themeId: "t3-chat",
127 });
128 expect(createMobileThemeSelectionPatch(themeIds, "light", "light", "iris")).toEqual({
129 lightThemeId: "iris",
130 darkThemeId: "grove",
131 themeId: "iris",
132 });
133 });
134
135 it("changes both appearance themes from the card action", () => {
136 expect(createMobileThemePairPatch("ember")).toEqual({
137 lightThemeId: "ember",
138 darkThemeId: "ember",
139 themeId: "ember",
140 });
141 });
142
143 it("converts OKLCH colors to React Native sRGB ColorValues", () => {
144 expect(themeColorToNativeColor("oklch(1 0 0)")).toBe("#ffffff");
145 expect(themeColorToNativeColor("oklch(0 0 0)")).toBe("#000000");
146 expect(themeColorToNativeColor("#123456")).toBe("#123456");
147 });
148
149 it("changes native palette color opacity for fades", () => {
150 expect(themeColorWithAlpha("#123456", 0)).toBe("rgba(18, 52, 86, 0)");
151 expect(themeColorWithAlpha("rgba(18, 52, 86, 0.98)", 0)).toBe("rgba(18, 52, 86, 0)");
152 });
153
154 it("maps semantic palette roles onto every mobile color variable", () => {
155 const variables = createMobileThemeVariables(T3_CHAT_THEME.colors, "light");
156 expect(Object.keys(variables)).toHaveLength(75);
157 expect(variables["--color-sheet-solid"]).toBe(
158 themeColorToNativeColor(T3_CHAT_THEME.colors.chrome),
159 );
160 expect(variables["--color-warning"]).toBe(
161 themeColorToNativeColor(T3_CHAT_THEME.colors.warningSurface),
162 );
163 expect(variables["--color-warning-foreground"]).toBe(
164 themeColorToNativeColor(T3_CHAT_THEME.colors.warningForeground),
165 );
166 expect(variables["--color-primary"]).not.toBe(variables["--color-screen"]);
167 expect(variables["--color-primary-shadow"]).toBe("#000000");
168 expect(variables["--color-backdrop"]).toBe("rgba(0, 0, 0, 0.22)");
169 expect(variables["--color-drawer-shadow"]).toBe("rgba(0, 0, 0, 0.12)");
170 expect(variables["--color-user-bubble-foreground"]).toMatch(/^#/);
171 });
172
173 it("keeps every built-in shadow and backdrop black-based in dark mode", () => {
174 for (const themeId of BUILT_IN_THEME_IDS) {
175 const variables = getMobileThemeVariables(themeId, "dark");
176 expect(variables["--color-primary-shadow"]).toBe("#000000");
177 expect(variables["--color-backdrop"]).toBe("rgba(0, 0, 0, 0.48)");
178 expect(variables["--color-drawer-shadow"]).toBe("rgba(0, 0, 0, 0.32)");
179 }
180 });
181
182 it("keeps placeholders and selected-row labels readable on their mobile surfaces", () => {
183 for (const themeId of BUILT_IN_THEME_IDS) {
184 for (const appearance of ["light", "dark"] as const) {
185 const variables = getMobileThemeVariables(themeId, appearance);
186 expect(
187 contrastRatio(variables["--color-placeholder"], variables["--color-input"]),
188 ).toBeGreaterThanOrEqual(4.5);
189 }
190 }
191
192 for (const themeId of BUILT_IN_THEME_IDS) {
193 for (const appearance of ["light", "dark"] as const) {
194 const variables = getMobileThemeVariables(themeId, appearance);
195 expect(
196 contrastRatio(
197 variables["--color-user-bubble-foreground"],
198 variables["--color-user-bubble"],
199 ),
200 ).toBeGreaterThanOrEqual(4.5);
201 expect(
202 contrastRatio(
203 variables["--color-user-bubble-skill-foreground"],
204 variables["--color-user-bubble"],
205 ),
206 ).toBeGreaterThanOrEqual(4.5);
207 expect(variables["--color-user-bubble-skill-foreground"]).not.toBe(
208 variables["--color-user-bubble-foreground"],
209 );
210 const fenceSurface = compositeOver(
211 variables["--color-md-user-fence-bg"],
212 variables["--color-user-bubble"],
213 );
214 expect(fenceSurface).not.toBe(variables["--color-user-bubble"]);
215 expect(
216 contrastRatio(variables["--color-md-user-fence-text"], fenceSurface),
217 ).toBeGreaterThanOrEqual(4.5);
218 }
219 }
220 });
221
222 // The default palette lives in global.css rather than BUILT_IN_THEMES, so the loops above
223 // never reached it; it kept an unreadable hardcoded bubble until this covered it.
224 it("keeps the default user bubble readable in both appearances", () => {
225 for (const appearance of ["light", "dark"] as const) {
226 const variables = readDefaultMobileThemeVariables(appearance);
227 const bubble = variables["--color-user-bubble"];
228 expect(
229 contrastRatio(variables["--color-user-bubble-foreground"], bubble),
230 ).toBeGreaterThanOrEqual(4.5);
231 expect(
232 contrastRatio(variables["--color-user-bubble-skill-foreground"], bubble),
233 ).toBeGreaterThanOrEqual(4.5);
234 expect(variables["--color-user-bubble-skill-foreground"]).not.toBe(
235 variables["--color-user-bubble-foreground"],
236 );
237 const fenceSurface = compositeOver(variables["--color-md-user-fence-bg"], bubble);
238 expect(fenceSurface).not.toBe(bubble);
239 expect(
240 contrastRatio(variables["--color-md-user-fence-text"], fenceSurface),
241 ).toBeGreaterThanOrEqual(4.5);
242 const codeSurface = compositeOver(variables["--color-md-user-code-bg"], bubble);
243 expect(
244 contrastRatio(variables["--color-md-user-code-text"], codeSurface),
245 ).toBeGreaterThanOrEqual(4.5);
246 }
247 });
248});
249
250describe("flattenThemeColor", () => {
251 it("composites a translucent border over its surface", async () => {
252 const { flattenThemeColor } = await import("./mobileTheme");
253 // `--color-border` in the dark theme, over the surface a chip sits on. Native chip drawing
254 // parses opaque hex only, so this has to resolve before it crosses the bridge.
255 expect(flattenThemeColor("rgba(255, 255, 255, 0.06)", "#171717")).toBe("#252525");
256 expect(flattenThemeColor("rgba(0, 0, 0, 0.08)", "#ffffff")).toBe("#ebebeb");
257 });
258
259 it("leaves an already opaque colour alone", async () => {
260 const { flattenThemeColor } = await import("./mobileTheme");
261 expect(flattenThemeColor("#171717", "#ffffff")).toBe("#171717");
262 });
263
264 it("treats a colour with no alpha as fully opaque", async () => {
265 const { flattenThemeColor } = await import("./mobileTheme");
266 expect(flattenThemeColor("rgb(255, 0, 0)", "#000000")).toBe("#ff0000");
267 });
268});