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

useEnvironmentTheme.test.ts

192 lines6.3 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import { environmentThemeDefinition, publishedThemeDefinitions } from "./useEnvironmentTheme";
4import {
5 getDefaultThemeColors,
6 getThemeDefinition,
7 installCustomTheme,
8 invalidateCustomThemes,
9 setEnvironmentThemes,
10 THEME_COLOR_ROLES,
11} from "../themePalette";
12​
13const NIGHTFALL_THEME = {
14 id: "nightfall",
15 name: "Nightfall",
16 appearance: "dark",
17 canvas: "#1a1b26",
18 accent: "#7aa2f7",
19} as const;
20​
21afterEach(() => {
22 setEnvironmentThemes([]);
23});
24​
25describe("environment themes", () => {
26 it("generates every role from the two published seeds", () => {
27 const theme = environmentThemeDefinition(NIGHTFALL_THEME);
28​
29 expect(theme.id).toBe("nightfall");
30 expect(theme.label).toBe("Nightfall");
31 expect(theme.appearance).toBe("dark");
32 for (const role of THEME_COLOR_ROLES) {
33 expect(theme.colors[role], `missing ${role}`).toBeTruthy();
34 }
35 });
36​
37 it("layers published roles over the generated palette", () => {
38 const generated = environmentThemeDefinition(NIGHTFALL_THEME);
39 const overridden = environmentThemeDefinition({
40 ...NIGHTFALL_THEME,
41 colors: { terminalSelection: "#292e42", error: "#f7768e" },
42 });
43​
44 expect(overridden.colors.terminalSelection).not.toBe(generated.colors.terminalSelection);
45 expect(overridden.colors.error).not.toBe(generated.colors.error);
46 expect(overridden.colors.sidebar).toBe(generated.colors.sidebar);
47 });
48​
49 it("renders an exported theme file on the stock defaults", () => {
50 const theme = environmentThemeDefinition({
51 id: "shared-light",
52 version: 1,
53 name: "Shared Light",
54 appearance: "light",
55 colors: { canvas: "oklch(0.95 0.01 250)", accent: "#1e66f5" },
56 variants: { dark: { canvas: "#1a1b26" } },
57 });
58​
59 expect(theme.label).toBe("Shared Light");
60 expect(theme.colors.accent).toBeTruthy();
61 expect(theme.variants?.dark?.canvas).toBeTruthy();
62 for (const role of THEME_COLOR_ROLES) {
63 expect(theme.colors[role], `missing ${role}`).toBeTruthy();
64 expect(theme.variants?.dark?.[role], `missing dark ${role}`).toBeTruthy();
65 }
66 });
67​
68 it("builds a seeded theme's variant on the variant appearance's defaults", () => {
69 const theme = environmentThemeDefinition({
70 ...NIGHTFALL_THEME,
71 variants: { light: { canvas: "#eff1f5" } },
72 });
73​
74 const lightDefaults = getDefaultThemeColors("light");
75 expect(theme.variants?.light?.text).toBe(lightDefaults.text);
76 expect(theme.variants?.light?.canvas).not.toBe(theme.colors.canvas);
77 });
78​
79 it("drops published themes with reserved ids", () => {
80 const definitions = publishedThemeDefinitions([
81 NIGHTFALL_THEME,
82 { ...NIGHTFALL_THEME, id: "t3-iris", name: "Impostor Iris" },
83 { ...NIGHTFALL_THEME, id: "ocean", name: "Impostor Ocean" },
84 { ...NIGHTFALL_THEME, id: "dark", name: "Impostor Dark" },
85 ]);
86​
87 expect(definitions.map((definition) => definition.id)).toEqual(["nightfall"]);
88 });
89​
90 it("drops published palettes with no usable colors", () => {
91 const theme = { id: "invalid", name: "Invalid", appearance: "dark" } as const;
92 const definitions = publishedThemeDefinitions([
93 { ...theme, colors: { canvas: "not-a-color" } },
94 { ...theme, id: "unknown-roles", colors: { futureRole: "#ffffff" } },
95 {
96 ...theme,
97 id: "ignored-variant",
98 colors: { canvas: "not-a-color" },
99 variants: { dark: { canvas: "#112233" } },
100 },
101 ]);
102​
103 expect(definitions).toEqual([]);
104 });
105​
106 it("keeps seeds, partial palettes, and usable alternate appearances", () => {
107 const theme = { name: "Shared", appearance: "dark" } as const;
108 const definitions = publishedThemeDefinitions([
109 NIGHTFALL_THEME,
110 {
111 ...theme,
112 id: "partial",
113 colors: { canvas: "not-a-color", text: "#ffffff", futureRole: "#ffffff" },
114 },
115 {
116 ...theme,
117 id: "light-variant",
118 colors: { canvas: "not-a-color" },
119 variants: { light: { canvas: "#eff1f5" } },
120 },
121 ]);
122​
123 expect(definitions.map((definition) => definition.id)).toEqual([
124 "nightfall",
125 "partial",
126 "light-variant",
127 ]);
128 });
129​
130 it("ignores published roles this build does not render", () => {
131 const theme = environmentThemeDefinition({
132 ...NIGHTFALL_THEME,
133 colors: { notARole: "#ff0000", text: "#ffffff" },
134 });
135​
136 expect(theme.colors).not.toHaveProperty("notARole");
137 for (const role of THEME_COLOR_ROLES) {
138 expect(theme.colors[role], `missing ${role}`).toBeTruthy();
139 }
140 });
141​
142 it("marks only the pure seeded form as managed", () => {
143 expect(environmentThemeDefinition(NIGHTFALL_THEME).managed).toBe(true);
144 expect(
145 environmentThemeDefinition({ ...NIGHTFALL_THEME, colors: { error: "#f7768e" } }).managed,
146 ).toBeUndefined();
147 expect(
148 environmentThemeDefinition({
149 id: "shared-light",
150 version: 1,
151 name: "Shared Light",
152 appearance: "light",
153 colors: { canvas: "#eff1f5", accent: "#1e66f5" },
154 }).managed,
155 ).toBeUndefined();
156 });
157​
158 it("resolves published ids only while the machine publishes them", () => {
159 expect(getThemeDefinition("nightfall")).toBe(null);
160​
161 setEnvironmentThemes([environmentThemeDefinition(NIGHTFALL_THEME)]);
162 expect(getThemeDefinition("nightfall")?.label).toBe("Nightfall");
163​
164 setEnvironmentThemes([]);
165 expect(getThemeDefinition("nightfall")).toBe(null);
166 });
167​
168 it("lets a theme the user saved win an id collision", () => {
169 const store = new Map<string, string>();
170 vi.stubGlobal("window", {
171 addEventListener: vi.fn(),
172 removeEventListener: vi.fn(),
173 localStorage: {
174 getItem: (key: string) => store.get(key) ?? null,
175 setItem: (key: string, value: string) => store.set(key, value),
176 removeItem: (key: string) => store.delete(key),
177 },
178 });
179 invalidateCustomThemes();
180 try {
181 const environment = environmentThemeDefinition(NIGHTFALL_THEME);
182 setEnvironmentThemes([environment]);
183 installCustomTheme({ ...environment, label: "My Nightfall" });
184​
185 expect(getThemeDefinition("nightfall")?.label).toBe("My Nightfall");
186 } finally {
187 vi.unstubAllGlobals();
188 invalidateCustomThemes();
189 }
190 expect(getThemeDefinition("nightfall")?.label).toBe("Nightfall");
191 });
192});