Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexHomeLayout.test.ts

382 lines15.6 KB

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