theme.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 {
  6. DEFAULT_THEMES,
  7. addTheme,
  8. allThemes,
  9. hasTheme,
  10. parseTheme,
  11. resolveTheme,
  12. setCustomThemes,
  13. upsertTheme,
  14. } from "../src/theme"
  15. import { discoverThemes } from "../src/theme/discovery"
  16. import { configDirectories } from "../src/util/config-directories"
  17. import { terminalMode } from "../src/theme/system"
  18. import { tmpdir } from "./fixture/fixture"
  19. test("addTheme writes into module theme store", () => {
  20. const name = `plugin-theme-${Date.now()}`
  21. expect(addTheme(name, DEFAULT_THEMES.opencode)).toBe(true)
  22. expect(allThemes()[name]).toBe(DEFAULT_THEMES.opencode)
  23. })
  24. test("addTheme keeps first theme for duplicate names", () => {
  25. const name = `plugin-theme-keep-${Date.now()}`
  26. const one = structuredClone(DEFAULT_THEMES.opencode)
  27. const two = structuredClone(DEFAULT_THEMES.opencode)
  28. one.theme.primary = "#101010"
  29. two.theme.primary = "#fefefe"
  30. expect(addTheme(name, one)).toBe(true)
  31. expect(addTheme(name, two)).toBe(false)
  32. expect(allThemes()[name]).toBe(one)
  33. })
  34. test("addTheme ignores values without a V1 theme or version", () => {
  35. const name = `plugin-theme-invalid-${Date.now()}`
  36. expect(addTheme(name, { defs: { a: "#ffffff" } })).toBe(false)
  37. expect(addTheme(name, { light: {} })).toBe(false)
  38. expect(allThemes()[name]).toBeUndefined()
  39. })
  40. test("addTheme defers validation of versioned sources", () => {
  41. const name = `plugin-theme-versioned-${Date.now()}`
  42. expect(addTheme(name, { version: 2 })).toBe(true)
  43. expect(() => parseTheme(allThemes()[name]!, name)).toThrow(`Invalid theme: ${name}`)
  44. })
  45. test("parseTheme delegates malformed V1 sources and rejects unknown versions", () => {
  46. expect(() => parseTheme({})).toThrow()
  47. expect(() => parseTheme({ version: 3 })).toThrow("Unsupported theme version: 3")
  48. })
  49. test("parses unversioned and explicit V1 themes lazily once", () => {
  50. const unversioned = structuredClone(DEFAULT_THEMES.opencode)
  51. const explicit = { ...structuredClone(DEFAULT_THEMES.opencode), version: 1 }
  52. const first = parseTheme(unversioned, "unversioned")
  53. const second = parseTheme(explicit, "explicit")
  54. expect(first.version).toBe(2)
  55. expect(second.version).toBe(2)
  56. expect(parseTheme(unversioned, "unversioned")).toBe(first)
  57. expect(parseTheme(explicit, "explicit")).toBe(second)
  58. })
  59. test("decodes native V2 themes lazily once", () => {
  60. const name = `plugin-theme-v2-${Date.now()}`
  61. const source = { version: 2, light: { categorical: ["red"] } } as const
  62. expect(addTheme(name, source)).toBe(true)
  63. expect(allThemes()[name]).toBe(source)
  64. const document = parseTheme(allThemes()[name]!, name)
  65. expect(document.light?.categorical).toEqual(["red"])
  66. expect(parseTheme(allThemes()[name]!, name)).toBe(document)
  67. })
  68. test("defers invalid V2 errors until parsing", () => {
  69. const name = `plugin-theme-invalid-v2-${Date.now()}`
  70. expect(addTheme(name, { version: 2, light: { categorical: [] } })).toBe(true)
  71. expect(() => parseTheme(allThemes()[name]!, name)).toThrow(`Invalid theme: ${name}`)
  72. })
  73. test("defers invalid V1 errors until parsing", () => {
  74. const name = `plugin-theme-invalid-v1-${Date.now()}`
  75. const source = structuredClone(DEFAULT_THEMES.opencode)
  76. source.defs = { ...source.defs, one: "two", two: "one" }
  77. source.theme.primary = "one"
  78. expect(addTheme(name, source)).toBe(true)
  79. expect(() => parseTheme(allThemes()[name]!, name)).toThrow("Circular color reference")
  80. })
  81. test("replacement sources receive independent parse caches", () => {
  82. const name = `plugin-theme-replace-${Date.now()}`
  83. const first = structuredClone(DEFAULT_THEMES.opencode)
  84. const second = structuredClone(DEFAULT_THEMES.opencode)
  85. second.theme.primary = "#123456"
  86. expect(addTheme(name, first)).toBe(true)
  87. const previous = parseTheme(allThemes()[name]!, name)
  88. expect(upsertTheme(name, second)).toBe(true)
  89. const next = parseTheme(allThemes()[name]!, name)
  90. expect(next).not.toBe(previous)
  91. expect(parseTheme(allThemes()[name]!, name)).toBe(next)
  92. })
  93. test("custom themes retain precedence over plugin themes", () => {
  94. const name = `plugin-theme-precedence-${Date.now()}`
  95. const plugin = structuredClone(DEFAULT_THEMES.opencode)
  96. const custom = structuredClone(DEFAULT_THEMES.opencode)
  97. expect(addTheme(name, plugin)).toBe(true)
  98. setCustomThemes({ [name]: custom })
  99. expect(allThemes()[name]).toBe(custom)
  100. setCustomThemes({})
  101. expect(allThemes()[name]).toBe(plugin)
  102. })
  103. test("hasTheme checks theme presence", () => {
  104. const name = `plugin-theme-has-${Date.now()}`
  105. expect(hasTheme(name)).toBe(false)
  106. expect(addTheme(name, DEFAULT_THEMES.opencode)).toBe(true)
  107. expect(hasTheme(name)).toBe(true)
  108. })
  109. test("resolveTheme rejects circular color refs", () => {
  110. const item = structuredClone(DEFAULT_THEMES.opencode)
  111. item.defs = { ...item.defs, one: "two", two: "one" }
  112. item.theme.primary = "one"
  113. expect(() => resolveTheme(item, "dark")).toThrow("Circular color reference")
  114. })
  115. test("resolveTheme preserves full theme numeric color and marker semantics", () => {
  116. const item = structuredClone(DEFAULT_THEMES.opencode)
  117. item.theme.primary = 6
  118. delete item.theme.selectedListItemText
  119. const theme = resolveTheme(item, "dark")
  120. expect(theme.primary.intent).toBe("rgb")
  121. expect(theme.selectedListItemText).toBe(theme.background)
  122. expect(theme._hasSelectedListItemText).toBe(false)
  123. })
  124. function terminalColors(defaultBackground: string | null, palette: Array<string | null> = []): TerminalColors {
  125. return {
  126. palette,
  127. defaultForeground: null,
  128. defaultBackground,
  129. cursorColor: null,
  130. mouseForeground: null,
  131. mouseBackground: null,
  132. tekForeground: null,
  133. tekBackground: null,
  134. highlightBackground: null,
  135. highlightForeground: null,
  136. }
  137. }
  138. test("terminalMode derives mode from refreshed background", () => {
  139. expect(terminalMode(terminalColors("#fbf1c7"))).toBe("light")
  140. expect(terminalMode(terminalColors("#1a1b26"))).toBe("dark")
  141. })
  142. test("terminalMode does not derive mode from ANSI slot zero", () => {
  143. expect(terminalMode(terminalColors(null, ["#000000"]))).toBeUndefined()
  144. })
  145. test("custom theme precedence follows directory order", async () => {
  146. await using tmp = await tmpdir()
  147. const global = path.join(tmp.path, "global")
  148. const project = path.join(tmp.path, "project")
  149. await mkdir(path.join(global, "themes"), { recursive: true })
  150. await mkdir(path.join(project, "themes"), { recursive: true })
  151. await writeFile(path.join(global, "themes", "custom.json"), JSON.stringify({ source: "global" }))
  152. await writeFile(path.join(project, "themes", "custom.json"), JSON.stringify({ source: "project" }))
  153. await expect(discoverThemes([global, project])).resolves.toEqual({ custom: { source: "project" } })
  154. })
  155. test("theme directories include global config before project directories", async () => {
  156. await using tmp = await tmpdir()
  157. const global = path.join(tmp.path, "global")
  158. const project = path.join(tmp.path, "repo", "package")
  159. await mkdir(path.join(global, "themes"), { recursive: true })
  160. await mkdir(path.join(project, ".opencode", "themes"), { recursive: true })
  161. await writeFile(path.join(global, "themes", "global.json"), JSON.stringify({ source: "global" }))
  162. await writeFile(path.join(project, ".opencode", "themes", "project.json"), JSON.stringify({ source: "project" }))
  163. await expect(discoverThemes(configDirectories(global, project))).resolves.toEqual({
  164. global: { source: "global" },
  165. project: { source: "project" },
  166. })
  167. })