config.test.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. plugin: ["example-plugin"],
  32. }),
  33. ).toMatchObject({ leader_timeout: 250, attention: { volume: 1 }, diff_style: "stacked" })
  34. expect(() => decodeInfo({ leader_timeout: 0 })).toThrow()
  35. expect(() => decodeInfo({ attention: { volume: 1.1 } })).toThrow()
  36. expect(() => decodeInfo({ prompt: { max_width: 0 } })).toThrow()
  37. expect(() => decodeInfo({ scroll_speed: 0 })).toThrow()
  38. expect(decodeInfo({ attention: { sounds: { unknown: "sound.wav" } } })).toEqual({ attention: { sounds: {} } })
  39. })
  40. test("resolves host-neutral defaults", () => {
  41. const config = resolve({}, { terminalSuspend: true })
  42. expect(config.attention).toEqual({
  43. enabled: false,
  44. notifications: true,
  45. sound: true,
  46. volume: 0.4,
  47. sound_pack: "opencode.default",
  48. sounds: {},
  49. })
  50. expect(config.leader_timeout).toBe(LeaderTimeoutDefault)
  51. expect(config.mouse).toBe(true)
  52. expect(config.keybinds.has("terminal.suspend")).toBe(true)
  53. expect(config.keybinds.has("session.list")).toBe(true)
  54. })
  55. test("resolves overrides without mutating input", () => {
  56. const input: TuiConfigInfo = {
  57. theme: "custom",
  58. mouse: false,
  59. leader_timeout: 750,
  60. attention: {
  61. enabled: true,
  62. notifications: false,
  63. sound: false,
  64. volume: 0.8,
  65. sound_pack: "custom.pack",
  66. sounds: { question: "/sounds/question.wav" },
  67. },
  68. keybinds: { session_list: "ctrl+l" },
  69. }
  70. const config = resolve(input, { terminalSuspend: true })
  71. expect(config).toMatchObject({ theme: "custom", mouse: false, leader_timeout: 750, attention: input.attention })
  72. expect(config.keybinds.get("session.list")).toHaveLength(1)
  73. expect(input.keybinds).toEqual({ session_list: "ctrl+l" })
  74. })
  75. test("resolves a session move keybind", () => {
  76. const config = resolve({ keybinds: { session_move: "ctrl+o" } }, { terminalSuspend: true })
  77. expect(config.keybinds.get("session.move")).toMatchObject([{ key: "ctrl+o" }])
  78. })
  79. test("disables suspend and assigns ctrl+z to undo when unsupported", () => {
  80. const config = resolve({}, { terminalSuspend: false })
  81. expect(config.keybinds.has("terminal.suspend")).toBe(false)
  82. expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+z,ctrl+-,super+z" }])
  83. })
  84. test("preserves an explicit undo binding when suspend is unsupported", () => {
  85. const config = resolve({ keybinds: { input_undo: "ctrl+u", terminal_suspend: "ctrl+s" } }, { terminalSuspend: false })
  86. expect(config.keybinds.has("terminal.suspend")).toBe(false)
  87. expect(config.keybinds.get("input.undo")).toHaveLength(1)
  88. expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+u" }])
  89. })
  90. test("provides resolved config through Solid context", async () => {
  91. const config = resolve({ theme: "custom" }, { terminalSuspend: true })
  92. function Consumer() {
  93. const value = useTuiConfig()
  94. return <text>{`${value.theme} ${value.mouse} ${value.leader_timeout}`}</text>
  95. }
  96. const app = await testRender(() => (
  97. <TuiConfigProvider config={config}>
  98. <Consumer />
  99. </TuiConfigProvider>
  100. ))
  101. try {
  102. await app.renderOnce()
  103. expect(app.captureCharFrame()).toContain(`custom true ${LeaderTimeoutDefault}`)
  104. } finally {
  105. app.renderer.destroy()
  106. }
  107. })
  108. test("requires the config provider", () => {
  109. expect(() => useTuiConfig()).toThrow("TuiConfigProvider is missing")
  110. })