config.test.ts 44 KB

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