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

vscodeThemeImport.test.ts

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