Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mobileTheme.test.ts

231 lines8.9 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as NodeFS from "node:fs";
3​
4import { BUILT_IN_THEME_IDS, BUILT_IN_THEMES } from "@t3tools/shared/themePalettes";
5import { DEFAULT_MOBILE_THEME_VARIABLES } from "./mobileDefaultTheme";
6​
7import {
8 createMobileThemePairPatch,
9 createMobileThemeSelectionPatch,
10 createMobileThemeVariables,
11 DEFAULT_MOBILE_THEME_ID,
12 getMobileThemePreviewColors,
13 getMobileThemeVariables,
14 MOBILE_THEME_IDS,
15 normalizeMobileThemeId,
16 normalizeMobileThemeMode,
17 resolveMobileThemeIds,
18 themeColorWithAlpha,
19 themeColorToNativeColor,
20} from "./mobileTheme";
21​
22function relativeLuminance(hex: string): number {
23 const channels = hex
24 .slice(1)
25 .match(/.{2}/g)!
26 .map((channel) => Number.parseInt(channel, 16) / 255)
27 .map((channel) => (channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4));
28 return 0.2126 * channels[0]! + 0.7152 * channels[1]! + 0.0722 * channels[2]!;
30​
31function contrastRatio(first: string, second: string): number {
32 const firstLuminance = relativeLuminance(first);
33 const secondLuminance = relativeLuminance(second);
34 return (
35 (Math.max(firstLuminance, secondLuminance) + 0.05) /
36 (Math.min(firstLuminance, secondLuminance) + 0.05)
37 );
39​
40function compositeOver(overlay: string, background: string): string {
41 const overlayMatch = /^rgba\((\d+), (\d+), (\d+), ([\d.]+)\)$/.exec(overlay)!;
42 const backgroundChannels = background
43 .slice(1)
44 .match(/.{2}/g)!
45 .map((channel) => Number.parseInt(channel, 16));
46 const alpha = Number(overlayMatch[4]);
47 const channels = [1, 2, 3].map((index) =>
48 Math.round(Number(overlayMatch[index]) * alpha + backgroundChannels[index - 1]! * (1 - alpha)),
49 );
50 return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
52​
53describe("mobile themes", () => {
54 it("declares every runtime theme variable in the static stylesheet", () => {
55 const stylesheet = NodeFS.readFileSync(new URL("../../global.css", import.meta.url), "utf8");
56 const stylesheetVariables = new Set(
57 Array.from(stylesheet.matchAll(/--color-[a-z0-9-]+/g), ([variable]) => variable),
58 );
59​
60 expect(Array.from(stylesheetVariables).sort()).toEqual(
61 Object.keys(DEFAULT_MOBILE_THEME_VARIABLES.light).sort(),
62 );
63 });
64​
65 it("shares all built-in desktop palettes", () => {
66 expect(BUILT_IN_THEMES.map((theme) => theme.id)).toEqual(BUILT_IN_THEME_IDS);
67 for (const themeId of BUILT_IN_THEME_IDS) {
68 expect(getMobileThemeVariables(themeId, "light")["--color-screen"]).toMatch(/^#/);
69 expect(getMobileThemeVariables(themeId, "dark")["--color-screen"]).toMatch(/^#/);
70 }
71 });
72​
73 it("preserves the existing mobile palette as the default", () => {
74 expect(getMobileThemeVariables(DEFAULT_MOBILE_THEME_ID, "light")["--color-screen"]).toBe(
75 "#f2f2f7",
76 );
77 expect(getMobileThemeVariables(DEFAULT_MOBILE_THEME_ID, "dark")["--color-screen"]).toBe(
78 "#0a0a0a",
79 );
80 expect(
81 getMobileThemeVariables(DEFAULT_MOBILE_THEME_ID, "light")[
82 "--color-user-bubble-skill-foreground"
83 ],
84 ).toBe("#f0abfc");
85 });
86​
87 it("applies palette overrides on top of the selected built-in theme", () => {
88 const variables = getMobileThemeVariables("ocean", "dark", {
89 "--color-primary": "#123456",
90 });
91​
92 expect(variables["--color-primary"]).toBe("#123456");
93 expect(variables["--color-screen"]).toMatch(/^#/);
94 });
95​
96 it("uses the same preview roles and standard artwork as desktop", () => {
97 expect(getMobileThemePreviewColors(DEFAULT_MOBILE_THEME_ID, "light")).toEqual({
98 canvas: "#fcfcfc",
99 accent: "#f4f4f5",
100 messageAction: "#4f46e5",
101 });
102 const desktopOcean = BUILT_IN_THEMES.find((theme) => theme.id === "ocean")!;
103 expect(getMobileThemePreviewColors("ocean", "light")).toEqual({
104 canvas: themeColorToNativeColor(desktopOcean.colors.canvas),
105 accent: themeColorToNativeColor(desktopOcean.colors.accent),
106 messageAction: themeColorToNativeColor(desktopOcean.colors.messageAction),
107 });
108 });
109​
110 it("normalizes persisted theme preferences", () => {
111 expect(normalizeMobileThemeId("ocean")).toBe("ocean");
112 expect(normalizeMobileThemeId("missing-theme")).toBe(DEFAULT_MOBILE_THEME_ID);
113 expect(normalizeMobileThemeMode("dark")).toBe("dark");
114 expect(normalizeMobileThemeMode("sepia")).toBe("system");
115 });
116​
117 it("migrates one theme choice to both appearances and preserves independent choices", () => {
118 expect(resolveMobileThemeIds({ themeId: "grove" })).toEqual({
119 light: "grove",
120 dark: "grove",
121 });
122 expect(
123 resolveMobileThemeIds({ themeId: "grove", lightThemeId: "iris", darkThemeId: "ocean" }),
124 ).toEqual({ light: "iris", dark: "ocean" });
125 expect(resolveMobileThemeIds({ themeId: "grove", lightThemeId: "missing" })).toEqual({
126 light: DEFAULT_MOBILE_THEME_ID,
127 dark: "grove",
128 });
129 });
130​
131 it("changes either theme without switching the active appearance", () => {
132 const themeIds = { light: "t3-chat", dark: "grove" } as const;
133 expect(createMobileThemeSelectionPatch(themeIds, "light", "dark", "ocean")).toEqual({
134 lightThemeId: "t3-chat",
135 darkThemeId: "ocean",
136 themeId: "t3-chat",
137 });
138 expect(createMobileThemeSelectionPatch(themeIds, "light", "light", "iris")).toEqual({
139 lightThemeId: "iris",
140 darkThemeId: "grove",
141 themeId: "iris",
142 });
143 });
144​
145 it("changes both appearance themes from the card action", () => {
146 expect(createMobileThemePairPatch("ember")).toEqual({
147 lightThemeId: "ember",
148 darkThemeId: "ember",
149 themeId: "ember",
150 });
151 });
152​
153 it("converts OKLCH colors to React Native sRGB ColorValues", () => {
154 expect(themeColorToNativeColor("oklch(1 0 0)")).toBe("#ffffff");
155 expect(themeColorToNativeColor("oklch(0 0 0)")).toBe("#000000");
156 expect(themeColorToNativeColor("#123456")).toBe("#123456");
157 });
158​
159 it("changes native palette color opacity for fades", () => {
160 expect(themeColorWithAlpha("#123456", 0)).toBe("rgba(18, 52, 86, 0)");
161 expect(themeColorWithAlpha("rgba(18, 52, 86, 0.98)", 0)).toBe("rgba(18, 52, 86, 0)");
162 });
163​
164 it("maps semantic palette roles onto every mobile color variable", () => {
165 const variables = createMobileThemeVariables(BUILT_IN_THEMES[0].colors, "light");
166 expect(Object.keys(variables)).toHaveLength(65);
167 expect(variables["--color-sheet-solid"]).toBe(
168 themeColorToNativeColor(BUILT_IN_THEMES[0].colors.chrome),
169 );
170 expect(variables["--color-primary"]).not.toBe(variables["--color-screen"]);
171 expect(variables["--color-primary-shadow"]).toBe("#000000");
172 expect(variables["--color-backdrop"]).toBe("rgba(0, 0, 0, 0.22)");
173 expect(variables["--color-drawer-shadow"]).toBe("rgba(0, 0, 0, 0.12)");
174 expect(variables["--color-user-bubble-foreground"]).toMatch(/^#/);
175 expect(Object.keys(DEFAULT_MOBILE_THEME_VARIABLES.light).sort()).toEqual(
176 Object.keys(variables).sort(),
177 );
178 expect(Object.keys(DEFAULT_MOBILE_THEME_VARIABLES.dark).sort()).toEqual(
179 Object.keys(variables).sort(),
180 );
181 });
182​
183 it("keeps every built-in shadow and backdrop black-based in dark mode", () => {
184 for (const theme of BUILT_IN_THEMES) {
185 const variables = getMobileThemeVariables(normalizeMobileThemeId(theme.id), "dark");
186 expect(variables["--color-primary-shadow"]).toBe("#000000");
187 expect(variables["--color-backdrop"]).toBe("rgba(0, 0, 0, 0.48)");
188 expect(variables["--color-drawer-shadow"]).toBe("rgba(0, 0, 0, 0.32)");
189 }
190 });
191​
192 it("keeps placeholders and selected-row labels readable on their mobile surfaces", () => {
193 for (const themeId of MOBILE_THEME_IDS) {
194 for (const appearance of ["light", "dark"] as const) {
195 const variables = getMobileThemeVariables(themeId, appearance);
196 expect(
197 contrastRatio(variables["--color-placeholder"], variables["--color-input"]),
198 ).toBeGreaterThanOrEqual(4.5);
199 }
200 }
201​
202 for (const themeId of BUILT_IN_THEME_IDS) {
203 for (const appearance of ["light", "dark"] as const) {
204 const variables = getMobileThemeVariables(themeId, appearance);
205 expect(
206 contrastRatio(
207 variables["--color-user-bubble-foreground"],
208 variables["--color-user-bubble"],
209 ),
210 ).toBeGreaterThanOrEqual(4.5);
211 expect(
212 contrastRatio(
213 variables["--color-user-bubble-skill-foreground"],
214 variables["--color-user-bubble"],
215 ),
216 ).toBeGreaterThanOrEqual(4.5);
217 expect(variables["--color-user-bubble-skill-foreground"]).not.toBe(
218 variables["--color-user-bubble-foreground"],
219 );
220 const fenceSurface = compositeOver(
221 variables["--color-md-user-fence-bg"],
222 variables["--color-user-bubble"],
223 );
224 expect(fenceSurface).not.toBe(variables["--color-user-bubble"]);
225 expect(
226 contrastRatio(variables["--color-md-user-fence-text"], fenceSurface),
227 ).toBeGreaterThanOrEqual(4.5);
228 }
229 }
230 });
231});