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