@stonerzju/opencode 1.2.17 → 1.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/bin/opencode +29 -157
  2. package/package.json +29 -29
  3. package/src/acp/agent.ts +4 -4
  4. package/src/acp/session.ts +1 -1
  5. package/src/agent/agent.ts +3 -3
  6. package/src/bun/index.ts +2 -2
  7. package/src/cli/cmd/acp.ts +3 -3
  8. package/src/cli/cmd/debug/file.ts +1 -1
  9. package/src/cli/cmd/github.ts +2 -2
  10. package/src/cli/cmd/pr.ts +1 -1
  11. package/src/cli/cmd/tui/app.tsx +24 -24
  12. package/src/cli/cmd/tui/attach.ts +3 -3
  13. package/src/cli/cmd/tui/component/dialog-agent.tsx +3 -3
  14. package/src/cli/cmd/tui/component/dialog-command.tsx +3 -3
  15. package/src/cli/cmd/tui/component/dialog-mcp.tsx +5 -5
  16. package/src/cli/cmd/tui/component/dialog-model.tsx +4 -4
  17. package/src/cli/cmd/tui/component/dialog-provider.tsx +4 -4
  18. package/src/cli/cmd/tui/component/dialog-session-list.tsx +5 -5
  19. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +3 -3
  20. package/src/cli/cmd/tui/component/dialog-skill.tsx +3 -3
  21. package/src/cli/cmd/tui/component/dialog-stash.tsx +3 -3
  22. package/src/cli/cmd/tui/component/dialog-status.tsx +2 -2
  23. package/src/cli/cmd/tui/component/dialog-tag.tsx +3 -3
  24. package/src/cli/cmd/tui/component/logo.tsx +2 -2
  25. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +6 -6
  26. package/src/cli/cmd/tui/component/prompt/frecency.tsx +2 -2
  27. package/src/cli/cmd/tui/component/prompt/history.tsx +2 -2
  28. package/src/cli/cmd/tui/component/prompt/index.tsx +14 -14
  29. package/src/cli/cmd/tui/component/prompt/stash.tsx +2 -2
  30. package/src/cli/cmd/tui/component/textarea-keybindings.ts +1 -1
  31. package/src/cli/cmd/tui/component/tips.tsx +1 -1
  32. package/src/cli/cmd/tui/context/directory.ts +1 -1
  33. package/src/cli/cmd/tui/context/exit.tsx +1 -1
  34. package/src/cli/cmd/tui/context/keybind.tsx +2 -2
  35. package/src/cli/cmd/tui/context/kv.tsx +2 -2
  36. package/src/cli/cmd/tui/context/local.tsx +6 -6
  37. package/src/cli/cmd/tui/context/sync.tsx +4 -4
  38. package/src/cli/cmd/tui/context/theme/opencode.json +245 -0
  39. package/src/cli/cmd/tui/context/theme.tsx +2 -2
  40. package/src/cli/cmd/tui/context/tui-config.tsx +1 -1
  41. package/src/cli/cmd/tui/event.ts +2 -2
  42. package/src/cli/cmd/tui/routes/home.tsx +6 -6
  43. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +6 -6
  44. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +6 -6
  45. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +2 -2
  46. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +3 -3
  47. package/src/cli/cmd/tui/routes/session/header.tsx +5 -5
  48. package/src/cli/cmd/tui/routes/session/index.tsx +32 -32
  49. package/src/cli/cmd/tui/routes/session/permission.tsx +4 -4
  50. package/src/cli/cmd/tui/routes/session/sidebar.tsx +4 -4
  51. package/src/cli/cmd/tui/thread.ts +9 -9
  52. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +1 -1
  53. package/src/cli/cmd/tui/ui/dialog-help.tsx +2 -2
  54. package/src/cli/cmd/tui/ui/dialog-select.tsx +5 -5
  55. package/src/cli/cmd/tui/ui/dialog.tsx +3 -3
  56. package/src/cli/cmd/tui/ui/toast.tsx +1 -1
  57. package/src/cli/cmd/tui/util/editor.ts +3 -3
  58. package/src/cli/cmd/tui/util/transcript.ts +1 -1
  59. package/src/cli/cmd/tui/worker.ts +10 -10
  60. package/src/cli/error.ts +1 -1
  61. package/src/cli/ui.ts +1 -1
  62. package/src/cli/upgrade.ts +4 -4
  63. package/src/command/index.ts +1 -1
  64. package/src/config/config.ts +10 -10
  65. package/src/config/markdown.ts +1 -1
  66. package/src/config/migrate-tui-config.ts +5 -5
  67. package/src/config/paths.ts +4 -4
  68. package/src/config/tui.ts +4 -4
  69. package/src/control/control.sql.ts +1 -1
  70. package/src/control/index.ts +1 -1
  71. package/src/control-plane/adaptors/worktree.ts +1 -1
  72. package/src/control-plane/session-proxy-middleware.ts +1 -1
  73. package/src/control-plane/workspace.sql.ts +1 -1
  74. package/src/control-plane/workspace.ts +7 -7
  75. package/src/file/index.ts +1 -1
  76. package/src/file/ripgrep.ts +2 -2
  77. package/src/file/watcher.ts +5 -5
  78. package/src/format/formatter.ts +1 -1
  79. package/src/ide/index.ts +3 -3
  80. package/src/index.ts +1 -1
  81. package/src/installation/index.ts +3 -3
  82. package/src/lsp/client.ts +3 -3
  83. package/src/lsp/index.ts +3 -3
  84. package/src/mcp/index.ts +4 -4
  85. package/src/permission/index.ts +2 -2
  86. package/src/permission/next.ts +10 -10
  87. package/src/plugin/codex.ts +1 -1
  88. package/src/plugin/copilot.ts +2 -2
  89. package/src/plugin/index.ts +1 -1
  90. package/src/project/bootstrap.ts +2 -2
  91. package/src/project/instance.ts +4 -4
  92. package/src/project/project.sql.ts +1 -1
  93. package/src/project/project.ts +5 -5
  94. package/src/project/state.ts +1 -1
  95. package/src/project/vcs.ts +4 -4
  96. package/src/provider/auth.ts +4 -4
  97. package/src/provider/error.ts +1 -1
  98. package/src/provider/models-snapshot.ts +2 -0
  99. package/src/provider/models.ts +1 -1
  100. package/src/provider/provider.ts +2 -2
  101. package/src/provider/transform.ts +2 -2
  102. package/src/pty/index.ts +5 -5
  103. package/src/question/index.ts +5 -5
  104. package/src/server/event.ts +1 -1
  105. package/src/server/mdns.ts +1 -1
  106. package/src/server/routes/global.ts +3 -3
  107. package/src/server/routes/permission.ts +1 -1
  108. package/src/server/routes/pty.ts +1 -1
  109. package/src/server/routes/session.ts +4 -4
  110. package/src/server/routes/tui.ts +1 -1
  111. package/src/server/server.ts +3 -3
  112. package/src/session/compaction.ts +7 -7
  113. package/src/session/index.ts +10 -10
  114. package/src/session/instruction.ts +1 -1
  115. package/src/session/llm.ts +11 -11
  116. package/src/session/message-v2.ts +10 -10
  117. package/src/session/message.ts +1 -1
  118. package/src/session/processor.ts +10 -10
  119. package/src/session/prompt.ts +8 -8
  120. package/src/session/retry.ts +2 -2
  121. package/src/session/revert.ts +1 -1
  122. package/src/session/session.sql.ts +3 -3
  123. package/src/session/status.ts +3 -3
  124. package/src/session/summary.ts +5 -5
  125. package/src/session/system.ts +1 -1
  126. package/src/session/todo.ts +2 -2
  127. package/src/share/share-next.ts +7 -7
  128. package/src/share/share.sql.ts +1 -1
  129. package/src/shell/shell.ts +3 -3
  130. package/src/skill/skill.ts +6 -6
  131. package/src/storage/db.ts +1 -1
  132. package/src/storage/storage.ts +1 -1
  133. package/src/tool/bash.ts +6 -6
  134. package/src/tool/edit.ts +1 -1
  135. package/src/tool/registry.ts +2 -2
  136. package/src/tool/skill.ts +1 -1
  137. package/src/tool/task.ts +3 -3
  138. package/src/util/array.ts +10 -0
  139. package/src/util/binary.ts +41 -0
  140. package/src/util/encode.ts +51 -0
  141. package/src/util/error.ts +54 -0
  142. package/src/util/identifier.ts +48 -0
  143. package/src/util/lazy.ts +4 -16
  144. package/src/util/path.ts +37 -0
  145. package/src/util/retry.ts +41 -0
  146. package/src/util/slug.ts +74 -0
  147. package/src/worktree/index.ts +3 -3
  148. package/AGENTS.md +0 -10
  149. package/BUN_SHELL_MIGRATION_PLAN.md +0 -136
  150. package/Dockerfile +0 -18
  151. package/README.md +0 -15
  152. package/bunfig.toml +0 -7
  153. package/drizzle.config.ts +0 -10
  154. package/script/build.ts +0 -224
  155. package/script/check-migrations.ts +0 -16
  156. package/script/postinstall.mjs +0 -131
  157. package/script/publish.ts +0 -181
  158. package/script/schema.ts +0 -63
  159. package/script/seed-e2e.ts +0 -50
  160. package/sst-env.d.ts +0 -10
  161. package/test/AGENTS.md +0 -81
  162. package/test/acp/agent-interface.test.ts +0 -51
  163. package/test/acp/event-subscription.test.ts +0 -683
  164. package/test/agent/agent.test.ts +0 -689
  165. package/test/bun.test.ts +0 -53
  166. package/test/cli/github-action.test.ts +0 -197
  167. package/test/cli/github-remote.test.ts +0 -80
  168. package/test/cli/import.test.ts +0 -38
  169. package/test/cli/plugin-auth-picker.test.ts +0 -120
  170. package/test/cli/tui/transcript.test.ts +0 -322
  171. package/test/config/agent-color.test.ts +0 -71
  172. package/test/config/config.test.ts +0 -1886
  173. package/test/config/fixtures/empty-frontmatter.md +0 -4
  174. package/test/config/fixtures/frontmatter.md +0 -28
  175. package/test/config/fixtures/markdown-header.md +0 -11
  176. package/test/config/fixtures/no-frontmatter.md +0 -1
  177. package/test/config/fixtures/weird-model-id.md +0 -13
  178. package/test/config/markdown.test.ts +0 -228
  179. package/test/config/tui.test.ts +0 -510
  180. package/test/control-plane/session-proxy-middleware.test.ts +0 -147
  181. package/test/control-plane/sse.test.ts +0 -56
  182. package/test/control-plane/workspace-server-sse.test.ts +0 -65
  183. package/test/control-plane/workspace-sync.test.ts +0 -97
  184. package/test/file/ignore.test.ts +0 -10
  185. package/test/file/index.test.ts +0 -394
  186. package/test/file/path-traversal.test.ts +0 -198
  187. package/test/file/ripgrep.test.ts +0 -39
  188. package/test/file/time.test.ts +0 -361
  189. package/test/fixture/db.ts +0 -11
  190. package/test/fixture/fixture.ts +0 -45
  191. package/test/fixture/lsp/fake-lsp-server.js +0 -77
  192. package/test/fixture/skills/agents-sdk/SKILL.md +0 -152
  193. package/test/fixture/skills/agents-sdk/references/callable.md +0 -92
  194. package/test/fixture/skills/cloudflare/SKILL.md +0 -211
  195. package/test/fixture/skills/index.json +0 -6
  196. package/test/ide/ide.test.ts +0 -82
  197. package/test/keybind.test.ts +0 -421
  198. package/test/lsp/client.test.ts +0 -95
  199. package/test/mcp/headers.test.ts +0 -153
  200. package/test/mcp/oauth-browser.test.ts +0 -249
  201. package/test/memory/abort-leak.test.ts +0 -136
  202. package/test/patch/patch.test.ts +0 -348
  203. package/test/permission/arity.test.ts +0 -33
  204. package/test/permission/next.test.ts +0 -689
  205. package/test/permission-task.test.ts +0 -319
  206. package/test/plugin/auth-override.test.ts +0 -44
  207. package/test/plugin/codex.test.ts +0 -123
  208. package/test/preload.ts +0 -80
  209. package/test/project/project.test.ts +0 -348
  210. package/test/project/worktree-remove.test.ts +0 -65
  211. package/test/provider/amazon-bedrock.test.ts +0 -446
  212. package/test/provider/copilot/convert-to-copilot-messages.test.ts +0 -523
  213. package/test/provider/copilot/copilot-chat-model.test.ts +0 -592
  214. package/test/provider/gitlab-duo.test.ts +0 -262
  215. package/test/provider/provider.test.ts +0 -2220
  216. package/test/provider/transform.test.ts +0 -2353
  217. package/test/pty/pty-output-isolation.test.ts +0 -140
  218. package/test/question/question.test.ts +0 -300
  219. package/test/scheduler.test.ts +0 -73
  220. package/test/server/global-session-list.test.ts +0 -89
  221. package/test/server/session-list.test.ts +0 -90
  222. package/test/server/session-select.test.ts +0 -78
  223. package/test/session/compaction.test.ts +0 -423
  224. package/test/session/instruction.test.ts +0 -170
  225. package/test/session/llm.test.ts +0 -667
  226. package/test/session/message-v2.test.ts +0 -924
  227. package/test/session/prompt.test.ts +0 -211
  228. package/test/session/retry.test.ts +0 -188
  229. package/test/session/revert-compact.test.ts +0 -285
  230. package/test/session/session.test.ts +0 -71
  231. package/test/session/structured-output-integration.test.ts +0 -233
  232. package/test/session/structured-output.test.ts +0 -385
  233. package/test/skill/discovery.test.ts +0 -110
  234. package/test/skill/skill.test.ts +0 -388
  235. package/test/snapshot/snapshot.test.ts +0 -1180
  236. package/test/storage/json-migration.test.ts +0 -846
  237. package/test/tool/__snapshots__/tool.test.ts.snap +0 -9
  238. package/test/tool/apply_patch.test.ts +0 -566
  239. package/test/tool/bash.test.ts +0 -402
  240. package/test/tool/edit.test.ts +0 -496
  241. package/test/tool/external-directory.test.ts +0 -127
  242. package/test/tool/fixtures/large-image.png +0 -0
  243. package/test/tool/fixtures/models-api.json +0 -38413
  244. package/test/tool/grep.test.ts +0 -110
  245. package/test/tool/question.test.ts +0 -107
  246. package/test/tool/read.test.ts +0 -504
  247. package/test/tool/registry.test.ts +0 -122
  248. package/test/tool/skill.test.ts +0 -112
  249. package/test/tool/truncation.test.ts +0 -160
  250. package/test/tool/webfetch.test.ts +0 -100
  251. package/test/tool/write.test.ts +0 -348
  252. package/test/util/filesystem.test.ts +0 -443
  253. package/test/util/format.test.ts +0 -59
  254. package/test/util/glob.test.ts +0 -164
  255. package/test/util/iife.test.ts +0 -36
  256. package/test/util/lazy.test.ts +0 -50
  257. package/test/util/lock.test.ts +0 -72
  258. package/test/util/process.test.ts +0 -59
  259. package/test/util/timeout.test.ts +0 -21
  260. package/test/util/wildcard.test.ts +0 -90
  261. package/tsconfig.json +0 -16
