theme-detection.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { resetCapabilitiesCache, setCapabilities } from "@earendil-works/pi-tui";
  2. import { afterEach, describe, expect, it } from "vitest";
  3. import {
  4. detectTerminalBackground,
  5. getThemeByName,
  6. getThemeForRgbColor,
  7. parseOsc11BackgroundColor,
  8. } from "../src/modes/interactive/theme/theme.ts";
  9. afterEach(() => {
  10. resetCapabilitiesCache();
  11. });
  12. describe("detectTerminalBackground", () => {
  13. it("uses the COLORFGBG background color index", () => {
  14. expect(detectTerminalBackground({ env: { COLORFGBG: "0;15" } })).toMatchObject({
  15. theme: "light",
  16. source: "COLORFGBG",
  17. confidence: "high",
  18. });
  19. expect(detectTerminalBackground({ env: { COLORFGBG: "15;0" } })).toMatchObject({
  20. theme: "dark",
  21. source: "COLORFGBG",
  22. confidence: "high",
  23. });
  24. });
  25. it("uses the last COLORFGBG field as the background", () => {
  26. expect(detectTerminalBackground({ env: { COLORFGBG: "0;7;15" } }).theme).toBe("light");
  27. });
  28. it("defaults to dark without terminal background hints", () => {
  29. expect(detectTerminalBackground({ env: {} })).toMatchObject({
  30. theme: "dark",
  31. source: "fallback",
  32. confidence: "low",
  33. });
  34. });
  35. });
  36. describe("theme color mode", () => {
  37. it("uses terminal capabilities", () => {
  38. setCapabilities({ images: null, trueColor: false, hyperlinks: false });
  39. const ansi256Theme = getThemeByName("dark");
  40. if (!ansi256Theme) throw new Error("dark theme not found");
  41. expect(ansi256Theme.getColorMode()).toBe("256color");
  42. expect(ansi256Theme.getFgAnsi("accent")).toMatch(/^\x1b\[38;5;\d+m$/);
  43. setCapabilities({ images: null, trueColor: true, hyperlinks: false });
  44. const truecolorTheme = getThemeByName("dark");
  45. if (!truecolorTheme) throw new Error("dark theme not found");
  46. expect(truecolorTheme.getColorMode()).toBe("truecolor");
  47. expect(truecolorTheme.getFgAnsi("accent")).toMatch(/^\x1b\[38;2;\d+;\d+;\d+m$/);
  48. });
  49. });
  50. describe("parseOsc11BackgroundColor", () => {
  51. it("parses 16-bit OSC 11 rgb responses", () => {
  52. expect(parseOsc11BackgroundColor("\x1b]11;rgb:0000/8000/ffff\x07")).toEqual({ r: 0, g: 128, b: 255 });
  53. });
  54. it("parses OSC 11 hex responses", () => {
  55. expect(parseOsc11BackgroundColor("\x1b]11;#ffffff\x1b\\")).toEqual({ r: 255, g: 255, b: 255 });
  56. expect(parseOsc11BackgroundColor("\x1b]11;#000000\x07")).toEqual({ r: 0, g: 0, b: 0 });
  57. });
  58. it("classifies RGB colors by luminance", () => {
  59. expect(getThemeForRgbColor({ r: 8, g: 8, b: 8 })).toBe("dark");
  60. expect(getThemeForRgbColor({ r: 250, g: 250, b: 250 })).toBe("light");
  61. });
  62. });