|
| 1 | +import { win32 } from 'node:path'; |
| 2 | +import { beforeAll, afterAll, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +const { execFileSyncMock, existsSyncMock, homedirMock } = vi.hoisted(() => ({ |
| 5 | + execFileSyncMock: vi.fn(), |
| 6 | + existsSyncMock: vi.fn(), |
| 7 | + homedirMock: vi.fn(() => 'home\junes') |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('node:child_process', async () => { |
| 11 | + const actual = await vi.importActual<typeof import('node:child_process')>('node:child_process'); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + execFileSync: execFileSyncMock |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +vi.mock('node:fs', async () => { |
| 19 | + const actual = await vi.importActual<typeof import('node:fs')>('node:fs'); |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + existsSync: existsSyncMock |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +vi.mock('node:os', async () => { |
| 27 | + const actual = await vi.importActual<typeof import('node:os')>('node:os'); |
| 28 | + return { |
| 29 | + ...actual, |
| 30 | + homedir: homedirMock |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform'); |
| 35 | +const homeDir = win32.join('home', 'junes'); |
| 36 | +const nodeRoot = win32.join('toolchains', 'nodejs'); |
| 37 | + |
| 38 | +function codexShimPath(): string { |
| 39 | + return win32.join(nodeRoot, 'codex.cmd'); |
| 40 | +} |
| 41 | + |
| 42 | +function nativeCodexPath(): string { |
| 43 | + return win32.join( |
| 44 | + nodeRoot, |
| 45 | + 'node_modules', |
| 46 | + '@openai', |
| 47 | + 'codex', |
| 48 | + 'node_modules', |
| 49 | + '@openai', |
| 50 | + 'codex-win32-x64', |
| 51 | + 'vendor', |
| 52 | + 'x86_64-pc-windows-msvc', |
| 53 | + 'bin', |
| 54 | + 'codex.exe' |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +function codexScriptPath(): string { |
| 59 | + return win32.join(nodeRoot, 'node_modules', '@openai', 'codex', 'bin', 'codex.js'); |
| 60 | +} |
| 61 | + |
| 62 | +function userCodexExePath(): string { |
| 63 | + return win32.join(homeDir, '.local', 'bin', 'codex.exe'); |
| 64 | +} |
| 65 | + |
| 66 | +function setPlatform(value: string) { |
| 67 | + Object.defineProperty(process, 'platform', { |
| 68 | + value, |
| 69 | + configurable: true |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +describe('resolveCodexCommand', () => { |
| 74 | + beforeAll(() => { |
| 75 | + if (!originalPlatformDescriptor?.configurable) { |
| 76 | + throw new Error('process.platform is not configurable in this runtime'); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + vi.clearAllMocks(); |
| 82 | + vi.resetModules(); |
| 83 | + homedirMock.mockReturnValue(homeDir); |
| 84 | + execFileSyncMock.mockImplementation(() => { |
| 85 | + throw new Error('not found'); |
| 86 | + }); |
| 87 | + existsSyncMock.mockReturnValue(false); |
| 88 | + }); |
| 89 | + |
| 90 | + afterAll(() => { |
| 91 | + if (originalPlatformDescriptor) { |
| 92 | + Object.defineProperty(process, 'platform', originalPlatformDescriptor); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it('resolves a Windows npm codex.cmd shim through the Codex launcher', async () => { |
| 97 | + setPlatform('win32'); |
| 98 | + const shim = codexShimPath(); |
| 99 | + const laterExe = userCodexExePath(); |
| 100 | + const executable = nativeCodexPath(); |
| 101 | + const script = codexScriptPath(); |
| 102 | + execFileSyncMock.mockImplementation((command: string, args: string[]) => { |
| 103 | + if (command === 'where.exe' && args[0] === 'codex') { |
| 104 | + return `${shim}\r\n${laterExe}\r\n`; |
| 105 | + } |
| 106 | + throw new Error('not found'); |
| 107 | + }); |
| 108 | + existsSyncMock.mockImplementation((candidate: string) => |
| 109 | + candidate === shim || candidate === laterExe || candidate === executable || candidate === script |
| 110 | + ); |
| 111 | + const { resolveCodexCommand } = await import('./codexExecutable'); |
| 112 | + |
| 113 | + expect(resolveCodexCommand()).toEqual({ |
| 114 | + command: 'node', |
| 115 | + args: [script] |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('continues to the next Windows PATH candidate when a shim has no launcher script', async () => { |
| 120 | + setPlatform('win32'); |
| 121 | + const shim = codexShimPath(); |
| 122 | + const executable = userCodexExePath(); |
| 123 | + execFileSyncMock.mockImplementation((command: string, args: string[]) => { |
| 124 | + if (command === 'where.exe' && args[0] === 'codex') { |
| 125 | + return `${shim}\r\n${executable}\r\n`; |
| 126 | + } |
| 127 | + throw new Error('not found'); |
| 128 | + }); |
| 129 | + existsSyncMock.mockImplementation((candidate: string) => candidate === shim || candidate === executable); |
| 130 | + const { resolveCodexCommand } = await import('./codexExecutable'); |
| 131 | + |
| 132 | + expect(resolveCodexCommand()).toEqual({ |
| 133 | + command: executable, |
| 134 | + args: [] |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('keeps a Windows codex.exe found first on PATH', async () => { |
| 139 | + setPlatform('win32'); |
| 140 | + const executable = userCodexExePath(); |
| 141 | + execFileSyncMock.mockImplementation((command: string, args: string[]) => { |
| 142 | + if (command === 'where.exe' && args[0] === 'codex') { |
| 143 | + return `${executable}\r\n`; |
| 144 | + } |
| 145 | + throw new Error('not found'); |
| 146 | + }); |
| 147 | + existsSyncMock.mockImplementation((candidate: string) => candidate === executable); |
| 148 | + const { resolveCodexCommand } = await import('./codexExecutable'); |
| 149 | + |
| 150 | + expect(resolveCodexCommand()).toEqual({ |
| 151 | + command: executable, |
| 152 | + args: [] |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('falls back to node plus codex.js when a Windows shim has no native exe', async () => { |
| 157 | + setPlatform('win32'); |
| 158 | + const shim = codexShimPath(); |
| 159 | + const script = codexScriptPath(); |
| 160 | + execFileSyncMock.mockImplementation((command: string, args: string[]) => { |
| 161 | + if (command === 'where.exe' && args[0] === 'codex') { |
| 162 | + return `${shim}\r\n`; |
| 163 | + } |
| 164 | + throw new Error('not found'); |
| 165 | + }); |
| 166 | + existsSyncMock.mockImplementation((candidate: string) => candidate === shim || candidate === script); |
| 167 | + const { resolveCodexCommand } = await import('./codexExecutable'); |
| 168 | + |
| 169 | + expect(resolveCodexCommand()).toEqual({ |
| 170 | + command: 'node', |
| 171 | + args: [script] |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it('uses the plain codex command outside Windows', async () => { |
| 176 | + setPlatform('linux'); |
| 177 | + const { resolveCodexCommand } = await import('./codexExecutable'); |
| 178 | + |
| 179 | + expect(resolveCodexCommand()).toEqual({ |
| 180 | + command: 'codex', |
| 181 | + args: [] |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments