Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import { Effect, FileSystem, Path, Schema } from "effect";
4
5import { CodexSettings } from "@t3tools/contracts";
6import {
7 CodexShadowHomeError,
8 materializeCodexShadowHome,
9 resolveCodexHomeLayout,
10} from "./CodexHomeLayout.ts";
11
12const decodeCodexSettings = (input: {
13 readonly enabled?: boolean;
14 readonly homePath?: string;
15 readonly shadowHomePath?: string;
16 readonly customModels?: readonly string[];
17 readonly binaryPath?: string;
18}): CodexSettings => Schema.decodeSync(CodexSettings)(input);
19
20const makeTempDir = Effect.fn("CodexHomeLayout.test.makeTempDir")(function* (prefix: string) {
21 const fileSystem = yield* FileSystem.FileSystem;
22 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
23});
24
25const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function* (
26 filePath: string,
27 contents: string,
28) {
29 const fileSystem = yield* FileSystem.FileSystem;
30 const path = yield* Path.Path;
31 yield* fileSystem.makeDirectory(path.dirname(filePath), { recursive: true });
32 yield* fileSystem.writeFileString(filePath, contents);
33});
34
35it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
36 describe("resolveCodexHomeLayout", () => {
37 it.effect("uses direct CODEX_HOME when no shadow home is configured", () =>
38 Effect.gen(function* () {
39 const homePath = yield* makeTempDir("t3code-codex-home-");
40
41 const layout = yield* resolveCodexHomeLayout(
42 decodeCodexSettings({
43 homePath,
44 }),
45 );
46
47 expect(layout).toMatchObject({
48 mode: "direct",
49 sharedHomePath: homePath,
50 effectiveHomePath: homePath,
51 continuationKey: `codex:home:${homePath}`,
52 });
53 }),
54 );
55
56 it.effect("uses the shared home for continuation and the shadow home for runtime", () =>
57 Effect.gen(function* () {
58 const path = yield* Path.Path;
59 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
60 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
61 const shadowHome = path.join(shadowRoot, "shadow");
62
63 const layout = yield* resolveCodexHomeLayout(
64 decodeCodexSettings({
65 homePath: sharedHome,
66 shadowHomePath: shadowHome,
67 }),
68 );
69
70 expect(layout).toMatchObject({
71 mode: "authOverlay",
72 sharedHomePath: sharedHome,
73 effectiveHomePath: shadowHome,
74 continuationKey: `codex:home:${sharedHome}`,
75 });
76 }),
77 );
78 });
79
80 describe("materializeCodexShadowHome", () => {
81 it.effect("materializes a shadow home with shared state links and private auth", () =>
82 Effect.gen(function* () {
83 const fileSystem = yield* FileSystem.FileSystem;
84 const path = yield* Path.Path;
85 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
86 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
87 const shadowHome = path.join(shadowRoot, "shadow");
88
89 yield* fileSystem.makeDirectory(path.join(sharedHome, "sessions"));
90 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
91 yield* writeTextFile(path.join(sharedHome, "models_cache.json"), '{"models":["shared"]}\n');
92 yield* writeTextFile(path.join(sharedHome, "auth.json"), '{"shared":true}\n');
93 yield* fileSystem.makeDirectory(shadowHome, { recursive: true });
94 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
95 yield* fileSystem.symlink(
96 path.join(sharedHome, "models_cache.json"),
97 path.join(shadowHome, "models_cache.json"),
98 );
99
100 const layout = yield* resolveCodexHomeLayout(
101 decodeCodexSettings({
102 homePath: sharedHome,
103 shadowHomePath: shadowHome,
104 }),
105 );
106
107 yield* materializeCodexShadowHome(layout);
108
109 const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions"));
110 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
111 const modelsCacheExists = yield* fileSystem.exists(
112 path.join(shadowHome, "models_cache.json"),
113 );
114 const authLinkResult = yield* fileSystem
115 .readLink(path.join(shadowHome, "auth.json"))
116 .pipe(Effect.result);
117 const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"));
118
119 expect(sessionsTarget).toBe(path.join(sharedHome, "sessions"));
120 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
121 expect(modelsCacheExists).toBe(false);
122 expect(authLinkResult._tag).toBe("Failure");
123 expect(authContents).toContain("shadow");
124 }),
125 );
126
127 it.effect("accepts Codex-created shadow-local runtime directories", () =>
128 Effect.gen(function* () {
129 const fileSystem = yield* FileSystem.FileSystem;
130 const path = yield* Path.Path;
131 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
132 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
133 const shadowHome = path.join(shadowRoot, "shadow");
134
135 yield* fileSystem.makeDirectory(path.join(sharedHome, "log"));
136 yield* fileSystem.makeDirectory(path.join(sharedHome, "memories"));
137 yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp"));
138 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
139 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
140 yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true });
141 yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true });
142 yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true });
143
144 const layout = yield* resolveCodexHomeLayout(
145 decodeCodexSettings({
146 homePath: sharedHome,
147 shadowHomePath: shadowHome,
148 }),
149 );
150
151 yield* materializeCodexShadowHome(layout);
152
153 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
154 const logLinkResult = yield* fileSystem
155 .readLink(path.join(shadowHome, "log"))
156 .pipe(Effect.result);
157 const memoriesLinkResult = yield* fileSystem
158 .readLink(path.join(shadowHome, "memories"))
159 .pipe(Effect.result);
160 const tmpLinkResult = yield* fileSystem
161 .readLink(path.join(shadowHome, "tmp"))
162 .pipe(Effect.result);
163
164 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
165 expect(logLinkResult._tag).toBe("Failure");
166 expect(memoriesLinkResult._tag).toBe("Failure");
167 expect(tmpLinkResult._tag).toBe("Failure");
168 }),
169 );
170
171 it.effect("rejects shadow homes that point at the shared home", () =>
172 Effect.gen(function* () {
173 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
174 const layout = yield* resolveCodexHomeLayout(
175 decodeCodexSettings({
176 homePath: sharedHome,
177 shadowHomePath: sharedHome,
178 }),
179 );
180
181 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
182
183 expect(error).toBeInstanceOf(CodexShadowHomeError);
184 }),
185 );
186
187 it.effect("rejects shared entries that already exist in the shadow home as real files", () =>
188 Effect.gen(function* () {
189 const path = yield* Path.Path;
190 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
191 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
192 const shadowHome = path.join(shadowRoot, "shadow");
193 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
194 yield* writeTextFile(path.join(shadowHome, "config.toml"), 'model = "local"\n');
195
196 const layout = yield* resolveCodexHomeLayout(
197 decodeCodexSettings({
198 homePath: sharedHome,
199 shadowHomePath: shadowHome,
200 }),
201 );
202
203 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
204
205 expect(error.detail).toContain("already exists and is not a symlink");
206 }),
207 );
208 });
209});