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