Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.8 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { existsSync, writeFileSync } from 'node:fs'
import { getCmdExePath } from './windows-batch-spawn'
import { buildWindowsHostInteractiveLoginSpawn } from './windows-interactive-login-spawn'

function withWindows<T>(fn: () => T): T {
  const platform = Object.getOwnPropertyDescriptor(process, 'platform')!
  Object.defineProperty(process, 'platform', { configurable: true, value: 'win32' })
  try {
    return fn()
  } finally {
    Object.defineProperty(process, 'platform', platform)
  }
}

function encodedValue(value: string): string {
  return `Read-OrcaValue '${Buffer.from(value).toString('base64')}'`
}

/** Positional-independent so the argv shape can change without silently reading the wrong slot. */
function decodedScript(args: string[]): string {
  const payload = args[args.indexOf('-EncodedCommand') + 1] ?? ''
  return Buffer.from(payload, 'base64').toString('utf16le')
}

function pidFilePathFromSpawnArgs(args: string[]): string {
  const script = decodedScript(args)
  const encodedPath = script.match(
    /WriteAllText\(\(Read-OrcaValue '([^']+)'\), \[string\]\$PID\)/
  )?.[1]
  if (!encodedPath) {
    throw new Error('PID relay path is missing')
  }
  return Buffer.from(encodedPath, 'base64').toString('utf8')
}

describe('buildWindowsHostInteractiveLoginSpawn', () => {
  it('relays a batch login through a visible, PID-addressable console', () => {
    const spawn = withWindows(() =>
      buildWindowsHostInteractiveLoginSpawn('C:\\Tools\\claude.cmd', [
        'auth',
        'login',
        '--claudeai'
      ])
    )
    expect(spawn.command).toBe(getCmdExePath())
    expect(spawn.args.slice(0, 5)).toEqual(['/d', '/c', 'start', '', '/wait'])
    expect(spawn.args[5]).toMatch(/WindowsPowerShell\\v1\.0\\powershell\.exe$/i)
    // Omit noisy no-op Bypass: EncodedCommand is not policy-gated. Keep base64 because start reparses via cmd, whose guard rejects raw script metacharacters.
    expect(spawn.args.slice(6, 9)).toEqual(['-NoLogo', '-NoProfile', '-EncodedCommand'])
    expect(spawn.args).not.toContain('-ExecutionPolicy')
    expect(spawn.args).not.toContain('Bypass')

    const script = decodedScript(spawn.args)
    expect(script).toContain('[string]$PID')
    expect(script).toContain(encodedValue(getCmdExePath()))
    expect(script).toContain(encodedValue('C:\\Tools\\claude.cmd'))
    expect(script).toContain(encodedValue('--claudeai'))
    const pidFilePath = pidFilePathFromSpawnArgs(spawn.args)

    expect(spawn.stdio).toBe('ignore')
    expect(spawn.windowsHide).toBe(true)
    expect(spawn.getTerminationPid()).toBeNull()
    writeFileSync(pidFilePath, '2468')
    expect(spawn.getTerminationPid()).toBe(2468)
    spawn.cleanup()
    expect(existsSync(pidFilePath)).toBe(false)
  })

  it('routes executable logins through the same waiting console boundary', () => {
    const spawn = withWindows(() =>
      buildWindowsHostInteractiveLoginSpawn('C:\\Tools\\codex.exe', ['login'])
    )
    const script = decodedScript(spawn.args)
    expect(script).toContain(encodedValue('C:\\Tools\\codex.exe'))
    expect(script).toContain(encodedValue('login'))
    spawn.cleanup()
  })

  it('waits for the relay PID when cancellation races console startup', async () => {
    vi.useFakeTimers()
    const spawn = withWindows(() =>
      buildWindowsHostInteractiveLoginSpawn('C:\\Tools\\claude.exe', ['auth', 'login'])
    )
    try {
      const pendingPid = spawn.waitForTerminationPid()
      expect(spawn.getTerminationPid()).toBeNull()

      await vi.advanceTimersByTimeAsync(100)
      const pidFilePath = pidFilePathFromSpawnArgs(spawn.args)
      writeFileSync(pidFilePath, '8642')
      await vi.advanceTimersByTimeAsync(25)

      await expect(pendingPid).resolves.toBe(8642)
    } finally {
      spawn.cleanup()
      vi.useRealTimers()
    }
  })
})