Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 getThemeColorsForMode,
5 parseThemeFile,
6 serializeThemeFile,
7 themeColorToHex,
8 THEME_FILE_VERSION,
9 toCanonicalThemeColor,
10} from "./themePalette";
11import {
12 isVsCodeThemeFile,
13 numberedThemeName,
14 pairVsCodeThemes,
15 parseThemeImport,
16 parseVsCodeThemeFile,
17 resolveThemeLabelCollisions,
18} from "./vscodeThemeImport";
19
20function asHex(value: string): string {
21 const hex = themeColorToHex(value);
22 if (!hex) throw new Error(`Expected a theme color, received ${value}`);
23 return hex;
24}
25
26function contrastRatio(first: string, second: string): number {
27 const toChannels = (value: string) => {
28 const hex = asHex(value).slice(1);
29 return [0, 1, 2].map(
30 (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255,
31 );
32 };
33 const luminance = (value: string) =>
34 toChannels(value)
35 .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4))
36 .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0);
37 const lighter = Math.max(luminance(first), luminance(second));
38 const darker = Math.min(luminance(first), luminance(second));
39 return (lighter + 0.05) / (darker + 0.05);
40}
41
42function oklchParts(value: string): { chroma: number; hue: number } {
43 const [, chroma, hue] = /^oklch\(\S+ (\S+) (\S+)\)$/.exec(value)!;
44 return { chroma: Number(chroma), hue: Number(hue) };
45}
46
47const VSCODE_DARK = {
48 name: "pierre-dark-soft",
49 type: "dark",
50 colors: {
51 "editor.background": "#171717",
52 "editor.foreground": "#d4d4d4",
53 foreground: "#d4d4d4",
54 "sideBar.background": "#101010",
55 "sideBar.foreground": "#8a8a8a",
56 "sideBar.border": "#1d1d1d",
57 focusBorder: "#69b1ff",
58 "button.background": "#69b1ff",
59 "button.foreground": "#171717",
60 "input.border": "#2c2c2c",
61 "input.placeholderForeground": "#525252",
62 "terminal.background": "#101010",
63 "terminal.foreground": "#8a8a8a",
64 "list.hoverBackground": "#1f3e5e59",
65 "list.activeSelectionBackground": "#1f3e5e99",
66 },
67 tokenColors: [],
68};
69
70describe("theme import text", () => {
71 it("preserves conversion of ordinary VS Code JSON", () => {
72 expect(parseThemeImport(JSON.stringify(VSCODE_DARK))).toEqual(
73 parseVsCodeThemeFile(VSCODE_DARK),
74 );
75 });
76
77 it.each([
78 '{ // line comment\n "name": "Test", "colors": { "editor.background": "#002b36" } }',
79 '{ /* block comment */ "name": "Test", "colors": { "editor.background": "#002b36" } }',
80 '{ "name": "Test", "colors": { "editor.background": "#002b36", }, "tokenColors": [ { "scope": ["comment",], "settings": {}, }, ], }',
81 ])("imports VS Code JSONC without changing conversion: %s", (source) => {
82 const theme = parseThemeImport(source);
83 expect(theme.label).toBe("Test");
84 expect(theme.appearance).toBe("dark");
85 expect(asHex(theme.colors.canvas)).toBe("#002b36");
86 });
87
88 it.each([
89 "",
90 '{ "name": "Test", "colors": { "editor.background": "#002b36" }',
91 '{ "name": "Test" "colors": { "editor.background": "#002b36" } }',
92 '{ "name": "Test", "colors": { "editor.background": "#002b36" } } /* unterminated',
93 '{ "name": "Test", "colors": { "editor.background": "#002b36" } } extra',
94 ])("rejects malformed input rather than using parser recovery: %s", (source) => {
95 expect(() => parseThemeImport(source)).toThrow("That theme file is not valid JSON.");
96 });
97
98 it("preserves native T3 theme parsing and strict JSON syntax", () => {
99 const nativeTheme = {
100 version: THEME_FILE_VERSION,
101 name: "Native theme",
102 appearance: "dark",
103 colors: { canvas: "#002b36" },
104 };
105 const source = JSON.stringify(nativeTheme);
106 expect(parseThemeImport(source)).toEqual(parseThemeFile(nativeTheme));
107 expect(() => parseThemeImport(source.replace("{", "{ /* native theme */"))).toThrow();
108 expect(() => parseThemeImport(source.slice(0, -1) + ",}")).toThrow();
109 });
110
111 it("still validates theme structure after parsing JSONC", () => {
112 expect(() =>
113 parseThemeImport('{ /* not a theme */ "colors": { "editor.foreground": "#fff" } }'),
114 ).toThrow('no "editor.background"');
115 expect(() =>
116 parseThemeImport(
117 '{ "version": 1, "name": "Bad native", "appearance": "dark", "colors": { "unknown": "#fff" } }',
118 ),
119 ).toThrow('"unknown" is not a supported theme color role.');
120 });
121});
122
123describe("VS Code theme import", () => {
124 it("recognises workbench themes and rejects our own files", () => {
125 expect(isVsCodeThemeFile(VSCODE_DARK)).toBe(true);
126 expect(isVsCodeThemeFile({ type: "dark", tokenColors: [] })).toBe(true);
127 expect(
128 isVsCodeThemeFile({
129 version: THEME_FILE_VERSION,
130 name: "Aurora",
131 appearance: "light",
132 colors: { canvas: "#ffffff" },
133 }),
134 ).toBe(false);
135 expect(isVsCodeThemeFile("nope")).toBe(false);
136 });
137
138 it("carries the editor surfaces and accent across", () => {
139 const theme = parseVsCodeThemeFile(VSCODE_DARK);
140 expect(theme.label).toBe("Pierre Dark Soft");
141 expect(theme.appearance).toBe("dark");
142 expect(asHex(theme.colors.canvas)).toBe("#171717");
143 expect(asHex(theme.colors.text)).toBe("#d4d4d4");
144 expect(asHex(theme.colors.accent)).toBe("#69b1ff");
145 expect(asHex(theme.colors.sidebar)).toBe("#101010");
146 expect(asHex(theme.colors.terminalBackground)).toBe("#101010");
147 });
148
149 it("keeps a fallback placeholder dimmer than entered text", () => {
150 const theme = parseVsCodeThemeFile({
151 name: "Dark placeholder fallback",
152 type: "dark",
153 colors: {
154 "editor.background": "#1e1e2e",
155 "editor.foreground": "#cdd6f4",
156 "input.placeholderForeground": "#cdd6f473",
157 },
158 });
159
160 expect(
161 contrastRatio(theme.colors.placeholder, theme.colors.surfaceRaised),
162 ).toBeGreaterThanOrEqual(4.5);
163 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeGreaterThanOrEqual(
164 4.5,
165 );
166 expect(contrastRatio(theme.colors.placeholder, theme.colors.canvas)).toBeLessThan(
167 contrastRatio(theme.colors.text, theme.colors.canvas),
168 );
169 });
170
171 it("moves reserved VS Code names to a non-reserved id", () => {
172 const darkPlus = parseVsCodeThemeFile({
173 name: "Dark+",
174 type: "dark",
175 colors: { "editor.background": "#1e1e1e" },
176 });
177 expect(darkPlus.label).toBe("Dark+");
178 expect(darkPlus.id).not.toBe("dark");
179 expect(darkPlus.id).toBe("dark-vscode");
180 });
181
182 it("keeps Dark+ and Light+ as separate themes instead of pairing them as +", () => {
183 const themes = pairVsCodeThemes([
184 parseVsCodeThemeFile({
185 name: "Light+",
186 type: "light",
187 colors: { "editor.background": "#ffffff" },
188 }),
189 parseVsCodeThemeFile({
190 name: "Dark+",
191 type: "dark",
192 colors: { "editor.background": "#1e1e1e" },
193 }),
194 ]);
195 expect(themes.map((theme) => [theme.id, theme.label])).toEqual([
196 ["light-vscode", "Light+"],
197 ["dark-vscode", "Dark+"],
198 ]);
199 });
200
201 it("prefers a visible input background over a transparent border", () => {
202 const catppuccin = parseVsCodeThemeFile({
203 name: "Catppuccin Mocha",
204 type: "dark",
205 colors: {
206 "editor.background": "#1e1e2e",
207 "input.border": "#00000000",
208 "input.background": "#313244",
209 },
210 });
211 expect(asHex(catppuccin.colors.input)).toBe("#313244");
212 });
213
214 it("keeps unchecked inputs distinct from the checked action color", () => {
215 const gruvbox = parseVsCodeThemeFile({
216 name: "Gruvbox Light Soft",
217 type: "light",
218 colors: {
219 "editor.background": "#f2e5bc",
220 "button.background": "#45858880",
221 "input.background": "#45858880",
222 "input.border": "#928374",
223 },
224 });
225 expect(
226 contrastRatio(gruvbox.colors.input, gruvbox.colors.messageAction),
227 ).toBeGreaterThanOrEqual(1.1);
228 });
229
230 it("skips a transparent focus border for a visible accent key", () => {
231 const vitesse = parseVsCodeThemeFile({
232 name: "Vitesse Dark",
233 type: "dark",
234 colors: {
235 "editor.background": "#121212",
236 focusBorder: "#00000000",
237 "button.background": "#4d9375",
238 },
239 });
240 expect(asHex(vitesse.colors.accent)).toBe("#4d9375");
241 expect(asHex(vitesse.colors.focus)).toBe("#4d9375");
242 expect(contrastRatio(vitesse.colors.focus, vitesse.colors.canvas)).toBeGreaterThanOrEqual(1.1);
243 });
244
245 it("keeps focus visible against an explicit raised surface", () => {
246 const theme = parseVsCodeThemeFile({
247 name: "Raised focus",
248 type: "dark",
249 colors: {
250 "editor.background": "#000000",
251 focusBorder: "#111111",
252 "editorWidget.background": "#111111",
253 "button.background": "#4d9375",
254 },
255 });
256 expect(contrastRatio(theme.colors.focus, theme.colors.surfaceRaised)).toBeGreaterThanOrEqual(
257 1.1,
258 );
259 expect(asHex(theme.colors.focus)).toBe("#4d9375");
260 });
261
262 it("skips a transparent button background for the action color", () => {
263 const theme = parseVsCodeThemeFile({
264 name: "Transparent button",
265 type: "dark",
266 colors: {
267 "editor.background": "#121212",
268 focusBorder: "#4d9375",
269 "button.background": "#00000000",
270 },
271 });
272 expect(asHex(theme.colors.messageAction)).toBe("#4d9375");
273 });
274 it("composites find backgrounds onto the canvas and foregrounds onto their match", () => {
275 const { colors } = parseVsCodeThemeFile({
276 type: "dark",
277 colors: {
278 "editor.background": "#202020",
279 "editor.findMatchBackground": "#33669980",
280 "editor.findMatchForeground": "#ffffffcc",
281 "editor.findMatchHighlightBackground": "#ffcc0040",
282 "editor.findMatchHighlightForeground": "#ffffff",
283 },
284 });
285
286 expect(asHex(colors.searchMatchActiveBackground)).toBe("#2a435d");
287 expect(asHex(colors.searchMatchActiveForeground)).toBe("#d4d9df");
288 expect(asHex(colors.searchMatchBackground)).toBe("#584b18");
289 expect(asHex(colors.searchMatchForeground)).toBe("#ffffff");
290 });
291
292 it.each(["light", "dark"])(
293 "keeps missing or invalid find colours readable in %s themes",
294 (type) => {
295 const base = {
296 "editor.background": type === "dark" ? "#101010" : "#fafafa",
297 };
298 const missing = parseVsCodeThemeFile({ type, colors: base }).colors;
299 const invalid = parseVsCodeThemeFile({
300 type,
301 colors: {
302 ...base,
303 "editor.findMatchBackground": "not-a-colour",
304 "editor.findMatchForeground": 42,
305 "editor.findMatchHighlightBackground": null,
306 "editor.findMatchHighlightForeground": "invalid",
307 },
308 }).colors;
309 const unreadable = parseVsCodeThemeFile({
310 type,
311 colors: {
312 ...base,
313 "editor.findMatchBackground": "#ffffcc",
314 "editor.findMatchForeground": "#ffffff",
315 "editor.findMatchHighlightBackground": "#101020",
316 "editor.findMatchHighlightForeground": "#111111",
317 },
318 }).colors;
319
320 for (const colors of [missing, invalid, unreadable]) {
321 expect(
322 contrastRatio(colors.searchMatchForeground, colors.searchMatchBackground),
323 ).toBeGreaterThanOrEqual(4.5);
324 expect(
325 contrastRatio(colors.searchMatchActiveForeground, colors.searchMatchActiveBackground),
326 ).toBeGreaterThanOrEqual(4.5);
327 }
328 expect(invalid.searchMatchBackground).toBe(missing.searchMatchBackground);
329 expect(invalid.searchMatchActiveBackground).toBe(missing.searchMatchActiveBackground);
330 expect(missing.searchMatchBackground).not.toBe(missing.searchMatchActiveBackground);
331 expect(missing.searchMatchBackground).not.toBe(missing.canvas);
332 expect(missing.searchMatchActiveBackground).not.toBe(missing.canvas);
333 },
334 );
335
336 it("fills every role the file omits with a readable derived value", () => {
337 const theme = parseVsCodeThemeFile(VSCODE_DARK);
338 const colors = getThemeColorsForMode(theme, "dark")!;
339 for (const value of Object.values(colors)) {
340 expect(value).toMatch(/^oklch\(/);
341 }
342 expect(contrastRatio(colors.text, colors.canvas)).toBeGreaterThanOrEqual(4.5);
343 expect(contrastRatio(colors.sidebarForeground, colors.sidebar)).toBeGreaterThanOrEqual(4.5);
344 expect(
345 contrastRatio(colors.messageActionForeground, colors.messageAction),
346 ).toBeGreaterThanOrEqual(4.5);
347 });
348
349 it("falls back to the editor background when the type is missing or odd", () => {
350 const untyped = parseVsCodeThemeFile({
351 name: "Untyped",
352 colors: { "editor.background": "#fdfdfd", "editor.foreground": "#202020" },
353 });
354 expect(untyped.appearance).toBe("light");
355 const hc = parseVsCodeThemeFile({
356 name: "High contrast",
357 type: "hc-light",
358 colors: { "editor.background": "#ffffff" },
359 });
360 expect(hc.appearance).toBe("light");
361 });
362
363 it.each([
364 ["dark", "#282828"],
365 ["light", "#ffffff"],
366 ] as const)(
367 "keeps a dim status color's hue on a %s canvas instead of replacing it",
368 (type, canvas) => {
369 const theme = parseVsCodeThemeFile({
370 name: "Gruvbox",
371 type,
372 colors: {
373 "editor.background": canvas,
374 "editorError.foreground": "#fb4934",
375 "editorWarning.foreground": "#b57614",
376 },
377 });
378 for (const [role, source] of [
379 ["error", "#fb4934"],
380 ["warning", "#b57614"],
381 ] as const) {
382 const imported = oklchParts(theme.colors[role]);
383 expect(contrastRatio(theme.colors[role], theme.colors.canvas)).toBeGreaterThanOrEqual(4.5);
384 expect(imported.chroma).toBeGreaterThan(0.1);
385 // 8-bit sRGB can only approximate the source hue at the new lightness.
386 expect(
387 Math.abs(imported.hue - oklchParts(toCanonicalThemeColor(source)!).hue),
388 ).toBeLessThan(2);
389 }
390 },
391 );
392
393 it("keeps the derived status color when the file hides its own", () => {
394 const theme = parseVsCodeThemeFile({
395 name: "Hidden squiggles",
396 type: "dark",
397 colors: { "editor.background": "#101010", "editorError.foreground": "#ff000000" },
398 });
399 expect(oklchParts(theme.colors.error).chroma).toBeGreaterThan(0.1);
400 });
401
402 it("stays readable when the file replaces a surface but not its foreground", () => {
403 const theme = parseVsCodeThemeFile({
404 name: "Split",
405 type: "dark",
406 colors: {
407 "editor.background": "#101010",
408 "editor.foreground": "#f5f5f5",
409 "sideBar.background": "#fafafa",
410 "terminal.background": "#fbfbfb",
411 },
412 });
413 expect(asHex(theme.colors.sidebar)).toBe("#fafafa");
414 expect(
415 contrastRatio(theme.colors.sidebarForeground, theme.colors.sidebar),
416 ).toBeGreaterThanOrEqual(4.5);
417 expect(
418 contrastRatio(theme.colors.terminalForeground, theme.colors.terminalBackground),
419 ).toBeGreaterThanOrEqual(4.5);
420 });
421
422 it("reads wide-gamut color() notation", () => {
423 const theme = parseVsCodeThemeFile({
424 name: "Vibrant",
425 type: "dark",
426 colors: {
427 "editor.background": "color(display-p3 0.039216 0.039216 0.039216)",
428 "editor.foreground": "color(display-p3 0.980392 0.980392 0.980392)",
429 focusBorder: "color(display-p3 0.308664 0.645271 1.000000)",
430 "editor.selectionBackground": "color(display-p3 0.308664 0.645271 1.000000 / 0.300000)",
431 },
432 });
433 expect(asHex(theme.colors.canvas)).toMatch(/^#0[89ab]/);
434 expect(asHex(theme.colors.text)).toMatch(/^#f[a-f0-9]/);
435 const accent = asHex(theme.colors.accent);
436 const [red, green, blue] = [1, 3, 5].map((index) =>
437 Number.parseInt(accent.slice(index, index + 2), 16),
438 ) as [number, number, number];
439 expect(blue).toBeGreaterThan(200);
440 expect(blue).toBeGreaterThan(red);
441 expect(green).toBeGreaterThan(red);
442 });
443
444 it("keeps the theme's syntax colors with TextMate precedence", () => {
445 const theme = parseVsCodeThemeFile({
446 type: "dark",
447 colors: { "editor.background": "#101820", "editor.foreground": "#aabbcc" },
448 tokenColors: [
449 { scope: "keyword", settings: { foreground: "#ff00ff" } },
450 { scope: "keyword.control", settings: { foreground: "#ff0000" } },
451 { scope: ["string", "source.ts comment"], settings: { foreground: "#00ff00" } },
452 { scope: "comment", settings: { foreground: "#ffff0080", fontStyle: "italic bold" } },
453 { scope: "markup.heading", settings: { fontStyle: "bold" } },
454 { scope: "variable", settings: { fontStyle: "italic bold" } },
455 { scope: "variable.parameter", settings: { fontStyle: "" } },
456 ],
457 });
458 expect(theme.syntax?.dark).toMatchObject({
459 keyword: { color: "#ff0000" },
460 operator: { color: "#ff00ff" },
461 string: { color: "#00ff00" },
462 comment: { color: "#888c10", font_style: "italic", font_weight: 700 },
463 foreground: { color: "#aabbcc" },
464 });
465 expect(theme.syntax?.dark?.title).toEqual({ font_weight: 700 });
466 expect(theme.syntax?.dark?.variable).toEqual({ font_style: "italic", font_weight: 700 });
467 expect(theme.syntax?.dark?.["variable.parameter"]).toEqual({
468 font_style: "normal",
469 font_weight: 400,
470 });
471 expect(parseThemeFile(JSON.parse(serializeThemeFile(theme))).syntax).toEqual(theme.syntax);
472 });
473
474 it("pairs light and dark files from one family into dual-mode themes", () => {
475 const make = (name: string, type: "light" | "dark") =>
476 parseVsCodeThemeFile({
477 name,
478 type,
479 colors: {
480 "editor.background": type === "dark" ? "#101014" : "#fdfdfd",
481 "editor.foreground": type === "dark" ? "#e6e6e6" : "#1f1f1f",
482 focusBorder: "#69b1ff",
483 },
484 });
485 const themes = pairVsCodeThemes([
486 make("github-dark", "dark"),
487 make("github-light", "light"),
488 make("github-dark-colorblind", "dark"),
489 make("github-light-colorblind", "light"),
490 make("github-dark-dimmed", "dark"),
491 ]);
492
493 const labels = themes.map((theme) => theme.label);
494 expect(labels).toEqual(["Github", "Github Colorblind", "Github Dark Dimmed"]);
495 const github = themes[0]!;
496 expect(github.appearance).toBe("light");
497 expect(getThemeColorsForMode(github, "dark")).not.toBeNull();
498 expect(asHex(getThemeColorsForMode(github, "dark")!.canvas)).toBe("#101014");
499 expect(asHex(github.colors.canvas)).toBe("#fdfdfd");
500 expect(getThemeColorsForMode(themes[2]!, "light")).toBeNull();
501 });
502
503 it.each([
504 { name: "shared", light: "a {}", dark: "a {}", expected: [["Aurora", "a {}"]] },
505 { name: "light-only", light: "a {}", dark: undefined, expected: [["Aurora", "a {}"]] },
506 { name: "dark-only", light: undefined, dark: "a {}", expected: [["Aurora", "a {}"]] },
507 {
508 name: "conflicting",
509 light: "a {}",
510 dark: "b {}",
511 expected: [
512 ["Aurora Light", "a {}"],
513 ["Aurora Dark", "b {}"],
514 ],
515 },
516 ])("pairs light and dark halves with $name custom CSS", ({ light, dark, expected }) => {
517 const themes = pairVsCodeThemes(
518 (["light", "dark"] as const).map((appearance) => {
519 const css = appearance === "light" ? light : dark;
520 return parseThemeFile({
521 version: THEME_FILE_VERSION,
522 name: `Aurora ${appearance === "light" ? "Light" : "Dark"}`,
523 appearance,
524 colors: { canvas: appearance === "light" ? "#fafafa" : "#101014" },
525 ...(css ? { css } : {}),
526 });
527 }),
528 );
529 expect(themes.map((theme) => [theme.label, theme.css])).toEqual(expected);
530 });
531
532 it("does not guess when a family is ambiguous", () => {
533 const make = (name: string, type: "light" | "dark") =>
534 parseVsCodeThemeFile({
535 name,
536 type,
537 colors: { "editor.background": type === "dark" ? "#101014" : "#fdfdfd" },
538 });
539 const themes = pairVsCodeThemes([
540 make("solar-dark", "dark"),
541 make("solar-dark-soft", "dark"),
542 make("solar-light", "light"),
543 ]);
544 expect(themes.map((theme) => theme.label).sort()).toEqual(["Solar", "Solar Dark Soft"]);
545 });
546
547 it("tells same-named variants apart by their file names", () => {
548 // Both Dracula theme files declare the same internal name.
549 const css = ".app { outline: 1px solid red; }";
550 const dracula = (bg: string) => ({
551 ...parseVsCodeThemeFile({
552 name: "Dracula",
553 type: "dark",
554 colors: { "editor.background": bg, "editor.foreground": "#f8f8f2" },
555 }),
556 css,
557 });
558 const themes = resolveThemeLabelCollisions([
559 { theme: dracula("#282a36"), sourceName: "dracula.json" },
560 { theme: dracula("#22232e"), sourceName: "dracula-soft.json" },
561 ]);
562 expect(themes.map((theme) => theme.label)).toEqual(["Dracula", "Dracula Soft"]);
563 expect(themes.map((theme) => theme.id)).toEqual(["dracula", "dracula-soft"]);
564 expect(themes.map((theme) => theme.css)).toEqual([css, css]);
565 const numbered = resolveThemeLabelCollisions([
566 { theme: dracula("#282a36") },
567 { theme: dracula("#22232e") },
568 ]);
569 expect(numbered.map((theme) => theme.label)).toEqual(["Dracula", "Dracula 2"]);
570 expect(numbered.map((theme) => theme.css)).toEqual([css, css]);
571 });
572
573 const darkVsCodeTheme = (name: string, background: string) =>
574 parseVsCodeThemeFile({
575 name,
576 type: "dark",
577 colors: { "editor.background": background, "editor.foreground": "#cdd6f4" },
578 });
579
580 it("keeps both digits of a suffix when a one-digit suffix just fits", () => {
581 const name = "GitHub Dark High Contrast Deuteranopia Preview";
582 const backgrounds = [
583 "#1e1e2e",
584 "#25203b",
585 "#152a26",
586 "#181825",
587 "#11111b",
588 "#232136",
589 "#101014",
590 "#24273a",
591 "#282a36",
592 "#22232e",
593 ];
594 const themes = resolveThemeLabelCollisions(
595 backgrounds.map((background) => ({ theme: darkVsCodeTheme(name, background) })),
596 );
597 expect(themes.map((theme) => theme.label)).toEqual([
598 name,
599 ...Array.from({ length: 8 }, (_, index) => `${name} ${index + 2}`),
600 "GitHub Dark High Contrast Deuteranopia Previe 10",
601 ]);
602 expect(new Set(themes.map((theme) => theme.id)).size).toBe(backgrounds.length);
603 });
604
605 it.each([
606 {
607 name: "Catppuccin Mocha High Contrast Accessible v2 🌙",
608 numberedBase: "Catppuccin Mocha High Contrast Accessible v2",
609 },
610 {
611 name: "İstanbul İncisi İzmir İskenderun İnegöl İpsala",
612 numberedBase: "İstanbul İncisi İzmir İskenderun İnegöl İ",
613 },
614 ])("keeps all numbered variants of $name", ({ name, numberedBase }) => {
615 const themes = resolveThemeLabelCollisions(
616 ["#1e1e2e", "#25203b", "#152a26"].map((background) => ({
617 theme: darkVsCodeTheme(name, background),
618 })),
619 );
620 expect(themes.map((theme) => theme.label)).toEqual([
621 name,
622 `${numberedBase} 2`,
623 `${numberedBase} 3`,
624 ]);
625 expect(new Set(themes.map((theme) => theme.id)).size).toBe(3);
626 });
627
628 it("does not take the ID of a later theme already named with that number", () => {
629 const name = "Catppuccin Mocha High Contrast Accessible Themes";
630 const numberedBase = "Catppuccin Mocha High Contrast Accessible Them";
631 const duplicates = ["#1e1e2e", "#25203b"].map((background) => ({
632 theme: darkVsCodeTheme(name, background),
633 }));
634 const peer = { theme: darkVsCodeTheme(`${numberedBase} 2`, "#152a26") };
635 const themes = resolveThemeLabelCollisions([...duplicates, peer]);
636 expect(themes.map(({ label }) => label)).toEqual([
637 name,
638 `${numberedBase} 3`,
639 `${numberedBase} 2`,
640 ]);
641 expect(themes.find(({ id }) => id === peer.theme.id)?.colors.canvas).toBe(
642 peer.theme.colors.canvas,
643 );
644 });
645
646 it("makes the appended number reach the ID when the name already contains that number", () => {
647 const name = "İzmir İzmit İznik İğdır İdil İspir İncir 2 Ö";
648 const themes = resolveThemeLabelCollisions(
649 ["#1e1e2e", "#25203b"].map((canvas) => ({
650 theme: parseThemeFile({
651 version: THEME_FILE_VERSION,
652 id: "izmir-nightfall",
653 name,
654 appearance: "dark",
655 colors: { canvas },
656 }),
657 })),
658 );
659 expect(themes.map(({ label, id }) => [label, id])).toEqual([
660 [name, "izmir-nightfall"],
661 [
662 "İzmir İzmit İznik İğdır İdil İspir İncir 2",
663 "i-zmir-i-zmit-i-znik-i-d-r-i-dil-i-spir-i-ncir-2",
664 ],
665 ]);
666 });
667
668 it.each([
669 { label: "surrogate pair", input: `${"a".repeat(47)}🌙`, expected: "a".repeat(47) },
670 { label: "flag emoji", input: `${"a".repeat(46)}🇺🇸`, expected: "a".repeat(46) },
671 {
672 label: "ZWJ sequence",
673 input: `${"a".repeat(45)}👨\u200D👩\u200D👧`,
674 expected: "a".repeat(45),
675 },
676 {
677 label: "oversized cluster",
678 input: `e${"\u0301".repeat(50)}`,
679 expected: "VS Code theme",
680 renamed: "Dracula 2",
681 },
682 ])(
683 "does not split a $label when truncating a 48-character theme name",
684 ({ input, expected, renamed }) => {
685 const imported = darkVsCodeTheme(input, "#101010");
686 const collided = resolveThemeLabelCollisions([
687 { theme: darkVsCodeTheme("Dracula", "#282a36"), sourceName: "dracula.json" },
688 { theme: darkVsCodeTheme("Dracula", "#22232e"), sourceName: `${input}.json` },
689 ]);
690 expect([imported.label, collided[1]?.label]).toEqual([expected, renamed ?? expected]);
691 },
692 );
693
694 it("does not split a grapheme cluster when trimming for a numbered suffix", () => {
695 const flagLabel = `${"a".repeat(44)}🇺🇸`;
696 const collided = resolveThemeLabelCollisions([
697 { theme: darkVsCodeTheme(flagLabel, "#282a36") },
698 { theme: darkVsCodeTheme(flagLabel, "#22232e") },
699 ]);
700 expect(collided[1]?.label).toBe(`${"a".repeat(44)} 2`);
701 expect(numberedThemeName(`${"a".repeat(42)}🇺🇸`, " (1)")).toBe(`${"a".repeat(42)} (1)`);
702 });
703});