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

CodexHomeLayout.test.ts

315 lines12.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 "materializes a shadow home with shared state links and private auth",
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 shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
96 const shadowHome = path.join(shadowRoot, "shadow");
97​
98 yield* fileSystem.makeDirectory(path.join(sharedHome, "sessions"));
99 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
100 yield* writeTextFile(
101 path.join(sharedHome, "models_cache.json"),
102 '{"models":["shared"]}\n',
103 );
104 yield* writeTextFile(path.join(sharedHome, "auth.json"), '{"shared":true}\n');
105 yield* fileSystem.makeDirectory(shadowHome, { recursive: true });
106 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
107 yield* fileSystem.symlink(
108 path.join(sharedHome, "models_cache.json"),
109 path.join(shadowHome, "models_cache.json"),
110 );
111​
112 const layout = yield* resolveCodexHomeLayout(
113 decodeCodexSettings({
114 homePath: sharedHome,
115 shadowHomePath: shadowHome,
116 }),
117 );
118​
119 yield* materializeCodexShadowHome(layout);
120​
121 const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions"));
122 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
123 const mcpOauthLocksTarget = yield* fileSystem.readLink(
124 path.join(shadowHome, "mcp-oauth-locks"),
125 );
126 const modelsCacheExists = yield* fileSystem.exists(
127 path.join(shadowHome, "models_cache.json"),
128 );
129 const authLinkResult = yield* fileSystem
130 .readLink(path.join(shadowHome, "auth.json"))
131 .pipe(Effect.result);
132 const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"));
133​
134 expect(sessionsTarget).toBe(path.join(sharedHome, "sessions"));
135 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
136 expect(mcpOauthLocksTarget).toBe(path.join(sharedHome, "mcp-oauth-locks"));
137 expect(modelsCacheExists).toBe(false);
138 expect(authLinkResult._tag).toBe("Failure");
139 expect(authContents).toContain("shadow");
140 }),
141 );
142​
143 it.effect.skipIf(!symlinksSupported)(
144 "replaces Codex-created local MCP OAuth locks with the shared lock directory",
145 () =>
146 Effect.gen(function* () {
147 const fileSystem = yield* FileSystem.FileSystem;
148 const path = yield* Path.Path;
149 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
150 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
151 const shadowHome = path.join(shadowRoot, "shadow");
152 const sharedLocks = path.join(sharedHome, "mcp-oauth-locks");
153 const shadowLocks = path.join(shadowHome, "mcp-oauth-locks");
154​
155 yield* writeTextFile(path.join(sharedLocks, "file-store.lock"), "");
156 yield* writeTextFile(path.join(shadowLocks, "file-store.lock"), "");
157​
158 const layout = yield* resolveCodexHomeLayout(
159 decodeCodexSettings({
160 homePath: sharedHome,
161 shadowHomePath: shadowHome,
162 }),
163 );
164​
165 yield* materializeCodexShadowHome(layout);
166​
167 const locksTarget = yield* fileSystem.readLink(shadowLocks);
168 const sharedLockExists = yield* fileSystem.exists(
169 path.join(sharedLocks, "file-store.lock"),
170 );
171​
172 expect(locksTarget).toBe(sharedLocks);
173 expect(sharedLockExists).toBe(true);
174 }),
175 );
176​
177 it.effect.skipIf(!symlinksSupported)(
178 "accepts Codex-created shadow-local runtime directories",
179 () =>
180 Effect.gen(function* () {
181 const fileSystem = yield* FileSystem.FileSystem;
182 const path = yield* Path.Path;
183 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
184 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
185 const shadowHome = path.join(shadowRoot, "shadow");
186​
187 yield* fileSystem.makeDirectory(path.join(sharedHome, "log"));
188 yield* fileSystem.makeDirectory(path.join(sharedHome, "memories"));
189 yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp"));
190 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
191 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
192 yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true });
193 yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true });
194 yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true });
195​
196 const layout = yield* resolveCodexHomeLayout(
197 decodeCodexSettings({
198 homePath: sharedHome,
199 shadowHomePath: shadowHome,
200 }),
201 );
202​
203 yield* materializeCodexShadowHome(layout);
204​
205 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
206 const logLinkResult = yield* fileSystem
207 .readLink(path.join(shadowHome, "log"))
208 .pipe(Effect.result);
209 const memoriesLinkResult = yield* fileSystem
210 .readLink(path.join(shadowHome, "memories"))
211 .pipe(Effect.result);
212 const tmpLinkResult = yield* fileSystem
213 .readLink(path.join(shadowHome, "tmp"))
214 .pipe(Effect.result);
215​
216 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
217 expect(logLinkResult._tag).toBe("Failure");
218 expect(memoriesLinkResult._tag).toBe("Failure");
219 expect(tmpLinkResult._tag).toBe("Failure");
220 }),
221 );
222​
223 it.effect("rejects shadow homes that point at the shared home", () =>
224 Effect.gen(function* () {
225 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
226 const layout = yield* resolveCodexHomeLayout(
227 decodeCodexSettings({
228 homePath: sharedHome,
229 shadowHomePath: sharedHome,
230 }),
231 );
232​
233 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
234​
235 expect(error).toBeInstanceOf(CodexShadowHomePathConflictError);
236 expect(error).toMatchObject({
237 sharedHomePath: sharedHome,
238 effectiveHomePath: sharedHome,
239 });
240 expect(error.message).toBe(
241 `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`,
242 );
243 }),
244 );
245​
246 it.effect.skipIf(!symlinksSupported)(
247 "rejects shared entries that already exist in the shadow home as real files",
248 () =>
249 Effect.gen(function* () {
250 const path = yield* Path.Path;
251 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
252 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
253 const shadowHome = path.join(shadowRoot, "shadow");
254 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
255 yield* writeTextFile(path.join(shadowHome, "config.toml"), 'model = "local"\n');
256​
257 const layout = yield* resolveCodexHomeLayout(
258 decodeCodexSettings({
259 homePath: sharedHome,
260 shadowHomePath: shadowHome,
261 }),
262 );
263​
264 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
265​
266 expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError);
267 expect(error).toMatchObject({
268 sharedHomePath: sharedHome,
269 effectiveHomePath: shadowHome,
270 entryName: "config.toml",
271 linkPath: path.join(shadowHome, "config.toml"),
272 targetPath: path.join(sharedHome, "config.toml"),
273 });
274 expect(error.message).toBe(
275 `Cannot create Codex shadow home entry 'config.toml' because '${path.join(shadowHome, "config.toml")}' already exists and is not a symlink.`,
276 );
277 }),
278 );
279​
280 it.effect("preserves filesystem operation, paths, and cause", () =>
281 Effect.gen(function* () {
282 const path = yield* Path.Path;
283 const sharedRoot = yield* makeTempDir("t3code-codex-shared-root-");
284 const sharedHome = path.join(sharedRoot, "shared-home");
285 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
286 const shadowHome = path.join(shadowRoot, "shadow");
287 yield* writeTextFile(sharedHome, "not a directory\n");
288​
289 const layout = yield* resolveCodexHomeLayout(
290 decodeCodexSettings({
291 homePath: sharedHome,
292 shadowHomePath: shadowHome,
293 }),
294 );
295​
296 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
297​
298 expect(error._tag).toBe("CodexShadowHomeFileSystemError");
299 if (error._tag !== "CodexShadowHomeFileSystemError") {
300 return expect.fail("Expected CodexShadowHomeFileSystemError");
301 }
302 expect(error).toMatchObject({
303 operation: "makeDirectory",
304 sharedHomePath: sharedHome,
305 effectiveHomePath: shadowHome,
306 });
307 expect(error.path.startsWith(sharedHome)).toBe(true);
308 expect(error.cause).toBeInstanceOf(PlatformError.PlatformError);
309 expect(error.message).toBe(
310 `Codex shadow home filesystem operation 'makeDirectory' failed for '${error.path}'.`,
311 );
312 }),
313 );
314 });
315});