tui-theme.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. LIGHT_TUI_THEME,
  6. activeTuiTheme,
  7. getActiveTuiThemeMode,
  8. inferThemeModeFromTerminalColors,
  9. resolveTuiThemeMode,
  10. setActiveTuiThemeMode,
  11. } from "../src/theme/index.js";
  12. test("resolves theme preferences against terminal mode", () => {
  13. assert.equal(resolveTuiThemeMode("dark", "light"), "dark");
  14. assert.equal(resolveTuiThemeMode("light", "dark"), "light");
  15. assert.equal(resolveTuiThemeMode("auto", "light"), "light");
  16. assert.equal(resolveTuiThemeMode("auto", null), "dark");
  17. assert.equal(resolveTuiThemeMode(undefined, "light"), "light");
  18. });
  19. test("infers terminal theme mode from terminal background colors", () => {
  20. assert.equal(inferThemeModeFromTerminalColors(terminalColors("#ffffff")), "light");
  21. assert.equal(inferThemeModeFromTerminalColors(terminalColors("#000000")), "dark");
  22. assert.equal(inferThemeModeFromTerminalColors(terminalColors(null)), null);
  23. });
  24. test("active theme controls legacy component colors", () => {
  25. try {
  26. setActiveTuiThemeMode("light");
  27. const style = reactElementStyle(ContentPane({ focused: false, messages: [] }));
  28. assert.equal(getActiveTuiThemeMode(), "light");
  29. assert.equal(activeTuiTheme().mode, "light");
  30. assert.equal(style.backgroundColor, LIGHT_TUI_THEME.background);
  31. } finally {
  32. setActiveTuiThemeMode("dark");
  33. }
  34. });
  35. function terminalColors(defaultBackground: string | null) {
  36. return {
  37. cursorColor: null,
  38. defaultBackground,
  39. defaultForeground: null,
  40. highlightBackground: null,
  41. highlightForeground: null,
  42. mouseBackground: null,
  43. mouseForeground: null,
  44. palette: [defaultBackground],
  45. tekBackground: null,
  46. tekForeground: null,
  47. };
  48. }
  49. function reactElementStyle(node: unknown): Record<string, unknown> {
  50. assert.ok(typeof node === "object" && node !== null && "props" in node);
  51. const element = node as { props?: { style?: Record<string, unknown> } };
  52. return element.props?.style ?? {};
  53. }