config.test.ts 47 KB

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