Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/main/agent-hooks/windows-direct-spawn-launcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import type * as Os from 'node:os'
import { join } from 'node:path'
import type * as InstallerUtils from './installer-utils'

const { getPathMock, homedirMock } = vi.hoisted(() => ({
getPathMock: vi.fn<(name: string) => string>(),
homedirMock: vi.fn<() => string>()
}))

vi.mock('electron', () => ({ app: { getPath: getPathMock } }))

vi.mock('os', async (importOriginal) => {
const actual = await importOriginal<typeof Os>()
return { ...actual, homedir: homedirMock }
})

// Why: stubbing only the path source keeps the real wrappers in play (so a wrapper swap still
// shows up) and pins a Windows path, letting this assert win32 behavior on ubuntu CI.
const WINDOWS_HOOK_DIR = 'C:\\Users\\alice\\.orca\\agent-hooks'

vi.mock('./installer-utils', async (importOriginal) => {
const actual = await importOriginal<typeof InstallerUtils>()
return {
...actual,
getSharedManagedScriptPath: (scriptFileName: string) =>
`${WINDOWS_HOOK_DIR}\\${scriptFileName}`,
writeManagedScript: vi.fn()
}
})

import { AntigravityHookService } from '../antigravity/hook-service'
import { cursorHookService } from '../cursor/hook-service'
import { getCodexManagedHookInstallMaterial } from '../codex/hook-service'
import { getDevinManagedCommand, getDevinManagedScriptPath } from '../devin/hook-settings'

const BARE_WINDOWS_CMD_LAUNCHER = /^C:\\Users\\alice\\\.orca\\agent-hooks\\[A-Za-z0-9._-]+\.cmd$/
const ENCODED_POWERSHELL_LAUNCHER =
/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/

function withPlatform<T>(platform: NodeJS.Platform, run: () => T): T {
const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')
Object.defineProperty(process, 'platform', { configurable: true, value: platform })
try {
return run()
} finally {
if (originalPlatform) {
Object.defineProperty(process, 'platform', originalPlatform)
}
}
}

// Why: these agents hand the command to CreateProcess as argv[0], not to cmd.exe, so anything
// but a single token naming a real .cmd is unspawnable and exits every hook 1 (#8430).
function expectDirectlySpawnable(command: string, label: string): void {
expect(command, `${label}: launcher must be the bare managed .cmd path`).toMatch(
BARE_WINDOWS_CMD_LAUNCHER
)
expect(command.split(' '), `${label}: launcher must be a single argv[0] token`).toHaveLength(1)
expect(command, `${label}: launcher must not be a shell launcher`).not.toMatch(/powershell/i)
}

function readCursorCommand(homeDir: string): string {
const config = JSON.parse(readFileSync(join(homeDir, '.cursor', 'hooks.json'), 'utf8')) as {
hooks: Record<string, { command?: string }[]>
}
return config.hooks.stop?.[0]?.command ?? ''
}

function readAntigravityCommands(homeDir: string): string[] {
const config = JSON.parse(
readFileSync(join(homeDir, '.gemini', 'config', 'hooks.json'), 'utf8')
) as {
'orca-status': Record<string, { command?: string; hooks?: { command?: string }[] }[]>
}
return Object.values(config['orca-status']).flatMap((definitions) =>
definitions.map((definition) => definition.command ?? definition.hooks?.[0]?.command ?? '')
)
}

describe('Windows direct-spawn hook launcher contract', () => {
let homeDir: string

beforeEach(() => {
homeDir = mkdtempSync(join(tmpdir(), 'orca-direct-spawn-home-'))
homedirMock.mockReturnValue(homeDir)
getPathMock.mockReturnValue(homeDir)
})

afterEach(() => {
vi.clearAllMocks()
rmSync(homeDir, { recursive: true, force: true })
})

it('keeps the Codex managed command directly spawnable', () => {
const { command } = withPlatform('win32', () => getCodexManagedHookInstallMaterial())
expectDirectlySpawnable(command, 'codex')
})

it('keeps the Devin managed command directly spawnable', () => {
const command = withPlatform('win32', () => getDevinManagedCommand(getDevinManagedScriptPath()))
expectDirectlySpawnable(command, 'devin')
})

it('keeps every Antigravity managed command directly spawnable', () => {
withPlatform('win32', () => new AntigravityHookService().install())
const commands = readAntigravityCommands(homeDir)
expect(commands.length).toBeGreaterThan(0)
Comment on lines +107 to +110

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Seed the Antigravity hooks configuration before installation.

This test creates only homeDir; AntigravityHookService.install() returns an error when .gemini/config/hooks.json is missing, so readAntigravityCommands() then throws while reading a nonexistent file. Create a minimal valid configuration and assert the install status is installed before reading the generated commands.

Based on the supplied AntigravityHookService.install() implementation, missing configuration returns an error.

for (const command of commands) {
expectDirectlySpawnable(command, 'antigravity')
}
})

// Why: pins the split — Cursor's runner goes through a shell, so it must keep the encoded
// launcher. Without this, "make every agent spawnable" would look like a valid refactor.
it('keeps a shell-launched agent on the encoded PowerShell launcher', () => {
withPlatform('win32', () => cursorHookService.install())
const command = readCursorCommand(homeDir)
expect(command).toMatch(ENCODED_POWERSHELL_LAUNCHER)
expect(command).not.toMatch(BARE_WINDOWS_CMD_LAUNCHER)
})
})
Loading