@@ -28,9 +28,16 @@ const encodeLegacySavedEnvironments = Schema.encodeEffect(
2828 Schema . Struct ( { version : Schema . Literal ( 1 ) , records : Schema . Array ( Schema . Unknown ) } ) ,
2929 ) ,
3030) ;
31- function layerSafeStorageFor ( available : boolean , failDecrypt : Ref . Ref < boolean > | null = null ) {
31+ type EncryptionAvailability =
32+ | boolean
33+ | Effect . Effect < boolean , ElectronSafeStorage . ElectronSafeStorageAvailabilityError > ;
34+
35+ function layerSafeStorageFor (
36+ available : EncryptionAvailability ,
37+ failDecrypt : Ref . Ref < boolean > | null = null ,
38+ ) {
3239 return Layer . succeed ( ElectronSafeStorage . ElectronSafeStorage , {
33- isEncryptionAvailable : Effect . succeed ( available ) ,
40+ isEncryptionAvailable : typeof available === "boolean" ? Effect . succeed ( available ) : available ,
3441 encryptString : ( value ) => Effect . succeed ( textEncoder . encode ( `encrypted:${ value } ` ) ) ,
3542 decryptString : ( value ) => {
3643 return Effect . gen ( function * ( ) {
@@ -52,9 +59,14 @@ function layerSafeStorageFor(available: boolean, failDecrypt: Ref.Ref<boolean> |
5259
5360function layerFor (
5461 baseDir : string ,
55- encryptionAvailable = true ,
62+ encryptionAvailable : EncryptionAvailability = true ,
5663 failDecrypt : Ref . Ref < boolean > | null = null ,
5764 fileSystemLayer : Layer . Layer < FileSystem . FileSystem > = NodeServices . layer ,
65+ runtime : {
66+ readonly dirname ?: string ;
67+ readonly platform ?: NodeJS . Platform ;
68+ readonly isPackaged ?: boolean ;
69+ } = { } ,
5870) {
5971 const layerEnvironment = DesktopEnvironment . layer ( {
6072 dirname : "/repo/apps/desktop/src" ,
@@ -66,6 +78,7 @@ function layerFor(
6678 isPackaged : true ,
6779 resourcesPath : "/missing/resources" ,
6880 runningUnderArm64Translation : false ,
81+ ...runtime ,
6982 } ) . pipe (
7083 Layer . provide (
7184 Layer . mergeAll ( NodeServices . layer , DesktopConfig . layerTest ( { T3CODE_HOME : baseDir } ) ) ,
@@ -90,7 +103,7 @@ function layerFor(
90103
91104const withStore = < A , E , R > (
92105 effect : Effect . Effect < A , E , R | DesktopConnectionCatalogStore . DesktopConnectionCatalogStore > ,
93- encryptionAvailable = true ,
106+ encryptionAvailable : EncryptionAvailability = true ,
94107) =>
95108 Effect . gen ( function * ( ) {
96109 const fileSystem = yield * FileSystem . FileSystem ;
@@ -101,6 +114,59 @@ const withStore = <A, E, R>(
101114 } ) . pipe ( Effect . provide ( NodeServices . layer ) , Effect . scoped ) ;
102115
103116describe ( "DesktopConnectionCatalogStore" , ( ) => {
117+ it . effect ( "isolates local macOS catalogs sharing a home and leaves legacy data untouched" , ( ) =>
118+ Effect . gen ( function * ( ) {
119+ const fileSystem = yield * FileSystem . FileSystem ;
120+ const path = yield * Path . Path ;
121+ const baseDir = yield * fileSystem . makeTempDirectoryScoped ( {
122+ prefix : "t3-local-keychain-catalog-test-" ,
123+ } ) ;
124+ const makeStore = ( dirname : string ) =>
125+ DesktopConnectionCatalogStore . DesktopConnectionCatalogStore . pipe (
126+ Effect . provide (
127+ layerFor ( baseDir , true , null , NodeServices . layer , { dirname, isPackaged : false } ) ,
128+ ) ,
129+ ) ;
130+ const sharedPath = path . join ( baseDir , "userdata" , "connection-catalog.json" ) ;
131+ const legacyPath = path . join ( baseDir , "userdata" , "saved-environments.json" ) ;
132+ yield * fileSystem . makeDirectory ( path . dirname ( sharedPath ) , { recursive : true } ) ;
133+ yield * fileSystem . writeFileString ( sharedPath , "unreadable shared ciphertext" ) ;
134+ yield * fileSystem . writeFileString ( legacyPath , "unreadable shared registry" ) ;
135+ const a = yield * makeStore ( "/repo/a/apps/desktop/dist-electron" ) ;
136+ const b = yield * makeStore ( "/repo/b/apps/desktop/dist-electron" ) ;
137+ assert . deepStrictEqual ( yield * a . get , Option . none ( ) ) ;
138+ assert . isTrue ( yield * a . set ( "catalog A" ) ) ;
139+ assert . deepStrictEqual ( yield * b . get , Option . none ( ) ) ;
140+ assert . isTrue ( yield * b . set ( "catalog B" ) ) ;
141+ assert . deepStrictEqual ( yield * a . get , Option . some ( "catalog A" ) ) ;
142+ assert . deepStrictEqual ( yield * b . get , Option . some ( "catalog B" ) ) ;
143+ yield * b . clear ;
144+ assert . deepStrictEqual ( yield * a . get , Option . some ( "catalog A" ) ) ;
145+ assert . deepStrictEqual ( yield * b . get , Option . none ( ) ) ;
146+ assert . equal ( yield * fileSystem . readFileString ( sharedPath ) , "unreadable shared ciphertext" ) ;
147+ assert . equal ( yield * fileSystem . readFileString ( legacyPath ) , "unreadable shared registry" ) ;
148+ } ) . pipe ( Effect . provide ( NodeServices . layer ) , Effect . scoped ) ,
149+ ) ;
150+
151+ it . effect ( "reads a fresh profile without accessing Keychain" , ( ) => {
152+ const cause = new ElectronSafeStorage . ElectronSafeStorageAvailabilityError ( {
153+ cause : new Error ( "Keychain requires consent" ) ,
154+ } ) ;
155+ return withStore (
156+ Effect . gen ( function * ( ) {
157+ const store = yield * DesktopConnectionCatalogStore . DesktopConnectionCatalogStore ;
158+ assert . deepStrictEqual ( yield * store . get , Option . none ( ) ) ;
159+ const error = yield * Effect . flip ( store . set ( "{}" ) ) ;
160+ assert . instanceOf (
161+ error ,
162+ DesktopConnectionCatalogStore . DesktopConnectionCatalogStoreProtectionError ,
163+ ) ;
164+ assert . strictEqual ( error . cause , cause ) ;
165+ } ) ,
166+ Effect . fail ( cause ) ,
167+ ) ;
168+ } ) ;
169+
104170 it . effect ( "persists, reads, and clears an encrypted connection catalog" , ( ) =>
105171 withStore (
106172 Effect . gen ( function * ( ) {
0 commit comments