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
33const VSCODE_DARK = {
34 name: "pierre-dark-soft",
35 type: "dark",
36 colors: {
37 "editor.background": "#171717",
38 "editor.foreground": "#d4d4d4",
39 foreground: "#d4d4d4",
40 "sideBar.background": "#101010",
41 "sideBar.foreground": "#8a8a8a",
42 "sideBar.border": "#1d1d1d",
43 focusBorder: "#69b1ff",
44 "button.background": "#69b1ff",
45 "button.foreground": "#171717",
46 "input.border": "#2c2c2c",
47 "input.placeholderForeground": "#525252",
48 "terminal.background": "#101010",
49 "terminal.foreground": "#8a8a8a",
50 "list.hoverBackground": "#1f3e5e59",
51 "list.activeSelectionBackground": "#1f3e5e99",
52 },
53 tokenColors: [],
54};
55
56describe("VS Code theme import", () => {
57 it("recognises workbench themes and rejects our own files", () => {
58 expect(isVsCodeThemeFile(VSCODE_DARK)).toBe(true);
59 expect(isVsCodeThemeFile({ type: "dark", tokenColors: [] })).toBe(true);
60 expect(
61 isVsCodeThemeFile({
62 version: THEME_FILE_VERSION,
63 name: "Aurora",
64 appearance: "light",
65 colors: { canvas: "#ffffff" },
66 }),
67 ).toBe(false);
68 expect(isVsCodeThemeFile("nope")).toBe(false);
69 });
70
71 it("carries the editor surfaces and accent across", () => {
72 const theme = parseVsCodeThemeFile(VSCODE_DARK);
73 expect(theme.label).toBe("Pierre Dark Soft");
74 expect(theme.appearance).toBe("dark");
75 expect(asHex(theme.colors.canvas)).toBe("#171717");
76 expect(asHex(theme.colors.text)).toBe("#d4d4d4");
77 expect(asHex(theme.colors.accent)).toBe("#69b1ff");
78 expect(asHex(theme.colors.sidebar)).toBe("#101010");
79 expect(asHex(theme.colors.terminalBackground)).toBe("#101010");
80 });
81
82 it("keeps a fallback placeholder dimmer than entered text", () => {
83 const theme = parseVsCodeThemeFile({
84 name: "Dark placeholder fallback",
85 type: "dark",
86 colors: {
87 "editor.background": "#1e1e2e",
88 "editor.foreground": "#cdd6f4",
89 "input.placeholderForeground": "#cdd6f473",
90 },
91 });
92
93 expect(
94 contrastRatio(theme.colors.placeholder, theme.colors.surfaceRaised),
95 ).toBeGreaterThanOrEqual(4.5);
96 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeGreaterThanOrEqual(
97 4.5,
98 );
99 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeLessThan(
100 contrastRatio(theme.colors.text, theme.colors.canvas),
101 );
102 });
103
104 it("moves reserved VS Code names to a non-reserved id", () => {
105 const darkPlus = parseVsCodeThemeFile({
106 name: "Dark+",
107 type: "dark",
108 colors: { "editor.background": "#1e1e1e" },
109 });
110 expect(darkPlus.label).toBe("Dark+");
111 expect(darkPlus.id).not.toBe("dark");
112 expect(darkPlus.id).toBe("dark-vscode");
113 });
114
115 it("keeps Dark+ and Light+ as separate themes instead of pairing them as +", () => {
116 const themes = pairVsCodeThemes([
117 parseVsCodeThemeFile({
118 name: "Light+",
119 type: "light",
120 colors: { "editor.background": "#ffffff" },
121 }),
122 parseVsCodeThemeFile({
123 name: "Dark+",
124 type: "dark",
125 colors: { "editor.background": "#1e1e1e" },
126 }),
127 ]);
128 expect(themes.map((theme) => [theme.id, theme.label])).toEqual([
129 ["light-vscode", "Light+"],
130 ["dark-vscode", "Dark+"],
131 ]);
132 });
133
134 it("prefers a visible input background over a transparent border", () => {
135 const catppuccin = parseVsCodeThemeFile({
136 name: "Catppuccin Mocha",
137 type: "dark",
138 colors: {
139 "editor.background": "#1e1e2e",
140 "input.border": "#00000000",
141 "input.background": "#313244",
142 },
143 });
144 expect(asHex(catppuccin.colors.input)).toBe("#313244");
145 });
146
147 it("keeps unchecked inputs distinct from the checked action color", () => {
148 const gruvbox = parseVsCodeThemeFile({
149 name: "Gruvbox Light Soft",
150 type: "light",
151 colors: {
152 "editor.background": "#f2e5bc",
153 "button.background": "#45858880",
154 "input.background": "#45858880",
155 "input.border": "#928374",
156 },
157 });
158 expect(
159 contrastRatio(gruvbox.colors.input, gruvbox.colors.messageAction),
160 ).toBeGreaterThanOrEqual(1.1);
161 });
162
163 it("skips a transparent focus border for a visible accent key", () => {
164 const vitesse = parseVsCodeThemeFile({
165 name: "Vitesse Dark",
166 type: "dark",
167 colors: {
168 "editor.background": "#121212",
169 focusBorder: "#00000000",
170 "button.background": "#4d9375",
171 },
172 });
173 expect(asHex(vitesse.colors.accent)).toBe("#4d9375");
174 expect(asHex(vitesse.colors.focus)).toBe("#4d9375");
175 expect(contrastRatio(vitesse.colors.focus, vitesse.colors.canvas)).toBeGreaterThanOrEqual(1.1);
176 });
177
178 it("keeps focus visible against an explicit raised surface", () => {
179 const theme = parseVsCodeThemeFile({
180 name: "Raised focus",
181 type: "dark",
182 colors: {
183 "editor.background": "#000000",
184 focusBorder: "#111111",
185 "editorWidget.background": "#111111",
186 "button.background": "#4d9375",
187 },
188 });
189 expect(contrastRatio(theme.colors.focus, theme.colors.surfaceRaised)).toBeGreaterThanOrEqual(
190 1.1,
191 );
192 expect(asHex(theme.colors.focus)).toBe("#4d9375");
193 });
194
195 it("skips a transparent button background for the action color", () => {
196 const theme = parseVsCodeThemeFile({
197 name: "Transparent button",
198 type: "dark",
199 colors: {
200 "editor.background": "#121212",
201 focusBorder: "#4d9375",
202 "button.background": "#00000000",
203 },
204 });
205 expect(asHex(theme.colors.messageAction)).toBe("#4d9375");
206 });
207 it("composites find backgrounds onto the canvas and foregrounds onto their match", () => {
208 const { colors } = parseVsCodeThemeFile({
209 type: "dark",
210 colors: {
211 "editor.background": "#202020",
212 "editor.findMatchBackground": "#33669980",
213 "editor.findMatchForeground": "#ffffffcc",
214 "editor.findMatchHighlightBackground": "#ffcc0040",
215 "editor.findMatchHighlightForeground": "#ffffff",
216 },
217 });
218
219 expect(asHex(colors.searchMatchActiveBackground)).toBe("#2a435d");
220 expect(asHex(colors.searchMatchActiveForeground)).toBe("#d4d9df");
221 expect(asHex(colors.searchMatchBackground)).toBe("#584b18");
222 expect(asHex(colors.searchMatchForeground)).toBe("#ffffff");
223 });
224
225 it.each(["light", "dark"])(
226 "keeps missing or invalid find colours readable in %s themes",
227 (type) => {
228 const base = {
229 "editor.background": type === "dark" ? "#101010" : "#fafafa",
230 };
231 const missing = parseVsCodeThemeFile({ type, colors: base }).colors;
232 const invalid = parseVsCodeThemeFile({
233 type,
234 colors: {
235 ...base,
236 "editor.findMatchBackground": "not-a-colour",
237 "editor.findMatchForeground": 42,
238 "editor.findMatchHighlightBackground": null,
239 "editor.findMatchHighlightForeground": "invalid",
240 },
241 }).colors;
242 const unreadable = parseVsCodeThemeFile({
243 type,
244 colors: {
245 ...base,
246 "editor.findMatchBackground": "#ffffcc",
247 "editor.findMatchForeground": "#ffffff",
248 "editor.findMatchHighlightBackground": "#101020",
249 "editor.findMatchHighlightForeground": "#111111",
250 },
251 }).colors;
252
253 for (const colors of [missing, invalid, unreadable]) {
254 expect(
255 contrastRatio(colors.searchMatchForeground, colors.searchMatchBackground),
256 ).toBeGreaterThanOrEqual(4.5);
257 expect(
258 contrastRatio(colors.searchMatchActiveForeground, colors.searchMatchActiveBackground),
259 ).toBeGreaterThanOrEqual(4.5);
260 }
261 expect(invalid.searchMatchBackground).toBe(missing.searchMatchBackground);
262 expect(invalid.searchMatchActiveBackground).toBe(missing.searchMatchActiveBackground);
263 expect(missing.searchMatchBackground).not.toBe(missing.searchMatchActiveBackground);
264 expect(missing.searchMatchBackground).not.toBe(missing.canvas);
265 expect(missing.searchMatchActiveBackground).not.toBe(missing.canvas);
266 },
267 );
268
269 it("fills every role the file omits with a readable derived value", () => {
270 const theme = parseVsCodeThemeFile(VSCODE_DARK);
271 const colors = getThemeColorsForMode(theme, "dark")!;
272 for (const value of Object.values(colors)) {
273 expect(value).toMatch(/^oklch\(/);
274 }
275 expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(4.5);
276 expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5);
277 expect(
278 contrastRatio(colors.messageActionForeground, colors.messageAction),
279 ).toBeGreaterThanOrEqual(4.5);
280 });
281
282 it("falls back to the editor background when the type is missing or odd", () => {
283 const untyped = parseVsCodeThemeFile({
284 name: "Untyped",
285 colors: { "editor.background": "#fdfdfd", "editor.foreground": "#202020" },
286 });
287 expect(untyped.appearance).toBe("light");
288 const hc = parseVsCodeThemeFile({
289 name: "High contrast",
290 type: "hc-light",
291 colors: { "editor.background": "#ffffff" },
292 });
293 expect(hc.appearance).toBe("light");
294 });
295
296 it("stays readable when the file replaces a surface but not its foreground", () => {
297 const theme = parseVsCodeThemeFile({
298 name: "Split",
299 type: "dark",
300 colors: {
301 "editor.background": "#101010",
302 "editor.foreground": "#f5f5f5",
303 "sideBar.background": "#fafafa",
304 "terminal.background": "#fbfbfb",
305 },
306 });
307 expect(asHex(theme.colors.sidebar)).toBe("#fafafa");
308 expect(
309 contrastRatio(theme.colors.sidebarForeground, theme.colors.sidebar),
310 ).toBeGreaterThanOrEqual(4.5);
311 expect(
312 contrastRatio(theme.colors.terminalForeground, theme.colors.terminalBackground),
313 ).toBeGreaterThanOrEqual(4.5);
314 });
315
316 it("reads wide-gamut color() notation", () => {
317 const theme = parseVsCodeThemeFile({
318 name: "Vibrant",
319 type: "dark",
320 colors: {
321 "editor.background": "color(display-p3 0.039216 0.039216 0.039216)",
322 "editor.foreground": "color(display-p3 0.980392 0.980392 0.980392)",
323 focusBorder: "color(display-p3 0.308664 0.645271 1.000000)",
324 "editor.selectionBackground": "color(display-p3 0.308664 0.645271 1.000000 / 0.300000)",
325 },
326 });
327 expect(asHex(theme.colors.canvas)).toMatch(/^#0[89ab]/);
328 expect(asHex(theme.colors.text)).toMatch(/^#f[a-f0-9]/);
329 const accent = asHex(theme.colors.accent);
330 const [red, green, blue] = [1, 3, 5].map((index) =>
331 Number.parseInt(accent.slice(index, index + 2), 16),
332 ) as [number, number, number];
333 expect(blue).toBeGreaterThan(200);
334 expect(blue).toBeGreaterThan(red);
335 expect(green).toBeGreaterThan(red);
336 });
337
338 it("pairs light and dark files from one family into dual-mode themes", () => {
339 const make = (name: string, type: "light" | "dark") =>
340 parseVsCodeThemeFile({
341 name,
342 type,
343 colors: {
344 "editor.background": type === "dark" ? "#101014" : "#fdfdfd",
345 "editor.foreground": type === "dark" ? "#e6e6e6" : "#1f1f1f",
346 focusBorder: "#69b1ff",
347 },
348 });
349 const themes = pairVsCodeThemes([
350 make("github-dark", "dark"),
351 make("github-light", "light"),
352 make("github-dark-colorblind", "dark"),
353 make("github-light-colorblind", "light"),
354 make("github-dark-dimmed", "dark"),
355 ]);
356
357 const labels = themes.map((theme) => theme.label);
358 expect(labels).toEqual(["Github", "Github Colorblind", "Github Dark Dimmed"]);
359 const github = themes[0]!;
360 expect(github.appearance).toBe("light");
361 expect(getThemeColorsForMode(github, "dark")).not.toBeNull();
362 expect(asHex(getThemeColorsForMode(github, "dark")!.canvas)).toBe("#101014");
363 expect(asHex(github.colors.canvas)).toBe("#fdfdfd");
364 expect(getThemeColorsForMode(themes[2]!, "light")).toBeNull();
365 });
366
367 it("does not guess when a family is ambiguous", () => {
368 const make = (name: string, type: "light" | "dark") =>
369 parseVsCodeThemeFile({
370 name,
371 type,
372 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
373 });
374 const themes = pairVsCodeThemes([
375 make("solar-dark", "dark"),
376 make("solar-dark-soft", "dark"),
377 make("solar-light", "light"),
378 ]);
379 expect(themes.map((theme) => theme.label).sort()).toEqual(["Solar", "Solar Dark Soft"]);
380 });
381
382 it("tells same-named variants apart by their file names", () => {
383 // Dracula ships dracula.json and dracula-soft.json that both say
384 // "Dracula" inside.
385 const dracula = (bg: string) =>
386 parseVsCodeThemeFile({
387 name: "Dracula",
388 type: "dark",
389 colors: { "editor.background": bg, "editor.foreground": "#f8f8f2" },
390 });
391 const themes = resolveThemeLabelCollisions([
392 { theme: dracula("#282a36"), sourceName: "dracula.json" },
393 { theme: dracula("#22232e"), sourceName: "dracula-soft.json" },
394 ]);
395 expect(themes.map((theme) => theme.label)).toEqual(["Dracula", "Dracula Soft"]);
396 expect(themes.map((theme) => theme.id)).toEqual(["dracula", "dracula-soft"]);
397 const numbered = resolveThemeLabelCollisions([
398 { theme: dracula("#282a36") },
399 { theme: dracula("#22232e") },
400 ]);
401 expect(numbered.map((theme) => theme.label)).toEqual(["Dracula", "Dracula 2"]);
402 });
403});