2
0

config.test.ts 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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("handles file inclusion substitution", async () => {
  124. await using tmp = await tmpdir({
  125. init: async (dir) => {
  126. await Bun.write(path.join(dir, "included.txt"), "test_theme")
  127. await Bun.write(
  128. path.join(dir, "opencode.json"),
  129. JSON.stringify({
  130. $schema: "https://opencode.ai/config.json",
  131. theme: "{file:included.txt}",
  132. }),
  133. )
  134. },
  135. })
  136. await Instance.provide({
  137. directory: tmp.path,
  138. fn: async () => {
  139. const config = await Config.get()
  140. expect(config.theme).toBe("test_theme")
  141. },
  142. })
  143. })
  144. test("validates config schema and throws on invalid fields", async () => {
  145. await using tmp = await tmpdir({
  146. init: async (dir) => {
  147. await Bun.write(
  148. path.join(dir, "opencode.json"),
  149. JSON.stringify({
  150. $schema: "https://opencode.ai/config.json",
  151. invalid_field: "should cause error",
  152. }),
  153. )
  154. },
  155. })
  156. await Instance.provide({
  157. directory: tmp.path,
  158. fn: async () => {
  159. // Strict schema should throw an error for invalid fields
  160. await expect(Config.get()).rejects.toThrow()
  161. },
  162. })
  163. })
  164. test("throws error for invalid JSON", async () => {
  165. await using tmp = await tmpdir({
  166. init: async (dir) => {
  167. await Bun.write(path.join(dir, "opencode.json"), "{ invalid json }")
  168. },
  169. })
  170. await Instance.provide({
  171. directory: tmp.path,
  172. fn: async () => {
  173. await expect(Config.get()).rejects.toThrow()
  174. },
  175. })
  176. })
  177. test("handles agent configuration", async () => {
  178. await using tmp = await tmpdir({
  179. init: async (dir) => {
  180. await Bun.write(
  181. path.join(dir, "opencode.json"),
  182. JSON.stringify({
  183. $schema: "https://opencode.ai/config.json",
  184. agent: {
  185. test_agent: {
  186. model: "test/model",
  187. temperature: 0.7,
  188. description: "test agent",
  189. },
  190. },
  191. }),
  192. )
  193. },
  194. })
  195. await Instance.provide({
  196. directory: tmp.path,
  197. fn: async () => {
  198. const config = await Config.get()
  199. expect(config.agent?.["test_agent"]).toEqual(
  200. expect.objectContaining({
  201. model: "test/model",
  202. temperature: 0.7,
  203. description: "test agent",
  204. }),
  205. )
  206. },
  207. })
  208. })
  209. test("handles command configuration", async () => {
  210. await using tmp = await tmpdir({
  211. init: async (dir) => {
  212. await Bun.write(
  213. path.join(dir, "opencode.json"),
  214. JSON.stringify({
  215. $schema: "https://opencode.ai/config.json",
  216. command: {
  217. test_command: {
  218. template: "test template",
  219. description: "test command",
  220. agent: "test_agent",
  221. },
  222. },
  223. }),
  224. )
  225. },
  226. })
  227. await Instance.provide({
  228. directory: tmp.path,
  229. fn: async () => {
  230. const config = await Config.get()
  231. expect(config.command?.["test_command"]).toEqual({
  232. template: "test template",
  233. description: "test command",
  234. agent: "test_agent",
  235. })
  236. },
  237. })
  238. })
  239. test("migrates autoshare to share field", async () => {
  240. await using tmp = await tmpdir({
  241. init: async (dir) => {
  242. await Bun.write(
  243. path.join(dir, "opencode.json"),
  244. JSON.stringify({
  245. $schema: "https://opencode.ai/config.json",
  246. autoshare: true,
  247. }),
  248. )
  249. },
  250. })
  251. await Instance.provide({
  252. directory: tmp.path,
  253. fn: async () => {
  254. const config = await Config.get()
  255. expect(config.share).toBe("auto")
  256. expect(config.autoshare).toBe(true)
  257. },
  258. })
  259. })
  260. test("migrates mode field to agent field", async () => {
  261. await using tmp = await tmpdir({
  262. init: async (dir) => {
  263. await Bun.write(
  264. path.join(dir, "opencode.json"),
  265. JSON.stringify({
  266. $schema: "https://opencode.ai/config.json",
  267. mode: {
  268. test_mode: {
  269. model: "test/model",
  270. temperature: 0.5,
  271. },
  272. },
  273. }),
  274. )
  275. },
  276. })
  277. await Instance.provide({
  278. directory: tmp.path,
  279. fn: async () => {
  280. const config = await Config.get()
  281. expect(config.agent?.["test_mode"]).toEqual({
  282. model: "test/model",
  283. temperature: 0.5,
  284. mode: "primary",
  285. options: {},
  286. permission: {},
  287. })
  288. },
  289. })
  290. })
  291. test("loads config from .opencode directory", async () => {
  292. await using tmp = await tmpdir({
  293. init: async (dir) => {
  294. const opencodeDir = path.join(dir, ".opencode")
  295. await fs.mkdir(opencodeDir, { recursive: true })
  296. const agentDir = path.join(opencodeDir, "agent")
  297. await fs.mkdir(agentDir, { recursive: true })
  298. await Bun.write(
  299. path.join(agentDir, "test.md"),
  300. `---
  301. model: test/model
  302. ---
  303. Test agent prompt`,
  304. )
  305. },
  306. })
  307. await Instance.provide({
  308. directory: tmp.path,
  309. fn: async () => {
  310. const config = await Config.get()
  311. expect(config.agent?.["test"]).toEqual(
  312. expect.objectContaining({
  313. name: "test",
  314. model: "test/model",
  315. prompt: "Test agent prompt",
  316. }),
  317. )
  318. },
  319. })
  320. })
  321. test("loads agents from .opencode/agents (plural)", async () => {
  322. await using tmp = await tmpdir({
  323. init: async (dir) => {
  324. const opencodeDir = path.join(dir, ".opencode")
  325. await fs.mkdir(opencodeDir, { recursive: true })
  326. const agentsDir = path.join(opencodeDir, "agents")
  327. await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
  328. await Bun.write(
  329. path.join(agentsDir, "helper.md"),
  330. `---
  331. model: test/model
  332. mode: subagent
  333. ---
  334. Helper agent prompt`,
  335. )
  336. await Bun.write(
  337. path.join(agentsDir, "nested", "child.md"),
  338. `---
  339. model: test/model
  340. mode: subagent
  341. ---
  342. Nested agent prompt`,
  343. )
  344. },
  345. })
  346. await Instance.provide({
  347. directory: tmp.path,
  348. fn: async () => {
  349. const config = await Config.get()
  350. expect(config.agent?.["helper"]).toMatchObject({
  351. name: "helper",
  352. model: "test/model",
  353. mode: "subagent",
  354. prompt: "Helper agent prompt",
  355. })
  356. expect(config.agent?.["nested/child"]).toMatchObject({
  357. name: "nested/child",
  358. model: "test/model",
  359. mode: "subagent",
  360. prompt: "Nested agent prompt",
  361. })
  362. },
  363. })
  364. })
  365. test("loads commands from .opencode/command (singular)", 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 commandDir = path.join(opencodeDir, "command")
  371. await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
  372. await Bun.write(
  373. path.join(commandDir, "hello.md"),
  374. `---
  375. description: Test command
  376. ---
  377. Hello from singular command`,
  378. )
  379. await Bun.write(
  380. path.join(commandDir, "nested", "child.md"),
  381. `---
  382. description: Nested command
  383. ---
  384. Nested command template`,
  385. )
  386. },
  387. })
  388. await Instance.provide({
  389. directory: tmp.path,
  390. fn: async () => {
  391. const config = await Config.get()
  392. expect(config.command?.["hello"]).toEqual({
  393. description: "Test command",
  394. template: "Hello from singular command",
  395. })
  396. expect(config.command?.["nested/child"]).toEqual({
  397. description: "Nested command",
  398. template: "Nested command template",
  399. })
  400. },
  401. })
  402. })
  403. test("loads commands from .opencode/commands (plural)", async () => {
  404. await using tmp = await tmpdir({
  405. init: async (dir) => {
  406. const opencodeDir = path.join(dir, ".opencode")
  407. await fs.mkdir(opencodeDir, { recursive: true })
  408. const commandsDir = path.join(opencodeDir, "commands")
  409. await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
  410. await Bun.write(
  411. path.join(commandsDir, "hello.md"),
  412. `---
  413. description: Test command
  414. ---
  415. Hello from plural commands`,
  416. )
  417. await Bun.write(
  418. path.join(commandsDir, "nested", "child.md"),
  419. `---
  420. description: Nested command
  421. ---
  422. Nested command template`,
  423. )
  424. },
  425. })
  426. await Instance.provide({
  427. directory: tmp.path,
  428. fn: async () => {
  429. const config = await Config.get()
  430. expect(config.command?.["hello"]).toEqual({
  431. description: "Test command",
  432. template: "Hello from plural commands",
  433. })
  434. expect(config.command?.["nested/child"]).toEqual({
  435. description: "Nested command",
  436. template: "Nested command template",
  437. })
  438. },
  439. })
  440. })
  441. test("updates config and writes to file", async () => {
  442. await using tmp = await tmpdir()
  443. await Instance.provide({
  444. directory: tmp.path,
  445. fn: async () => {
  446. const newConfig = { model: "updated/model" }
  447. await Config.update(newConfig as any)
  448. const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text())
  449. expect(writtenConfig.model).toBe("updated/model")
  450. },
  451. })
  452. })
  453. test("gets config directories", async () => {
  454. await using tmp = await tmpdir()
  455. await Instance.provide({
  456. directory: tmp.path,
  457. fn: async () => {
  458. const dirs = await Config.directories()
  459. expect(dirs.length).toBeGreaterThanOrEqual(1)
  460. },
  461. })
  462. })
  463. test("resolves scoped npm plugins in config", async () => {
  464. await using tmp = await tmpdir({
  465. init: async (dir) => {
  466. const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
  467. await fs.mkdir(pluginDir, { recursive: true })
  468. await Bun.write(
  469. path.join(dir, "package.json"),
  470. JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
  471. )
  472. await Bun.write(
  473. path.join(pluginDir, "package.json"),
  474. JSON.stringify(
  475. {
  476. name: "@scope/plugin",
  477. version: "1.0.0",
  478. type: "module",
  479. main: "./index.js",
  480. },
  481. null,
  482. 2,
  483. ),
  484. )
  485. await Bun.write(path.join(pluginDir, "index.js"), "export default {}\n")
  486. await Bun.write(
  487. path.join(dir, "opencode.json"),
  488. JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
  489. )
  490. },
  491. })
  492. await Instance.provide({
  493. directory: tmp.path,
  494. fn: async () => {
  495. const config = await Config.get()
  496. const pluginEntries = config.plugin ?? []
  497. const baseUrl = pathToFileURL(path.join(tmp.path, "opencode.json")).href
  498. const expected = import.meta.resolve("@scope/plugin", baseUrl)
  499. expect(pluginEntries.includes(expected)).toBe(true)
  500. const scopedEntry = pluginEntries.find((entry) => entry === expected)
  501. expect(scopedEntry).toBeDefined()
  502. expect(scopedEntry?.includes("/node_modules/@scope/plugin/")).toBe(true)
  503. },
  504. })
  505. })
  506. test("merges plugin arrays from global and local configs", async () => {
  507. await using tmp = await tmpdir({
  508. init: async (dir) => {
  509. // Create a nested project structure with local .opencode config
  510. const projectDir = path.join(dir, "project")
  511. const opencodeDir = path.join(projectDir, ".opencode")
  512. await fs.mkdir(opencodeDir, { recursive: true })
  513. // Global config with plugins
  514. await Bun.write(
  515. path.join(dir, "opencode.json"),
  516. JSON.stringify({
  517. $schema: "https://opencode.ai/config.json",
  518. plugin: ["global-plugin-1", "global-plugin-2"],
  519. }),
  520. )
  521. // Local .opencode config with different plugins
  522. await Bun.write(
  523. path.join(opencodeDir, "opencode.json"),
  524. JSON.stringify({
  525. $schema: "https://opencode.ai/config.json",
  526. plugin: ["local-plugin-1"],
  527. }),
  528. )
  529. },
  530. })
  531. await Instance.provide({
  532. directory: path.join(tmp.path, "project"),
  533. fn: async () => {
  534. const config = await Config.get()
  535. const plugins = config.plugin ?? []
  536. // Should contain both global and local plugins
  537. expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
  538. expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
  539. expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
  540. // Should have all 3 plugins (not replaced, but merged)
  541. const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
  542. expect(pluginNames.length).toBeGreaterThanOrEqual(3)
  543. },
  544. })
  545. })
  546. test("does not error when only custom agent is a subagent", async () => {
  547. await using tmp = await tmpdir({
  548. init: async (dir) => {
  549. const opencodeDir = path.join(dir, ".opencode")
  550. await fs.mkdir(opencodeDir, { recursive: true })
  551. const agentDir = path.join(opencodeDir, "agent")
  552. await fs.mkdir(agentDir, { recursive: true })
  553. await Bun.write(
  554. path.join(agentDir, "helper.md"),
  555. `---
  556. model: test/model
  557. mode: subagent
  558. ---
  559. Helper subagent prompt`,
  560. )
  561. },
  562. })
  563. await Instance.provide({
  564. directory: tmp.path,
  565. fn: async () => {
  566. const config = await Config.get()
  567. expect(config.agent?.["helper"]).toMatchObject({
  568. name: "helper",
  569. model: "test/model",
  570. mode: "subagent",
  571. prompt: "Helper subagent prompt",
  572. })
  573. },
  574. })
  575. })
  576. test("merges instructions arrays from global and local configs", async () => {
  577. await using tmp = await tmpdir({
  578. init: async (dir) => {
  579. const projectDir = path.join(dir, "project")
  580. const opencodeDir = path.join(projectDir, ".opencode")
  581. await fs.mkdir(opencodeDir, { recursive: true })
  582. await Bun.write(
  583. path.join(dir, "opencode.json"),
  584. JSON.stringify({
  585. $schema: "https://opencode.ai/config.json",
  586. instructions: ["global-instructions.md", "shared-rules.md"],
  587. }),
  588. )
  589. await Bun.write(
  590. path.join(opencodeDir, "opencode.json"),
  591. JSON.stringify({
  592. $schema: "https://opencode.ai/config.json",
  593. instructions: ["local-instructions.md"],
  594. }),
  595. )
  596. },
  597. })
  598. await Instance.provide({
  599. directory: path.join(tmp.path, "project"),
  600. fn: async () => {
  601. const config = await Config.get()
  602. const instructions = config.instructions ?? []
  603. expect(instructions).toContain("global-instructions.md")
  604. expect(instructions).toContain("shared-rules.md")
  605. expect(instructions).toContain("local-instructions.md")
  606. expect(instructions.length).toBe(3)
  607. },
  608. })
  609. })
  610. test("deduplicates duplicate instructions from global and local configs", async () => {
  611. await using tmp = await tmpdir({
  612. init: async (dir) => {
  613. const projectDir = path.join(dir, "project")
  614. const opencodeDir = path.join(projectDir, ".opencode")
  615. await fs.mkdir(opencodeDir, { recursive: true })
  616. await Bun.write(
  617. path.join(dir, "opencode.json"),
  618. JSON.stringify({
  619. $schema: "https://opencode.ai/config.json",
  620. instructions: ["duplicate.md", "global-only.md"],
  621. }),
  622. )
  623. await Bun.write(
  624. path.join(opencodeDir, "opencode.json"),
  625. JSON.stringify({
  626. $schema: "https://opencode.ai/config.json",
  627. instructions: ["duplicate.md", "local-only.md"],
  628. }),
  629. )
  630. },
  631. })
  632. await Instance.provide({
  633. directory: path.join(tmp.path, "project"),
  634. fn: async () => {
  635. const config = await Config.get()
  636. const instructions = config.instructions ?? []
  637. expect(instructions).toContain("global-only.md")
  638. expect(instructions).toContain("local-only.md")
  639. expect(instructions).toContain("duplicate.md")
  640. const duplicates = instructions.filter((i) => i === "duplicate.md")
  641. expect(duplicates.length).toBe(1)
  642. expect(instructions.length).toBe(3)
  643. },
  644. })
  645. })
  646. test("deduplicates duplicate plugins from global and local configs", async () => {
  647. await using tmp = await tmpdir({
  648. init: async (dir) => {
  649. // Create a nested project structure with local .opencode config
  650. const projectDir = path.join(dir, "project")
  651. const opencodeDir = path.join(projectDir, ".opencode")
  652. await fs.mkdir(opencodeDir, { recursive: true })
  653. // Global config with plugins
  654. await Bun.write(
  655. path.join(dir, "opencode.json"),
  656. JSON.stringify({
  657. $schema: "https://opencode.ai/config.json",
  658. plugin: ["duplicate-plugin", "global-plugin-1"],
  659. }),
  660. )
  661. // Local .opencode config with some overlapping plugins
  662. await Bun.write(
  663. path.join(opencodeDir, "opencode.json"),
  664. JSON.stringify({
  665. $schema: "https://opencode.ai/config.json",
  666. plugin: ["duplicate-plugin", "local-plugin-1"],
  667. }),
  668. )
  669. },
  670. })
  671. await Instance.provide({
  672. directory: path.join(tmp.path, "project"),
  673. fn: async () => {
  674. const config = await Config.get()
  675. const plugins = config.plugin ?? []
  676. // Should contain all unique plugins
  677. expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
  678. expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
  679. expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
  680. // Should deduplicate the duplicate plugin
  681. const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
  682. expect(duplicatePlugins.length).toBe(1)
  683. // Should have exactly 3 unique plugins
  684. const pluginNames = plugins.filter(
  685. (p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
  686. )
  687. expect(pluginNames.length).toBe(3)
  688. },
  689. })
  690. })
  691. // Legacy tools migration tests
  692. test("migrates legacy tools config to permissions - allow", async () => {
  693. await using tmp = await tmpdir({
  694. init: async (dir) => {
  695. await Bun.write(
  696. path.join(dir, "opencode.json"),
  697. JSON.stringify({
  698. $schema: "https://opencode.ai/config.json",
  699. agent: {
  700. test: {
  701. tools: {
  702. bash: true,
  703. read: true,
  704. },
  705. },
  706. },
  707. }),
  708. )
  709. },
  710. })
  711. await Instance.provide({
  712. directory: tmp.path,
  713. fn: async () => {
  714. const config = await Config.get()
  715. expect(config.agent?.["test"]?.permission).toEqual({
  716. bash: "allow",
  717. read: "allow",
  718. })
  719. },
  720. })
  721. })
  722. test("migrates legacy tools config to permissions - deny", async () => {
  723. await using tmp = await tmpdir({
  724. init: async (dir) => {
  725. await Bun.write(
  726. path.join(dir, "opencode.json"),
  727. JSON.stringify({
  728. $schema: "https://opencode.ai/config.json",
  729. agent: {
  730. test: {
  731. tools: {
  732. bash: false,
  733. webfetch: false,
  734. },
  735. },
  736. },
  737. }),
  738. )
  739. },
  740. })
  741. await Instance.provide({
  742. directory: tmp.path,
  743. fn: async () => {
  744. const config = await Config.get()
  745. expect(config.agent?.["test"]?.permission).toEqual({
  746. bash: "deny",
  747. webfetch: "deny",
  748. })
  749. },
  750. })
  751. })
  752. test("migrates legacy write tool to edit permission", async () => {
  753. await using tmp = await tmpdir({
  754. init: async (dir) => {
  755. await Bun.write(
  756. path.join(dir, "opencode.json"),
  757. JSON.stringify({
  758. $schema: "https://opencode.ai/config.json",
  759. agent: {
  760. test: {
  761. tools: {
  762. write: true,
  763. },
  764. },
  765. },
  766. }),
  767. )
  768. },
  769. })
  770. await Instance.provide({
  771. directory: tmp.path,
  772. fn: async () => {
  773. const config = await Config.get()
  774. expect(config.agent?.["test"]?.permission).toEqual({
  775. edit: "allow",
  776. })
  777. },
  778. })
  779. })
  780. test("migrates legacy edit tool to edit permission", async () => {
  781. await using tmp = await tmpdir({
  782. init: async (dir) => {
  783. await Bun.write(
  784. path.join(dir, "opencode.json"),
  785. JSON.stringify({
  786. $schema: "https://opencode.ai/config.json",
  787. agent: {
  788. test: {
  789. tools: {
  790. edit: false,
  791. },
  792. },
  793. },
  794. }),
  795. )
  796. },
  797. })
  798. await Instance.provide({
  799. directory: tmp.path,
  800. fn: async () => {
  801. const config = await Config.get()
  802. expect(config.agent?.["test"]?.permission).toEqual({
  803. edit: "deny",
  804. })
  805. },
  806. })
  807. })
  808. test("migrates legacy patch tool to edit permission", async () => {
  809. await using tmp = await tmpdir({
  810. init: async (dir) => {
  811. await Bun.write(
  812. path.join(dir, "opencode.json"),
  813. JSON.stringify({
  814. $schema: "https://opencode.ai/config.json",
  815. agent: {
  816. test: {
  817. tools: {
  818. patch: true,
  819. },
  820. },
  821. },
  822. }),
  823. )
  824. },
  825. })
  826. await Instance.provide({
  827. directory: tmp.path,
  828. fn: async () => {
  829. const config = await Config.get()
  830. expect(config.agent?.["test"]?.permission).toEqual({
  831. edit: "allow",
  832. })
  833. },
  834. })
  835. })
  836. test("migrates legacy multiedit tool to edit permission", async () => {
  837. await using tmp = await tmpdir({
  838. init: async (dir) => {
  839. await Bun.write(
  840. path.join(dir, "opencode.json"),
  841. JSON.stringify({
  842. $schema: "https://opencode.ai/config.json",
  843. agent: {
  844. test: {
  845. tools: {
  846. multiedit: false,
  847. },
  848. },
  849. },
  850. }),
  851. )
  852. },
  853. })
  854. await Instance.provide({
  855. directory: tmp.path,
  856. fn: async () => {
  857. const config = await Config.get()
  858. expect(config.agent?.["test"]?.permission).toEqual({
  859. edit: "deny",
  860. })
  861. },
  862. })
  863. })
  864. test("migrates mixed legacy tools config", async () => {
  865. await using tmp = await tmpdir({
  866. init: async (dir) => {
  867. await Bun.write(
  868. path.join(dir, "opencode.json"),
  869. JSON.stringify({
  870. $schema: "https://opencode.ai/config.json",
  871. agent: {
  872. test: {
  873. tools: {
  874. bash: true,
  875. write: true,
  876. read: false,
  877. webfetch: true,
  878. },
  879. },
  880. },
  881. }),
  882. )
  883. },
  884. })
  885. await Instance.provide({
  886. directory: tmp.path,
  887. fn: async () => {
  888. const config = await Config.get()
  889. expect(config.agent?.["test"]?.permission).toEqual({
  890. bash: "allow",
  891. edit: "allow",
  892. read: "deny",
  893. webfetch: "allow",
  894. })
  895. },
  896. })
  897. })
  898. test("merges legacy tools with existing permission config", async () => {
  899. await using tmp = await tmpdir({
  900. init: async (dir) => {
  901. await Bun.write(
  902. path.join(dir, "opencode.json"),
  903. JSON.stringify({
  904. $schema: "https://opencode.ai/config.json",
  905. agent: {
  906. test: {
  907. permission: {
  908. glob: "allow",
  909. },
  910. tools: {
  911. bash: true,
  912. },
  913. },
  914. },
  915. }),
  916. )
  917. },
  918. })
  919. await Instance.provide({
  920. directory: tmp.path,
  921. fn: async () => {
  922. const config = await Config.get()
  923. expect(config.agent?.["test"]?.permission).toEqual({
  924. glob: "allow",
  925. bash: "allow",
  926. })
  927. },
  928. })
  929. })
  930. test("permission config preserves key order", async () => {
  931. await using tmp = await tmpdir({
  932. init: async (dir) => {
  933. await Bun.write(
  934. path.join(dir, "opencode.json"),
  935. JSON.stringify({
  936. $schema: "https://opencode.ai/config.json",
  937. permission: {
  938. "*": "deny",
  939. edit: "ask",
  940. write: "ask",
  941. external_directory: "ask",
  942. read: "allow",
  943. todowrite: "allow",
  944. todoread: "allow",
  945. "thoughts_*": "allow",
  946. "reasoning_model_*": "allow",
  947. "tools_*": "allow",
  948. "pr_comments_*": "allow",
  949. },
  950. }),
  951. )
  952. },
  953. })
  954. await Instance.provide({
  955. directory: tmp.path,
  956. fn: async () => {
  957. const config = await Config.get()
  958. expect(Object.keys(config.permission!)).toEqual([
  959. "*",
  960. "edit",
  961. "write",
  962. "external_directory",
  963. "read",
  964. "todowrite",
  965. "todoread",
  966. "thoughts_*",
  967. "reasoning_model_*",
  968. "tools_*",
  969. "pr_comments_*",
  970. ])
  971. },
  972. })
  973. })
  974. // MCP config merging tests
  975. test("project config can override MCP server enabled status", async () => {
  976. await using tmp = await tmpdir({
  977. init: async (dir) => {
  978. // Simulates a base config (like from remote .well-known) with disabled MCP
  979. await Bun.write(
  980. path.join(dir, "opencode.jsonc"),
  981. JSON.stringify({
  982. $schema: "https://opencode.ai/config.json",
  983. mcp: {
  984. jira: {
  985. type: "remote",
  986. url: "https://jira.example.com/mcp",
  987. enabled: false,
  988. },
  989. wiki: {
  990. type: "remote",
  991. url: "https://wiki.example.com/mcp",
  992. enabled: false,
  993. },
  994. },
  995. }),
  996. )
  997. // Project config enables just jira
  998. await Bun.write(
  999. path.join(dir, "opencode.json"),
  1000. JSON.stringify({
  1001. $schema: "https://opencode.ai/config.json",
  1002. mcp: {
  1003. jira: {
  1004. type: "remote",
  1005. url: "https://jira.example.com/mcp",
  1006. enabled: true,
  1007. },
  1008. },
  1009. }),
  1010. )
  1011. },
  1012. })
  1013. await Instance.provide({
  1014. directory: tmp.path,
  1015. fn: async () => {
  1016. const config = await Config.get()
  1017. // jira should be enabled (overridden by project config)
  1018. expect(config.mcp?.jira).toEqual({
  1019. type: "remote",
  1020. url: "https://jira.example.com/mcp",
  1021. enabled: true,
  1022. })
  1023. // wiki should still be disabled (not overridden)
  1024. expect(config.mcp?.wiki).toEqual({
  1025. type: "remote",
  1026. url: "https://wiki.example.com/mcp",
  1027. enabled: false,
  1028. })
  1029. },
  1030. })
  1031. })
  1032. test("MCP config deep merges preserving base config properties", async () => {
  1033. await using tmp = await tmpdir({
  1034. init: async (dir) => {
  1035. // Base config with full MCP definition
  1036. await Bun.write(
  1037. path.join(dir, "opencode.jsonc"),
  1038. JSON.stringify({
  1039. $schema: "https://opencode.ai/config.json",
  1040. mcp: {
  1041. myserver: {
  1042. type: "remote",
  1043. url: "https://myserver.example.com/mcp",
  1044. enabled: false,
  1045. headers: {
  1046. "X-Custom-Header": "value",
  1047. },
  1048. },
  1049. },
  1050. }),
  1051. )
  1052. // Override just enables it, should preserve other properties
  1053. await Bun.write(
  1054. path.join(dir, "opencode.json"),
  1055. JSON.stringify({
  1056. $schema: "https://opencode.ai/config.json",
  1057. mcp: {
  1058. myserver: {
  1059. type: "remote",
  1060. url: "https://myserver.example.com/mcp",
  1061. enabled: true,
  1062. },
  1063. },
  1064. }),
  1065. )
  1066. },
  1067. })
  1068. await Instance.provide({
  1069. directory: tmp.path,
  1070. fn: async () => {
  1071. const config = await Config.get()
  1072. expect(config.mcp?.myserver).toEqual({
  1073. type: "remote",
  1074. url: "https://myserver.example.com/mcp",
  1075. enabled: true,
  1076. headers: {
  1077. "X-Custom-Header": "value",
  1078. },
  1079. })
  1080. },
  1081. })
  1082. })
  1083. test("local .opencode config can override MCP from project config", async () => {
  1084. await using tmp = await tmpdir({
  1085. init: async (dir) => {
  1086. // Project config with disabled MCP
  1087. await Bun.write(
  1088. path.join(dir, "opencode.json"),
  1089. JSON.stringify({
  1090. $schema: "https://opencode.ai/config.json",
  1091. mcp: {
  1092. docs: {
  1093. type: "remote",
  1094. url: "https://docs.example.com/mcp",
  1095. enabled: false,
  1096. },
  1097. },
  1098. }),
  1099. )
  1100. // Local .opencode directory config enables it
  1101. const opencodeDir = path.join(dir, ".opencode")
  1102. await fs.mkdir(opencodeDir, { recursive: true })
  1103. await Bun.write(
  1104. path.join(opencodeDir, "opencode.json"),
  1105. JSON.stringify({
  1106. $schema: "https://opencode.ai/config.json",
  1107. mcp: {
  1108. docs: {
  1109. type: "remote",
  1110. url: "https://docs.example.com/mcp",
  1111. enabled: true,
  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.mcp?.docs?.enabled).toBe(true)
  1123. },
  1124. })
  1125. })
  1126. test("project config overrides remote well-known config", async () => {
  1127. const originalFetch = globalThis.fetch
  1128. let fetchedUrl: string | undefined
  1129. const mockFetch = mock((url: string | URL | Request) => {
  1130. const urlStr = url.toString()
  1131. if (urlStr.includes(".well-known/opencode")) {
  1132. fetchedUrl = urlStr
  1133. return Promise.resolve(
  1134. new Response(
  1135. JSON.stringify({
  1136. config: {
  1137. mcp: {
  1138. jira: {
  1139. type: "remote",
  1140. url: "https://jira.example.com/mcp",
  1141. enabled: false,
  1142. },
  1143. },
  1144. },
  1145. }),
  1146. { status: 200 },
  1147. ),
  1148. )
  1149. }
  1150. return originalFetch(url)
  1151. })
  1152. globalThis.fetch = mockFetch as unknown as typeof fetch
  1153. const originalAuthAll = Auth.all
  1154. Auth.all = mock(() =>
  1155. Promise.resolve({
  1156. "https://example.com": {
  1157. type: "wellknown" as const,
  1158. key: "TEST_TOKEN",
  1159. token: "test-token",
  1160. },
  1161. }),
  1162. )
  1163. try {
  1164. await using tmp = await tmpdir({
  1165. git: true,
  1166. init: async (dir) => {
  1167. // Project config enables jira (overriding remote default)
  1168. await Bun.write(
  1169. path.join(dir, "opencode.json"),
  1170. JSON.stringify({
  1171. $schema: "https://opencode.ai/config.json",
  1172. mcp: {
  1173. jira: {
  1174. type: "remote",
  1175. url: "https://jira.example.com/mcp",
  1176. enabled: true,
  1177. },
  1178. },
  1179. }),
  1180. )
  1181. },
  1182. })
  1183. await Instance.provide({
  1184. directory: tmp.path,
  1185. fn: async () => {
  1186. const config = await Config.get()
  1187. // Verify fetch was called for wellknown config
  1188. expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
  1189. // Project config (enabled: true) should override remote (enabled: false)
  1190. expect(config.mcp?.jira?.enabled).toBe(true)
  1191. },
  1192. })
  1193. } finally {
  1194. globalThis.fetch = originalFetch
  1195. Auth.all = originalAuthAll
  1196. }
  1197. })
  1198. describe("getPluginName", () => {
  1199. test("extracts name from file:// URL", () => {
  1200. expect(Config.getPluginName("file:///path/to/plugin/foo.js")).toBe("foo")
  1201. expect(Config.getPluginName("file:///path/to/plugin/bar.ts")).toBe("bar")
  1202. expect(Config.getPluginName("file:///some/path/my-plugin.js")).toBe("my-plugin")
  1203. })
  1204. test("extracts name from npm package with version", () => {
  1205. expect(Config.getPluginName("[email protected]")).toBe("oh-my-opencode")
  1206. expect(Config.getPluginName("[email protected]")).toBe("some-plugin")
  1207. expect(Config.getPluginName("plugin@latest")).toBe("plugin")
  1208. })
  1209. test("extracts name from scoped npm package", () => {
  1210. expect(Config.getPluginName("@scope/[email protected]")).toBe("@scope/pkg")
  1211. expect(Config.getPluginName("@opencode/[email protected]")).toBe("@opencode/plugin")
  1212. })
  1213. test("returns full string for package without version", () => {
  1214. expect(Config.getPluginName("some-plugin")).toBe("some-plugin")
  1215. expect(Config.getPluginName("@scope/pkg")).toBe("@scope/pkg")
  1216. })
  1217. })
  1218. describe("deduplicatePlugins", () => {
  1219. test("removes duplicates keeping higher priority (later entries)", () => {
  1220. const plugins = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
  1221. const result = Config.deduplicatePlugins(plugins)
  1222. expect(result).toContain("[email protected]")
  1223. expect(result).toContain("[email protected]")
  1224. expect(result).toContain("[email protected]")
  1225. expect(result).not.toContain("[email protected]")
  1226. expect(result.length).toBe(3)
  1227. })
  1228. test("prefers local file over npm package with same name", () => {
  1229. const plugins = ["[email protected]", "file:///project/.opencode/plugin/oh-my-opencode.js"]
  1230. const result = Config.deduplicatePlugins(plugins)
  1231. expect(result.length).toBe(1)
  1232. expect(result[0]).toBe("file:///project/.opencode/plugin/oh-my-opencode.js")
  1233. })
  1234. test("preserves order of remaining plugins", () => {
  1235. const plugins = ["[email protected]", "[email protected]", "[email protected]"]
  1236. const result = Config.deduplicatePlugins(plugins)
  1237. expect(result).toEqual(["[email protected]", "[email protected]", "[email protected]"])
  1238. })
  1239. test("local plugin directory overrides global opencode.json plugin", async () => {
  1240. await using tmp = await tmpdir({
  1241. init: async (dir) => {
  1242. const projectDir = path.join(dir, "project")
  1243. const opencodeDir = path.join(projectDir, ".opencode")
  1244. const pluginDir = path.join(opencodeDir, "plugin")
  1245. await fs.mkdir(pluginDir, { recursive: true })
  1246. await Bun.write(
  1247. path.join(dir, "opencode.json"),
  1248. JSON.stringify({
  1249. $schema: "https://opencode.ai/config.json",
  1250. plugin: ["[email protected]"],
  1251. }),
  1252. )
  1253. await Bun.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
  1254. },
  1255. })
  1256. await Instance.provide({
  1257. directory: path.join(tmp.path, "project"),
  1258. fn: async () => {
  1259. const config = await Config.get()
  1260. const plugins = config.plugin ?? []
  1261. const myPlugins = plugins.filter((p) => Config.getPluginName(p) === "my-plugin")
  1262. expect(myPlugins.length).toBe(1)
  1263. expect(myPlugins[0].startsWith("file://")).toBe(true)
  1264. },
  1265. })
  1266. })
  1267. })