config.test.ts 49 KB

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