config.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { NodeFileSystem } from "@effect/platform-node"
  2. import { Global } from "@opencode-ai/core/global"
  3. import { Effect } from "effect"
  4. import { expect, test } from "bun:test"
  5. import path from "path"
  6. import { Config } from "../src/config"
  7. function run<A, E>(directory: string, effect: Effect.Effect<A, E, Config.Service>) {
  8. return Effect.runPromise(
  9. effect.pipe(
  10. Effect.provide(Config.layer),
  11. Effect.provide(Global.layerWith({ config: directory, state: directory })),
  12. Effect.provide(NodeFileSystem.layer),
  13. ),
  14. )
  15. }
  16. test("migrates tui and kv config into cli.json", async () => {
  17. const directory = await Bun.$`mktemp -d`.text().then((value) => value.trim())
  18. await Bun.write(
  19. path.join(directory, "tui.json"),
  20. JSON.stringify({
  21. theme: "legacy",
  22. keybinds: { leader: "ctrl+o" },
  23. plugin: [["example", { mode: "safe" }]],
  24. plugin_enabled: { disabled: false },
  25. leader_timeout: 500,
  26. scroll_speed: 2,
  27. scroll_acceleration: { enabled: true },
  28. diff_style: "stacked",
  29. mouse: false,
  30. }),
  31. )
  32. await Bun.write(
  33. path.join(directory, "kv.json"),
  34. JSON.stringify({
  35. theme_mode_lock: "light",
  36. attention_sound_pack: "custom.pack",
  37. diff_wrap_mode: "none",
  38. diff_viewer_show_file_tree: false,
  39. diff_viewer_single_patch: true,
  40. diff_viewer_view: "split",
  41. terminal_title_enabled: false,
  42. file_context_enabled: false,
  43. paste_summary_enabled: false,
  44. sidebar: "hide",
  45. scrollbar_visible: true,
  46. thinking_mode: "show",
  47. exploration_grouping: false,
  48. tips_hidden: true,
  49. dismissed_getting_started: true,
  50. animations_enabled: false,
  51. skipped_version: "9.9.9",
  52. which_key_layout: "overlay",
  53. }),
  54. )
  55. try {
  56. const config = await run(
  57. directory,
  58. Effect.gen(function* () {
  59. const service = yield* Config.Service
  60. return yield* service.get()
  61. }),
  62. )
  63. expect(config).toMatchObject({
  64. theme: { name: "legacy", mode: "light" },
  65. keybinds: { leader: "ctrl+o" },
  66. plugins: [{ package: "example", options: { mode: "safe" } }, "-disabled"],
  67. leader: { timeout: 500 },
  68. scroll: { speed: 2, acceleration: true },
  69. attention: { sound_pack: "custom.pack" },
  70. diffs: { wrap: "none", tree: false, single: true, view: "split" },
  71. terminal: { title: false },
  72. prompt: { editor: false, paste: "full" },
  73. session: { sidebar: "hide", scrollbar: true, thinking: "show", grouping: "none" },
  74. hints: { tips: false, onboarding: false },
  75. animations: false,
  76. mouse: false,
  77. })
  78. expect(config).not.toHaveProperty("skipped_version")
  79. expect(config).not.toHaveProperty("which_key")
  80. expect((await Bun.file(path.join(directory, "cli.json")).json()).keybinds).toEqual({ leader: "ctrl+o" })
  81. expect(await Bun.file(path.join(directory, "cli.json")).exists()).toBe(true)
  82. expect(await Bun.file(path.join(directory, "tui.json")).exists()).toBe(true)
  83. expect(await Bun.file(path.join(directory, "kv.json")).exists()).toBe(true)
  84. } finally {
  85. await Bun.$`rm -rf ${directory}`
  86. }
  87. })
  88. test("migrates before the first update and does not remigrate afterward", async () => {
  89. const directory = await Bun.$`mktemp -d`.text().then((value) => value.trim())
  90. await Bun.write(path.join(directory, "tui.json"), JSON.stringify({ theme: "legacy" }))
  91. try {
  92. const config = await run(
  93. directory,
  94. Effect.gen(function* () {
  95. const service = yield* Config.Service
  96. yield* service.update((draft) => {
  97. draft.animations = false
  98. draft.mouse = false
  99. })
  100. yield* Effect.promise(() =>
  101. Bun.write(path.join(directory, "tui.json"), JSON.stringify({ theme: "changed" })),
  102. )
  103. return yield* service.get()
  104. }),
  105. )
  106. expect(config).toEqual({ theme: { name: "legacy" }, animations: false, mouse: false })
  107. expect(await Bun.file(path.join(directory, "cli.json")).json()).toEqual({
  108. theme: { name: "legacy" },
  109. animations: false,
  110. mouse: false,
  111. })
  112. } finally {
  113. await Bun.$`rm -rf ${directory}`
  114. }
  115. })
  116. test("updates a config draft while preserving JSONC comments", async () => {
  117. const directory = await Bun.$`mktemp -d`.text().then((value) => value.trim())
  118. await Bun.write(path.join(directory, "cli.json"), "{\n // Keep this comment\n \"animations\": true\n}\n")
  119. try {
  120. const config = await run(
  121. directory,
  122. Effect.gen(function* () {
  123. const service = yield* Config.Service
  124. return yield* service.update((draft) => {
  125. draft.prompt = { paste: "compact" }
  126. })
  127. }),
  128. )
  129. expect(config).toEqual({ animations: true, prompt: { paste: "compact" } })
  130. expect(await Bun.file(path.join(directory, "cli.json")).text()).toContain("// Keep this comment")
  131. } finally {
  132. await Bun.$`rm -rf ${directory}`
  133. }
  134. })