tui.test.ts 24 KB

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