Files
orca/src/shared/setup-runner-command.test.ts
T
8c5371ebad fix(worktrees): respect Windows shell for setup runners (#6967)
* Honor configured shells during worktree setup

* Align setup launch paths with selected Windows shells

* Carry setup shell selection through deferred launches

* Prove Windows setup shell routing at its real adapters

* Ground remote PowerShell proof in the real writer

* Preserve Git Bash across deferred setup launches

* Harden Windows setup runner shell selection

- Resolve remote PowerShell binary without local pwsh probe: for SSH/remote
  Windows worktrees, isPwshAvailable() reflects only the LOCAL host, so an
  'auto' implementation could route the remote runner to a pwsh.exe the remote
  lacks. Add resolveSetupRunnerShell(..., { probeLocalPwsh: false }) so remote
  auto keeps the always-present powershell.exe; explicit pwsh.exe still honored.
- Preserve native exit codes in the PowerShell runner by checking
  $LASTEXITCODE before $?, so a failing native command surfaces its real code
  instead of a generic exit 1; $? still catches cmdlet soft-failures.
- Write the PowerShell runner with a UTF-8 BOM so Windows PowerShell 5.1 (the
  new default powershell.exe) reads it as UTF-8 instead of ANSI, preventing
  non-ASCII setup-script corruption.
- Add unit tests for the remote-probe behavior.

* Restore setup-shell scope narrowing over the rebase

The force-pushed rebase dropped five review-fix commits that were already
on this branch; this reapplies their combined effect on top of the new
base and the hardening commit:

- Keep SSH setup shell selection remote-owned (no local terminalWindowsShell
  or pwsh routing for remote hosts; supersedes the probeLocalPwsh guard)
- Preserve cmd setup compatibility outside POSIX shells (no .ps1 runner
  family, so the BOM/exit-code hardening is no longer applicable)
- Route WSL setup runners from the project runtime
- Avoid blocking PowerShell probes during setup creation
- Correct SSH and WSL background setup fixtures

* Satisfy the changed-code gates for the setup-shell runner

- createWorktreeRunnerScript took 7 positional parameters, tripping the
  changed-code max-params gate; move it to a single options object.
- hooks-runner.test.ts deep-equals the createSetupRunnerScript result, so
  assert the cmd shell now returned for native Windows worktrees.

* Carry the setup launch shell through observed and issue runners

- buildObservedSetupCommand takes the runner's launch shell so WSL-routed
  Windows-drive setup replays use /mnt/c instead of Git Bash /c
- resolveSetupRunnerShell gates the posix runner on the same Git Bash
  resolution the PTY uses, so a missing or non-MSYS bash keeps the cmd runner
- issue-command runners carry their launch shell, and the renderer passes it
  when building the queued command
- treat a bare `bash` shell setting as POSIX like `bash.exe`

Co-authored-by: Orca <help@stably.ai>

* fix(worktrees): close counsel P1 gaps for Windows setup shells

Route windowless/headless creates through the shell-aware setup runner when a
PTY controller is available, existence-check explicit Git Bash paths before
committing to .sh runners, thread the resolved shell into issue-command
runners, and document the intentional Git Bash interpreter flip with a narrow
scope table.

* Convert setup env to MSYS form and harden the bare cmd runner launch

C3: a Git Bash setup runner now receives ORCA_*/CONDUCTOR_*/GHOSTX_* path
values in /c/... form, matching the runner path and the shell's own HOME/PWD.
C5: extension-less `bash` resolves to Git Bash everywhere, matching how
resolveWindowsShellStartupFamily already classifies it.
C7: runner paths carrying characters that cannot be quoted on a cmd command
line launch through a delayed-expansion PowerShell shim instead, and the batch
runner disables inherited delayed expansion so `!` in setup lines survives.

Co-authored-by: Orca <help@stably.ai>

* docs: note MSYS ORCA_* paths and bare bash Git Bash resolution

Keep the setup-shell release note aligned with C3 env conversion and C5 bare
bash resolution so the published claim matches runtime behavior.

* revert: drop windows-setup-shell doc allowlist and AGENTS link

Keep the counsel P1/P2 product fixes without expanding the docs allowlist
or AGENTS.md guidance surface.

* fix(plugins): contain Parcel unsubscribe rejections under Vitest

Dev plugin watchers fire-and-forget unsubscribe, and in-process Parcel
can reject when temp watch roots are already deleted. Catch those
rejections so they cannot fail the suite as unhandled errors.

* fix(plugins): keep in-process unsubscribe rejection surface

Swallowing Parcel unsubscribe errors broke mocked unsubscribe tests
that return non-Promises and expect rejections. Contain failures only
in PluginDevWatcher fire-and-forget paths.

---------

Co-authored-by: OrcaWin <alpha-eng@stably.ai>
Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
Co-authored-by: Orca <help@stably.ai>
2026-08-02 17:40:58 -07:00

203 lines
7.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
buildSetupRunnerCommand,
getSetupRunnerCommandPlatformForPath,
nativeWindowsPathToPosixShellPath,
resolveSetupRunnerCommand
} from './setup-runner-command'
describe('buildSetupRunnerCommand', () => {
it('uses bash for WSL UNC runner scripts regardless of host casing', () => {
expect(
buildSetupRunnerCommand(
'\\\\WSL.LOCALHOST\\Ubuntu\\home\\jin\\repo\\.git\\worktrees\\feature\\orca\\setup-runner.sh',
'windows'
)
).toBe('bash /home/jin/repo/.git/worktrees/feature/orca/setup-runner.sh')
})
it('uses bash with Linux paths for forward-slash WSL UNC runner scripts', () => {
expect(
buildSetupRunnerCommand(
'//wsl.localhost/Ubuntu/home/jin/repo/.git/worktrees/feature/orca/setup-runner.sh',
'windows'
)
).toBe('bash /home/jin/repo/.git/worktrees/feature/orca/setup-runner.sh')
})
it('keeps generic forward-slash UNC runner scripts on cmd.exe', () => {
expect(
buildSetupRunnerCommand('//server/share/repo/.git/orca/setup-runner.cmd', 'windows')
).toBe('cmd.exe /c "//server/share/repo/.git/orca/setup-runner.cmd"')
})
it('uses POSIX launch semantics for native Windows runners when the setup shell is POSIX', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix'
})
).toBe('bash /c/repo/.git/orca/setup-runner.sh')
})
it('uses the active WSL shell with WSL paths for native Windows POSIX runners', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix',
executable: 'wsl.exe'
})
).toBe('bash /mnt/c/repo/.git/orca/setup-runner.sh')
})
it('keeps cmd.exe launch semantics for cmd setup runners', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.cmd', 'windows', {
family: 'cmd'
})
).toBe('cmd.exe /c "C:\\repo\\.git\\orca\\setup-runner.cmd"')
})
it('infers generated POSIX runner shell semantics from extension when metadata is absent', () => {
expect(buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows')).toBe(
'bash /c/repo/.git/orca/setup-runner.sh'
)
})
})
describe('buildSetupRunnerCommand cmd metacharacter guard', () => {
const cmdRunner = (segment: string) => `C:\\repo${segment}\\.git\\orca\\setup-runner.cmd`
const decodePowerShellCommand = (command: string): string => {
const encoded = command.match(/-EncodedCommand (\S+)$/)?.[1]
expect(encoded).toBeTruthy()
const bytes = atob(encoded as string)
let decoded = ''
for (let index = 0; index < bytes.length; index += 2) {
decoded += String.fromCharCode(bytes.charCodeAt(index) | (bytes.charCodeAt(index + 1) << 8))
}
return decoded
}
it.each(['%', '&', '|', '<', '>', '^', '(', ')', '!', ',', ';', '=', '$', '`'])(
'hardens the launch when the runner path contains %s',
(character) => {
const command = buildSetupRunnerCommand(cmdRunner(`\\a${character}b`), 'windows', {
family: 'cmd'
})
expect(command).toMatch(
/^powershell\.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand [A-Za-z0-9+/=]+$/
)
}
)
it.each([
['plain', 'C:\\repo\\.git\\orca\\setup-runner.cmd'],
['spaces', 'C:\\Program Files\\repo\\.git\\orca\\setup-runner.cmd'],
['single quote', "C:\\o'brien\\.git\\orca\\setup-runner.cmd"],
['brackets and dash', 'C:\\repo-[2]\\.git\\orca\\setup-runner.cmd']
])('keeps the plain cmd launch for a %s path', (_label, runnerScriptPath) => {
expect(buildSetupRunnerCommand(runnerScriptPath, 'windows', { family: 'cmd' })).toBe(
`cmd.exe /c "${runnerScriptPath}"`
)
})
it('passes the runner path through the environment rather than the cmd argument string', () => {
const runnerScriptPath = cmdRunner('\\100%%\\a&b')
const script = decodePowerShellCommand(
buildSetupRunnerCommand(runnerScriptPath, 'windows', { family: 'cmd' })
)
expect(script).toContain(`$runner = '${runnerScriptPath}'`)
expect(script).toContain('$processInfo.EnvironmentVariables["ORCA_SETUP_RUNNER"] = $runner')
expect(script).toContain('/d /s /v:on /c ""!ORCA_SETUP_RUNNER!""')
// Why: the whole point of the guard is that the hostile path never reaches cmd as syntax.
expect(script).not.toContain(`/c ""${runnerScriptPath}""`)
expect(script).toContain('$processInfo.UseShellExecute = $false')
})
it('escapes single quotes when embedding the path in the PowerShell literal', () => {
const script = decodePowerShellCommand(
buildSetupRunnerCommand("C:\\o'brien&co\\.git\\orca\\setup-runner.cmd", 'windows', {
family: 'cmd'
})
)
expect(script).toContain("$runner = 'C:\\o''brien&co\\.git\\orca\\setup-runner.cmd'")
})
it('leaves runnerScriptPathForShell untouched so marker paths keep the native form', () => {
const runnerScriptPath = cmdRunner('\\a&b')
expect(resolveSetupRunnerCommand(runnerScriptPath, 'windows', { family: 'cmd' })).toMatchObject(
{
runnerScriptPathForShell: runnerScriptPath,
shell: 'windows'
}
)
})
it.each([
['native POSIX runner', 'C:\\repo\\a&b\\.git\\orca\\setup-runner.sh', undefined],
['WSL UNC runner', '\\\\wsl.localhost\\Ubuntu\\home\\a&b\\orca\\setup-runner.sh', undefined]
])('does not disturb the %s launch', (_label, runnerScriptPath) => {
expect(buildSetupRunnerCommand(runnerScriptPath, 'windows')).toMatch(/^bash /)
})
it('does not disturb the wsl.exe POSIX launch', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\a&b\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix',
executable: 'wsl.exe'
})
).toBe("bash '/mnt/c/repo/a&b/.git/orca/setup-runner.sh'")
})
})
describe('nativeWindowsPathToPosixShellPath', () => {
it('converts a drive path to the MSYS form Git Bash uses', () => {
expect(nativeWindowsPathToPosixShellPath('C:\\Users\\jin\\repo')).toBe('/c/Users/jin/repo')
})
it('is idempotent, so a double-applied conversion cannot corrupt a value', () => {
const once = nativeWindowsPathToPosixShellPath('D:\\repo\\worktrees\\feature')
expect(nativeWindowsPathToPosixShellPath(once)).toBe(once)
})
})
describe('getSetupRunnerCommandPlatformForPath', () => {
it('prefers POSIX for absolute POSIX runner paths even from Windows clients', () => {
expect(
getSetupRunnerCommandPlatformForPath('/remote/repo/.git/orca/setup-runner.sh', 'windows')
).toBe('posix')
})
it('prefers Windows for native Windows runner paths even from POSIX clients', () => {
expect(
getSetupRunnerCommandPlatformForPath('C:\\repo\\.git\\orca\\setup-runner.cmd', 'posix')
).toBe('windows')
})
it('keeps WSL UNC paths on the Windows resolver so they can be converted', () => {
expect(
getSetupRunnerCommandPlatformForPath(
'\\\\wsl.localhost\\Ubuntu\\home\\jin\\repo\\.git\\orca\\setup-runner.sh',
'posix'
)
).toBe('windows')
})
it('keeps forward-slash UNC paths on the Windows resolver', () => {
expect(
getSetupRunnerCommandPlatformForPath(
'//wsl.localhost/Ubuntu/home/jin/repo/.git/orca/setup-runner.sh',
'posix'
)
).toBe('windows')
expect(
getSetupRunnerCommandPlatformForPath(
'//server/share/repo/.git/orca/setup-runner.cmd',
'posix'
)
).toBe('windows')
})
})