2
0

config.test.ts 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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. // Get managed config directory from environment (set in preload.ts)
  11. const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
  12. afterEach(async () => {
  13. await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
  14. })
  15. async function writeManagedSettings(settings: object, filename = "opencode.json") {
  16. await fs.mkdir(managedConfigDir, { recursive: true })
  17. await Bun.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
  18. }
  19. async function writeConfig(dir: string, config: object, name = "opencode.json") {
  20. await Bun.write(path.join(dir, name), JSON.stringify(config))
  21. }
  22. test("loads config with defaults when no files exist", async () => {
  23. await using tmp = await tmpdir()
  24. await Instance.provide({
  25. directory: tmp.path,
  26. fn: async () => {
  27. const config = await Config.get()
  28. expect(config.username).toBeDefined()
  29. },
  30. })
  31. })
  32. test("loads JSON config file", async () => {
  33. await using tmp = await tmpdir({
  34. init: async (dir) => {
  35. await writeConfig(dir, {
  36. $schema: "https://opencode.ai/config.json",
  37. model: "test/model",
  38. username: "testuser",
  39. })
  40. },
  41. })
  42. await Instance.provide({
  43. directory: tmp.path,
  44. fn: async () => {
  45. const config = await Config.get()
  46. expect(config.model).toBe("test/model")
  47. expect(config.username).toBe("testuser")
  48. },
  49. })
  50. })
  51. test("loads JSONC config file", async () => {
  52. await using tmp = await tmpdir({
  53. init: async (dir) => {
  54. await Bun.write(
  55. path.join(dir, "opencode.jsonc"),
  56. `{
  57. // This is a comment
  58. "$schema": "https://opencode.ai/config.json",
  59. "model": "test/model",
  60. "username": "testuser"
  61. }`,
  62. )
  63. },
  64. })
  65. await Instance.provide({
  66. directory: tmp.path,
  67. fn: async () => {
  68. const config = await Config.get()
  69. expect(config.model).toBe("test/model")
  70. expect(config.username).toBe("testuser")
  71. },
  72. })
  73. })
  74. test("merges multiple config files with correct precedence", async () => {
  75. await using tmp = await tmpdir({
  76. init: async (dir) => {
  77. await writeConfig(
  78. dir,
  79. {
  80. $schema: "https://opencode.ai/config.json",
  81. model: "base",
  82. username: "base",
  83. },
  84. "opencode.jsonc",
  85. )
  86. await writeConfig(dir, {
  87. $schema: "https://opencode.ai/config.json",
  88. model: "override",
  89. })
  90. },
  91. })
  92. await Instance.provide({
  93. directory: tmp.path,
  94. fn: async () => {
  95. const config = await Config.get()
  96. expect(config.model).toBe("override")
  97. expect(config.username).toBe("base")
  98. },
  99. })
  100. })
  101. test("handles environment variable substitution", async () => {
  102. const originalEnv = process.env["TEST_VAR"]
  103. process.env["TEST_VAR"] = "test_theme"
  104. try {
  105. await using tmp = await tmpdir({
  106. init: async (dir) => {
  107. await writeConfig(dir, {
  108. $schema: "https://opencode.ai/config.json",
  109. theme: "{env:TEST_VAR}",
  110. })
  111. },
  112. })
  113. await Instance.provide({
  114. directory: tmp.path,
  115. fn: async () => {
  116. const config = await Config.get()
  117. expect(config.theme).toBe("test_theme")
  118. },
  119. })
  120. } finally {
  121. if (originalEnv !== undefined) {
  122. process.env["TEST_VAR"] = originalEnv
  123. } else {
  124. delete process.env["TEST_VAR"]
  125. }
  126. }
  127. })
  128. test("preserves env variables when adding $schema to config", async () => {
  129. const originalEnv = process.env["PRESERVE_VAR"]
  130. process.env["PRESERVE_VAR"] = "secret_value"
  131. try {
  132. await using tmp = await tmpdir({
  133. init: async (dir) => {
  134. // Config without $schema - should trigger auto-add
  135. await Bun.write(
  136. path.join(dir, "opencode.json"),
  137. JSON.stringify({
  138. theme: "{env:PRESERVE_VAR}",
  139. }),
  140. )
  141. },
  142. })
  143. await Instance.provide({
  144. directory: tmp.path,
  145. fn: async () => {
  146. const config = await Config.get()
  147. expect(config.theme).toBe("secret_value")
  148. // Read the file to verify the env variable was preserved
  149. const content = await Bun.file(path.join(tmp.path, "opencode.json")).text()
  150. expect(content).toContain("{env:PRESERVE_VAR}")
  151. expect(content).not.toContain("secret_value")
  152. expect(content).toContain("$schema")
  153. },
  154. })
  155. } finally {
  156. if (originalEnv !== undefined) {
  157. process.env["PRESERVE_VAR"] = originalEnv
  158. } else {
  159. delete process.env["PRESERVE_VAR"]
  160. }
  161. }
  162. })
  163. test("handles file inclusion substitution", async () => {
  164. await using tmp = await tmpdir({
  165. init: async (dir) => {
  166. await Bun.write(path.join(dir, "included.txt"), "test_theme")
  167. await writeConfig(dir, {
  168. $schema: "https://opencode.ai/config.json",
  169. theme: "{file:included.txt}",
  170. })
  171. },
  172. })
  173. await Instance.provide({
  174. directory: tmp.path,
  175. fn: async () => {
  176. const config = await Config.get()
  177. expect(config.theme).toBe("test_theme")
  178. },
  179. })
  180. })
  181. test("validates config schema and throws on invalid fields", async () => {
  182. await using tmp = await tmpdir({
  183. init: async (dir) => {
  184. await writeConfig(dir, {
  185. $schema: "https://opencode.ai/config.json",
  186. invalid_field: "should cause error",
  187. })
  188. },
  189. })
  190. await Instance.provide({
  191. directory: tmp.path,
  192. fn: async () => {
  193. // Strict schema should throw an error for invalid fields
  194. await expect(Config.get()).rejects.toThrow()
  195. },
  196. })
  197. })
  198. test("throws error for invalid JSON", async () => {
  199. await using tmp = await tmpdir({
  200. init: async (dir) => {
  201. await Bun.write(path.join(dir, "opencode.json"), "{ invalid json }")
  202. },
  203. })
  204. await Instance.provide({
  205. directory: tmp.path,
  206. fn: async () => {
  207. await expect(Config.get()).rejects.toThrow()
  208. },
  209. })
  210. })
  211. test("handles agent configuration", async () => {
  212. await using tmp = await tmpdir({
  213. init: async (dir) => {
  214. await writeConfig(dir, {
  215. $schema: "https://opencode.ai/config.json",
  216. agent: {
  217. test_agent: {
  218. model: "test/model",
  219. temperature: 0.7,
  220. description: "test agent",
  221. },
  222. },
  223. })
  224. },
  225. })
  226. await Instance.provide({
  227. directory: tmp.path,
  228. fn: async () => {
  229. const config = await Config.get()
  230. expect(config.agent?.["test_agent"]).toEqual(
  231. expect.objectContaining({
  232. model: "test/model",
  233. temperature: 0.7,
  234. description: "test agent",
  235. }),
  236. )
  237. },
  238. })
  239. })
  240. test("handles command configuration", async () => {
  241. await using tmp = await tmpdir({
  242. init: async (dir) => {
  243. await writeConfig(dir, {
  244. $schema: "https://opencode.ai/config.json",
  245. command: {
  246. test_command: {
  247. template: "test template",
  248. description: "test command",
  249. agent: "test_agent",
  250. },
  251. },
  252. })
  253. },
  254. })
  255. await Instance.provide({
  256. directory: tmp.path,
  257. fn: async () => {
  258. const config = await Config.get()
  259. expect(config.command?.["test_command"]).toEqual({
  260. template: "test template",
  261. description: "test command",
  262. agent: "test_agent",
  263. })
  264. },
  265. })
  266. })
  267. test("migrates autoshare to share field", async () => {
  268. await using tmp = await tmpdir({
  269. init: async (dir) => {
  270. await Bun.write(
  271. path.join(dir, "opencode.json"),
  272. JSON.stringify({
  273. $schema: "https://opencode.ai/config.json",
  274. autoshare: true,
  275. }),
  276. )
  277. },
  278. })
  279. await Instance.provide({
  280. directory: tmp.path,
  281. fn: async () => {
  282. const config = await Config.get()
  283. expect(config.share).toBe("auto")
  284. expect(config.autoshare).toBe(true)
  285. },
  286. })
  287. })
  288. test("migrates mode field to agent field", async () => {
  289. await using tmp = await tmpdir({
  290. init: async (dir) => {
  291. await Bun.write(
  292. path.join(dir, "opencode.json"),
  293. JSON.stringify({
  294. $schema: "https://opencode.ai/config.json",
  295. mode: {
  296. test_mode: {
  297. model: "test/model",
  298. temperature: 0.5,
  299. },
  300. },
  301. }),
  302. )
  303. },
  304. })
  305. await Instance.provide({
  306. directory: tmp.path,
  307. fn: async () => {
  308. const config = await Config.get()
  309. expect(config.agent?.["test_mode"]).toEqual({
  310. model: "test/model",
  311. temperature: 0.5,
  312. mode: "primary",
  313. options: {},
  314. permission: {},
  315. })
  316. },
  317. })
  318. })
  319. test("loads config from .opencode directory", async () => {
  320. await using tmp = await tmpdir({
  321. init: async (dir) => {
  322. const opencodeDir = path.join(dir, ".opencode")
  323. await fs.mkdir(opencodeDir, { recursive: true })
  324. const agentDir = path.join(opencodeDir, "agent")
  325. await fs.mkdir(agentDir, { recursive: true })
  326. await Bun.write(
  327. path.join(agentDir, "test.md"),
  328. `---
  329. model: test/model
  330. ---
  331. Test agent prompt`,
  332. )
  333. },
  334. })
  335. await Instance.provide({
  336. directory: tmp.path,
  337. fn: async () => {
  338. const config = await Config.get()
  339. expect(config.agent?.["test"]).toEqual(
  340. expect.objectContaining({
  341. name: "test",
  342. model: "test/model",
  343. prompt: "Test agent prompt",
  344. }),
  345. )
  346. },
  347. })
  348. })
  349. test("loads agents from .opencode/agents (plural)", async () => {
  350. await using tmp = await tmpdir({
  351. init: async (dir) => {
  352. const opencodeDir = path.join(dir, ".opencode")
  353. await fs.mkdir(opencodeDir, { recursive: true })
  354. const agentsDir = path.join(opencodeDir, "agents")
  355. await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
  356. await Bun.write(
  357. path.join(agentsDir, "helper.md"),
  358. `---
  359. model: test/model
  360. mode: subagent
  361. ---
  362. Helper agent prompt`,
  363. )
  364. await Bun.write(
  365. path.join(agentsDir, "nested", "child.md"),
  366. `---
  367. model: test/model
  368. mode: subagent
  369. ---
  370. Nested agent prompt`,
  371. )
  372. },
  373. })
  374. await Instance.provide({
  375. directory: tmp.path,
  376. fn: async () => {
  377. const config = await Config.get()
  378. expect(config.agent?.["helper"]).toMatchObject({
  379. name: "helper",
  380. model: "test/model",
  381. mode: "subagent",
  382. prompt: "Helper agent prompt",
  383. })
  384. expect(config.agent?.["nested/child"]).toMatchObject({
  385. name: "nested/child",
  386. model: "test/model",
  387. mode: "subagent",
  388. prompt: "Nested agent prompt",
  389. })
  390. },
  391. })
  392. })
  393. test("loads commands from .opencode/command (singular)", async () => {
  394. await using tmp = await tmpdir({
  395. init: async (dir) => {
  396. const opencodeDir = path.join(dir, ".opencode")
  397. await fs.mkdir(opencodeDir, { recursive: true })
  398. const commandDir = path.join(opencodeDir, "command")
  399. await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
  400. await Bun.write(
  401. path.join(commandDir, "hello.md"),
  402. `---
  403. description: Test command
  404. ---
  405. Hello from singular command`,
  406. )
  407. await Bun.write(
  408. path.join(commandDir, "nested", "child.md"),
  409. `---
  410. description: Nested command
  411. ---
  412. Nested command template`,
  413. )
  414. },
  415. })
  416. await Instance.provide({
  417. directory: tmp.path,
  418. fn: async () => {
  419. const config = await Config.get()
  420. expect(config.command?.["hello"]).toEqual({
  421. description: "Test command",
  422. template: "Hello from singular command",
  423. })
  424. expect(config.command?.["nested/child"]).toEqual({
  425. description: "Nested command",
  426. template: "Nested command template",
  427. })
  428. },
  429. })
  430. })
  431. test("loads commands from .opencode/commands (plural)", async () => {
  432. await using tmp = await tmpdir({
  433. init: async (dir) => {
  434. const opencodeDir = path.join(dir, ".opencode")
  435. await fs.mkdir(opencodeDir, { recursive: true })
  436. const commandsDir = path.join(opencodeDir, "commands")
  437. await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
  438. await Bun.write(
  439. path.join(commandsDir, "hello.md"),
  440. `---
  441. description: Test command
  442. ---
  443. Hello from plural commands`,
  444. )
  445. await Bun.write(
  446. path.join(commandsDir, "nested", "child.md"),
  447. `---
  448. description: Nested command
  449. ---
  450. Nested command template`,
  451. )
  452. },
  453. })
  454. await Instance.provide({
  455. directory: tmp.path,
  456. fn: async () => {
  457. const config = await Config.get()
  458. expect(config.command?.["hello"]).toEqual({
  459. description: "Test command",
  460. template: "Hello from plural commands",
  461. })
  462. expect(config.command?.["nested/child"]).toEqual({
  463. description: "Nested command",
  464. template: "Nested command template",
  465. })
  466. },
  467. })
  468. })
  469. test("updates config and writes to file", async () => {
  470. await using tmp = await tmpdir()
  471. await Instance.provide({
  472. directory: tmp.path,
  473. fn: async () => {
  474. const newConfig = { model: "updated/model" }
  475. await Config.update(newConfig as any)
  476. const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text())
  477. expect(writtenConfig.model).toBe("updated/model")
  478. },
  479. })
  480. })
  481. test("gets config directories", async () => {
  482. await using tmp = await tmpdir()
  483. await Instance.provide({
  484. directory: tmp.path,
  485. fn: async () => {
  486. const dirs = await Config.directories()
  487. expect(dirs.length).toBeGreaterThanOrEqual(1)
  488. },
  489. })
  490. })
  491. test("resolves scoped npm plugins in config", async () => {
  492. await using tmp = await tmpdir({
  493. init: async (dir) => {
  494. const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
  495. await fs.mkdir(pluginDir, { recursive: true })
  496. await Bun.write(
  497. path.join(dir, "package.json"),
  498. JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
  499. )
  500. await Bun.write(
  501. path.join(pluginDir, "package.json"),
  502. JSON.stringify(
  503. {
  504. name: "@scope/plugin",
  505. version: "1.0.0",
  506. type: "module",
  507. main: "./index.js",
  508. },
  509. null,
  510. 2,
  511. ),
  512. )
  513. await Bun.write(path.join(pluginDir, "index.js"), "export default {}\n")
  514. await Bun.write(
  515. path.join(dir, "opencode.json"),
  516. JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
  517. )
  518. },
  519. })
  520. await Instance.provide({
  521. directory: tmp.path,
  522. fn: async () => {
  523. const config = await Config.get()
  524. const pluginEntries = config.plugin ?? []
  525. const baseUrl = pathToFileURL(path.join(tmp.path, "opencode.json")).href
  526. const expected = import.meta.resolve("@scope/plugin", baseUrl)
  527. expect(pluginEntries.includes(expected)).toBe(true)
  528. const scopedEntry = pluginEntries.find((entry) => entry === expected)
  529. expect(scopedEntry).toBeDefined()
  530. expect(scopedEntry?.includes("/node_modules/@scope/plugin/")).toBe(true)
  531. },
  532. })
  533. })
  534. test("merges plugin arrays from global and local configs", async () => {
  535. await using tmp = await tmpdir({
  536. init: async (dir) => {
  537. // Create a nested project structure with local .opencode config
  538. const projectDir = path.join(dir, "project")
  539. const opencodeDir = path.join(projectDir, ".opencode")
  540. await fs.mkdir(opencodeDir, { recursive: true })
  541. // Global config with plugins
  542. await Bun.write(
  543. path.join(dir, "opencode.json"),
  544. JSON.stringify({
  545. $schema: "https://opencode.ai/config.json",
  546. plugin: ["global-plugin-1", "global-plugin-2"],
  547. }),
  548. )
  549. // Local .opencode config with different plugins
  550. await Bun.write(
  551. path.join(opencodeDir, "opencode.json"),
  552. JSON.stringify({
  553. $schema: "https://opencode.ai/config.json",
  554. plugin: ["local-plugin-1"],
  555. }),
  556. )
  557. },
  558. })
  559. await Instance.provide({
  560. directory: path.join(tmp.path, "project"),
  561. fn: async () => {
  562. const config = await Config.get()
  563. const plugins = config.plugin ?? []
  564. // Should contain both global and local plugins
  565. expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
  566. expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
  567. expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
  568. // Should have all 3 plugins (not replaced, but merged)
  569. const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
  570. expect(pluginNames.length).toBeGreaterThanOrEqual(3)
  571. },
  572. })
  573. })
  574. test("does not error when only custom agent is a subagent", async () => {
  575. await using tmp = await tmpdir({
  576. init: async (dir) => {
  577. const opencodeDir = path.join(dir, ".opencode")
  578. await fs.mkdir(opencodeDir, { recursive: true })
  579. const agentDir = path.join(opencodeDir, "agent")
  580. await fs.mkdir(agentDir, { recursive: true })
  581. await Bun.write(
  582. path.join(agentDir, "helper.md"),
  583. `---
  584. model: test/model
  585. mode: subagent
  586. ---
  587. Helper subagent prompt`,
  588. )
  589. },
  590. })
  591. await Instance.provide({
  592. directory: tmp.path,
  593. fn: async () => {
  594. const config = await Config.get()
  595. expect(config.agent?.["helper"]).toMatchObject({
  596. name: "helper",
  597. model: "test/model",
  598. mode: "subagent",
  599. prompt: "Helper subagent prompt",
  600. })
  601. },
  602. })
  603. })
  604. test("merges instructions arrays from global and local configs", async () => {
  605. await using tmp = await tmpdir({
  606. init: async (dir) => {
  607. const projectDir = path.join(dir, "project")
  608. const opencodeDir = path.join(projectDir, ".opencode")
  609. await fs.mkdir(opencodeDir, { recursive: true })
  610. await Bun.write(
  611. path.join(dir, "opencode.json"),
  612. JSON.stringify({
  613. $schema: "https://opencode.ai/config.json",
  614. instructions: ["global-instructions.md", "shared-rules.md"],
  615. }),
  616. )
  617. await Bun.write(
  618. path.join(opencodeDir, "opencode.json"),
  619. JSON.stringify({
  620. $schema: "https://opencode.ai/config.json",
  621. instructions: ["local-instructions.md"],
  622. }),
  623. )
  624. },
  625. })
  626. await Instance.provide({
  627. directory: path.join(tmp.path, "project"),
  628. fn: async () => {
  629. const config = await Config.get()
  630. const instructions = config.instructions ?? []
  631. expect(instructions).toContain("global-instructions.md")
  632. expect(instructions).toContain("shared-rules.md")
  633. expect(instructions).toContain("local-instructions.md")
  634. expect(instructions.length).toBe(3)
  635. },
  636. })
  637. })
  638. test("deduplicates duplicate instructions from global and local configs", async () => {
  639. await using tmp = await tmpdir({
  640. init: async (dir) => {
  641. const projectDir = path.join(dir, "project")
  642. const opencodeDir = path.join(projectDir, ".opencode")
  643. await fs.mkdir(opencodeDir, { recursive: true })
  644. await Bun.write(
  645. path.join(dir, "opencode.json"),
  646. JSON.stringify({
  647. $schema: "https://opencode.ai/config.json",
  648. instructions: ["duplicate.md", "global-only.md"],
  649. }),
  650. )
  651. await Bun.write(
  652. path.join(opencodeDir, "opencode.json"),
  653. JSON.stringify({
  654. $schema: "https://opencode.ai/config.json",
  655. instructions: ["duplicate.md", "local-only.md"],
  656. }),
  657. )
  658. },
  659. })
  660. await Instance.provide({
  661. directory: path.join(tmp.path, "project"),
  662. fn: async () => {
  663. const config = await Config.get()
  664. const instructions = config.instructions ?? []
  665. expect(instructions).toContain("global-only.md")
  666. expect(instructions).toContain("local-only.md")
  667. expect(instructions).toContain("duplicate.md")
  668. const duplicates = instructions.filter((i) => i === "duplicate.md")
  669. expect(duplicates.length).toBe(1)
  670. expect(instructions.length).toBe(3)
  671. },
  672. })
  673. })
  674. test("deduplicates duplicate plugins from global and local configs", async () => {
  675. await using tmp = await tmpdir({
  676. init: async (dir) => {
  677. // Create a nested project structure with local .opencode config
  678. const projectDir = path.join(dir, "project")
  679. const opencodeDir = path.join(projectDir, ".opencode")
  680. await fs.mkdir(opencodeDir, { recursive: true })
  681. // Global config with plugins
  682. await Bun.write(
  683. path.join(dir, "opencode.json"),
  684. JSON.stringify({
  685. $schema: "https://opencode.ai/config.json",
  686. plugin: ["duplicate-plugin", "global-plugin-1"],
  687. }),
  688. )
  689. // Local .opencode config with some overlapping plugins
  690. await Bun.write(
  691. path.join(opencodeDir, "opencode.json"),
  692. JSON.stringify({
  693. $schema: "https://opencode.ai/config.json",
  694. plugin: ["duplicate-plugin", "local-plugin-1"],
  695. }),
  696. )
  697. },
  698. })
  699. await Instance.provide({
  700. directory: path.join(tmp.path, "project"),
  701. fn: async () => {
  702. const config = await Config.get()
  703. const plugins = config.plugin ?? []
  704. // Should contain all unique plugins
  705. expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
  706. expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
  707. expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
  708. // Should deduplicate the duplicate plugin
  709. const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
  710. expect(duplicatePlugins.length).toBe(1)
  711. // Should have exactly 3 unique plugins
  712. const pluginNames = plugins.filter(
  713. (p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
  714. )
  715. expect(pluginNames.length).toBe(3)
  716. },
  717. })
  718. })
  719. // Legacy tools migration tests
  720. test("migrates legacy tools config to permissions - allow", async () => {
  721. await using tmp = await tmpdir({
  722. init: async (dir) => {
  723. await Bun.write(
  724. path.join(dir, "opencode.json"),
  725. JSON.stringify({
  726. $schema: "https://opencode.ai/config.json",
  727. agent: {
  728. test: {
  729. tools: {
  730. bash: true,
  731. read: true,
  732. },
  733. },
  734. },
  735. }),
  736. )
  737. },
  738. })
  739. await Instance.provide({
  740. directory: tmp.path,
  741. fn: async () => {
  742. const config = await Config.get()
  743. expect(config.agent?.["test"]?.permission).toEqual({
  744. bash: "allow",
  745. read: "allow",
  746. })
  747. },
  748. })
  749. })
  750. test("migrates legacy tools config to permissions - deny", async () => {
  751. await using tmp = await tmpdir({
  752. init: async (dir) => {
  753. await Bun.write(
  754. path.join(dir, "opencode.json"),
  755. JSON.stringify({
  756. $schema: "https://opencode.ai/config.json",
  757. agent: {
  758. test: {
  759. tools: {
  760. bash: false,
  761. webfetch: false,
  762. },
  763. },
  764. },
  765. }),
  766. )
  767. },
  768. })
  769. await Instance.provide({
  770. directory: tmp.path,
  771. fn: async () => {
  772. const config = await Config.get()
  773. expect(config.agent?.["test"]?.permission).toEqual({
  774. bash: "deny",
  775. webfetch: "deny",
  776. })
  777. },
  778. })
  779. })
  780. test("migrates legacy write tool to edit permission", async () => {
  781. await using tmp = await tmpdir({
  782. init: async (dir) => {
  783. await Bun.write(
  784. path.join(dir, "opencode.json"),
  785. JSON.stringify({
  786. $schema: "https://opencode.ai/config.json",
  787. agent: {
  788. test: {
  789. tools: {
  790. write: true,
  791. },
  792. },
  793. },
  794. }),
  795. )
  796. },
  797. })
  798. await Instance.provide({
  799. directory: tmp.path,
  800. fn: async () => {
  801. const config = await Config.get()
  802. expect(config.agent?.["test"]?.permission).toEqual({
  803. edit: "allow",
  804. })
  805. },
  806. })
  807. })
  808. // Managed settings tests
  809. // Note: preload.ts sets OPENCODE_TEST_MANAGED_CONFIG which Global.Path.managedConfig uses
  810. test("managed settings override user settings", async () => {
  811. await using tmp = await tmpdir({
  812. init: async (dir) => {
  813. await writeConfig(dir, {
  814. $schema: "https://opencode.ai/config.json",
  815. model: "user/model",
  816. share: "auto",
  817. username: "testuser",
  818. })
  819. },
  820. })
  821. await writeManagedSettings({
  822. $schema: "https://opencode.ai/config.json",
  823. model: "managed/model",
  824. share: "disabled",
  825. })
  826. await Instance.provide({
  827. directory: tmp.path,
  828. fn: async () => {
  829. const config = await Config.get()
  830. expect(config.model).toBe("managed/model")
  831. expect(config.share).toBe("disabled")
  832. expect(config.username).toBe("testuser")
  833. },
  834. })
  835. })
  836. test("managed settings override project settings", async () => {
  837. await using tmp = await tmpdir({
  838. init: async (dir) => {
  839. await writeConfig(dir, {
  840. $schema: "https://opencode.ai/config.json",
  841. autoupdate: true,
  842. disabled_providers: [],
  843. theme: "dark",
  844. })
  845. },
  846. })
  847. await writeManagedSettings({
  848. $schema: "https://opencode.ai/config.json",
  849. autoupdate: false,
  850. disabled_providers: ["openai"],
  851. })
  852. await Instance.provide({
  853. directory: tmp.path,
  854. fn: async () => {
  855. const config = await Config.get()
  856. expect(config.autoupdate).toBe(false)
  857. expect(config.disabled_providers).toEqual(["openai"])
  858. expect(config.theme).toBe("dark")
  859. },
  860. })
  861. })
  862. test("missing managed settings file is not an error", async () => {
  863. await using tmp = await tmpdir({
  864. init: async (dir) => {
  865. await writeConfig(dir, {
  866. $schema: "https://opencode.ai/config.json",
  867. model: "user/model",
  868. })
  869. },
  870. })
  871. await Instance.provide({
  872. directory: tmp.path,
  873. fn: async () => {
  874. const config = await Config.get()
  875. expect(config.model).toBe("user/model")
  876. },
  877. })
  878. })
  879. test("migrates legacy edit tool to edit permission", async () => {
  880. await using tmp = await tmpdir({
  881. init: async (dir) => {
  882. await Bun.write(
  883. path.join(dir, "opencode.json"),
  884. JSON.stringify({
  885. $schema: "https://opencode.ai/config.json",
  886. agent: {
  887. test: {
  888. tools: {
  889. edit: false,
  890. },
  891. },
  892. },
  893. }),
  894. )
  895. },
  896. })
  897. await Instance.provide({
  898. directory: tmp.path,
  899. fn: async () => {
  900. const config = await Config.get()
  901. expect(config.agent?.["test"]?.permission).toEqual({
  902. edit: "deny",
  903. })
  904. },
  905. })
  906. })
  907. test("migrates legacy patch tool to edit permission", async () => {
  908. await using tmp = await tmpdir({
  909. init: async (dir) => {
  910. await Bun.write(
  911. path.join(dir, "opencode.json"),
  912. JSON.stringify({
  913. $schema: "https://opencode.ai/config.json",
  914. agent: {
  915. test: {
  916. tools: {
  917. patch: true,
  918. },
  919. },
  920. },
  921. }),
  922. )
  923. },
  924. })
  925. await Instance.provide({
  926. directory: tmp.path,
  927. fn: async () => {
  928. const config = await Config.get()
  929. expect(config.agent?.["test"]?.permission).toEqual({
  930. edit: "allow",
  931. })
  932. },
  933. })
  934. })
  935. test("migrates legacy multiedit tool to edit permission", async () => {
  936. await using tmp = await tmpdir({
  937. init: async (dir) => {
  938. await Bun.write(
  939. path.join(dir, "opencode.json"),
  940. JSON.stringify({
  941. $schema: "https://opencode.ai/config.json",
  942. agent: {
  943. test: {
  944. tools: {
  945. multiedit: false,
  946. },
  947. },
  948. },
  949. }),
  950. )
  951. },
  952. })
  953. await Instance.provide({
  954. directory: tmp.path,
  955. fn: async () => {
  956. const config = await Config.get()
  957. expect(config.agent?.["test"]?.permission).toEqual({
  958. edit: "deny",
  959. })
  960. },
  961. })
  962. })
  963. test("migrates mixed legacy tools config", async () => {
  964. await using tmp = await tmpdir({
  965. init: async (dir) => {
  966. await Bun.write(
  967. path.join(dir, "opencode.json"),
  968. JSON.stringify({
  969. $schema: "https://opencode.ai/config.json",
  970. agent: {
  971. test: {
  972. tools: {
  973. bash: true,
  974. write: true,
  975. read: false,
  976. webfetch: true,
  977. },
  978. },
  979. },
  980. }),
  981. )
  982. },
  983. })
  984. await Instance.provide({
  985. directory: tmp.path,
  986. fn: async () => {
  987. const config = await Config.get()
  988. expect(config.agent?.["test"]?.permission).toEqual({
  989. bash: "allow",
  990. edit: "allow",
  991. read: "deny",
  992. webfetch: "allow",
  993. })
  994. },
  995. })
  996. })
  997. test("merges legacy tools with existing permission config", async () => {
  998. await using tmp = await tmpdir({
  999. init: async (dir) => {
  1000. await Bun.write(
  1001. path.join(dir, "opencode.json"),
  1002. JSON.stringify({
  1003. $schema: "https://opencode.ai/config.json",
  1004. agent: {
  1005. test: {
  1006. permission: {
  1007. glob: "allow",
  1008. },
  1009. tools: {
  1010. bash: 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. glob: "allow",
  1024. bash: "allow",
  1025. })
  1026. },
  1027. })
  1028. })
  1029. test("permission config preserves key order", async () => {
  1030. await using tmp = await tmpdir({
  1031. init: async (dir) => {
  1032. await Bun.write(
  1033. path.join(dir, "opencode.json"),
  1034. JSON.stringify({
  1035. $schema: "https://opencode.ai/config.json",
  1036. permission: {
  1037. "*": "deny",
  1038. edit: "ask",
  1039. write: "ask",
  1040. external_directory: "ask",
  1041. read: "allow",
  1042. todowrite: "allow",
  1043. todoread: "allow",
  1044. "thoughts_*": "allow",
  1045. "reasoning_model_*": "allow",
  1046. "tools_*": "allow",
  1047. "pr_comments_*": "allow",
  1048. },
  1049. }),
  1050. )
  1051. },
  1052. })
  1053. await Instance.provide({
  1054. directory: tmp.path,
  1055. fn: async () => {
  1056. const config = await Config.get()
  1057. expect(Object.keys(config.permission!)).toEqual([
  1058. "*",
  1059. "edit",
  1060. "write",
  1061. "external_directory",
  1062. "read",
  1063. "todowrite",
  1064. "todoread",
  1065. "thoughts_*",
  1066. "reasoning_model_*",
  1067. "tools_*",
  1068. "pr_comments_*",
  1069. ])
  1070. },
  1071. })
  1072. })
  1073. // MCP config merging tests
  1074. test("project config can override MCP server enabled status", async () => {
  1075. await using tmp = await tmpdir({
  1076. init: async (dir) => {
  1077. // Simulates a base config (like from remote .well-known) with disabled MCP
  1078. await Bun.write(
  1079. path.join(dir, "opencode.jsonc"),
  1080. JSON.stringify({
  1081. $schema: "https://opencode.ai/config.json",
  1082. mcp: {
  1083. jira: {
  1084. type: "remote",
  1085. url: "https://jira.example.com/mcp",
  1086. enabled: false,
  1087. },
  1088. wiki: {
  1089. type: "remote",
  1090. url: "https://wiki.example.com/mcp",
  1091. enabled: false,
  1092. },
  1093. },
  1094. }),
  1095. )
  1096. // Project config enables just jira
  1097. await Bun.write(
  1098. path.join(dir, "opencode.json"),
  1099. JSON.stringify({
  1100. $schema: "https://opencode.ai/config.json",
  1101. mcp: {
  1102. jira: {
  1103. type: "remote",
  1104. url: "https://jira.example.com/mcp",
  1105. enabled: true,
  1106. },
  1107. },
  1108. }),
  1109. )
  1110. },
  1111. })
  1112. await Instance.provide({
  1113. directory: tmp.path,
  1114. fn: async () => {
  1115. const config = await Config.get()
  1116. // jira should be enabled (overridden by project config)
  1117. expect(config.mcp?.jira).toEqual({
  1118. type: "remote",
  1119. url: "https://jira.example.com/mcp",
  1120. enabled: true,
  1121. })
  1122. // wiki should still be disabled (not overridden)
  1123. expect(config.mcp?.wiki).toEqual({
  1124. type: "remote",
  1125. url: "https://wiki.example.com/mcp",
  1126. enabled: false,
  1127. })
  1128. },
  1129. })
  1130. })
  1131. test("MCP config deep merges preserving base config properties", async () => {
  1132. await using tmp = await tmpdir({
  1133. init: async (dir) => {
  1134. // Base config with full MCP definition
  1135. await Bun.write(
  1136. path.join(dir, "opencode.jsonc"),
  1137. JSON.stringify({
  1138. $schema: "https://opencode.ai/config.json",
  1139. mcp: {
  1140. myserver: {
  1141. type: "remote",
  1142. url: "https://myserver.example.com/mcp",
  1143. enabled: false,
  1144. headers: {
  1145. "X-Custom-Header": "value",
  1146. },
  1147. },
  1148. },
  1149. }),
  1150. )
  1151. // Override just enables it, should preserve other properties
  1152. await Bun.write(
  1153. path.join(dir, "opencode.json"),
  1154. JSON.stringify({
  1155. $schema: "https://opencode.ai/config.json",
  1156. mcp: {
  1157. myserver: {
  1158. type: "remote",
  1159. url: "https://myserver.example.com/mcp",
  1160. enabled: true,
  1161. },
  1162. },
  1163. }),
  1164. )
  1165. },
  1166. })
  1167. await Instance.provide({
  1168. directory: tmp.path,
  1169. fn: async () => {
  1170. const config = await Config.get()
  1171. expect(config.mcp?.myserver).toEqual({
  1172. type: "remote",
  1173. url: "https://myserver.example.com/mcp",
  1174. enabled: true,
  1175. headers: {
  1176. "X-Custom-Header": "value",
  1177. },
  1178. })
  1179. },
  1180. })
  1181. })
  1182. test("local .opencode config can override MCP from project config", async () => {
  1183. await using tmp = await tmpdir({
  1184. init: async (dir) => {
  1185. // Project config with disabled MCP
  1186. await Bun.write(
  1187. path.join(dir, "opencode.json"),
  1188. JSON.stringify({
  1189. $schema: "https://opencode.ai/config.json",
  1190. mcp: {
  1191. docs: {
  1192. type: "remote",
  1193. url: "https://docs.example.com/mcp",
  1194. enabled: false,
  1195. },
  1196. },
  1197. }),
  1198. )
  1199. // Local .opencode directory config enables it
  1200. const opencodeDir = path.join(dir, ".opencode")
  1201. await fs.mkdir(opencodeDir, { recursive: true })
  1202. await Bun.write(
  1203. path.join(opencodeDir, "opencode.json"),
  1204. JSON.stringify({
  1205. $schema: "https://opencode.ai/config.json",
  1206. mcp: {
  1207. docs: {
  1208. type: "remote",
  1209. url: "https://docs.example.com/mcp",
  1210. enabled: true,
  1211. },
  1212. },
  1213. }),
  1214. )
  1215. },
  1216. })
  1217. await Instance.provide({
  1218. directory: tmp.path,
  1219. fn: async () => {
  1220. const config = await Config.get()
  1221. expect(config.mcp?.docs?.enabled).toBe(true)
  1222. },
  1223. })
  1224. })
  1225. test("project config overrides remote well-known config", async () => {
  1226. const originalFetch = globalThis.fetch
  1227. let fetchedUrl: string | undefined
  1228. const mockFetch = mock((url: string | URL | Request) => {
  1229. const urlStr = url.toString()
  1230. if (urlStr.includes(".well-known/opencode")) {
  1231. fetchedUrl = urlStr
  1232. return Promise.resolve(
  1233. new Response(
  1234. JSON.stringify({
  1235. config: {
  1236. mcp: {
  1237. jira: {
  1238. type: "remote",
  1239. url: "https://jira.example.com/mcp",
  1240. enabled: false,
  1241. },
  1242. },
  1243. },
  1244. }),
  1245. { status: 200 },
  1246. ),
  1247. )
  1248. }
  1249. return originalFetch(url)
  1250. })
  1251. globalThis.fetch = mockFetch as unknown as typeof fetch
  1252. const originalAuthAll = Auth.all
  1253. Auth.all = mock(() =>
  1254. Promise.resolve({
  1255. "https://example.com": {
  1256. type: "wellknown" as const,
  1257. key: "TEST_TOKEN",
  1258. token: "test-token",
  1259. },
  1260. }),
  1261. )
  1262. try {
  1263. await using tmp = await tmpdir({
  1264. git: true,
  1265. init: async (dir) => {
  1266. // Project config enables jira (overriding remote default)
  1267. await Bun.write(
  1268. path.join(dir, "opencode.json"),
  1269. JSON.stringify({
  1270. $schema: "https://opencode.ai/config.json",
  1271. mcp: {
  1272. jira: {
  1273. type: "remote",
  1274. url: "https://jira.example.com/mcp",
  1275. enabled: true,
  1276. },
  1277. },
  1278. }),
  1279. )
  1280. },
  1281. })
  1282. await Instance.provide({
  1283. directory: tmp.path,
  1284. fn: async () => {
  1285. const config = await Config.get()
  1286. // Verify fetch was called for wellknown config
  1287. expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
  1288. // Project config (enabled: true) should override remote (enabled: false)
  1289. expect(config.mcp?.jira?.enabled).toBe(true)
  1290. },
  1291. })
  1292. } finally {
  1293. globalThis.fetch = originalFetch
  1294. Auth.all = originalAuthAll
  1295. }
  1296. })
  1297. describe("getPluginName", () => {
  1298. test("extracts name from file:// URL", () => {
  1299. expect(Config.getPluginName("file:///path/to/plugin/foo.js")).toBe("foo")
  1300. expect(Config.getPluginName("file:///path/to/plugin/bar.ts")).toBe("bar")
  1301. expect(Config.getPluginName("file:///some/path/my-plugin.js")).toBe("my-plugin")
  1302. })
  1303. test("extracts name from npm package with version", () => {
  1304. expect(Config.getPluginName("[email protected]")).toBe("oh-my-opencode")
  1305. expect(Config.getPluginName("[email protected]")).toBe("some-plugin")
  1306. expect(Config.getPluginName("plugin@latest")).toBe("plugin")
  1307. })
  1308. test("extracts name from scoped npm package", () => {
  1309. expect(Config.getPluginName("@scope/[email protected]")).toBe("@scope/pkg")
  1310. expect(Config.getPluginName("@opencode/[email protected]")).toBe("@opencode/plugin")
  1311. })
  1312. test("returns full string for package without version", () => {
  1313. expect(Config.getPluginName("some-plugin")).toBe("some-plugin")
  1314. expect(Config.getPluginName("@scope/pkg")).toBe("@scope/pkg")
  1315. })
  1316. })
  1317. describe("deduplicatePlugins", () => {
  1318. test("removes duplicates keeping higher priority (later entries)", () => {
  1319. const plugins = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
  1320. const result = Config.deduplicatePlugins(plugins)
  1321. expect(result).toContain("[email protected]")
  1322. expect(result).toContain("[email protected]")
  1323. expect(result).toContain("[email protected]")
  1324. expect(result).not.toContain("[email protected]")
  1325. expect(result.length).toBe(3)
  1326. })
  1327. test("prefers local file over npm package with same name", () => {
  1328. const plugins = ["[email protected]", "file:///project/.opencode/plugin/oh-my-opencode.js"]
  1329. const result = Config.deduplicatePlugins(plugins)
  1330. expect(result.length).toBe(1)
  1331. expect(result[0]).toBe("file:///project/.opencode/plugin/oh-my-opencode.js")
  1332. })
  1333. test("preserves order of remaining plugins", () => {
  1334. const plugins = ["[email protected]", "[email protected]", "[email protected]"]
  1335. const result = Config.deduplicatePlugins(plugins)
  1336. expect(result).toEqual(["[email protected]", "[email protected]", "[email protected]"])
  1337. })
  1338. test("local plugin directory overrides global opencode.json plugin", async () => {
  1339. await using tmp = await tmpdir({
  1340. init: async (dir) => {
  1341. const projectDir = path.join(dir, "project")
  1342. const opencodeDir = path.join(projectDir, ".opencode")
  1343. const pluginDir = path.join(opencodeDir, "plugin")
  1344. await fs.mkdir(pluginDir, { recursive: true })
  1345. await Bun.write(
  1346. path.join(dir, "opencode.json"),
  1347. JSON.stringify({
  1348. $schema: "https://opencode.ai/config.json",
  1349. plugin: ["[email protected]"],
  1350. }),
  1351. )
  1352. await Bun.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
  1353. },
  1354. })
  1355. await Instance.provide({
  1356. directory: path.join(tmp.path, "project"),
  1357. fn: async () => {
  1358. const config = await Config.get()
  1359. const plugins = config.plugin ?? []
  1360. const myPlugins = plugins.filter((p) => Config.getPluginName(p) === "my-plugin")
  1361. expect(myPlugins.length).toBe(1)
  1362. expect(myPlugins[0].startsWith("file://")).toBe(true)
  1363. },
  1364. })
  1365. })
  1366. })
  1367. describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
  1368. test("skips project config files when flag is set", async () => {
  1369. const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1370. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
  1371. try {
  1372. await using tmp = await tmpdir({
  1373. init: async (dir) => {
  1374. // Create a project config that would normally be loaded
  1375. await Bun.write(
  1376. path.join(dir, "opencode.json"),
  1377. JSON.stringify({
  1378. $schema: "https://opencode.ai/config.json",
  1379. model: "project/model",
  1380. username: "project-user",
  1381. }),
  1382. )
  1383. },
  1384. })
  1385. await Instance.provide({
  1386. directory: tmp.path,
  1387. fn: async () => {
  1388. const config = await Config.get()
  1389. // Project config should NOT be loaded - model should be default, not "project/model"
  1390. expect(config.model).not.toBe("project/model")
  1391. expect(config.username).not.toBe("project-user")
  1392. },
  1393. })
  1394. } finally {
  1395. if (originalEnv === undefined) {
  1396. delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1397. } else {
  1398. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
  1399. }
  1400. }
  1401. })
  1402. test("skips project .opencode/ directories when flag is set", async () => {
  1403. const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1404. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
  1405. try {
  1406. await using tmp = await tmpdir({
  1407. init: async (dir) => {
  1408. // Create a .opencode directory with a command
  1409. const opencodeDir = path.join(dir, ".opencode", "command")
  1410. await fs.mkdir(opencodeDir, { recursive: true })
  1411. await Bun.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
  1412. },
  1413. })
  1414. await Instance.provide({
  1415. directory: tmp.path,
  1416. fn: async () => {
  1417. const directories = await Config.directories()
  1418. // Project .opencode should NOT be in directories list
  1419. const hasProjectOpencode = directories.some((d) => d.startsWith(tmp.path))
  1420. expect(hasProjectOpencode).toBe(false)
  1421. },
  1422. })
  1423. } finally {
  1424. if (originalEnv === undefined) {
  1425. delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1426. } else {
  1427. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
  1428. }
  1429. }
  1430. })
  1431. test("still loads global config when flag is set", async () => {
  1432. const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1433. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
  1434. try {
  1435. await using tmp = await tmpdir()
  1436. await Instance.provide({
  1437. directory: tmp.path,
  1438. fn: async () => {
  1439. // Should still get default config (from global or defaults)
  1440. const config = await Config.get()
  1441. expect(config).toBeDefined()
  1442. expect(config.username).toBeDefined()
  1443. },
  1444. })
  1445. } finally {
  1446. if (originalEnv === undefined) {
  1447. delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1448. } else {
  1449. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
  1450. }
  1451. }
  1452. })
  1453. test("skips relative instructions with warning when flag is set but no config dir", async () => {
  1454. const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1455. const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
  1456. try {
  1457. // Ensure no config dir is set
  1458. delete process.env["OPENCODE_CONFIG_DIR"]
  1459. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
  1460. await using tmp = await tmpdir({
  1461. init: async (dir) => {
  1462. // Create a config with relative instruction path
  1463. await Bun.write(
  1464. path.join(dir, "opencode.json"),
  1465. JSON.stringify({
  1466. $schema: "https://opencode.ai/config.json",
  1467. instructions: ["./CUSTOM.md"],
  1468. }),
  1469. )
  1470. // Create the instruction file (should be skipped)
  1471. await Bun.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
  1472. },
  1473. })
  1474. await Instance.provide({
  1475. directory: tmp.path,
  1476. fn: async () => {
  1477. // The relative instruction should be skipped without error
  1478. // We're mainly verifying this doesn't throw and the config loads
  1479. const config = await Config.get()
  1480. expect(config).toBeDefined()
  1481. // The instruction should have been skipped (warning logged)
  1482. // We can't easily test the warning was logged, but we verify
  1483. // the relative path didn't cause an error
  1484. },
  1485. })
  1486. } finally {
  1487. if (originalDisable === undefined) {
  1488. delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1489. } else {
  1490. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
  1491. }
  1492. if (originalConfigDir === undefined) {
  1493. delete process.env["OPENCODE_CONFIG_DIR"]
  1494. } else {
  1495. process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
  1496. }
  1497. }
  1498. })
  1499. test("OPENCODE_CONFIG_DIR still works when flag is set", async () => {
  1500. const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1501. const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
  1502. try {
  1503. await using configDirTmp = await tmpdir({
  1504. init: async (dir) => {
  1505. // Create config in the custom config dir
  1506. await Bun.write(
  1507. path.join(dir, "opencode.json"),
  1508. JSON.stringify({
  1509. $schema: "https://opencode.ai/config.json",
  1510. model: "configdir/model",
  1511. }),
  1512. )
  1513. },
  1514. })
  1515. await using projectTmp = await tmpdir({
  1516. init: async (dir) => {
  1517. // Create config in project (should be ignored)
  1518. await Bun.write(
  1519. path.join(dir, "opencode.json"),
  1520. JSON.stringify({
  1521. $schema: "https://opencode.ai/config.json",
  1522. model: "project/model",
  1523. }),
  1524. )
  1525. },
  1526. })
  1527. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
  1528. process.env["OPENCODE_CONFIG_DIR"] = configDirTmp.path
  1529. await Instance.provide({
  1530. directory: projectTmp.path,
  1531. fn: async () => {
  1532. const config = await Config.get()
  1533. // Should load from OPENCODE_CONFIG_DIR, not project
  1534. expect(config.model).toBe("configdir/model")
  1535. },
  1536. })
  1537. } finally {
  1538. if (originalDisable === undefined) {
  1539. delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
  1540. } else {
  1541. process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
  1542. }
  1543. if (originalConfigDir === undefined) {
  1544. delete process.env["OPENCODE_CONFIG_DIR"]
  1545. } else {
  1546. process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
  1547. }
  1548. }
  1549. })
  1550. })