@@ -24,6 +24,16 @@ const tmpPrefix = join(tmpdir(), "clawsweeper-codex-process-test-");
2424test ( "Codex process resolves command overrides and escaped Windows launchers" , ( ) => {
2525 assert . equal ( codexProcessCommand ( { } ) , "codex" ) ;
2626 assert . equal ( codexProcessCommand ( { CODEX_BIN : " custom-codex " } ) , "custom-codex" ) ;
27+ assert . equal (
28+ codexProcessCommand (
29+ {
30+ CODEX_BIN : "custom-codex" ,
31+ CLAWSWEEPER_PREFER_WINDOWS_CODEX_APP : "1" ,
32+ } ,
33+ "win32" ,
34+ ) ,
35+ "custom-codex" ,
36+ ) ;
2737 assert . deepEqual ( codexSpawnInvocation ( [ "exec" , "-" ] , { CODEX_BIN : "codex" } , "linux" ) , {
2838 command : "codex" ,
2939 args : [ "exec" , "-" ] ,
@@ -45,6 +55,7 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
4555 const root = mkdtempSync ( tmpPrefix ) ;
4656 const binDir = join ( root , "bin" ) ;
4757 mkdirSync ( binDir ) ;
58+ writeFileSync ( join ( binDir , "codex" ) , "#!/bin/sh\r\n" ) ;
4859 writeFileSync ( join ( binDir , "codex.cmd" ) , "@echo off\r\n" ) ;
4960 try {
5061 const invocation = codexSpawnInvocation (
@@ -74,6 +85,74 @@ test("Codex process resolves command overrides and escaped Windows launchers", (
7485 ) ;
7586} ) ;
7687
88+ test ( "Windows Codex selection prefers PATH and falls back to the Desktop app" , ( ) => {
89+ const root = mkdtempSync ( tmpPrefix ) ;
90+ const pathBin = join ( root , "path-bin" ) ;
91+ const unsupportedBin = join ( root , "unsupported-bin" ) ;
92+ const fallbackPathBin = join ( root , "fallback-path-bin" ) ;
93+ const localAppData = join ( root , "local-app-data" ) ;
94+ const desktopCodex = join ( localAppData , "OpenAI" , "Codex" , "bin" , "codex.exe" ) ;
95+ mkdirSync ( pathBin ) ;
96+ mkdirSync ( unsupportedBin ) ;
97+ mkdirSync ( fallbackPathBin ) ;
98+ mkdirSync ( join ( localAppData , "OpenAI" , "Codex" , "bin" ) , { recursive : true } ) ;
99+ writeFileSync ( join ( pathBin , "codex.exe" ) , "" ) ;
100+ writeFileSync ( join ( unsupportedBin , "codex" ) , "#!/bin/sh\r\n" ) ;
101+ writeFileSync ( join ( fallbackPathBin , "codex.exe" ) , "" ) ;
102+ writeFileSync ( desktopCodex , "" ) ;
103+ try {
104+ const pathEnv = {
105+ LOCALAPPDATA : localAppData ,
106+ Path : pathBin ,
107+ PATHEXT : ".EXE" ,
108+ SystemRoot : String . raw `C:\Windows` ,
109+ } ;
110+ const pathInvocation = codexSpawnInvocation ( [ "exec" ] , pathEnv , "win32" , root ) ;
111+ assert . equal ( pathInvocation . command , join ( pathBin , "codex.exe" ) ) ;
112+
113+ const desktopEnv = { ...pathEnv , Path : "" } ;
114+ assert . equal ( codexProcessCommand ( desktopEnv , "win32" , root ) , desktopCodex ) ;
115+ const desktopInvocation = codexSpawnInvocation ( [ "exec" ] , desktopEnv , "win32" , root ) ;
116+ assert . equal ( desktopInvocation . command , desktopCodex ) ;
117+
118+ const unsupportedEnv = { ...pathEnv , Path : unsupportedBin } ;
119+ assert . equal ( codexProcessCommand ( unsupportedEnv , "win32" , root ) , desktopCodex ) ;
120+ const unsupportedInvocation = codexSpawnInvocation ( [ "exec" ] , unsupportedEnv , "win32" , root ) ;
121+ assert . equal ( unsupportedInvocation . command , desktopCodex ) ;
122+
123+ const laterPathEnv = { ...pathEnv , Path : `${ unsupportedBin } ${ delimiter } ${ fallbackPathBin } ` } ;
124+ const laterPathInvocation = codexSpawnInvocation ( [ "exec" ] , laterPathEnv , "win32" , root ) ;
125+ assert . equal ( laterPathInvocation . command , join ( fallbackPathBin , "codex.exe" ) ) ;
126+ } finally {
127+ rmSync ( root , { recursive : true , force : true } ) ;
128+ }
129+ } ) ;
130+
131+ test ( "Windows Codex selection resolves relative PATH entries from the launch cwd" , ( ) => {
132+ const root = mkdtempSync ( tmpPrefix ) ;
133+ const launcherDir = join ( root , "launcher" ) ;
134+ const targetDir = join ( root , "target" ) ;
135+ const localAppData = join ( root , "local-app-data" ) ;
136+ const desktopCodex = join ( localAppData , "OpenAI" , "Codex" , "bin" , "codex.exe" ) ;
137+ mkdirSync ( launcherDir ) ;
138+ mkdirSync ( join ( targetDir , "bin" ) , { recursive : true } ) ;
139+ mkdirSync ( join ( localAppData , "OpenAI" , "Codex" , "bin" ) , { recursive : true } ) ;
140+ writeFileSync ( join ( targetDir , "bin" , "codex.exe" ) , "" ) ;
141+ writeFileSync ( desktopCodex , "" ) ;
142+ try {
143+ const env = {
144+ LOCALAPPDATA : localAppData ,
145+ Path : "bin" ,
146+ PATHEXT : ".EXE" ,
147+ SystemRoot : String . raw `C:\Windows` ,
148+ } ;
149+ assert . equal ( codexProcessCommand ( env , "win32" , launcherDir ) , desktopCodex ) ;
150+ assert . equal ( codexProcessCommand ( env , "win32" , targetDir ) , "codex" ) ;
151+ } finally {
152+ rmSync ( root , { recursive : true , force : true } ) ;
153+ }
154+ } ) ;
155+
77156test ( "Codex process resolves extensionless Windows node shebang shims" , ( ) => {
78157 const root = mkdtempSync ( tmpPrefix ) ;
79158 const binDir = join ( root , "bin" ) ;
0 commit comments