tui-theme.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { ContentPane } from "../src/app-transcript-components.js";
  4. import {
  5. DARK_TUI_THEME,
  6. LIGHT_TUI_THEME,
  7. activeTuiTheme,
  8. getActiveTuiThemeMode,
  9. inferThemeModeFromTerminalColors,
  10. resolveTuiThemeMode,
  11. setActiveTuiThemeMode,
  12. themeToLegacyPalette,
  13. } from "../src/theme/index.js";
  14. test("resolves theme preferences against terminal mode", () => {
  15. assert.equal(resolveTuiThemeMode("dark", "light"), "dark");
  16. assert.equal(resolveTuiThemeMode("light", "dark"), "light");
  17. assert.equal(resolveTuiThemeMode("auto", "light"), "light");
  18. assert.equal(resolveTuiThemeMode("auto", null), "dark");
  19. assert.equal(resolveTuiThemeMode(undefined, "light"), "light");
  20. });
  21. test("maps semantic themes to the legacy palette", () => {
  22. const dark = themeToLegacyPalette(DARK_TUI_THEME);
  23. const light = themeToLegacyPalette(LIGHT_TUI_THEME);
  24. assert.equal(dark.background, "#0f1419");
  25. assert.equal(dark.userMessageBackground, DARK_TUI_THEME.backgroundMessageUser);
  26. assert.equal(light.background, "#f8fafc");
  27. assert.equal(light.panelAlt, LIGHT_TUI_THEME.backgroundElement);
  28. });
  29. test("infers terminal theme mode from terminal background colors", () => {
  30. assert.equal(inferThemeModeFromTerminalColors(terminalColors("#ffffff")), "light");
  31. assert.equal(inferThemeModeFromTerminalColors(terminalColors("#000000")), "dark");
  32. assert.equal(inferThemeModeFromTerminalColors(terminalColors(null)), null);
  33. });
  34. test("active theme controls legacy component colors", () => {
  35. try {
  36. setActiveTuiThemeMode("light");
  37. const style = reactElementStyle(ContentPane({ focused: false, messages: [] }));
  38. assert.equal(getActiveTuiThemeMode(), "light");
  39. assert.equal(activeTuiTheme().mode, "light");
  40. assert.equal(style.backgroundColor, LIGHT_TUI_THEME.background);
  41. } finally {
  42. setActiveTuiThemeMode("dark");
  43. }
  44. });
  45. function terminalColors(defaultBackground: string | null) {
  46. return {
  47. cursorColor: null,
  48. defaultBackground,
  49. defaultForeground: null,
  50. highlightBackground: null,
  51. highlightForeground: null,
  52. mouseBackground: null,
  53. mouseForeground: null,
  54. palette: [defaultBackground],
  55. tekBackground: null,
  56. tekForeground: null,
  57. };
  58. }
  59. function reactElementStyle(node: unknown): Record<string, unknown> {
  60. assert.ok(typeof node === "object" && node !== null && "props" in node);
  61. const element = node as { props?: { style?: Record<string, unknown> } };
  62. return element.props?.style ?? {};
  63. }