theme.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { expect, test } from "bun:test"
  2. import type { TerminalColors } from "@opentui/core"
  3. import { DEFAULT_THEMES, addTheme, allThemes, hasTheme, resolveTheme, terminalMode } from "../src/theme"
  4. test("addTheme writes into module theme store", () => {
  5. const name = `plugin-theme-${Date.now()}`
  6. expect(addTheme(name, DEFAULT_THEMES.opencode)).toBe(true)
  7. expect(allThemes()[name]).toBeDefined()
  8. })
  9. test("addTheme keeps first theme for duplicate names", () => {
  10. const name = `plugin-theme-keep-${Date.now()}`
  11. const one = structuredClone(DEFAULT_THEMES.opencode)
  12. const two = structuredClone(DEFAULT_THEMES.opencode)
  13. one.theme.primary = "#101010"
  14. two.theme.primary = "#fefefe"
  15. expect(addTheme(name, one)).toBe(true)
  16. expect(addTheme(name, two)).toBe(false)
  17. expect(allThemes()[name]!.theme.primary).toBe("#101010")
  18. })
  19. test("addTheme ignores entries without a theme object", () => {
  20. const name = `plugin-theme-invalid-${Date.now()}`
  21. expect(addTheme(name, { defs: { a: "#ffffff" } })).toBe(false)
  22. expect(allThemes()[name]).toBeUndefined()
  23. })
  24. test("hasTheme checks theme presence", () => {
  25. const name = `plugin-theme-has-${Date.now()}`
  26. expect(hasTheme(name)).toBe(false)
  27. expect(addTheme(name, DEFAULT_THEMES.opencode)).toBe(true)
  28. expect(hasTheme(name)).toBe(true)
  29. })
  30. test("resolveTheme rejects circular color refs", () => {
  31. const item = structuredClone(DEFAULT_THEMES.opencode)
  32. item.defs = { ...item.defs, one: "two", two: "one" }
  33. item.theme.primary = "one"
  34. expect(() => resolveTheme(item, "dark")).toThrow("Circular color reference")
  35. })
  36. function terminalColors(defaultBackground: string | null, palette: Array<string | null> = []): TerminalColors {
  37. return {
  38. palette,
  39. defaultForeground: null,
  40. defaultBackground,
  41. cursorColor: null,
  42. mouseForeground: null,
  43. mouseBackground: null,
  44. tekForeground: null,
  45. tekBackground: null,
  46. highlightBackground: null,
  47. highlightForeground: null,
  48. }
  49. }
  50. test("terminalMode derives mode from refreshed background", () => {
  51. expect(terminalMode(terminalColors("#fbf1c7"))).toBe("light")
  52. expect(terminalMode(terminalColors("#1a1b26"))).toBe("dark")
  53. })
  54. test("terminalMode does not derive mode from ANSI slot zero", () => {
  55. expect(terminalMode(terminalColors(null, ["#000000"]))).toBeUndefined()
  56. })