Files
openclaw/extensions/codex/src/app-server/managed-binary.test.ts
Peter Steinberger 96f0983a85 fix(onboarding): skip setup for configured gateways and require inference first (#102883)
* fix(crestodian): keep onboarding RPCs restart-safe

* fix(profiles): isolate approval state migrations

* fix(crestodian): bypass configured gateway setup

* test(crestodian): type onboarding mocks

* fix(onboarding): require inference before Crestodian

* fix(onboarding): enforce verified inference handoff

* fix(macos): reset setup on gateway endpoint edits

* chore(i18n): refresh native source inventory

* fix(gateway): keep socket on request cancellation

* test(packaging): require workspace templates

* fix(onboarding): bind setup to verified inference

* fix(onboarding): align inference gate contracts

* fix(crestodian): classify concurrent policy rejection

* test(crestodian): expect registry restoration

* fix(onboarding): bind setup to configured gateways

* fix(codex): preserve startup phase deadlines

* test(crestodian): match fail-closed policy ordering

* test(onboarding): assert bound gateway handoff

* fix(codex): bind runtime resolution to spawn cwd

* test(crestodian): assert policy rejection order

* fix(cli): preserve gateway routing across restarts

* fix(macos): fail closed during gateway edits

* test(macos): cover gateway route generation races

* chore: keep release notes out of onboarding PR

* fix(ci): refresh onboarding generated checks

* style(swift): align gateway channel formatting

* fix(ci): refresh plugin SDK surface budgets

* fix(ci): resync native string inventory

* refactor(swift): split gateway channel support

* test(doctor): isolate plugin compatibility registry

* test(macos): isolate gateway onboarding fixtures

* test(macos): assert gateway lease health ordering

* fix(codex): reconcile computer-use startup changes
2026-07-11 10:25:14 -07:00

308 lines
11 KiB
TypeScript

// Codex tests cover managed binary plugin behavior.
import { mkdir, mkdtemp, realpath, writeFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import type { CodexAppServerStartOptions } from "./config.js";
import {
resolveManagedCodexAppServerPaths,
resolveManagedCodexAppServerStartOptions,
resolveManagedCodexNativeCommand,
testing,
} from "./managed-binary.js";
function startOptions(
commandSource: CodexAppServerStartOptions["commandSource"],
managedCommandOrder?: CodexAppServerStartOptions["managedCommandOrder"],
): CodexAppServerStartOptions {
return {
transport: "stdio",
command: "codex",
commandSource,
...(managedCommandOrder ? { managedCommandOrder } : {}),
args: ["app-server", "--listen", "stdio://"],
headers: {},
};
}
function managedCommandPath(root: string, platform: NodeJS.Platform): string {
const pathApi = platform === "win32" ? path.win32 : path.posix;
return pathApi.join(root, "node_modules", ".bin", platform === "win32" ? "codex.cmd" : "codex");
}
const MACOS_DESKTOP_CODEX_APP_SERVER_COMMAND = "/Applications/Codex.app/Contents/Resources/codex";
const MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND =
"/Applications/ChatGPT.app/Contents/Resources/codex";
describe("managed Codex app-server binary", () => {
it("resolves the platform-native artifact behind the managed npm launcher", () => {
const packageJsonPath =
"/repo/extensions/codex/node_modules/@openai/codex-darwin-arm64/package.json";
const expected =
"/repo/extensions/codex/node_modules/@openai/codex-darwin-arm64/vendor/aarch64-apple-darwin/bin/codex";
expect(
resolveManagedCodexNativeCommand("/repo/extensions/codex/node_modules/.bin/codex", {
platform: "darwin",
arch: "arm64",
resolvePackageJson: (packageName, root) =>
packageName === "@openai/codex-darwin-arm64" &&
root === "/repo/extensions/codex/node_modules/@openai/codex"
? packageJsonPath
: undefined,
pathExists: (candidate) => candidate === expected,
}),
).toBe(expected);
});
it("reports the desktop bundle binary as its native artifact", () => {
expect(
resolveManagedCodexNativeCommand(MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND, {
platform: "darwin",
arch: "arm64",
}),
).toBe(MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND);
});
it("leaves explicit command overrides unchanged", async () => {
const explicitOptions = startOptions("config");
const pathExists = vi.fn(async () => false);
await expect(
resolveManagedCodexAppServerStartOptions(explicitOptions, {
platform: "darwin",
pathExists,
}),
).resolves.toBe(explicitOptions);
expect(pathExists).not.toHaveBeenCalled();
});
it("keeps the pinned package ahead of stale desktop bundles for ordinary turns", async () => {
const pluginRoot = path.join("/tmp", "openclaw", "extensions", "codex");
const paths = resolveManagedCodexAppServerPaths({ platform: "darwin", pluginRoot });
const pluginLocalCommand = managedCommandPath(pluginRoot, "darwin");
const pathExists = vi.fn(
async (filePath: string) =>
filePath === MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND ||
filePath === MACOS_DESKTOP_CODEX_APP_SERVER_COMMAND ||
filePath === pluginLocalCommand,
);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
platform: "darwin",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed"),
command: pluginLocalCommand,
commandSource: "resolved-managed",
managedFallbackCommandPaths: [
MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND,
MACOS_DESKTOP_CODEX_APP_SERVER_COMMAND,
],
});
expect(paths.commandPath).toBe(pluginLocalCommand);
expect(paths.candidateCommandPaths).toContain(MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND);
});
it("prefers the ChatGPT.app desktop bundle for Computer Use", async () => {
const pluginRoot = path.join("/tmp", "openclaw", "extensions", "codex");
const pluginLocalCommand = managedCommandPath(pluginRoot, "darwin");
const pathExists = vi.fn(
async (filePath: string) =>
filePath === MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND || filePath === pluginLocalCommand,
);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed", "desktop-first"), {
platform: "darwin",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed", "desktop-first"),
command: MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND,
commandSource: "resolved-managed",
managedFallbackCommandPaths: [pluginLocalCommand],
});
expect(
resolveManagedCodexAppServerPaths({
platform: "darwin",
pluginRoot,
managedCommandOrder: "desktop-first",
}).commandPath,
).toBe(MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND);
});
it("falls back to the legacy Codex.app desktop bundle when ChatGPT.app is absent", async () => {
const pluginRoot = path.join("/tmp", "openclaw", "extensions", "codex");
const pluginLocalCommand = managedCommandPath(pluginRoot, "darwin");
const pathExists = vi.fn(
async (filePath: string) =>
filePath === MACOS_DESKTOP_CODEX_APP_SERVER_COMMAND || filePath === pluginLocalCommand,
);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed", "desktop-first"), {
platform: "darwin",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed", "desktop-first"),
command: MACOS_DESKTOP_CODEX_APP_SERVER_COMMAND,
commandSource: "resolved-managed",
managedFallbackCommandPaths: [pluginLocalCommand],
});
});
it("falls back to the plugin-local binary when neither desktop bundle exists", async () => {
const pluginRoot = path.join("/tmp", "openclaw", "extensions", "codex");
const pluginLocalCommand = managedCommandPath(pluginRoot, "darwin");
const pathExists = vi.fn(async (filePath: string) => filePath === pluginLocalCommand);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed", "desktop-first"), {
platform: "darwin",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed", "desktop-first"),
command: pluginLocalCommand,
commandSource: "resolved-managed",
});
expect(pathExists).toHaveBeenCalledWith(MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND, "darwin");
expect(pathExists).toHaveBeenCalledWith(MACOS_DESKTOP_CODEX_APP_SERVER_COMMAND, "darwin");
});
it("resolves Windows Codex command shims", () => {
const pluginRoot = path.win32.join("C:\\", "OpenClaw", "dist", "extensions", "codex");
const paths = resolveManagedCodexAppServerPaths({ platform: "win32", pluginRoot });
expect(paths.commandPath.endsWith(path.win32.join("node_modules", ".bin", "codex.cmd"))).toBe(
true,
);
});
it("uses the package root when the resolver is bundled into a dist chunk", () => {
expect(testing.resolveDefaultCodexPluginRoot("/repo/openclaw/dist")).toBe("/repo/openclaw");
expect(testing.resolveDefaultCodexPluginRoot("/repo/openclaw/dist-runtime")).toBe(
"/repo/openclaw",
);
expect(
testing.resolveDefaultCodexPluginRoot("/repo/openclaw/extensions/codex/src/app-server"),
).toBe("/repo/openclaw/extensions/codex");
});
it("finds Codex in the package install root used by packaged plugins", async () => {
const installRoot = path.join("/tmp", "openclaw-plugin-package", "codex");
const pluginRoot = path.join(installRoot, "dist", "extensions", "codex");
const installedCommand = managedCommandPath(installRoot, "linux");
const pathExists = vi.fn(async (filePath: string) => filePath === installedCommand);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
platform: "linux",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed"),
command: installedCommand,
commandSource: "resolved-managed",
});
});
it("finds Codex bins hoisted into an isolated npm project root", async () => {
const projectRoot = path.join("/tmp", "state", "npm", "projects", "openclaw-codex-hash");
const pluginRoot = path.join(projectRoot, "node_modules", "@openclaw", "codex");
const installedCommand = managedCommandPath(projectRoot, "linux");
const pathExists = vi.fn(async (filePath: string) => filePath === installedCommand);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
platform: "linux",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed"),
command: installedCommand,
commandSource: "resolved-managed",
});
});
it("finds Windows Codex shims hoisted into an isolated npm project root", async () => {
const projectRoot = path.win32.join(
"C:\\",
"Users",
"test",
".openclaw",
"npm",
"projects",
"openclaw-codex-hash",
);
const pluginRoot = path.win32.join(projectRoot, "node_modules", "@openclaw", "codex");
const installedCommand = managedCommandPath(projectRoot, "win32");
const pathExists = vi.fn(async (filePath: string) => filePath === installedCommand);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
platform: "win32",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed"),
command: installedCommand,
commandSource: "resolved-managed",
});
});
it("falls back to the resolved Codex package bin when no command shim exists", async () => {
const installRoot = await mkdtemp(path.join(os.tmpdir(), "openclaw-codex-package-"));
const pluginRoot = path.join(installRoot, "dist", "extensions", "codex");
const packageRoot = path.join(installRoot, "node_modules", "@openai", "codex");
const packageBin = path.join(packageRoot, "bin", "codex.js");
await mkdir(path.dirname(packageBin), { recursive: true });
await writeFile(
path.join(packageRoot, "package.json"),
JSON.stringify({
name: "@openai/codex",
bin: {
codex: "bin/codex.js",
},
}),
);
await writeFile(packageBin, "#!/usr/bin/env node\n");
const resolvedPackageBin = await realpath(packageBin);
const pathExists = vi.fn(async (filePath: string) => filePath === resolvedPackageBin);
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
platform: "linux",
pluginRoot,
pathExists,
}),
).resolves.toEqual({
...startOptions("managed"),
command: resolvedPackageBin,
commandSource: "resolved-managed",
});
});
it("fails clearly when the managed Codex binary is missing", async () => {
await expect(
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
platform: "darwin",
pluginRoot: path.join("/tmp", "openclaw", "extensions", "codex"),
pathExists: vi.fn(async () => false),
}),
).rejects.toThrow("Managed Codex app-server binary was not found");
});
});