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

CodexHomeLayout.test.ts

267 lines10.7 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 modelsCacheExists = yield* fileSystem.exists(
118 path.join(shadowHome, "models_cache.json"),
119 );
120 const authLinkResult = yield* fileSystem
121 .readLink(path.join(shadowHome, "auth.json"))
122 .pipe(Effect.result);
123 const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"));
124​
125 expect(sessionsTarget).toBe(path.join(sharedHome, "sessions"));
126 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
127 expect(modelsCacheExists).toBe(false);
128 expect(authLinkResult._tag).toBe("Failure");
129 expect(authContents).toContain("shadow");
130 }),
131 );
132​
133 it.effect("accepts Codex-created shadow-local runtime directories", () =>
134 Effect.gen(function* () {
135 const fileSystem = yield* FileSystem.FileSystem;
136 const path = yield* Path.Path;
137 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
138 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
139 const shadowHome = path.join(shadowRoot, "shadow");
140​
141 yield* fileSystem.makeDirectory(path.join(sharedHome, "log"));
142 yield* fileSystem.makeDirectory(path.join(sharedHome, "memories"));
143 yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp"));
144 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
145 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
146 yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true });
147 yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true });
148 yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true });
149​
150 const layout = yield* resolveCodexHomeLayout(
151 decodeCodexSettings({
152 homePath: sharedHome,
153 shadowHomePath: shadowHome,
154 }),
155 );
156​
157 yield* materializeCodexShadowHome(layout);
158​
159 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
160 const logLinkResult = yield* fileSystem
161 .readLink(path.join(shadowHome, "log"))
162 .pipe(Effect.result);
163 const memoriesLinkResult = yield* fileSystem
164 .readLink(path.join(shadowHome, "memories"))
165 .pipe(Effect.result);
166 const tmpLinkResult = yield* fileSystem
167 .readLink(path.join(shadowHome, "tmp"))
168 .pipe(Effect.result);
169​
170 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
171 expect(logLinkResult._tag).toBe("Failure");
172 expect(memoriesLinkResult._tag).toBe("Failure");
173 expect(tmpLinkResult._tag).toBe("Failure");
174 }),
175 );
176​
177 it.effect("rejects shadow homes that point at the shared home", () =>
178 Effect.gen(function* () {
179 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
180 const layout = yield* resolveCodexHomeLayout(
181 decodeCodexSettings({
182 homePath: sharedHome,
183 shadowHomePath: sharedHome,
184 }),
185 );
186​
187 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
188​
189 expect(error).toBeInstanceOf(CodexShadowHomePathConflictError);
190 expect(error).toMatchObject({
191 sharedHomePath: sharedHome,
192 effectiveHomePath: sharedHome,
193 });
194 expect(error.message).toBe(
195 `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`,
196 );
197 }),
198 );
199​
200 it.effect("rejects shared entries that already exist in the shadow home as real files", () =>
201 Effect.gen(function* () {
202 const path = yield* Path.Path;
203 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
204 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
205 const shadowHome = path.join(shadowRoot, "shadow");
206 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
207 yield* writeTextFile(path.join(shadowHome, "config.toml"), 'model = "local"\n');
208​
209 const layout = yield* resolveCodexHomeLayout(
210 decodeCodexSettings({
211 homePath: sharedHome,
212 shadowHomePath: shadowHome,
213 }),
214 );
215​
216 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
217​
218 expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError);
219 expect(error).toMatchObject({
220 sharedHomePath: sharedHome,
221 effectiveHomePath: shadowHome,
222 entryName: "config.toml",
223 linkPath: path.join(shadowHome, "config.toml"),
224 targetPath: path.join(sharedHome, "config.toml"),
225 });
226 expect(error.message).toBe(
227 `Cannot create Codex shadow home entry 'config.toml' because '${path.join(shadowHome, "config.toml")}' already exists and is not a symlink.`,
228 );
229 }),
230 );
231​
232 it.effect("preserves filesystem operation, paths, and cause", () =>
233 Effect.gen(function* () {
234 const path = yield* Path.Path;
235 const sharedRoot = yield* makeTempDir("t3code-codex-shared-root-");
236 const sharedHome = path.join(sharedRoot, "shared-home");
237 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
238 const shadowHome = path.join(shadowRoot, "shadow");
239 yield* writeTextFile(sharedHome, "not a directory\n");
240​
241 const layout = yield* resolveCodexHomeLayout(
242 decodeCodexSettings({
243 homePath: sharedHome,
244 shadowHomePath: shadowHome,
245 }),
246 );
247​
248 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
249​
250 expect(error._tag).toBe("CodexShadowHomeFileSystemError");
251 if (error._tag !== "CodexShadowHomeFileSystemError") {
252 return expect.fail("Expected CodexShadowHomeFileSystemError");
253 }
254 expect(error).toMatchObject({
255 operation: "makeDirectory",
256 sharedHomePath: sharedHome,
257 effectiveHomePath: shadowHome,
258 });
259 expect(error.path.startsWith(sharedHome)).toBe(true);
260 expect(error.cause).toBeInstanceOf(PlatformError.PlatformError);
261 expect(error.message).toBe(
262 `Codex shadow home filesystem operation 'makeDirectory' failed for '${error.path}'.`,
263 );
264 }),
265 );
266 });
267});