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

vscodeThemeImport.test.ts

282 lines11.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { getThemeColorsForMode, THEME_FILE_VERSION } from "./themePalette";
4import {
5 isVsCodeThemeFile,
6 pairVsCodeThemes,
7 parseVsCodeThemeFile,
8 resolveThemeLabelCollisions,
9} from "./vscodeThemeImport";
10​
11function contrastRatio(first: string, second: string): number {
12 const toChannels = (value: string) => {
13 const hex = value.slice(1);
14 return [0, 1, 2].map(
15 (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255,
16 );
17 };
18 const luminance = (value: string) =>
19 toChannels(value)
20 .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4))
21 .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0);
22 const lighter = Math.max(luminance(first), luminance(second));
23 const darker = Math.min(luminance(first), luminance(second));
24 return (lighter + 0.05) / (darker + 0.05);
26​
27// Shaped like a real workbench theme: dotted keys, alpha overlays, and a lot
28// of roles simply left out.
29const VSCODE_DARK = {
30 name: "pierre-dark-soft",
31 type: "dark",
32 colors: {
33 "editor.background": "#171717",
34 "editor.foreground": "#d4d4d4",
35 foreground: "#d4d4d4",
36 "sideBar.background": "#101010",
37 "sideBar.foreground": "#8a8a8a",
38 "sideBar.border": "#1d1d1d",
39 focusBorder: "#69b1ff",
40 "button.background": "#69b1ff",
41 "button.foreground": "#171717",
42 "input.border": "#2c2c2c",
43 "input.placeholderForeground": "#525252",
44 "terminal.background": "#101010",
45 "terminal.foreground": "#8a8a8a",
46 "list.hoverBackground": "#1f3e5e59",
47 "list.activeSelectionBackground": "#1f3e5e99",
48 },
49 tokenColors: [],
50};
51​
52describe("VS Code theme import", () => {
53 it("recognises workbench themes and rejects our own files", () => {
54 expect(isVsCodeThemeFile(VSCODE_DARK)).toBe(true);
55 expect(isVsCodeThemeFile({ type: "dark", tokenColors: [] })).toBe(true);
56 expect(
57 isVsCodeThemeFile({
58 version: THEME_FILE_VERSION,
59 name: "Aurora",
60 appearance: "light",
61 colors: { canvas: "#ffffff" },
62 }),
63 ).toBe(false);
64 expect(isVsCodeThemeFile("nope")).toBe(false);
65 });
66​
67 it("carries the editor surfaces and accent across", () => {
68 const theme = parseVsCodeThemeFile(VSCODE_DARK);
69 // The slug name is read as words; a displayName would win verbatim.
70 expect(theme.label).toBe("Pierre Dark Soft");
71 expect(theme.appearance).toBe("dark");
72 expect(theme.colors.canvas).toBe("#171717");
73 expect(theme.colors.text).toBe("#d4d4d4");
74 expect(theme.colors.accent).toBe("#69b1ff");
75 expect(theme.colors.sidebar).toBe("#101010");
76 expect(theme.colors.terminalBackground).toBe("#101010");
77 });
78​
79 it("flattens alpha overlays onto the surface they sit on", () => {
80 const theme = parseVsCodeThemeFile(VSCODE_DARK);
81 // #1f3e5e59 over the #101010 sidebar, not left semi-transparent.
82 expect(theme.colors.sidebarRowHover).toMatch(/^#[0-9a-f]{6}$/);
83 expect(theme.colors.sidebarRowHover).not.toBe("#1f3e5e59");
84 expect(theme.colors.sidebarRowSelected).not.toBe(theme.colors.sidebar);
85 });
86​
87 it("fills every role the file omits with a readable derived value", () => {
88 const theme = parseVsCodeThemeFile(VSCODE_DARK);
89 const colors = getThemeColorsForMode(theme, "dark")!;
90 for (const value of Object.values(colors)) {
91 expect(value).toMatch(/^#[0-9a-f]{3,8}$/i);
92 }
93 expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(4.5);
94 expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5);
95 expect(
96 contrastRatio(colors.messageActionForeground, colors.messageAction),
97 ).toBeGreaterThanOrEqual(4.5);
98 });
99​
100 it("falls back to the editor background when the type is missing or odd", () => {
101 const untyped = parseVsCodeThemeFile({
102 name: "Untyped",
103 colors: { "editor.background": "#fdfdfd", "editor.foreground": "#202020" },
104 });
105 expect(untyped.appearance).toBe("light");
106 const hc = parseVsCodeThemeFile({
107 name: "High contrast",
108 type: "hc-light",
109 colors: { "editor.background": "#ffffff" },
110 });
111 expect(hc.appearance).toBe("light");
112 });
113​
114 it("keeps an unreadable foreground out of the palette", () => {
115 const theme = parseVsCodeThemeFile({
116 name: "Unreadable",
117 type: "dark",
118 colors: { "editor.background": "#101010", "editor.foreground": "#111111" },
119 });
120 expect(theme.colors.text).not.toBe("#111111");
121 expect(contrastRatio(theme.colors.text, theme.colors.canvas)).toBeGreaterThanOrEqual(4.5);
122 });
123​
124 it("stays readable when the file replaces a surface but not its foreground", () => {
125 // A dark theme with a light sidebar: the derived sidebar foreground was
126 // solved for a dark surface and would vanish on this one.
127 const theme = parseVsCodeThemeFile({
128 name: "Split",
129 type: "dark",
130 colors: {
131 "editor.background": "#101010",
132 "editor.foreground": "#f5f5f5",
133 "sideBar.background": "#fafafa",
134 "terminal.background": "#fbfbfb",
135 },
136 });
137 expect(theme.colors.sidebar).toBe("#fafafa");
138 expect(
139 contrastRatio(theme.colors.sidebarForeground, theme.colors.sidebar),
140 ).toBeGreaterThanOrEqual(4.5);
141 expect(
142 contrastRatio(theme.colors.terminalForeground, theme.colors.terminalBackground),
143 ).toBeGreaterThanOrEqual(4.5);
144 });
145​
146 it("reads wide-gamut color() notation", () => {
147 // Themes authored for P3 displays (the shipped Pierre "vibrant" pair) use
148 // this instead of hex.
149 const theme = parseVsCodeThemeFile({
150 name: "Vibrant",
151 type: "dark",
152 colors: {
153 "editor.background": "color(display-p3 0.039216 0.039216 0.039216)",
154 "editor.foreground": "color(display-p3 0.980392 0.980392 0.980392)",
155 focusBorder: "color(display-p3 0.308664 0.645271 1.000000)",
156 "editor.selectionBackground": "color(display-p3 0.308664 0.645271 1.000000 / 0.300000)",
157 },
158 });
159 expect(theme.colors.canvas).toMatch(/^#0[89ab]/);
160 expect(theme.colors.text).toMatch(/^#f[a-f0-9]/);
161 // The P3 blue lands in sRGB blue, not black or a clipped grey.
162 const accent = theme.colors.accent;
163 const [red, green, blue] = [1, 3, 5].map((index) =>
164 Number.parseInt(accent.slice(index, index + 2), 16),
165 ) as [number, number, number];
166 expect(blue).toBeGreaterThan(200);
167 expect(blue).toBeGreaterThan(red);
168 expect(green).toBeGreaterThan(red);
169 });
170​
171 it("pairs light and dark files from one family into dual-mode themes", () => {
172 const make = (name: string, type: "light" | "dark") =>
173 parseVsCodeThemeFile({
174 name,
175 type,
176 colors: {
177 "editor.background": type === "dark" ? "#101014" : "#fdfdfd",
178 "editor.foreground": type === "dark" ? "#e6e6e6" : "#1f1f1f",
179 focusBorder: "#69b1ff",
180 },
181 });
182 const themes = pairVsCodeThemes([
183 make("github-dark", "dark"),
184 make("github-light", "light"),
185 make("github-dark-colorblind", "dark"),
186 make("github-light-colorblind", "light"),
187 make("github-dark-dimmed", "dark"),
188 ]);
189​
190 const labels = themes.map((theme) => theme.label);
191 expect(labels).toEqual(["Github", "Github Colorblind", "Github Dark Dimmed"]);
192 const github = themes[0]!;
193 expect(github.appearance).toBe("light");
194 expect(getThemeColorsForMode(github, "dark")).not.toBeNull();
195 expect(getThemeColorsForMode(github, "dark")!.canvas).toBe("#101014");
196 expect(github.colors.canvas).toBe("#fdfdfd");
197 // The unpaired dimmed variant stays a single dark theme.
198 expect(getThemeColorsForMode(themes[2]!, "light")).toBeNull();
199 });
200​
201 it("does not guess when a family is ambiguous", () => {
202 const make = (name: string, type: "light" | "dark") =>
203 parseVsCodeThemeFile({
204 name,
205 type,
206 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
207 });
208 const themes = pairVsCodeThemes([
209 make("solar-dark", "dark"),
210 make("solar-dark-soft", "dark"),
211 make("solar-light", "light"),
212 ]);
213 // "solar-dark-soft" groups under its own key with no light partner, so
214 // it keeps its full name; the remaining pair merges.
215 expect(themes.map((theme) => theme.label).sort()).toEqual(["Solar", "Solar Dark Soft"]);
216 });
217​
218 it("keeps derived surfaces neutral instead of washing them with the accent", () => {
219 // A gray theme with a blue focusBorder: roles the file omits (code
220 // surface, plain surfaces) must stay near the canvas, not turn blue.
221 const theme = parseVsCodeThemeFile(VSCODE_DARK);
222 const spread = (value: string) => {
223 const channels = [1, 3, 5].map((index) => Number.parseInt(value.slice(index, index + 2), 16));
224 return Math.max(...channels) - Math.min(...channels);
225 };
226 expect(spread(theme.colors.codeBackground)).toBeLessThanOrEqual(8);
227 expect(spread(theme.colors.surface)).toBeLessThanOrEqual(8);
228 expect(spread(theme.colors.text)).toBeLessThanOrEqual(12);
229 // The accent itself keeps the file's color.
230 expect(theme.colors.accent).toBe("#69b1ff");
231 });
232​
233 it("tells same-named variants apart by their file names", () => {
234 // Dracula ships dracula.json and dracula-soft.json that both say
235 // "Dracula" inside.
236 const dracula = (bg: string) =>
237 parseVsCodeThemeFile({
238 name: "Dracula",
239 type: "dark",
240 colors: { "editor.background": bg, "editor.foreground": "#f8f8f2" },
241 });
242 const themes = resolveThemeLabelCollisions([
243 { theme: dracula("#282a36"), sourceName: "dracula.json" },
244 { theme: dracula("#22232e"), sourceName: "dracula-soft.json" },
245 ]);
246 expect(themes.map((theme) => theme.label)).toEqual(["Dracula", "Dracula Soft"]);
247 expect(themes.map((theme) => theme.id)).toEqual(["dracula", "dracula-soft"]);
248 // Without file names the second falls back to numbering.
249 const numbered = resolveThemeLabelCollisions([
250 { theme: dracula("#282a36") },
251 { theme: dracula("#22232e") },
252 ]);
253 expect(numbered.map((theme) => theme.label)).toEqual(["Dracula", "Dracula 2"]);
254 });
255​
256 it("falls back to the name when the displayName humanizes to nothing", () => {
257 const theme = parseVsCodeThemeFile({
258 displayName: "---",
259 name: "night-owl",
260 type: "dark",
261 colors: { "editor.background": "#011627" },
262 });
263 expect(theme.label).toBe("Night Owl");
264 });
265​
266 it("keeps a pair whose stripped name is reserved as two single themes", () => {
267 const make = (name: string, type: "light" | "dark") =>
268 parseVsCodeThemeFile({
269 name,
270 type,
271 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
272 });
273 const themes = pairVsCodeThemes([make("grove-light", "light"), make("grove-dark", "dark")]);
274 expect(themes.map((theme) => theme.label).sort()).toEqual(["Grove Dark", "Grove Light"]);
275 });
276​
277 it("explains a file with no editor background", () => {
278 expect(() => parseVsCodeThemeFile({ name: "Empty", type: "dark", colors: {} })).toThrow(
279 /editor\.background/,
280 );
281 });
282});