theme.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { expect, test } from "bun:test"
  2. import { mkdir, writeFile } from "node:fs/promises"
  3. import path from "node:path"
  4. import type { TerminalColors } from "@opentui/core"
  5. import { DEFAULT_THEMES, addTheme, allThemes, hasTheme, resolveTheme } from "../src/theme"
  6. import { discoverThemes, themeDirectories } from "../src/theme/discovery"
  7. import { terminalMode } from "../src/theme/system"
  8. import { tmpdir } from "./fixture/fixture"
  9. test("addTheme writes into module theme store", () => {
  10. const name = `plugin-theme-${Date.now()}`
  11. expect(addTheme(name, DEFAULT_THEMES.opencode)).toBe(true)
  12. expect(allThemes()[name]).toBeDefined()
  13. })
  14. test("addTheme keeps first theme for duplicate names", () => {
  15. const name = `plugin-theme-keep-${Date.now()}`
  16. const one = structuredClone(DEFAULT_THEMES.opencode)
  17. const two = structuredClone(DEFAULT_THEMES.opencode)
  18. one.theme.primary = "#101010"
  19. two.theme.primary = "#fefefe"
  20. expect(addTheme(name, one)).toBe(true)
  21. expect(addTheme(name, two)).toBe(false)
  22. expect(allThemes()[name]!.theme.primary).toBe("#101010")
  23. })
  24. test("addTheme ignores entries without a theme object", () => {
  25. const name = `plugin-theme-invalid-${Date.now()}`
  26. expect(addTheme(name, { defs: { a: "#ffffff" } })).toBe(false)
  27. expect(allThemes()[name]).toBeUndefined()
  28. })
  29. test("hasTheme checks theme presence", () => {
  30. const name = `plugin-theme-has-${Date.now()}`
  31. expect(hasTheme(name)).toBe(false)
  32. expect(addTheme(name, DEFAULT_THEMES.opencode)).toBe(true)
  33. expect(hasTheme(name)).toBe(true)
  34. })
  35. test("resolveTheme rejects circular color refs", () => {
  36. const item = structuredClone(DEFAULT_THEMES.opencode)
  37. item.defs = { ...item.defs, one: "two", two: "one" }
  38. item.theme.primary = "one"
  39. expect(() => resolveTheme(item, "dark")).toThrow("Circular color reference")
  40. })
  41. test("resolveTheme preserves full theme numeric color and marker semantics", () => {
  42. const item = structuredClone(DEFAULT_THEMES.opencode)
  43. item.theme.primary = 6
  44. delete item.theme.selectedListItemText
  45. const theme = resolveTheme(item, "dark")
  46. expect(theme.primary.intent).toBe("rgb")
  47. expect(theme.selectedListItemText).toBe(theme.background)
  48. expect(theme._hasSelectedListItemText).toBe(false)
  49. })
  50. function terminalColors(defaultBackground: string | null, palette: Array<string | null> = []): TerminalColors {
  51. return {
  52. palette,
  53. defaultForeground: null,
  54. defaultBackground,
  55. cursorColor: null,
  56. mouseForeground: null,
  57. mouseBackground: null,
  58. tekForeground: null,
  59. tekBackground: null,
  60. highlightBackground: null,
  61. highlightForeground: null,
  62. }
  63. }
  64. test("terminalMode derives mode from refreshed background", () => {
  65. expect(terminalMode(terminalColors("#fbf1c7"))).toBe("light")
  66. expect(terminalMode(terminalColors("#1a1b26"))).toBe("dark")
  67. })
  68. test("terminalMode does not derive mode from ANSI slot zero", () => {
  69. expect(terminalMode(terminalColors(null, ["#000000"]))).toBeUndefined()
  70. })
  71. test("custom theme precedence follows directory order", async () => {
  72. await using tmp = await tmpdir()
  73. const global = path.join(tmp.path, "global")
  74. const project = path.join(tmp.path, "project")
  75. await mkdir(path.join(global, "themes"), { recursive: true })
  76. await mkdir(path.join(project, "themes"), { recursive: true })
  77. await writeFile(path.join(global, "themes", "custom.json"), JSON.stringify({ source: "global" }))
  78. await writeFile(path.join(project, "themes", "custom.json"), JSON.stringify({ source: "project" }))
  79. await expect(discoverThemes([global, project])).resolves.toEqual({ custom: { source: "project" } })
  80. })
  81. test("theme directories include global config before project directories", async () => {
  82. await using tmp = await tmpdir()
  83. const global = path.join(tmp.path, "global")
  84. const project = path.join(tmp.path, "repo", "package")
  85. await mkdir(path.join(global, "themes"), { recursive: true })
  86. await mkdir(path.join(project, ".opencode", "themes"), { recursive: true })
  87. await writeFile(path.join(global, "themes", "global.json"), JSON.stringify({ source: "global" }))
  88. await writeFile(path.join(project, ".opencode", "themes", "project.json"), JSON.stringify({ source: "project" }))
  89. await expect(discoverThemes(themeDirectories(global, project))).resolves.toEqual({
  90. global: { source: "global" },
  91. project: { source: "project" },
  92. })
  93. })