Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2
3import indexHtml from "../index.html?raw";
4import {
5 CUSTOM_THEMES_STORAGE_KEY,
6 getDefaultThemeColors,
7 getThemeColorsForMode,
8 invalidateCustomThemes,
9 isKnownThemePreference,
10 resolveThemeAppearance,
11 T3_CHAT_THEME,
12 EMBER_THEME,
13 GROVE_THEME,
14 IRIS_THEME,
15 OCEAN_THEME,
16 THEME_APPEARANCE_MODE_STORAGE_KEY,
17 THEME_FOLLOW_SYSTEM_STORAGE_KEY,
18 toCanonicalThemeColor,
19} from "./themePalette";
20
21const THEME_STORAGE_KEY = "t3code:theme";
22const DEFAULT_DARK_CHROME = getDefaultThemeColors("dark").chrome;
23
24const bootScript = (() => {
25 const match = indexHtml.match(/<script>([\s\S]*?)<\/script>/);
26 if (!match?.[1]) throw new Error("Could not find the inline boot script in index.html");
27 return match[1];
28})();
29
30type BootResult = {
31 isDark: boolean;
32 themeId: string | undefined;
33 themeSelected: string | undefined;
34 backgroundColor: string;
35 bootVariables: Record<string, string>;
36 metaContent: string | null;
37};
38
39function runBootScript(options: {
40 storage?: Record<string, string>;
41 storageThrows?: boolean;
42 prefersDark: boolean;
43}): BootResult {
44 const classes = new Set<string>();
45 const bootVariables: Record<string, string> = {};
46 const meta = {
47 content: null as string | null,
48 setAttribute(_name: string, value: string) {
49 this.content = value;
50 },
51 };
52 const documentElement = {
53 dataset: {} as Record<string, string | undefined>,
54 classList: {
55 add: (name: string) => void classes.add(name),
56 remove: (name: string) => void classes.delete(name),
57 toggle: (name: string, force?: boolean) => {
58 const next = force ?? !classes.has(name);
59 if (next) classes.add(name);
60 else classes.delete(name);
61 return next;
62 },
63 },
64 style: {
65 backgroundColor: "",
66 setProperty: (name: string, value: string) => {
67 bootVariables[name] = value;
68 },
69 },
70 };
71 const fakeDocument = {
72 documentElement,
73 querySelectorAll: (selector: string) => (selector === 'meta[name="theme-color"]' ? [meta] : []),
74 };
75 const fakeWindow = {
76 localStorage: {
77 getItem: (key: string): string | null => {
78 if (options.storageThrows) throw new Error("storage blocked");
79 return options.storage?.[key] ?? null;
80 },
81 },
82 matchMedia: () => ({ matches: options.prefersDark }),
83 };
84
85 const fakeCss = {
86 supports: (property: string, value: string) =>
87 property === "color" && toCanonicalThemeColor(value) !== null,
88 };
89
90 new Function("window", "document", "CSS", bootScript)(fakeWindow, fakeDocument, fakeCss);
91
92 return {
93 isDark: classes.has("dark"),
94 themeId: documentElement.dataset.themeId,
95 themeSelected: documentElement.dataset.themeSelected,
96 backgroundColor: documentElement.style.backgroundColor,
97 bootVariables,
98 metaContent: meta.content,
99 };
100}
101
102function runtimeResolvedAppearance(
103 storage: Record<string, string>,
104 prefersDark: boolean,
105): "light" | "dark" {
106 vi.stubGlobal("window", {
107 localStorage: { getItem: (key: string) => storage[key] ?? null },
108 matchMedia: () => ({ matches: prefersDark }),
109 });
110 invalidateCustomThemes();
111 try {
112 const raw = storage[THEME_STORAGE_KEY] ?? null;
113 const theme = raw !== null && isKnownThemePreference(raw) ? raw : "system";
114 const followRaw = storage[THEME_FOLLOW_SYSTEM_STORAGE_KEY] ?? null;
115 const appearanceRaw = storage[THEME_APPEARANCE_MODE_STORAGE_KEY] ?? null;
116 const appearanceMode =
117 appearanceRaw === "light" || appearanceRaw === "dark" || appearanceRaw === "system"
118 ? appearanceRaw
119 : followRaw === "true"
120 ? "system"
121 : followRaw === "false"
122 ? null
123 : theme === "system"
124 ? "system"
125 : null;
126 const followSystem = appearanceMode === "system";
127 return resolveThemeAppearance(theme, prefersDark, followSystem, appearanceMode ?? undefined);
128 } finally {
129 vi.unstubAllGlobals();
130 invalidateCustomThemes();
131 }
132}
133
134const AURORA_DUAL = {
135 id: "aurora",
136 label: "Aurora",
137 appearance: "light",
138 colors: { canvas: "#f8fbff", text: "#10243d", accent: "#5b6cff" },
139 variants: { dark: { canvas: "#101827", text: "#eef5ff", accent: "#7c93ff" } },
140};
141const CHARCOAL_DARK_ONLY = {
142 id: "charcoal",
143 label: "Charcoal",
144 appearance: "dark",
145 colors: { canvas: "#1c1210", text: "#ffe8d9", accent: "#ff7a45" },
146};
147
148describe("index.html boot script", () => {
149 const parityCases: ReadonlyArray<{
150 name: string;
151 storage: Record<string, string>;
152 prefersDark: boolean;
153 }> = [
154 { name: "no stored preference on a dark OS", storage: {}, prefersDark: true },
155 {
156 name: "T3 Chat follows a dark OS",
157 storage: { [THEME_STORAGE_KEY]: "t3-chat", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
158 prefersDark: true,
159 },
160 {
161 name: "an explicit global dark mode applies to T3 Chat",
162 storage: {
163 [THEME_STORAGE_KEY]: "t3-chat",
164 [THEME_APPEARANCE_MODE_STORAGE_KEY]: "dark",
165 [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false",
166 },
167 prefersDark: false,
168 },
169 {
170 name: "Grove follows a dark OS",
171 storage: { [THEME_STORAGE_KEY]: "grove", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
172 prefersDark: true,
173 },
174 {
175 name: "Ocean follows a dark OS",
176 storage: { [THEME_STORAGE_KEY]: "ocean", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
177 prefersDark: true,
178 },
179 {
180 name: "Ember follows a dark OS",
181 storage: { [THEME_STORAGE_KEY]: "ember", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
182 prefersDark: true,
183 },
184 {
185 name: "Iris follows a dark OS",
186 storage: { [THEME_STORAGE_KEY]: "iris", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
187 prefersDark: true,
188 },
189 {
190 name: "a legacy t3-grove preference resolves through the alias",
191 storage: { [THEME_STORAGE_KEY]: "t3-grove", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
192 prefersDark: true,
193 },
194 {
195 name: "legacy t3-chat-dark resolves to dark T3 Chat",
196 storage: { [THEME_STORAGE_KEY]: "t3-chat-dark" },
197 prefersDark: true,
198 },
199 {
200 name: "a dual-mode custom theme follows the OS",
201 storage: {
202 [THEME_STORAGE_KEY]: "aurora",
203 [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true",
204 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([AURORA_DUAL]),
205 },
206 prefersDark: true,
207 },
208 {
209 name: "a dark-only custom theme stays dark on a light OS",
210 storage: {
211 [THEME_STORAGE_KEY]: "charcoal",
212 [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true",
213 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([CHARCOAL_DARK_ONLY]),
214 },
215 prefersDark: false,
216 },
217 {
218 name: "a legacy mode-suffixed preference is treated as unknown",
219 storage: {
220 [THEME_STORAGE_KEY]: "aurora:dark",
221 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([AURORA_DUAL]),
222 },
223 prefersDark: true,
224 },
225 {
226 name: "a removed custom theme falls back to system",
227 storage: { [THEME_STORAGE_KEY]: "gone-theme" },
228 prefersDark: true,
229 },
230 {
231 name: "a corrupted follow-system value falls back to inference",
232 storage: { [THEME_STORAGE_KEY]: "system", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "1" },
233 prefersDark: true,
234 },
235 {
236 name: "follow-system off keeps an explicit light preference on a dark OS",
237 storage: { [THEME_STORAGE_KEY]: "light", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false" },
238 prefersDark: true,
239 },
240 {
241 name: "a bare dark preference stays dark on a light OS",
242 storage: { [THEME_STORAGE_KEY]: "dark" },
243 prefersDark: false,
244 },
245 {
246 name: "follow-system off keeps a bare dark preference on a light OS",
247 storage: { [THEME_STORAGE_KEY]: "dark", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false" },
248 prefersDark: false,
249 },
250 ];
251
252 it.each(parityCases)("matches the runtime appearance: $name", ({ storage, prefersDark }) => {
253 const boot = runBootScript({ storage, prefersDark });
254 expect(boot.isDark).toBe(runtimeResolvedAppearance(storage, prefersDark) === "dark");
255 });
256
257 it("marks built-in and custom themes on the document element", () => {
258 const chat = runBootScript({
259 storage: { [THEME_STORAGE_KEY]: "t3-chat", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" },
260 prefersDark: true,
261 });
262 expect(chat.themeId).toBe("t3-chat");
263 expect(chat.themeSelected).toBe("true");
264 expect(chat.isDark).toBe(true);
265
266 const aurora = runBootScript({
267 storage: {
268 [THEME_STORAGE_KEY]: "aurora",
269 [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true",
270 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([AURORA_DUAL]),
271 },
272 prefersDark: true,
273 });
274 expect(aurora.themeId).toBe("aurora");
275 expect(aurora.isDark).toBe(true);
276 expect(aurora.backgroundColor).toBe(DEFAULT_DARK_CHROME);
277 expect(aurora.bootVariables["--boot-background"]).toBe(AURORA_DUAL.variants.dark.canvas);
278 expect(aurora.metaContent).toBe(DEFAULT_DARK_CHROME);
279 });
280
281 it("accepts exponent-form OKLCH before the runtime mounts", () => {
282 const colors = {
283 canvas: "oklch(9.5e-1 1e-2 2.8e2)",
284 chrome: "oklch(9.4e-1 1e-2 2.8e2)",
285 text: "oklch(2e-1 0 0 / 9e-1)",
286 accent: "oklch(6.2e-1 0.2 2.8e2)",
287 };
288 const boot = runBootScript({
289 storage: {
290 [THEME_STORAGE_KEY]: "scientific",
291 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([
292 {
293 id: "scientific",
294 label: "Scientific",
295 appearance: "light",
296 colors,
297 },
298 ]),
299 },
300 prefersDark: false,
301 });
302
303 expect(boot.bootVariables["--boot-background"]).toBe(colors.canvas);
304 expect(boot.bootVariables["--boot-foreground"]).toBe(colors.text);
305 expect(boot.bootVariables["--boot-accent"]).toBe(colors.accent);
306 expect(boot.backgroundColor).toBe(colors.chrome);
307 expect(boot.metaContent).toBe(colors.chrome);
308 });
309
310 it("accepts legacy CSS color formats before the runtime mounts", () => {
311 const colors = {
312 canvas: "rgb(248 251 255)",
313 chrome: "hsl(210 100% 99%)",
314 text: "rebeccapurple",
315 accent: "color(display-p3 0.36 0.42 1)",
316 };
317 const boot = runBootScript({
318 storage: {
319 [THEME_STORAGE_KEY]: "legacy-css",
320 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([
321 {
322 id: "legacy-css",
323 label: "Legacy CSS",
324 appearance: "light",
325 colors,
326 },
327 ]),
328 },
329 prefersDark: false,
330 });
331
332 expect(boot.bootVariables["--boot-background"]).toBe(colors.canvas);
333 expect(boot.bootVariables["--boot-foreground"]).toBe(colors.text);
334 expect(boot.bootVariables["--boot-accent"]).toBe(colors.accent);
335 expect(boot.backgroundColor).toBe(colors.chrome);
336 });
337
338 it("keeps every built-in boot splash in sync with the real palettes", () => {
339 for (const theme of [T3_CHAT_THEME, GROVE_THEME, OCEAN_THEME, EMBER_THEME, IRIS_THEME]) {
340 expect(theme.appearance).toBe("light");
341 for (const mode of ["light", "dark"] as const) {
342 const colors = getThemeColorsForMode(theme, mode);
343 expect(colors).not.toBeNull();
344 const boot = runBootScript({
345 storage: {
346 [THEME_STORAGE_KEY]: theme.id,
347 [THEME_APPEARANCE_MODE_STORAGE_KEY]: mode,
348 },
349 prefersDark: mode === "dark",
350 });
351 expect(boot.themeId).toBe(theme.id);
352 expect(boot.isDark).toBe(mode === "dark");
353 expect(boot.bootVariables["--boot-background"]).toBe(colors!.canvas);
354 expect(boot.bootVariables["--boot-foreground"]).toBe(colors!.text);
355 expect(boot.bootVariables["--boot-accent"]).toBe(colors!.accent);
356 expect(boot.backgroundColor).toBe(colors!.chrome);
357 expect(boot.metaContent).toBe(colors!.chrome);
358 }
359 }
360 });
361
362 it("applies the matching half of an automatic mix to the splash", () => {
363 const storage = {
364 [THEME_STORAGE_KEY]: "t3-chat",
365 [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system",
366 "t3code:theme-halves:v1": JSON.stringify({ dark: GROVE_THEME.id }),
367 };
368
369 const dark = runBootScript({ storage, prefersDark: true });
370 expect(dark.isDark).toBe(true);
371 expect(dark.themeId).toBe(GROVE_THEME.id);
372 expect(dark.bootVariables["--boot-background"]).toBe(
373 getThemeColorsForMode(GROVE_THEME, "dark")!.canvas,
374 );
375
376 const light = runBootScript({ storage, prefersDark: false });
377 expect(light.isDark).toBe(false);
378 expect(light.themeId).toBe("t3-chat");
379 expect(light.bootVariables["--boot-background"]).toBe(
380 getThemeColorsForMode(T3_CHAT_THEME, "light")!.canvas,
381 );
382 });
383
384 it("lets a dark half go dark when the light-only base cannot", () => {
385 const boot = runBootScript({
386 storage: {
387 [THEME_STORAGE_KEY]: "paper",
388 [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system",
389 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([
390 {
391 id: "paper",
392 label: "Paper",
393 appearance: "light",
394 colors: { canvas: "#f8fbff", text: "#10243d", accent: "#5b6cff" },
395 },
396 ]),
397 "t3code:theme-halves:v1": JSON.stringify({ dark: GROVE_THEME.id }),
398 },
399 prefersDark: true,
400 });
401 expect(boot.isDark).toBe(true);
402 expect(boot.themeId).toBe(GROVE_THEME.id);
403 });
404
405 it("paints the half's splash when the base theme no longer exists", () => {
406 const boot = runBootScript({
407 storage: {
408 [THEME_STORAGE_KEY]: "gone-theme",
409 [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system",
410 "t3code:theme-halves:v1": JSON.stringify({ dark: GROVE_THEME.id }),
411 },
412 prefersDark: true,
413 });
414 expect(boot.isDark).toBe(true);
415 expect(boot.themeId).toBe(GROVE_THEME.id);
416 expect(boot.themeSelected).toBe("true");
417 expect(boot.bootVariables["--boot-background"]).toBe(
418 getThemeColorsForMode(GROVE_THEME, "dark")!.canvas,
419 );
420 });
421
422 it("resolves a legacy-prefixed mix half onto the renamed theme", () => {
423 const boot = runBootScript({
424 storage: {
425 [THEME_STORAGE_KEY]: "t3-chat",
426 [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system",
427 "t3code:theme-halves:v1": JSON.stringify({ dark: "t3-grove" }),
428 },
429 prefersDark: true,
430 });
431 expect(boot.isDark).toBe(true);
432 expect(boot.themeId).toBe(GROVE_THEME.id);
433 expect(boot.bootVariables["--boot-background"]).toBe(
434 getThemeColorsForMode(GROVE_THEME, "dark")!.canvas,
435 );
436 });
437
438 it("ignores a mix half that names an unknown theme", () => {
439 const boot = runBootScript({
440 storage: {
441 [THEME_STORAGE_KEY]: "t3-chat",
442 [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system",
443 "t3code:theme-halves:v1": JSON.stringify({ dark: "gone-theme" }),
444 },
445 prefersDark: true,
446 });
447 expect(boot.themeId).toBe("t3-chat");
448 expect(boot.isDark).toBe(true);
449 });
450
451 it("uses runtime defaults for malformed custom roles", () => {
452 const boot = runBootScript({
453 storage: {
454 [THEME_STORAGE_KEY]: "partial",
455 [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true",
456 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([
457 {
458 id: "partial",
459 label: "Partial",
460 appearance: "dark",
461 colors: { canvas: "not-a-color", text: "#fffaff", accent: "nope" },
462 },
463 ]),
464 },
465 prefersDark: true,
466 });
467
468 expect(boot.themeId).toBe("partial");
469 expect(boot.bootVariables["--boot-background"]).toBe(getDefaultThemeColors("dark").canvas);
470 expect(boot.bootVariables["--boot-foreground"]).toBe("#fffaff");
471 expect(boot.bootVariables["--boot-accent"]).toBe(getDefaultThemeColors("dark").accent);
472 expect(boot.backgroundColor).toBe(DEFAULT_DARK_CHROME);
473 expect(boot.metaContent).toBe(DEFAULT_DARK_CHROME);
474 });
475
476 it("ignores malformed custom theme entries before applying a splash", () => {
477 const boot = runBootScript({
478 storage: {
479 [THEME_STORAGE_KEY]: "broken",
480 [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([
481 { id: "broken", label: "Broken", appearance: "light", colors: "bad" },
482 ]),
483 },
484 prefersDark: false,
485 });
486
487 expect(boot.themeId).toBeUndefined();
488 expect(boot.themeSelected).toBeUndefined();
489 expect(boot.backgroundColor).toBe("#ffffff");
490 expect(boot.metaContent).toBe("#ffffff");
491 });
492
493 it("leaves unknown preferences unthemed so the runtime default applies", () => {
494 const boot = runBootScript({
495 storage: { [THEME_STORAGE_KEY]: "gone-theme" },
496 prefersDark: true,
497 });
498 expect(boot.themeId).toBeUndefined();
499 expect(boot.themeSelected).toBeUndefined();
500 expect(boot.isDark).toBe(true);
501 });
502
503 it("follows the OS appearance when storage is unavailable", () => {
504 const light = runBootScript({ storageThrows: true, prefersDark: false });
505 expect(light.isDark).toBe(false);
506 expect(light.themeId).toBeUndefined();
507
508 const dark = runBootScript({ storageThrows: true, prefersDark: true });
509 expect(dark.isDark).toBe(true);
510 });
511});