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

vscodeThemeImport.test.ts

311 lines12.0 KB

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;
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);
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("fills every role the file omits with a readable derived value", () => {
116 const theme = parseVsCodeThemeFile(VSCODE_DARK);
117 const colors = getThemeColorsForMode(theme, "dark")!;
118 for (const value of Object.values(colors)) {
119 expect(value).toMatch(/^oklch\(/);
120 }
121 expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(4.5);
122 expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5);
123 expect(
124 contrastRatio(colors.messageActionForeground, colors.messageAction),
125 ).toBeGreaterThanOrEqual(4.5);
126 });
127​
128 it("falls back to the editor background when the type is missing or odd", () => {
129 const untyped = parseVsCodeThemeFile({
130 name: "Untyped",
131 colors: { "editor.background": "#fdfdfd", "editor.foreground": "#202020" },
132 });
133 expect(untyped.appearance).toBe("light");
134 const hc = parseVsCodeThemeFile({
135 name: "High contrast",
136 type: "hc-light",
137 colors: { "editor.background": "#ffffff" },
138 });
139 expect(hc.appearance).toBe("light");
140 });
141​
142 it("keeps an unreadable foreground out of the palette", () => {
143 const theme = parseVsCodeThemeFile({
144 name: "Unreadable",
145 type: "dark",
146 colors: { "editor.background": "#101010", "editor.foreground": "#111111" },
147 });
148 expect(asHex(theme.colors.text)).not.toBe("#111111");
149 expect(contrastRatio(theme.colors.text, theme.colors.canvas)).toBeGreaterThanOrEqual(4.5);
150 });
151​
152 it("stays readable when the file replaces a surface but not its foreground", () => {
153 // A dark theme with a light sidebar: the derived sidebar foreground was
154 // solved for a dark surface and would vanish on this one.
155 const theme = parseVsCodeThemeFile({
156 name: "Split",
157 type: "dark",
158 colors: {
159 "editor.background": "#101010",
160 "editor.foreground": "#f5f5f5",
161 "sideBar.background": "#fafafa",
162 "terminal.background": "#fbfbfb",
163 },
164 });
165 expect(asHex(theme.colors.sidebar)).toBe("#fafafa");
166 expect(
167 contrastRatio(theme.colors.sidebarForeground, theme.colors.sidebar),
168 ).toBeGreaterThanOrEqual(4.5);
169 expect(
170 contrastRatio(theme.colors.terminalForeground, theme.colors.terminalBackground),
171 ).toBeGreaterThanOrEqual(4.5);
172 });
173​
174 it("reads wide-gamut color() notation", () => {
175 // Themes authored for P3 displays (the shipped Pierre "vibrant" pair) use
176 // this instead of hex.
177 const theme = parseVsCodeThemeFile({
178 name: "Vibrant",
179 type: "dark",
180 colors: {
181 "editor.background": "color(display-p3 0.039216 0.039216 0.039216)",
182 "editor.foreground": "color(display-p3 0.980392 0.980392 0.980392)",
183 focusBorder: "color(display-p3 0.308664 0.645271 1.000000)",
184 "editor.selectionBackground": "color(display-p3 0.308664 0.645271 1.000000 / 0.300000)",
185 },
186 });
187 expect(asHex(theme.colors.canvas)).toMatch(/^#0[89ab]/);
188 expect(asHex(theme.colors.text)).toMatch(/^#f[a-f0-9]/);
189 // The P3 blue lands in sRGB blue, not black or a clipped grey.
190 const accent = asHex(theme.colors.accent);
191 const [red, green, blue] = [1, 3, 5].map((index) =>
192 Number.parseInt(accent.slice(index, index + 2), 16),
193 ) as [number, number, number];
194 expect(blue).toBeGreaterThan(200);
195 expect(blue).toBeGreaterThan(red);
196 expect(green).toBeGreaterThan(red);
197 });
198​
199 it("pairs light and dark files from one family into dual-mode themes", () => {
200 const make = (name: string, type: "light" | "dark") =>
201 parseVsCodeThemeFile({
202 name,
203 type,
204 colors: {
205 "editor.background": type === "dark" ? "#101014" : "#fdfdfd",
206 "editor.foreground": type === "dark" ? "#e6e6e6" : "#1f1f1f",
207 focusBorder: "#69b1ff",
208 },
209 });
210 const themes = pairVsCodeThemes([
211 make("github-dark", "dark"),
212 make("github-light", "light"),
213 make("github-dark-colorblind", "dark"),
214 make("github-light-colorblind", "light"),
215 make("github-dark-dimmed", "dark"),
216 ]);
217​
218 const labels = themes.map((theme) => theme.label);
219 expect(labels).toEqual(["Github", "Github Colorblind", "Github Dark Dimmed"]);
220 const github = themes[0]!;
221 expect(github.appearance).toBe("light");
222 expect(getThemeColorsForMode(github, "dark")).not.toBeNull();
223 expect(asHex(getThemeColorsForMode(github, "dark")!.canvas)).toBe("#101014");
224 expect(asHex(github.colors.canvas)).toBe("#fdfdfd");
225 // The unpaired dimmed variant stays a single dark theme.
226 expect(getThemeColorsForMode(themes[2]!, "light")).toBeNull();
227 });
228​
229 it("does not guess when a family is ambiguous", () => {
230 const make = (name: string, type: "light" | "dark") =>
231 parseVsCodeThemeFile({
232 name,
233 type,
234 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
235 });
236 const themes = pairVsCodeThemes([
237 make("solar-dark", "dark"),
238 make("solar-dark-soft", "dark"),
239 make("solar-light", "light"),
240 ]);
241 // "solar-dark-soft" groups under its own key with no light partner, so
242 // it keeps its full name; the remaining pair merges.
243 expect(themes.map((theme) => theme.label).sort()).toEqual(["Solar", "Solar Dark Soft"]);
244 });
245​
246 it("keeps derived surfaces neutral instead of washing them with the accent", () => {
247 // A gray theme with a blue focusBorder: roles the file omits (code
248 // surface, plain surfaces) must stay near the canvas, not turn blue.
249 const theme = parseVsCodeThemeFile(VSCODE_DARK);
250 const spread = (value: string) => {
251 const hex = asHex(value);
252 const channels = [1, 3, 5].map((index) => Number.parseInt(hex.slice(index, index + 2), 16));
253 return Math.max(...channels) - Math.min(...channels);
254 };
255 expect(spread(theme.colors.codeBackground)).toBeLessThanOrEqual(8);
256 expect(spread(theme.colors.surface)).toBeLessThanOrEqual(8);
257 expect(spread(theme.colors.text)).toBeLessThanOrEqual(12);
258 // The accent itself keeps the file's color.
259 expect(asHex(theme.colors.accent)).toBe("#69b1ff");
260 });
261​
262 it("tells same-named variants apart by their file names", () => {
263 // Dracula ships dracula.json and dracula-soft.json that both say
264 // "Dracula" inside.
265 const dracula = (bg: string) =>
266 parseVsCodeThemeFile({
267 name: "Dracula",
268 type: "dark",
269 colors: { "editor.background": bg, "editor.foreground": "#f8f8f2" },
270 });
271 const themes = resolveThemeLabelCollisions([
272 { theme: dracula("#282a36"), sourceName: "dracula.json" },
273 { theme: dracula("#22232e"), sourceName: "dracula-soft.json" },
274 ]);
275 expect(themes.map((theme) => theme.label)).toEqual(["Dracula", "Dracula Soft"]);
276 expect(themes.map((theme) => theme.id)).toEqual(["dracula", "dracula-soft"]);
277 // Without file names the second falls back to numbering.
278 const numbered = resolveThemeLabelCollisions([
279 { theme: dracula("#282a36") },
280 { theme: dracula("#22232e") },
281 ]);
282 expect(numbered.map((theme) => theme.label)).toEqual(["Dracula", "Dracula 2"]);
283 });
284​
285 it("falls back to the name when the displayName humanizes to nothing", () => {
286 const theme = parseVsCodeThemeFile({
287 displayName: "---",
288 name: "night-owl",
289 type: "dark",
290 colors: { "editor.background": "#011627" },
291 });
292 expect(theme.label).toBe("Night Owl");
293 });
294​
295 it("keeps a pair whose stripped name is reserved as two single themes", () => {
296 const make = (name: string, type: "light" | "dark") =>
297 parseVsCodeThemeFile({
298 name,
299 type,
300 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
301 });
302 const themes = pairVsCodeThemes([make("grove-light", "light"), make("grove-dark", "dark")]);
303 expect(themes.map((theme) => theme.label).sort()).toEqual(["Grove Dark", "Grove Light"]);
304 });
305​
306 it("explains a file with no editor background", () => {
307 expect(() => parseVsCodeThemeFile({ name: "Empty", type: "dark", colors: {} })).toThrow(
308 /editor\.background/,
309 );
310 });
311});