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";
21import { CodexAppServerClientFactory } from "../../orchestration-v2/Adapters/CodexAdapterV2.ts";
22import { layer as idAllocatorLayer } from "../../orchestration-v2/IdAllocator.ts";
23
24const testLayer = ServerConfig.layerTest(process.cwd(), {
25 prefix: "t3-codex-driver-maintenance-",
26}).pipe(
27 Layer.provideMerge(NodeServices.layer),
28 Layer.provideMerge(idAllocatorLayer),
29 Layer.provideMerge(
30 Layer.mock(CodexAppServerClientFactory)({
31 open: () => Effect.die("Maintenance resolution must not open a Codex session"),
32 }),
33 ),
34 Layer.provideMerge(ServerSettingsService.layerTest()),
35 Layer.provideMerge(ModelManifest.layerTest),
36 Layer.provideMerge(codexResetCreditLayerTest),
37 Layer.provideMerge(
38 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
39 shouldRunScopeWork: () => Effect.succeed(false),
40 }),
41 ),
42 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
43 Layer.provideMerge(
44 Layer.succeed(
45 HttpClient.HttpClient,
46 HttpClient.make(() => Effect.die("Disabled Codex must not make an HTTP request")),
47 ),
48 ),
49);
50
51// The `#!/bin/sh` stub below cannot be resolved as an executable on Windows.
52const windowsHost = HostProcessPlatform.defaultValue() === "win32";
53
54const noSpawn = ChildProcessSpawner.make(() =>
55 Effect.die("Disabled Codex must not spawn a process"),
56);
57
58it.layer(testLayer)("CodexDriver", (it) => {
59 it.effect.skipIf(windowsHost)(
60 "runs the standalone updater against the shared home, not the shadow home",
61 () =>
62 Effect.gen(function* () {
63 const fs = yield* FileSystem.FileSystem;
64 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-driver-" });
65 const sharedHome = NodePath.join(tempDir, "codex-home");
66 const shadowHome = NodePath.join(tempDir, "codex-shadow");
67 const binaryPath = NodePath.join(sharedHome, "packages", "standalone", "bin", "codex");
68 yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
69 yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
70 yield* fs.chmod(binaryPath, 0o755);
71
72 const instance = yield* CodexDriver.create({
73 instanceId: ProviderInstanceId.make("codex-shadow"),
74 displayName: "Codex test",
75 enabled: false,
76 environment: [],
77 config: {
78 ...CodexDriver.defaultConfig(),
79 binaryPath,
80 homePath: sharedHome,
81 shadowHomePath: shadowHome,
82 },
83 });
84
85 const capabilities = yield* instance.snapshot.resolveMaintenance();
86 expect(capabilities.update).toMatchObject({
87 executable: binaryPath,
88 args: ["update"],
89 lockKey: "codex-native",
90 env: { CODEX_HOME: sharedHome },
91 });
92 }).pipe(
93 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
94 Effect.scoped,
95 ),
96 );
97
98 it.effect("stays manual-only when the configured executable does not exist", () =>
99 Effect.gen(function* () {
100 const instance = yield* CodexDriver.create({
101 instanceId: ProviderInstanceId.make("codex-missing"),
102 displayName: "Codex test",
103 enabled: false,
104 environment: [],
105 config: {
106 ...CodexDriver.defaultConfig(),
107 binaryPath: NodePath.join(NodeOS.tmpdir(), "t3-codex-missing", "codex"),
108 },
109 });
110 expect((yield* instance.snapshot.resolveMaintenance()).update).toBeNull();
111 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn), Effect.scoped),
112 );
113});