The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { assert, describe, it } from "@effect/vitest"; |
| 4 | import { ConnectionCatalogDocument } from "@t3tools/client-runtime/platform"; |
| 5 | import { EnvironmentId, type PersistedSavedEnvironmentRecord } from "@t3tools/contracts"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Base64 from "effect/encoding/Base64"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as Ref from "effect/Ref"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | |
| 15 | import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts"; |
| 16 | import * as DesktopSavedEnvironments from "../settings/DesktopSavedEnvironments.ts"; |
| 17 | import * as DesktopConfig from "./DesktopConfig.ts"; |
| 18 | import * as DesktopConnectionCatalogStore from "./DesktopConnectionCatalogStore.ts"; |
| 19 | import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 20 | |
| 21 | const textDecoder = new TextDecoder(); |
| 22 | const textEncoder = new TextEncoder(); |
| 23 | const decodeConnectionCatalog = Schema.decodeEffect( |
| 24 | Schema.fromJsonString(ConnectionCatalogDocument), |
| 25 | ); |
| 26 | const encodeLegacySavedEnvironments = Schema.encodeEffect( |
| 27 | Schema.fromJsonString( |
| 28 | Schema.Struct({ version: Schema.Literal(1), records: Schema.Array(Schema.Unknown) }), |
| 29 | ), |
| 30 | ); |
| 31 | function layerSafeStorageFor(available: boolean, failDecrypt: Ref.Ref<boolean> | null = null) { |
| 32 | return Layer.succeed(ElectronSafeStorage.ElectronSafeStorage, { |
| 33 | isEncryptionAvailable: Effect.succeed(available), |
| 34 | encryptString: (value) => Effect.succeed(textEncoder.encode(`encrypted:${value}`)), |
| 35 | decryptString: (value) => { |
| 36 | return Effect.gen(function* () { |
| 37 | const decoded = textDecoder.decode(value); |
| 38 | if ( |
| 39 | !decoded.startsWith("encrypted:") || |
| 40 | (failDecrypt !== null && (yield* Ref.get(failDecrypt))) |
| 41 | ) { |
| 42 | return yield* new ElectronSafeStorage.ElectronSafeStorageDecryptError({ |
| 43 | cause: new Error("invalid encrypted catalog"), |
| 44 | }); |
| 45 | } |
| 46 | return decoded.slice("encrypted:".length); |
| 47 | }); |
| 48 | }, |
| 49 | selectedStorageBackend: Effect.succeedNone, |
| 50 | } satisfies ElectronSafeStorage.ElectronSafeStorage["Service"]); |
| 51 | } |
| 52 | |
| 53 | function layerFor( |
| 54 | baseDir: string, |
| 55 | encryptionAvailable = true, |
| 56 | failDecrypt: Ref.Ref<boolean> | null = null, |
| 57 | fileSystemLayer: Layer.Layer<FileSystem.FileSystem> = NodeServices.layer, |
| 58 | ) { |
| 59 | const layerEnvironment = DesktopEnvironment.layer({ |
| 60 | dirname: "/repo/apps/desktop/src", |
| 61 | homeDirectory: baseDir, |
| 62 | platform: "darwin", |
| 63 | processArch: "arm64", |
| 64 | appVersion: "1.2.3", |
| 65 | appPath: "/repo", |
| 66 | isPackaged: true, |
| 67 | resourcesPath: "/missing/resources", |
| 68 | runningUnderArm64Translation: false, |
| 69 | }).pipe( |
| 70 | Layer.provide( |
| 71 | Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })), |
| 72 | ), |
| 73 | ); |
| 74 | const layerSafeStorage = layerSafeStorageFor(encryptionAvailable, failDecrypt); |
| 75 | const layerDependencies = Layer.mergeAll( |
| 76 | layerEnvironment, |
| 77 | layerSafeStorage, |
| 78 | NodeServices.layer, |
| 79 | NodeCrypto.layer, |
| 80 | fileSystemLayer, |
| 81 | ); |
| 82 | const layerSavedEnvironments = DesktopSavedEnvironments.layer.pipe( |
| 83 | Layer.provideMerge(layerDependencies), |
| 84 | ); |
| 85 | |
| 86 | return DesktopConnectionCatalogStore.layer.pipe( |
| 87 | Layer.provideMerge(layerSavedEnvironments), |
| 88 | Layer.provideMerge(layerDependencies), |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | const withStore = <A, E, R>( |
| 93 | effect: Effect.Effect<A, E, R | DesktopConnectionCatalogStore.DesktopConnectionCatalogStore>, |
| 94 | encryptionAvailable = true, |
| 95 | ) => |
| 96 | Effect.gen(function* () { |
| 97 | const fileSystem = yield* FileSystem.FileSystem; |
| 98 | const baseDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 99 | prefix: "t3-desktop-connection-catalog-test-", |
| 100 | }); |
| 101 | return yield* effect.pipe(Effect.provide(layerFor(baseDir, encryptionAvailable))); |
| 102 | }).pipe(Effect.provide(NodeServices.layer)); |
| 103 | |
| 104 | describe("DesktopConnectionCatalogStore", () => { |
| 105 | it.effect("persists and reads an encrypted connection catalog", () => |
| 106 | withStore( |
| 107 | Effect.gen(function* () { |
| 108 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore; |
| 109 | const catalog = '{"schemaVersion":1,"targets":[]}'; |
| 110 | |
| 111 | assert.isTrue(yield* store.set(catalog)); |
| 112 | assert.deepStrictEqual(yield* store.get, Option.some(catalog)); |
| 113 | }), |
| 114 | ), |
| 115 | ); |
| 116 | |
| 117 | it.effect("does not persist when secure storage is unavailable", () => |
| 118 | withStore( |
| 119 | Effect.gen(function* () { |
| 120 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore; |
| 121 | assert.isFalse(yield* store.set("{}")); |
| 122 | assert.deepStrictEqual(yield* store.get, Option.none()); |
| 123 | }), |
| 124 | false, |
| 125 | ), |
| 126 | ); |
| 127 | |
| 128 | it.effect("migrates legacy relay, SSH, bearer profile, and credential data", () => |
| 129 | withStore( |
| 130 | Effect.gen(function* () { |
| 131 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore; |
| 132 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 133 | const fileSystem = yield* FileSystem.FileSystem; |
| 134 | const records: readonly PersistedSavedEnvironmentRecord[] = [ |
| 135 | { |
| 136 | environmentId: EnvironmentId.make("relay-environment"), |
| 137 | label: "Relay", |
| 138 | httpBaseUrl: "https://relay.example.com/", |
| 139 | wsBaseUrl: "wss://relay.example.com/", |
| 140 | createdAt: "2026-06-01T00:00:00.000Z", |
| 141 | lastConnectedAt: null, |
| 142 | relayManaged: { relayUrl: "https://relay-control.example.com/" }, |
| 143 | }, |
| 144 | { |
| 145 | environmentId: EnvironmentId.make("ssh-environment"), |
| 146 | label: "SSH", |
| 147 | httpBaseUrl: "http://127.0.0.1:41773/", |
| 148 | wsBaseUrl: "ws://127.0.0.1:41773/", |
| 149 | createdAt: "2026-06-02T00:00:00.000Z", |
| 150 | lastConnectedAt: null, |
| 151 | desktopSsh: { |
| 152 | alias: "devbox", |
| 153 | hostname: "devbox.example.com", |
| 154 | username: "julius", |
| 155 | port: 22, |
| 156 | }, |
| 157 | }, |
| 158 | { |
| 159 | environmentId: EnvironmentId.make("bearer-environment"), |
| 160 | label: "Bearer", |
| 161 | httpBaseUrl: "https://bearer.example.com/", |
| 162 | wsBaseUrl: "wss://bearer.example.com/", |
| 163 | createdAt: "2026-06-03T00:00:00.000Z", |
| 164 | lastConnectedAt: null, |
| 165 | }, |
| 166 | ]; |
| 167 | yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); |
| 168 | yield* fileSystem.writeFileString( |
| 169 | environment.savedEnvironmentRegistryPath, |
| 170 | yield* encodeLegacySavedEnvironments({ |
| 171 | version: 1, |
| 172 | records: records.map((record) => |
| 173 | record.environmentId === "bearer-environment" |
| 174 | ? { |
| 175 | ...record, |
| 176 | encryptedBearerToken: Base64.encode( |
| 177 | textEncoder.encode("encrypted:legacy-token"), |
| 178 | ), |
| 179 | } |
| 180 | : record, |
| 181 | ), |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | const migrated = yield* store.get; |
| 186 | assert.isTrue(Option.isSome(migrated)); |
| 187 | if (Option.isNone(migrated)) { |
| 188 | return; |
| 189 | } |
| 190 | const catalog = yield* decodeConnectionCatalog(migrated.value); |
| 191 | |
| 192 | assert.deepInclude(catalog.targets[0], { |
| 193 | _tag: "RelayConnectionTarget", |
| 194 | environmentId: EnvironmentId.make("relay-environment"), |
| 195 | label: "Relay", |
| 196 | }); |
| 197 | assert.deepInclude(catalog.targets[1], { |
| 198 | _tag: "SshConnectionTarget", |
| 199 | environmentId: EnvironmentId.make("ssh-environment"), |
| 200 | label: "SSH", |
| 201 | connectionId: "ssh:ssh-environment", |
| 202 | }); |
| 203 | assert.deepInclude(catalog.targets[2], { |
| 204 | _tag: "BearerConnectionTarget", |
| 205 | environmentId: EnvironmentId.make("bearer-environment"), |
| 206 | label: "Bearer", |
| 207 | connectionId: "bearer:bearer-environment", |
| 208 | }); |
| 209 | assert.deepInclude(catalog.profiles[0], { |
| 210 | _tag: "SshConnectionProfile", |
| 211 | connectionId: "ssh:ssh-environment", |
| 212 | environmentId: EnvironmentId.make("ssh-environment"), |
| 213 | label: "SSH", |
| 214 | target: { |
| 215 | alias: "devbox", |
| 216 | hostname: "devbox.example.com", |
| 217 | username: "julius", |
| 218 | port: 22, |
| 219 | }, |
| 220 | }); |
| 221 | assert.deepInclude(catalog.profiles[1], { |
| 222 | _tag: "BearerConnectionProfile", |
| 223 | connectionId: "bearer:bearer-environment", |
| 224 | environmentId: EnvironmentId.make("bearer-environment"), |
| 225 | label: "Bearer", |
| 226 | httpBaseUrl: "https://bearer.example.com/", |
| 227 | wsBaseUrl: "wss://bearer.example.com/", |
| 228 | }); |
| 229 | assert.equal(catalog.credentials.length, 1); |
| 230 | assert.equal(catalog.credentials[0]?.connectionId, "bearer:bearer-environment"); |
| 231 | assert.equal(catalog.credentials[0]?.credential._tag, "BearerConnectionCredential"); |
| 232 | if (catalog.credentials[0]?.credential._tag === "BearerConnectionCredential") { |
| 233 | assert.equal(catalog.credentials[0].credential.token, "legacy-token"); |
| 234 | } |
| 235 | |
| 236 | yield* fileSystem.writeFileString( |
| 237 | environment.savedEnvironmentRegistryPath, |
| 238 | '{"version":1,"records":[]}', |
| 239 | ); |
| 240 | assert.deepEqual(yield* store.get, migrated); |
| 241 | }), |
| 242 | ), |
| 243 | ); |
| 244 | |
| 245 | it.effect("surfaces malformed catalog documents without deleting them", () => |
| 246 | withStore( |
| 247 | Effect.gen(function* () { |
| 248 | const path = yield* Path.Path; |
| 249 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 250 | const fileSystem = yield* FileSystem.FileSystem; |
| 251 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore; |
| 252 | const catalogPath = path.join(environment.stateDir, "connection-catalog.json"); |
| 253 | yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); |
| 254 | yield* fileSystem.writeFileString(catalogPath, "{not-json"); |
| 255 | |
| 256 | const error = yield* store.get.pipe(Effect.flip); |
| 257 | assert.instanceOf( |
| 258 | error, |
| 259 | DesktopConnectionCatalogStore.DesktopConnectionCatalogStoreDocumentDecodeError, |
| 260 | ); |
| 261 | assert.equal(error.catalogPath, catalogPath); |
| 262 | assert.exists(error.cause); |
| 263 | assert.equal(yield* fileSystem.readFileString(catalogPath), "{not-json"); |
| 264 | }), |
| 265 | ), |
| 266 | ); |
| 267 | |
| 268 | it.effect("surfaces a catalog that can no longer be decrypted without deleting it", () => |
| 269 | Effect.gen(function* () { |
| 270 | const path = yield* Path.Path; |
| 271 | const fileSystem = yield* FileSystem.FileSystem; |
| 272 | const baseDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 273 | prefix: "t3-desktop-connection-catalog-test-", |
| 274 | }); |
| 275 | const failDecrypt = yield* Ref.make(false); |
| 276 | const layer = layerFor(baseDir, true, failDecrypt); |
| 277 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore.pipe( |
| 278 | Effect.provide(layer), |
| 279 | ); |
| 280 | |
| 281 | assert.isTrue(yield* store.set('{"schemaVersion":1,"targets":[]}')); |
| 282 | yield* Ref.set(failDecrypt, true); |
| 283 | const error = yield* store.get.pipe(Effect.flip); |
| 284 | assert.instanceOf( |
| 285 | error, |
| 286 | DesktopConnectionCatalogStore.DesktopConnectionCatalogStoreProtectionError, |
| 287 | ); |
| 288 | assert.equal(error.operation, "decrypt-catalog"); |
| 289 | assert.equal(error.catalogPath, path.join(baseDir, "userdata", "connection-catalog.json")); |
| 290 | assert.instanceOf(error.cause, ElectronSafeStorage.ElectronSafeStorageDecryptError); |
| 291 | const decryptError = error.cause as ElectronSafeStorage.ElectronSafeStorageDecryptError; |
| 292 | assert.instanceOf(decryptError.cause, Error); |
| 293 | assert.equal(decryptError.cause.message, "invalid encrypted catalog"); |
| 294 | assert.equal( |
| 295 | error.message, |
| 296 | `Desktop connection catalog protection failed during decrypt-catalog at ${path.join(baseDir, "userdata", "connection-catalog.json")}.`, |
| 297 | ); |
| 298 | assert.notEqual(error.message, decryptError.message); |
| 299 | yield* Ref.set(failDecrypt, false); |
| 300 | assert.deepStrictEqual(yield* store.get, Option.some('{"schemaVersion":1,"targets":[]}')); |
| 301 | }).pipe(Effect.provide(NodeServices.layer)), |
| 302 | ); |
| 303 | |
| 304 | const emptyCatalog = |
| 305 | '{"schemaVersion":1,"targets":[],"profiles":[],"credentials":[],"remoteDpopTokens":[],"disabledEnvironmentIds":[]}'; |
| 306 | const savedFilesHolding = Effect.fn(function* (stateDir: string, contents: string) { |
| 307 | const fileSystem = yield* FileSystem.FileSystem; |
| 308 | const path = yield* Path.Path; |
| 309 | const kept: string[] = []; |
| 310 | for (const name of yield* fileSystem.readDirectory(stateDir)) { |
| 311 | const file = path.join(stateDir, name); |
| 312 | if ((yield* fileSystem.stat(file)).type !== "File") continue; |
| 313 | if ((yield* fileSystem.readFileString(file)) === contents) kept.push(name); |
| 314 | } |
| 315 | return kept; |
| 316 | }); |
| 317 | |
| 318 | it.effect("keeps a catalog this build cannot decode when the next save replaces it", () => |
| 319 | withStore( |
| 320 | Effect.gen(function* () { |
| 321 | const path = yield* Path.Path; |
| 322 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 323 | const fileSystem = yield* FileSystem.FileSystem; |
| 324 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore; |
| 325 | const catalogPath = path.join(environment.stateDir, "connection-catalog.json"); |
| 326 | assert.isTrue( |
| 327 | yield* store.set( |
| 328 | '{"schemaVersion":1,"targets":[{"_tag":"FutureConnectionTarget","environmentId":"remote"}],"profiles":[],"credentials":[],"remoteDpopTokens":[],"disabledEnvironmentIds":[]}', |
| 329 | ), |
| 330 | ); |
| 331 | const original = yield* fileSystem.readFileString(catalogPath); |
| 332 | |
| 333 | yield* store.get; |
| 334 | assert.isTrue(yield* store.set(emptyCatalog)); |
| 335 | |
| 336 | assert.isNotEmpty(yield* savedFilesHolding(environment.stateDir, original)); |
| 337 | }), |
| 338 | ), |
| 339 | ); |
| 340 | |
| 341 | it.effect("keeps a catalog that no longer decrypts when the next save replaces it", () => |
| 342 | Effect.gen(function* () { |
| 343 | const path = yield* Path.Path; |
| 344 | const fileSystem = yield* FileSystem.FileSystem; |
| 345 | const baseDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 346 | prefix: "t3-desktop-connection-catalog-test-", |
| 347 | }); |
| 348 | const failDecrypt = yield* Ref.make(false); |
| 349 | const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore.pipe( |
| 350 | Effect.provide(layerFor(baseDir, true, failDecrypt)), |
| 351 | ); |
| 352 | const stateDir = path.join(baseDir, "userdata"); |
| 353 | assert.isTrue(yield* store.set('{"schemaVersion":1,"targets":[]}')); |
| 354 | const original = yield* fileSystem.readFileString( |
| 355 | path.join(stateDir, "connection-catalog.json"), |
| 356 | ); |
| 357 | |
| 358 | yield* Ref.set(failDecrypt, true); |
| 359 | yield* store.get.pipe(Effect.flip); |
| 360 | assert.isTrue(yield* store.set(emptyCatalog)); |
| 361 | |
| 362 | assert.isNotEmpty(yield* savedFilesHolding(stateDir, original)); |
| 363 | }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), |
| 364 | ); |
| 365 | }); |