@@ -29,6 +29,23 @@ test("managed service ports are stable per installation channel", () => {
2929 expect ( ServiceConfig . defaultPort ( "latest" , "darwin" ) ) . toBe ( 0xc0de )
3030 expect ( ServiceConfig . defaultPort ( "preview-a" , "linux" ) ) . toBe ( ServiceConfig . defaultPort ( "preview-a" , "linux" ) )
3131 expect ( ServiceConfig . defaultPort ( "preview-a" , "linux" ) ) . not . toBe ( ServiceConfig . defaultPort ( "preview-b" , "linux" ) )
32+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . toBe (
33+ ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ,
34+ )
35+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . not . toBe ( 0xc0de )
36+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . not . toBe (
37+ ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocB" ) ,
38+ )
39+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . toBe (
40+ ServiceConfig . defaultPort ( "beta" , "linux" , "/tmp/ocA" ) ,
41+ )
42+ expect ( ServiceConfig . defaultPort ( "latest" , "linux" , "/tmp/ocA" ) ) . not . toBe (
43+ ServiceConfig . defaultPort ( "dev" , "linux" , "/tmp/ocA" ) ,
44+ )
45+ const profilePorts = Array . from ( { length : 200 } , ( _ , index ) =>
46+ ServiceConfig . defaultPort ( "latest" , "linux" , `/tmp/oc-${ index } ` ) ,
47+ )
48+ expect ( profilePorts . every ( ( port ) => port >= 10_000 && port < 0x8000 ) ) . toBe ( true )
3249} )
3350
3451test ( "managed service ports on Windows stay below the ranges Windows and WSL reserve" , ( ) => {
@@ -269,12 +286,69 @@ test("service filenames share release channels and identify dev and preview chan
269286 expect ( ServiceConfig . filename ( "local" ) ) . toBe ( "service-local.json" )
270287 expect ( ServiceConfig . filename ( "preview-a" ) ) . toBe ( "service-preview-a.json" )
271288 expect ( ServiceConfig . filename ( "preview/a" ) ) . toBe ( "service-preview-a.json" )
289+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . toBe ( ServiceConfig . filename ( "beta" , "/tmp/ocA" ) )
290+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . not . toBe ( ServiceConfig . filename ( "dev" , "/tmp/ocA" ) )
291+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . not . toBe ( "service.json" )
292+ expect ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) ) . not . toBe ( ServiceConfig . filename ( "latest" , "/tmp/ocB" ) )
293+ expect ( ServiceConfig . filename ( "local" , "/tmp/ocA" ) ) . not . toBe ( ServiceConfig . filename ( "latest" , "/tmp/ocA" ) )
272294 expect ( ServiceConfig . versionBelongsToChannel ( "0.0.0-preview-a-1234" , "preview-a" ) ) . toBe ( true )
273295 expect ( ServiceConfig . versionBelongsToChannel ( "0.0.0-preview-a-1234.2" , "preview-a" ) ) . toBe ( true )
274296 expect ( ServiceConfig . versionBelongsToChannel ( "0.0.0-preview-a-other-1234" , "preview-a" ) ) . toBe ( false )
275297 expect ( ServiceConfig . versionBelongsToChannel ( "1.2.3" , "preview-a" ) ) . toBe ( false )
276298} )
277299
300+ test ( "custom OPENCODE_CONFIG_DIR profiles isolate registration and port in a shared state directory" , async ( ) => {
301+ const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-profiles-" ) )
302+ const state = path . join ( root , "state" )
303+ const configA = path . join ( root , "ocA" )
304+ const configB = path . join ( root , "ocB" )
305+ const previous = process . env . OPENCODE_CONFIG_DIR
306+ const run = ( configDir : string ) => {
307+ process . env . OPENCODE_CONFIG_DIR = configDir
308+ return Effect . runPromise (
309+ ServiceConfig . options ( ) . pipe (
310+ Effect . provide ( Global . layerWith ( { config : configDir , state } ) ) ,
311+ Effect . provide ( NodeFileSystem . layer ) ,
312+ ) ,
313+ )
314+ }
315+ try {
316+ const optionsA = await run ( configA )
317+ const optionsB = await run ( configB )
318+ expect ( path . dirname ( optionsA . file ) ) . toBe ( state )
319+ expect ( path . dirname ( optionsB . file ) ) . toBe ( state )
320+ expect ( optionsA . file ) . not . toBe ( path . join ( state , ServiceConfig . filename ( ) ) )
321+ expect ( optionsA . file ) . not . toBe ( optionsB . file )
322+ expect ( optionsA . env ?. OPENCODE_CONFIG_DIR ) . toBe ( path . resolve ( configA ) )
323+ expect ( optionsB . env ?. OPENCODE_CONFIG_DIR ) . toBe ( path . resolve ( configB ) )
324+
325+ // Upgrading with a single custom OPENCODE_CONFIG_DIR adopts an unhashed registration only when its password matches.
326+ const legacyRegistration = path . join ( state , ServiceConfig . filename ( ) )
327+ const record = {
328+ id : "existing-profile-a" ,
329+ version : OPENCODE_VERSION ,
330+ url : "http://127.0.0.1:49374" ,
331+ pid : 1234 ,
332+ password : "profile-a-secret" ,
333+ }
334+ await fs . writeFile ( path . join ( configA , ServiceConfig . filename ( ) ) , JSON . stringify ( { password : "profile-a-secret" } ) )
335+ await fs . writeFile ( path . join ( configB , ServiceConfig . filename ( ) ) , JSON . stringify ( { password : "profile-b-secret" } ) )
336+ await fs . writeFile ( legacyRegistration , JSON . stringify ( record ) )
337+
338+ await run ( configB )
339+ expect ( await Bun . file ( optionsB . file ) . exists ( ) ) . toBe ( false )
340+ expect ( await Bun . file ( legacyRegistration ) . exists ( ) ) . toBe ( true )
341+
342+ await run ( configA )
343+ expect ( await Bun . file ( optionsA . file ) . json ( ) ) . toEqual ( record )
344+ expect ( await Bun . file ( legacyRegistration ) . exists ( ) ) . toBe ( false )
345+ } finally {
346+ if ( previous === undefined ) delete process . env . OPENCODE_CONFIG_DIR
347+ else process . env . OPENCODE_CONFIG_DIR = previous
348+ await fs . rm ( root , { recursive : true , force : true } )
349+ }
350+ } )
351+
278352test ( "service config migrates from the hashed channel filename" , async ( ) => {
279353 const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-config-migration-" ) )
280354 const legacy = path . join ( root , ServiceConfig . legacyFilename ( "preview-a" ) ! )
@@ -497,8 +571,9 @@ test.each([
497571 "managed service applies CORS configuration with flag overrides: $args" ,
498572 async ( { args, origins } ) => {
499573 const root = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-service-cors-" ) )
500- const config = path . join ( root , "config" , ServiceConfig . filename ( ) )
501- const registration = path . join ( root , "state" , "opencode" , ServiceConfig . filename ( ) )
574+ const configDir = path . join ( root , "config" )
575+ const config = path . join ( configDir , ServiceConfig . filename ( ) )
576+ const registration = path . join ( root , "state" , "opencode" , ServiceConfig . filename ( undefined , configDir ) )
502577 const cors = [ "http://192.0.2.10:3001" , "https://configured.example.com" ]
503578 await fs . mkdir ( path . dirname ( config ) , { recursive : true } )
504579 await fs . writeFile ( config , JSON . stringify ( { cors } ) )
@@ -544,10 +619,11 @@ test("the original managed service contender binds when the occupied port is rel
544619 } )
545620 const port = listener . port
546621 if ( port === undefined ) throw new Error ( "Server did not bind a port" )
547- const registration = path . join ( root , "state" , "opencode" , "service-local.json" )
548- await fs . mkdir ( path . join ( root , "config" ) , { recursive : true } )
622+ const configDir = path . join ( root , "config" )
623+ const registration = path . join ( root , "state" , "opencode" , ServiceConfig . filename ( "local" , configDir ) )
624+ await fs . mkdir ( configDir , { recursive : true } )
549625 await fs . mkdir ( path . dirname ( registration ) , { recursive : true } )
550- await fs . writeFile ( path . join ( root , "config" , "service-local.json" ) , JSON . stringify ( { port } ) )
626+ await fs . writeFile ( path . join ( configDir , "service-local.json" ) , JSON . stringify ( { port } ) )
551627 await fs . writeFile (
552628 registration ,
553629 JSON . stringify ( {
0 commit comments