Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { getThemeColorsForMode, themeColorToHex, THEME_FILE_VERSION } from "./themePalette";
4import {
5 isVsCodeThemeFile,
6 pairVsCodeThemes,
7 parseVsCodeThemeFile,
8 resolveThemeLabelCollisions,
9} from "./vscodeThemeImport";
10
11function asHex(value: string): string {
12 const hex = themeColorToHex(value);
13 if (!hex) throw new Error(`Expected a theme color, received ${value}`);
14 return hex;
15}
16
17function contrastRatio(first: string, second: string): number {
18 const toChannels = (value: string) => {
19 const hex = asHex(value).slice(1);
20 return [0, 1, 2].map(
21 (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255,
22 );
23 };
24 const luminance = (value: string) =>
25 toChannels(value)
26 .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4))
27 .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0);
28 const lighter = Math.max(luminance(first), luminance(second));
29 const darker = Math.min(luminance(first), luminance(second));
30 return (lighter + 0.05) / (darker + 0.05);
31}
32
33// Shaped like a real workbench theme: dotted keys, alpha overlays, and a lot
34// of roles simply left out.
35const VSCODE_DARK = {
36 name: "pierre-dark-soft",
37 type: "dark",
38 colors: {
39 "editor.background": "#171717",
40 "editor.foreground": "#d4d4d4",
41 foreground: "#d4d4d4",
42 "sideBar.background": "#101010",
43 "sideBar.foreground": "#8a8a8a",
44 "sideBar.border": "#1d1d1d",
45 focusBorder: "#69b1ff",
46 "button.background": "#69b1ff",
47 "button.foreground": "#171717",
48 "input.border": "#2c2c2c",
49 "input.placeholderForeground": "#525252",
50 "terminal.background": "#101010",
51 "terminal.foreground": "#8a8a8a",
52 "list.hoverBackground": "#1f3e5e59",
53 "list.activeSelectionBackground": "#1f3e5e99",
54 },
55 tokenColors: [],
56};
57
58describe("VS Code theme import", () => {
59 it("recognises workbench themes and rejects our own files", () => {
60 expect(isVsCodeThemeFile(VSCODE_DARK)).toBe(true);
61 expect(isVsCodeThemeFile({ type: "dark", tokenColors: [] })).toBe(true);
62 expect(
63 isVsCodeThemeFile({
64 version: THEME_FILE_VERSION,
65 name: "Aurora",
66 appearance: "light",
67 colors: { canvas: "#ffffff" },
68 }),
69 ).toBe(false);
70 expect(isVsCodeThemeFile("nope")).toBe(false);
71 });
72
73 it("carries the editor surfaces and accent across", () => {
74 const theme = parseVsCodeThemeFile(VSCODE_DARK);
75 // The slug name is read as words; a displayName would win verbatim.
76 expect(theme.label).toBe("Pierre Dark Soft");
77 expect(theme.appearance).toBe("dark");
78 expect(asHex(theme.colors.canvas)).toBe("#171717");
79 expect(asHex(theme.colors.text)).toBe("#d4d4d4");
80 expect(asHex(theme.colors.accent)).toBe("#69b1ff");
81 expect(asHex(theme.colors.sidebar)).toBe("#101010");
82 expect(asHex(theme.colors.terminalBackground)).toBe("#101010");
83 });
84
85 it("flattens alpha overlays onto the surface they sit on", () => {
86 const theme = parseVsCodeThemeFile(VSCODE_DARK);
87 // #1f3e5e59 over the #101010 sidebar, not left semi-transparent.
88 expect(theme.colors.sidebarRowHover).toMatch(/^oklch\(/);
89 expect(asHex(theme.colors.sidebarRowHover)).not.toBe("#1f3e5e59");
90 expect(theme.colors.sidebarRowSelected).not.toBe(theme.colors.sidebar);
91 });
92
93 it("keeps a fallback placeholder dimmer than entered text", () => {
94 const theme = parseVsCodeThemeFile({
95 name: "Dark placeholder fallback",
96 type: "dark",
97 colors: {
98 "editor.background": "#1e1e2e",
99 "editor.foreground": "#cdd6f4",
100 "input.placeholderForeground": "#cdd6f473",
101 },
102 });
103
104 expect(
105 contrastRatio(theme.colors.placeholder, theme.colors.surfaceRaised),
106 ).toBeGreaterThanOrEqual(4.5);
107 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeGreaterThanOrEqual(
108 4.5,
109 );
110 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeLessThan(
111 contrastRatio(theme.colors.text, theme.colors.canvas),
112 );
113 });
114
115 it("moves reserved VS Code names to a non-reserved id", () => {
116 const darkPlus = parseVsCodeThemeFile({
117 name: "Dark+",
118 type: "dark",
119 colors: { "editor.background": "#1e1e1e" },
120 });
121 expect(darkPlus.label).toBe("Dark+");
122 expect(darkPlus.id).not.toBe("dark");
123 expect(darkPlus.id).toBe("dark-vscode");
124 });
125
126 it("keeps Dark+ and Light+ as separate themes instead of pairing them as +", () => {
127 const themes = pairVsCodeThemes([
128 parseVsCodeThemeFile({
129 name: "Light+",
130 type: "light",
131 colors: { "editor.background": "#ffffff" },
132 }),
133 parseVsCodeThemeFile({
134 name: "Dark+",
135 type: "dark",
136 colors: { "editor.background": "#1e1e1e" },
137 }),
138 ]);
139 expect(themes.map((theme) => [theme.id, theme.label])).toEqual([
140 ["light-vscode", "Light+"],
141 ["dark-vscode", "Dark+"],
142 ]);
143 });
144
145 it("fills every role the file omits with a readable derived value", () => {
146 const theme = parseVsCodeThemeFile(VSCODE_DARK);
147 const colors = getThemeColorsForMode(theme, "dark")!;
148 for (const value of Object.values(colors)) {
149 expect(value).toMatch(/^oklch\(/);
150 }
151 expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(4.5);
152 expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5);
153 expect(
154 contrastRatio(colors.messageActionForeground, colors.messageAction),
155 ).toBeGreaterThanOrEqual(4.5);
156 });
157
158 it("falls back to the editor background when the type is missing or odd", () => {
159 const untyped = parseVsCodeThemeFile({
160 name: "Untyped",
161 colors: { "editor.background": "#fdfdfd", "editor.foreground": "#202020" },
162 });
163 expect(untyped.appearance).toBe("light");
164 const hc = parseVsCodeThemeFile({
165 name: "High contrast",
166 type: "hc-light",
167 colors: { "editor.background": "#ffffff" },
168 });
169 expect(hc.appearance).toBe("light");
170 });
171
172 it("keeps an unreadable foreground out of the palette", () => {
173 const theme = parseVsCodeThemeFile({
174 name: "Unreadable",
175 type: "dark",
176 colors: { "editor.background": "#101010", "editor.foreground": "#111111" },
177 });
178 expect(asHex(theme.colors.text)).not.toBe("#111111");
179 expect(contrastRatio(theme.colors.text, theme.colors.canvas)).toBeGreaterThanOrEqual(4.5);
180 });
181
182 it("stays readable when the file replaces a surface but not its foreground", () => {
183 // A dark theme with a light sidebar: the derived sidebar foreground was
184 // solved for a dark surface and would vanish on this one.
185 const theme = parseVsCodeThemeFile({
186 name: "Split",
187 type: "dark",
188 colors: {
189 "editor.background": "#101010",
190 "editor.foreground": "#f5f5f5",
191 "sideBar.background": "#fafafa",
192 "terminal.background": "#fbfbfb",
193 },
194 });
195 expect(asHex(theme.colors.sidebar)).toBe("#fafafa");
196 expect(
197 contrastRatio(theme.colors.sidebarForeground, theme.colors.sidebar),
198 ).toBeGreaterThanOrEqual(4.5);
199 expect(
200 contrastRatio(theme.colors.terminalForeground, theme.colors.terminalBackground),
201 ).toBeGreaterThanOrEqual(4.5);
202 });
203
204 it("reads wide-gamut color() notation", () => {
205 // Themes authored for P3 displays (the shipped Pierre "vibrant" pair) use
206 // this instead of hex.
207 const theme = parseVsCodeThemeFile({
208 name: "Vibrant",
209 type: "dark",
210 colors: {
211 "editor.background": "color(display-p3 0.039216 0.039216 0.039216)",
212 "editor.foreground": "color(display-p3 0.980392 0.980392 0.980392)",
213 focusBorder: "color(display-p3 0.308664 0.645271 1.000000)",
214 "editor.selectionBackground": "color(display-p3 0.308664 0.645271 1.000000 / 0.300000)",
215 },
216 });
217 expect(asHex(theme.colors.canvas)).toMatch(/^#0[89ab]/);
218 expect(asHex(theme.colors.text)).toMatch(/^#f[a-f0-9]/);
219 // The P3 blue lands in sRGB blue, not black or a clipped grey.
220 const accent = asHex(theme.colors.accent);
221 const [red, green, blue] = [1, 3, 5].map((index) =>
222 Number.parseInt(accent.slice(index, index + 2), 16),
223 ) as [number, number, number];
224 expect(blue).toBeGreaterThan(200);
225 expect(blue).toBeGreaterThan(red);
226 expect(green).toBeGreaterThan(red);
227 });
228
229 it("pairs light and dark files from one family into dual-mode themes", () => {
230 const make = (name: string, type: "light" | "dark") =>
231 parseVsCodeThemeFile({
232 name,
233 type,
234 colors: {
235 "editor.background": type === "dark" ? "#101014" : "#fdfdfd",
236 "editor.foreground": type === "dark" ? "#e6e6e6" : "#1f1f1f",
237 focusBorder: "#69b1ff",
238 },
239 });
240 const themes = pairVsCodeThemes([
241 make("github-dark", "dark"),
242 make("github-light", "light"),
243 make("github-dark-colorblind", "dark"),
244 make("github-light-colorblind", "light"),
245 make("github-dark-dimmed", "dark"),
246 ]);
247
248 const labels = themes.map((theme) => theme.label);
249 expect(labels).toEqual(["Github", "Github Colorblind", "Github Dark Dimmed"]);
250 const github = themes[0]!;
251 expect(github.appearance).toBe("light");
252 expect(getThemeColorsForMode(github, "dark")).not.toBeNull();
253 expect(asHex(getThemeColorsForMode(github, "dark")!.canvas)).toBe("#101014");
254 expect(asHex(github.colors.canvas)).toBe("#fdfdfd");
255 // The unpaired dimmed variant stays a single dark theme.
256 expect(getThemeColorsForMode(themes[2]!, "light")).toBeNull();
257 });
258
259 it("does not guess when a family is ambiguous", () => {
260 const make = (name: string, type: "light" | "dark") =>
261 parseVsCodeThemeFile({
262 name,
263 type,
264 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
265 });
266 const themes = pairVsCodeThemes([
267 make("solar-dark", "dark"),
268 make("solar-dark-soft", "dark"),
269 make("solar-light", "light"),
270 ]);
271 // "solar-dark-soft" groups under its own key with no light partner, so
272 // it keeps its full name; the remaining pair merges.
273 expect(themes.map((theme) => theme.label).sort()).toEqual(["Solar", "Solar Dark Soft"]);
274 });
275
276 it("keeps derived surfaces neutral instead of washing them with the accent", () => {
277 // A gray theme with a blue focusBorder: roles the file omits (code
278 // surface, plain surfaces) must stay near the canvas, not turn blue.
279 const theme = parseVsCodeThemeFile(VSCODE_DARK);
280 const spread = (value: string) => {
281 const hex = asHex(value);
282 const channels = [1, 3, 5].map((index) => Number.parseInt(hex.slice(index, index + 2), 16));
283 return Math.max(...channels) - Math.min(...channels);
284 };
285 expect(spread(theme.colors.codeBackground)).toBeLessThanOrEqual(8);
286 expect(spread(theme.colors.surface)).toBeLessThanOrEqual(8);
287 expect(spread(theme.colors.text)).toBeLessThanOrEqual(12);
288 // The accent itself keeps the file's color.
289 expect(asHex(theme.colors.accent)).toBe("#69b1ff");
290 });
291
292 it("tells same-named variants apart by their file names", () => {
293 // Dracula ships dracula.json and dracula-soft.json that both say
294 // "Dracula" inside.
295 const dracula = (bg: string) =>
296 parseVsCodeThemeFile({
297 name: "Dracula",
298 type: "dark",
299 colors: { "editor.background": bg, "editor.foreground": "#f8f8f2" },
300 });
301 const themes = resolveThemeLabelCollisions([
302 { theme: dracula("#282a36"), sourceName: "dracula.json" },
303 { theme: dracula("#22232e"), sourceName: "dracula-soft.json" },
304 ]);
305 expect(themes.map((theme) => theme.label)).toEqual(["Dracula", "Dracula Soft"]);
306 expect(themes.map((theme) => theme.id)).toEqual(["dracula", "dracula-soft"]);
307 // Without file names the second falls back to numbering.
308 const numbered = resolveThemeLabelCollisions([
309 { theme: dracula("#282a36") },
310 { theme: dracula("#22232e") },
311 ]);
312 expect(numbered.map((theme) => theme.label)).toEqual(["Dracula", "Dracula 2"]);
313 });
314
315 it("falls back to the name when the displayName humanizes to nothing", () => {
316 const theme = parseVsCodeThemeFile({
317 displayName: "---",
318 name: "night-owl",
319 type: "dark",
320 colors: { "editor.background": "#011627" },
321 });
322 expect(theme.label).toBe("Night Owl");
323 });
324
325 it("keeps a pair whose stripped name is reserved as two single themes", () => {
326 const make = (name: string, type: "light" | "dark") =>
327 parseVsCodeThemeFile({
328 name,
329 type,
330 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
331 });
332 const themes = pairVsCodeThemes([make("grove-light", "light"), make("grove-dark", "dark")]);
333 expect(themes.map((theme) => theme.label).sort()).toEqual(["Grove Dark", "Grove Light"]);
334 });
335
336 it("explains a file with no editor background", () => {
337 expect(() => parseVsCodeThemeFile({ name: "Empty", type: "dark", colors: {} })).toThrow(
338 /editor\.background/,
339 );
340 });
341});