config.test.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /** @jsxImportSource @opentui/solid */
  2. import { testRender } from "@opentui/solid"
  3. import { expect, test } from "bun:test"
  4. import { Schema } from "effect"
  5. import {
  6. AttentionSoundName,
  7. Info,
  8. LeaderTimeoutDefault,
  9. PluginSpec,
  10. resolve,
  11. TuiConfigProvider,
  12. type Info as TuiConfigInfo,
  13. useTuiConfig,
  14. } from "../src/config"
  15. const decodeInfo = Schema.decodeUnknownSync(Info)
  16. const decodePlugin = Schema.decodeUnknownSync(PluginSpec)
  17. test("defines package-owned plugin specs and attention sound names", () => {
  18. expect(decodePlugin("example-plugin")).toBe("example-plugin")
  19. expect(decodePlugin(["example-plugin", { enabled: true }])).toEqual(["example-plugin", { enabled: true }])
  20. expect(() => decodePlugin(["example-plugin"])).toThrow()
  21. expect(AttentionSoundName.literals).toEqual(["default", "question", "permission", "error", "done", "subagent_done"])
  22. })
  23. test("validates config constraints", () => {
  24. expect(
  25. decodeInfo({
  26. leader_timeout: 250,
  27. attention: { volume: 1, sounds: { done: "done.wav" } },
  28. prompt: { max_height: 10, max_width: "auto" },
  29. scroll_speed: 0.001,
  30. diff_style: "stacked",
  31. cursor: { blinking: false },
  32. plugin: ["example-plugin"],
  33. }),
  34. ).toMatchObject({
  35. leader_timeout: 250,
  36. attention: { volume: 1 },
  37. diff_style: "stacked",
  38. cursor: { blinking: false },
  39. })
  40. expect(() => decodeInfo({ leader_timeout: 0 })).toThrow()
  41. expect(() => decodeInfo({ attention: { volume: 1.1 } })).toThrow()
  42. expect(() => decodeInfo({ prompt: { max_width: 0 } })).toThrow()
  43. expect(() => decodeInfo({ scroll_speed: 0 })).toThrow()
  44. expect(() => decodeInfo({ cursor: { style: "beam" } })).toThrow()
  45. expect(decodeInfo({ attention: { sounds: { unknown: "sound.wav" } } })).toEqual({ attention: { sounds: {} } })
  46. })
  47. test("resolves host-neutral defaults", () => {
  48. const config = resolve({}, { terminalSuspend: true })
  49. expect(config.attention).toEqual({
  50. enabled: false,
  51. notifications: true,
  52. sound: true,
  53. volume: 0.4,
  54. sound_pack: "opencode.default",
  55. sounds: {},
  56. })
  57. expect(config.leader_timeout).toBe(LeaderTimeoutDefault)
  58. expect(config.mouse).toBe(true)
  59. expect(config.keybinds.has("terminal.suspend")).toBe(true)
  60. expect(config.keybinds.has("session.list")).toBe(true)
  61. expect(config.cursor).toBeUndefined()
  62. })
  63. test("resolves overrides without mutating input", () => {
  64. const input: TuiConfigInfo = {
  65. theme: "custom",
  66. mouse: false,
  67. leader_timeout: 750,
  68. attention: {
  69. enabled: true,
  70. notifications: false,
  71. sound: false,
  72. volume: 0.8,
  73. sound_pack: "custom.pack",
  74. sounds: { question: "/sounds/question.wav" },
  75. },
  76. keybinds: { session_list: "ctrl+l" },
  77. cursor: { blinking: false },
  78. }
  79. const config = resolve(input, { terminalSuspend: true })
  80. expect(config).toMatchObject({
  81. theme: "custom",
  82. mouse: false,
  83. leader_timeout: 750,
  84. attention: input.attention,
  85. cursor: { style: "block", blinking: false },
  86. })
  87. expect(config.keybinds.get("session.list")).toHaveLength(1)
  88. expect(input.keybinds).toEqual({ session_list: "ctrl+l" })
  89. })
  90. test("resolves a session move keybind", () => {
  91. const config = resolve({ keybinds: { session_move: "ctrl+o" } }, { terminalSuspend: true })
  92. expect(config.keybinds.get("session.move")).toMatchObject([{ key: "ctrl+o" }])
  93. })
  94. test("disables suspend and assigns ctrl+z to undo when unsupported", () => {
  95. const config = resolve({}, { terminalSuspend: false })
  96. expect(config.keybinds.has("terminal.suspend")).toBe(false)
  97. expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+z,ctrl+-,super+z" }])
  98. })
  99. test("preserves an explicit undo binding when suspend is unsupported", () => {
  100. const config = resolve({ keybinds: { input_undo: "ctrl+u", terminal_suspend: "ctrl+s" } }, { terminalSuspend: false })
  101. expect(config.keybinds.has("terminal.suspend")).toBe(false)
  102. expect(config.keybinds.get("input.undo")).toHaveLength(1)
  103. expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+u" }])
  104. })
  105. test("provides resolved config through Solid context", async () => {
  106. const config = resolve({ theme: "custom" }, { terminalSuspend: true })
  107. function Consumer() {
  108. const value = useTuiConfig()
  109. return <text>{`${value.theme} ${value.mouse} ${value.leader_timeout}`}</text>
  110. }
  111. const app = await testRender(() => (
  112. <TuiConfigProvider config={config}>
  113. <Consumer />
  114. </TuiConfigProvider>
  115. ))
  116. try {
  117. await app.renderOnce()
  118. expect(app.captureCharFrame()).toContain(`custom true ${LeaderTimeoutDefault}`)
  119. } finally {
  120. app.renderer.destroy()
  121. }
  122. })
  123. test("requires the config provider", () => {
  124. expect(() => useTuiConfig()).toThrow("TuiConfigProvider is missing")
  125. })