The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import indexHtml from "../index.html?raw"; |
| 4 | import { |
| 5 | CUSTOM_THEMES_STORAGE_KEY, |
| 6 | ENVIRONMENT_THEMES_STORAGE_KEY, |
| 7 | getDefaultThemeColors, |
| 8 | getThemeColorsForMode, |
| 9 | invalidateCustomThemes, |
| 10 | isKnownThemePreference, |
| 11 | publishedThemeDefinitions, |
| 12 | resolveThemeAppearance, |
| 13 | T3_CHAT_THEME, |
| 14 | EMBER_THEME, |
| 15 | GROVE_THEME, |
| 16 | IRIS_THEME, |
| 17 | OCEAN_THEME, |
| 18 | THEME_APPEARANCE_MODE_STORAGE_KEY, |
| 19 | THEME_FOLLOW_SYSTEM_STORAGE_KEY, |
| 20 | toCanonicalThemeColor, |
| 21 | } from "./themePalette"; |
| 22 | |
| 23 | const THEME_STORAGE_KEY = "t3code:theme"; |
| 24 | const DEFAULT_DARK_CHROME = getDefaultThemeColors("dark").chrome; |
| 25 | |
| 26 | const bootScript = (() => { |
| 27 | const match = indexHtml.match(/<script>([\s\S]*?)<\/script>/); |
| 28 | if (!match?.[1]) throw new Error("Could not find the inline boot script in index.html"); |
| 29 | return match[1]; |
| 30 | })(); |
| 31 | |
| 32 | type BootResult = { |
| 33 | isDark: boolean; |
| 34 | themeId: string | undefined; |
| 35 | themeSelected: string | undefined; |
| 36 | backgroundColor: string; |
| 37 | bootVariables: Record<string, string>; |
| 38 | metaContent: string | null; |
| 39 | }; |
| 40 | |
| 41 | function runBootScript(options: { |
| 42 | storage?: Record<string, string>; |
| 43 | storageThrows?: boolean; |
| 44 | prefersDark: boolean; |
| 45 | }): BootResult { |
| 46 | const classes = new Set<string>(); |
| 47 | const bootVariables: Record<string, string> = {}; |
| 48 | const meta = { |
| 49 | content: null as string | null, |
| 50 | setAttribute(_name: string, value: string) { |
| 51 | this.content = value; |
| 52 | }, |
| 53 | }; |
| 54 | const documentElement = { |
| 55 | dataset: {} as Record<string, string | undefined>, |
| 56 | classList: { |
| 57 | add: (name: string) => void classes.add(name), |
| 58 | remove: (name: string) => void classes.delete(name), |
| 59 | toggle: (name: string, force?: boolean) => { |
| 60 | const next = force ?? !classes.has(name); |
| 61 | if (next) classes.add(name); |
| 62 | else classes.delete(name); |
| 63 | return next; |
| 64 | }, |
| 65 | }, |
| 66 | style: { |
| 67 | backgroundColor: "", |
| 68 | setProperty: (name: string, value: string) => { |
| 69 | bootVariables[name] = value; |
| 70 | }, |
| 71 | }, |
| 72 | }; |
| 73 | const fakeDocument = { |
| 74 | documentElement, |
| 75 | querySelectorAll: (selector: string) => (selector === 'meta[name="theme-color"]' ? [meta] : []), |
| 76 | }; |
| 77 | const fakeWindow = { |
| 78 | localStorage: { |
| 79 | getItem: (key: string): string | null => { |
| 80 | if (options.storageThrows) throw new Error("storage blocked"); |
| 81 | return options.storage?.[key] ?? null; |
| 82 | }, |
| 83 | }, |
| 84 | matchMedia: () => ({ matches: options.prefersDark }), |
| 85 | }; |
| 86 | |
| 87 | const fakeCss = { |
| 88 | supports: (property: string, value: string) => |
| 89 | property === "color" && toCanonicalThemeColor(value) !== null, |
| 90 | }; |
| 91 | |
| 92 | new Function("window", "document", "CSS", bootScript)(fakeWindow, fakeDocument, fakeCss); |
| 93 | |
| 94 | return { |
| 95 | isDark: classes.has("dark"), |
| 96 | themeId: documentElement.dataset.themeId, |
| 97 | themeSelected: documentElement.dataset.themeSelected, |
| 98 | backgroundColor: documentElement.style.backgroundColor, |
| 99 | bootVariables, |
| 100 | metaContent: meta.content, |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | function runtimeResolvedAppearance( |
| 105 | storage: Record<string, string>, |
| 106 | prefersDark: boolean, |
| 107 | ): "light" | "dark" { |
| 108 | vi.stubGlobal("window", { |
| 109 | localStorage: { getItem: (key: string) => storage[key] ?? null }, |
| 110 | matchMedia: () => ({ matches: prefersDark }), |
| 111 | }); |
| 112 | invalidateCustomThemes(); |
| 113 | try { |
| 114 | const raw = storage[THEME_STORAGE_KEY] ?? null; |
| 115 | const theme = raw !== null && isKnownThemePreference(raw) ? raw : "system"; |
| 116 | const followRaw = storage[THEME_FOLLOW_SYSTEM_STORAGE_KEY] ?? null; |
| 117 | const appearanceRaw = storage[THEME_APPEARANCE_MODE_STORAGE_KEY] ?? null; |
| 118 | const appearanceMode = |
| 119 | appearanceRaw === "light" || appearanceRaw === "dark" || appearanceRaw === "system" |
| 120 | ? appearanceRaw |
| 121 | : followRaw === "true" |
| 122 | ? "system" |
| 123 | : followRaw === "false" |
| 124 | ? null |
| 125 | : theme === "system" |
| 126 | ? "system" |
| 127 | : null; |
| 128 | const followSystem = appearanceMode === "system"; |
| 129 | return resolveThemeAppearance(theme, prefersDark, followSystem, appearanceMode ?? undefined); |
| 130 | } finally { |
| 131 | vi.unstubAllGlobals(); |
| 132 | invalidateCustomThemes(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const AURORA_DUAL = { |
| 137 | id: "aurora", |
| 138 | label: "Aurora", |
| 139 | appearance: "light", |
| 140 | colors: { canvas: "#f8fbff", text: "#10243d", accent: "#5b6cff" }, |
| 141 | variants: { dark: { canvas: "#101827", text: "#eef5ff", accent: "#7c93ff" } }, |
| 142 | }; |
| 143 | const CHARCOAL_DARK_ONLY = { |
| 144 | id: "charcoal", |
| 145 | label: "Charcoal", |
| 146 | appearance: "dark", |
| 147 | colors: { canvas: "#1c1210", text: "#ffe8d9", accent: "#ff7a45" }, |
| 148 | }; |
| 149 | |
| 150 | describe("index.html boot script", () => { |
| 151 | const parityCases: ReadonlyArray<{ |
| 152 | name: string; |
| 153 | storage: Record<string, string>; |
| 154 | prefersDark: boolean; |
| 155 | }> = [ |
| 156 | { name: "no stored preference on a dark OS", storage: {}, prefersDark: true }, |
| 157 | { |
| 158 | name: "T3 Chat follows a dark OS", |
| 159 | storage: { [THEME_STORAGE_KEY]: "t3-chat", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" }, |
| 160 | prefersDark: true, |
| 161 | }, |
| 162 | { |
| 163 | name: "an explicit global dark mode applies to T3 Chat", |
| 164 | storage: { |
| 165 | [THEME_STORAGE_KEY]: "t3-chat", |
| 166 | [THEME_APPEARANCE_MODE_STORAGE_KEY]: "dark", |
| 167 | [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false", |
| 168 | }, |
| 169 | prefersDark: false, |
| 170 | }, |
| 171 | { |
| 172 | name: "Grove follows a dark OS", |
| 173 | storage: { [THEME_STORAGE_KEY]: "grove", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" }, |
| 174 | prefersDark: true, |
| 175 | }, |
| 176 | { |
| 177 | name: "Ocean follows a dark OS", |
| 178 | storage: { [THEME_STORAGE_KEY]: "ocean", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" }, |
| 179 | prefersDark: true, |
| 180 | }, |
| 181 | { |
| 182 | name: "Ember follows a dark OS", |
| 183 | storage: { [THEME_STORAGE_KEY]: "ember", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" }, |
| 184 | prefersDark: true, |
| 185 | }, |
| 186 | { |
| 187 | name: "Iris follows a dark OS", |
| 188 | storage: { [THEME_STORAGE_KEY]: "iris", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" }, |
| 189 | prefersDark: true, |
| 190 | }, |
| 191 | { |
| 192 | name: "a legacy t3-grove preference resolves through the alias", |
| 193 | storage: { [THEME_STORAGE_KEY]: "t3-grove", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true" }, |
| 194 | prefersDark: true, |
| 195 | }, |
| 196 | { |
| 197 | name: "legacy t3-chat-dark resolves to dark T3 Chat", |
| 198 | storage: { [THEME_STORAGE_KEY]: "t3-chat-dark" }, |
| 199 | prefersDark: true, |
| 200 | }, |
| 201 | { |
| 202 | name: "a dual-mode custom theme follows the OS", |
| 203 | storage: { |
| 204 | [THEME_STORAGE_KEY]: "aurora", |
| 205 | [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true", |
| 206 | [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([AURORA_DUAL]), |
| 207 | }, |
| 208 | prefersDark: true, |
| 209 | }, |
| 210 | { |
| 211 | name: "a dark-only custom theme stays dark on a light OS", |
| 212 | storage: { |
| 213 | [THEME_STORAGE_KEY]: "charcoal", |
| 214 | [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true", |
| 215 | [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([CHARCOAL_DARK_ONLY]), |
| 216 | }, |
| 217 | prefersDark: false, |
| 218 | }, |
| 219 | { |
| 220 | name: "a legacy mode-suffixed preference is treated as unknown", |
| 221 | storage: { |
| 222 | [THEME_STORAGE_KEY]: "aurora:dark", |
| 223 | [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([AURORA_DUAL]), |
| 224 | }, |
| 225 | prefersDark: true, |
| 226 | }, |
| 227 | { |
| 228 | name: "a removed custom theme falls back to system", |
| 229 | storage: { [THEME_STORAGE_KEY]: "gone-theme" }, |
| 230 | prefersDark: true, |
| 231 | }, |
| 232 | { |
| 233 | name: "a corrupted follow-system value falls back to inference", |
| 234 | storage: { [THEME_STORAGE_KEY]: "system", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "1" }, |
| 235 | prefersDark: true, |
| 236 | }, |
| 237 | { |
| 238 | name: "follow-system off keeps an explicit light preference on a dark OS", |
| 239 | storage: { [THEME_STORAGE_KEY]: "light", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false" }, |
| 240 | prefersDark: true, |
| 241 | }, |
| 242 | { |
| 243 | name: "a bare dark preference stays dark on a light OS", |
| 244 | storage: { [THEME_STORAGE_KEY]: "dark" }, |
| 245 | prefersDark: false, |
| 246 | }, |
| 247 | { |
| 248 | name: "follow-system off keeps a bare dark preference on a light OS", |
| 249 | storage: { [THEME_STORAGE_KEY]: "dark", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false" }, |
| 250 | prefersDark: false, |
| 251 | }, |
| 252 | ]; |
| 253 | |
| 254 | it.each(parityCases)("matches the runtime appearance: $name", ({ storage, prefersDark }) => { |
| 255 | const boot = runBootScript({ storage, prefersDark }); |
| 256 | expect(boot.isDark).toBe(runtimeResolvedAppearance(storage, prefersDark) === "dark"); |
| 257 | }); |
| 258 | |
| 259 | it("accepts exponent-form OKLCH before the runtime mounts", () => { |
| 260 | const colors = { |
| 261 | canvas: "oklch(9.5e-1 1e-2 2.8e2)", |
| 262 | chrome: "oklch(9.4e-1 1e-2 2.8e2)", |
| 263 | text: "oklch(2e-1 0 0 / 9e-1)", |
| 264 | accent: "oklch(6.2e-1 0.2 2.8e2)", |
| 265 | }; |
| 266 | const boot = runBootScript({ |
| 267 | storage: { |
| 268 | [THEME_STORAGE_KEY]: "scientific", |
| 269 | [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([ |
| 270 | { |
| 271 | id: "scientific", |
| 272 | label: "Scientific", |
| 273 | appearance: "light", |
| 274 | colors, |
| 275 | }, |
| 276 | ]), |
| 277 | }, |
| 278 | prefersDark: false, |
| 279 | }); |
| 280 | |
| 281 | expect(boot.bootVariables["--boot-background"]).toBe(colors.canvas); |
| 282 | expect(boot.bootVariables["--boot-foreground"]).toBe(colors.text); |
| 283 | expect(boot.bootVariables["--boot-accent"]).toBe(colors.accent); |
| 284 | expect(boot.backgroundColor).toBe(colors.chrome); |
| 285 | expect(boot.metaContent).toBe(colors.chrome); |
| 286 | }); |
| 287 | |
| 288 | it("paints a published theme from its stored copy before the server connects", () => { |
| 289 | const [published] = publishedThemeDefinitions([ |
| 290 | { |
| 291 | id: "nightfall", |
| 292 | name: "Nightfall", |
| 293 | appearance: "dark", |
| 294 | canvas: "#1a1b26", |
| 295 | accent: "#7aa2f7", |
| 296 | }, |
| 297 | ]); |
| 298 | if (!published) throw new Error("expected the published theme"); |
| 299 | const boot = runBootScript({ |
| 300 | storage: { |
| 301 | [THEME_STORAGE_KEY]: "nightfall", |
| 302 | [ENVIRONMENT_THEMES_STORAGE_KEY]: JSON.stringify([published]), |
| 303 | }, |
| 304 | prefersDark: true, |
| 305 | }); |
| 306 | |
| 307 | expect(boot.themeId).toBe("nightfall"); |
| 308 | expect(boot.bootVariables["--boot-background"]).toBe(published.colors.canvas); |
| 309 | expect(boot.backgroundColor).toBe(published.colors.chrome); |
| 310 | }); |
| 311 | |
| 312 | it("keeps every built-in boot splash in sync with the real palettes", () => { |
| 313 | for (const theme of [T3_CHAT_THEME, GROVE_THEME, OCEAN_THEME, EMBER_THEME, IRIS_THEME]) { |
| 314 | expect(theme.appearance).toBe("light"); |
| 315 | for (const mode of ["light", "dark"] as const) { |
| 316 | const colors = getThemeColorsForMode(theme, mode); |
| 317 | expect(colors).not.toBeNull(); |
| 318 | const boot = runBootScript({ |
| 319 | storage: { |
| 320 | [THEME_STORAGE_KEY]: theme.id, |
| 321 | [THEME_APPEARANCE_MODE_STORAGE_KEY]: mode, |
| 322 | }, |
| 323 | prefersDark: mode === "dark", |
| 324 | }); |
| 325 | expect(boot.themeId).toBe(theme.id); |
| 326 | expect(boot.isDark).toBe(mode === "dark"); |
| 327 | expect(boot.bootVariables["--boot-background"]).toBe(colors!.canvas); |
| 328 | expect(boot.bootVariables["--boot-foreground"]).toBe(colors!.text); |
| 329 | expect(boot.bootVariables["--boot-accent"]).toBe(colors!.accent); |
| 330 | expect(boot.backgroundColor).toBe(colors!.chrome); |
| 331 | expect(boot.metaContent).toBe(colors!.chrome); |
| 332 | } |
| 333 | } |
| 334 | }); |
| 335 | |
| 336 | it("applies the matching half of an automatic mix to the splash", () => { |
| 337 | const storage = { |
| 338 | [THEME_STORAGE_KEY]: "t3-chat", |
| 339 | [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system", |
| 340 | "t3code:theme-halves:v1": JSON.stringify({ dark: GROVE_THEME.id }), |
| 341 | }; |
| 342 | |
| 343 | const dark = runBootScript({ storage, prefersDark: true }); |
| 344 | expect(dark.isDark).toBe(true); |
| 345 | expect(dark.themeId).toBe(GROVE_THEME.id); |
| 346 | expect(dark.bootVariables["--boot-background"]).toBe( |
| 347 | getThemeColorsForMode(GROVE_THEME, "dark")!.canvas, |
| 348 | ); |
| 349 | |
| 350 | const light = runBootScript({ storage, prefersDark: false }); |
| 351 | expect(light.isDark).toBe(false); |
| 352 | expect(light.themeId).toBe("t3-chat"); |
| 353 | expect(light.bootVariables["--boot-background"]).toBe( |
| 354 | getThemeColorsForMode(T3_CHAT_THEME, "light")!.canvas, |
| 355 | ); |
| 356 | }); |
| 357 | |
| 358 | it("paints the half's splash when the base theme no longer exists", () => { |
| 359 | const boot = runBootScript({ |
| 360 | storage: { |
| 361 | [THEME_STORAGE_KEY]: "gone-theme", |
| 362 | [THEME_APPEARANCE_MODE_STORAGE_KEY]: "system", |
| 363 | "t3code:theme-halves:v1": JSON.stringify({ dark: GROVE_THEME.id }), |
| 364 | }, |
| 365 | prefersDark: true, |
| 366 | }); |
| 367 | expect(boot.isDark).toBe(true); |
| 368 | expect(boot.themeId).toBe(GROVE_THEME.id); |
| 369 | expect(boot.themeSelected).toBe("true"); |
| 370 | expect(boot.bootVariables["--boot-background"]).toBe( |
| 371 | getThemeColorsForMode(GROVE_THEME, "dark")!.canvas, |
| 372 | ); |
| 373 | }); |
| 374 | |
| 375 | it("uses runtime defaults for malformed custom roles", () => { |
| 376 | const boot = runBootScript({ |
| 377 | storage: { |
| 378 | [THEME_STORAGE_KEY]: "partial", |
| 379 | [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true", |
| 380 | [CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([ |
| 381 | { |
| 382 | id: "partial", |
| 383 | label: "Partial", |
| 384 | appearance: "dark", |
| 385 | colors: { canvas: "not-a-color", text: "#fffaff", accent: "nope" }, |
| 386 | }, |
| 387 | ]), |
| 388 | }, |
| 389 | prefersDark: true, |
| 390 | }); |
| 391 | |
| 392 | expect(boot.themeId).toBe("partial"); |
| 393 | expect(boot.bootVariables["--boot-background"]).toBe(getDefaultThemeColors("dark").canvas); |
| 394 | expect(boot.bootVariables["--boot-foreground"]).toBe("#fffaff"); |
| 395 | expect(boot.bootVariables["--boot-accent"]).toBe(getDefaultThemeColors("dark").accent); |
| 396 | expect(boot.backgroundColor).toBe(DEFAULT_DARK_CHROME); |
| 397 | expect(boot.metaContent).toBe(DEFAULT_DARK_CHROME); |
| 398 | }); |
| 399 | |
| 400 | it("follows the OS appearance when storage is unavailable", () => { |
| 401 | const light = runBootScript({ storageThrows: true, prefersDark: false }); |
| 402 | expect(light.isDark).toBe(false); |
| 403 | expect(light.themeId).toBeUndefined(); |
| 404 | |
| 405 | const dark = runBootScript({ storageThrows: true, prefersDark: true }); |
| 406 | expect(dark.isDark).toBe(true); |
| 407 | }); |
| 408 | }); |