The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | |
| 8 | import { CodexSettings } from "@t3tools/contracts"; |
| 9 | import { |
| 10 | CodexShadowHomeEntryConflictError, |
| 11 | CodexShadowHomePathConflictError, |
| 12 | materializeCodexShadowHome, |
| 13 | resolveCodexHomeLayout, |
| 14 | } from "./CodexHomeLayout.ts"; |
| 15 | import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; |
| 16 | const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings); |
| 17 | |
| 18 | const decodeCodexSettings = (input: { |
| 19 | readonly enabled?: boolean; |
| 20 | readonly homePath?: string; |
| 21 | readonly shadowHomePath?: string; |
| 22 | readonly customModels?: readonly string[]; |
| 23 | readonly binaryPath?: string; |
| 24 | }): CodexSettings => decodeCodexSettingsValue(input); |
| 25 | |
| 26 | const makeTempDir = Effect.fn("CodexHomeLayout.test.makeTempDir")(function* (prefix: string) { |
| 27 | const fileSystem = yield* FileSystem.FileSystem; |
| 28 | return yield* fileSystem.makeTempDirectoryScoped({ prefix }); |
| 29 | }); |
| 30 | |
| 31 | const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function* ( |
| 32 | filePath: string, |
| 33 | contents: string, |
| 34 | ) { |
| 35 | const fileSystem = yield* FileSystem.FileSystem; |
| 36 | const path = yield* Path.Path; |
| 37 | yield* fileSystem.makeDirectory(path.dirname(filePath), { recursive: true }); |
| 38 | yield* fileSystem.writeFileString(filePath, contents); |
| 39 | }); |
| 40 | |
| 41 | it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { |
| 42 | describe("resolveCodexHomeLayout", () => { |
| 43 | it.effect("uses the shared home for continuation and the shadow home for runtime", () => |
| 44 | Effect.gen(function* () { |
| 45 | const path = yield* Path.Path; |
| 46 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 47 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 48 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 49 | |
| 50 | const layout = yield* resolveCodexHomeLayout( |
| 51 | decodeCodexSettings({ |
| 52 | homePath: sharedHome, |
| 53 | shadowHomePath: shadowHome, |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | expect(layout).toMatchObject({ |
| 58 | mode: "authOverlay", |
| 59 | sharedHomePath: sharedHome, |
| 60 | effectiveHomePath: shadowHome, |
| 61 | continuationKey: `codex:home:${sharedHome}`, |
| 62 | }); |
| 63 | }), |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | describe("materializeCodexShadowHome", () => { |
| 68 | it.effect.skipIf(!symlinksSupported)( |
| 69 | "keeps Finder metadata local across repeated shadow home setup", |
| 70 | () => |
| 71 | Effect.gen(function* () { |
| 72 | const fileSystem = yield* FileSystem.FileSystem; |
| 73 | const path = yield* Path.Path; |
| 74 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 75 | const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); |
| 76 | const sharedMetadata = path.join(sharedHome, ".DS_Store"); |
| 77 | const shadowMetadata = path.join(shadowHome, ".DS_Store"); |
| 78 | |
| 79 | yield* writeTextFile(sharedMetadata, "shared Finder metadata"); |
| 80 | yield* writeTextFile(shadowMetadata, "shadow Finder metadata"); |
| 81 | yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n'); |
| 82 | const layout = yield* resolveCodexHomeLayout( |
| 83 | decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), |
| 84 | ); |
| 85 | |
| 86 | yield* materializeCodexShadowHome(layout); |
| 87 | expect(yield* fileSystem.readFileString(shadowMetadata)).toBe("shadow Finder metadata"); |
| 88 | yield* fileSystem.remove(shadowMetadata); |
| 89 | yield* materializeCodexShadowHome(layout); |
| 90 | expect(yield* fileSystem.exists(shadowMetadata)).toBe(false); |
| 91 | |
| 92 | yield* writeTextFile(shadowMetadata, "shadow Finder metadata"); |
| 93 | yield* materializeCodexShadowHome(layout); |
| 94 | yield* materializeCodexShadowHome(layout); |
| 95 | |
| 96 | expect(yield* fileSystem.readFileString(sharedMetadata)).toBe("shared Finder metadata"); |
| 97 | expect(yield* fileSystem.readFileString(shadowMetadata)).toBe("shadow Finder metadata"); |
| 98 | expect(yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"))).toBe( |
| 99 | '{"shadow":true}\n', |
| 100 | ); |
| 101 | expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe( |
| 102 | path.join(sharedHome, "sessions"), |
| 103 | ); |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | it.effect.skipIf(!symlinksSupported)( |
| 108 | "materializes a shadow home with shared state links and private auth", |
| 109 | () => |
| 110 | Effect.gen(function* () { |
| 111 | const fileSystem = yield* FileSystem.FileSystem; |
| 112 | const path = yield* Path.Path; |
| 113 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 114 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 115 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 116 | |
| 117 | yield* fileSystem.makeDirectory(path.join(sharedHome, "sessions")); |
| 118 | yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n'); |
| 119 | yield* writeTextFile( |
| 120 | path.join(sharedHome, "models_cache.json"), |
| 121 | '{"models":["shared"]}\n', |
| 122 | ); |
| 123 | yield* writeTextFile(path.join(sharedHome, "auth.json"), '{"shared":true}\n'); |
| 124 | yield* writeTextFile(path.join(sharedHome, "auth.lock"), ""); |
| 125 | yield* fileSystem.makeDirectory(shadowHome, { recursive: true }); |
| 126 | yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n'); |
| 127 | yield* writeTextFile(path.join(shadowHome, "auth.lock"), ""); |
| 128 | yield* fileSystem.symlink( |
| 129 | path.join(sharedHome, "models_cache.json"), |
| 130 | path.join(shadowHome, "models_cache.json"), |
| 131 | ); |
| 132 | |
| 133 | const layout = yield* resolveCodexHomeLayout( |
| 134 | decodeCodexSettings({ |
| 135 | homePath: sharedHome, |
| 136 | shadowHomePath: shadowHome, |
| 137 | }), |
| 138 | ); |
| 139 | |
| 140 | yield* materializeCodexShadowHome(layout); |
| 141 | |
| 142 | const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions")); |
| 143 | const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml")); |
| 144 | const mcpOauthLocksTarget = yield* fileSystem.readLink( |
| 145 | path.join(shadowHome, "mcp-oauth-locks"), |
| 146 | ); |
| 147 | const modelsCacheExists = yield* fileSystem.exists( |
| 148 | path.join(shadowHome, "models_cache.json"), |
| 149 | ); |
| 150 | const authLinkResult = yield* fileSystem |
| 151 | .readLink(path.join(shadowHome, "auth.json")) |
| 152 | .pipe(Effect.result); |
| 153 | const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json")); |
| 154 | const authLockLinkResult = yield* fileSystem |
| 155 | .readLink(path.join(shadowHome, "auth.lock")) |
| 156 | .pipe(Effect.result); |
| 157 | |
| 158 | expect(sessionsTarget).toBe(path.join(sharedHome, "sessions")); |
| 159 | expect(configTarget).toBe(path.join(sharedHome, "config.toml")); |
| 160 | expect(mcpOauthLocksTarget).toBe(path.join(sharedHome, "mcp-oauth-locks")); |
| 161 | expect(modelsCacheExists).toBe(false); |
| 162 | expect(authLinkResult._tag).toBe("Failure"); |
| 163 | expect(authContents).toContain("shadow"); |
| 164 | expect(authLockLinkResult._tag).toBe("Failure"); |
| 165 | }), |
| 166 | ); |
| 167 | |
| 168 | it.effect.skipIf(!symlinksSupported)( |
| 169 | "replaces a Codex-created local sqlite maintenance lock with the shared lock", |
| 170 | () => |
| 171 | Effect.gen(function* () { |
| 172 | const fileSystem = yield* FileSystem.FileSystem; |
| 173 | const path = yield* Path.Path; |
| 174 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 175 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 176 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 177 | const sharedLock = path.join(sharedHome, ".sqlite-maintenance.lock"); |
| 178 | const shadowLock = path.join(shadowHome, ".sqlite-maintenance.lock"); |
| 179 | |
| 180 | yield* writeTextFile(sharedLock, ""); |
| 181 | yield* writeTextFile(shadowLock, ""); |
| 182 | |
| 183 | const layout = yield* resolveCodexHomeLayout( |
| 184 | decodeCodexSettings({ |
| 185 | homePath: sharedHome, |
| 186 | shadowHomePath: shadowHome, |
| 187 | }), |
| 188 | ); |
| 189 | |
| 190 | yield* materializeCodexShadowHome(layout); |
| 191 | |
| 192 | expect(yield* fileSystem.readLink(shadowLock)).toBe(sharedLock); |
| 193 | }), |
| 194 | ); |
| 195 | |
| 196 | it.effect.skipIf(!symlinksSupported)( |
| 197 | "replaces Codex-created local MCP OAuth locks with the shared lock directory", |
| 198 | () => |
| 199 | Effect.gen(function* () { |
| 200 | const fileSystem = yield* FileSystem.FileSystem; |
| 201 | const path = yield* Path.Path; |
| 202 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 203 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 204 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 205 | const sharedLocks = path.join(sharedHome, "mcp-oauth-locks"); |
| 206 | const shadowLocks = path.join(shadowHome, "mcp-oauth-locks"); |
| 207 | |
| 208 | yield* writeTextFile(path.join(sharedLocks, "file-store.lock"), ""); |
| 209 | yield* writeTextFile(path.join(shadowLocks, "file-store.lock"), ""); |
| 210 | |
| 211 | const layout = yield* resolveCodexHomeLayout( |
| 212 | decodeCodexSettings({ |
| 213 | homePath: sharedHome, |
| 214 | shadowHomePath: shadowHome, |
| 215 | }), |
| 216 | ); |
| 217 | |
| 218 | yield* materializeCodexShadowHome(layout); |
| 219 | |
| 220 | const locksTarget = yield* fileSystem.readLink(shadowLocks); |
| 221 | const sharedLockExists = yield* fileSystem.exists( |
| 222 | path.join(sharedLocks, "file-store.lock"), |
| 223 | ); |
| 224 | |
| 225 | expect(locksTarget).toBe(sharedLocks); |
| 226 | expect(sharedLockExists).toBe(true); |
| 227 | }), |
| 228 | ); |
| 229 | |
| 230 | it.effect.skipIf(!symlinksSupported)( |
| 231 | "shares local thread writer locks once no writer still holds one", |
| 232 | () => |
| 233 | Effect.gen(function* () { |
| 234 | const fileSystem = yield* FileSystem.FileSystem; |
| 235 | const path = yield* Path.Path; |
| 236 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 237 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 238 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 239 | const shadowLocks = path.join(shadowHome, "thread-writer-locks"); |
| 240 | const threadLock = path.join(shadowLocks, "019a0000-0000-7000-8000-000000000000.lock"); |
| 241 | |
| 242 | yield* writeTextFile(path.join(shadowLocks, ".coordination.lock"), ""); |
| 243 | yield* writeTextFile(path.join(shadowLocks, ".DS_Store"), ""); |
| 244 | yield* writeTextFile(threadLock, ""); |
| 245 | |
| 246 | const layout = yield* resolveCodexHomeLayout( |
| 247 | decodeCodexSettings({ |
| 248 | homePath: sharedHome, |
| 249 | shadowHomePath: shadowHome, |
| 250 | }), |
| 251 | ); |
| 252 | |
| 253 | yield* materializeCodexShadowHome(layout); |
| 254 | expect(yield* fileSystem.exists(threadLock)).toBe(true); |
| 255 | |
| 256 | yield* fileSystem.remove(threadLock); |
| 257 | yield* materializeCodexShadowHome(layout); |
| 258 | expect(yield* fileSystem.readLink(shadowLocks)).toBe( |
| 259 | path.join(sharedHome, "thread-writer-locks"), |
| 260 | ); |
| 261 | }), |
| 262 | ); |
| 263 | |
| 264 | it.effect.skipIf(!symlinksSupported)( |
| 265 | "accepts Codex-created shadow-local runtime directories", |
| 266 | () => |
| 267 | Effect.gen(function* () { |
| 268 | const fileSystem = yield* FileSystem.FileSystem; |
| 269 | const path = yield* Path.Path; |
| 270 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 271 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 272 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 273 | |
| 274 | yield* fileSystem.makeDirectory(path.join(sharedHome, "log")); |
| 275 | yield* fileSystem.makeDirectory(path.join(sharedHome, "memories")); |
| 276 | yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp")); |
| 277 | yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n'); |
| 278 | yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n'); |
| 279 | yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true }); |
| 280 | yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true }); |
| 281 | yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true }); |
| 282 | |
| 283 | const layout = yield* resolveCodexHomeLayout( |
| 284 | decodeCodexSettings({ |
| 285 | homePath: sharedHome, |
| 286 | shadowHomePath: shadowHome, |
| 287 | }), |
| 288 | ); |
| 289 | |
| 290 | yield* materializeCodexShadowHome(layout); |
| 291 | |
| 292 | const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml")); |
| 293 | const logLinkResult = yield* fileSystem |
| 294 | .readLink(path.join(shadowHome, "log")) |
| 295 | .pipe(Effect.result); |
| 296 | const memoriesLinkResult = yield* fileSystem |
| 297 | .readLink(path.join(shadowHome, "memories")) |
| 298 | .pipe(Effect.result); |
| 299 | const tmpLinkResult = yield* fileSystem |
| 300 | .readLink(path.join(shadowHome, "tmp")) |
| 301 | .pipe(Effect.result); |
| 302 | |
| 303 | expect(configTarget).toBe(path.join(sharedHome, "config.toml")); |
| 304 | expect(logLinkResult._tag).toBe("Failure"); |
| 305 | expect(memoriesLinkResult._tag).toBe("Failure"); |
| 306 | expect(tmpLinkResult._tag).toBe("Failure"); |
| 307 | }), |
| 308 | ); |
| 309 | |
| 310 | it.effect("rejects shadow homes that point at the shared home", () => |
| 311 | Effect.gen(function* () { |
| 312 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 313 | const layout = yield* resolveCodexHomeLayout( |
| 314 | decodeCodexSettings({ |
| 315 | homePath: sharedHome, |
| 316 | shadowHomePath: sharedHome, |
| 317 | }), |
| 318 | ); |
| 319 | |
| 320 | const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); |
| 321 | |
| 322 | expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); |
| 323 | expect(error).toMatchObject({ |
| 324 | sharedHomePath: sharedHome, |
| 325 | effectiveHomePath: sharedHome, |
| 326 | }); |
| 327 | expect(error.message).toBe( |
| 328 | `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`, |
| 329 | ); |
| 330 | }), |
| 331 | ); |
| 332 | |
| 333 | it.effect.skipIf(!symlinksSupported)( |
| 334 | "rejects shared entries that already exist in the shadow home as real files", |
| 335 | () => |
| 336 | Effect.gen(function* () { |
| 337 | const path = yield* Path.Path; |
| 338 | const sharedHome = yield* makeTempDir("t3code-codex-shared-"); |
| 339 | const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); |
| 340 | const shadowHome = path.join(shadowRoot, "shadow"); |
| 341 | yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n'); |
| 342 | yield* writeTextFile(path.join(shadowHome, "config.toml"), 'model = "local"\n'); |
| 343 | |
| 344 | const layout = yield* resolveCodexHomeLayout( |
| 345 | decodeCodexSettings({ |
| 346 | homePath: sharedHome, |
| 347 | shadowHomePath: shadowHome, |
| 348 | }), |
| 349 | ); |
| 350 | |
| 351 | const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); |
| 352 | |
| 353 | expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError); |
| 354 | expect(error).toMatchObject({ |
| 355 | sharedHomePath: sharedHome, |
| 356 | effectiveHomePath: shadowHome, |
| 357 | entryName: "config.toml", |
| 358 | linkPath: path.join(shadowHome, "config.toml"), |
| 359 | targetPath: path.join(sharedHome, "config.toml"), |
| 360 | }); |
| 361 | expect(error.message).toBe( |
| 362 | `Cannot create Codex shadow home entry 'config.toml' because '${path.join(shadowHome, "config.toml")}' already exists and is not a symlink.`, |
| 363 | ); |
| 364 | }), |
| 365 | ); |
| 366 | }); |
| 367 | }); |