tui.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. import { afterEach, beforeEach, expect, test } from "bun:test"
  2. import path from "path"
  3. import fs from "fs/promises"
  4. import { tmpdir } from "../fixture/fixture"
  5. import { Instance } from "../../src/project/instance"
  6. import { Config } from "../../src/config"
  7. import { TuiConfig } from "../../src/config"
  8. import { Global } from "../../src/global"
  9. import { Filesystem } from "../../src/util"
  10. import { AppRuntime } from "../../src/effect/app-runtime"
  11. const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
  12. const wintest = process.platform === "win32" ? test : test.skip
  13. const clear = (wait = false) => AppRuntime.runPromise(Config.Service.use((svc) => svc.invalidate(wait)))
  14. const load = () => AppRuntime.runPromise(Config.Service.use((svc) => svc.get()))
  15. beforeEach(async () => {
  16. await clear(true)
  17. })
  18. afterEach(async () => {
  19. delete process.env.OPENCODE_CONFIG
  20. delete process.env.OPENCODE_TUI_CONFIG
  21. await fs.rm(path.join(Global.Path.config, "opencode.json"), { force: true }).catch(() => {})
  22. await fs.rm(path.join(Global.Path.config, "opencode.jsonc"), { force: true }).catch(() => {})
  23. await fs.rm(path.join(Global.Path.config, "tui.json"), { force: true }).catch(() => {})
  24. await fs.rm(path.join(Global.Path.config, "tui.jsonc"), { force: true }).catch(() => {})
  25. await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
  26. await clear(true)
  27. })
  28. test("keeps server and tui plugin merge semantics aligned", async () => {
  29. await using tmp = await tmpdir({
  30. init: async (dir) => {
  31. const local = path.join(dir, ".opencode")
  32. await fs.mkdir(local, { recursive: true })
  33. await Bun.write(
  34. path.join(Global.Path.config, "opencode.json"),
  35. JSON.stringify(
  36. {
  37. plugin: [["shared-plugin@1.0.0", { source: "global" }], "global-only@1.0.0"],
  38. },
  39. null,
  40. 2,
  41. ),
  42. )
  43. await Bun.write(
  44. path.join(Global.Path.config, "tui.json"),
  45. JSON.stringify(
  46. {
  47. plugin: [["shared-plugin@1.0.0", { source: "global" }], "global-only@1.0.0"],
  48. },
  49. null,
  50. 2,
  51. ),
  52. )
  53. await Bun.write(
  54. path.join(local, "opencode.json"),
  55. JSON.stringify(
  56. {
  57. plugin: [["shared-plugin@2.0.0", { source: "local" }], "local-only@1.0.0"],
  58. },
  59. null,
  60. 2,
  61. ),
  62. )
  63. await Bun.write(
  64. path.join(local, "tui.json"),
  65. JSON.stringify(
  66. {
  67. plugin: [["shared-plugin@2.0.0", { source: "local" }], "local-only@1.0.0"],
  68. },
  69. null,
  70. 2,
  71. ),
  72. )
  73. },
  74. })
  75. await Instance.provide({
  76. directory: tmp.path,
  77. fn: async () => {
  78. const server = await load()
  79. const tui = await TuiConfig.get()
  80. const serverPlugins = (server.plugin ?? []).map((item) => Config.pluginSpecifier(item))
  81. const tuiPlugins = (tui.plugin ?? []).map((item) => Config.pluginSpecifier(item))
  82. expect(serverPlugins).toEqual(tuiPlugins)
  83. expect(serverPlugins).toContain("shared-plugin@2.0.0")
  84. expect(serverPlugins).not.toContain("shared-plugin@1.0.0")
  85. const serverOrigins = server.plugin_origins ?? []
  86. const tuiOrigins = tui.plugin_origins ?? []
  87. expect(serverOrigins.map((item) => Config.pluginSpecifier(item.spec))).toEqual(serverPlugins)
  88. expect(tuiOrigins.map((item) => Config.pluginSpecifier(item.spec))).toEqual(tuiPlugins)
  89. expect(serverOrigins.map((item) => item.scope)).toEqual(tuiOrigins.map((item) => item.scope))
  90. },
  91. })
  92. })
  93. test("loads tui config with the same precedence order as server config paths", async () => {
  94. await using tmp = await tmpdir({
  95. init: async (dir) => {
  96. await Bun.write(path.join(Global.Path.config, "tui.json"), JSON.stringify({ theme: "global" }, null, 2))
  97. await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ theme: "project" }, null, 2))
  98. await fs.mkdir(path.join(dir, ".opencode"), { recursive: true })
  99. await Bun.write(
  100. path.join(dir, ".opencode", "tui.json"),
  101. JSON.stringify({ theme: "local", diff_style: "stacked" }, null, 2),
  102. )
  103. },
  104. })
  105. await Instance.provide({
  106. directory: tmp.path,
  107. fn: async () => {
  108. const config = await TuiConfig.get()
  109. expect(config.theme).toBe("local")
  110. expect(config.diff_style).toBe("stacked")
  111. },
  112. })
  113. })
  114. test("migrates tui-specific keys from opencode.json when tui.json does not exist", async () => {
  115. await using tmp = await tmpdir({
  116. init: async (dir) => {
  117. await Bun.write(
  118. path.join(dir, "opencode.json"),
  119. JSON.stringify(
  120. {
  121. theme: "migrated-theme",
  122. tui: { scroll_speed: 5 },
  123. keybinds: { app_exit: "ctrl+q" },
  124. },
  125. null,
  126. 2,
  127. ),
  128. )
  129. },
  130. })
  131. await Instance.provide({
  132. directory: tmp.path,
  133. fn: async () => {
  134. const config = await TuiConfig.get()
  135. expect(config.theme).toBe("migrated-theme")
  136. expect(config.scroll_speed).toBe(5)
  137. expect(config.keybinds?.app_exit).toBe("ctrl+q")
  138. const text = await Filesystem.readText(path.join(tmp.path, "tui.json"))
  139. expect(JSON.parse(text)).toMatchObject({
  140. theme: "migrated-theme",
  141. scroll_speed: 5,
  142. })
  143. const server = JSON.parse(await Filesystem.readText(path.join(tmp.path, "opencode.json")))
  144. expect(server.theme).toBeUndefined()
  145. expect(server.keybinds).toBeUndefined()
  146. expect(server.tui).toBeUndefined()
  147. expect(await Filesystem.exists(path.join(tmp.path, "opencode.json.tui-migration.bak"))).toBe(true)
  148. expect(await Filesystem.exists(path.join(tmp.path, "tui.json"))).toBe(true)
  149. },
  150. })
  151. })
  152. test("migrates project legacy tui keys even when global tui.json already exists", async () => {
  153. await using tmp = await tmpdir({
  154. init: async (dir) => {
  155. await Bun.write(path.join(Global.Path.config, "tui.json"), JSON.stringify({ theme: "global" }, null, 2))
  156. await Bun.write(
  157. path.join(dir, "opencode.json"),
  158. JSON.stringify(
  159. {
  160. theme: "project-migrated",
  161. tui: { scroll_speed: 2 },
  162. },
  163. null,
  164. 2,
  165. ),
  166. )
  167. },
  168. })
  169. await Instance.provide({
  170. directory: tmp.path,
  171. fn: async () => {
  172. const config = await TuiConfig.get()
  173. expect(config.theme).toBe("project-migrated")
  174. expect(config.scroll_speed).toBe(2)
  175. expect(await Filesystem.exists(path.join(tmp.path, "tui.json"))).toBe(true)
  176. const server = JSON.parse(await Filesystem.readText(path.join(tmp.path, "opencode.json")))
  177. expect(server.theme).toBeUndefined()
  178. expect(server.tui).toBeUndefined()
  179. },
  180. })
  181. })
  182. test("drops unknown legacy tui keys during migration", async () => {
  183. await using tmp = await tmpdir({
  184. init: async (dir) => {
  185. await Bun.write(
  186. path.join(dir, "opencode.json"),
  187. JSON.stringify(
  188. {
  189. theme: "migrated-theme",
  190. tui: { scroll_speed: 2, foo: 1 },
  191. },
  192. null,
  193. 2,
  194. ),
  195. )
  196. },
  197. })
  198. await Instance.provide({
  199. directory: tmp.path,
  200. fn: async () => {
  201. const config = await TuiConfig.get()
  202. expect(config.theme).toBe("migrated-theme")
  203. expect(config.scroll_speed).toBe(2)
  204. const text = await Filesystem.readText(path.join(tmp.path, "tui.json"))
  205. const migrated = JSON.parse(text)
  206. expect(migrated.scroll_speed).toBe(2)
  207. expect(migrated.foo).toBeUndefined()
  208. },
  209. })
  210. })
  211. test("skips migration when opencode.jsonc is syntactically invalid", async () => {
  212. await using tmp = await tmpdir({
  213. init: async (dir) => {
  214. await Bun.write(
  215. path.join(dir, "opencode.jsonc"),
  216. `{
  217. "theme": "broken-theme",
  218. "tui": { "scroll_speed": 2 }
  219. "username": "still-broken"
  220. }`,
  221. )
  222. },
  223. })
  224. await Instance.provide({
  225. directory: tmp.path,
  226. fn: async () => {
  227. const config = await TuiConfig.get()
  228. expect(config.theme).toBeUndefined()
  229. expect(config.scroll_speed).toBeUndefined()
  230. expect(await Filesystem.exists(path.join(tmp.path, "tui.json"))).toBe(false)
  231. expect(await Filesystem.exists(path.join(tmp.path, "opencode.jsonc.tui-migration.bak"))).toBe(false)
  232. const source = await Filesystem.readText(path.join(tmp.path, "opencode.jsonc"))
  233. expect(source).toContain('"theme": "broken-theme"')
  234. expect(source).toContain('"tui": { "scroll_speed": 2 }')
  235. },
  236. })
  237. })
  238. test("skips migration when tui.json already exists", async () => {
  239. await using tmp = await tmpdir({
  240. init: async (dir) => {
  241. await Bun.write(path.join(dir, "opencode.json"), JSON.stringify({ theme: "legacy" }, null, 2))
  242. await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ diff_style: "stacked" }, null, 2))
  243. },
  244. })
  245. await Instance.provide({
  246. directory: tmp.path,
  247. fn: async () => {
  248. const config = await TuiConfig.get()
  249. expect(config.diff_style).toBe("stacked")
  250. expect(config.theme).toBeUndefined()
  251. const server = JSON.parse(await Filesystem.readText(path.join(tmp.path, "opencode.json")))
  252. expect(server.theme).toBe("legacy")
  253. expect(await Filesystem.exists(path.join(tmp.path, "opencode.json.tui-migration.bak"))).toBe(false)
  254. },
  255. })
  256. })
  257. test("continues loading tui config when legacy source cannot be stripped", async () => {
  258. await using tmp = await tmpdir({
  259. init: async (dir) => {
  260. await Bun.write(path.join(dir, "opencode.json"), JSON.stringify({ theme: "readonly-theme" }, null, 2))
  261. },
  262. })
  263. const source = path.join(tmp.path, "opencode.json")
  264. await fs.chmod(source, 0o444)
  265. try {
  266. await Instance.provide({
  267. directory: tmp.path,
  268. fn: async () => {
  269. const config = await TuiConfig.get()
  270. expect(config.theme).toBe("readonly-theme")
  271. expect(await Filesystem.exists(path.join(tmp.path, "tui.json"))).toBe(true)
  272. const server = JSON.parse(await Filesystem.readText(source))
  273. expect(server.theme).toBe("readonly-theme")
  274. },
  275. })
  276. } finally {
  277. await fs.chmod(source, 0o644)
  278. }
  279. })
  280. test("migration backup preserves JSONC comments", async () => {
  281. await using tmp = await tmpdir({
  282. init: async (dir) => {
  283. await Bun.write(
  284. path.join(dir, "opencode.jsonc"),
  285. `{
  286. // top-level comment
  287. "theme": "jsonc-theme",
  288. "tui": {
  289. // nested comment
  290. "scroll_speed": 1.5
  291. }
  292. }`,
  293. )
  294. },
  295. })
  296. await Instance.provide({
  297. directory: tmp.path,
  298. fn: async () => {
  299. await TuiConfig.get()
  300. const backup = await Filesystem.readText(path.join(tmp.path, "opencode.jsonc.tui-migration.bak"))
  301. expect(backup).toContain("// top-level comment")
  302. expect(backup).toContain("// nested comment")
  303. expect(backup).toContain('"theme": "jsonc-theme"')
  304. expect(backup).toContain('"scroll_speed": 1.5')
  305. },
  306. })
  307. })
  308. test("migrates legacy tui keys across multiple opencode.json levels", async () => {
  309. await using tmp = await tmpdir({
  310. init: async (dir) => {
  311. const nested = path.join(dir, "apps", "client")
  312. await fs.mkdir(nested, { recursive: true })
  313. await Bun.write(path.join(dir, "opencode.json"), JSON.stringify({ theme: "root-theme" }, null, 2))
  314. await Bun.write(path.join(nested, "opencode.json"), JSON.stringify({ theme: "nested-theme" }, null, 2))
  315. },
  316. })
  317. await Instance.provide({
  318. directory: path.join(tmp.path, "apps", "client"),
  319. fn: async () => {
  320. const config = await TuiConfig.get()
  321. expect(config.theme).toBe("nested-theme")
  322. expect(await Filesystem.exists(path.join(tmp.path, "tui.json"))).toBe(true)
  323. expect(await Filesystem.exists(path.join(tmp.path, "apps", "client", "tui.json"))).toBe(true)
  324. },
  325. })
  326. })
  327. test("flattens nested tui key inside tui.json", async () => {
  328. await using tmp = await tmpdir({
  329. init: async (dir) => {
  330. await Bun.write(
  331. path.join(dir, "tui.json"),
  332. JSON.stringify({
  333. theme: "outer",
  334. tui: { scroll_speed: 3, diff_style: "stacked" },
  335. }),
  336. )
  337. },
  338. })
  339. await Instance.provide({
  340. directory: tmp.path,
  341. fn: async () => {
  342. const config = await TuiConfig.get()
  343. expect(config.scroll_speed).toBe(3)
  344. expect(config.diff_style).toBe("stacked")
  345. // top-level keys take precedence over nested tui keys
  346. expect(config.theme).toBe("outer")
  347. },
  348. })
  349. })
  350. test("top-level keys in tui.json take precedence over nested tui key", async () => {
  351. await using tmp = await tmpdir({
  352. init: async (dir) => {
  353. await Bun.write(
  354. path.join(dir, "tui.json"),
  355. JSON.stringify({
  356. diff_style: "auto",
  357. tui: { diff_style: "stacked", scroll_speed: 2 },
  358. }),
  359. )
  360. },
  361. })
  362. await Instance.provide({
  363. directory: tmp.path,
  364. fn: async () => {
  365. const config = await TuiConfig.get()
  366. expect(config.diff_style).toBe("auto")
  367. expect(config.scroll_speed).toBe(2)
  368. },
  369. })
  370. })
  371. test("project config takes precedence over OPENCODE_TUI_CONFIG (matches OPENCODE_CONFIG)", async () => {
  372. await using tmp = await tmpdir({
  373. init: async (dir) => {
  374. await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ theme: "project", diff_style: "auto" }))
  375. const custom = path.join(dir, "custom-tui.json")
  376. await Bun.write(custom, JSON.stringify({ theme: "custom", diff_style: "stacked" }))
  377. process.env.OPENCODE_TUI_CONFIG = custom
  378. },
  379. })
  380. await Instance.provide({
  381. directory: tmp.path,
  382. fn: async () => {
  383. const config = await TuiConfig.get()
  384. // project tui.json overrides the custom path, same as server config precedence
  385. expect(config.theme).toBe("project")
  386. // project also set diff_style, so that wins
  387. expect(config.diff_style).toBe("auto")
  388. },
  389. })
  390. })
  391. test("merges keybind overrides across precedence layers", async () => {
  392. await using tmp = await tmpdir({
  393. init: async (dir) => {
  394. await Bun.write(path.join(Global.Path.config, "tui.json"), JSON.stringify({ keybinds: { app_exit: "ctrl+q" } }))
  395. await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ keybinds: { theme_list: "ctrl+k" } }))
  396. },
  397. })
  398. await Instance.provide({
  399. directory: tmp.path,
  400. fn: async () => {
  401. const config = await TuiConfig.get()
  402. expect(config.keybinds?.app_exit).toBe("ctrl+q")
  403. expect(config.keybinds?.theme_list).toBe("ctrl+k")
  404. },
  405. })
  406. })
  407. wintest("defaults Ctrl+Z to input undo on Windows", async () => {
  408. await using tmp = await tmpdir()
  409. await Instance.provide({
  410. directory: tmp.path,
  411. fn: async () => {
  412. const config = await TuiConfig.get()
  413. expect(config.keybinds?.terminal_suspend).toBe("none")
  414. expect(config.keybinds?.input_undo).toBe("ctrl+z,ctrl+-,super+z")
  415. },
  416. })
  417. })
  418. wintest("keeps explicit input undo overrides on Windows", async () => {
  419. await using tmp = await tmpdir({
  420. init: async (dir) => {
  421. await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ keybinds: { input_undo: "ctrl+y" } }))
  422. },
  423. })
  424. await Instance.provide({
  425. directory: tmp.path,
  426. fn: async () => {
  427. const config = await TuiConfig.get()
  428. expect(config.keybinds?.terminal_suspend).toBe("none")
  429. expect(config.keybinds?.input_undo).toBe("ctrl+y")
  430. },
  431. })
  432. })
  433. wintest("ignores terminal suspend bindings on Windows", async () => {
  434. await using tmp = await tmpdir({
  435. init: async (dir) => {
  436. await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ keybinds: { terminal_suspend: "alt+z" } }))
  437. },
  438. })
  439. await Instance.provide({
  440. directory: tmp.path,
  441. fn: async () => {
  442. const config = await TuiConfig.get()
  443. expect(config.keybinds?.terminal_suspend).toBe("none")
  444. expect(config.keybinds?.input_undo).toBe("ctrl+z,ctrl+-,super+z")
  445. },
  446. })
  447. })
  448. test("OPENCODE_TUI_CONFIG provides settings when no project config exists", async () => {
  449. await using tmp = await tmpdir({
  450. init: async (dir) => {
  451. const custom = path.join(dir, "custom-tui.json")
  452. await Bun.write(custom, JSON.stringify({ theme: "from-env", diff_style: "stacked" }))
  453. process.env.OPENCODE_TUI_CONFIG = custom
  454. },
  455. })
  456. await Instance.provide({
  457. directory: tmp.path,
  458. fn: async () => {
  459. const config = await TuiConfig.get()
  460. expect(config.theme).toBe("from-env")
  461. expect(config.diff_style).toBe("stacked")
  462. },
  463. })
  464. })
  465. test("does not derive tui path from OPENCODE_CONFIG", async () => {
  466. await using tmp = await tmpdir({
  467. init: async (dir) => {
  468. const customDir = path.join(dir, "custom")
  469. await fs.mkdir(customDir, { recursive: true })
  470. await Bun.write(path.join(customDir, "opencode.json"), JSON.stringify({ model: "test/model" }))
  471. await Bun.write(path.join(customDir, "tui.json"), JSON.stringify({ theme: "should-not-load" }))
  472. process.env.OPENCODE_CONFIG = path.join(customDir, "opencode.json")
  473. },
  474. })
  475. await Instance.provide({
  476. directory: tmp.path,
  477. fn: async () => {
  478. const config = await TuiConfig.get()
  479. expect(config.theme).toBeUndefined()
  480. },
  481. })
  482. })
  483. test("applies env and file substitutions in tui.json", async () => {
  484. const original = process.env.TUI_THEME_TEST
  485. process.env.TUI_THEME_TEST = "env-theme"
  486. try {
  487. await using tmp = await tmpdir({
  488. init: async (dir) => {
  489. await Bun.write(path.join(dir, "keybind.txt"), "ctrl+q")
  490. await Bun.write(
  491. path.join(dir, "tui.json"),
  492. JSON.stringify({
  493. theme: "{env:TUI_THEME_TEST}",
  494. keybinds: { app_exit: "{file:keybind.txt}" },
  495. }),
  496. )
  497. },
  498. })
  499. await Instance.provide({
  500. directory: tmp.path,
  501. fn: async () => {
  502. const config = await TuiConfig.get()
  503. expect(config.theme).toBe("env-theme")
  504. expect(config.keybinds?.app_exit).toBe("ctrl+q")
  505. },
  506. })
  507. } finally {
  508. if (original === undefined) delete process.env.TUI_THEME_TEST
  509. else process.env.TUI_THEME_TEST = original
  510. }
  511. })
  512. test("applies file substitutions when first identical token is in a commented line", async () => {
  513. await using tmp = await tmpdir({
  514. init: async (dir) => {
  515. await Bun.write(path.join(dir, "theme.txt"), "resolved-theme")
  516. await Bun.write(
  517. path.join(dir, "tui.jsonc"),
  518. `{
  519. // "theme": "{file:theme.txt}",
  520. "theme": "{file:theme.txt}"
  521. }`,
  522. )
  523. },
  524. })
  525. await Instance.provide({
  526. directory: tmp.path,
  527. fn: async () => {
  528. const config = await TuiConfig.get()
  529. expect(config.theme).toBe("resolved-theme")
  530. },
  531. })
  532. })
  533. test("loads managed tui config and gives it highest precedence", async () => {
  534. await using tmp = await tmpdir({
  535. init: async (dir) => {
  536. await Bun.write(
  537. path.join(dir, "tui.json"),
  538. JSON.stringify({ theme: "project-theme", plugin: ["shared-plugin@1.0.0"] }, null, 2),
  539. )
  540. await fs.mkdir(managedConfigDir, { recursive: true })
  541. await Bun.write(
  542. path.join(managedConfigDir, "tui.json"),
  543. JSON.stringify({ theme: "managed-theme", plugin: ["shared-plugin@2.0.0"] }, null, 2),
  544. )
  545. },
  546. })
  547. await Instance.provide({
  548. directory: tmp.path,
  549. fn: async () => {
  550. const config = await TuiConfig.get()
  551. expect(config.theme).toBe("managed-theme")
  552. expect(config.plugin).toEqual(["shared-plugin@2.0.0"])
  553. expect(config.plugin_origins).toEqual([
  554. {
  555. spec: "shared-plugin@2.0.0",
  556. scope: "global",
  557. source: path.join(managedConfigDir, "tui.json"),
  558. },
  559. ])
  560. },
  561. })
  562. })
  563. test("loads .opencode/tui.json", async () => {
  564. await using tmp = await tmpdir({
  565. init: async (dir) => {
  566. await fs.mkdir(path.join(dir, ".opencode"), { recursive: true })
  567. await Bun.write(path.join(dir, ".opencode", "tui.json"), JSON.stringify({ diff_style: "stacked" }, null, 2))
  568. },
  569. })
  570. await Instance.provide({
  571. directory: tmp.path,
  572. fn: async () => {
  573. const config = await TuiConfig.get()
  574. expect(config.diff_style).toBe("stacked")
  575. },
  576. })
  577. })
  578. test("gracefully falls back when tui.json has invalid JSON", async () => {
  579. await using tmp = await tmpdir({
  580. init: async (dir) => {
  581. await Bun.write(path.join(dir, "tui.json"), "{ invalid json }")
  582. await fs.mkdir(managedConfigDir, { recursive: true })
  583. await Bun.write(path.join(managedConfigDir, "tui.json"), JSON.stringify({ theme: "managed-fallback" }, null, 2))
  584. },
  585. })
  586. await Instance.provide({
  587. directory: tmp.path,
  588. fn: async () => {
  589. const config = await TuiConfig.get()
  590. expect(config.theme).toBe("managed-fallback")
  591. expect(config.keybinds).toBeDefined()
  592. },
  593. })
  594. })
  595. test("supports tuple plugin specs with options in tui.json", async () => {
  596. await using tmp = await tmpdir({
  597. init: async (dir) => {
  598. await Bun.write(
  599. path.join(dir, "tui.json"),
  600. JSON.stringify({
  601. plugin: [["acme-plugin@1.2.3", { enabled: true, label: "demo" }]],
  602. }),
  603. )
  604. },
  605. })
  606. await Instance.provide({
  607. directory: tmp.path,
  608. fn: async () => {
  609. const config = await TuiConfig.get()
  610. expect(config.plugin).toEqual([["acme-plugin@1.2.3", { enabled: true, label: "demo" }]])
  611. expect(config.plugin_origins).toEqual([
  612. {
  613. spec: ["acme-plugin@1.2.3", { enabled: true, label: "demo" }],
  614. scope: "local",
  615. source: path.join(tmp.path, "tui.json"),
  616. },
  617. ])
  618. },
  619. })
  620. })
  621. test("deduplicates tuple plugin specs by name with higher precedence winning", async () => {
  622. await using tmp = await tmpdir({
  623. init: async (dir) => {
  624. await Bun.write(
  625. path.join(Global.Path.config, "tui.json"),
  626. JSON.stringify({
  627. plugin: [["acme-plugin@1.0.0", { source: "global" }]],
  628. }),
  629. )
  630. await Bun.write(
  631. path.join(dir, "tui.json"),
  632. JSON.stringify({
  633. plugin: [
  634. ["acme-plugin@2.0.0", { source: "project" }],
  635. ["second-plugin@3.0.0", { source: "project" }],
  636. ],
  637. }),
  638. )
  639. },
  640. })
  641. await Instance.provide({
  642. directory: tmp.path,
  643. fn: async () => {
  644. const config = await TuiConfig.get()
  645. expect(config.plugin).toEqual([
  646. ["acme-plugin@2.0.0", { source: "project" }],
  647. ["second-plugin@3.0.0", { source: "project" }],
  648. ])
  649. expect(config.plugin_origins).toEqual([
  650. {
  651. spec: ["acme-plugin@2.0.0", { source: "project" }],
  652. scope: "local",
  653. source: path.join(tmp.path, "tui.json"),
  654. },
  655. {
  656. spec: ["second-plugin@3.0.0", { source: "project" }],
  657. scope: "local",
  658. source: path.join(tmp.path, "tui.json"),
  659. },
  660. ])
  661. },
  662. })
  663. })
  664. test("tracks global and local plugin metadata in merged tui config", async () => {
  665. await using tmp = await tmpdir({
  666. init: async (dir) => {
  667. await Bun.write(
  668. path.join(Global.Path.config, "tui.json"),
  669. JSON.stringify({
  670. plugin: ["global-plugin@1.0.0"],
  671. }),
  672. )
  673. await Bun.write(
  674. path.join(dir, "tui.json"),
  675. JSON.stringify({
  676. plugin: ["local-plugin@2.0.0"],
  677. }),
  678. )
  679. },
  680. })
  681. await Instance.provide({
  682. directory: tmp.path,
  683. fn: async () => {
  684. const config = await TuiConfig.get()
  685. expect(config.plugin).toEqual(["global-plugin@1.0.0", "local-plugin@2.0.0"])
  686. expect(config.plugin_origins).toEqual([
  687. {
  688. spec: "global-plugin@1.0.0",
  689. scope: "global",
  690. source: path.join(Global.Path.config, "tui.json"),
  691. },
  692. {
  693. spec: "local-plugin@2.0.0",
  694. scope: "local",
  695. source: path.join(tmp.path, "tui.json"),
  696. },
  697. ])
  698. },
  699. })
  700. })
  701. test("merges plugin_enabled flags across config layers", async () => {
  702. await using tmp = await tmpdir({
  703. init: async (dir) => {
  704. await Bun.write(
  705. path.join(Global.Path.config, "tui.json"),
  706. JSON.stringify({
  707. plugin_enabled: {
  708. "internal:sidebar-context": false,
  709. "demo.plugin": true,
  710. },
  711. }),
  712. )
  713. await Bun.write(
  714. path.join(dir, "tui.json"),
  715. JSON.stringify({
  716. plugin_enabled: {
  717. "demo.plugin": false,
  718. "local.plugin": true,
  719. },
  720. }),
  721. )
  722. },
  723. })
  724. await Instance.provide({
  725. directory: tmp.path,
  726. fn: async () => {
  727. const config = await TuiConfig.get()
  728. expect(config.plugin_enabled).toEqual({
  729. "internal:sidebar-context": false,
  730. "demo.plugin": false,
  731. "local.plugin": true,
  732. })
  733. },
  734. })
  735. })