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

CodexHomeLayout.test.ts

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