22import { mkdir , mkdtemp , realpath , writeFile } from "node:fs/promises" ;
33import os from "node:os" ;
44import path from "node:path" ;
5- import { describe , expect , it , vi } from "vitest" ;
5+ import { afterEach , describe , expect , it , vi } from "vitest" ;
66import type { CodexAppServerStartOptions } from "./config.js" ;
77import {
88 resolveManagedCodexAppServerStartOptions ,
99 resolveManagedCodexNativeCommand ,
10+ setManagedCodexPluginRoot ,
1011} from "./managed-binary.js" ;
1112
1213function startOptions (
@@ -33,6 +34,8 @@ const MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND =
3334 "/Applications/ChatGPT.app/Contents/Resources/codex" ;
3435
3536describe ( "managed Codex app-server binary" , ( ) => {
37+ afterEach ( ( ) => setManagedCodexPluginRoot ( undefined ) ) ;
38+
3639 it ( "resolves the platform-native artifact behind the managed npm launcher" , ( ) => {
3740 const packageJsonPath =
3841 "/repo/extensions/codex/node_modules/@openai/codex-darwin-arm64/package.json" ;
@@ -62,7 +65,7 @@ describe("managed Codex app-server binary", () => {
6265 ) . toBe ( MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND ) ;
6366 } ) ;
6467
65- it ( "leaves explicit command overrides unchanged" , async ( ) => {
68+ it ( "leaves explicit command overrides unchanged without probing managed paths " , async ( ) => {
6669 const explicitOptions = startOptions ( "config" ) ;
6770 const pathExists = vi . fn ( async ( ) => false ) ;
6871
@@ -146,7 +149,7 @@ describe("managed Codex app-server binary", () => {
146149 } ) ;
147150 } ) ;
148151
149- it ( "falls back to the plugin-local binary when neither desktop bundle exists" , async ( ) => {
152+ it ( "falls back to the source plugin-local binary when neither desktop bundle exists" , async ( ) => {
150153 const pluginRoot = path . join ( "/tmp" , "openclaw" , "extensions" , "codex" ) ;
151154 const pluginLocalCommand = managedCommandPath ( pluginRoot , "darwin" ) ;
152155 const pathExists = vi . fn ( async ( filePath : string ) => filePath === pluginLocalCommand ) ;
@@ -185,6 +188,50 @@ describe("managed Codex app-server binary", () => {
185188 } ) ;
186189 } ) ;
187190
191+ it ( "prefers the bundled plugin binary over a stale hoisted package binary" , async ( ) => {
192+ const installRoot = path . join ( "/tmp" , "openclaw-package" ) ;
193+ const packageRoot = path . join ( installRoot , "node_modules" , "openclaw" ) ;
194+ const bundledPluginRoot = path . join ( packageRoot , "dist" , "extensions" , "codex" ) ;
195+ const bundledCommand = managedCommandPath ( bundledPluginRoot , "linux" ) ;
196+ const hoistedCommand = managedCommandPath ( installRoot , "linux" ) ;
197+ const pathExists = vi . fn (
198+ async ( filePath : string ) => filePath === bundledCommand || filePath === hoistedCommand ,
199+ ) ;
200+ setManagedCodexPluginRoot ( bundledPluginRoot ) ;
201+
202+ await expect (
203+ resolveManagedCodexAppServerStartOptions ( startOptions ( "managed" ) , {
204+ platform : "linux" ,
205+ pathExists,
206+ } ) ,
207+ ) . resolves . toEqual ( {
208+ ...startOptions ( "managed" ) ,
209+ command : bundledCommand ,
210+ commandSource : "resolved-managed" ,
211+ managedFallbackCommandPaths : [ hoistedCommand ] ,
212+ } ) ;
213+ } ) ;
214+
215+ it ( "falls back to the hoisted package when the bundled plugin binary is absent" , async ( ) => {
216+ const installRoot = path . join ( "/tmp" , "openclaw-package" ) ;
217+ const packageRoot = path . join ( installRoot , "node_modules" , "openclaw" ) ;
218+ const bundledPluginRoot = path . join ( packageRoot , "dist" , "extensions" , "codex" ) ;
219+ const hoistedCommand = managedCommandPath ( installRoot , "linux" ) ;
220+ const pathExists = vi . fn ( async ( filePath : string ) => filePath === hoistedCommand ) ;
221+ setManagedCodexPluginRoot ( bundledPluginRoot ) ;
222+
223+ await expect (
224+ resolveManagedCodexAppServerStartOptions ( startOptions ( "managed" ) , {
225+ platform : "linux" ,
226+ pathExists,
227+ } ) ,
228+ ) . resolves . toEqual ( {
229+ ...startOptions ( "managed" ) ,
230+ command : hoistedCommand ,
231+ commandSource : "resolved-managed" ,
232+ } ) ;
233+ } ) ;
234+
188235 it ( "finds Codex bins hoisted into an isolated npm project root" , async ( ) => {
189236 const projectRoot = path . join ( "/tmp" , "state" , "npm" , "projects" , "openclaw-codex-hash" ) ;
190237 const pluginRoot = path . join ( projectRoot , "node_modules" , "@openclaw" , "codex" ) ;
@@ -204,7 +251,7 @@ describe("managed Codex app-server binary", () => {
204251 } ) ;
205252 } ) ;
206253
207- it ( "finds Windows Codex shims hoisted into an isolated npm project root" , async ( ) => {
254+ it ( "finds a Windows codex.cmd shim in an isolated npm root using win32 paths " , async ( ) => {
208255 const projectRoot = path . win32 . join (
209256 "C:\\" ,
210257 "Users" ,
0 commit comments