tui.test.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. import { expect } from "bun:test"
  2. import path from "path"
  3. import { pathToFileURL } from "url"
  4. import { Effect, Layer } from "effect"
  5. import { AppFileSystem } from "@opencode-ai/core/filesystem"
  6. import { Global } from "@opencode-ai/core/global"
  7. import { Config } from "@/config/config"
  8. import { ConfigPlugin } from "@/config/plugin"
  9. import { CurrentWorkingDirectory } from "@/cli/cmd/tui/config/cwd"
  10. import { TuiConfig } from "../../src/cli/cmd/tui/config/tui"
  11. import { TestInstance } from "../fixture/fixture"
  12. import { testEffect } from "../lib/effect"
  13. const it = testEffect(Layer.mergeAll(Config.defaultLayer, AppFileSystem.defaultLayer))
  14. const winIt = process.platform === "win32" ? it.instance : it.instance.skip
  15. const globalConfigFiles = ["opencode.json", "opencode.jsonc", "tui.json", "tui.jsonc"].map((file) =>
  16. path.join(Global.Path.config, file),
  17. )
  18. const cleanState = Effect.gen(function* () {
  19. const fs = yield* AppFileSystem.Service
  20. delete process.env.OPENCODE_CONFIG
  21. delete process.env.OPENCODE_TUI_CONFIG
  22. yield* Effect.forEach(globalConfigFiles, (file) => fs.remove(file, { force: true }).pipe(Effect.ignore), {
  23. discard: true,
  24. })
  25. })
  26. const withCleanState = <A, E, R>(self: Effect.Effect<A, E, R>) =>
  27. Effect.acquireUseRelease(
  28. cleanState,
  29. () => self,
  30. () => cleanState,
  31. )
  32. const withEnv = <A, E, R>(name: string, value: string | undefined, self: Effect.Effect<A, E, R>) =>
  33. Effect.acquireUseRelease(
  34. Effect.sync(() => {
  35. const previous = process.env[name]
  36. if (value === undefined) delete process.env[name]
  37. else process.env[name] = value
  38. return previous
  39. }),
  40. () => self,
  41. (previous) =>
  42. Effect.sync(() => {
  43. if (previous === undefined) delete process.env[name]
  44. else process.env[name] = previous
  45. }),
  46. )
  47. const withPlatform = <A, E, R>(platform: typeof process.platform, self: Effect.Effect<A, E, R>) =>
  48. Effect.acquireUseRelease(
  49. Effect.sync(() => {
  50. const original = Object.getOwnPropertyDescriptor(process, "platform")
  51. Object.defineProperty(process, "platform", {
  52. ...original,
  53. value: platform,
  54. })
  55. return original
  56. }),
  57. () => self,
  58. (original) =>
  59. Effect.sync(() => {
  60. if (original) Object.defineProperty(process, "platform", original)
  61. }),
  62. )
  63. const getTuiConfig = (directory: string) =>
  64. TuiConfig.Service.use((svc) => svc.get()).pipe(
  65. Effect.provide(TuiConfig.defaultLayer.pipe(Layer.provide(Layer.succeed(CurrentWorkingDirectory, directory)))),
  66. )
  67. it.instance("keeps server and tui plugin merge semantics aligned", () =>
  68. withCleanState(
  69. Effect.gen(function* () {
  70. const fs = yield* AppFileSystem.Service
  71. const test = yield* TestInstance
  72. const local = path.join(test.directory, ".opencode")
  73. yield* fs.makeDirectory(local, { recursive: true })
  74. yield* fs.writeJson(path.join(Global.Path.config, "opencode.json"), {
  75. plugin: [["shared-plugin@1.0.0", { source: "global" }], "global-only@1.0.0"],
  76. })
  77. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), {
  78. plugin: [["shared-plugin@1.0.0", { source: "global" }], "global-only@1.0.0"],
  79. })
  80. yield* fs.writeJson(path.join(local, "opencode.json"), {
  81. plugin: [["shared-plugin@2.0.0", { source: "local" }], "local-only@1.0.0"],
  82. })
  83. yield* fs.writeJson(path.join(local, "tui.json"), {
  84. plugin: [["shared-plugin@2.0.0", { source: "local" }], "local-only@1.0.0"],
  85. })
  86. const server = yield* Config.Service.use((svc) => svc.get())
  87. const tui = yield* getTuiConfig(test.directory)
  88. const serverPlugins = (server.plugin ?? []).map((item) => ConfigPlugin.pluginSpecifier(item))
  89. const tuiPlugins = (tui.plugin ?? []).map((item) => ConfigPlugin.pluginSpecifier(item))
  90. expect(serverPlugins).toEqual(tuiPlugins)
  91. expect(serverPlugins).toContain("shared-plugin@2.0.0")
  92. expect(serverPlugins).not.toContain("shared-plugin@1.0.0")
  93. const serverOrigins = server.plugin_origins ?? []
  94. const tuiOrigins = tui.plugin_origins ?? []
  95. expect(serverOrigins.map((item) => ConfigPlugin.pluginSpecifier(item.spec))).toEqual(serverPlugins)
  96. expect(tuiOrigins.map((item) => ConfigPlugin.pluginSpecifier(item.spec))).toEqual(tuiPlugins)
  97. expect(serverOrigins.map((item) => item.scope)).toEqual(tuiOrigins.map((item) => item.scope))
  98. }),
  99. ),
  100. )
  101. it.instance("loads tui config with the same precedence order as server config paths", () =>
  102. withCleanState(
  103. Effect.gen(function* () {
  104. const fs = yield* AppFileSystem.Service
  105. const test = yield* TestInstance
  106. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), { theme: "global" })
  107. yield* fs.writeJson(path.join(test.directory, "tui.json"), { theme: "project" })
  108. yield* fs.writeWithDirs(
  109. path.join(test.directory, ".opencode", "tui.json"),
  110. JSON.stringify({ theme: "local", diff_style: "stacked" }, null, 2),
  111. )
  112. const config = yield* getTuiConfig(test.directory)
  113. expect(config.theme).toBe("local")
  114. expect(config.diff_style).toBe("stacked")
  115. }),
  116. ),
  117. )
  118. it.instance("resolves attention config defaults and overrides", () =>
  119. withCleanState(
  120. Effect.gen(function* () {
  121. const fs = yield* AppFileSystem.Service
  122. const test = yield* TestInstance
  123. expect((yield* getTuiConfig(test.directory)).attention).toEqual({
  124. enabled: false,
  125. notifications: true,
  126. sound: true,
  127. volume: 0.4,
  128. sound_pack: "opencode.default",
  129. sounds: {},
  130. })
  131. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  132. attention: {
  133. enabled: false,
  134. notifications: false,
  135. sound: false,
  136. volume: 0.7,
  137. sound_pack: "acme.soft",
  138. sounds: {
  139. default: path.join(test.directory, "default.mp3"),
  140. question: pathToFileURL(path.join(test.directory, "question.mp3")).href,
  141. error: "./error.mp3",
  142. subagent_done: "./subagent-done.mp3",
  143. },
  144. },
  145. })
  146. expect((yield* getTuiConfig(test.directory)).attention).toEqual({
  147. enabled: false,
  148. notifications: false,
  149. sound: false,
  150. volume: 0.7,
  151. sound_pack: "acme.soft",
  152. sounds: {
  153. default: path.join(test.directory, "default.mp3"),
  154. question: path.join(test.directory, "question.mp3"),
  155. error: path.join(test.directory, "error.mp3"),
  156. subagent_done: path.join(test.directory, "subagent-done.mp3"),
  157. },
  158. })
  159. }),
  160. ),
  161. )
  162. it.instance("migrates tui-specific keys from opencode.json when tui.json does not exist", () =>
  163. withCleanState(
  164. Effect.gen(function* () {
  165. const fs = yield* AppFileSystem.Service
  166. const test = yield* TestInstance
  167. const source = path.join(test.directory, "opencode.json")
  168. yield* fs.writeJson(source, {
  169. theme: "migrated-theme",
  170. tui: { scroll_speed: 5 },
  171. keybinds: { app_exit: "ctrl+q" },
  172. })
  173. const config = yield* getTuiConfig(test.directory)
  174. expect(config.theme).toBe("migrated-theme")
  175. expect(config.scroll_speed).toBe(5)
  176. expect(config.keybinds.get("app.exit")?.[0]?.key).toBe("ctrl+q")
  177. expect(JSON.parse(yield* fs.readFileString(path.join(test.directory, "tui.json")))).toMatchObject({
  178. theme: "migrated-theme",
  179. scroll_speed: 5,
  180. })
  181. const server = JSON.parse(yield* fs.readFileString(source))
  182. expect(server.theme).toBeUndefined()
  183. expect(server.keybinds).toBeUndefined()
  184. expect(server.tui).toBeUndefined()
  185. expect(yield* fs.existsSafe(path.join(test.directory, "opencode.json.tui-migration.bak"))).toBe(true)
  186. expect(yield* fs.existsSafe(path.join(test.directory, "tui.json"))).toBe(true)
  187. }),
  188. ),
  189. )
  190. it.instance("migrates project legacy tui keys even when global tui.json already exists", () =>
  191. withCleanState(
  192. Effect.gen(function* () {
  193. const fs = yield* AppFileSystem.Service
  194. const test = yield* TestInstance
  195. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), { theme: "global" })
  196. yield* fs.writeJson(path.join(test.directory, "opencode.json"), {
  197. theme: "project-migrated",
  198. tui: { scroll_speed: 2 },
  199. })
  200. const config = yield* getTuiConfig(test.directory)
  201. expect(config.theme).toBe("project-migrated")
  202. expect(config.scroll_speed).toBe(2)
  203. expect(yield* fs.existsSafe(path.join(test.directory, "tui.json"))).toBe(true)
  204. const server = JSON.parse(yield* fs.readFileString(path.join(test.directory, "opencode.json")))
  205. expect(server.theme).toBeUndefined()
  206. expect(server.tui).toBeUndefined()
  207. }),
  208. ),
  209. )
  210. it.instance("drops unknown legacy tui keys during migration", () =>
  211. withCleanState(
  212. Effect.gen(function* () {
  213. const fs = yield* AppFileSystem.Service
  214. const test = yield* TestInstance
  215. yield* fs.writeJson(path.join(test.directory, "opencode.json"), {
  216. theme: "migrated-theme",
  217. tui: { scroll_speed: 2, foo: 1 },
  218. })
  219. const config = yield* getTuiConfig(test.directory)
  220. expect(config.theme).toBe("migrated-theme")
  221. expect(config.scroll_speed).toBe(2)
  222. const migrated = JSON.parse(yield* fs.readFileString(path.join(test.directory, "tui.json")))
  223. expect(migrated.scroll_speed).toBe(2)
  224. expect(migrated.foo).toBeUndefined()
  225. }),
  226. ),
  227. )
  228. it.instance("skips migration when opencode.jsonc is syntactically invalid", () =>
  229. withCleanState(
  230. Effect.gen(function* () {
  231. const fs = yield* AppFileSystem.Service
  232. const test = yield* TestInstance
  233. yield* fs.writeFileString(
  234. path.join(test.directory, "opencode.jsonc"),
  235. `{
  236. "theme": "broken-theme",
  237. "tui": { "scroll_speed": 2 }
  238. "username": "still-broken"
  239. }`,
  240. )
  241. const config = yield* getTuiConfig(test.directory)
  242. expect(config.theme).toBeUndefined()
  243. expect(config.scroll_speed).toBeUndefined()
  244. expect(yield* fs.existsSafe(path.join(test.directory, "tui.json"))).toBe(false)
  245. expect(yield* fs.existsSafe(path.join(test.directory, "opencode.jsonc.tui-migration.bak"))).toBe(false)
  246. const source = yield* fs.readFileString(path.join(test.directory, "opencode.jsonc"))
  247. expect(source).toContain('"theme": "broken-theme"')
  248. expect(source).toContain('"tui": { "scroll_speed": 2 }')
  249. }),
  250. ),
  251. )
  252. it.instance("skips migration when tui.json already exists", () =>
  253. withCleanState(
  254. Effect.gen(function* () {
  255. const fs = yield* AppFileSystem.Service
  256. const test = yield* TestInstance
  257. yield* fs.writeJson(path.join(test.directory, "opencode.json"), { theme: "legacy" })
  258. yield* fs.writeJson(path.join(test.directory, "tui.json"), { diff_style: "stacked" })
  259. const config = yield* getTuiConfig(test.directory)
  260. expect(config.diff_style).toBe("stacked")
  261. expect(config.theme).toBeUndefined()
  262. const server = JSON.parse(yield* fs.readFileString(path.join(test.directory, "opencode.json")))
  263. expect(server.theme).toBe("legacy")
  264. expect(yield* fs.existsSafe(path.join(test.directory, "opencode.json.tui-migration.bak"))).toBe(false)
  265. }),
  266. ),
  267. )
  268. it.instance("continues loading tui config when legacy source cannot be stripped", () =>
  269. withCleanState(
  270. Effect.gen(function* () {
  271. const fs = yield* AppFileSystem.Service
  272. const test = yield* TestInstance
  273. const source = path.join(test.directory, "opencode.json")
  274. yield* fs.writeJson(source, { theme: "readonly-theme" })
  275. yield* Effect.acquireUseRelease(
  276. fs.chmod(source, 0o444),
  277. () =>
  278. Effect.gen(function* () {
  279. const config = yield* getTuiConfig(test.directory)
  280. expect(config.theme).toBe("readonly-theme")
  281. expect(yield* fs.existsSafe(path.join(test.directory, "tui.json"))).toBe(true)
  282. const server = JSON.parse(yield* fs.readFileString(source))
  283. expect(server.theme).toBe("readonly-theme")
  284. }),
  285. () => fs.chmod(source, 0o644).pipe(Effect.ignore),
  286. )
  287. }),
  288. ),
  289. )
  290. it.instance("migration backup preserves JSONC comments", () =>
  291. withCleanState(
  292. Effect.gen(function* () {
  293. const fs = yield* AppFileSystem.Service
  294. const test = yield* TestInstance
  295. yield* fs.writeFileString(
  296. path.join(test.directory, "opencode.jsonc"),
  297. `{
  298. // top-level comment
  299. "theme": "jsonc-theme",
  300. "tui": {
  301. // nested comment
  302. "scroll_speed": 1.5
  303. }
  304. }`,
  305. )
  306. yield* getTuiConfig(test.directory)
  307. const backup = yield* fs.readFileString(path.join(test.directory, "opencode.jsonc.tui-migration.bak"))
  308. expect(backup).toContain("// top-level comment")
  309. expect(backup).toContain("// nested comment")
  310. expect(backup).toContain('"theme": "jsonc-theme"')
  311. expect(backup).toContain('"scroll_speed": 1.5')
  312. }),
  313. ),
  314. )
  315. it.instance("migrates legacy tui keys across multiple opencode.json levels", () =>
  316. withCleanState(
  317. Effect.gen(function* () {
  318. const fs = yield* AppFileSystem.Service
  319. const test = yield* TestInstance
  320. const nested = path.join(test.directory, "apps", "client")
  321. yield* fs.makeDirectory(nested, { recursive: true })
  322. yield* fs.writeJson(path.join(test.directory, "opencode.json"), { theme: "root-theme" })
  323. yield* fs.writeJson(path.join(nested, "opencode.json"), { theme: "nested-theme" })
  324. const config = yield* getTuiConfig(nested)
  325. expect(config.theme).toBe("nested-theme")
  326. expect(yield* fs.existsSafe(path.join(test.directory, "tui.json"))).toBe(true)
  327. expect(yield* fs.existsSafe(path.join(nested, "tui.json"))).toBe(true)
  328. }),
  329. ),
  330. )
  331. it.instance("flattens nested tui key inside tui.json", () =>
  332. withCleanState(
  333. Effect.gen(function* () {
  334. const fs = yield* AppFileSystem.Service
  335. const test = yield* TestInstance
  336. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  337. theme: "outer",
  338. tui: { scroll_speed: 3, diff_style: "stacked" },
  339. })
  340. const config = yield* getTuiConfig(test.directory)
  341. expect(config.scroll_speed).toBe(3)
  342. expect(config.diff_style).toBe("stacked")
  343. expect(config.theme).toBe("outer")
  344. }),
  345. ),
  346. )
  347. it.instance("top-level keys in tui.json take precedence over nested tui key", () =>
  348. withCleanState(
  349. Effect.gen(function* () {
  350. const fs = yield* AppFileSystem.Service
  351. const test = yield* TestInstance
  352. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  353. diff_style: "auto",
  354. tui: { diff_style: "stacked", scroll_speed: 2 },
  355. })
  356. const config = yield* getTuiConfig(test.directory)
  357. expect(config.diff_style).toBe("auto")
  358. expect(config.scroll_speed).toBe(2)
  359. }),
  360. ),
  361. )
  362. it.instance("project config takes precedence over OPENCODE_TUI_CONFIG (matches OPENCODE_CONFIG)", () =>
  363. withCleanState(
  364. Effect.gen(function* () {
  365. const fs = yield* AppFileSystem.Service
  366. const test = yield* TestInstance
  367. const custom = path.join(test.directory, "custom-tui.json")
  368. yield* fs.writeJson(path.join(test.directory, "tui.json"), { theme: "project", diff_style: "auto" })
  369. yield* fs.writeJson(custom, { theme: "custom", diff_style: "stacked" })
  370. yield* withEnv(
  371. "OPENCODE_TUI_CONFIG",
  372. custom,
  373. Effect.gen(function* () {
  374. const config = yield* getTuiConfig(test.directory)
  375. expect(config.theme).toBe("project")
  376. expect(config.diff_style).toBe("auto")
  377. }),
  378. )
  379. }),
  380. ),
  381. )
  382. it.instance("merges keybind overrides across precedence layers", () =>
  383. withCleanState(
  384. Effect.gen(function* () {
  385. const fs = yield* AppFileSystem.Service
  386. const test = yield* TestInstance
  387. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), { keybinds: { app_exit: "ctrl+q" } })
  388. yield* fs.writeJson(path.join(test.directory, "tui.json"), { keybinds: { theme_list: "ctrl+k" } })
  389. const config = yield* getTuiConfig(test.directory)
  390. expect(config.keybinds.get("app.exit")?.[0]?.key).toBe("ctrl+q")
  391. expect(config.keybinds.get("theme.switch")?.[0]?.key).toBe("ctrl+k")
  392. }),
  393. ),
  394. )
  395. it.instance("ignores unknown keybind names without dropping valid overrides from the same file", () =>
  396. withCleanState(
  397. Effect.gen(function* () {
  398. const fs = yield* AppFileSystem.Service
  399. const test = yield* TestInstance
  400. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), {
  401. keybinds: {
  402. session_delete: "ctrl+d",
  403. not_a_real_keybind: "ctrl+q",
  404. },
  405. })
  406. const config = yield* getTuiConfig(test.directory)
  407. expect(config.keybinds.get("session.delete")?.[0]?.key).toBe("ctrl+d")
  408. expect(config.keybinds.get("not_a_real_keybind")).toEqual([])
  409. }),
  410. ),
  411. )
  412. it.instance("resolves keybind lookup from canonical keybinds", () =>
  413. withCleanState(
  414. Effect.gen(function* () {
  415. const fs = yield* AppFileSystem.Service
  416. const test = yield* TestInstance
  417. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  418. keybinds: {
  419. leader: { key: { name: "g", ctrl: true } },
  420. command_list: "alt+p",
  421. which_key_toggle: "alt+k",
  422. editor_open: "ctrl+e",
  423. "prompt.autocomplete.next": "ctrl+j",
  424. "dialog.prompt.submit": "ctrl+s",
  425. "dialog.mcp.toggle": "ctrl+t",
  426. model_favorite_toggle: "ctrl+f",
  427. "dialog.plugins.install": "shift+i",
  428. },
  429. leader_timeout: 1234,
  430. })
  431. const config = yield* getTuiConfig(test.directory)
  432. expect(config.keybinds.get("leader")?.[0]?.key).toEqual({ name: "g", ctrl: true })
  433. expect(config.leader_timeout).toBe(1234)
  434. expect(config.keybinds.get("command.palette.show")?.[0]?.key).toBe("alt+p")
  435. expect(config.keybinds.get("session.new")?.[0]?.key).toBe("<leader>n")
  436. expect(config.keybinds.get("which-key.toggle")?.[0]?.key).toBe("alt+k")
  437. expect(config.keybinds.get("which-key.layout.toggle")?.[0]?.key).toBe("ctrl+alt+shift+k")
  438. expect(config.keybinds.get("which-key.pending.toggle")?.[0]?.key).toBe("ctrl+alt+shift+p")
  439. expect(config.keybinds.get("which-key.group.next")?.[0]?.key).toBe("ctrl+alt+right,ctrl+alt+]")
  440. expect((config.keybinds.get("which-key.toggle")?.[0] as { desc?: unknown } | undefined)?.desc).toBe(
  441. "Toggle which-key panel",
  442. )
  443. expect(config.keybinds.get("prompt.editor")?.[0]?.key).toBe("ctrl+e")
  444. expect(config.keybinds.get("prompt.autocomplete.next")?.[0]?.key).toBe("ctrl+j")
  445. expect(config.keybinds.get("dialog.prompt.submit")?.[0]?.key).toBe("ctrl+s")
  446. expect(config.keybinds.get("dialog.mcp.toggle")?.[0]?.key).toBe("ctrl+t")
  447. expect(config.keybinds.get("model.dialog.favorite")?.[0]?.key).toBe("ctrl+f")
  448. expect(config.keybinds.get("dialog.plugins.install")?.[0]?.key).toBe("shift+i")
  449. expect(
  450. config.keybinds.gather("plugins.dialog", ["dialog.plugins.install"]).map((binding) => binding.cmd),
  451. ).toEqual(["dialog.plugins.install"])
  452. }),
  453. ),
  454. )
  455. it.instance("keybinds accept OpenTUI binding specs", () =>
  456. withCleanState(
  457. Effect.gen(function* () {
  458. const fs = yield* AppFileSystem.Service
  459. const test = yield* TestInstance
  460. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  461. keybinds: {
  462. command_list: [{ key: "alt+p", preventDefault: false }],
  463. editor_open: { key: { name: "e", ctrl: true }, group: "Explicit" },
  464. "prompt.autocomplete.next": false,
  465. plugin_manager: "ctrl+shift+p",
  466. },
  467. })
  468. const config = yield* getTuiConfig(test.directory)
  469. expect(config.keybinds.get("command.palette.show")).toEqual([
  470. { key: "alt+p", cmd: "command.palette.show", preventDefault: false, desc: "List available commands" },
  471. ])
  472. expect(config.keybinds.get("prompt.editor")?.[0]).toMatchObject({
  473. key: { name: "e", ctrl: true },
  474. cmd: "prompt.editor",
  475. group: "Explicit",
  476. })
  477. expect(config.keybinds.get("prompt.autocomplete.next")).toEqual([])
  478. expect(config.keybinds.get("plugins.list")?.[0]?.key).toBe("ctrl+shift+p")
  479. }),
  480. ),
  481. )
  482. winIt("defaults Ctrl+Z to input undo on Windows", () =>
  483. withCleanState(
  484. Effect.gen(function* () {
  485. const test = yield* TestInstance
  486. const config = yield* getTuiConfig(test.directory)
  487. expect(config.keybinds.get("terminal.suspend")).toEqual([])
  488. expect(config.keybinds.get("input.undo")?.[0]?.key).toBe("ctrl+z,ctrl+-,super+z")
  489. }),
  490. ),
  491. )
  492. winIt("keeps explicit input undo overrides on Windows", () =>
  493. withCleanState(
  494. Effect.gen(function* () {
  495. const fs = yield* AppFileSystem.Service
  496. const test = yield* TestInstance
  497. yield* fs.writeJson(path.join(test.directory, "tui.json"), { keybinds: { input_undo: "ctrl+y" } })
  498. const config = yield* getTuiConfig(test.directory)
  499. expect(config.keybinds.get("terminal.suspend")).toEqual([])
  500. expect(config.keybinds.get("input.undo")?.[0]?.key).toBe("ctrl+y")
  501. }),
  502. ),
  503. )
  504. winIt("ignores terminal suspend bindings on Windows", () =>
  505. withCleanState(
  506. Effect.gen(function* () {
  507. const fs = yield* AppFileSystem.Service
  508. const test = yield* TestInstance
  509. yield* fs.writeJson(path.join(test.directory, "tui.json"), { keybinds: { terminal_suspend: "alt+z" } })
  510. const config = yield* getTuiConfig(test.directory)
  511. expect(config.keybinds.get("terminal.suspend")).toEqual([])
  512. expect(config.keybinds.get("input.undo")?.[0]?.key).toBe("ctrl+z,ctrl+-,super+z")
  513. }),
  514. ),
  515. )
  516. it.instance("applies Windows keybind defaults", () =>
  517. withCleanState(
  518. withPlatform(
  519. "win32",
  520. Effect.gen(function* () {
  521. const test = yield* TestInstance
  522. const config = yield* getTuiConfig(test.directory)
  523. expect(config.keybinds.get("terminal.suspend")).toEqual([])
  524. expect(config.keybinds.get("input.undo")?.[0]?.key).toBe("ctrl+z,ctrl+-,super+z")
  525. }),
  526. ),
  527. ),
  528. )
  529. it.instance("ignores explicit keybind terminal suspend binding on Windows", () =>
  530. withCleanState(
  531. withPlatform(
  532. "win32",
  533. Effect.gen(function* () {
  534. const fs = yield* AppFileSystem.Service
  535. const test = yield* TestInstance
  536. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  537. keybinds: {
  538. terminal_suspend: "alt+z",
  539. },
  540. })
  541. const config = yield* getTuiConfig(test.directory)
  542. expect(config.keybinds.get("terminal.suspend")).toEqual([])
  543. }),
  544. ),
  545. ),
  546. )
  547. it.instance("keeps explicit configured keybind input undo on Windows", () =>
  548. withCleanState(
  549. withPlatform(
  550. "win32",
  551. Effect.gen(function* () {
  552. const fs = yield* AppFileSystem.Service
  553. const test = yield* TestInstance
  554. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  555. keybinds: {
  556. input_undo: "ctrl+y",
  557. },
  558. })
  559. const config = yield* getTuiConfig(test.directory)
  560. expect(config.keybinds.get("input.undo")?.[0]?.key).toBe("ctrl+y")
  561. }),
  562. ),
  563. ),
  564. )
  565. it.instance("OPENCODE_TUI_CONFIG provides settings when no project config exists", () =>
  566. withCleanState(
  567. Effect.gen(function* () {
  568. const fs = yield* AppFileSystem.Service
  569. const test = yield* TestInstance
  570. const custom = path.join(test.directory, "custom-tui.json")
  571. yield* fs.writeJson(custom, { theme: "from-env", diff_style: "stacked" })
  572. yield* withEnv(
  573. "OPENCODE_TUI_CONFIG",
  574. custom,
  575. Effect.gen(function* () {
  576. const config = yield* getTuiConfig(test.directory)
  577. expect(config.theme).toBe("from-env")
  578. expect(config.diff_style).toBe("stacked")
  579. }),
  580. )
  581. }),
  582. ),
  583. )
  584. it.instance("does not derive tui path from OPENCODE_CONFIG", () =>
  585. withCleanState(
  586. Effect.gen(function* () {
  587. const fs = yield* AppFileSystem.Service
  588. const test = yield* TestInstance
  589. const customDir = path.join(test.directory, "custom")
  590. yield* fs.makeDirectory(customDir, { recursive: true })
  591. yield* fs.writeJson(path.join(customDir, "opencode.json"), { model: "test/model" })
  592. yield* fs.writeJson(path.join(customDir, "tui.json"), { theme: "should-not-load" })
  593. yield* withEnv(
  594. "OPENCODE_CONFIG",
  595. path.join(customDir, "opencode.json"),
  596. Effect.gen(function* () {
  597. const config = yield* getTuiConfig(test.directory)
  598. expect(config.theme).toBeUndefined()
  599. }),
  600. )
  601. }),
  602. ),
  603. )
  604. it.instance("applies env and file substitutions in tui.json", () =>
  605. withCleanState(
  606. withEnv(
  607. "TUI_THEME_TEST",
  608. "env-theme",
  609. Effect.gen(function* () {
  610. const fs = yield* AppFileSystem.Service
  611. const test = yield* TestInstance
  612. yield* fs.writeFileString(path.join(test.directory, "keybind.txt"), "ctrl+q")
  613. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  614. theme: "{env:TUI_THEME_TEST}",
  615. keybinds: { app_exit: "{file:keybind.txt}" },
  616. })
  617. const config = yield* getTuiConfig(test.directory)
  618. expect(config.theme).toBe("env-theme")
  619. expect(config.keybinds.get("app.exit")?.[0]?.key).toBe("ctrl+q")
  620. }),
  621. ),
  622. ),
  623. )
  624. it.instance("applies file substitutions when first identical token is in a commented line", () =>
  625. withCleanState(
  626. Effect.gen(function* () {
  627. const fs = yield* AppFileSystem.Service
  628. const test = yield* TestInstance
  629. yield* fs.writeFileString(path.join(test.directory, "theme.txt"), "resolved-theme")
  630. yield* fs.writeFileString(
  631. path.join(test.directory, "tui.jsonc"),
  632. `{
  633. // "theme": "{file:theme.txt}",
  634. "theme": "{file:theme.txt}"
  635. }`,
  636. )
  637. const config = yield* getTuiConfig(test.directory)
  638. expect(config.theme).toBe("resolved-theme")
  639. }),
  640. ),
  641. )
  642. it.instance("loads .opencode/tui.json", () =>
  643. withCleanState(
  644. Effect.gen(function* () {
  645. const fs = yield* AppFileSystem.Service
  646. const test = yield* TestInstance
  647. yield* fs.writeWithDirs(
  648. path.join(test.directory, ".opencode", "tui.json"),
  649. JSON.stringify({ diff_style: "stacked" }, null, 2),
  650. )
  651. const config = yield* getTuiConfig(test.directory)
  652. expect(config.diff_style).toBe("stacked")
  653. }),
  654. ),
  655. )
  656. it.instance("supports tuple plugin specs with options in tui.json", () =>
  657. withCleanState(
  658. Effect.gen(function* () {
  659. const fs = yield* AppFileSystem.Service
  660. const test = yield* TestInstance
  661. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  662. plugin: [["acme-plugin@1.2.3", { enabled: true, label: "demo" }]],
  663. })
  664. const config = yield* getTuiConfig(test.directory)
  665. expect(config.plugin).toEqual([["acme-plugin@1.2.3", { enabled: true, label: "demo" }]])
  666. expect(config.plugin_origins).toEqual([
  667. {
  668. spec: ["acme-plugin@1.2.3", { enabled: true, label: "demo" }],
  669. scope: "local",
  670. source: path.join(test.directory, "tui.json"),
  671. },
  672. ])
  673. }),
  674. ),
  675. )
  676. it.instance("deduplicates tuple plugin specs by name with higher precedence winning", () =>
  677. withCleanState(
  678. Effect.gen(function* () {
  679. const fs = yield* AppFileSystem.Service
  680. const test = yield* TestInstance
  681. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), {
  682. plugin: [["acme-plugin@1.0.0", { source: "global" }]],
  683. })
  684. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  685. plugin: [
  686. ["acme-plugin@2.0.0", { source: "project" }],
  687. ["second-plugin@3.0.0", { source: "project" }],
  688. ],
  689. })
  690. const config = yield* getTuiConfig(test.directory)
  691. expect(config.plugin).toEqual([
  692. ["acme-plugin@2.0.0", { source: "project" }],
  693. ["second-plugin@3.0.0", { source: "project" }],
  694. ])
  695. expect(config.plugin_origins).toEqual([
  696. {
  697. spec: ["acme-plugin@2.0.0", { source: "project" }],
  698. scope: "local",
  699. source: path.join(test.directory, "tui.json"),
  700. },
  701. {
  702. spec: ["second-plugin@3.0.0", { source: "project" }],
  703. scope: "local",
  704. source: path.join(test.directory, "tui.json"),
  705. },
  706. ])
  707. }),
  708. ),
  709. )
  710. it.instance("tracks global and local plugin metadata in merged tui config", () =>
  711. withCleanState(
  712. Effect.gen(function* () {
  713. const fs = yield* AppFileSystem.Service
  714. const test = yield* TestInstance
  715. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), { plugin: ["global-plugin@1.0.0"] })
  716. yield* fs.writeJson(path.join(test.directory, "tui.json"), { plugin: ["local-plugin@2.0.0"] })
  717. const config = yield* getTuiConfig(test.directory)
  718. expect(config.plugin).toEqual(["global-plugin@1.0.0", "local-plugin@2.0.0"])
  719. expect(config.plugin_origins).toEqual([
  720. {
  721. spec: "global-plugin@1.0.0",
  722. scope: "global",
  723. source: path.join(Global.Path.config, "tui.json"),
  724. },
  725. {
  726. spec: "local-plugin@2.0.0",
  727. scope: "local",
  728. source: path.join(test.directory, "tui.json"),
  729. },
  730. ])
  731. }),
  732. ),
  733. )
  734. it.instance("merges plugin_enabled flags across config layers", () =>
  735. withCleanState(
  736. Effect.gen(function* () {
  737. const fs = yield* AppFileSystem.Service
  738. const test = yield* TestInstance
  739. yield* fs.writeJson(path.join(Global.Path.config, "tui.json"), {
  740. plugin_enabled: {
  741. "internal:sidebar-context": false,
  742. "demo.plugin": true,
  743. },
  744. })
  745. yield* fs.writeJson(path.join(test.directory, "tui.json"), {
  746. plugin_enabled: {
  747. "demo.plugin": false,
  748. "local.plugin": true,
  749. },
  750. })
  751. const config = yield* getTuiConfig(test.directory)
  752. expect(config.plugin_enabled).toEqual({
  753. "internal:sidebar-context": false,
  754. "demo.plugin": false,
  755. "local.plugin": true,
  756. })
  757. }),
  758. ),
  759. )
  760. it.instance("silently skips malformed tui.json - load failures degrade to {}", () =>
  761. withCleanState(
  762. Effect.gen(function* () {
  763. const fs = yield* AppFileSystem.Service
  764. const test = yield* TestInstance
  765. yield* fs.writeFileString(path.join(test.directory, "tui.json"), '{ "theme": "broken",')
  766. yield* fs.writeWithDirs(path.join(test.directory, ".opencode", "tui.json"), JSON.stringify({ theme: "fallback" }))
  767. const config = yield* getTuiConfig(test.directory)
  768. expect(config.theme).toBe("fallback")
  769. }),
  770. ),
  771. )
  772. it.instance("silently skips non-ENOENT read failures (e.g. tui.json is a directory) - fallback layer still loads", () =>
  773. withCleanState(
  774. Effect.gen(function* () {
  775. const fs = yield* AppFileSystem.Service
  776. const test = yield* TestInstance
  777. yield* fs.makeDirectory(path.join(test.directory, "tui.json"), { recursive: true })
  778. yield* fs.writeWithDirs(path.join(test.directory, ".opencode", "tui.json"), JSON.stringify({ theme: "fallback" }))
  779. const config = yield* getTuiConfig(test.directory)
  780. expect(config.theme).toBe("fallback")
  781. }),
  782. ),
  783. )
  784. it.instance("missing tui.json - silently treated as empty (ENOENT path)", () =>
  785. withCleanState(
  786. Effect.gen(function* () {
  787. const test = yield* TestInstance
  788. const config = yield* getTuiConfig(test.directory)
  789. expect(config).toBeDefined()
  790. expect(config.theme).toBeUndefined()
  791. }),
  792. ),
  793. )