/* eslint-disable max-lines -- Why: cli-installer covers darwin/linux/win32 install, remove, fallback, and privileged-runner paths; each platform combination requires its own fixture and assertions to catch regressions. */ import { chmod, lstat, mkdtemp, mkdir, readFile, readlink, symlink, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const execFileMock = vi.hoisted(() => vi.fn()) vi.mock('electron', () => ({ app: { isPackaged: false, getPath: () => tmpdir(), getAppPath: () => tmpdir() } })) vi.mock('node:child_process', () => ({ execFile: execFileMock })) import { CliInstaller } from './cli-installer' import { buildAppImageCliWrapper } from './appimage-cli-wrapper' async function makeFixture(): Promise<{ root: string userDataPath: string appPath: string }> { const root = await mkdtemp(join(tmpdir(), 'orca-cli-installer-')) const userDataPath = join(root, 'userData') const appPath = join(root, 'app') const cliEntryPath = join(appPath, 'out', 'cli', 'index.js') await mkdir(join(appPath, 'out', 'cli'), { recursive: true }) await writeFile(cliEntryPath, 'console.log("orca")\n', 'utf8') return { root, userDataPath, appPath } } describe('CliInstaller', () => { beforeEach(() => { execFileMock.mockReset() }) afterEach(() => { vi.useRealTimers() vi.restoreAllMocks() }) // Why: this test creates Unix symlinks and shell scripts that only apply on macOS. it.skipIf(process.platform === 'win32')( 'creates a dev launcher and installs a macOS symlink in the requested path', async () => { const fixture = await makeFixture() const installPath = join(fixture.root, 'bin', 'orca') const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, commandPathOverride: installPath, processPathEnv: join(fixture.root, 'bin') }) const initial = await installer.getStatus() expect(initial.state).toBe('not_installed') expect(initial.launcherPath).toContain(join('userData', 'cli', 'bin', 'orca')) const installed = await installer.install() expect(installed.state).toBe('installed') expect(installed.pathConfigured).toBe(true) const launcherContent = await readFile(installed.launcherPath as string, 'utf8') expect(launcherContent).toContain('ELECTRON_RUN_AS_NODE=1') expect(launcherContent).toContain(join(fixture.appPath, 'out', 'cli', 'index.js')) const removed = await installer.remove() expect(removed.state).toBe('not_installed') } ) // Why: this test creates Unix symlinks and shell scripts that only apply on Linux. it.skipIf(process.platform === 'win32')( 'creates a linux symlink under the requested path and warns when PATH is missing', async () => { const fixture = await makeFixture() const installPath = join(fixture.root, '.local', 'bin', 'orca-ide') const installer = new CliInstaller({ platform: 'linux', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/opt/Orca/orca-ide', appPath: fixture.appPath, commandPathOverride: installPath, processPathEnv: '/usr/bin' }) const installed = await installer.install() expect(installed.state).toBe('installed') expect(installed.commandName).toBe('orca-ide') expect(installed.pathConfigured).toBe(false) expect(installed.detail).toContain('.local') const launcherContent = await readFile(installed.launcherPath as string, 'utf8') expect(launcherContent).toContain('ELECTRON_RUN_AS_NODE=1') const removed = await installer.remove() expect(removed.state).toBe('not_installed') } ) // Why: AppImage resources live under a per-launch FUSE mount, so the // installed shell command must be a stable wrapper rather than a symlink. it.skipIf(process.platform === 'win32')( 'creates an AppImage wrapper under the linux command path', async () => { const fixture = await makeFixture() const commandDir = join(fixture.root, '.local', 'bin') const installPath = join(commandDir, 'orca-ide') const appImagePath = join(fixture.root, 'Orca.AppImage') await writeFile(appImagePath, '#!/usr/bin/env bash\n', { encoding: 'utf8', mode: 0o755 }) const installer = new CliInstaller({ platform: 'linux', isPackaged: true, appImagePath, commandPathOverride: installPath, processPathEnv: commandDir }) const initial = await installer.getStatus() expect(initial).toMatchObject({ state: 'not_installed', installMethod: 'wrapper', launcherPath: appImagePath }) const installed = await installer.install() expect(installed).toMatchObject({ state: 'installed', commandName: 'orca-ide', installMethod: 'wrapper', launcherPath: appImagePath, currentTarget: appImagePath, pathConfigured: true }) const commandStats = await lstat(installPath) expect(commandStats.isFile()).toBe(true) expect(commandStats.mode & 0o111).not.toBe(0) await expect(readlink(installPath)).rejects.toMatchObject({ code: 'EINVAL' }) await expect(readFile(installPath, 'utf8')).resolves.toBe( buildAppImageCliWrapper(appImagePath) ) const removed = await installer.remove() expect(removed.state).toBe('not_installed') } ) it.skipIf(process.platform === 'win32')( 'reports a stale AppImage wrapper when the AppImage path changes', async () => { const fixture = await makeFixture() const commandDir = join(fixture.root, '.local', 'bin') const installPath = join(commandDir, 'orca-ide') const oldAppImagePath = join(fixture.root, 'Old-Orca.AppImage') const newAppImagePath = join(fixture.root, 'Orca.AppImage') await mkdir(commandDir, { recursive: true }) await writeFile(installPath, buildAppImageCliWrapper(oldAppImagePath), { encoding: 'utf8', mode: 0o755 }) await writeFile(newAppImagePath, '#!/usr/bin/env bash\n', { encoding: 'utf8', mode: 0o755 }) const installer = new CliInstaller({ platform: 'linux', isPackaged: true, appImagePath: newAppImagePath, commandPathOverride: installPath, processPathEnv: commandDir }) await expect(installer.getStatus()).resolves.toMatchObject({ state: 'stale', installMethod: 'wrapper', currentTarget: newAppImagePath }) await expect(installer.install()).resolves.toMatchObject({ state: 'installed' }) await expect(readFile(installPath, 'utf8')).resolves.toBe( buildAppImageCliWrapper(newAppImagePath) ) } ) // Why: Linux renamed the public command to avoid shadowing GNOME Orca, so // upgrading must clean up only the old symlink owned by prior Orca installs. it.skipIf(process.platform === 'win32')( 'removes the old managed linux orca symlink when installing orca-ide', async () => { const fixture = await makeFixture() const homePath = join(fixture.root, 'home') const commandDir = join(homePath, '.local', 'bin') const oldLauncherPath = join(fixture.userDataPath, 'cli', 'bin', 'orca') const legacyCommandPath = join(commandDir, 'orca') await mkdir(commandDir, { recursive: true }) await mkdir(join(fixture.userDataPath, 'cli', 'bin'), { recursive: true }) await writeFile(oldLauncherPath, '#!/usr/bin/env bash\n', 'utf8') await symlink(oldLauncherPath, legacyCommandPath) const installer = new CliInstaller({ platform: 'linux', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/opt/Orca/orca-ide', appPath: fixture.appPath, homePath, processPathEnv: commandDir }) const installed = await installer.install() expect(installed.commandPath).toBe(join(commandDir, 'orca-ide')) await expect(lstat(legacyCommandPath)).rejects.toMatchObject({ code: 'ENOENT' }) } ) it.skipIf(process.platform === 'win32')( 'removes a legacy linux orca symlink when installing an AppImage wrapper', async () => { const fixture = await makeFixture() const homePath = join(fixture.root, 'home') const commandDir = join(homePath, '.local', 'bin') const legacyCommandPath = join(commandDir, 'orca') const appImagePath = join(fixture.root, 'Orca.AppImage') await mkdir(commandDir, { recursive: true }) await writeFile(appImagePath, '#!/usr/bin/env bash\n', { encoding: 'utf8', mode: 0o755 }) await symlink(join('/tmp', '.mount_Orca1234', 'resources', 'bin', 'orca'), legacyCommandPath) const installer = new CliInstaller({ platform: 'linux', isPackaged: true, appImagePath, homePath, processPathEnv: commandDir }) const installed = await installer.install() expect(installed.commandPath).toBe(join(commandDir, 'orca-ide')) await expect(lstat(legacyCommandPath)).rejects.toMatchObject({ code: 'ENOENT' }) } ) it('creates a windows wrapper and updates the user PATH', async () => { const fixture = await makeFixture() const installPath = join(fixture.root, 'Programs', 'Orca', 'bin', 'orca.cmd') let userPath = 'C:\\Windows\\System32' const installer = new CliInstaller({ platform: 'win32', isPackaged: false, userDataPath: fixture.userDataPath, execPath: 'C:\\Users\\me\\AppData\\Local\\Orca\\Orca.exe', appPath: fixture.appPath, commandPathOverride: installPath, userPathReader: async () => userPath, userPathWriter: async (value) => { userPath = value } }) const installed = await installer.install() expect(installed.state).toBe('installed') expect(installed.pathConfigured).toBe(true) expect(userPath).toContain(join(fixture.root, 'Programs', 'Orca', 'bin')) const wrapperContent = await readFile(installPath, 'utf8') expect(wrapperContent).toContain('ORCA_LAUNCHER=') expect(wrapperContent).toContain('orca.cmd') const removed = await installer.remove() expect(removed.state).toBe('not_installed') expect(userPath).not.toContain(join(fixture.root, 'Programs', 'Orca', 'bin')) }) it('settles when the Windows PATH query hangs', async () => { vi.useFakeTimers() const fixture = await makeFixture() const installPath = join(fixture.root, 'Programs', 'Orca', 'bin', 'orca.cmd') const killMock = vi.fn() execFileMock.mockImplementation(() => ({ kill: killMock })) const installer = new CliInstaller({ platform: 'win32', isPackaged: false, userDataPath: fixture.userDataPath, execPath: 'C:\\Users\\me\\AppData\\Local\\Orca\\Orca.exe', appPath: fixture.appPath, commandPathOverride: installPath }) const promise = installer.getStatus() let settled = false void promise .catch(() => undefined) .finally(() => { settled = true }) await vi.waitFor(() => expect(execFileMock).toHaveBeenCalled()) await vi.advanceTimersByTimeAsync(5_000) await Promise.resolve() expect(settled).toBe(true) await expect(promise).rejects.toThrow('Windows PATH command timed out') expect(killMock).toHaveBeenCalled() }) // Why: this test creates a Unix symlink to /tmp/not-orca, which only applies on macOS/Linux. it.skipIf(process.platform === 'win32')( 'refuses to replace an unknown symlink at the command path', async () => { const fixture = await makeFixture() const installPath = join(fixture.root, 'bin', 'orca') const existingTarget = '/tmp/not-orca' await mkdir(join(fixture.root, 'bin'), { recursive: true }) await symlink(existingTarget, installPath) const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, commandPathOverride: installPath }) await expect(installer.getStatus()).resolves.toMatchObject({ state: 'conflict', supported: true }) await expect(installer.install()).rejects.toThrow('Refusing to replace non-Orca command') await expect(readlink(installPath)).resolves.toBe(existingTarget) } ) // Why: packaged app moves can leave a symlink to an older Orca-owned launcher; // those are safe to refresh, unlike arbitrary user symlinks. it.skipIf(process.platform === 'win32')( 'replaces stale packaged Orca launcher symlinks', async () => { const fixture = await makeFixture() const commandDir = join(fixture.root, 'bin') const installPath = join(commandDir, 'orca') const resourcesPath = join(fixture.root, 'Current.app', 'Contents', 'Resources') const launcherPath = join(resourcesPath, 'bin', 'orca') const oldLauncherPath = join(fixture.root, 'Old.app', 'Contents', 'Resources', 'bin', 'orca') await mkdir(commandDir, { recursive: true }) await mkdir(join(resourcesPath, 'bin'), { recursive: true }) await writeFile(launcherPath, '#!/usr/bin/env bash\n', 'utf8') await symlink(oldLauncherPath, installPath) const installer = new CliInstaller({ platform: 'darwin', isPackaged: true, resourcesPath, commandPathOverride: installPath, processPathEnv: commandDir }) await expect(installer.getStatus()).resolves.toMatchObject({ state: 'stale', currentTarget: oldLauncherPath }) await expect(installer.install()).resolves.toMatchObject({ state: 'installed' }) await expect(readlink(installPath)).resolves.toBe(launcherPath) } ) // Why: on Apple Silicon, /usr/local/bin does not exist by default. The installer // must fall back to ~/.local/bin (user-writable, no sudo) rather than failing // silently when the parent directory is absent. it.skipIf(process.platform === 'win32')( 'falls back to ~/.local/bin/orca on macOS when /usr/local/bin does not exist', async () => { const fixture = await makeFixture() const homePath = join(fixture.root, 'home') // Simulate arm64: point defaultMacCommandPath at a dir that does not exist // in the fixture so existsSync(dirname(...)) returns false. const absentUsrLocalBin = join(fixture.root, 'usr', 'local', 'bin', 'orca') const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, homePath, defaultMacCommandPath: absentUsrLocalBin, processPathEnv: join(homePath, '.local', 'bin') }) const status = await installer.getStatus() expect(status.commandPath).toBe(join(homePath, '.local', 'bin', 'orca')) expect(status.state).toBe('not_installed') expect(status.supported).toBe(true) const installed = await installer.install() expect(installed.state).toBe('installed') expect(installed.commandPath).toBe(join(homePath, '.local', 'bin', 'orca')) expect(installed.pathConfigured).toBe(true) } ) // Why: on Intel Macs /usr/local/bin exists, so the installer must keep using // it as the canonical path and not regress to ~/.local/bin. it.skipIf(process.platform === 'win32')( 'uses /usr/local/bin/orca on macOS when /usr/local/bin exists', async () => { const fixture = await makeFixture() const usrLocalBin = join(fixture.root, 'usr', 'local', 'bin') await mkdir(usrLocalBin, { recursive: true }) const installPath = join(usrLocalBin, 'orca') const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, defaultMacCommandPath: installPath, processPathEnv: usrLocalBin }) const installed = await installer.install() expect(installed.state).toBe('installed') expect(installed.commandPath).toBe(installPath) expect(installed.pathConfigured).toBe(true) } ) // Why: when macCommandPath falls back to ~/.local/bin/orca on arm64, commandName // must still be 'orca' (not 'orca-ide' which is Linux-only). it.skipIf(process.platform === 'win32')( 'reports commandName as orca (not orca-ide) when falling back to ~/.local/bin on macOS', async () => { const fixture = await makeFixture() const homePath = join(fixture.root, 'home') const absentUsrLocalBin = join(fixture.root, 'usr', 'local', 'bin', 'orca') const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, homePath, defaultMacCommandPath: absentUsrLocalBin, processPathEnv: join(homePath, '.local', 'bin') }) const status = await installer.getStatus() expect(status.commandName).toBe('orca') } ) // Why: the privilegedRunner is injectable so the EACCES→osascript path can be // exercised in integration without spawning osascript in unit tests. it.skipIf(process.platform === 'win32' || process.getuid?.() === 0)( 'invokes the injected privilegedRunner when install falls back to elevated permissions', async () => { const fixture = await makeFixture() const protectedDir = join(fixture.root, 'protected') await mkdir(protectedDir) await chmod(protectedDir, 0o500) const installPath = join(protectedDir, 'bin', 'orca') const privilegedCommands: string[] = [] const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, commandPathOverride: installPath, privilegedRunner: async (command: string) => { privilegedCommands.push(command) await chmod(protectedDir, 0o700) const launcherPath = (await installer.getStatus()).launcherPath as string await mkdir(dirname(installPath), { recursive: true }) await symlink(launcherPath, installPath) }, processPathEnv: dirname(installPath) }) try { const installed = await installer.install() expect(installed.state).toBe('installed') expect(installed.pathConfigured).toBe(true) expect(privilegedCommands).toHaveLength(1) expect(privilegedCommands[0]).toContain('mkdir -p') expect(privilegedCommands[0]).toContain('ln -sfn') await expect(readlink(installPath)).resolves.toBe(installed.launcherPath) } finally { await chmod(protectedDir, 0o700).catch(() => undefined) } } ) // Why: macCommandPath is resolved at construction — getStatus() must return the // same commandPath on repeated calls without re-running existsSync. it.skipIf(process.platform === 'win32')( 'resolves macCommandPath once at construction — commandPath stable across repeated getStatus()', async () => { const fixture = await makeFixture() const homePath = join(fixture.root, 'home') const absentUsrLocalBin = join(fixture.root, 'usr', 'local', 'bin', 'orca') const installer = new CliInstaller({ platform: 'darwin', isPackaged: false, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, homePath, defaultMacCommandPath: absentUsrLocalBin, processPathEnv: join(homePath, '.local', 'bin') }) const s1 = await installer.getStatus() await mkdir(dirname(absentUsrLocalBin), { recursive: true }) const s2 = await installer.getStatus() const s3 = await installer.getStatus() expect(s1.commandPath).toBe(s2.commandPath) expect(s2.commandPath).toBe(s3.commandPath) expect(s1.commandPath).toBe(join(homePath, '.local', 'bin', 'orca')) } ) // Why: the arm64 fallback must apply for packaged builds, not just dev launchers. it.skipIf(process.platform === 'win32')( 'resolves to ~/.local/bin/orca on arm64 even when isPackaged is true', async () => { const fixture = await makeFixture() const homePath = join(fixture.root, 'home') const absentUsrLocalBin = join(fixture.root, 'usr', 'local', 'bin', 'orca') const resourcesPath = join(fixture.root, 'resources') const bundledLauncher = join(resourcesPath, 'bin', 'orca') await mkdir(join(resourcesPath, 'bin'), { recursive: true }) await writeFile(bundledLauncher, '#!/usr/bin/env bash\necho orca\n', { encoding: 'utf8', mode: 0o755 }) const installer = new CliInstaller({ platform: 'darwin', isPackaged: true, resourcesPath, userDataPath: fixture.userDataPath, execPath: '/Applications/Orca.app/Contents/MacOS/Orca', appPath: fixture.appPath, homePath, defaultMacCommandPath: absentUsrLocalBin, processPathEnv: join(homePath, '.local', 'bin') }) const status = await installer.getStatus() expect(status.commandPath).toBe(join(homePath, '.local', 'bin', 'orca')) expect(status.supported).toBe(true) } ) })