@@ -2288,3 +2288,100 @@ test("parseManagedPlist handles empty config", async () => {
22882288 )
22892289 expect ( config . $schema ) . toBe ( "https://opencode.ai/config.json" )
22902290} )
2291+
2292+ const loadGlobalWithOverrides = ( ) =>
2293+ Effect . runPromise (
2294+ Config . Service . use ( ( svc ) => svc . getGlobalWithOverrides ( ) ) . pipe ( Effect . scoped , Effect . provide ( layer ) ) ,
2295+ )
2296+
2297+ describe ( "getGlobalWithOverrides" , ( ) => {
2298+ const origConfig = process . env [ "OPENCODE_CONFIG" ]
2299+ const origContent = process . env [ "OPENCODE_CONFIG_CONTENT" ]
2300+
2301+ afterEach ( async ( ) => {
2302+ if ( origConfig === undefined ) delete process . env [ "OPENCODE_CONFIG" ]
2303+ else process . env [ "OPENCODE_CONFIG" ] = origConfig
2304+ if ( origContent === undefined ) delete process . env [ "OPENCODE_CONFIG_CONTENT" ]
2305+ else process . env [ "OPENCODE_CONFIG_CONTENT" ] = origContent
2306+ await clear ( true )
2307+ } )
2308+
2309+ test ( "returns global config when no env vars are set" , async ( ) => {
2310+ await using globalTmp = await tmpdir ( )
2311+ const prevConfigPath = Global . Path . config
2312+ ; ( Global . Path as { config : string } ) . config = globalTmp . path
2313+ delete process . env [ "OPENCODE_CONFIG" ]
2314+ delete process . env [ "OPENCODE_CONFIG_CONTENT" ]
2315+ try {
2316+ await writeConfig ( globalTmp . path , {
2317+ $schema : "https://opencode.ai/config.json" ,
2318+ server : { port : 4096 , hostname : "127.0.0.1" } ,
2319+ } )
2320+ await clear ( true )
2321+ const config = await loadGlobalWithOverrides ( )
2322+ expect ( config . server ?. port ) . toBe ( 4096 )
2323+ expect ( config . server ?. hostname ) . toBe ( "127.0.0.1" )
2324+ } finally {
2325+ ; ( Global . Path as { config : string } ) . config = prevConfigPath
2326+ await clear ( true )
2327+ }
2328+ } )
2329+
2330+ test ( "OPENCODE_CONFIG merges on top of global config" , async ( ) => {
2331+ await using globalTmp = await tmpdir ( )
2332+ await using overrideTmp = await tmpdir ( )
2333+ const prevConfigPath = Global . Path . config
2334+ ; ( Global . Path as { config : string } ) . config = globalTmp . path
2335+ try {
2336+ await writeConfig ( globalTmp . path , {
2337+ $schema : "https://opencode.ai/config.json" ,
2338+ server : { port : 4096 , hostname : "127.0.0.1" } ,
2339+ } )
2340+ await writeConfig ( overrideTmp . path , {
2341+ $schema : "https://opencode.ai/config.json" ,
2342+ server : { port : 9090 } ,
2343+ } )
2344+ process . env [ "OPENCODE_CONFIG" ] = path . join ( overrideTmp . path , "opencode.json" )
2345+ await clear ( true )
2346+ const config = await loadGlobalWithOverrides ( )
2347+ // OPENCODE_CONFIG port wins
2348+ expect ( config . server ?. port ) . toBe ( 9090 )
2349+ // global hostname is preserved since OPENCODE_CONFIG doesn't define it
2350+ expect ( config . server ?. hostname ) . toBe ( "127.0.0.1" )
2351+ } finally {
2352+ ; ( Global . Path as { config : string } ) . config = prevConfigPath
2353+ await clear ( true )
2354+ }
2355+ } )
2356+
2357+ test ( "OPENCODE_CONFIG_CONTENT merges on top of global and OPENCODE_CONFIG" , async ( ) => {
2358+ await using globalTmp = await tmpdir ( )
2359+ await using overrideTmp = await tmpdir ( )
2360+ const prevConfigPath = Global . Path . config
2361+ ; ( Global . Path as { config : string } ) . config = globalTmp . path
2362+ try {
2363+ await writeConfig ( globalTmp . path , {
2364+ $schema : "https://opencode.ai/config.json" ,
2365+ server : { port : 4096 , hostname : "127.0.0.1" } ,
2366+ } )
2367+ await writeConfig ( overrideTmp . path , {
2368+ $schema : "https://opencode.ai/config.json" ,
2369+ server : { port : 9090 , hostname : "0.0.0.0" } ,
2370+ } )
2371+ process . env [ "OPENCODE_CONFIG" ] = path . join ( overrideTmp . path , "opencode.json" )
2372+ process . env [ "OPENCODE_CONFIG_CONTENT" ] = JSON . stringify ( {
2373+ $schema : "https://opencode.ai/config.json" ,
2374+ server : { port : 7777 } ,
2375+ } )
2376+ await clear ( true )
2377+ const config = await loadGlobalWithOverrides ( )
2378+ // OPENCODE_CONFIG_CONTENT port wins over both
2379+ expect ( config . server ?. port ) . toBe ( 7777 )
2380+ // OPENCODE_CONFIG hostname is preserved since OPENCODE_CONFIG_CONTENT doesn't define it
2381+ expect ( config . server ?. hostname ) . toBe ( "0.0.0.0" )
2382+ } finally {
2383+ ; ( Global . Path as { config : string } ) . config = prevConfigPath
2384+ await clear ( true )
2385+ }
2386+ } )
2387+ } )
0 commit comments