terminalTheme.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. import type { ITheme } from "@xterm/xterm";
  4. import { restrictInheritedTerminalTheme } from "@/terminal/terminalTheme.js";
  5. function readCssBlock(selector: string): string {
  6. const styles = readFileSync("packages/ui/src/styles.css", "utf8");
  7. const escapedSelector = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  8. const match = styles.match(new RegExp(`${escapedSelector}\\s*\\{([\\s\\S]*?)\\n\\}`));
  9. return match?.[1] ?? "";
  10. }
  11. function readCssToken(block: string, token: string): string | undefined {
  12. const escapedToken = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  13. return block.match(new RegExp(`${escapedToken}:\\s*([^;]+);`))?.[1]?.trim();
  14. }
  15. describe("terminalTheme", () => {
  16. it("keeps app-controlled readable colors out of inherited profile colors", () => {
  17. const inherited = restrictInheritedTerminalTheme({
  18. background: "#111111",
  19. foreground: "#eeeeee",
  20. cursor: "#00ff00",
  21. cursorAccent: "#111111",
  22. red: "#ff0000",
  23. brightBlue: "#3399ff",
  24. } satisfies ITheme);
  25. expect(inherited).toEqual({
  26. red: "#ff0000",
  27. brightBlue: "#3399ff",
  28. });
  29. });
  30. it("drops inherited theme when it only contains app-controlled surface colors", () => {
  31. expect(
  32. restrictInheritedTerminalTheme({
  33. background: "#111111",
  34. foreground: "#eeeeee",
  35. cursor: "#00ff00",
  36. cursorAccent: "#111111",
  37. } satisfies ITheme),
  38. ).toBeUndefined();
  39. });
  40. it("keeps Zai Light terminal surface colors bound to app theme tokens", () => {
  41. const zaiLightBlock = readCssBlock(".theme-zai-light");
  42. expect(readCssToken(zaiLightBlock, "--color-terminal-bg")).toBe("var(--color-background)");
  43. expect(readCssToken(zaiLightBlock, "--color-terminal-fg")).toBe("var(--color-foreground)");
  44. expect(readCssToken(zaiLightBlock, "--color-terminal-cursor")).toBe("#0d0d0d");
  45. expect(readCssToken(zaiLightBlock, "--color-terminal-cursor-accent")).toBe(
  46. "var(--color-background)",
  47. );
  48. });
  49. });