Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import { expect, it } from "@effect/vitest";
6import { ProviderInstanceId } from "@t3tools/contracts";
7import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import { HttpClient } from "effect/unstable/http";
12import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
13
14import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
15import { ServerConfig } from "../../config.ts";
16import { ServerSettingsService } from "../../serverSettings.ts";
17import { layerTest as codexResetCreditLayerTest } from "../Layers/codexResetCredit.ts";
18import { NoOpProviderEventLoggers, ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
19import * as ModelManifest from "../ModelManifest.ts";
20import { CodexDriver } from "./CodexDriver.ts";
21
22const testLayer = ServerConfig.layerTest(process.cwd(), {
23 prefix: "t3-codex-driver-maintenance-",
24}).pipe(
25 Layer.provideMerge(NodeServices.layer),
26 Layer.provideMerge(ServerSettingsService.layerTest()),
27 Layer.provideMerge(ModelManifest.layerTest),
28 Layer.provideMerge(codexResetCreditLayerTest),
29 Layer.provideMerge(
30 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
31 shouldRunScopeWork: () => Effect.succeed(false),
32 }),
33 ),
34 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
35 Layer.provideMerge(
36 Layer.succeed(
37 HttpClient.HttpClient,
38 HttpClient.make(() => Effect.die("Disabled Codex must not make an HTTP request")),
39 ),
40 ),
41);
42
43// The `#!/bin/sh` stub below cannot be resolved as an executable on Windows.
44const windowsHost = HostProcessPlatform.defaultValue() === "win32";
45
46const noSpawn = ChildProcessSpawner.make(() =>
47 Effect.die("Disabled Codex must not spawn a process"),
48);
49
50it.layer(testLayer)("CodexDriver", (it) => {
51 it.effect.skipIf(windowsHost)(
52 "runs the standalone updater against the shared home, not the shadow home",
53 () =>
54 Effect.gen(function* () {
55 const fs = yield* FileSystem.FileSystem;
56 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-driver-" });
57 const sharedHome = NodePath.join(tempDir, "codex-home");
58 const shadowHome = NodePath.join(tempDir, "codex-shadow");
59 const binaryPath = NodePath.join(sharedHome, "packages", "standalone", "bin", "codex");
60 yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
61 yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
62 yield* fs.chmod(binaryPath, 0o755);
63
64 const instance = yield* CodexDriver.create({
65 instanceId: ProviderInstanceId.make("codex-shadow"),
66 displayName: "Codex test",
67 enabled: false,
68 environment: [],
69 config: {
70 ...CodexDriver.defaultConfig(),
71 binaryPath,
72 homePath: sharedHome,
73 shadowHomePath: shadowHome,
74 },
75 });
76
77 const capabilities = yield* instance.snapshot.resolveMaintenance();
78 expect(capabilities.update).toMatchObject({
79 executable: binaryPath,
80 args: ["update"],
81 lockKey: "codex-native",
82 env: { CODEX_HOME: sharedHome },
83 });
84 }).pipe(
85 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
86 Effect.scoped,
87 ),
88 );
89
90 it.effect("stays manual-only when the configured executable does not exist", () =>
91 Effect.gen(function* () {
92 const instance = yield* CodexDriver.create({
93 instanceId: ProviderInstanceId.make("codex-missing"),
94 displayName: "Codex test",
95 enabled: false,
96 environment: [],
97 config: {
98 ...CodexDriver.defaultConfig(),
99 binaryPath: NodePath.join(NodeOS.tmpdir(), "t3-codex-missing", "codex"),
100 },
101 });
102 expect((yield* instance.snapshot.resolveMaintenance()).update).toBeNull();
103 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn), Effect.scoped),
104 );
105
106 for (const fixture of [
107 {
108 name: "leaves mise npm-backend installations manual-only",
109 installSegments: ["mise", "installs", "npm-openai-codex", "0.110.0"],
110 npmOwned: false,
111 },
112 {
113 name: "leaves mise tool aliases backed by npm manual-only",
114 installSegments: ["mise", "installs", "codex", "0.110.0"],
115 npmOwned: false,
116 },
117 {
118 name: "keeps npm updates for globals in a mise Node installation",
119 installSegments: ["mise", "installs", "node", "24.0.0"],
120 npmOwned: true,
121 },
122 {
123 name: "keeps npm updates for ordinary global installations",
124 installSegments: ["npm-global"],
125 npmOwned: true,
126 },
127 ] as const) {
128 it.effect.skipIf(windowsHost)(fixture.name, () =>
129 Effect.gen(function* () {
130 const fs = yield* FileSystem.FileSystem;
131 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-installer-" });
132 const installPath = NodePath.join(tempDir, ...fixture.installSegments);
133 const realBinaryPath = NodePath.join(
134 installPath,
135 "lib",
136 "node_modules",
137 "@openai",
138 "codex",
139 "bin",
140 "codex.js",
141 );
142 const binaryPath = NodePath.join(tempDir, "bin", "codex");
143 yield* fs.makeDirectory(NodePath.dirname(realBinaryPath), { recursive: true });
144 yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
145 yield* fs.writeFileString(realBinaryPath, "#!/bin/sh\n");
146 yield* fs.chmod(realBinaryPath, 0o755);
147 yield* fs.symlink(realBinaryPath, binaryPath);
148
149 const instance = yield* CodexDriver.create({
150 instanceId: ProviderInstanceId.make("codex-installer"),
151 displayName: "Codex installer test",
152 enabled: false,
153 environment: [],
154 config: {
155 ...CodexDriver.defaultConfig(),
156 binaryPath,
157 homePath: NodePath.join(tempDir, "codex-home"),
158 },
159 });
160
161 const update = (yield* instance.snapshot.resolveMaintenance()).update;
162 if (fixture.npmOwned) {
163 expect(update).toMatchObject({
164 executable: "npm",
165 args: [
166 "install",
167 "-g",
168 "--prefix",
169 installPath,
170 "--allow-scripts=@openai/codex",
171 "@openai/codex@latest",
172 ],
173 });
174 } else {
175 expect(update).toBeNull();
176 }
177 }).pipe(
178 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
179 Effect.scoped,
180 ),
181 );
182 }
183
184 for (const layout of ["direct", "wrapper"] as const) {
185 it.effect.skipIf(windowsHost)(`leaves a mise ${layout} installation manual-only`, () =>
186 Effect.gen(function* () {
187 const fs = yield* FileSystem.FileSystem;
188 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: `t3-codex-mise-${layout}-` });
189 const binaryPath =
190 layout === "direct"
191 ? NodePath.join(tempDir, "mise", "installs", "codex", "0.110.0", "codex")
192 : NodePath.join(tempDir, "omarchy", "bin", "codex");
193 yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
194 yield* fs.writeFileString(
195 binaryPath,
196 layout === "direct"
197 ? "#!/bin/sh\n"
198 : '#!/bin/sh\nmise use -g --quiet "codex" || exit 1\nexec mise x "codex" -- "codex" "$@"\n',
199 );
200 yield* fs.chmod(binaryPath, 0o755);
201
202 const instance = yield* CodexDriver.create({
203 instanceId: ProviderInstanceId.make(`codex-mise-${layout}`),
204 displayName: "Codex mise test",
205 enabled: false,
206 environment: [],
207 config: {
208 ...CodexDriver.defaultConfig(),
209 binaryPath,
210 homePath: NodePath.join(tempDir, "codex-home"),
211 },
212 });
213
214 expect((yield* instance.snapshot.resolveMaintenance()).update).toBeNull();
215 }).pipe(
216 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
217 Effect.scoped,
218 ),
219 );
220 }
221});