@@ -1,1886 +0,0 @@
1
- import { test, expect, describe, mock, afterEach } from "bun:test"
2
- import { Config } from "../../src/config/config"
3
- import { Instance } from "../../src/project/instance"
4
- import { Auth } from "../../src/auth"
5
- import { tmpdir } from "../fixture/fixture"
6
- import path from "path"
7
- import fs from "fs/promises"
8
- import { pathToFileURL } from "url"
9
- import { Global } from "../../src/global"
10
- import { Filesystem } from "../../src/util/filesystem"
11
-
12
- // Get managed config directory from environment (set in preload.ts)
13
- const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
14
-
15
- afterEach(async () => {
16
- await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
17
- })
18
-
19
- async function writeManagedSettings(settings: object, filename = "opencode.json") {
20
- await fs.mkdir(managedConfigDir, { recursive: true })
21
- await Filesystem.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
22
- }
23
-
24
- async function writeConfig(dir: string, config: object, name = "opencode.json") {
25
- await Filesystem.write(path.join(dir, name), JSON.stringify(config))
26
- }
27
-
28
- test("loads config with defaults when no files exist", async () => {
29
- await using tmp = await tmpdir()
30
- await Instance.provide({
31
- directory: tmp.path,
32
- fn: async () => {
33
- const config = await Config.get()
34
- expect(config.username).toBeDefined()
35
- },
36
- })
37
- })
38
-
39
- test("loads JSON config file", async () => {
40
- await using tmp = await tmpdir({
41
- init: async (dir) => {
42
- await writeConfig(dir, {
43
- $schema: "https://opencode.ai/config.json",
44
- model: "test/model",
45
- username: "testuser",
46
- })
47
- },
48
- })
49
- await Instance.provide({
50
- directory: tmp.path,
51
- fn: async () => {
52
- const config = await Config.get()
53
- expect(config.model).toBe("test/model")
54
- expect(config.username).toBe("testuser")
55
- },
56
- })
57
- })
58
-
59
- test("ignores legacy tui keys in opencode config", async () => {
60
- await using tmp = await tmpdir({
61
- init: async (dir) => {
62
- await writeConfig(dir, {
63
- $schema: "https://opencode.ai/config.json",
64
- model: "test/model",
65
- theme: "legacy",
66
- tui: { scroll_speed: 4 },
67
- })
68
- },
69
- })
70
- await Instance.provide({
71
- directory: tmp.path,
72
- fn: async () => {
73
- const config = await Config.get()
74
- expect(config.model).toBe("test/model")
75
- expect((config as Record<string, unknown>).theme).toBeUndefined()
76
- expect((config as Record<string, unknown>).tui).toBeUndefined()
77
- },
78
- })
79
- })
80
-
81
- test("loads JSONC config file", async () => {
82
- await using tmp = await tmpdir({
83
- init: async (dir) => {
84
- await Filesystem.write(
85
- path.join(dir, "opencode.jsonc"),
86
- `{
87
- // This is a comment
88
- "$schema": "https://opencode.ai/config.json",
89
- "model": "test/model",
90
- "username": "testuser"
91
- }`,
92
- )
93
- },
94
- })
95
- await Instance.provide({
96
- directory: tmp.path,
97
- fn: async () => {
98
- const config = await Config.get()
99
- expect(config.model).toBe("test/model")
100
- expect(config.username).toBe("testuser")
101
- },
102
- })
103
- })
104
-
105
- test("merges multiple config files with correct precedence", async () => {
106
- await using tmp = await tmpdir({
107
- init: async (dir) => {
108
- await writeConfig(
109
- dir,
110
- {
111
- $schema: "https://opencode.ai/config.json",
112
- model: "base",
113
- username: "base",
114
- },
115
- "opencode.jsonc",
116
- )
117
- await writeConfig(dir, {
118
- $schema: "https://opencode.ai/config.json",
119
- model: "override",
120
- })
121
- },
122
- })
123
- await Instance.provide({
124
- directory: tmp.path,
125
- fn: async () => {
126
- const config = await Config.get()
127
- expect(config.model).toBe("override")
128
- expect(config.username).toBe("base")
129
- },
130
- })
131
- })
132
-
133
- test("handles environment variable substitution", async () => {
134
- const originalEnv = process.env["TEST_VAR"]
135
- process.env["TEST_VAR"] = "test-user"
136
-
137
- try {
138
- await using tmp = await tmpdir({
139
- init: async (dir) => {
140
- await writeConfig(dir, {
141
- $schema: "https://opencode.ai/config.json",
142
- username: "{env:TEST_VAR}",
143
- })
144
- },
145
- })
146
- await Instance.provide({
147
- directory: tmp.path,
148
- fn: async () => {
149
- const config = await Config.get()
150
- expect(config.username).toBe("test-user")
151
- },
152
- })
153
- } finally {
154
- if (originalEnv !== undefined) {
155
- process.env["TEST_VAR"] = originalEnv
156
- } else {
157
- delete process.env["TEST_VAR"]
158
- }
159
- }
160
- })
161
-
162
- test("preserves env variables when adding $schema to config", async () => {
163
- const originalEnv = process.env["PRESERVE_VAR"]
164
- process.env["PRESERVE_VAR"] = "secret_value"
165
-
166
- try {
167
- await using tmp = await tmpdir({
168
- init: async (dir) => {
169
- // Config without $schema - should trigger auto-add
170
- await Filesystem.write(
171
- path.join(dir, "opencode.json"),
172
- JSON.stringify({
173
- username: "{env:PRESERVE_VAR}",
174
- }),
175
- )
176
- },
177
- })
178
- await Instance.provide({
179
- directory: tmp.path,
180
- fn: async () => {
181
- const config = await Config.get()
182
- expect(config.username).toBe("secret_value")
183
-
184
- // Read the file to verify the env variable was preserved
185
- const content = await Filesystem.readText(path.join(tmp.path, "opencode.json"))
186
- expect(content).toContain("{env:PRESERVE_VAR}")
187
- expect(content).not.toContain("secret_value")
188
- expect(content).toContain("$schema")
189
- },
190
- })
191
- } finally {
192
- if (originalEnv !== undefined) {
193
- process.env["PRESERVE_VAR"] = originalEnv
194
- } else {
195
- delete process.env["PRESERVE_VAR"]
196
- }
197
- }
198
- })
199
-
200
- test("handles file inclusion substitution", async () => {
201
- await using tmp = await tmpdir({
202
- init: async (dir) => {
203
- await Filesystem.write(path.join(dir, "included.txt"), "test-user")
204
- await writeConfig(dir, {
205
- $schema: "https://opencode.ai/config.json",
206
- username: "{file:included.txt}",
207
- })
208
- },
209
- })
210
- await Instance.provide({
211
- directory: tmp.path,
212
- fn: async () => {
213
- const config = await Config.get()
214
- expect(config.username).toBe("test-user")
215
- },
216
- })
217
- })
218
-
219
- test("handles file inclusion with replacement tokens", async () => {
220
- await using tmp = await tmpdir({
221
- init: async (dir) => {
222
- await Filesystem.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
223
- await writeConfig(dir, {
224
- $schema: "https://opencode.ai/config.json",
225
- username: "{file:included.md}",
226
- })
227
- },
228
- })
229
- await Instance.provide({
230
- directory: tmp.path,
231
- fn: async () => {
232
- const config = await Config.get()
233
- expect(config.username).toBe("const out = await Bun.$`echo hi`")
234
- },
235
- })
236
- })
237
-
238
- test("validates config schema and throws on invalid fields", async () => {
239
- await using tmp = await tmpdir({
240
- init: async (dir) => {
241
- await writeConfig(dir, {
242
- $schema: "https://opencode.ai/config.json",
243
- invalid_field: "should cause error",
244
- })
245
- },
246
- })
247
- await Instance.provide({
248
- directory: tmp.path,
249
- fn: async () => {
250
- // Strict schema should throw an error for invalid fields
251
- await expect(Config.get()).rejects.toThrow()
252
- },
253
- })
254
- })
255
-
256
- test("throws error for invalid JSON", async () => {
257
- await using tmp = await tmpdir({
258
- init: async (dir) => {
259
- await Filesystem.write(path.join(dir, "opencode.json"), "{ invalid json }")
260
- },
261
- })
262
- await Instance.provide({
263
- directory: tmp.path,
264
- fn: async () => {
265
- await expect(Config.get()).rejects.toThrow()
266
- },
267
- })
268
- })
269
-
270
- test("handles agent configuration", async () => {
271
- await using tmp = await tmpdir({
272
- init: async (dir) => {
273
- await writeConfig(dir, {
274
- $schema: "https://opencode.ai/config.json",
275
- agent: {
276
- test_agent: {
277
- model: "test/model",
278
- temperature: 0.7,
279
- description: "test agent",
280
- },
281
- },
282
- })
283
- },
284
- })
285
- await Instance.provide({
286
- directory: tmp.path,
287
- fn: async () => {
288
- const config = await Config.get()
289
- expect(config.agent?.["test_agent"]).toEqual(
290
- expect.objectContaining({
291
- model: "test/model",
292
- temperature: 0.7,
293
- description: "test agent",
294
- }),
295
- )
296
- },
297
- })
298
- })
299
-
300
- test("treats agent variant as model-scoped setting (not provider option)", async () => {
301
- await using tmp = await tmpdir({
302
- init: async (dir) => {
303
- await writeConfig(dir, {
304
- $schema: "https://opencode.ai/config.json",
305
- agent: {
306
- test_agent: {
307
- model: "openai/gpt-5.2",
308
- variant: "xhigh",
309
- max_tokens: 123,
310
- },
311
- },
312
- })
313
- },
314
- })
315
-
316
- await Instance.provide({
317
- directory: tmp.path,
318
- fn: async () => {
319
- const config = await Config.get()
320
- const agent = config.agent?.["test_agent"]
321
-
322
- expect(agent?.variant).toBe("xhigh")
323
- expect(agent?.options).toMatchObject({
324
- max_tokens: 123,
325
- })
326
- expect(agent?.options).not.toHaveProperty("variant")
327
- },
328
- })
329
- })
330
-
331
- test("handles command configuration", async () => {
332
- await using tmp = await tmpdir({
333
- init: async (dir) => {
334
- await writeConfig(dir, {
335
- $schema: "https://opencode.ai/config.json",
336
- command: {
337
- test_command: {
338
- template: "test template",
339
- description: "test command",
340
- agent: "test_agent",
341
- },
342
- },
343
- })
344
- },
345
- })
346
- await Instance.provide({
347
- directory: tmp.path,
348
- fn: async () => {
349
- const config = await Config.get()
350
- expect(config.command?.["test_command"]).toEqual({
351
- template: "test template",
352
- description: "test command",
353
- agent: "test_agent",
354
- })
355
- },
356
- })
357
- })
358
-
359
- test("migrates autoshare to share field", async () => {
360
- await using tmp = await tmpdir({
361
- init: async (dir) => {
362
- await Filesystem.write(
363
- path.join(dir, "opencode.json"),
364
- JSON.stringify({
365
- $schema: "https://opencode.ai/config.json",
366
- autoshare: true,
367
- }),
368
- )
369
- },
370
- })
371
- await Instance.provide({
372
- directory: tmp.path,
373
- fn: async () => {
374
- const config = await Config.get()
375
- expect(config.share).toBe("auto")
376
- expect(config.autoshare).toBe(true)
377
- },
378
- })
379
- })
380
-
381
- test("migrates mode field to agent field", async () => {
382
- await using tmp = await tmpdir({
383
- init: async (dir) => {
384
- await Filesystem.write(
385
- path.join(dir, "opencode.json"),
386
- JSON.stringify({
387
- $schema: "https://opencode.ai/config.json",
388
- mode: {
389
- test_mode: {
390
- model: "test/model",
391
- temperature: 0.5,
392
- },
393
- },
394
- }),
395
- )
396
- },
397
- })
398
- await Instance.provide({
399
- directory: tmp.path,
400
- fn: async () => {
401
- const config = await Config.get()
402
- expect(config.agent?.["test_mode"]).toEqual({
403
- model: "test/model",
404
- temperature: 0.5,
405
- mode: "primary",
406
- options: {},
407
- permission: {},
408
- })
409
- },
410
- })
411
- })
412
-
413
- test("loads config from .opencode directory", async () => {
414
- await using tmp = await tmpdir({
415
- init: async (dir) => {
416
- const opencodeDir = path.join(dir, ".opencode")
417
- await fs.mkdir(opencodeDir, { recursive: true })
418
- const agentDir = path.join(opencodeDir, "agent")
419
- await fs.mkdir(agentDir, { recursive: true })
420
-
421
- await Filesystem.write(
422
- path.join(agentDir, "test.md"),
423
- `---
424
- model: test/model
425
- ---
426
- Test agent prompt`,
427
- )
428
- },
429
- })
430
- await Instance.provide({
431
- directory: tmp.path,
432
- fn: async () => {
433
- const config = await Config.get()
434
- expect(config.agent?.["test"]).toEqual(
435
- expect.objectContaining({
436
- name: "test",
437
- model: "test/model",
438
- prompt: "Test agent prompt",
439
- }),
440
- )
441
- },
442
- })
443
- })
444
-
445
- test("loads agents from .opencode/agents (plural)", async () => {
446
- await using tmp = await tmpdir({
447
- init: async (dir) => {
448
- const opencodeDir = path.join(dir, ".opencode")
449
- await fs.mkdir(opencodeDir, { recursive: true })
450
-
451
- const agentsDir = path.join(opencodeDir, "agents")
452
- await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
453
-
454
- await Filesystem.write(
455
- path.join(agentsDir, "helper.md"),
456
- `---
457
- model: test/model
458
- mode: subagent
459
- ---
460
- Helper agent prompt`,
461
- )
462
-
463
- await Filesystem.write(
464
- path.join(agentsDir, "nested", "child.md"),
465
- `---
466
- model: test/model
467
- mode: subagent
468
- ---
469
- Nested agent prompt`,
470
- )
471
- },
472
- })
473
-
474
- await Instance.provide({
475
- directory: tmp.path,
476
- fn: async () => {
477
- const config = await Config.get()
478
-
479
- expect(config.agent?.["helper"]).toMatchObject({
480
- name: "helper",
481
- model: "test/model",
482
- mode: "subagent",
483
- prompt: "Helper agent prompt",
484
- })
485
-
486
- expect(config.agent?.["nested/child"]).toMatchObject({
487
- name: "nested/child",
488
- model: "test/model",
489
- mode: "subagent",
490
- prompt: "Nested agent prompt",
491
- })
492
- },
493
- })
494
- })
495
-
496
- test("loads commands from .opencode/command (singular)", async () => {
497
- await using tmp = await tmpdir({
498
- init: async (dir) => {
499
- const opencodeDir = path.join(dir, ".opencode")
500
- await fs.mkdir(opencodeDir, { recursive: true })
501
-
502
- const commandDir = path.join(opencodeDir, "command")
503
- await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
504
-
505
- await Filesystem.write(
506
- path.join(commandDir, "hello.md"),
507
- `---
508
- description: Test command
509
- ---
510
- Hello from singular command`,
511
- )
512
-
513
- await Filesystem.write(
514
- path.join(commandDir, "nested", "child.md"),
515
- `---
516
- description: Nested command
517
- ---
518
- Nested command template`,
519
- )
520
- },
521
- })
522
-
523
- await Instance.provide({
524
- directory: tmp.path,
525
- fn: async () => {
526
- const config = await Config.get()
527
-
528
- expect(config.command?.["hello"]).toEqual({
529
- description: "Test command",
530
- template: "Hello from singular command",
531
- })
532
-
533
- expect(config.command?.["nested/child"]).toEqual({
534
- description: "Nested command",
535
- template: "Nested command template",
536
- })
537
- },
538
- })
539
- })
540
-
541
- test("loads commands from .opencode/commands (plural)", async () => {
542
- await using tmp = await tmpdir({
543
- init: async (dir) => {
544
- const opencodeDir = path.join(dir, ".opencode")
545
- await fs.mkdir(opencodeDir, { recursive: true })
546
-
547
- const commandsDir = path.join(opencodeDir, "commands")
548
- await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
549
-
550
- await Filesystem.write(
551
- path.join(commandsDir, "hello.md"),
552
- `---
553
- description: Test command
554
- ---
555
- Hello from plural commands`,
556
- )
557
-
558
- await Filesystem.write(
559
- path.join(commandsDir, "nested", "child.md"),
560
- `---
561
- description: Nested command
562
- ---
563
- Nested command template`,
564
- )
565
- },
566
- })
567
-
568
- await Instance.provide({
569
- directory: tmp.path,
570
- fn: async () => {
571
- const config = await Config.get()
572
-
573
- expect(config.command?.["hello"]).toEqual({
574
- description: "Test command",
575
- template: "Hello from plural commands",
576
- })
577
-
578
- expect(config.command?.["nested/child"]).toEqual({
579
- description: "Nested command",
580
- template: "Nested command template",
581
- })
582
- },
583
- })
584
- })
585
-
586
- test("updates config and writes to file", async () => {
587
- await using tmp = await tmpdir()
588
- await Instance.provide({
589
- directory: tmp.path,
590
- fn: async () => {
591
- const newConfig = { model: "updated/model" }
592
- await Config.update(newConfig as any)
593
-
594
- const writtenConfig = await Filesystem.readJson(path.join(tmp.path, "config.json"))
595
- expect(writtenConfig.model).toBe("updated/model")
596
- },
597
- })
598
- })
599
-
600
- test("gets config directories", async () => {
601
- await using tmp = await tmpdir()
602
- await Instance.provide({
603
- directory: tmp.path,
604
- fn: async () => {
605
- const dirs = await Config.directories()
606
- expect(dirs.length).toBeGreaterThanOrEqual(1)
607
- },
608
- })
609
- })
610
-
611
- test("does not try to install dependencies in read-only OPENCODE_CONFIG_DIR", async () => {
612
- if (process.platform === "win32") return
613
-
614
- await using tmp = await tmpdir<string>({
615
- init: async (dir) => {
616
- const ro = path.join(dir, "readonly")
617
- await fs.mkdir(ro, { recursive: true })
618
- await fs.chmod(ro, 0o555)
619
- return ro
620
- },
621
- dispose: async (dir) => {
622
- const ro = path.join(dir, "readonly")
623
- await fs.chmod(ro, 0o755).catch(() => {})
624
- return ro
625
- },
626
- })
627
-
628
- const prev = process.env.OPENCODE_CONFIG_DIR
629
- process.env.OPENCODE_CONFIG_DIR = tmp.extra
630
-
631
- try {
632
- await Instance.provide({
633
- directory: tmp.path,
634
- fn: async () => {
635
- await Config.get()
636
- },
637
- })
638
- } finally {
639
- if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
640
- else process.env.OPENCODE_CONFIG_DIR = prev
641
- }
642
- })
643
-
644
- test("installs dependencies in writable OPENCODE_CONFIG_DIR", async () => {
645
- await using tmp = await tmpdir<string>({
646
- init: async (dir) => {
647
- const cfg = path.join(dir, "configdir")
648
- await fs.mkdir(cfg, { recursive: true })
649
- return cfg
650
- },
651
- })
652
-
653
- const prev = process.env.OPENCODE_CONFIG_DIR
654
- process.env.OPENCODE_CONFIG_DIR = tmp.extra
655
-
656
- try {
657
- await Instance.provide({
658
- directory: tmp.path,
659
- fn: async () => {
660
- await Config.get()
661
- await Config.waitForDependencies()
662
- },
663
- })
664
-
665
- expect(await Filesystem.exists(path.join(tmp.extra, "package.json"))).toBe(true)
666
- expect(await Filesystem.exists(path.join(tmp.extra, ".gitignore"))).toBe(true)
667
- } finally {
668
- if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
669
- else process.env.OPENCODE_CONFIG_DIR = prev
670
- }
671
- })
672
-
673
- test("resolves scoped npm plugins in config", async () => {
674
- await using tmp = await tmpdir({
675
- init: async (dir) => {
676
- const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
677
- await fs.mkdir(pluginDir, { recursive: true })
678
-
679
- await Filesystem.write(
680
- path.join(dir, "package.json"),
681
- JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
682
- )
683
-
684
- await Filesystem.write(
685
- path.join(pluginDir, "package.json"),
686
- JSON.stringify(
687
- {
688
- name: "@scope/plugin",
689
- version: "1.0.0",
690
- type: "module",
691
- main: "./index.js",
692
- },
693
- null,
694
- 2,
695
- ),
696
- )
697
-
698
- await Filesystem.write(path.join(pluginDir, "index.js"), "export default {}\n")
699
-
700
- await Filesystem.write(
701
- path.join(dir, "opencode.json"),
702
- JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
703
- )
704
- },
705
- })
706
-
707
- await Instance.provide({
708
- directory: tmp.path,
709
- fn: async () => {
710
- const config = await Config.get()
711
- const pluginEntries = config.plugin ?? []
712
-
713
- const baseUrl = pathToFileURL(path.join(tmp.path, "opencode.json")).href
714
- const expected = pathToFileURL(path.join(tmp.path, "node_modules", "@scope", "plugin", "index.js")).href
715
-
716
- expect(pluginEntries.includes(expected)).toBe(true)
717
-
718
- const scopedEntry = pluginEntries.find((entry) => entry === expected)
719
- expect(scopedEntry).toBeDefined()
720
- expect(scopedEntry?.includes("/node_modules/@scope/plugin/")).toBe(true)
721
- },
722
- })
723
- })
724
-
725
- test("merges plugin arrays from global and local configs", async () => {
726
- await using tmp = await tmpdir({
727
- init: async (dir) => {
728
- // Create a nested project structure with local .opencode config
729
- const projectDir = path.join(dir, "project")
730
- const opencodeDir = path.join(projectDir, ".opencode")
731
- await fs.mkdir(opencodeDir, { recursive: true })
732
-
733
- // Global config with plugins
734
- await Filesystem.write(
735
- path.join(dir, "opencode.json"),
736
- JSON.stringify({
737
- $schema: "https://opencode.ai/config.json",
738
- plugin: ["global-plugin-1", "global-plugin-2"],
739
- }),
740
- )
741
-
742
- // Local .opencode config with different plugins
743
- await Filesystem.write(
744
- path.join(opencodeDir, "opencode.json"),
745
- JSON.stringify({
746
- $schema: "https://opencode.ai/config.json",
747
- plugin: ["local-plugin-1"],
748
- }),
749
- )
750
- },
751
- })
752
-
753
- await Instance.provide({
754
- directory: path.join(tmp.path, "project"),
755
- fn: async () => {
756
- const config = await Config.get()
757
- const plugins = config.plugin ?? []
758
-
759
- // Should contain both global and local plugins
760
- expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
761
- expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
762
- expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
763
-
764
- // Should have all 3 plugins (not replaced, but merged)
765
- const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
766
- expect(pluginNames.length).toBeGreaterThanOrEqual(3)
767
- },
768
- })
769
- })
770
-
771
- test("does not error when only custom agent is a subagent", async () => {
772
- await using tmp = await tmpdir({
773
- init: async (dir) => {
774
- const opencodeDir = path.join(dir, ".opencode")
775
- await fs.mkdir(opencodeDir, { recursive: true })
776
- const agentDir = path.join(opencodeDir, "agent")
777
- await fs.mkdir(agentDir, { recursive: true })
778
-
779
- await Filesystem.write(
780
- path.join(agentDir, "helper.md"),
781
- `---
782
- model: test/model
783
- mode: subagent
784
- ---
785
- Helper subagent prompt`,
786
- )
787
- },
788
- })
789
- await Instance.provide({
790
- directory: tmp.path,
791
- fn: async () => {
792
- const config = await Config.get()
793
- expect(config.agent?.["helper"]).toMatchObject({
794
- name: "helper",
795
- model: "test/model",
796
- mode: "subagent",
797
- prompt: "Helper subagent prompt",
798
- })
799
- },
800
- })
801
- })
802
-
803
- test("merges instructions arrays from global and local configs", async () => {
804
- await using tmp = await tmpdir({
805
- init: async (dir) => {
806
- const projectDir = path.join(dir, "project")
807
- const opencodeDir = path.join(projectDir, ".opencode")
808
- await fs.mkdir(opencodeDir, { recursive: true })
809
-
810
- await Filesystem.write(
811
- path.join(dir, "opencode.json"),
812
- JSON.stringify({
813
- $schema: "https://opencode.ai/config.json",
814
- instructions: ["global-instructions.md", "shared-rules.md"],
815
- }),
816
- )
817
-
818
- await Filesystem.write(
819
- path.join(opencodeDir, "opencode.json"),
820
- JSON.stringify({
821
- $schema: "https://opencode.ai/config.json",
822
- instructions: ["local-instructions.md"],
823
- }),
824
- )
825
- },
826
- })
827
-
828
- await Instance.provide({
829
- directory: path.join(tmp.path, "project"),
830
- fn: async () => {
831
- const config = await Config.get()
832
- const instructions = config.instructions ?? []
833
-
834
- expect(instructions).toContain("global-instructions.md")
835
- expect(instructions).toContain("shared-rules.md")
836
- expect(instructions).toContain("local-instructions.md")
837
- expect(instructions.length).toBe(3)
838
- },
839
- })
840
- })
841
-
842
- test("deduplicates duplicate instructions from global and local configs", async () => {
843
- await using tmp = await tmpdir({
844
- init: async (dir) => {
845
- const projectDir = path.join(dir, "project")
846
- const opencodeDir = path.join(projectDir, ".opencode")
847
- await fs.mkdir(opencodeDir, { recursive: true })
848
-
849
- await Filesystem.write(
850
- path.join(dir, "opencode.json"),
851
- JSON.stringify({
852
- $schema: "https://opencode.ai/config.json",
853
- instructions: ["duplicate.md", "global-only.md"],
854
- }),
855
- )
856
-
857
- await Filesystem.write(
858
- path.join(opencodeDir, "opencode.json"),
859
- JSON.stringify({
860
- $schema: "https://opencode.ai/config.json",
861
- instructions: ["duplicate.md", "local-only.md"],
862
- }),
863
- )
864
- },
865
- })
866
-
867
- await Instance.provide({
868
- directory: path.join(tmp.path, "project"),
869
- fn: async () => {
870
- const config = await Config.get()
871
- const instructions = config.instructions ?? []
872
-
873
- expect(instructions).toContain("global-only.md")
874
- expect(instructions).toContain("local-only.md")
875
- expect(instructions).toContain("duplicate.md")
876
-
877
- const duplicates = instructions.filter((i) => i === "duplicate.md")
878
- expect(duplicates.length).toBe(1)
879
- expect(instructions.length).toBe(3)
880
- },
881
- })
882
- })
883
-
884
- test("deduplicates duplicate plugins from global and local configs", async () => {
885
- await using tmp = await tmpdir({
886
- init: async (dir) => {
887
- // Create a nested project structure with local .opencode config
888
- const projectDir = path.join(dir, "project")
889
- const opencodeDir = path.join(projectDir, ".opencode")
890
- await fs.mkdir(opencodeDir, { recursive: true })
891
-
892
- // Global config with plugins
893
- await Filesystem.write(
894
- path.join(dir, "opencode.json"),
895
- JSON.stringify({
896
- $schema: "https://opencode.ai/config.json",
897
- plugin: ["duplicate-plugin", "global-plugin-1"],
898
- }),
899
- )
900
-
901
- // Local .opencode config with some overlapping plugins
902
- await Filesystem.write(
903
- path.join(opencodeDir, "opencode.json"),
904
- JSON.stringify({
905
- $schema: "https://opencode.ai/config.json",
906
- plugin: ["duplicate-plugin", "local-plugin-1"],
907
- }),
908
- )
909
- },
910
- })
911
-
912
- await Instance.provide({
913
- directory: path.join(tmp.path, "project"),
914
- fn: async () => {
915
- const config = await Config.get()
916
- const plugins = config.plugin ?? []
917
-
918
- // Should contain all unique plugins
919
- expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
920
- expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
921
- expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
922
-
923
- // Should deduplicate the duplicate plugin
924
- const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
925
- expect(duplicatePlugins.length).toBe(1)
926
-
927
- // Should have exactly 3 unique plugins
928
- const pluginNames = plugins.filter(
929
- (p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
930
- )
931
- expect(pluginNames.length).toBe(3)
932
- },
933
- })
934
- })
935
-
936
- // Legacy tools migration tests
937
-
938
- test("migrates legacy tools config to permissions - allow", async () => {
939
- await using tmp = await tmpdir({
940
- init: async (dir) => {
941
- await Filesystem.write(
942
- path.join(dir, "opencode.json"),
943
- JSON.stringify({
944
- $schema: "https://opencode.ai/config.json",
945
- agent: {
946
- test: {
947
- tools: {
948
- bash: true,
949
- read: true,
950
- },
951
- },
952
- },
953
- }),
954
- )
955
- },
956
- })
957
- await Instance.provide({
958
- directory: tmp.path,
959
- fn: async () => {
960
- const config = await Config.get()
961
- expect(config.agent?.["test"]?.permission).toEqual({
962
- bash: "allow",
963
- read: "allow",
964
- })
965
- },
966
- })
967
- })
968
-
969
- test("migrates legacy tools config to permissions - deny", async () => {
970
- await using tmp = await tmpdir({
971
- init: async (dir) => {
972
- await Filesystem.write(
973
- path.join(dir, "opencode.json"),
974
- JSON.stringify({
975
- $schema: "https://opencode.ai/config.json",
976
- agent: {
977
- test: {
978
- tools: {
979
- bash: false,
980
- webfetch: false,
981
- },
982
- },
983
- },
984
- }),
985
- )
986
- },
987
- })
988
- await Instance.provide({
989
- directory: tmp.path,
990
- fn: async () => {
991
- const config = await Config.get()
992
- expect(config.agent?.["test"]?.permission).toEqual({
993
- bash: "deny",
994
- webfetch: "deny",
995
- })
996
- },
997
- })
998
- })
999
-
1000
- test("migrates legacy write tool to edit permission", async () => {
1001
- await using tmp = await tmpdir({
1002
- init: async (dir) => {
1003
- await Filesystem.write(
1004
- path.join(dir, "opencode.json"),
1005
- JSON.stringify({
1006
- $schema: "https://opencode.ai/config.json",
1007
- agent: {
1008
- test: {
1009
- tools: {
1010
- write: true,
1011
- },
1012
- },
1013
- },
1014
- }),
1015
- )
1016
- },
1017
- })
1018
- await Instance.provide({
1019
- directory: tmp.path,
1020
- fn: async () => {
1021
- const config = await Config.get()
1022
- expect(config.agent?.["test"]?.permission).toEqual({
1023
- edit: "allow",
1024
- })
1025
- },
1026
- })
1027
- })
1028
-
1029
- // Managed settings tests
1030
- // Note: preload.ts sets OPENCODE_TEST_MANAGED_CONFIG which Global.Path.managedConfig uses
1031
-
1032
- test("managed settings override user settings", async () => {
1033
- await using tmp = await tmpdir({
1034
- init: async (dir) => {
1035
- await writeConfig(dir, {
1036
- $schema: "https://opencode.ai/config.json",
1037
- model: "user/model",
1038
- share: "auto",
1039
- username: "testuser",
1040
- })
1041
- },
1042
- })
1043
-
1044
- await writeManagedSettings({
1045
- $schema: "https://opencode.ai/config.json",
1046
- model: "managed/model",
1047
- share: "disabled",
1048
- })
1049
-
1050
- await Instance.provide({
1051
- directory: tmp.path,
1052
- fn: async () => {
1053
- const config = await Config.get()
1054
- expect(config.model).toBe("managed/model")
1055
- expect(config.share).toBe("disabled")
1056
- expect(config.username).toBe("testuser")
1057
- },
1058
- })
1059
- })
1060
-
1061
- test("managed settings override project settings", async () => {
1062
- await using tmp = await tmpdir({
1063
- init: async (dir) => {
1064
- await writeConfig(dir, {
1065
- $schema: "https://opencode.ai/config.json",
1066
- autoupdate: true,
1067
- disabled_providers: [],
1068
- })
1069
- },
1070
- })
1071
-
1072
- await writeManagedSettings({
1073
- $schema: "https://opencode.ai/config.json",
1074
- autoupdate: false,
1075
- disabled_providers: ["openai"],
1076
- })
1077
-
1078
- await Instance.provide({
1079
- directory: tmp.path,
1080
- fn: async () => {
1081
- const config = await Config.get()
1082
- expect(config.autoupdate).toBe(false)
1083
- expect(config.disabled_providers).toEqual(["openai"])
1084
- },
1085
- })
1086
- })
1087
-
1088
- test("missing managed settings file is not an error", async () => {
1089
- await using tmp = await tmpdir({
1090
- init: async (dir) => {
1091
- await writeConfig(dir, {
1092
- $schema: "https://opencode.ai/config.json",
1093
- model: "user/model",
1094
- })
1095
- },
1096
- })
1097
-
1098
- await Instance.provide({
1099
- directory: tmp.path,
1100
- fn: async () => {
1101
- const config = await Config.get()
1102
- expect(config.model).toBe("user/model")
1103
- },
1104
- })
1105
- })
1106
-
1107
- test("migrates legacy edit tool to edit permission", async () => {
1108
- await using tmp = await tmpdir({
1109
- init: async (dir) => {
1110
- await Filesystem.write(
1111
- path.join(dir, "opencode.json"),
1112
- JSON.stringify({
1113
- $schema: "https://opencode.ai/config.json",
1114
- agent: {
1115
- test: {
1116
- tools: {
1117
- edit: false,
1118
- },
1119
- },
1120
- },
1121
- }),
1122
- )
1123
- },
1124
- })
1125
- await Instance.provide({
1126
- directory: tmp.path,
1127
- fn: async () => {
1128
- const config = await Config.get()
1129
- expect(config.agent?.["test"]?.permission).toEqual({
1130
- edit: "deny",
1131
- })
1132
- },
1133
- })
1134
- })
1135
-
1136
- test("migrates legacy patch tool to edit permission", async () => {
1137
- await using tmp = await tmpdir({
1138
- init: async (dir) => {
1139
- await Filesystem.write(
1140
- path.join(dir, "opencode.json"),
1141
- JSON.stringify({
1142
- $schema: "https://opencode.ai/config.json",
1143
- agent: {
1144
- test: {
1145
- tools: {
1146
- patch: true,
1147
- },
1148
- },
1149
- },
1150
- }),
1151
- )
1152
- },
1153
- })
1154
- await Instance.provide({
1155
- directory: tmp.path,
1156
- fn: async () => {
1157
- const config = await Config.get()
1158
- expect(config.agent?.["test"]?.permission).toEqual({
1159
- edit: "allow",
1160
- })
1161
- },
1162
- })
1163
- })
1164
-
1165
- test("migrates legacy multiedit tool to edit permission", async () => {
1166
- await using tmp = await tmpdir({
1167
- init: async (dir) => {
1168
- await Filesystem.write(
1169
- path.join(dir, "opencode.json"),
1170
- JSON.stringify({
1171
- $schema: "https://opencode.ai/config.json",
1172
- agent: {
1173
- test: {
1174
- tools: {
1175
- multiedit: false,
1176
- },
1177
- },
1178
- },
1179
- }),
1180
- )
1181
- },
1182
- })
1183
- await Instance.provide({
1184
- directory: tmp.path,
1185
- fn: async () => {
1186
- const config = await Config.get()
1187
- expect(config.agent?.["test"]?.permission).toEqual({
1188
- edit: "deny",
1189
- })
1190
- },
1191
- })
1192
- })
1193
-
1194
- test("migrates mixed legacy tools config", async () => {
1195
- await using tmp = await tmpdir({
1196
- init: async (dir) => {
1197
- await Filesystem.write(
1198
- path.join(dir, "opencode.json"),
1199
- JSON.stringify({
1200
- $schema: "https://opencode.ai/config.json",
1201
- agent: {
1202
- test: {
1203
- tools: {
1204
- bash: true,
1205
- write: true,
1206
- read: false,
1207
- webfetch: true,
1208
- },
1209
- },
1210
- },
1211
- }),
1212
- )
1213
- },
1214
- })
1215
- await Instance.provide({
1216
- directory: tmp.path,
1217
- fn: async () => {
1218
- const config = await Config.get()
1219
- expect(config.agent?.["test"]?.permission).toEqual({
1220
- bash: "allow",
1221
- edit: "allow",
1222
- read: "deny",
1223
- webfetch: "allow",
1224
- })
1225
- },
1226
- })
1227
- })
1228
-
1229
- test("merges legacy tools with existing permission config", async () => {
1230
- await using tmp = await tmpdir({
1231
- init: async (dir) => {
1232
- await Filesystem.write(
1233
- path.join(dir, "opencode.json"),
1234
- JSON.stringify({
1235
- $schema: "https://opencode.ai/config.json",
1236
- agent: {
1237
- test: {
1238
- permission: {
1239
- glob: "allow",
1240
- },
1241
- tools: {
1242
- bash: true,
1243
- },
1244
- },
1245
- },
1246
- }),
1247
- )
1248
- },
1249
- })
1250
- await Instance.provide({
1251
- directory: tmp.path,
1252
- fn: async () => {
1253
- const config = await Config.get()
1254
- expect(config.agent?.["test"]?.permission).toEqual({
1255
- glob: "allow",
1256
- bash: "allow",
1257
- })
1258
- },
1259
- })
1260
- })
1261
-
1262
- test("permission config preserves key order", async () => {
1263
- await using tmp = await tmpdir({
1264
- init: async (dir) => {
1265
- await Filesystem.write(
1266
- path.join(dir, "opencode.json"),
1267
- JSON.stringify({
1268
- $schema: "https://opencode.ai/config.json",
1269
- permission: {
1270
- "*": "deny",
1271
- edit: "ask",
1272
- write: "ask",
1273
- external_directory: "ask",
1274
- read: "allow",
1275
- todowrite: "allow",
1276
- todoread: "allow",
1277
- "thoughts_*": "allow",
1278
- "reasoning_model_*": "allow",
1279
- "tools_*": "allow",
1280
- "pr_comments_*": "allow",
1281
- },
1282
- }),
1283
- )
1284
- },
1285
- })
1286
- await Instance.provide({
1287
- directory: tmp.path,
1288
- fn: async () => {
1289
- const config = await Config.get()
1290
- expect(Object.keys(config.permission!)).toEqual([
1291
- "*",
1292
- "edit",
1293
- "write",
1294
- "external_directory",
1295
- "read",
1296
- "todowrite",
1297
- "todoread",
1298
- "thoughts_*",
1299
- "reasoning_model_*",
1300
- "tools_*",
1301
- "pr_comments_*",
1302
- ])
1303
- },
1304
- })
1305
- })
1306
-
1307
- // MCP config merging tests
1308
-
1309
- test("project config can override MCP server enabled status", async () => {
1310
- await using tmp = await tmpdir({
1311
- init: async (dir) => {
1312
- // Simulates a base config (like from remote .well-known) with disabled MCP
1313
- await Filesystem.write(
1314
- path.join(dir, "opencode.jsonc"),
1315
- JSON.stringify({
1316
- $schema: "https://opencode.ai/config.json",
1317
- mcp: {
1318
- jira: {
1319
- type: "remote",
1320
- url: "https://jira.example.com/mcp",
1321
- enabled: false,
1322
- },
1323
- wiki: {
1324
- type: "remote",
1325
- url: "https://wiki.example.com/mcp",
1326
- enabled: false,
1327
- },
1328
- },
1329
- }),
1330
- )
1331
- // Project config enables just jira
1332
- await Filesystem.write(
1333
- path.join(dir, "opencode.json"),
1334
- JSON.stringify({
1335
- $schema: "https://opencode.ai/config.json",
1336
- mcp: {
1337
- jira: {
1338
- type: "remote",
1339
- url: "https://jira.example.com/mcp",
1340
- enabled: true,
1341
- },
1342
- },
1343
- }),
1344
- )
1345
- },
1346
- })
1347
- await Instance.provide({
1348
- directory: tmp.path,
1349
- fn: async () => {
1350
- const config = await Config.get()
1351
- // jira should be enabled (overridden by project config)
1352
- expect(config.mcp?.jira).toEqual({
1353
- type: "remote",
1354
- url: "https://jira.example.com/mcp",
1355
- enabled: true,
1356
- })
1357
- // wiki should still be disabled (not overridden)
1358
- expect(config.mcp?.wiki).toEqual({
1359
- type: "remote",
1360
- url: "https://wiki.example.com/mcp",
1361
- enabled: false,
1362
- })
1363
- },
1364
- })
1365
- })
1366
-
1367
- test("MCP config deep merges preserving base config properties", async () => {
1368
- await using tmp = await tmpdir({
1369
- init: async (dir) => {
1370
- // Base config with full MCP definition
1371
- await Filesystem.write(
1372
- path.join(dir, "opencode.jsonc"),
1373
- JSON.stringify({
1374
- $schema: "https://opencode.ai/config.json",
1375
- mcp: {
1376
- myserver: {
1377
- type: "remote",
1378
- url: "https://myserver.example.com/mcp",
1379
- enabled: false,
1380
- headers: {
1381
- "X-Custom-Header": "value",
1382
- },
1383
- },
1384
- },
1385
- }),
1386
- )
1387
- // Override just enables it, should preserve other properties
1388
- await Filesystem.write(
1389
- path.join(dir, "opencode.json"),
1390
- JSON.stringify({
1391
- $schema: "https://opencode.ai/config.json",
1392
- mcp: {
1393
- myserver: {
1394
- type: "remote",
1395
- url: "https://myserver.example.com/mcp",
1396
- enabled: true,
1397
- },
1398
- },
1399
- }),
1400
- )
1401
- },
1402
- })
1403
- await Instance.provide({
1404
- directory: tmp.path,
1405
- fn: async () => {
1406
- const config = await Config.get()
1407
- expect(config.mcp?.myserver).toEqual({
1408
- type: "remote",
1409
- url: "https://myserver.example.com/mcp",
1410
- enabled: true,
1411
- headers: {
1412
- "X-Custom-Header": "value",
1413
- },
1414
- })
1415
- },
1416
- })
1417
- })
1418
-
1419
- test("local .opencode config can override MCP from project config", async () => {
1420
- await using tmp = await tmpdir({
1421
- init: async (dir) => {
1422
- // Project config with disabled MCP
1423
- await Filesystem.write(
1424
- path.join(dir, "opencode.json"),
1425
- JSON.stringify({
1426
- $schema: "https://opencode.ai/config.json",
1427
- mcp: {
1428
- docs: {
1429
- type: "remote",
1430
- url: "https://docs.example.com/mcp",
1431
- enabled: false,
1432
- },
1433
- },
1434
- }),
1435
- )
1436
- // Local .opencode directory config enables it
1437
- const opencodeDir = path.join(dir, ".opencode")
1438
- await fs.mkdir(opencodeDir, { recursive: true })
1439
- await Filesystem.write(
1440
- path.join(opencodeDir, "opencode.json"),
1441
- JSON.stringify({
1442
- $schema: "https://opencode.ai/config.json",
1443
- mcp: {
1444
- docs: {
1445
- type: "remote",
1446
- url: "https://docs.example.com/mcp",
1447
- enabled: true,
1448
- },
1449
- },
1450
- }),
1451
- )
1452
- },
1453
- })
1454
- await Instance.provide({
1455
- directory: tmp.path,
1456
- fn: async () => {
1457
- const config = await Config.get()
1458
- expect(config.mcp?.docs?.enabled).toBe(true)
1459
- },
1460
- })
1461
- })
1462
-
1463
- test("project config overrides remote well-known config", async () => {
1464
- const originalFetch = globalThis.fetch
1465
- let fetchedUrl: string | undefined
1466
- const mockFetch = mock((url: string | URL | Request) => {
1467
- const urlStr = url.toString()
1468
- if (urlStr.includes(".well-known/opencode")) {
1469
- fetchedUrl = urlStr
1470
- return Promise.resolve(
1471
- new Response(
1472
- JSON.stringify({
1473
- config: {
1474
- mcp: {
1475
- jira: {
1476
- type: "remote",
1477
- url: "https://jira.example.com/mcp",
1478
- enabled: false,
1479
- },
1480
- },
1481
- },
1482
- }),
1483
- { status: 200 },
1484
- ),
1485
- )
1486
- }
1487
- return originalFetch(url)
1488
- })
1489
- globalThis.fetch = mockFetch as unknown as typeof fetch
1490
-
1491
- const originalAuthAll = Auth.all
1492
- Auth.all = mock(() =>
1493
- Promise.resolve({
1494
- "https://example.com": {
1495
- type: "wellknown" as const,
1496
- key: "TEST_TOKEN",
1497
- token: "test-token",
1498
- },
1499
- }),
1500
- )
1501
-
1502
- try {
1503
- await using tmp = await tmpdir({
1504
- git: true,
1505
- init: async (dir) => {
1506
- // Project config enables jira (overriding remote default)
1507
- await Filesystem.write(
1508
- path.join(dir, "opencode.json"),
1509
- JSON.stringify({
1510
- $schema: "https://opencode.ai/config.json",
1511
- mcp: {
1512
- jira: {
1513
- type: "remote",
1514
- url: "https://jira.example.com/mcp",
1515
- enabled: true,
1516
- },
1517
- },
1518
- }),
1519
- )
1520
- },
1521
- })
1522
- await Instance.provide({
1523
- directory: tmp.path,
1524
- fn: async () => {
1525
- const config = await Config.get()
1526
- // Verify fetch was called for wellknown config
1527
- expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
1528
- // Project config (enabled: true) should override remote (enabled: false)
1529
- expect(config.mcp?.jira?.enabled).toBe(true)
1530
- },
1531
- })
1532
- } finally {
1533
- globalThis.fetch = originalFetch
1534
- Auth.all = originalAuthAll
1535
- }
1536
- })
1537
-
1538
- describe("getPluginName", () => {
1539
- test("extracts name from file:// URL", () => {
1540
- expect(Config.getPluginName("file:///path/to/plugin/foo.js")).toBe("foo")
1541
- expect(Config.getPluginName("file:///path/to/plugin/bar.ts")).toBe("bar")
1542
- expect(Config.getPluginName("file:///some/path/my-plugin.js")).toBe("my-plugin")
1543
- })
1544
-
1545
- test("extracts name from npm package with version", () => {
1546
- expect(Config.getPluginName("oh-my-opencode@2.4.3")).toBe("oh-my-opencode")
1547
- expect(Config.getPluginName("some-plugin@1.0.0")).toBe("some-plugin")
1548
- expect(Config.getPluginName("plugin@latest")).toBe("plugin")
1549
- })
1550
-
1551
- test("extracts name from scoped npm package", () => {
1552
- expect(Config.getPluginName("@scope/pkg@1.0.0")).toBe("@scope/pkg")
1553
- expect(Config.getPluginName("@opencode/plugin@2.0.0")).toBe("@opencode/plugin")
1554
- })
1555
-
1556
- test("returns full string for package without version", () => {
1557
- expect(Config.getPluginName("some-plugin")).toBe("some-plugin")
1558
- expect(Config.getPluginName("@scope/pkg")).toBe("@scope/pkg")
1559
- })
1560
- })
1561
-
1562
- describe("deduplicatePlugins", () => {
1563
- test("removes duplicates keeping higher priority (later entries)", () => {
1564
- const plugins = ["global-plugin@1.0.0", "shared-plugin@1.0.0", "local-plugin@2.0.0", "shared-plugin@2.0.0"]
1565
-
1566
- const result = Config.deduplicatePlugins(plugins)
1567
-
1568
- expect(result).toContain("global-plugin@1.0.0")
1569
- expect(result).toContain("local-plugin@2.0.0")
1570
- expect(result).toContain("shared-plugin@2.0.0")
1571
- expect(result).not.toContain("shared-plugin@1.0.0")
1572
- expect(result.length).toBe(3)
1573
- })
1574
-
1575
- test("prefers local file over npm package with same name", () => {
1576
- const plugins = ["oh-my-opencode@2.4.3", "file:///project/.opencode/plugin/oh-my-opencode.js"]
1577
-
1578
- const result = Config.deduplicatePlugins(plugins)
1579
-
1580
- expect(result.length).toBe(1)
1581
- expect(result[0]).toBe("file:///project/.opencode/plugin/oh-my-opencode.js")
1582
- })
1583
-
1584
- test("preserves order of remaining plugins", () => {
1585
- const plugins = ["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"]
1586
-
1587
- const result = Config.deduplicatePlugins(plugins)
1588
-
1589
- expect(result).toEqual(["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"])
1590
- })
1591
-
1592
- test("local plugin directory overrides global opencode.json plugin", async () => {
1593
- await using tmp = await tmpdir({
1594
- init: async (dir) => {
1595
- const projectDir = path.join(dir, "project")
1596
- const opencodeDir = path.join(projectDir, ".opencode")
1597
- const pluginDir = path.join(opencodeDir, "plugin")
1598
- await fs.mkdir(pluginDir, { recursive: true })
1599
-
1600
- await Filesystem.write(
1601
- path.join(dir, "opencode.json"),
1602
- JSON.stringify({
1603
- $schema: "https://opencode.ai/config.json",
1604
- plugin: ["my-plugin@1.0.0"],
1605
- }),
1606
- )
1607
-
1608
- await Filesystem.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
1609
- },
1610
- })
1611
-
1612
- await Instance.provide({
1613
- directory: path.join(tmp.path, "project"),
1614
- fn: async () => {
1615
- const config = await Config.get()
1616
- const plugins = config.plugin ?? []
1617
-
1618
- const myPlugins = plugins.filter((p) => Config.getPluginName(p) === "my-plugin")
1619
- expect(myPlugins.length).toBe(1)
1620
- expect(myPlugins[0].startsWith("file://")).toBe(true)
1621
- },
1622
- })
1623
- })
1624
- })
1625
-
1626
- describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
1627
- test("skips project config files when flag is set", async () => {
1628
- const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1629
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1630
-
1631
- try {
1632
- await using tmp = await tmpdir({
1633
- init: async (dir) => {
1634
- // Create a project config that would normally be loaded
1635
- await Filesystem.write(
1636
- path.join(dir, "opencode.json"),
1637
- JSON.stringify({
1638
- $schema: "https://opencode.ai/config.json",
1639
- model: "project/model",
1640
- username: "project-user",
1641
- }),
1642
- )
1643
- },
1644
- })
1645
- await Instance.provide({
1646
- directory: tmp.path,
1647
- fn: async () => {
1648
- const config = await Config.get()
1649
- // Project config should NOT be loaded - model should be default, not "project/model"
1650
- expect(config.model).not.toBe("project/model")
1651
- expect(config.username).not.toBe("project-user")
1652
- },
1653
- })
1654
- } finally {
1655
- if (originalEnv === undefined) {
1656
- delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1657
- } else {
1658
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
1659
- }
1660
- }
1661
- })
1662
-
1663
- test("skips project .opencode/ directories when flag is set", async () => {
1664
- const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1665
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1666
-
1667
- try {
1668
- await using tmp = await tmpdir({
1669
- init: async (dir) => {
1670
- // Create a .opencode directory with a command
1671
- const opencodeDir = path.join(dir, ".opencode", "command")
1672
- await fs.mkdir(opencodeDir, { recursive: true })
1673
- await Filesystem.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
1674
- },
1675
- })
1676
- await Instance.provide({
1677
- directory: tmp.path,
1678
- fn: async () => {
1679
- const directories = await Config.directories()
1680
- // Project .opencode should NOT be in directories list
1681
- const hasProjectOpencode = directories.some((d) => d.startsWith(tmp.path))
1682
- expect(hasProjectOpencode).toBe(false)
1683
- },
1684
- })
1685
- } finally {
1686
- if (originalEnv === undefined) {
1687
- delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1688
- } else {
1689
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
1690
- }
1691
- }
1692
- })
1693
-
1694
- test("still loads global config when flag is set", async () => {
1695
- const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1696
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1697
-
1698
- try {
1699
- await using tmp = await tmpdir()
1700
- await Instance.provide({
1701
- directory: tmp.path,
1702
- fn: async () => {
1703
- // Should still get default config (from global or defaults)
1704
- const config = await Config.get()
1705
- expect(config).toBeDefined()
1706
- expect(config.username).toBeDefined()
1707
- },
1708
- })
1709
- } finally {
1710
- if (originalEnv === undefined) {
1711
- delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1712
- } else {
1713
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
1714
- }
1715
- }
1716
- })
1717
-
1718
- test("skips relative instructions with warning when flag is set but no config dir", async () => {
1719
- const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1720
- const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
1721
-
1722
- try {
1723
- // Ensure no config dir is set
1724
- delete process.env["OPENCODE_CONFIG_DIR"]
1725
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1726
-
1727
- await using tmp = await tmpdir({
1728
- init: async (dir) => {
1729
- // Create a config with relative instruction path
1730
- await Filesystem.write(
1731
- path.join(dir, "opencode.json"),
1732
- JSON.stringify({
1733
- $schema: "https://opencode.ai/config.json",
1734
- instructions: ["./CUSTOM.md"],
1735
- }),
1736
- )
1737
- // Create the instruction file (should be skipped)
1738
- await Filesystem.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
1739
- },
1740
- })
1741
-
1742
- await Instance.provide({
1743
- directory: tmp.path,
1744
- fn: async () => {
1745
- // The relative instruction should be skipped without error
1746
- // We're mainly verifying this doesn't throw and the config loads
1747
- const config = await Config.get()
1748
- expect(config).toBeDefined()
1749
- // The instruction should have been skipped (warning logged)
1750
- // We can't easily test the warning was logged, but we verify
1751
- // the relative path didn't cause an error
1752
- },
1753
- })
1754
- } finally {
1755
- if (originalDisable === undefined) {
1756
- delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1757
- } else {
1758
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
1759
- }
1760
- if (originalConfigDir === undefined) {
1761
- delete process.env["OPENCODE_CONFIG_DIR"]
1762
- } else {
1763
- process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
1764
- }
1765
- }
1766
- })
1767
-
1768
- test("OPENCODE_CONFIG_DIR still works when flag is set", async () => {
1769
- const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1770
- const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
1771
-
1772
- try {
1773
- await using configDirTmp = await tmpdir({
1774
- init: async (dir) => {
1775
- // Create config in the custom config dir
1776
- await Filesystem.write(
1777
- path.join(dir, "opencode.json"),
1778
- JSON.stringify({
1779
- $schema: "https://opencode.ai/config.json",
1780
- model: "configdir/model",
1781
- }),
1782
- )
1783
- },
1784
- })
1785
-
1786
- await using projectTmp = await tmpdir({
1787
- init: async (dir) => {
1788
- // Create config in project (should be ignored)
1789
- await Filesystem.write(
1790
- path.join(dir, "opencode.json"),
1791
- JSON.stringify({
1792
- $schema: "https://opencode.ai/config.json",
1793
- model: "project/model",
1794
- }),
1795
- )
1796
- },
1797
- })
1798
-
1799
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1800
- process.env["OPENCODE_CONFIG_DIR"] = configDirTmp.path
1801
-
1802
- await Instance.provide({
1803
- directory: projectTmp.path,
1804
- fn: async () => {
1805
- const config = await Config.get()
1806
- // Should load from OPENCODE_CONFIG_DIR, not project
1807
- expect(config.model).toBe("configdir/model")
1808
- },
1809
- })
1810
- } finally {
1811
- if (originalDisable === undefined) {
1812
- delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1813
- } else {
1814
- process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
1815
- }
1816
- if (originalConfigDir === undefined) {
1817
- delete process.env["OPENCODE_CONFIG_DIR"]
1818
- } else {
1819
- process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
1820
- }
1821
- }
1822
- })
1823
- })
1824
-
1825
- describe("OPENCODE_CONFIG_CONTENT token substitution", () => {
1826
- test("substitutes {env:} tokens in OPENCODE_CONFIG_CONTENT", async () => {
1827
- const originalEnv = process.env["OPENCODE_CONFIG_CONTENT"]
1828
- const originalTestVar = process.env["TEST_CONFIG_VAR"]
1829
- process.env["TEST_CONFIG_VAR"] = "test_api_key_12345"
1830
- process.env["OPENCODE_CONFIG_CONTENT"] = JSON.stringify({
1831
- $schema: "https://opencode.ai/config.json",
1832
- username: "{env:TEST_CONFIG_VAR}",
1833
- })
1834
-
1835
- try {
1836
- await using tmp = await tmpdir()
1837
- await Instance.provide({
1838
- directory: tmp.path,
1839
- fn: async () => {
1840
- const config = await Config.get()
1841
- expect(config.username).toBe("test_api_key_12345")
1842
- },
1843
- })
1844
- } finally {
1845
- if (originalEnv !== undefined) {
1846
- process.env["OPENCODE_CONFIG_CONTENT"] = originalEnv
1847
- } else {
1848
- delete process.env["OPENCODE_CONFIG_CONTENT"]
1849
- }
1850
- if (originalTestVar !== undefined) {
1851
- process.env["TEST_CONFIG_VAR"] = originalTestVar
1852
- } else {
1853
- delete process.env["TEST_CONFIG_VAR"]
1854
- }
1855
- }
1856
- })
1857
-
1858
- test("substitutes {file:} tokens in OPENCODE_CONFIG_CONTENT", async () => {
1859
- const originalEnv = process.env["OPENCODE_CONFIG_CONTENT"]
1860
-
1861
- try {
1862
- await using tmp = await tmpdir({
1863
- init: async (dir) => {
1864
- await Filesystem.write(path.join(dir, "api_key.txt"), "secret_key_from_file")
1865
- process.env["OPENCODE_CONFIG_CONTENT"] = JSON.stringify({
1866
- $schema: "https://opencode.ai/config.json",
1867
- username: "{file:./api_key.txt}",
1868
- })
1869
- },
1870
- })
1871
- await Instance.provide({
1872
- directory: tmp.path,
1873
- fn: async () => {
1874
- const config = await Config.get()
1875
- expect(config.username).toBe("secret_key_from_file")
1876
- },
1877
- })
1878
- } finally {
1879
- if (originalEnv !== undefined) {
1880
- process.env["OPENCODE_CONFIG_CONTENT"] = originalEnv
1881
- } else {
1882
- delete process.env["OPENCODE_CONFIG_CONTENT"]
1883
- }
1884
- }
1885
- })
1886
- })