Open History to see the last commit
1import { DEFAULT_SERVER_SETTINGS, type ServerSettings } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4const server = vi.hoisted(() => ({
5 settings: null as unknown as import("effect/reactivity").Atom.Writable<ServerSettings>,
6}));
7vi.mock(import("../state/server"), async () => {
8 const { Atom } = await import("effect/reactivity");
9 const { DEFAULT_SERVER_SETTINGS } = await import("@t3tools/contracts");
10 server.settings = Atom.make<ServerSettings>(DEFAULT_SERVER_SETTINGS);
11 const nightfall = {
12 id: "nightfall",
13 name: "Nightfall",
14 appearance: "dark",
15 canvas: "#1a1b26",
16 accent: "#7aa2f7",
17 } as const;
18 return {
19 themeServerSettingsAtom: server.settings,
20 themeServerEnvironmentThemesAtom: Atom.make([nightfall]),
21 } as never;
22});
23vi.mock(import("../state/primaryEnvironment"), async () => {
24 const { Atom } = await import("effect/reactivity");
25 return { localEnvironmentIdAtom: Atom.make("env-1") } as never;
26});
27
28import { appAtomRegistry } from "../rpc/atomRegistry";
29import { THEME_APPEARANCE_MODE_STORAGE_KEY } from "../themePalette";
30import {
31 defaultThemeGeneration,
32 defaultThemeToApply,
33 startDefaultThemeAdoption,
34 themeSetsOwnMode,
35} from "./useDefaultTheme";
36import { getThemeSnapshot, refreshTheme } from "./useTheme";
37
38const BASE = {
39 environmentId: "env-1",
40 defaultTheme: "nightfall",
41 defaultThemeSetAt: "2026-08-28T00:00:00.000Z",
42 appliedGeneration: null,
43 resolves: true,
44} as const;
45
46describe("default theme adoption", () => {
47 it("does not replay a generation it already applied", () => {
48 const generation = defaultThemeGeneration(BASE.defaultTheme, BASE.defaultThemeSetAt);
49 expect(defaultThemeToApply({ ...BASE, appliedGeneration: generation })).toBe(null);
50 });
51
52 it("applies the same theme again when the environment re-sets it", () => {
53 const applied = defaultThemeGeneration(BASE.defaultTheme, "2026-08-28T00:00:00.000Z");
54 const next = defaultThemeToApply({
55 ...BASE,
56 defaultThemeSetAt: "2026-08-29T00:00:00.000Z",
57 appliedGeneration: applied,
58 });
59 expect(next).not.toBe(null);
60 expect(next).not.toBe(applied);
61 });
62
63 it("applies the stock theme only when the environment set it", () => {
64 expect(defaultThemeToApply({ ...BASE, defaultTheme: "", defaultThemeSetAt: "" })).toBe(null);
65 expect(defaultThemeToApply({ ...BASE, defaultTheme: "" })).not.toBe(null);
66 });
67});
68
69it("lets a theme set its own mode only when no mode was set with it or after it", () => {
70 const theme = "2026-11-13T10:00:00.000Z";
71 const set = (defaultAppearance: string, defaultAppearanceSetAt: string) =>
72 themeSetsOwnMode({ defaultThemeSetAt: theme, defaultAppearance, defaultAppearanceSetAt });
73 expect(set("", "")).toBe(true);
74 expect(set("dark", "2026-11-13T09:00:00.000Z")).toBe(true);
75 expect(set("dark", theme)).toBe(false);
76 expect(set("light", "2026-11-13T11:00:00.000Z")).toBe(false);
77});
78
79describe("adopting both defaults together", () => {
80 const stubWindowStorage = (
81 setItem?: (store: Map<string, string>, key: string, value: string) => void,
82 ) => {
83 const store = new Map<string, string>([[THEME_APPEARANCE_MODE_STORAGE_KEY, "light"]]);
84 vi.stubGlobal("window", {
85 addEventListener: () => {},
86 removeEventListener: () => {},
87 localStorage: {
88 getItem: (key: string) => store.get(key) ?? null,
89 setItem: (key: string, value: string) =>
90 setItem ? setItem(store, key, value) : store.set(key, value),
91 removeItem: (key: string) => store.delete(key),
92 },
93 });
94 refreshTheme();
95 };
96
97 const adopt = (
98 defaults: Pick<
99 ServerSettings,
100 "defaultTheme" | "defaultThemeSetAt" | "defaultAppearance" | "defaultAppearanceSetAt"
101 >,
102 ) => {
103 stubWindowStorage();
104 appAtomRegistry.set(server.settings, { ...DEFAULT_SERVER_SETTINGS, ...defaults });
105 const stop = startDefaultThemeAdoption();
106 try {
107 const { theme, appearanceMode } = getThemeSnapshot();
108 return { theme, appearanceMode };
109 } finally {
110 stop();
111 vi.unstubAllGlobals();
112 }
113 };
114
115 it.each([
116 {
117 name: "a newer single-appearance theme keeps its mode",
118 defaultTheme: "nightfall",
119 defaultAppearance: "light" as const,
120 defaultAppearanceSetAt: "2026-11-13T09:00:00.000Z",
121 expected: { theme: "nightfall", appearanceMode: "dark" },
122 },
123 {
124 name: "the stock theme takes the mode set with it",
125 defaultTheme: "",
126 defaultAppearance: "dark" as const,
127 defaultAppearanceSetAt: "2026-11-13T10:00:00.000Z",
128 expected: { theme: "dark", appearanceMode: "dark" },
129 },
130 ])("$name", ({ expected, name: _name, ...defaults }) => {
131 expect(adopt({ ...defaults, defaultThemeSetAt: "2026-11-13T10:00:00.000Z" })).toEqual(expected);
132 });
133
134 it("defers theme adoption when persisting the mode generation fails", () => {
135 let failOnce = true;
136 stubWindowStorage((store, key, value) => {
137 if (failOnce && key.startsWith("t3code:default-appearance-applied:v1:")) {
138 failOnce = false;
139 throw new Error("storage write failed");
140 }
141 store.set(key, value);
142 });
143 const initialTheme = getThemeSnapshot().theme;
144 appAtomRegistry.set(server.settings, {
145 ...DEFAULT_SERVER_SETTINGS,
146 defaultTheme: "nightfall",
147 defaultThemeSetAt: "2026-11-13T10:00:00.000Z",
148 defaultAppearance: "light" as const,
149 defaultAppearanceSetAt: "2026-11-13T09:00:00.000Z",
150 });
151 try {
152 const stopFirst = startDefaultThemeAdoption();
153 try {
154 expect(getThemeSnapshot().theme).toBe(initialTheme);
155 } finally {
156 stopFirst();
157 }
158 const stopRetry = startDefaultThemeAdoption();
159 try {
160 const { theme, appearanceMode } = getThemeSnapshot();
161 expect({ theme, appearanceMode }).toEqual({
162 theme: "nightfall",
163 appearanceMode: "dark",
164 });
165 } finally {
166 stopRetry();
167 }
168 } finally {
169 vi.unstubAllGlobals();
170 }
171 });
172});