forked from stablyai/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.test.ts
More file actions
315 lines (256 loc) · 11.4 KB
/
Copy pathcommand.test.ts
File metadata and controls
315 lines (256 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { chmodSync, mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { delimiter, dirname, join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import {
getVersionManagerBinPaths,
resolveClaudeCommand,
resolveCliCommands,
resolveCodexCommand
} from './command'
function makeExecutable(path: string): void {
mkdirSync(dirname(path), { recursive: true })
writeFileSync(path, '')
if (process.platform !== 'win32') {
chmodSync(path, 0o755)
}
}
function makeNonExecutableFile(path: string): void {
mkdirSync(dirname(path), { recursive: true })
writeFileSync(path, '')
if (process.platform !== 'win32') {
chmodSync(path, 0o644)
}
}
describe('resolveCodexCommand', () => {
afterEach(() => {
delete process.env.PATH
delete process.env.Path
})
it('prefers Codex already present on PATH', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const pathDir = join(root, 'bin')
const commandPath = join(pathDir, 'codex')
makeExecutable(commandPath)
expect(resolveCodexCommand({ platform: 'darwin', pathEnv: pathDir, homePath: root })).toBe(
commandPath
)
})
it.skipIf(process.platform === 'win32')(
'skips non-runnable PATH entries and keeps scanning',
() => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const badDir = join(root, 'bad-bin')
const goodDir = join(root, 'good-bin')
const badCommandPath = join(badDir, 'codex')
const goodCommandPath = join(goodDir, 'codex')
makeNonExecutableFile(badCommandPath)
makeExecutable(goodCommandPath)
expect(
resolveCodexCommand({
platform: 'linux',
pathEnv: [badDir, goodDir].join(delimiter),
homePath: root
})
).toBe(goodCommandPath)
}
)
it('skips PATH directories named like the command', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const badDir = join(root, 'bad-bin')
const goodDir = join(root, 'good-bin')
mkdirSync(join(badDir, 'codex'), { recursive: true })
const goodCommandPath = join(goodDir, 'codex')
makeExecutable(goodCommandPath)
expect(
resolveCodexCommand({
platform: 'linux',
pathEnv: [badDir, goodDir].join(delimiter),
homePath: root
})
).toBe(goodCommandPath)
})
it('falls back to the newest nvm-installed Codex when PATH misses it', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const v22Path = join(root, '.nvm', 'versions', 'node', 'v22.14.0', 'bin', 'codex')
const v24Path = join(root, '.nvm', 'versions', 'node', 'v24.13.0', 'bin', 'codex')
makeExecutable(v22Path)
makeExecutable(v24Path)
expect(resolveCodexCommand({ platform: 'darwin', pathEnv: '', homePath: root })).toBe(v24Path)
})
it('finds Codex in pnpm global bin on macOS', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const pnpmPath = join(root, 'Library', 'pnpm', 'codex')
makeExecutable(pnpmPath)
expect(resolveCodexCommand({ platform: 'darwin', pathEnv: '', homePath: root })).toBe(pnpmPath)
})
it('finds Codex in pnpm global bin on Linux', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const pnpmPath = join(root, '.local', 'share', 'pnpm', 'codex')
makeExecutable(pnpmPath)
expect(resolveCodexCommand({ platform: 'linux', pathEnv: '', homePath: root })).toBe(pnpmPath)
})
it('finds Codex in pnpm global bin on Windows', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const pnpmPath = join(root, 'AppData', 'Local', 'pnpm', 'codex.cmd')
makeExecutable(pnpmPath)
expect(resolveCodexCommand({ platform: 'win32', pathEnv: '', homePath: root })).toBe(pnpmPath)
})
it('finds Codex in yarn global bin on macOS', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const yarnPath = join(root, '.yarn', 'bin', 'codex')
makeExecutable(yarnPath)
expect(resolveCodexCommand({ platform: 'darwin', pathEnv: '', homePath: root })).toBe(yarnPath)
})
it('finds Codex in yarn global bin on Windows', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const yarnPath = join(root, 'AppData', 'Local', 'Yarn', 'bin', 'codex.cmd')
makeExecutable(yarnPath)
expect(resolveCodexCommand({ platform: 'win32', pathEnv: '', homePath: root })).toBe(yarnPath)
})
it('finds Codex in bun global bin', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const bunPath = join(root, '.bun', 'bin', 'codex')
makeExecutable(bunPath)
expect(resolveCodexCommand({ platform: 'linux', pathEnv: '', homePath: root })).toBe(bunPath)
})
it('finds Codex in bun global bin on Windows', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const bunPath = join(root, '.bun', 'bin', 'codex.exe')
makeExecutable(bunPath)
expect(resolveCodexCommand({ platform: 'win32', pathEnv: '', homePath: root })).toBe(bunPath)
})
it('finds Codex in mise shims directory', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
const misePath = join(root, '.local', 'share', 'mise', 'shims', 'codex')
makeExecutable(misePath)
expect(resolveCodexCommand({ platform: 'linux', pathEnv: '', homePath: root })).toBe(misePath)
})
it('returns the bare command when no filesystem candidate exists', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-codex-command-'))
expect(resolveCodexCommand({ platform: 'linux', pathEnv: '', homePath: root })).toBe('codex')
})
})
describe('resolveClaudeCommand', () => {
afterEach(() => {
delete process.env.PATH
delete process.env.Path
})
it('prefers claude already present on PATH', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
const pathDir = join(root, 'bin')
const commandPath = join(pathDir, 'claude')
makeExecutable(commandPath)
expect(resolveClaudeCommand({ platform: 'darwin', pathEnv: pathDir, homePath: root })).toBe(
commandPath
)
})
it('falls back to the newest nvm-installed claude when PATH misses it', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
const v22Path = join(root, '.nvm', 'versions', 'node', 'v22.14.0', 'bin', 'claude')
const v24Path = join(root, '.nvm', 'versions', 'node', 'v24.13.0', 'bin', 'claude')
makeExecutable(v22Path)
makeExecutable(v24Path)
expect(resolveClaudeCommand({ platform: 'darwin', pathEnv: '', homePath: root })).toBe(v24Path)
})
it('finds claude in pnpm global bin on macOS', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
const pnpmPath = join(root, 'Library', 'pnpm', 'claude')
makeExecutable(pnpmPath)
expect(resolveClaudeCommand({ platform: 'darwin', pathEnv: '', homePath: root })).toBe(pnpmPath)
})
it('finds claude in yarn global bin', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
const yarnPath = join(root, '.yarn', 'bin', 'claude')
makeExecutable(yarnPath)
expect(resolveClaudeCommand({ platform: 'linux', pathEnv: '', homePath: root })).toBe(yarnPath)
})
it('finds claude in bun global bin', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
const bunPath = join(root, '.bun', 'bin', 'claude')
makeExecutable(bunPath)
expect(resolveClaudeCommand({ platform: 'darwin', pathEnv: '', homePath: root })).toBe(bunPath)
})
it('finds native Windows claude.exe in user-local bin', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
const nativePath = join(root, '.local', 'bin', 'claude.exe')
makeExecutable(nativePath)
expect(resolveClaudeCommand({ platform: 'win32', pathEnv: '', homePath: root })).toBe(
nativePath
)
})
it('returns the bare command when no filesystem candidate exists', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-claude-command-'))
expect(resolveClaudeCommand({ platform: 'linux', pathEnv: '', homePath: root })).toBe('claude')
})
})
describe('resolveCliCommands', () => {
afterEach(() => {
delete process.env.PATH
delete process.env.Path
})
it('resolves a batch from PATH and install directories', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-cli-commands-'))
const pathDir = join(root, 'bin')
const pathClaude = join(pathDir, 'claude')
const nvmCodex = join(root, '.nvm', 'versions', 'node', 'v24.13.0', 'bin', 'codex')
const pnpmOpencode = join(root, 'Library', 'pnpm', 'opencode')
makeExecutable(pathClaude)
makeExecutable(nvmCodex)
makeExecutable(pnpmOpencode)
const resolved = resolveCliCommands(['claude', 'codex', 'opencode', 'missing'], {
platform: 'darwin',
pathEnv: pathDir,
homePath: root
})
expect(resolved.get('claude')).toBe(pathClaude)
expect(resolved.get('codex')).toBe(nvmCodex)
expect(resolved.get('opencode')).toBe(pnpmOpencode)
expect(resolved.get('missing')).toBe('missing')
})
it('deduplicates command names in the returned map', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-cli-commands-'))
const pathDir = join(root, 'bin')
const pathClaude = join(pathDir, 'claude')
makeExecutable(pathClaude)
const resolved = resolveCliCommands(['claude', 'claude'], {
platform: 'linux',
pathEnv: pathDir,
homePath: root
})
expect([...resolved.keys()]).toEqual(['claude'])
expect(resolved.get('claude')).toBe(pathClaude)
})
})
describe('getVersionManagerBinPaths', () => {
it('includes volta, asdf, fnm, mise, pnpm, yarn, and bun directories', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-paths-'))
const paths = getVersionManagerBinPaths({ platform: 'darwin', pathEnv: '', homePath: root })
expect(paths).toContain(join(root, '.volta', 'bin'))
expect(paths).toContain(join(root, '.asdf', 'shims'))
expect(paths).toContain(join(root, '.fnm', 'aliases', 'default', 'bin'))
expect(paths).toContain(join(root, '.local', 'share', 'mise', 'shims'))
expect(paths).toContain(join(root, 'Library', 'pnpm'))
expect(paths).toContain(join(root, '.yarn', 'bin'))
expect(paths).toContain(join(root, '.bun', 'bin'))
})
it('includes nvm bin dir when node versions exist', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-paths-'))
const nodeBin = join(root, '.nvm', 'versions', 'node', 'v22.14.0', 'bin', 'node')
makeExecutable(nodeBin)
const paths = getVersionManagerBinPaths({ platform: 'darwin', pathEnv: '', homePath: root })
expect(paths).toContain(join(root, '.nvm', 'versions', 'node', 'v22.14.0', 'bin'))
})
it('uses platform-specific pnpm path on Linux', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-paths-'))
const paths = getVersionManagerBinPaths({ platform: 'linux', pathEnv: '', homePath: root })
expect(paths).toContain(join(root, '.local', 'share', 'pnpm'))
expect(paths).not.toContain(join(root, 'Library', 'pnpm'))
})
it('includes Windows user-local bin for native CLI installers', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-paths-'))
const paths = getVersionManagerBinPaths({ platform: 'win32', pathEnv: '', homePath: root })
expect(paths).toContain(join(root, '.local', 'bin'))
expect(paths).toContain(join(root, 'AppData', 'Roaming', 'npm'))
})
})