@@ -26,6 +26,13 @@ test("managed service ports are stable per installation channel", () => {
2626 expect ( ServiceConfig . defaultPort ( "latest" , "darwin" ) ) . toBe ( 0xc0de )
2727 expect ( ServiceConfig . defaultPort ( "preview-a" , "linux" ) ) . toBe ( ServiceConfig . defaultPort ( "preview-a" , "linux" ) )
2828 expect ( ServiceConfig . defaultPort ( "preview-a" , "linux" ) ) . not . toBe ( ServiceConfig . defaultPort ( "preview-b" , "linux" ) )
29+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . toBe (
30+ ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ,
31+ )
32+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . not . toBe ( 0xc0de )
33+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . not . toBe (
34+ ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocB" ) ,
35+ )
2936} )
3037
3138test ( "managed service ports on Windows stay below the ranges Windows and WSL reserve" , ( ) => {
@@ -200,12 +207,86 @@ test("service filenames share release channels and identify preview channels", (
200207 expect ( ServiceConfig . filename ( "local" ) ) . toBe ( "service-local.json" )
201208 expect ( ServiceConfig . filename ( "preview-a" ) ) . toBe ( "service-preview-a.json" )
202209 expect ( ServiceConfig . filename ( "preview/a" ) ) . toBe ( "service-preview-a.json" )
210+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . toBe ( ServiceConfig . filename ( "dev" , "/tmp/ocA" ) )
211+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . not . toBe ( "service.json" )
212+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . not . toBe ( ServiceConfig . filename ( "latest" , "/tmp/ocB" ) )
213+ expect ( ServiceConfig . filename ( "local" , "/tmp/ocA" ) ) . not . toBe ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) )
203214 expect ( ServiceConfig . versionBelongsToChannel ( "0.0.0-preview-a-1234" , "preview-a" ) ) . toBe ( true )
204215 expect ( ServiceConfig . versionBelongsToChannel ( "0.0.0-preview-a-1234.2" , "preview-a" ) ) . toBe ( true )
205216 expect ( ServiceConfig . versionBelongsToChannel ( "0.0.0-preview-a-other-1234" , "preview-a" ) ) . toBe ( false )
206217 expect ( ServiceConfig . versionBelongsToChannel ( "1.2.3" , "preview-a" ) ) . toBe ( false )
207218} )
208219
220+ test ( "custom OPENCODE_CONFIG_DIR profiles isolate registration and port in a shared state directory" , async ( ) => {
221+ const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-profiles-" ) )
222+ const state = path . join ( root , "state" )
223+ const configA = path . join ( root , "ocA" )
224+ const configB = path . join ( root , "ocB" )
225+ const previous = process . env . OPENCODE_CONFIG_DIR
226+ const run = ( configDir : string ) => {
227+ process . env . OPENCODE_CONFIG_DIR = configDir
228+ return Effect . runPromise (
229+ ServiceConfig . options ( ) . pipe (
230+ Effect . provide ( Global . layerWith ( { config : configDir , state } ) ) ,
231+ Effect . provide ( NodeFileSystem . layer ) ,
232+ ) ,
233+ )
234+ }
235+ try {
236+ const optionsA = await run ( configA )
237+ const optionsB = await run ( configB )
238+ expect ( path . dirname ( optionsA . file ) ) . toBe ( state )
239+ expect ( path . dirname ( optionsB . file ) ) . toBe ( state )
240+ expect ( optionsA . file ) . not . toBe ( path . join ( state , ServiceConfig . filename ( ) ) )
241+ expect ( optionsA . file ) . not . toBe ( optionsB . file )
242+ expect ( optionsA . env ?. OPENCODE_CONFIG_DIR ) . toBe ( path . resolve ( configA ) )
243+ expect ( optionsB . env ?. OPENCODE_CONFIG_DIR ) . toBe ( path . resolve ( configB ) )
244+ } finally {
245+ if ( previous === undefined ) delete process . env . OPENCODE_CONFIG_DIR
246+ else process . env . OPENCODE_CONFIG_DIR = previous
247+ await fs . rm ( root , { recursive : true , force : true } )
248+ }
249+ } )
250+
251+ test ( "distinct OPENCODE_CONFIG_DIR profiles start, report status, and stop independent services" , async ( ) => {
252+ const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-profile-processes-" ) )
253+ const entry = path . join ( import . meta. dir , "../src/index.ts" )
254+ const configA = path . join ( root , "ocA" )
255+ const configB = path . join ( root , "ocB" )
256+ const envA = { ...serviceEnv ( root ) , OPENCODE_CONFIG_DIR : configA }
257+ const envB = { ...serviceEnv ( root ) , OPENCODE_CONFIG_DIR : configB }
258+ const cli = async ( env : Record < string , string | undefined > , ...args : string [ ] ) => {
259+ const child = Bun . spawn ( [ process . execPath , entry , ...args ] , { env, stdout : "pipe" , stderr : "pipe" } )
260+ const [ code , stdout , stderr ] = await Promise . all ( [
261+ child . exited ,
262+ new Response ( child . stdout ) . text ( ) ,
263+ new Response ( child . stderr ) . text ( ) ,
264+ ] )
265+ if ( code !== 0 ) throw new Error ( `opencode ${ args . join ( " " ) } exited ${ code } : ${ stderr . trim ( ) } ` )
266+ return stdout . trim ( )
267+ }
268+ try {
269+ const urlA = await cli ( envA , "service" , "start" )
270+ const urlB = await cli ( envB , "service" , "start" )
271+ expect ( urlA ) . not . toBe ( urlB )
272+ expect ( new URL ( urlA ) . port ) . toBe ( String ( ServiceConfig . defaultPort ( "local" , process . platform , configA ) ) )
273+ expect ( new URL ( urlB ) . port ) . toBe ( String ( ServiceConfig . defaultPort ( "local" , process . platform , configB ) ) )
274+ expect ( await cli ( envA , "service" , "status" ) ) . toBe ( urlA )
275+ expect ( await cli ( envB , "service" , "status" ) ) . toBe ( urlB )
276+
277+ await cli ( envA , "service" , "stop" )
278+ expect ( await cli ( envA , "service" , "status" ) ) . toBe ( "stopped" )
279+ expect ( await cli ( envB , "service" , "status" ) ) . toBe ( urlB )
280+
281+ await cli ( envB , "service" , "stop" )
282+ expect ( await cli ( envB , "service" , "status" ) ) . toBe ( "stopped" )
283+ } finally {
284+ await cli ( envA , "service" , "stop" ) . catch ( ( ) => undefined )
285+ await cli ( envB , "service" , "stop" ) . catch ( ( ) => undefined )
286+ await fs . rm ( root , { recursive : true , force : true } )
287+ }
288+ } , 60_000 )
289+
209290test ( "service config migrates from the hashed channel filename" , async ( ) => {
210291 const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-config-migration-" ) )
211292 const legacy = path . join ( root , ServiceConfig . legacyFilename ( "preview-a" ) ! )
@@ -427,8 +508,9 @@ test.each([
427508 "managed service applies CORS configuration with flag overrides: $args" ,
428509 async ( { args, origins } ) => {
429510 const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-cors-" ) )
430- const config = path . join ( root , "config" , ServiceConfig . filename ( ) )
431- const registration = path . join ( root , "state" , "opencode" , ServiceConfig . filename ( ) )
511+ const configDir = path . join ( root , "config" )
512+ const config = path . join ( configDir , ServiceConfig . filename ( ) )
513+ const registration = path . join ( root , "state" , "opencode" , ServiceConfig . filename ( undefined , configDir ) )
432514 const cors = [ "http://192.0.2.10:3001" , "https://configured.example.com" ]
433515 await fs . mkdir ( path . dirname ( config ) , { recursive : true } )
434516 await fs . writeFile ( config , JSON . stringify ( { cors } ) )
@@ -474,10 +556,11 @@ test("the original managed service contender binds when the occupied port is rel
474556 } )
475557 const port = listener . port
476558 if ( port === undefined ) throw new Error ( "Server did not bind a port" )
477- const registration = path . join ( root , "state" , "opencode" , "service-local.json" )
478- await fs . mkdir ( path . join ( root , "config" ) , { recursive : true } )
559+ const configDir = path . join ( root , "config" )
560+ const registration = path . join ( root , "state" , "opencode" , ServiceConfig . filename ( "local" , configDir ) )
561+ await fs . mkdir ( configDir , { recursive : true } )
479562 await fs . mkdir ( path . dirname ( registration ) , { recursive : true } )
480- await fs . writeFile ( path . join ( root , "config" , "service-local.json" ) , JSON . stringify ( { port } ) )
563+ await fs . writeFile ( path . join ( configDir , "service-local.json" ) , JSON . stringify ( { port } ) )
481564 await fs . writeFile (
482565 registration ,
483566 JSON . stringify ( {
0 commit comments