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("flattens alpha overlays onto the surface they sit on", () => {
83 const theme = parseVsCodeThemeFile(VSCODE_DARK);
84 expect(theme.colors.sidebarRowHover).toMatch(/^oklch\(/);
85 expect(asHex(theme.colors.sidebarRowHover)).not.toBe("#1f3e5e59");
86 expect(theme.colors.sidebarRowSelected).not.toBe(theme.colors.sidebar);
87 });
88
89 it("keeps a fallback placeholder dimmer than entered text", () => {
90 const theme = parseVsCodeThemeFile({
91 name: "Dark placeholder fallback",
92 type: "dark",
93 colors: {
94 "editor.background": "#1e1e2e",
95 "editor.foreground": "#cdd6f4",
96 "input.placeholderForeground": "#cdd6f473",
97 },
98 });
99
100 expect(
101 contrastRatio(theme.colors.placeholder, theme.colors.surfaceRaised),
102 ).toBeGreaterThanOrEqual(4.5);
103 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeGreaterThanOrEqual(
104 4.5,
105 );
106 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeLessThan(
107 contrastRatio(theme.colors.text, theme.colors.canvas),
108 );
109 });
110
111 it("moves reserved VS Code names to a non-reserved id", () => {
112 const darkPlus = parseVsCodeThemeFile({
113 name: "Dark+",
114 type: "dark",
115 colors: { "editor.background": "#1e1e1e" },
116 });
117 expect(darkPlus.label).toBe("Dark+");
118 expect(darkPlus.id).not.toBe("dark");
119 expect(darkPlus.id).toBe("dark-vscode");
120 });
121
122 it("keeps Dark+ and Light+ as separate themes instead of pairing them as +", () => {
123 const themes = pairVsCodeThemes([
124 parseVsCodeThemeFile({
125 name: "Light+",
126 type: "light",
127 colors: { "editor.background": "#ffffff" },
128 }),
129 parseVsCodeThemeFile({
130 name: "Dark+",
131 type: "dark",
132 colors: { "editor.background": "#1e1e1e" },
133 }),
134 ]);
135 expect(themes.map((theme) => [theme.id, theme.label])).toEqual([
136 ["light-vscode", "Light+"],
137 ["dark-vscode", "Dark+"],
138 ]);
139 });
140
141 it("prefers a visible input background over a transparent border", () => {
142 const catppuccin = parseVsCodeThemeFile({
143 name: "Catppuccin Mocha",
144 type: "dark",
145 colors: {
146 "editor.background": "#1e1e2e",
147 "input.border": "#00000000",
148 "input.background": "#313244",
149 },
150 });
151 expect(asHex(catppuccin.colors.input)).toBe("#313244");
152 });
153
154 it("keeps unchecked inputs distinct from the checked action color", () => {
155 const gruvbox = parseVsCodeThemeFile({
156 name: "Gruvbox Light Soft",
157 type: "light",
158 colors: {
159 "editor.background": "#f2e5bc",
160 "button.background": "#45858880",
161 "input.background": "#45858880",
162 "input.border": "#928374",
163 },
164 });
165 expect(
166 contrastRatio(gruvbox.colors.input, gruvbox.colors.messageAction),
167 ).toBeGreaterThanOrEqual(1.1);
168 });
169
170 it("keeps the derived input distinct when a button reuses it", () => {
171 const theme = parseVsCodeThemeFile({
172 name: "Derived input collision",
173 type: "dark",
174 colors: {
175 "editor.background": "#1e1e2e",
176 focusBorder: "#89b4fa",
177 "button.background": "#525661",
178 },
179 });
180
181 expect(asHex(theme.colors.messageAction)).toBe("#525661");
182 expect(asHex(theme.colors.input)).not.toBe("#525661");
183 expect(contrastRatio(theme.colors.input, theme.colors.canvas)).toBeGreaterThanOrEqual(1.1);
184 expect(contrastRatio(theme.colors.input, theme.colors.messageAction)).toBeGreaterThanOrEqual(
185 1.1,
186 );
187 });
188
189 it("skips a transparent focus border for a visible accent key", () => {
190 const vitesse = parseVsCodeThemeFile({
191 name: "Vitesse Dark",
192 type: "dark",
193 colors: {
194 "editor.background": "#121212",
195 focusBorder: "#00000000",
196 "button.background": "#4d9375",
197 },
198 });
199 expect(asHex(vitesse.colors.accent)).toBe("#4d9375");
200 expect(asHex(vitesse.colors.focus)).toBe("#4d9375");
201 expect(contrastRatio(vitesse.colors.focus, vitesse.colors.canvas)).toBeGreaterThanOrEqual(1.1);
202 });
203
204 it("keeps focus visible against an explicit raised surface", () => {
205 const theme = parseVsCodeThemeFile({
206 name: "Raised focus",
207 type: "dark",
208 colors: {
209 "editor.background": "#000000",
210 focusBorder: "#111111",
211 "editorWidget.background": "#111111",
212 "button.background": "#4d9375",
213 },
214 });
215 expect(contrastRatio(theme.colors.focus, theme.colors.surfaceRaised)).toBeGreaterThanOrEqual(
216 1.1,
217 );
218 expect(asHex(theme.colors.focus)).toBe("#4d9375");
219 });
220
221 it("keeps the fallback focus visible against an explicit raised surface", () => {
222 const theme = parseVsCodeThemeFile({
223 name: "Raised fallback focus",
224 type: "dark",
225 colors: {
226 "editor.background": "#121212",
227 "editorWidget.background": "#346bf1",
228 },
229 });
230 expect(asHex(theme.colors.focus)).toBe("#ffffff");
231 expect(contrastRatio(theme.colors.focus, theme.colors.canvas)).toBeGreaterThanOrEqual(1.1);
232 expect(contrastRatio(theme.colors.focus, theme.colors.surfaceRaised)).toBeGreaterThanOrEqual(
233 1.1,
234 );
235 });
236
237 it("skips a transparent button background for the action color", () => {
238 const theme = parseVsCodeThemeFile({
239 name: "Transparent button",
240 type: "dark",
241 colors: {
242 "editor.background": "#121212",
243 focusBorder: "#4d9375",
244 "button.background": "#00000000",
245 },
246 });
247 expect(asHex(theme.colors.messageAction)).toBe("#4d9375");
248 });
249
250 it("skips a button background that matches the raised surface", () => {
251 const theme = parseVsCodeThemeFile({
252 name: "Raised button",
253 type: "dark",
254 colors: {
255 "editor.background": "#121212",
256 focusBorder: "#4d9375",
257 "editorWidget.background": "#2a2d3a",
258 "button.background": "#2a2d3a",
259 },
260 });
261 expect(asHex(theme.colors.messageAction)).toBe("#4d9375");
262 });
263
264 it("uses a visible default accent when the file has no usable accent key", () => {
265 const theme = parseVsCodeThemeFile({
266 name: "No accent",
267 type: "dark",
268 colors: { "editor.background": "#121212", focusBorder: "#121212" },
269 });
270 expect(contrastRatio(theme.colors.focus, theme.colors.canvas)).toBeGreaterThanOrEqual(1.1);
271 });
272
273 it("validates placeholders against the resolved raised surface", () => {
274 const lightPlus = parseVsCodeThemeFile({
275 name: "Light Plus Shape",
276 type: "light",
277 colors: {
278 "editor.background": "#eaeff3",
279 "editor.foreground": "#1f1f1f",
280 "editorWidget.background": "#ffffff",
281 "input.placeholderForeground": "#767676",
282 },
283 });
284 expect(
285 contrastRatio(lightPlus.colors.placeholder, lightPlus.colors.surfaceRaised),
286 ).toBeGreaterThanOrEqual(4.5);
287 expect(
288 contrastRatio(lightPlus.colors.placeholder, lightPlus.colors.surfaceRaised),
289 ).toBeLessThan(contrastRatio(lightPlus.colors.text, lightPlus.colors.surfaceRaised));
290 });
291 it("imports current and other find matches as independent background/text pairs", () => {
292 const { colors } = parseVsCodeThemeFile({
293 type: "dark",
294 colors: {
295 "editor.background": "#202020",
296 "editor.findMatchBackground": "#663399",
297 "editor.findMatchForeground": "#ffffff",
298 "editor.findMatchHighlightBackground": "#bce4ff",
299 "editor.findMatchHighlightForeground": "#17314a",
300 },
301 });
302
303 expect(asHex(colors.searchMatchActiveBackground)).toBe("#663399");
304 expect(asHex(colors.searchMatchActiveForeground)).toBe("#ffffff");
305 expect(asHex(colors.searchMatchBackground)).toBe("#bce4ff");
306 expect(asHex(colors.searchMatchForeground)).toBe("#17314a");
307 });
308
309 it("composites find backgrounds onto the canvas and foregrounds onto their match", () => {
310 const { colors } = parseVsCodeThemeFile({
311 type: "dark",
312 colors: {
313 "editor.background": "#202020",
314 "editor.findMatchBackground": "#33669980",
315 "editor.findMatchForeground": "#ffffffcc",
316 "editor.findMatchHighlightBackground": "#ffcc0040",
317 "editor.findMatchHighlightForeground": "#ffffff",
318 },
319 });
320
321 expect(asHex(colors.searchMatchActiveBackground)).toBe("#2a435d");
322 expect(asHex(colors.searchMatchActiveForeground)).toBe("#d4d9df");
323 expect(asHex(colors.searchMatchBackground)).toBe("#584b18");
324 expect(asHex(colors.searchMatchForeground)).toBe("#ffffff");
325 });
326
327 it("uses editor text when no find foreground is specified", () => {
328 const { colors } = parseVsCodeThemeFile({
329 type: "dark",
330 colors: {
331 "editor.background": "#101010",
332 "editor.foreground": "#eeeeee",
333 "editor.findMatchBackground": "#304050",
334 "editor.findMatchHighlightBackground": "#403020",
335 },
336 });
337
338 expect(asHex(colors.searchMatchActiveForeground)).toBe("#eeeeee");
339 expect(asHex(colors.searchMatchForeground)).toBe("#eeeeee");
340 });
341
342 it.each(["light", "dark"])(
343 "keeps missing or invalid find colours readable in %s themes",
344 (type) => {
345 const base = {
346 "editor.background": type === "dark" ? "#101010" : "#fafafa",
347 };
348 const missing = parseVsCodeThemeFile({ type, colors: base }).colors;
349 const invalid = parseVsCodeThemeFile({
350 type,
351 colors: {
352 ...base,
353 "editor.findMatchBackground": "not-a-colour",
354 "editor.findMatchForeground": 42,
355 "editor.findMatchHighlightBackground": null,
356 "editor.findMatchHighlightForeground": "invalid",
357 },
358 }).colors;
359 const unreadable = parseVsCodeThemeFile({
360 type,
361 colors: {
362 ...base,
363 "editor.findMatchBackground": "#ffffcc",
364 "editor.findMatchForeground": "#ffffff",
365 "editor.findMatchHighlightBackground": "#101020",
366 "editor.findMatchHighlightForeground": "#111111",
367 },
368 }).colors;
369
370 for (const colors of [missing, invalid, unreadable]) {
371 expect(
372 contrastRatio(colors.searchMatchForeground, colors.searchMatchBackground),
373 ).toBeGreaterThanOrEqual(4.5);
374 expect(
375 contrastRatio(colors.searchMatchActiveForeground, colors.searchMatchActiveBackground),
376 ).toBeGreaterThanOrEqual(4.5);
377 }
378 expect(invalid.searchMatchBackground).toBe(missing.searchMatchBackground);
379 expect(invalid.searchMatchActiveBackground).toBe(missing.searchMatchActiveBackground);
380 expect(missing.searchMatchBackground).not.toBe(missing.searchMatchActiveBackground);
381 expect(missing.searchMatchBackground).not.toBe(missing.canvas);
382 expect(missing.searchMatchActiveBackground).not.toBe(missing.canvas);
383 },
384 );
385
386 it("fills every role the file omits with a readable derived value", () => {
387 const theme = parseVsCodeThemeFile(VSCODE_DARK);
388 const colors = getThemeColorsForMode(theme, "dark")!;
389 for (const value of Object.values(colors)) {
390 expect(value).toMatch(/^oklch\(/);
391 }
392 expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(4.5);
393 expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5);
394 expect(
395 contrastRatio(colors.messageActionForeground, colors.messageAction),
396 ).toBeGreaterThanOrEqual(4.5);
397 });
398
399 it("falls back to the editor background when the type is missing or odd", () => {
400 const untyped = parseVsCodeThemeFile({
401 name: "Untyped",
402 colors: { "editor.background": "#fdfdfd", "editor.foreground": "#202020" },
403 });
404 expect(untyped.appearance).toBe("light");
405 const hc = parseVsCodeThemeFile({
406 name: "High contrast",
407 type: "hc-light",
408 colors: { "editor.background": "#ffffff" },
409 });
410 expect(hc.appearance).toBe("light");
411 });
412
413 it("keeps an unreadable foreground out of the palette", () => {
414 const theme = parseVsCodeThemeFile({
415 name: "Unreadable",
416 type: "dark",
417 colors: { "editor.background": "#101010", "editor.foreground": "#111111" },
418 });
419 expect(asHex(theme.colors.text)).not.toBe("#111111");
420 expect(contrastRatio(theme.colors.text, theme.colors.canvas)).toBeGreaterThanOrEqual(4.5);
421 });
422
423 it("stays readable when the file replaces a surface but not its foreground", () => {
424 const theme = parseVsCodeThemeFile({
425 name: "Split",
426 type: "dark",
427 colors: {
428 "editor.background": "#101010",
429 "editor.foreground": "#f5f5f5",
430 "sideBar.background": "#fafafa",
431 "terminal.background": "#fbfbfb",
432 },
433 });
434 expect(asHex(theme.colors.sidebar)).toBe("#fafafa");
435 expect(
436 contrastRatio(theme.colors.sidebarForeground, theme.colors.sidebar),
437 ).toBeGreaterThanOrEqual(4.5);
438 expect(
439 contrastRatio(theme.colors.terminalForeground, theme.colors.terminalBackground),
440 ).toBeGreaterThanOrEqual(4.5);
441 });
442
443 it("reads wide-gamut color() notation", () => {
444 const theme = parseVsCodeThemeFile({
445 name: "Vibrant",
446 type: "dark",
447 colors: {
448 "editor.background": "color(display-p3 0.039216 0.039216 0.039216)",
449 "editor.foreground": "color(display-p3 0.980392 0.980392 0.980392)",
450 focusBorder: "color(display-p3 0.308664 0.645271 1.000000)",
451 "editor.selectionBackground": "color(display-p3 0.308664 0.645271 1.000000 / 0.300000)",
452 },
453 });
454 expect(asHex(theme.colors.canvas)).toMatch(/^#0[89ab]/);
455 expect(asHex(theme.colors.text)).toMatch(/^#f[a-f0-9]/);
456 const accent = asHex(theme.colors.accent);
457 const [red, green, blue] = [1, 3, 5].map((index) =>
458 Number.parseInt(accent.slice(index, index + 2), 16),
459 ) as [number, number, number];
460 expect(blue).toBeGreaterThan(200);
461 expect(blue).toBeGreaterThan(red);
462 expect(green).toBeGreaterThan(red);
463 });
464
465 it("pairs light and dark files from one family into dual-mode themes", () => {
466 const make = (name: string, type: "light" | "dark") =>
467 parseVsCodeThemeFile({
468 name,
469 type,
470 colors: {
471 "editor.background": type === "dark" ? "#101014" : "#fdfdfd",
472 "editor.foreground": type === "dark" ? "#e6e6e6" : "#1f1f1f",
473 focusBorder: "#69b1ff",
474 },
475 });
476 const themes = pairVsCodeThemes([
477 make("github-dark", "dark"),
478 make("github-light", "light"),
479 make("github-dark-colorblind", "dark"),
480 make("github-light-colorblind", "light"),
481 make("github-dark-dimmed", "dark"),
482 ]);
483
484 const labels = themes.map((theme) => theme.label);
485 expect(labels).toEqual(["Github", "Github Colorblind", "Github Dark Dimmed"]);
486 const github = themes[0]!;
487 expect(github.appearance).toBe("light");
488 expect(getThemeColorsForMode(github, "dark")).not.toBeNull();
489 expect(asHex(getThemeColorsForMode(github, "dark")!.canvas)).toBe("#101014");
490 expect(asHex(github.colors.canvas)).toBe("#fdfdfd");
491 expect(getThemeColorsForMode(themes[2]!, "light")).toBeNull();
492 });
493
494 it("does not guess when a family is ambiguous", () => {
495 const make = (name: string, type: "light" | "dark") =>
496 parseVsCodeThemeFile({
497 name,
498 type,
499 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
500 });
501 const themes = pairVsCodeThemes([
502 make("solar-dark", "dark"),
503 make("solar-dark-soft", "dark"),
504 make("solar-light", "light"),
505 ]);
506 expect(themes.map((theme) => theme.label).sort()).toEqual(["Solar", "Solar Dark Soft"]);
507 });
508
509 it("keeps derived surfaces neutral instead of washing them with the accent", () => {
510 const theme = parseVsCodeThemeFile(VSCODE_DARK);
511 const spread = (value: string) => {
512 const hex = asHex(value);
513 const channels = [1, 3, 5].map((index) => Number.parseInt(hex.slice(index, index + 2), 16));
514 return Math.max(...channels) - Math.min(...channels);
515 };
516 expect(spread(theme.colors.codeBackground)).toBeLessThanOrEqual(8);
517 expect(spread(theme.colors.surface)).toBeLessThanOrEqual(8);
518 expect(spread(theme.colors.text)).toBeLessThanOrEqual(12);
519 expect(asHex(theme.colors.accent)).toBe("#69b1ff");
520 });
521
522 it("tells same-named variants apart by their file names", () => {
523 // Dracula ships dracula.json and dracula-soft.json that both say
524 // "Dracula" inside.
525 const dracula = (bg: string) =>
526 parseVsCodeThemeFile({
527 name: "Dracula",
528 type: "dark",
529 colors: { "editor.background": bg, "editor.foreground": "#f8f8f2" },
530 });
531 const themes = resolveThemeLabelCollisions([
532 { theme: dracula("#282a36"), sourceName: "dracula.json" },
533 { theme: dracula("#22232e"), sourceName: "dracula-soft.json" },
534 ]);
535 expect(themes.map((theme) => theme.label)).toEqual(["Dracula", "Dracula Soft"]);
536 expect(themes.map((theme) => theme.id)).toEqual(["dracula", "dracula-soft"]);
537 const numbered = resolveThemeLabelCollisions([
538 { theme: dracula("#282a36") },
539 { theme: dracula("#22232e") },
540 ]);
541 expect(numbered.map((theme) => theme.label)).toEqual(["Dracula", "Dracula 2"]);
542 });
543
544 it("falls back to the name when the displayName humanizes to nothing", () => {
545 const theme = parseVsCodeThemeFile({
546 displayName: "---",
547 name: "night-owl",
548 type: "dark",
549 colors: { "editor.background": "#011627" },
550 });
551 expect(theme.label).toBe("Night Owl");
552 });
553
554 it("keeps a pair whose stripped name is reserved as two single themes", () => {
555 const make = (name: string, type: "light" | "dark") =>
556 parseVsCodeThemeFile({
557 name,
558 type,
559 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
560 });
561 const themes = pairVsCodeThemes([make("grove-light", "light"), make("grove-dark", "dark")]);
562 expect(themes.map((theme) => theme.label).sort()).toEqual(["Grove Dark", "Grove Light"]);
563 });
564
565 it("explains a file with no editor background", () => {
566 expect(() => parseVsCodeThemeFile({ name: "Empty", type: "dark", colors: {} })).toThrow(
567 /editor\.background/,
568 );
569 });
570});