|
| 1 | +// Codex plugins as one-click stdio presets. |
| 2 | +// |
| 3 | +// The server-side scanner reads `$CODEX_HOME` and reports locally installed |
| 4 | +// OpenAI Codex plugins with stdio MCP servers: the three curated ones the |
| 5 | +// shared "Codex Computer Use" client binary implements (Apple Messages, |
| 6 | +// Computer Use, Computer History) plus anything in the plugin cache with a |
| 7 | +// local-command `.mcp.json`. This scenario boots the real local server with |
| 8 | +// `CODEX_HOME` pointed at a fixture layout whose "client binary" is a wrapper |
| 9 | +// around the e2e stdio MCP fixture, and drives the same API the add-form's |
| 10 | +// Codex-plugins section uses: |
| 11 | +// |
| 12 | +// 1. `mcp.listCodexPlugins` reports the curated entries and the scanned |
| 13 | +// cache entry, all available. |
| 14 | +// 2. Adding an entry with its reported recipe (the one-click card path) |
| 15 | +// registers the integration, auto-connects, and detects its tools — |
| 16 | +// including `saw_codex_home`, which the fixture advertises only when |
| 17 | +// CODEX_HOME actually reached the spawned subprocess. |
| 18 | +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 19 | +import { tmpdir } from "node:os"; |
| 20 | +import { join } from "node:path"; |
| 21 | +import { fileURLToPath } from "node:url"; |
| 22 | + |
| 23 | +import { expect } from "@effect/vitest"; |
| 24 | +import { Effect } from "effect"; |
| 25 | +import { HttpApiClient } from "effect/unstable/httpapi"; |
| 26 | +import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http"; |
| 27 | +import { composePluginApi } from "@executor-js/api/server"; |
| 28 | +import { mcpHttpPlugin } from "@executor-js/plugin-mcp/api"; |
| 29 | + |
| 30 | +import { scenario } from "../src/scenario"; |
| 31 | +import { Cli, RunDir } from "../src/services"; |
| 32 | +import { withLocalServer } from "./local-server"; |
| 33 | + |
| 34 | +const api = composePluginApi([mcpHttpPlugin()] as const); |
| 35 | + |
| 36 | +const FIXTURE = fileURLToPath(new URL("./fixtures/stdio-mcp-server.mjs", import.meta.url)); |
| 37 | +const CHROME_CLIENT_RELATIVE = join( |
| 38 | + "plugins", |
| 39 | + "cache", |
| 40 | + "openai-bundled", |
| 41 | + "chrome", |
| 42 | + "latest", |
| 43 | + "scripts", |
| 44 | + "browser-client.mjs", |
| 45 | +); |
| 46 | +const APP_SERVER_FIXTURE = fileURLToPath( |
| 47 | + new URL("./fixtures/codex-app-server.mjs", import.meta.url), |
| 48 | +); |
| 49 | + |
| 50 | +/** A fixture CODEX_HOME: the curated install markers (the Computer Use app |
| 51 | + * and a `codex` CLI whose `app-server` is the fake app-server fixture) and |
| 52 | + * one cached plugin wrapping the self-contained stdio MCP fixture. */ |
| 53 | +const makeCodexHome = (): string => { |
| 54 | + const home = mkdtempSync(join(tmpdir(), "codex-home-e2e-")); |
| 55 | + const wrapper = `#!/bin/sh\nexec node "${FIXTURE}" "$@"\n`; |
| 56 | + |
| 57 | + // The Computer Use app is the plugin-installed marker; the bridge never |
| 58 | + // spawns it, so an empty executable is enough. |
| 59 | + const clientDir = join( |
| 60 | + home, |
| 61 | + "computer-use", |
| 62 | + "Codex Computer Use.app", |
| 63 | + "Contents", |
| 64 | + "SharedSupport", |
| 65 | + "SkyComputerUseClient.app", |
| 66 | + "Contents", |
| 67 | + "MacOS", |
| 68 | + ); |
| 69 | + mkdirSync(clientDir, { recursive: true }); |
| 70 | + writeFileSync(join(clientDir, "SkyComputerUseClient"), wrapper, { mode: 0o755 }); |
| 71 | + |
| 72 | + // The `codex` CLI the curated recipes spawn — resolved through PATH, so |
| 73 | + // the scenario prepends this bin dir to the server's PATH. |
| 74 | + mkdirSync(join(home, "bin"), { recursive: true }); |
| 75 | + writeFileSync(join(home, "bin", "codex"), `#!/bin/sh\nexec node "${APP_SERVER_FIXTURE}" "$@"\n`, { |
| 76 | + mode: 0o755, |
| 77 | + }); |
| 78 | + |
| 79 | + // Chrome's bundled browser client, behind the `latest` symlink Codex keeps. |
| 80 | + const chromeClient = join(home, CHROME_CLIENT_RELATIVE); |
| 81 | + mkdirSync(join(chromeClient, ".."), { recursive: true }); |
| 82 | + writeFileSync(chromeClient, "export const setupBrowserRuntime = async () => ({});\n"); |
| 83 | + |
| 84 | + const versionDir = join(home, "plugins", "cache", "personal", "echo-suite", "1.0.2"); |
| 85 | + mkdirSync(join(versionDir, ".codex-plugin"), { recursive: true }); |
| 86 | + mkdirSync(join(versionDir, "bin"), { recursive: true }); |
| 87 | + writeFileSync( |
| 88 | + join(versionDir, ".codex-plugin", "plugin.json"), |
| 89 | + JSON.stringify({ |
| 90 | + name: "echo-suite", |
| 91 | + mcpServers: "./.mcp.json", |
| 92 | + interface: { displayName: "Echo Suite", shortDescription: "Echo tools for e2e" }, |
| 93 | + }), |
| 94 | + ); |
| 95 | + writeFileSync( |
| 96 | + join(versionDir, ".mcp.json"), |
| 97 | + JSON.stringify({ mcpServers: { "echo-suite": { command: "./bin/run", cwd: "." } } }), |
| 98 | + ); |
| 99 | + writeFileSync(join(versionDir, "bin", "run"), wrapper, { mode: 0o755 }); |
| 100 | + |
| 101 | + return home; |
| 102 | +}; |
| 103 | + |
| 104 | +scenario( |
| 105 | + "Local · Codex plugins are discovered from CODEX_HOME and add as one-click stdio presets", |
| 106 | + // Above the 240s boot-URL wait in `withLocalServer` for the same reason as |
| 107 | + // stdio-mcp.test.ts: a cold vite boot must fail with the harness's |
| 108 | + // diagnostic, not vitest's generic timeout. |
| 109 | + { timeout: 300_000 }, |
| 110 | + Effect.gen(function* () { |
| 111 | + const cli = yield* Cli; |
| 112 | + const runDir = yield* RunDir; |
| 113 | + const codexHome = makeCodexHome(); |
| 114 | + |
| 115 | + yield* withLocalServer( |
| 116 | + cli, |
| 117 | + runDir, |
| 118 | + (server) => |
| 119 | + Effect.gen(function* () { |
| 120 | + const client = yield* HttpApiClient.make(api, { |
| 121 | + baseUrl: new URL("/api", server.origin).toString(), |
| 122 | + transformClient: HttpClient.mapRequest((request) => |
| 123 | + HttpClientRequest.setHeader(request, "authorization", `Bearer ${server.token}`), |
| 124 | + ), |
| 125 | + }).pipe(Effect.provide(FetchHttpClient.layer)); |
| 126 | + |
| 127 | + // The scanner reports the curated plugins and the cached one, all |
| 128 | + // available (the fixture home has every binary in place). |
| 129 | + const { plugins } = yield* client.mcp.listCodexPlugins(); |
| 130 | + const byId = new Map(plugins.map((plugin) => [plugin.id, plugin])); |
| 131 | + expect([...byId.keys()].sort(), "curated + scanned entries are reported").toEqual([ |
| 132 | + "codex-chrome", |
| 133 | + "codex-computer-history", |
| 134 | + "codex-computer-use", |
| 135 | + "codex-echo-suite", |
| 136 | + "codex-messages", |
| 137 | + "codex-openai-docs", |
| 138 | + ]); |
| 139 | + for (const plugin of plugins) { |
| 140 | + expect(plugin.available, `${plugin.id} is available`).toBe(true); |
| 141 | + expect(plugin.env, `${plugin.id} declares CODEX_HOME`).toEqual({ |
| 142 | + CODEX_HOME: codexHome, |
| 143 | + }); |
| 144 | + } |
| 145 | + // Curated entries carry the app-server bridge recipe: `codex |
| 146 | + // app-server` plus the server name the bridge calls tools on. |
| 147 | + const messages = byId.get("codex-messages"); |
| 148 | + expect(messages?.command.endsWith("codex"), "curated entries spawn the codex CLI").toBe( |
| 149 | + true, |
| 150 | + ); |
| 151 | + expect(messages?.args, "curated entries run the app-server").toEqual(["app-server"]); |
| 152 | + expect(messages?.appServer, "curated entries name their Codex server").toEqual({ |
| 153 | + server: "messages", |
| 154 | + }); |
| 155 | + // Computer Use and Chrome have no server of their own: both are |
| 156 | + // projected onto `node_repl`, and Chrome carries the client module |
| 157 | + // its surface imports, resolved through the `latest` symlink. |
| 158 | + expect(byId.get("codex-computer-use")?.appServer).toEqual({ |
| 159 | + server: "node_repl", |
| 160 | + surface: "sky", |
| 161 | + }); |
| 162 | + expect(byId.get("codex-chrome")?.appServer).toEqual({ |
| 163 | + server: "node_repl", |
| 164 | + surface: "browser", |
| 165 | + modulePath: join(codexHome, CHROME_CLIENT_RELATIVE), |
| 166 | + }); |
| 167 | + |
| 168 | + // Add two entries exactly as the add-form's Codex-plugins card does: |
| 169 | + // the reported recipe, verbatim. |
| 170 | + for (const id of ["codex-messages", "codex-echo-suite"] as const) { |
| 171 | + const plugin = byId.get(id)!; |
| 172 | + yield* client.mcp.addServer({ |
| 173 | + payload: { |
| 174 | + transport: "stdio", |
| 175 | + name: plugin.name, |
| 176 | + slug: plugin.slug, |
| 177 | + description: plugin.summary, |
| 178 | + command: plugin.command, |
| 179 | + args: [...plugin.args], |
| 180 | + ...(plugin.cwd === undefined ? {} : { cwd: plugin.cwd }), |
| 181 | + ...(plugin.env === undefined ? {} : { env: { ...plugin.env } }), |
| 182 | + ...(plugin.appServer === undefined |
| 183 | + ? {} |
| 184 | + : { appServer: { server: plugin.appServer.server } }), |
| 185 | + }, |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + const integrations = yield* client.integrations.list(); |
| 190 | + const slugs = integrations.map((integration) => String(integration.slug)); |
| 191 | + expect(slugs, "both plugins registered").toEqual( |
| 192 | + expect.arrayContaining(["codex_messages", "codex_echo_suite"]), |
| 193 | + ); |
| 194 | + |
| 195 | + // One-click means connected: the env values auto-create the default |
| 196 | + // connection, so tools are discovered with no further step. |
| 197 | + for (const slug of ["codex_messages", "codex_echo_suite"]) { |
| 198 | + const connections = yield* client.connections.list({ query: { integration: slug } }); |
| 199 | + expect( |
| 200 | + connections.map((connection) => String(connection.name)), |
| 201 | + `${slug} auto-connected`, |
| 202 | + ).toContain("default"); |
| 203 | + |
| 204 | + const tools = yield* client.tools.list({ query: { integration: slug } }); |
| 205 | + const names = tools.map((tool) => tool.name); |
| 206 | + expect(names, `${slug} tools are detected`).toContain("echo_tool"); |
| 207 | + expect( |
| 208 | + names, |
| 209 | + `CODEX_HOME reached ${slug}'s spawned subprocess (saw_codex_home is gated on it)`, |
| 210 | + ).toContain("saw_codex_home"); |
| 211 | + } |
| 212 | + }), |
| 213 | + { |
| 214 | + env: { |
| 215 | + CODEX_HOME: codexHome, |
| 216 | + // The scanner resolves the `codex` CLI through the server's PATH. |
| 217 | + PATH: `${join(codexHome, "bin")}:${process.env["PATH"] ?? ""}`, |
| 218 | + }, |
| 219 | + }, |
| 220 | + ); |
| 221 | + }), |
| 222 | +); |
0 commit comments