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