config-v2.test.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /** @jsxImportSource @opentui/solid */
  2. import { testRender } from "@opentui/solid"
  3. import { expect, test } from "bun:test"
  4. import { Schema } from "effect"
  5. import { resolve, ConfigProvider, Info, useConfig, type Interface } from "../src/config"
  6. import { settings } from "../src/component/dialog-config"
  7. import { TuiKeybind } from "../src/config/keybind"
  8. import { CommandMap, Definitions } from "../src/config/v1/keybind"
  9. const decodeInfo = Schema.decodeUnknownSync(Info)
  10. test("validates mini replay settings", () => {
  11. expect(decodeInfo({ mini: { replay: false, replay_limit: 50 } })).toEqual({
  12. mini: { replay: false, replay_limit: 50 },
  13. })
  14. expect(() => decodeInfo({ mini: { replay_limit: 0 } })).toThrow()
  15. expect(() => decodeInfo({ mini: { replay_limit: 1.5 } })).toThrow()
  16. })
  17. test("validates the session tabs setting", () => {
  18. const decode = Schema.decodeUnknownSync(Info)
  19. expect(decode({ tabs: { enabled: true, layout: "vertical" } })).toEqual({
  20. tabs: { enabled: true, layout: "vertical" },
  21. })
  22. expect(() => decode({ tabs: { layout: true } })).toThrow()
  23. expect(() => decode({ tabs: { enabled: "on" } })).toThrow()
  24. expect(decode({ prompt: { image_preview: true } })).toEqual({ prompt: { image_preview: true } })
  25. expect(decode({ session: { image_preview: true } })).toEqual({ session: { image_preview: true } })
  26. expect(decode({ session: { new_location: "inherit" } })).toEqual({ session: { new_location: "inherit" } })
  27. expect(() => decode({ session: { new_location: "current" } })).toThrow()
  28. })
  29. test("resolves nested config and keybind defaults", () => {
  30. const config = resolve(
  31. {
  32. keybinds: { leader: "ctrl+o" },
  33. leader: { timeout: 500 },
  34. scroll: { speed: 2, acceleration: true },
  35. diffs: { view: "split" },
  36. debug: { devtools: true },
  37. },
  38. { terminalSuspend: true },
  39. )
  40. expect(config.leader.timeout).toBe(500)
  41. expect(config.keybinds.get("leader")?.[0]?.key).toBe("ctrl+o")
  42. expect(config.scroll).toEqual({ speed: 2, acceleration: true })
  43. expect(config.diffs).toEqual({ view: "split" })
  44. expect(config.debug).toEqual({ devtools: true })
  45. expect(config.tabs).toEqual({ enabled: true, scope: "cwd", layout: "horizontal" })
  46. expect(config.session.new_location).toBe("launch")
  47. })
  48. test("shows resolved tab defaults in settings", () => {
  49. expect(settings.find((setting) => setting.path.join(".") === "tabs.enabled")?.default).toBe(true)
  50. expect(settings.find((setting) => setting.path.join(".") === "tabs.scope")?.default).toBe("cwd")
  51. expect(settings.find((setting) => setting.path.join(".") === "tabs.layout")?.default).toBe("horizontal")
  52. })
  53. test("shows the new session location default in settings", () => {
  54. expect(settings.find((setting) => setting.path.join(".") === "session.new_location")?.default).toBe("launch")
  55. })
  56. test("uses command IDs as keybind keys", () => {
  57. const config = resolve({ keybinds: { "session.list": "ctrl+l" } }, { terminalSuspend: true })
  58. expect(config.keybinds.get("session.list")).toMatchObject([{ key: "ctrl+l" }])
  59. expect(TuiKeybind.unknownKeys({ session_list: "ctrl+l" })).toEqual(["session_list"])
  60. expect(
  61. Object.keys(TuiKeybind.Definitions)
  62. .filter((key) => key !== "leader")
  63. .every((key) => key.includes(".")),
  64. ).toBe(true)
  65. })
  66. test("preserves current navigation defaults", () => {
  67. const config = resolve({}, { terminalSuspend: true })
  68. expect(config.keybinds.get("open.menu")).toMatchObject([{ key: "ctrl+o" }])
  69. expect(config.keybinds.get("session.tab.next")).toMatchObject([{ key: "ctrl+tab,alt+down" }])
  70. expect(config.keybinds.get("session.tab.previous")).toMatchObject([{ key: "ctrl+shift+tab,alt+up" }])
  71. expect(config.keybinds.get("session.tab.next_unread")).toMatchObject([{ key: "alt+shift+down" }])
  72. expect(config.keybinds.get("session.tab.previous_unread")).toMatchObject([{ key: "alt+shift+up" }])
  73. expect(config.keybinds.get("session.tab.reopen")).toMatchObject([{ key: "ctrl+shift+t" }])
  74. expect(config.keybinds.get("session.tab.select.10")).toMatchObject([{ key: "<leader>0,ctrl+0" }])
  75. expect(config.keybinds.get("session.message.next")).toEqual([])
  76. expect(config.keybinds.get("session.message.previous")).toEqual([])
  77. expect(config.keybinds.get("session.message.user.next")).toEqual([])
  78. expect(config.keybinds.get("session.message.user.previous")).toEqual([])
  79. expect(config.keybinds.get("input.buffer.home")).toEqual([])
  80. expect(config.keybinds.get("input.buffer.end")).toEqual([])
  81. expect(config.keybinds.get("prompt.images.view")).toMatchObject([{ key: "<leader>i" }])
  82. })
  83. test("preserves migrated v1 keybind defaults", () => {
  84. const pairs = [
  85. ["app.exit", "app_exit"],
  86. ["prompt.paste", "input_paste"],
  87. ["session.delete", "session_delete"],
  88. ["session.list", "session_list"],
  89. ["agent.list", "agent_list"],
  90. ] as const
  91. pairs.forEach(([command, name]) => {
  92. expect(CommandMap[name]).toBe(command)
  93. expect(TuiKeybind.Definitions[command].default).toEqual(Definitions[name].default)
  94. })
  95. })
  96. test("accepts every v2-only named command ID", () => {
  97. const commands = [
  98. "server.pair",
  99. "session.toggle.exploration_grouping",
  100. "composer.subagent.up",
  101. "composer.subagent.down",
  102. "composer.subagent.select",
  103. "composer.subagent.interrupt",
  104. "composer.shell.up",
  105. "composer.shell.down",
  106. "composer.shell.kill",
  107. "diff.down",
  108. "diff.up",
  109. "diff.page.down",
  110. "diff.page.up",
  111. "diff.mark_reviewed",
  112. "opencode.settings",
  113. "service.restart",
  114. "permission.mode",
  115. "session.cd",
  116. "app.scrap",
  117. ]
  118. const config = resolve(
  119. decodeInfo({ keybinds: Object.fromEntries(commands.map((command) => [command, "ctrl+alt+z"])) }),
  120. { terminalSuspend: true },
  121. )
  122. commands.forEach((command) => expect(config.keybinds.get(command)).toMatchObject([{ key: "ctrl+alt+z" }]))
  123. })
  124. test("centralizes named command defaults and resolves explicit none", () => {
  125. const defaults = {
  126. "composer.subagent.up": "up",
  127. "composer.subagent.down": "down",
  128. "composer.subagent.select": "return",
  129. "composer.subagent.interrupt": "ctrl+d",
  130. "composer.shell.up": "up",
  131. "composer.shell.down": "down",
  132. "composer.shell.kill": "ctrl+d",
  133. "diff.down": "j,down",
  134. "diff.up": "k,up",
  135. "diff.page.down": "pagedown,ctrl+f",
  136. "diff.page.up": "pageup,ctrl+b",
  137. "diff.mark_reviewed": "m",
  138. }
  139. const config = resolve({}, { terminalSuspend: true })
  140. Object.entries(defaults).forEach(([command, key]) => expect(config.keybinds.get(command)).toMatchObject([{ key }]))
  141. const disabled = resolve(
  142. decodeInfo({ keybinds: Object.fromEntries(Object.keys(defaults).map((command) => [command, "none"])) }),
  143. { terminalSuspend: true },
  144. )
  145. Object.keys(defaults).forEach((command) => expect(disabled.keybinds.get(command)).toEqual([]))
  146. })
  147. test("rejects orphaned keybind definitions", () => {
  148. expect(decodeInfo({ keybinds: { "app.heap_snapshot": "ctrl+h" } })).toEqual({ keybinds: {} })
  149. })
  150. test("uses ctrl+z for input undo when terminal suspend is unavailable", () => {
  151. const config = resolve({}, { terminalSuspend: false })
  152. expect(config.keybinds.has("terminal.suspend")).toBe(false)
  153. expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+z,ctrl+-,super+z" }])
  154. const overridden = resolve(
  155. { keybinds: { "terminal.suspend": "ctrl+s", "input.undo": "ctrl+u" } },
  156. { terminalSuspend: false },
  157. )
  158. expect(overridden.keybinds.has("terminal.suspend")).toBe(false)
  159. expect(overridden.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+u" }])
  160. })
  161. test("keeps turn token usage inside developer tools", () => {
  162. expect(settings.find((setting) => setting.path.join(".") === "debug.devtools")?.title).toBe("Developer tools")
  163. expect(settings.some((setting) => setting.path.join(".") === "debug.turn_tokens")).toBe(false)
  164. })
  165. test("provides config and its host interface", async () => {
  166. const config = resolve({}, { terminalSuspend: true })
  167. let current = {}
  168. const service: Interface = {
  169. get: async () => current,
  170. update: async (update) => {
  171. const draft: Record<string, any> = { ...current }
  172. update(draft)
  173. current = draft
  174. return draft
  175. },
  176. }
  177. let context: ReturnType<typeof useConfig> | undefined
  178. function Consumer() {
  179. context = useConfig()
  180. return <text>{`${context.data.mouse ? "mouse" : "none"} ${context.data.keybinds.get("leader")?.[0]?.key}`}</text>
  181. }
  182. const app = await testRender(() => (
  183. <ConfigProvider config={config} service={service}>
  184. <Consumer />
  185. </ConfigProvider>
  186. ))
  187. try {
  188. await app.renderOnce()
  189. expect(app.captureCharFrame()).toContain("mouse ctrl+x")
  190. if (!context) throw new Error("Config context was not provided")
  191. await context.update((draft) => {
  192. draft.mouse = false
  193. draft.keybinds = { leader: "ctrl+o" }
  194. })
  195. await app.renderOnce()
  196. expect(app.captureCharFrame()).toContain("none ctrl+o")
  197. } finally {
  198. app.renderer.destroy()
  199. }
  200. })