Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7
8import { CodexSettings } from "@t3tools/contracts";
9import {
10 CodexShadowHomeEntryConflictError,
11 CodexShadowHomePathConflictError,
12 materializeCodexShadowHome,
13 resolveCodexHomeLayout,
14} from "./CodexHomeLayout.ts";
15import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
16const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings);
17
18const 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
26const makeTempDir = Effect.fn("CodexHomeLayout.test.makeTempDir")(function* (prefix: string) {
27 const fileSystem = yield* FileSystem.FileSystem;
28 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
29});
30
31const 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
41it.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* fileSystem.makeDirectory(shadowHome, { recursive: true });
125 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
126 yield* fileSystem.symlink(
127 path.join(sharedHome, "models_cache.json"),
128 path.join(shadowHome, "models_cache.json"),
129 );
130
131 const layout = yield* resolveCodexHomeLayout(
132 decodeCodexSettings({
133 homePath: sharedHome,
134 shadowHomePath: shadowHome,
135 }),
136 );
137
138 yield* materializeCodexShadowHome(layout);
139
140 const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions"));
141 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
142 const mcpOauthLocksTarget = yield* fileSystem.readLink(
143 path.join(shadowHome, "mcp-oauth-locks"),
144 );
145 const modelsCacheExists = yield* fileSystem.exists(
146 path.join(shadowHome, "models_cache.json"),
147 );
148 const authLinkResult = yield* fileSystem
149 .readLink(path.join(shadowHome, "auth.json"))
150 .pipe(Effect.result);
151 const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"));
152
153 expect(sessionsTarget).toBe(path.join(sharedHome, "sessions"));
154 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
155 expect(mcpOauthLocksTarget).toBe(path.join(sharedHome, "mcp-oauth-locks"));
156 expect(modelsCacheExists).toBe(false);
157 expect(authLinkResult._tag).toBe("Failure");
158 expect(authContents).toContain("shadow");
159 }),
160 );
161
162 it.effect.skipIf(!symlinksSupported)(
163 "replaces a Codex-created local sqlite maintenance lock with the shared lock",
164 () =>
165 Effect.gen(function* () {
166 const fileSystem = yield* FileSystem.FileSystem;
167 const path = yield* Path.Path;
168 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
169 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
170 const shadowHome = path.join(shadowRoot, "shadow");
171 const sharedLock = path.join(sharedHome, ".sqlite-maintenance.lock");
172 const shadowLock = path.join(shadowHome, ".sqlite-maintenance.lock");
173
174 yield* writeTextFile(sharedLock, "");
175 yield* writeTextFile(shadowLock, "");
176
177 const layout = yield* resolveCodexHomeLayout(
178 decodeCodexSettings({
179 homePath: sharedHome,
180 shadowHomePath: shadowHome,
181 }),
182 );
183
184 yield* materializeCodexShadowHome(layout);
185
186 expect(yield* fileSystem.readLink(shadowLock)).toBe(sharedLock);
187 }),
188 );
189
190 it.effect.skipIf(!symlinksSupported)(
191 "replaces Codex-created local MCP OAuth locks with the shared lock directory",
192 () =>
193 Effect.gen(function* () {
194 const fileSystem = yield* FileSystem.FileSystem;
195 const path = yield* Path.Path;
196 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
197 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
198 const shadowHome = path.join(shadowRoot, "shadow");
199 const sharedLocks = path.join(sharedHome, "mcp-oauth-locks");
200 const shadowLocks = path.join(shadowHome, "mcp-oauth-locks");
201
202 yield* writeTextFile(path.join(sharedLocks, "file-store.lock"), "");
203 yield* writeTextFile(path.join(shadowLocks, "file-store.lock"), "");
204
205 const layout = yield* resolveCodexHomeLayout(
206 decodeCodexSettings({
207 homePath: sharedHome,
208 shadowHomePath: shadowHome,
209 }),
210 );
211
212 yield* materializeCodexShadowHome(layout);
213
214 const locksTarget = yield* fileSystem.readLink(shadowLocks);
215 const sharedLockExists = yield* fileSystem.exists(
216 path.join(sharedLocks, "file-store.lock"),
217 );
218
219 expect(locksTarget).toBe(sharedLocks);
220 expect(sharedLockExists).toBe(true);
221 }),
222 );
223
224 it.effect.skipIf(!symlinksSupported)(
225 "accepts Codex-created shadow-local runtime directories",
226 () =>
227 Effect.gen(function* () {
228 const fileSystem = yield* FileSystem.FileSystem;
229 const path = yield* Path.Path;
230 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
231 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
232 const shadowHome = path.join(shadowRoot, "shadow");
233
234 yield* fileSystem.makeDirectory(path.join(sharedHome, "log"));
235 yield* fileSystem.makeDirectory(path.join(sharedHome, "memories"));
236 yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp"));
237 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
238 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
239 yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true });
240 yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true });
241 yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true });
242
243 const layout = yield* resolveCodexHomeLayout(
244 decodeCodexSettings({
245 homePath: sharedHome,
246 shadowHomePath: shadowHome,
247 }),
248 );
249
250 yield* materializeCodexShadowHome(layout);
251
252 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
253 const logLinkResult = yield* fileSystem
254 .readLink(path.join(shadowHome, "log"))
255 .pipe(Effect.result);
256 const memoriesLinkResult = yield* fileSystem
257 .readLink(path.join(shadowHome, "memories"))
258 .pipe(Effect.result);
259 const tmpLinkResult = yield* fileSystem
260 .readLink(path.join(shadowHome, "tmp"))
261 .pipe(Effect.result);
262
263 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
264 expect(logLinkResult._tag).toBe("Failure");
265 expect(memoriesLinkResult._tag).toBe("Failure");
266 expect(tmpLinkResult._tag).toBe("Failure");
267 }),
268 );
269
270 it.effect("rejects shadow homes that point at the shared home", () =>
271 Effect.gen(function* () {
272 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
273 const layout = yield* resolveCodexHomeLayout(
274 decodeCodexSettings({
275 homePath: sharedHome,
276 shadowHomePath: sharedHome,
277 }),
278 );
279
280 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
281
282 expect(error).toBeInstanceOf(CodexShadowHomePathConflictError);
283 expect(error).toMatchObject({
284 sharedHomePath: sharedHome,
285 effectiveHomePath: sharedHome,
286 });
287 expect(error.message).toBe(
288 `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`,
289 );
290 }),
291 );
292
293 it.effect.skipIf(!symlinksSupported)(
294 "rejects shared entries that already exist in the shadow home as real files",
295 () =>
296 Effect.gen(function* () {
297 const path = yield* Path.Path;
298 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
299 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
300 const shadowHome = path.join(shadowRoot, "shadow");
301 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
302 yield* writeTextFile(path.join(shadowHome, "config.toml"), 'model = "local"\n');
303
304 const layout = yield* resolveCodexHomeLayout(
305 decodeCodexSettings({
306 homePath: sharedHome,
307 shadowHomePath: shadowHome,
308 }),
309 );
310
311 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
312
313 expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError);
314 expect(error).toMatchObject({
315 sharedHomePath: sharedHome,
316 effectiveHomePath: shadowHome,
317 entryName: "config.toml",
318 linkPath: path.join(shadowHome, "config.toml"),
319 targetPath: path.join(sharedHome, "config.toml"),
320 });
321 expect(error.message).toBe(
322 `Cannot create Codex shadow home entry 'config.toml' because '${path.join(shadowHome, "config.toml")}' already exists and is not a symlink.`,
323 );
324 }),
325 );
326 });
327});