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