|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import * as NodeOS from "node:os"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import { expect, it } from "@effect/vitest"; |
| 6 | +import { ProviderInstanceId } from "@t3tools/contracts"; |
| 7 | +import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 8 | +import * as Effect from "effect/Effect"; |
| 9 | +import * as FileSystem from "effect/FileSystem"; |
| 10 | +import * as Layer from "effect/Layer"; |
| 11 | +import { HttpClient } from "effect/unstable/http"; |
| 12 | +import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; |
| 13 | + |
| 14 | +import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts"; |
| 15 | +import { ServerConfig } from "../../config.ts"; |
| 16 | +import { ServerSettingsService } from "../../serverSettings.ts"; |
| 17 | +import { layerTest as codexResetCreditLayerTest } from "../Layers/codexResetCredit.ts"; |
| 18 | +import { NoOpProviderEventLoggers, ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts"; |
| 19 | +import * as ModelManifest from "../ModelManifest.ts"; |
| 20 | +import { CodexDriver } from "./CodexDriver.ts"; |
| 21 | + |
| 22 | +const 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. |
| 44 | +const windowsHost = HostProcessPlatform.defaultValue() === "win32"; |
| 45 | + |
| 46 | +const noSpawn = ChildProcessSpawner.make(() => |
| 47 | + Effect.die("Disabled Codex must not spawn a process"), |
| 48 | +); |
| 49 | + |
| 50 | +it.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 | +}); |
0 commit comments